4 ** Published / author: 2005-08-12 / grymse@alhem.net
8 Copyright (C) 2001-2006 Anders Hedstrom
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #pragma warning(disable:4786)
44 #ifdef SQLITEW_NAMESPACE
45 namespace SQLITEW_NAMESPACE
{
49 Query::Query(Database
& dbin
)
55 ,cache_rc_valid(false)
62 Query::Query(Database
& dbin
,const std::string
& sql
)
68 ,cache_rc_valid(false)
87 Database
& Query::GetDatabase() const
94 The sqlite3_finalize() routine deallocates a prepared SQL statement.
95 All prepared statements must be finalized before the database can be closed.
97 bool Query::execute(const std::string
& sql
)
100 Assert(!(odb
&& res
));
103 const char *s
= NULL
;
104 size_t rc
= sqlite3_prepare(odb
-> db
, sql
.c_str(), (int)sql
.size(), &res
, &s
);
106 throw std::runtime_error("execute: prepare query failed");
108 throw std::runtime_error("execute: query failed");
109 rc
= sqlite3_step(res
); // execute
110 sqlite3_finalize(res
); // deallocate statement
115 throw std::runtime_error("execute: database busy");
120 throw std::runtime_error(sqlite3_errmsg(odb
->db
));
122 throw std::runtime_error("execute: database misuse");
124 throw std::runtime_error("execute: unknown result code");
131 // methods using db specific api calls
133 sqlite3_stmt
*Query::get_result(const std::string
& sql
)
135 Assert(!(odb
&& res
));
138 const char *s
= NULL
;
139 int rc
= sqlite3_prepare(odb
-> db
, sql
.c_str(), (int)sql
.size(), &res
, &s
);
141 throw std::runtime_error("get_result: prepare query failed");
143 throw std::runtime_error("get_result: query failed");
144 // get column names from result
149 const char *p
= sqlite3_column_name(res
, i
);
156 cache_rc
= sqlite3_step(res
);
157 cache_rc_valid
= true;
158 m_row_count
= (cache_rc
== SQLITE_ROW
) ? 1 : 0;
164 void Query::free_result()
168 sqlite3_finalize(res
);
171 cache_rc_valid
= false;
173 // clear column names
174 while (m_nmap
.size())
176 std::map
<std::string
,int>::iterator it
= m_nmap
.begin();
182 bool Query::fetch_row()
188 int rc
= cache_rc_valid
? cache_rc
: sqlite3_step(res
); // execute
189 cache_rc_valid
= false;
193 throw std::runtime_error("execute: database busy");
200 throw std::runtime_error(sqlite3_errmsg(odb
->db
));
203 throw std::runtime_error("execute: database misuse");
206 throw std::runtime_error("execute: unknown result code");
212 sqlite_int64
Query::insert_id()
216 return sqlite3_last_insert_rowid(odb
-> db
);
225 long Query::num_rows()
227 return odb
&& res
? m_row_count
: 0;
231 bool Query::is_null(int x
)
233 if (odb
&& res
&& row
)
235 if (sqlite3_column_type(res
, x
) == SQLITE_NULL
)
242 const char *Query::getstr(const std::string
& x
)
244 int index
= m_nmap
[x
] - 1;
246 throw std::runtime_error("Column name lookup failure: " + x
);
247 return getstr(index
);
251 const char *Query::getstr(int x
)
253 if (odb
&& res
&& row
&& x
< sqlite3_column_count(res
) )
255 const unsigned char *tmp
= sqlite3_column_text(res
, x
);
256 return tmp
? (const char *)tmp
: "";
262 const char *Query::getstr()
264 return getstr(rowcount
++);
268 double Query::getnum(const std::string
& x
)
270 int index
= m_nmap
[x
] - 1;
272 throw std::runtime_error("Column name lookup failure: " + x
);
273 return getnum(index
);
277 double Query::getnum(int x
)
279 if (odb
&& res
&& row
)
281 return sqlite3_column_double(res
, x
);
287 long Query::getval(const std::string
& x
)
289 int index
= m_nmap
[x
] - 1;
291 throw std::runtime_error("Column name lookup failure: " + x
);
292 return getval(index
);
296 long Query::getval(int x
)
298 if (odb
&& res
&& row
)
300 return sqlite3_column_int(res
, x
);
306 double Query::getnum()
308 return getnum(rowcount
++);
314 return getval(rowcount
++);
318 unsigned long Query::getuval(const std::string
& x
)
320 int index
= m_nmap
[x
] - 1;
322 throw std::runtime_error("Column name lookup failure: " + x
);
323 return getuval(index
);
327 unsigned long Query::getuval(int x
)
330 if (odb
&& res
&& row
)
332 l
= sqlite3_column_int(res
, x
);
338 unsigned long Query::getuval()
340 return getuval(rowcount
++);
344 int64_t Query::getbigint(const std::string
& x
)
346 int index
= m_nmap
[x
] - 1;
348 throw std::runtime_error("Column name lookup");
349 return getbigint(index
);
353 int64_t Query::getbigint(int x
)
355 if (odb
&& res
&& row
)
357 return sqlite3_column_int64(res
, x
);
363 int64_t Query::getbigint()
365 return getbigint(rowcount
++);
369 uint64_t Query::getubigint(const std::string
& x
)
371 int index
= m_nmap
[x
] - 1;
373 throw std::runtime_error("Column name lookup failure: " + x
);
374 return getubigint(index
);
378 uint64_t Query::getubigint(int x
)
381 if (odb
&& res
&& row
)
383 l
= sqlite3_column_int64(res
, x
);
389 uint64_t Query::getubigint()
391 return getubigint(rowcount
++);
395 double Query::get_num(const std::string
& sql
)
410 long Query::get_count(const std::string
& sql
)
423 const char *Query::get_string(const std::string
& sql
)
436 return m_tmpstr
.c_str(); // %! changed from 1.0 which didn't return NULL on failed query
440 const std::string
& Query::GetLastQuery()
446 std::string
Query::GetError()
449 return sqlite3_errmsg(odb
-> db
);
454 int Query::GetErrno()
457 return sqlite3_errcode(odb
-> db
);
462 bool Query::Connected()
464 return odb
? true : false;
470 void Query::ViewRes()
474 printf("no result stored\n");
477 printf("result column count = %d\n", sqlite3_column_count(res
));
478 for (int i
= 0; i
< sqlite3_column_count(res
); i
++)
480 printf(" %2d type %d name '%s'", i
, sqlite3_column_type(res
, i
), sqlite3_column_name(res
, i
));
481 printf(" / '%s'", (char *)sqlite3_column_text(res
, i
));
482 printf(" / %d", sqlite3_column_int(res
, i
));
483 printf(" / %f", sqlite3_column_double(res
, i
));
488 #ifdef SQLITEW_NAMESPACE
489 } // namespace SQLITEW_NAMESPACE {