• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Adapters/BnSQLite/BnSQLite.cpp

Go to the documentation of this file.
00001 #include "BnSQLite.h"
00002 
00003 
00004 static int callback(void* p_this, int argc, char** argv, char** azColName)
00005 {
00006     BnSQLite* pThis = (BnSQLite*)p_this;
00007     if (!pThis) return -1;
00008 
00009     /* Initialize fields names map if it is empty. */
00010     if (pThis->mLastQueryNames.empty ())
00011         for (int i = 0; i < argc; i++)
00012             pThis->mLastQueryNames [azColName[i]] = i;
00013 
00014     /* Add line to the results vector, and fill it with values. */
00015     pThis->vLastQuery.push_back (t_query_fields (argc));
00016     for(int i = 0; i < argc; i++)
00017         pThis->vLastQuery.back ()[i] = argv[i] ? argv[i] : "NULL";
00018 
00019     pThis->vLastQueryItr = pThis->vLastQuery.begin ();
00020     return 0;
00021 }
00022 
00023 BnSQLite::BnSQLite (const char* name, bool (*pGetParam)(const char*, char*, int))
00024 :nLastErrCode (0), csErrMsg (NULL), isInitErr (false), db (NULL)
00025 {
00026     sName = name;
00027     pfGetParam = pGetParam;
00028 
00029     char db_path[50] = {0};
00030     char path[50];
00031     sprintf (path, "Adapters->%s->DB", name);
00032     isInitErr = !pGetParam (path, db_path, 49);
00033 
00034     if (!isInitErr)
00035         isInitErr = sqlite3_open(db_path, &db);
00036 }
00037 
00038 BnSQLite::~BnSQLite ()
00039 {
00040     if (csErrMsg)
00041         sqlite3_free(csErrMsg);
00042     sqlite3_close(db);
00043 }
00044 
00045 BufferHolder BnSQLite::Read (BufferHolder address) throw (BufferHolder)
00046 {
00047     if (isInitErr)
00048     {   /* If Init failed throw an exception. */
00049         sRetMsg = "Cannot initialize BnSQLite: ";
00050         if (db)
00051             sRetMsg += sqlite3_errmsg(db);
00052         throw BufferHolder (sRetMsg.c_str ());
00053     }
00054 
00055     sRetMsg.clear ();
00056     if (address.empty ()) return
00057         BufferHolder (sRetMsg.c_str ()); /* Empty string */
00058     string cmd (address.pointer, address.size);
00059 
00060     if (cmd == "_LastError")
00061     {   /* Error message */
00062         if (nLastErrCode == SQLITE_OK)
00063             sRetMsg = "OK";
00064         else
00065         {   /* We do have an error */
00066             if (csErrMsg)
00067                 sRetMsg = csErrMsg;
00068             char buff [40];
00069             sprintf (buff, " (%d)", nLastErrCode);
00070             sRetMsg += buff;
00071         }
00072     }
00073     else if (cmd == "_EOF")
00074     {   /* Is iterator in the end? */
00075         sRetMsg = (vLastQueryItr == vLastQuery.end () ? "True" : "False");
00076     }
00077     else if (cmd == "_FieldsList")
00078     {
00079         for (t_fields_names_itr itr = mLastQueryNames.begin ();
00080                 itr != mLastQueryNames.end (); ++itr)
00081             sRetMsg += itr->first + ' ';
00082     }
00083     else if (vLastQueryItr != vLastQuery.end ())
00084     {   /* Read a field value */
00085         t_fields_names_itr itr = mLastQueryNames.find (cmd);
00086         if (itr != mLastQueryNames.end () && itr->second < vLastQueryItr->size ())
00087             sRetMsg = (*vLastQueryItr)[itr->second];
00088         else
00089         {   /* Or by field number */
00090             int num = atoi(cmd.c_str ());
00091             if ((num > 0 || cmd == "0") && num < vLastQueryItr->size ())
00092                 sRetMsg = (*vLastQueryItr)[num];
00093         }
00094     }
00095     return BufferHolder (sRetMsg.c_str ());
00096 }
00097 
00098 bool BnSQLite::Write (BufferHolder address, BufferHolder value) throw (BufferHolder)
00099 {
00100     if (isInitErr)
00101     {   /* If Init failed throw an eception. */
00102         sRetMsg = "Cannot initialize BnSQLite: ";
00103         if (db)
00104             sRetMsg += sqlite3_errmsg(db);
00105         throw BufferHolder (sRetMsg.c_str ());
00106     }
00107 
00108     if (address.empty ()) return false;
00109     string cmd (address.pointer, address.size);
00110 
00111     if (cmd == "Clear")
00112     {
00113         mLastQueryNames.clear ();
00114         for (t_query_itr itr = vLastQuery.begin (); itr != vLastQuery.end (); ++itr)
00115             itr->clear ();
00116         vLastQuery.clear ();
00117         return true;
00118     }
00119     else if (cmd == "MoveFirst")
00120     {
00121         vLastQueryItr = vLastQuery.begin ();
00122         return true;
00123     }
00124     else if (cmd == "MoveNext")
00125     {
00126         if (vLastQueryItr != vLastQuery.end ()) vLastQueryItr++;
00127         return true;
00128     }
00129     else if (cmd == "Exec")
00130     {
00131         if (value.empty ()) return false;
00132         if (csErrMsg) sqlite3_free(csErrMsg);
00133         string exec (value.pointer, value.size);
00134         nLastErrCode = sqlite3_exec(db, exec.c_str (), callback, this, &csErrMsg);
00135         return  nLastErrCode == SQLITE_OK;
00136     }
00137     return false;
00138 }
00139 

Generated on Mon Oct 11 2010 16:23:23 for Beesnest by  doxygen 1.7.2