SOCI Logo Get SOCI at SourceForge.net. Fast, secure and Free Open Source software downloads

session.cpp

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
00003 // Distributed under the Boost Software License, Version 1.0.
00004 // (See accompanying file LICENSE_1_0.txt or copy at
00005 // http://www.boost.org/LICENSE_1_0.txt)
00006 //
00007 
00008 
00009 #include "soci-sqlite3.h"
00010 
00011 #include <sstream>
00012 #include <string>
00013 
00014 #ifdef _MSC_VER
00015 #pragma warning(disable:4355)
00016 #endif
00017 
00018 using namespace soci;
00019 using namespace soci::details;
00020 using namespace sqlite_api;
00021 
00022 namespace // anonymous
00023 {
00024 
00025 // helper function for hardcoded queries
00026 void hardExec(sqlite_api::sqlite3 *conn, char const *query, char const *errMsg)
00027 {
00028     char *zErrMsg = 0;
00029     int res = sqlite3_exec(conn, query, 0, 0, &zErrMsg);
00030     if (res != SQLITE_OK)
00031     {
00032         std::ostringstream ss;
00033         ss << errMsg << " "
00034            << zErrMsg;
00035 
00036         sqlite3_free(zErrMsg);
00037 
00038         throw soci_error(ss.str());
00039     }
00040 }
00041 
00042 } // namespace anonymous
00043 
00044 
00045 sqlite3_session_backend::sqlite3_session_backend(
00046     std::string const & connectString)
00047 {
00048     int timeout = 0;
00049     std::string dbname(connectString);
00050     std::stringstream ssconn(connectString);
00051     while (!ssconn.eof() && ssconn.str().find('=') >= 0)
00052     {
00053         std::string key, val;
00054         std::getline(ssconn, key, '=');
00055         std::getline(ssconn, val, ' ');
00056         if ("dbname" == key || "db" == key)
00057         {
00058             dbname = val;
00059         }
00060         else if ("timeout" == key)
00061         {
00062             std::istringstream converter(val);
00063             converter >> timeout;
00064         }
00065     }
00066 
00067     int res = sqlite3_open(dbname.c_str(), &conn_);
00068     if (SQLITE_OK != res)
00069     {
00070         const char *zErrMsg = sqlite3_errmsg(conn_);
00071         std::ostringstream ss;
00072         ss << "Cannot establish connection to the database. " << zErrMsg;
00073         throw soci_error(ss.str());
00074     }
00075 
00076     res = sqlite3_busy_timeout(conn_, timeout * 1000);
00077     if (SQLITE_OK != res)
00078     {
00079         const char *zErrMsg = sqlite3_errmsg(conn_);
00080         std::ostringstream ss;
00081         ss << "Failed to set busy timeout for connection. " << zErrMsg;
00082         throw soci_error(ss.str());
00083     }
00084 }
00085 
00086 sqlite3_session_backend::~sqlite3_session_backend()
00087 {
00088     clean_up();
00089 }
00090 
00091 void sqlite3_session_backend::begin()
00092 {
00093     hardExec(conn_, "BEGIN", "Cannot begin transaction.");
00094 }
00095 
00096 void sqlite3_session_backend::commit()
00097 {
00098     hardExec(conn_, "COMMIT", "Cannot commit transaction.");
00099 }
00100 
00101 void sqlite3_session_backend::rollback()
00102 {
00103     hardExec(conn_, "ROLLBACK", "Cannot rollback transaction.");
00104 }
00105 
00106 void sqlite3_session_backend::clean_up()
00107 {
00108     sqlite3_close(conn_);
00109 }
00110 
00111 sqlite3_statement_backend * sqlite3_session_backend::make_statement_backend()
00112 {
00113     return new sqlite3_statement_backend(*this);
00114 }
00115 
00116 sqlite3_rowid_backend * sqlite3_session_backend::make_rowid_backend()
00117 {
00118     return new sqlite3_rowid_backend(*this);
00119 }
00120 
00121 sqlite3_blob_backend * sqlite3_session_backend::make_blob_backend()
00122 {
00123     return new sqlite3_blob_backend(*this);
00124 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
SourceForge Logo

Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1