sqlite2xx.cpp

Go to the documentation of this file.
00001 /*
00002 _______________________________________________________________________________
00003 
00004                                  SqlWrapperXX
00005 
00006 -------------------------------------------------------------------------------
00007 Copyright (c) 2007 Kai Braaten
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -------------------------------------------------------------------------------
00027 */
00028 
00029 #include "sqlite2xx.hpp"
00030 
00031 // Public member functions
00032 
00033 Sqlite2Context::Sqlite2Context( const string &dbName )
00034   : handle( 0 ),
00035     _dbName( dbName )
00036 {
00037   if( !_dbName.empty() )
00038     {
00039       try
00040         {
00041           open();
00042         }
00043       catch( SqlDatabaseOpenException &e )
00044         {
00045           throw e;
00046         }
00047     }
00048 }
00049 
00050 Sqlite2Context::~Sqlite2Context()
00051 {
00052   close();
00053 }
00054 
00055 void Sqlite2Context::close()
00056 {
00057   if( handle )
00058     {
00059       sqlite_close( handle );
00060       handle = 0;
00061     }
00062 }
00063 
00064 string Sqlite2Context::escapeString( const string &param ) const
00065 {
00066   string output = param;
00067   string::size_type pos = 0;
00068 
00069   while( ( pos = output.find( "'", pos ) ) != string::npos )
00070     {
00071       output.replace( pos, 1, "''" );
00072       pos += 2;
00073     }
00074 
00075   return output;
00076 }
00077 
00078 string Sqlite2Context::version() const
00079 {
00080   string v = "SQLite ";
00081   v += sqlite_version;
00082   return v;
00083 }
00084 
00085 // Private member functions
00086 
00087 void Sqlite2Context::open()
00088 {
00089   if( handle == 0 )
00090     {
00091       char *errmsg = 0;
00092 
00093       if( !( handle = sqlite_open( _dbName.c_str(), 0, &errmsg ) ) )
00094         {
00095           string ex = "Failed to open database: ";
00096           ex += errmsg;
00097           close();
00098           throw SqlDatabaseOpenException( ex );
00099         }
00100     }
00101 }
00102 
00103 SqlResult Sqlite2Context::query( const string &sql ) const
00104 {
00105   sqlite_vm *vm = 0;
00106   const char *tail = 0;
00107   string original = sql;
00108   SqlResult result;
00109 
00110   do
00111     {
00112       char *errMsg = 0;
00113 
00114       if( sqlite_compile( handle, original.c_str(), &tail, &vm, &errMsg ) != SQLITE_OK )
00115         {
00116           string ex = "Bad SQL statement: ";
00117           ex += original;
00118           ex += "\nSqlite2 message: ";
00119           ex += errMsg;
00120           sqlite_freemem( errMsg );
00121           throw SqlDatabaseBadStatementException( ex );
00122         }
00123 
00124       int numCols = 0;
00125       const char **rawResult;
00126 
00127       while( sqlite_step( vm, &numCols, &rawResult, 0 ) == SQLITE_ROW )
00128         {
00129           SqlRow *row = new SqlRow( numCols );
00130           result.add( row );
00131 
00132           for( int col = 0; col < row->columns(); ++col )
00133             {
00134               row->add( rawResult[ col ] );
00135             }
00136         }
00137 
00138       original = tail;
00139     }
00140   while( *tail != '\0' );
00141 
00142   sqlite_finalize( vm, 0 );
00143   return result;
00144 }

Generated on Sat Aug 18 16:21:44 2007 for SqlWrapperXX by  doxygen 1.5.0