DB::Sqlite3_Connection Class Reference

#include <sqlite3_db.h>

Inheritance diagram for DB::Sqlite3_Connection:

DB::Connection List of all members.

Public Member Functions

 Sqlite3_Connection ()
 ~Sqlite3_Connection ()
bool open (const char *database, const char *host, const int port, const char *user, const char *pass)
bool close (void)
bool isConnected (void)
bool execute (const char *sql)
ResultSetexecuteQuery (const char *sql)
char * escape (const char *)
const char * unixtimeToSql (const time_t)
const unsigned long insertId (void)
bool beginTrans (void)
bool commitTrans (void)
bool rollbackTrans (void)
bool setTransactionMode (const enum TRANS_MODE mode)
unsigned int errorno (void) const
const char * errormsg (void) const
const char * version (void) const
void * operator new (size_t bytes)
void operator delete (void *ptr)

Private Member Functions

 Sqlite3_Connection (const Sqlite3_Connection &old)
const Sqlite3_Connectionoperator= (const Sqlite3_Connection &old)

Private Attributes

sqlite3 * db_
int errorno_
std::string errormsg_

Detailed Description

Definition at line 75 of file sqlite3_db.h.


Constructor & Destructor Documentation

DB::Sqlite3_Connection::Sqlite3_Connection ( const Sqlite3_Connection old  )  [private]

DB::Sqlite3_Connection::Sqlite3_Connection (  )  [inline]

Definition at line 82 of file sqlite3_db.h.

00082 : db_(NULL) {};

DB::Sqlite3_Connection::~Sqlite3_Connection (  )  [inline]

Definition at line 83 of file sqlite3_db.h.

References close().

00083 { close(); }


Member Function Documentation

bool DB::Sqlite3_Connection::beginTrans ( void   )  [virtual]

Begins a new transaction. If for some reason it fails, false will be returned (for instance, if the underlying driver does not support transactions.)

Returns:
bool

Implements DB::Connection.

Definition at line 318 of file sqlite3_db.cpp.

References db_, and execute().

00319 {
00320     if (!db_) return (false);
00321     return (execute("BEGIN TRANSACTION"));
00322 }

bool DB::Sqlite3_Connection::close ( void   )  [virtual]

Close the database connection

Returns:
bool True if successful.

Implements DB::Connection.

Definition at line 233 of file sqlite3_db.cpp.

References db_.

Referenced by ~Sqlite3_Connection().

00234 {
00235     if (!db_) return (false);
00236     if (sqlite3_close(db_) != SQLITE_OK) {
00237         return (false);
00238     }
00239     db_ = NULL;
00240     return (true);
00241 }

bool DB::Sqlite3_Connection::commitTrans ( void   )  [virtual]

Attempts to commit the current transaction

Returns:
bool

Implements DB::Connection.

Definition at line 325 of file sqlite3_db.cpp.

References db_, and execute().

00326 {
00327     if (!db_) return (false);
00328     return (execute("COMMIT TRANSACTION"));
00329 }

const char * DB::Sqlite3_Connection::errormsg ( void   )  const [virtual]

Returns a textual representation of the last error to occur. The value returned should NOT be freed.

Returns:
const char*

Implements DB::Connection.

Definition at line 369 of file sqlite3_db.cpp.

References db_.

00370 {
00371     return (sqlite3_errmsg(db_));
00372 }

unsigned int DB::Sqlite3_Connection::errorno ( void   )  const [virtual]

Returns the last error code to occur.

Returns:
unsigned int

Implements DB::Connection.

Definition at line 363 of file sqlite3_db.cpp.

References db_.

00364 {
00365     return ((unsigned int) sqlite3_errcode(db_));
00366 }

char * DB::Sqlite3_Connection::escape ( const char *   )  [virtual]

Returns a database specific escaped string from the input. The string returned must be freed by the caller. This is used by the Query class.

Returns:
char*

Implements DB::Connection.

Definition at line 285 of file sqlite3_db.cpp.

00286 {
00287     /* this is overly complex so that valgrind doesn't report a mismatched
00288      * delete, free, delete[].
00289      */
00290     char *esc = sqlite3_mprintf("\'%q\'", str);
00291     char *ret = new char[strlen(esc) + 1];
00292     strcpy(ret, esc);
00293     free(esc);
00294     return (ret);
00295 
00296 }

bool DB::Sqlite3_Connection::execute ( const char *  sql  )  [virtual]

Executes a query to the database, discarding any result data. This function is typically used for non-SELECT statements.

Parameters:
sql 
Returns:
bool

Implements DB::Connection.

Definition at line 251 of file sqlite3_db.cpp.

References db_.

Referenced by beginTrans(), commitTrans(), rollbackTrans(), and setTransactionMode().

00252 {
00253     bool ret = false;
00254     char *err = NULL;
00255 
00256     if (!db_) return (ret);
00257 
00258     if (sqlite3_exec(db_, sql, NULL, NULL, &err) == SQLITE_OK) {
00259         ret = true;
00260     }
00261     if (err) {
00262         sqlite3_free(err);
00263     }
00264     return (ret);
00265 }

DB::ResultSet * DB::Sqlite3_Connection::executeQuery ( const char *  sql  )  [virtual]

Executes a query to the database which returns row data. The row data is manipulated using the ResultSet object returned by this method. If the database wasn't able to return row data or an error occurs, then zero is returned. Therefore be careful to check the returned value, before using it - or a seg. fault is possible!

Parameters:
sql 
Returns:
ResultSet*

Implements DB::Connection.

Definition at line 268 of file sqlite3_db.cpp.

References db_.

00269 {
00270     sqlite3_stmt *vm = NULL;
00271     const char *tail = NULL;
00272 
00273     if (!db_) return (false);
00274 
00275     if (sqlite3_prepare(db_, sql, -1, &vm, &tail) != SQLITE_OK) {
00276         return (0);
00277     }
00278 
00279     DB::ResultSet *c = 0;
00280     ACE_NEW_RETURN (c, DB::Sqlite3_ResultSet(vm), 0);
00281     return (c);
00282 }

const unsigned long DB::Sqlite3_Connection::insertId ( void   )  [virtual]

Returns the last SQL INSERT unique identifier if the underlying database supports this feature.

Returns:
const unsigned long

Implements DB::Connection.

Definition at line 312 of file sqlite3_db.cpp.

References db_.

00313 {
00314     return ((const unsigned long) sqlite3_last_insert_rowid(db_));
00315 }

bool DB::Sqlite3_Connection::isConnected ( void   )  [virtual]

Attempts to tell if we are still connected to the database.

Returns:
bool

Implements DB::Connection.

Definition at line 244 of file sqlite3_db.cpp.

References db_.

00245 {
00246     if (!db_) return (false);
00247     return (true);
00248 }

bool DB::Sqlite3_Connection::open ( const char *  database,
const char *  host,
const int  port,
const char *  user,
const char *  pass 
) [virtual]

Open a connection to a database.

Parameters:
database Database name. Sqlite3 uses this for the filename.
host Hostname
port Port
user Username
pass Password
Returns:
bool True if successful.

Implements DB::Connection.

Definition at line 223 of file sqlite3_db.cpp.

References db_.

00224 {
00225     if (sqlite3_open(database, &db_) != SQLITE_OK) {
00226         db_ = NULL;
00227         return (false);
00228     }
00229     return (true);
00230 }

void DB::Sqlite3_Connection::operator delete ( void *  ptr  ) 

Definition at line 402 of file sqlite3_db.cpp.

00403 {
00404   delete [] static_cast <char *> (ptr);
00405 }

void * DB::Sqlite3_Connection::operator new ( size_t  bytes  ) 

Definition at line 383 of file sqlite3_db.cpp.

00384 {
00385   return (::new char[bytes]);
00386 }

const Sqlite3_Connection& DB::Sqlite3_Connection::operator= ( const Sqlite3_Connection old  )  [private]

bool DB::Sqlite3_Connection::rollbackTrans ( void   )  [virtual]

Attempts to rollback the current transaction

Returns:
bool

Implements DB::Connection.

Definition at line 332 of file sqlite3_db.cpp.

References db_, and execute().

00333 {
00334     if (!db_) return (false);
00335     return (execute("ROLLBACK TRANSACTION"));
00336 }

bool DB::Sqlite3_Connection::setTransactionMode ( const enum TRANS_MODE  mode  ) 

Definition at line 339 of file sqlite3_db.cpp.

References execute(), DB::Connection::READ_COMMITTED, DB::Connection::READ_UNCOMMITTED, DB::Connection::REPEATABLE_READ, and DB::Connection::SERIALIZABLE.

00340 {
00341     const char *sql = "";
00342 
00343     switch (mode) {
00344     case READ_UNCOMMITTED:
00345         sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
00346         break;
00347     case READ_COMMITTED:
00348         sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED";
00349         break;
00350     case REPEATABLE_READ:
00351         sql = "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ";
00352         break;
00353     case SERIALIZABLE:
00354         sql = "SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE";
00355         break;
00356     default:
00357         return false;
00358     }
00359     return (execute(sql));
00360 }

const char * DB::Sqlite3_Connection::unixtimeToSql ( const   time_t  )  [virtual]

Returns a database specific representation of the time_t value. This is used by the Query class. The string returned must be freed by the caller.

Parameters:
time_t 
Returns:
const char*

Implements DB::Connection.

Definition at line 299 of file sqlite3_db.cpp.

00300 {
00301     char *buf = new char[22];
00302     struct tm *tmp = new struct tm;
00303     buf[0] = '\'';
00304     ACE_OS::strftime(buf+1, 20, "%Y-%m-%d %H:%M:%S", ACE_OS::localtime_r(&val, tmp));
00305     buf[20] = '\'';
00306     buf[21] = 0;
00307     delete tmp;
00308     return (buf);
00309 }

const char * DB::Sqlite3_Connection::version ( void   )  const [virtual]

Returns the DB abstraction module's version number and possibly the version of the underlying database client library used. The returned value should NOT be freed.

Returns:
const char*

Implements DB::Connection.

Definition at line 375 of file sqlite3_db.cpp.

00376 {
00377     static char ret[256];
00378     ACE_OS::snprintf(ret, 256, "Sqlite3 Driver v0.1 using %s", sqlite3_libversion());
00379     return ((const char *) ret);
00380 }


Member Data Documentation

sqlite3* DB::Sqlite3_Connection::db_ [private]

Definition at line 118 of file sqlite3_db.h.

Referenced by beginTrans(), close(), commitTrans(), errormsg(), errorno(), execute(), executeQuery(), insertId(), isConnected(), open(), and rollbackTrans().

std::string DB::Sqlite3_Connection::errormsg_ [private]

Definition at line 120 of file sqlite3_db.h.

int DB::Sqlite3_Connection::errorno_ [private]

Definition at line 119 of file sqlite3_db.h.


The documentation for this class was generated from the following files:
Generated on Tue Apr 24 18:59:42 2007 for DbAbstract by  doxygen 1.4.7