SQLiteC++  0.5.0
SQLiteC++ is a smart and easy to use C++ SQLite3 wrapper.
 All Classes Namespaces Files Functions Friends Macros
Column.h
Go to the documentation of this file.
1 /**
2  * @file Column.h
3  * @brief Encapsulation of a Column in a row of the result pointed by the prepared SQLite::Statement.
4  *
5  * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6  *
7  * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
8  * or copy at http://opensource.org/licenses/MIT)
9  */
10 #pragma once
11 
12 #include <sqlite3.h>
13 #include "Exception.h"
14 #include "Statement.h"
15 
16 namespace SQLite
17 {
18 
19 
20 /**
21  * @brief Encapsulation of a Column in a row of the result pointed by the prepared Statement.
22  *
23  * A Column is a particular field of SQLite data in the current row of result
24  * of the Statement : it points to a single cell.
25  *
26  * Its value can be expressed as a text, and, when applicable, as a numeric
27  * (integer or floting point) or a binary blob.
28  */
29 class Column
30 {
31 public:
32  /**
33  * @brief Encapsulation of a Column in a Row of the result.
34  *
35  * @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object.
36  * @param[in] aIndex Index of the column in the row of result
37  */
38  Column(Statement::Ptr& aStmtPtr, int aIndex) throw(); // nothrow
39  /// @brief Simple destructor
40  virtual ~Column(void) throw(); // nothrow
41 
42  // default copy constructor and assignment operator are perfectly suited :
43  // they copy the Statement::Ptr which in turn increments the reference counter.
44 
45  /// @brief Return the integer value of the column.
46  int getInt (void) const throw(); // nothrow
47  /// @brief Return the 64bits integer value of the column.
48  sqlite3_int64 getInt64 (void) const throw(); // nothrow
49  /// @brief Return the double (64bits float) value of the column.
50  double getDouble(void) const throw(); // nothrow
51  /**
52  * @brief Return a pointer to the text value (NULL terminated string) of the column.
53  *
54  * @warning The value pointed at is only valid while the statement is valid (ie. not finalized),
55  * thus you must copy it before using it beyond its scope (to a std::string for instance).
56  */
57  const char* getText (void) const throw(); // nothrow
58  /**
59  * @brief Return a pointer to the binary blob value of the column.
60  *
61  * @warning The value pointed at is only valid while the statement is valid (ie. not finalized),
62  * thus you must copy it before using it beyond its scope (to a std::string for instance).
63  */
64  const void* getBlob (void) const throw(); // nothrow
65 
66  /**
67  * @brief Return the type of the value of the column
68  *
69  * Return either SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL.
70  *
71  * @warning After a type conversion (by a call to a getXxx on a Column of a Yyy type),
72  * the value returned by sqlite3_column_type() is undefined.
73  */
74  int getType(void) const throw(); // nothrow
75 
76  /// @brief Test if the column is an integer type value (meaningfull only before any conversion)
77  inline bool isInteger(void) const throw() // nothrow
78  {
79  return (SQLITE_INTEGER == getType());
80  }
81  /// @brief Test if the column is a floting point type value (meaningfull only before any conversion)
82  inline bool isFloat(void) const throw() // nothrow
83  {
84  return (SQLITE_FLOAT == getType());
85  }
86  /// @brief Test if the column is a text type value (meaningfull only before any conversion)
87  inline bool isText(void) const throw() // nothrow
88  {
89  return (SQLITE_TEXT == getType());
90  }
91  /// @brief Test if the column is a binary blob type value (meaningfull only before any conversion)
92  inline bool isBlob(void) const throw() // nothrow
93  {
94  return (SQLITE_BLOB == getType());
95  }
96  /// @brief Test if the column is NULL (meaningfull only before any conversion)
97  inline bool isNull(void) const throw() // nothrow
98  {
99  return (SQLITE_NULL == getType());
100  }
101 
102  /**
103  * @brief Return the number of bytes used by the text (or blob) value of the column
104  *
105  * Return either :
106  * - size in bytes (not in characters) of the string returned by getText() without the '\0' terminator
107  * - size in bytes of the string representation of the numerical value (integer or double)
108  * - size in bytes of the binary blob returned by getBlob()
109  * - 0 for a NULL value
110  */
111  int getBytes(void) const throw();
112 
113  /// @brief Alias returning the number of bytes used by the text (or blob) value of the column
114  inline int size(void) const throw()
115  {
116  return getBytes ();
117  }
118 
119  /// @brief Inline cast operator to int
120  inline operator int() const
121  {
122  return getInt();
123  }
124  /// @brief Inline cast operator to 64bits integer
125  inline operator sqlite3_int64() const
126  {
127  return getInt64();
128  }
129  /// @brief Inline cast operator to double
130  inline operator double() const
131  {
132  return getDouble();
133  }
134  /**
135  * @brief Inline cast operator to char*
136  *
137  * @see getText
138  */
139  inline operator const char*() const
140  {
141  return getText();
142  }
143  /**
144  * @brief Inline cast operator to void*
145  *
146  * @see getBlob
147  */
148  inline operator const void*() const
149  {
150  return getBlob();
151  }
152 #ifdef __GNUC__
153  // NOTE : the following is required by GCC to cast a Column result in a std::string
154  // (error: conversion from ‘SQLite::Column’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested)
155  // but is not working under Microsoft Visual Studio 2010 and 2012
156  // (error C2440: 'initializing' : cannot convert from 'SQLite::Column' to 'std::basic_string<_Elem,_Traits,_Ax>'
157  // [...] constructor overload resolution was ambiguous)
158  /// Inline cast operator to std::string
159  inline operator const std::string() const
160  {
161  return getText();
162  }
163 #endif
164 
165  /// @brief Return UTF-8 encoded English language explanation of the most recent error.
166  inline const char* errmsg(void) const
167  {
168  return sqlite3_errmsg(mStmtPtr);
169  }
170 private:
171  Statement::Ptr mStmtPtr; //!< Shared Pointer to the prepared SQLite Statement Object
172  int mIndex; //!< Index of the column in the row of result
173 };
174 
175 /**
176  * @brief Standard std::ostream text inserter
177  *
178  * Insert the text value of the Column object, using getText(), into the provided stream.
179  *
180  * @param[in] aStream Stream to use
181  * @param[in] aColumn Column object to insert into the provided stream
182  *
183  * @return Reference to the stream used
184  */
185 std::ostream& operator<<(std::ostream& aStream, const Column& aColumn);
186 
187 } // namespace SQLite