libsqlite3x  2007.10.18
sqlite3x_cursor.cpp
1 /*
2  Copyright (C) 2004-2005 Cory Nelson
3 
4  This software is provided 'as-is', without any express or implied
5  warranty. In no event will the authors be held liable for any damages
6  arising from the use of this software.
7 
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it
10  freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16  2. Altered source versions must be plainly marked as such, and must not be
17  misrepresented as being the original software.
18  3. This notice may not be removed or altered from any source distribution.
19 
20 */
21 
22 #include <sqlite3.h>
23 #include "sqlite3x.hpp"
24 
25 namespace sqlite3x {
26 
28 
29  sqlite3_cursor::sqlite3_cursor(const sqlite3_cursor &copy) : cmd(copy.cmd) {
30  if(this->cmd) ++this->cmd->refs;
31  }
32 
34  ++this->cmd->refs;
35  }
36 
38  this->close();
39  }
40 
42  this->close();
43 
44  this->cmd=copy.cmd;
45  if(this->cmd) ++this->cmd->refs;
46 
47  return *this;
48  }
49 
51  {
52  if( ! this->cmd )
53  {
54  throw database_error("sqlite3_cursor::colcount(): reader is closed");
55  }
56  return this->cmd->colcount();
57  }
58 
60  if(!this->cmd) throw database_error("sqlite3_cursor::step(): reader is closed");
61 
62  switch(sqlite3_step(this->cmd->stmt)) {
63  case SQLITE_ROW:
64  return true;
65  case SQLITE_DONE:
66  return false;
67  default:
68  throw database_error(this->cmd->con);
69  }
70  }
71 
73  if(!this->cmd) throw database_error("sqlite3_cursor::reset(): reader is closed");
74 
75  if(! this->cmd->reset() )
76  {
77  throw database_error("sqlite3_cursor::reset() db error: %s", this->cmd->con.errormsg().c_str() );
78  }
79  }
80 
82  if(this->cmd) {
83  if(--this->cmd->refs==0) { sqlite3_reset(this->cmd->stmt); }
84  this->cmd=NULL;
85  }
86  }
87 
88 #define READER_CHECK(FUNC) \
89  if( ! this->cmd ) throw database_error( "sqlite3_cursor::%s(%d): reader is closed", # FUNC, index ); \
90  if( (index)>(this->cmd->argc-1)) throw database_error("sqlite3_cursor::%s(%d): index out of range", # FUNC, index );
91 
92  bool sqlite3_cursor::isnull(int index) {
93  READER_CHECK(isnull);
94  return sqlite3_column_type(this->cmd->stmt, index) == SQLITE_NULL;
95  }
96 
97  int sqlite3_cursor::getint(int index) {
98  READER_CHECK(getint);
99  return sqlite3_column_int(this->cmd->stmt, index);
100  }
101 
103  READER_CHECK(getint64);
104  return sqlite3_column_int64(this->cmd->stmt, index);
105  }
106 
107  double sqlite3_cursor::getdouble(int index) {
108  READER_CHECK(getdouble);
109  return sqlite3_column_double(this->cmd->stmt, index);
110  }
111 
112  std::string sqlite3_cursor::getstring(int index) {
113  READER_CHECK(string);
114  return std::string((const char*)sqlite3_column_text(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index));
115  }
116 
117  char const * sqlite3_cursor::getstring(int index, int & size) {
118  READER_CHECK(string);
119  size = sqlite3_column_bytes(this->cmd->stmt, index);
120  return (char const *)sqlite3_column_text(this->cmd->stmt, index);
121  }
122 
123 #if SQLITE3X_USE_WCHAR
124  std::wstring sqlite3_cursor::getstring16(int index) {
125  READER_CHECK(wstring);
126  return std::wstring((const wchar_t*)sqlite3_column_text16(this->cmd->stmt, index), sqlite3_column_bytes16(this->cmd->stmt, index)/2);
127  }
128 #endif
129 
130  std::string sqlite3_cursor::getblob(int index) {
131  READER_CHECK(string);
132  return std::string((const char*)sqlite3_column_blob(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index));
133  }
134 
135  void const * sqlite3_cursor::getblob(int index, int & size ) {
136  READER_CHECK(string);
137  size = sqlite3_column_bytes(this->cmd->stmt, index);
138  return sqlite3_column_blob(this->cmd->stmt, index);
139  }
140 
141  std::string sqlite3_cursor::getcolname(int index) {
142  READER_CHECK(string);
143  char const * cn = sqlite3_column_name(this->cmd->stmt, index);
144  return cn ? cn : "";
145  }
146 
147 // char const * sqlite3_cursor::getcolname(int index) {
148 // READER_CHECK(string);
149 // char const * cn = sqlite3_column_name(this->cmd->stmt, index);
150 // return cn ? cn : "";
151 // }
152 
153 #if SQLITE3X_USE_WCHAR
154  std::wstring sqlite3_cursor::getcolname16(int index) {
155  READER_CHECK(wstring);
156  return (const wchar_t*)sqlite3_column_name16(this->cmd->stmt, index);
157  }
158 #endif
159 
160 #undef READER_CHECK
161 }
sqlite3_cursor()
Creates an empty cursor object, suitable only for use as the target of a copy/assignment.
std::string getstring(int index)
Gets the string value at the given field number.
~sqlite3_cursor()
Closes this cursor, freeing up db resources if this is the last cursor of a copied set...
Encapsulates a command to send to an sqlite3_connection.
Definition: sqlite3x.hpp:592
A type for reading results from an sqlite3_command.
Definition: sqlite3x.hpp:458
This namespace encapsulates a C++ API wrapper for sqlite3 databases.
Definition: sqlite3x.hpp:120
sqlite3_cursor & operator=(const sqlite3_cursor &copy)
Copies the given cursor object.
std::string getblob(int index)
Gets the blob value at the given field number.
int colcount()
Returns the column count of this object&#39;s query, or throws on error.
sqlite_int64 int64_t
64-bit integer type used by this code.
Definition: sqlite3x.hpp:125
bool isnull(int index)
Check if the given field number is NULL.
void close()
Closes this cursor.
int colcount()
Returns the column count of the result set or throws on error.
bool reset()
Resets this statement using sqlite3_reset().
double getdouble(int index)
Gets the double value at the given field number.
std::string getcolname(int index)
Gets the column name for the given column index.
void reset()
Resets the underlying prepared statement of this cursor.
std::string errormsg() const
Returns the equivalent of sqlite3_errmsg(), or an empty string if that function returns null...
int64_t getint64(int index)
Gets the (int64_t) value at the given field number.
bool step()
Steps one step through the sql result set and returns true on SQLITE_ROW, false on SQLITE3_DONE...
Exception type used by the sqlite3x classes.
Definition: sqlite3x.hpp:777
int getint(int index)
Gets the integer value at the given field number.