2 Copyright (C) 2004-2005 Cory Nelson
\r
4 This software is provided 'as-is', without any express or implied
\r
5 warranty. In no event will the authors be held liable for any damages
\r
6 arising from the use of this software.
\r
8 Permission is granted to anyone to use this software for any purpose,
\r
9 including commercial applications, and to alter it and redistribute it
\r
10 freely, subject to the following restrictions:
\r
12 1. The origin of this software must not be misrepresented; you must not
\r
13 claim that you wrote the original software. If you use this software
\r
14 in a product, an acknowledgment in the product documentation would be
\r
15 appreciated but is not required.
\r
16 2. Altered source versions must be plainly marked as such, and must not be
\r
17 misrepresented as being the original software.
\r
18 3. This notice may not be removed or altered from any source distribution.
\r
21 $Author: phrostbyte $
\r
22 $Date: 2005/06/16 20:46:40 $
\r
27 this source contains modifications by Novell Inc.
\r
31 * dmacvicar@novell.com
\r
36 #include <sqlite3.h>
\r
37 #include "sqlite3x.hpp"
\r
39 static int global_progress_handler(void* ptr)
\r
41 //RepoImpl *r = dynamic_cast<RepoImpl *>(ptr);
\r
42 sqlite3x::sqlite3_connection *r = (sqlite3x::sqlite3_connection *)(ptr);
\r
44 return r->_progress_handler_accessor(ptr);
\r
51 int sqlite3_connection::_progress_handler_accessor(void* ptr)
\r
53 if ( _ticks.tick() )
\r
58 sqlite3_connection::sqlite3_connection() : db(NULL)
\r
61 sqlite3_connection::sqlite3_connection(const char *db) : db(NULL)
\r
66 sqlite3_connection::sqlite3_connection(const wchar_t *db) : db(NULL)
\r
71 sqlite3_connection::~sqlite3_connection()
\r
73 _ticks = zypp::ProgressData();
\r
74 resetprogresshandler();
\r
75 if (this->db) sqlite3_close(this->db);
\r
78 void sqlite3_connection::setprogresshandler( const zypp::ProgressData::ReceiverFnc &fnc,
\r
83 sqlite3_progress_handler(db, n, global_progress_handler, (void*)this);
\r
85 sqlite3_progress_handler(db, n, NULL, (void*)this);
\r
88 void sqlite3_connection::resetprogresshandler()
\r
90 _ticks = zypp::ProgressData();
\r
91 sqlite3_progress_handler(db, 0, NULL, (void*)this);
\r
94 void sqlite3_connection::open(const char *db)
\r
96 if (sqlite3_open(db, &this->db)!=SQLITE_OK)
\r
98 std::string msg( "unable to open database at " );
\r
99 msg += ( db ? db : "NULL" );
\r
100 SQLITE3X_THROW(database_error(msg));
\r
104 void sqlite3_connection::open(const wchar_t *db)
\r
106 if (sqlite3_open16(db, &this->db)!=SQLITE_OK)
\r
107 SQLITE3X_THROW(database_error("unable to open database"));
\r
110 void sqlite3_connection::close()
\r
114 if (sqlite3_close(this->db)!=SQLITE_OK)
\r
115 SQLITE3X_THROW(database_error(*this));
\r
120 long long sqlite3_connection::insertid()
\r
122 if (!this->db) throw database_error("database is not open");
\r
123 return sqlite3_last_insert_rowid(this->db);
\r
126 void sqlite3_connection::setbusytimeout(int ms)
\r
128 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
130 if (sqlite3_busy_timeout(this->db, ms)!=SQLITE_OK)
\r
131 SQLITE3X_THROW(database_error(*this));
\r
134 void sqlite3_connection::executenonquery(const char *sql)
\r
136 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
137 sqlite3_command(*this, sql).executenonquery();
\r
140 void sqlite3_connection::executenonquery(const wchar_t *sql)
\r
142 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
143 sqlite3_command(*this, sql).executenonquery();
\r
146 void sqlite3_connection::executenonquery(const std::string &sql)
\r
148 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
149 sqlite3_command(*this, sql).executenonquery();
\r
152 void sqlite3_connection::executenonquery(const std::wstring &sql)
\r
154 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
155 sqlite3_command(*this, sql).executenonquery();
\r
158 int sqlite3_connection::executeint(const char *sql)
\r
160 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
161 return sqlite3_command(*this, sql).executeint();
\r
164 int sqlite3_connection::executeint(const wchar_t *sql)
\r
166 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
167 return sqlite3_command(*this, sql).executeint();
\r
170 int sqlite3_connection::executeint(const std::string &sql)
\r
172 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
173 return sqlite3_command(*this, sql).executeint();
\r
176 int sqlite3_connection::executeint(const std::wstring &sql)
\r
178 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
179 return sqlite3_command(*this, sql).executeint();
\r
182 long long sqlite3_connection::executeint64(const char *sql)
\r
184 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
185 return sqlite3_command(*this, sql).executeint64();
\r
188 long long sqlite3_connection::executeint64(const wchar_t *sql)
\r
190 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
191 return sqlite3_command(*this, sql).executeint64();
\r
194 long long sqlite3_connection::executeint64(const std::string &sql)
\r
196 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
197 return sqlite3_command(*this, sql).executeint64();
\r
200 long long sqlite3_connection::executeint64(const std::wstring &sql)
\r
202 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
203 return sqlite3_command(*this, sql).executeint64();
\r
206 double sqlite3_connection::executedouble(const char *sql)
\r
208 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
209 return sqlite3_command(*this, sql).executedouble();
\r
212 double sqlite3_connection::executedouble(const wchar_t *sql)
\r
214 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
215 return sqlite3_command(*this, sql).executedouble();
\r
218 double sqlite3_connection::executedouble(const std::string &sql)
\r
220 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
221 return sqlite3_command(*this, sql).executedouble();
\r
224 double sqlite3_connection::executedouble(const std::wstring &sql)
\r
226 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
227 return sqlite3_command(*this, sql).executedouble();
\r
230 std::string sqlite3_connection::executestring(const char *sql)
\r
232 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
233 return sqlite3_command(*this, sql).executestring();
\r
236 std::string sqlite3_connection::executestring(const wchar_t *sql)
\r
238 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
239 return sqlite3_command(*this, sql).executestring();
\r
242 std::string sqlite3_connection::executestring(const std::string &sql)
\r
244 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
245 return sqlite3_command(*this, sql).executestring();
\r
248 std::string sqlite3_connection::executestring(const std::wstring &sql)
\r
250 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
251 return sqlite3_command(*this, sql).executestring();
\r
254 std::wstring sqlite3_connection::executestring16(const char *sql)
\r
256 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
257 return sqlite3_command(*this, sql).executestring16();
\r
260 std::wstring sqlite3_connection::executestring16(const wchar_t *sql)
\r
262 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
263 return sqlite3_command(*this, sql).executestring16();
\r
266 std::wstring sqlite3_connection::executestring16(const std::string &sql)
\r
268 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
269 return sqlite3_command(*this, sql).executestring16();
\r
272 std::wstring sqlite3_connection::executestring16(const std::wstring &sql)
\r
274 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
275 return sqlite3_command(*this, sql).executestring16();
\r
278 std::string sqlite3_connection::executeblob(const char *sql)
\r
280 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
281 return sqlite3_command(*this, sql).executeblob();
\r
284 std::string sqlite3_connection::executeblob(const wchar_t *sql)
\r
286 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
287 return sqlite3_command(*this, sql).executeblob();
\r
290 std::string sqlite3_connection::executeblob(const std::string &sql)
\r
292 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
293 return sqlite3_command(*this, sql).executeblob();
\r
296 std::string sqlite3_connection::executeblob(const std::wstring &sql)
\r
298 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
299 return sqlite3_command(*this, sql).executeblob();
\r
302 void sqlite3_connection::execute(const std::string &sql)
\r
304 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
308 if ( sqlite3_exec( this->db, sql.c_str(), NULL, NULL, &err_msg ) != SQLITE_OK )
\r
310 std::string err(err_msg);
\r
311 sqlite3_free(err_msg);
\r
312 SQLITE3X_THROW(database_error(err.c_str()));
\r