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 if (this->db) sqlite3_close(this->db);
\r
76 void sqlite3_connection::setprogresshandler( const zypp::ProgressData::ReceiverFnc &fnc,
\r
81 sqlite3_progress_handler(db, n, global_progress_handler, (void*)this);
\r
83 sqlite3_progress_handler(db, n, NULL, (void*)this);
\r
86 void sqlite3_connection::resetprogresshandler()
\r
88 sqlite3_progress_handler(db, 0, NULL, (void*)this);
\r
91 void sqlite3_connection::open(const char *db)
\r
93 if (sqlite3_open(db, &this->db)!=SQLITE_OK)
\r
95 std::string msg( "unable to open database at " );
\r
96 msg += ( db ? db : "NULL" );
\r
97 SQLITE3X_THROW(database_error(msg));
\r
101 void sqlite3_connection::open(const wchar_t *db)
\r
103 if (sqlite3_open16(db, &this->db)!=SQLITE_OK)
\r
104 SQLITE3X_THROW(database_error("unable to open database"));
\r
107 void sqlite3_connection::close()
\r
111 if (sqlite3_close(this->db)!=SQLITE_OK)
\r
112 SQLITE3X_THROW(database_error(*this));
\r
117 long long sqlite3_connection::insertid()
\r
119 if (!this->db) throw database_error("database is not open");
\r
120 return sqlite3_last_insert_rowid(this->db);
\r
123 void sqlite3_connection::setbusytimeout(int ms)
\r
125 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
127 if (sqlite3_busy_timeout(this->db, ms)!=SQLITE_OK)
\r
128 SQLITE3X_THROW(database_error(*this));
\r
131 void sqlite3_connection::executenonquery(const char *sql)
\r
133 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
134 sqlite3_command(*this, sql).executenonquery();
\r
137 void sqlite3_connection::executenonquery(const wchar_t *sql)
\r
139 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
140 sqlite3_command(*this, sql).executenonquery();
\r
143 void sqlite3_connection::executenonquery(const std::string &sql)
\r
145 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
146 sqlite3_command(*this, sql).executenonquery();
\r
149 void sqlite3_connection::executenonquery(const std::wstring &sql)
\r
151 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
152 sqlite3_command(*this, sql).executenonquery();
\r
155 int sqlite3_connection::executeint(const char *sql)
\r
157 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
158 return sqlite3_command(*this, sql).executeint();
\r
161 int sqlite3_connection::executeint(const wchar_t *sql)
\r
163 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
164 return sqlite3_command(*this, sql).executeint();
\r
167 int sqlite3_connection::executeint(const std::string &sql)
\r
169 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
170 return sqlite3_command(*this, sql).executeint();
\r
173 int sqlite3_connection::executeint(const std::wstring &sql)
\r
175 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
176 return sqlite3_command(*this, sql).executeint();
\r
179 long long sqlite3_connection::executeint64(const char *sql)
\r
181 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
182 return sqlite3_command(*this, sql).executeint64();
\r
185 long long sqlite3_connection::executeint64(const wchar_t *sql)
\r
187 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
188 return sqlite3_command(*this, sql).executeint64();
\r
191 long long sqlite3_connection::executeint64(const std::string &sql)
\r
193 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
194 return sqlite3_command(*this, sql).executeint64();
\r
197 long long sqlite3_connection::executeint64(const std::wstring &sql)
\r
199 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
200 return sqlite3_command(*this, sql).executeint64();
\r
203 double sqlite3_connection::executedouble(const char *sql)
\r
205 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
206 return sqlite3_command(*this, sql).executedouble();
\r
209 double sqlite3_connection::executedouble(const wchar_t *sql)
\r
211 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
212 return sqlite3_command(*this, sql).executedouble();
\r
215 double sqlite3_connection::executedouble(const std::string &sql)
\r
217 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
218 return sqlite3_command(*this, sql).executedouble();
\r
221 double sqlite3_connection::executedouble(const std::wstring &sql)
\r
223 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
224 return sqlite3_command(*this, sql).executedouble();
\r
227 std::string sqlite3_connection::executestring(const char *sql)
\r
229 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
230 return sqlite3_command(*this, sql).executestring();
\r
233 std::string sqlite3_connection::executestring(const wchar_t *sql)
\r
235 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
236 return sqlite3_command(*this, sql).executestring();
\r
239 std::string sqlite3_connection::executestring(const std::string &sql)
\r
241 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
242 return sqlite3_command(*this, sql).executestring();
\r
245 std::string sqlite3_connection::executestring(const std::wstring &sql)
\r
247 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
248 return sqlite3_command(*this, sql).executestring();
\r
251 std::wstring sqlite3_connection::executestring16(const char *sql)
\r
253 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
254 return sqlite3_command(*this, sql).executestring16();
\r
257 std::wstring sqlite3_connection::executestring16(const wchar_t *sql)
\r
259 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
260 return sqlite3_command(*this, sql).executestring16();
\r
263 std::wstring sqlite3_connection::executestring16(const std::string &sql)
\r
265 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
266 return sqlite3_command(*this, sql).executestring16();
\r
269 std::wstring sqlite3_connection::executestring16(const std::wstring &sql)
\r
271 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
272 return sqlite3_command(*this, sql).executestring16();
\r
275 std::string sqlite3_connection::executeblob(const char *sql)
\r
277 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
278 return sqlite3_command(*this, sql).executeblob();
\r
281 std::string sqlite3_connection::executeblob(const wchar_t *sql)
\r
283 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
284 return sqlite3_command(*this, sql).executeblob();
\r
287 std::string sqlite3_connection::executeblob(const std::string &sql)
\r
289 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
290 return sqlite3_command(*this, sql).executeblob();
\r
293 std::string sqlite3_connection::executeblob(const std::wstring &sql)
\r
295 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
296 return sqlite3_command(*this, sql).executeblob();
\r
299 void sqlite3_connection::execute(const std::string &sql)
\r
301 if (!this->db) SQLITE3X_THROW(database_error("database is not open"));
\r
305 if ( sqlite3_exec( this->db, sql.c_str(), NULL, NULL, &err_msg ) != SQLITE_OK )
\r
307 std::string err(err_msg);
\r
308 sqlite3_free(err_msg);
\r
309 SQLITE3X_THROW(database_error(err.c_str()));
\r