Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

db_command.cpp

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2005 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00029 #include "precomp.h"
00030 #include "db_command.h"
00031 #include "db_connection.h"
00032 #include "exception.h"
00033 #include "bytearray.h"
00034 #include "system.h"
00035 #include "string_help.h"
00036 #include "logger.h"
00037 #include <sqlite3.h>
00038 #include <stdarg.h>
00039 
00041 // CL_DBCommand Construction:
00042 
00043 CL_DBCommand::CL_DBCommand(CL_DBConnection *database)
00044 : db(database), vm(0), last_insert_rowid(-1)
00045 {
00046 }
00047 
00048 CL_DBCommand::CL_DBCommand(const CL_String &sql_statement, CL_DBConnection *database)
00049 : db(database), vm(0), last_insert_rowid(-1)
00050 {
00051         compile(sql_statement);
00052 }
00053 
00054 CL_DBCommand::~CL_DBCommand()
00055 {
00056         reset();
00057 }
00058 
00060 // CL_DBCommand Attributes:
00061 
00062 int CL_DBCommand::get_last_insert_rowid() const
00063 {
00064         return last_insert_rowid;
00065 }
00066 
00067 int CL_DBCommand::get_columns() const
00068 {
00069         CL_MutexSection mutex_lock(&db->mutex);
00070         return sqlite3_column_count(vm);
00071 }
00072         
00073 const char *CL_DBCommand::get_name(int column) const
00074 {
00075         CL_MutexSection mutex_lock(&db->mutex);
00076         return sqlite3_column_name(vm, column);
00077 }
00078 
00079 int CL_DBCommand::get_index(const char *name) const
00080 {
00081         CL_MutexSection mutex_lock(&db->mutex);
00082         int count = get_columns();
00083         for (int index=0; index<count; index++)
00084         {
00085                 const char *col_name = sqlite3_column_name(vm, index);
00086                 if (col_name != 0 && strcmp(col_name, name) == 0)
00087                 {
00088                         return index;
00089                 }
00090         }
00091 
00092         CL_String name_text = CL_StringHelp::utf8_to_text(name);
00093         throw CL_Exception(TEXT("No such column name ") + name_text);
00094         return -1;
00095 }
00096 
00097 int CL_DBCommand::get_index(const CL_String &name) const
00098 {
00099         return get_index(name.c_str());
00100 }
00101 
00102 const char *CL_DBCommand::get_decltype(int column) const
00103 {
00104         CL_MutexSection mutex_lock(&db->mutex);
00105         return sqlite3_column_decltype(vm, column);
00106 }
00107 
00108 const char *CL_DBCommand::get_decltype(const char *name) const
00109 {
00110         CL_MutexSection mutex_lock(&db->mutex);
00111         return sqlite3_column_decltype(vm, get_index(name));
00112 }
00113 
00114 CL_ByteArray CL_DBCommand::get_blob(int column) const
00115 {
00116         CL_ByteArray array;
00117         get_blob(column, array);
00118         return array;
00119 }
00120 
00121 CL_ByteArray CL_DBCommand::get_blob(const char *name) const
00122 {
00123         CL_MutexSection mutex_lock(&db->mutex);
00124         return get_blob(get_index(name));
00125 }
00126 
00127 void CL_DBCommand::get_blob(int column, CL_ByteArray &array) const
00128 {
00129         CL_MutexSection mutex_lock(&db->mutex);
00130         const void *blob = sqlite3_column_blob(vm, column);
00131         if (blob == 0)
00132         {
00133                 array.set_size(0);
00134                 return;
00135         }
00136         
00137         array.set_size(sqlite3_column_bytes(vm, column));
00138         memcpy(array.get_data(), blob, array.get_size());
00139 }
00140 
00141 void CL_DBCommand::get_blob(const char *name, CL_ByteArray &array) const
00142 {
00143         return get_blob(get_index(name), array);
00144 }
00145 
00146 const void *CL_DBCommand::get_blob_raw(int column) const
00147 {
00148         CL_MutexSection mutex_lock(&db->mutex);
00149         return sqlite3_column_blob(vm, column);
00150 }
00151 
00152 const void *CL_DBCommand::get_blob_raw(const char *name) const
00153 {
00154         return get_blob_raw(get_index(name));
00155 }
00156 
00157 int CL_DBCommand::get_blob_size(int column) const
00158 {
00159         CL_MutexSection mutex_lock(&db->mutex);
00160         return sqlite3_column_bytes(vm, column);
00161 }
00162 
00163 int CL_DBCommand::get_blob_size(const char *name) const
00164 {
00165         return get_blob_size(get_index(name));
00166 }
00167 
00168 double CL_DBCommand::get_double(int column) const
00169 {
00170         CL_MutexSection mutex_lock(&db->mutex);
00171         return sqlite3_column_double(vm, column);
00172 }
00173 
00174 double CL_DBCommand::get_double(const char *name) const
00175 {
00176         return get_double(get_index(name));
00177 }
00178 
00179 int CL_DBCommand::get_int(int column) const
00180 {
00181         CL_MutexSection mutex_lock(&db->mutex);
00182         return sqlite3_column_int(vm, column);
00183 }
00184 
00185 int CL_DBCommand::get_int(const char *name) const
00186 {
00187         return get_int(get_index(name));
00188 }
00189 
00190 const char *CL_DBCommand::get_text(int column) const
00191 {
00192         CL_MutexSection mutex_lock(&db->mutex);
00193         return (char *) sqlite3_column_text(vm, column);
00194 }
00195 
00196 const char *CL_DBCommand::get_text(const char *name) const
00197 {
00198         return get_text(get_index(name));
00199 }
00200 
00201 CL_DBReturnValue CL_DBCommand::operator[](int column) const
00202 {
00203         return CL_DBReturnValue(this, column);
00204 }
00205         
00206 CL_DBReturnValue CL_DBCommand::operator[](const char *name) const
00207 {
00208         return CL_DBReturnValue(this, get_index(name));
00209 }
00210         
00211 CL_DBReturnValue CL_DBCommand::operator[](const CL_String &name) const
00212 {
00213         return CL_DBReturnValue(this, get_index(name));
00214 }
00215 
00216 int CL_DBCommand::get_param_column(const char *name) const
00217 {
00218         return 0;
00219 }
00220 
00222 // CL_DBCommand Operations:
00223 
00224 void CL_DBCommand::compile(const CL_String &sql_statement)
00225 {
00226         compile(sql_statement.c_str());
00227 }
00228 
00229 void CL_DBCommand::compile(const char *sql_statement)
00230 {
00231         cl_log_event("db", sql_statement);
00232         reset();
00233         
00234         CL_MutexSection mutex_lock(&db->mutex);
00235         const char *tail = 0;
00236         int result = sqlite3_prepare(db->db, sql_statement, strlen(sql_statement), &vm, &tail);
00237         if (result != SQLITE_OK)
00238         {
00239                 CL_StringA error = sqlite3_errmsg(db->db);
00240                 throw CL_Exception(error);
00241         }
00242 }
00243 
00244 void CL_DBCommand::compilef(const char *sql_statement, ...)
00245 {
00246         CL_MutexSection mutex_lock(&db->mutex);
00247         va_list params;
00248         va_start(params, sql_statement);
00249         char *str = sqlite3_vmprintf(sql_statement, params);
00250         va_end(params);
00251         try
00252         {
00253                 compile(str);
00254         }
00255         catch (CL_Exception e)
00256         {
00257                 sqlite3_free(str);
00258                 throw;
00259         }
00260         sqlite3_free(str);
00261 }
00262 
00263 void CL_DBCommand::set_param_double(int column, double value)
00264 {
00265 }
00266 
00267 void CL_DBCommand::set_param_double(const char *column_name, double value)
00268 {
00269         set_param_double(get_param_column(column_name), value);
00270 }
00271 
00272 void CL_DBCommand::set_param_int(int column, int value)
00273 {
00274 }
00275 
00276 void CL_DBCommand::set_param_int(const char *column_name, int value)
00277 {
00278         set_param_int(get_param_column(column_name), value);
00279 }
00280 
00281 void CL_DBCommand::set_param_blob(int column, const CL_ByteArray &blob)
00282 {
00283 }
00284 
00285 void CL_DBCommand::set_param_blob(const char *column_name, const CL_ByteArray &blob)
00286 {
00287         set_param_blob(get_param_column(column_name), blob);
00288 }
00289 
00290 void CL_DBCommand::set_param_text(int column, const char *text)
00291 {
00292 }
00293 
00294 void CL_DBCommand::set_param_text(const char *column_name, const char *text)
00295 {
00296         set_param_text(get_param_column(column_name), text);
00297 }
00298 
00299 bool CL_DBCommand::step()
00300 {
00301         if (vm == 0)
00302                 return false;
00303 
00304         for (int i=0; i<100; i++)
00305         {
00306                 CL_MutexSection mutex_lock(&db->mutex);
00307                 last_insert_rowid = -1;
00308                 int result = sqlite3_step(vm);
00309                 switch (result)
00310                 {
00311                 case SQLITE_BUSY:
00312                         CL_System::sleep(1);
00313                         break;
00314                 case SQLITE_ROW:
00315                         last_insert_rowid = sqlite3_last_insert_rowid(db->db);
00316                         return true;
00317                 case SQLITE_DONE:
00318                         last_insert_rowid = sqlite3_last_insert_rowid(db->db);
00319                         return false;
00320                 case SQLITE_ERROR:
00321                         reset();
00322                         return false;
00323                 case SQLITE_MISUSE:
00324                         throw CL_Exception(TEXT("Database Misuse!"));
00325                 }
00326         }
00327         
00328         throw CL_Exception(TEXT("Database Busy!"));
00329 }
00330 
00331 void CL_DBCommand::reset()
00332 {
00333         if (vm)
00334         {
00335                 CL_MutexSection mutex_lock(&db->mutex);
00336                 int result = sqlite3_finalize(vm);
00337                 vm = 0;
00338                 last_insert_rowid = -1;
00339                 if (result != SQLITE_OK)
00340                 {
00341                         const char *err = sqlite3_errmsg(db->db);
00342                         CL_StringA error = (err == 0) ? "Unknown error" : err;
00343                         throw CL_Exception(error);
00344                 }
00345         }
00346 }
00347 
00349 // CL_DBCommand Implementation:

Generated on Sat Feb 19 22:51:15 2005 for npcore by  doxygen 1.4.1