clean up sources and sinks 99/2499/1
authorKevron Rees <tripzero.kev@gmail.com>
Thu, 15 Nov 2012 00:43:31 +0000 (16:43 -0800)
committerKevron Rees <tripzero.kev@gmail.com>
Thu, 15 Nov 2012 00:44:22 +0000 (16:44 -0800)
13 files changed:
ambd/pluginloader.cpp
ambd/pluginloader.h
plugins/database/CMakeLists.txt
plugins/database/basedb.hpp [new file with mode: 0644]
plugins/database/baseobject.h [new file with mode: 0644]
plugins/database/databasesink.cpp [new file with mode: 0644]
plugins/database/databasesink.h [new file with mode: 0644]
plugins/database/sqlitedatabase.cpp [new file with mode: 0644]
plugins/database/sqlitedatabase.h [new file with mode: 0644]
plugins/database/sqlitequery.cpp [new file with mode: 0644]
plugins/database/sqlitequery.h [new file with mode: 0644]
plugins/database/utils.cpp [new file with mode: 0644]
plugins/database/utils.h [new file with mode: 0644]

index 33107de..55ae6b7 100644 (file)
@@ -153,6 +153,19 @@ PluginLoader::PluginLoader(string configFile, AbstractRoutingEngine* re): f_crea
        
 }
 
+PluginLoader::~PluginLoader()
+{
+       for(auto itr = mSinks.begin(); itr != mSinks.end(); itr++)
+       {
+               delete *itr;
+       }
+
+       for(auto itr = mSources.begin(); itr != mSources.end(); itr++)
+       {
+               delete *itr;
+       }
+}
+
 SinkList PluginLoader::sinks()
 {
        return mSinks;
index 18edb9e..d0489c8 100644 (file)
@@ -41,6 +41,7 @@ class PluginLoader
 
 public:
        PluginLoader(string configFile, AbstractRoutingEngine* routingEngine);
+       ~PluginLoader();
 
        SourceList sources();
        SinkList sinks();
index 8e9236f..305fc45 100644 (file)
@@ -2,14 +2,15 @@ if(database_plugin)
 
 include(CheckIncludeFiles)
 
+pkg_check_modules(sqlite REQUIRED sqlite3)
 
-include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${gio_INCLUDE_DIRS} ${gio-unix_INCLUDE_DIRS} )
+include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${sqlite_INCLUDE_DIRS})
 
-set(databasesinkplugin_headers *.h )
-set(databasesinkplugin_sources *.cpp)
+set(databasesinkplugin_headers databasesink.h utils.h basedb.hpp baseobject.h sqlitedatabase.h sqlitequery.h)
+set(databasesinkplugin_sources databasesink.cpp utils.cpp sqlitedatabase.cpp sqlitequery.cpp)
 add_library(databasesinkplugin MODULE ${databasesinkplugin_sources})
 set_target_properties(databasesinkplugin PROPERTIES PREFIX "")
-target_link_libraries(databasesinkplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries} )
+target_link_libraries(databasesinkplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries} ${sqlite_LIBRARIES})
 
 install(TARGETS databasesinkplugin LIBRARY DESTINATION lib/automotive-message-broker)
 
diff --git a/plugins/database/basedb.hpp b/plugins/database/basedb.hpp
new file mode 100644 (file)
index 0000000..a49872c
--- /dev/null
@@ -0,0 +1,305 @@
+/*
+ * timedate - Displays time and date and daily events
+ * Copyright (c) <2009>, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef _BASEDB_H_
+#define _BASEDB_H_
+
+#include "sqlitedatabase.h"
+#include "sqlitequery.h"
+#include <string>
+#include <sstream>
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+
+using namespace std;
+
+template<typename T>
+class NameValuePair
+{
+       public:
+               NameValuePair(){ }
+               NameValuePair(std::string n, T v){name = n; value = v;}
+               std::string name;
+               T value;
+};
+
+template<typename T>
+class DictionaryList: public std::vector<NameValuePair<T> >
+{
+
+};
+
+class BaseDB
+{
+public:
+       BaseDB():db(NULL),q(NULL)
+       {
+               
+       }
+       
+       
+       virtual ~BaseDB()
+       {
+               printf("BaseDB: Destroying db object. Table: %s",table.c_str());
+               delete q;
+               delete db;
+       }
+       
+       void setTable(string tablename)
+       {
+               if(tablename == "") return;
+               table = tablename;
+               
+               if(!tableExists())
+                       reloadTable();
+       }
+       
+       virtual void
+       init(string dbname, string tablename, string tablestring)
+       {
+               printf("BaseDB: Initializing db object. Table: %s",tablename.c_str());
+               tableString = tablestring;
+               
+               db = new sqlitedatabase();
+               
+               db->init(dbname);
+               
+               printf("BaseDB: Using db/db-file: %s",dbname.c_str());
+               
+               if(! db->Connected())
+               {
+                       printf("BaseDB: database not found %s",dbname.c_str());
+               }
+               q = new sqlitequery();;
+               
+               q->init(db);
+
+               setTable(tablename);
+       }
+       
+       virtual void
+       reloadTable()
+       {
+               printf("BaseDB: reloading table %s",table.c_str());
+               dropTable();
+               createTable();
+       }
+       
+       virtual bool tableExists()
+       {
+               bool exists=false;
+               string query = "SELECT * FROM "+table+" LIMIT 0,1";
+               printf("BaseDB: checking for existing table with %s",query.c_str());
+               q->getResult(query);
+               int numrows = q->numRows();
+               if(numrows <= 0 )
+                       exists = false;
+               else exists = true;
+
+               printf("BaseDB: Table '%s' exists? %d because %d rows where found.", table.c_str(), exists, numrows);
+               q->freeResult();
+               return exists;
+       }
+       
+       virtual void
+       renameTable(string newname)
+       {
+               dropTable(newname);
+               string query = "ALTER TABLE "+table+" RENAME TO "+newname;
+               q->execute(query);
+       }
+       
+       template<typename T>
+       void insert(DictionaryList<T> params)
+       {
+               string query = "INSERT INTO "+table+" (";
+               ostringstream endquery;
+               endquery<<" VALUES ( ";
+               for(size_t i=0; i< params.size(); i++)
+               {
+                       query+=" `"+fixInvalids(params[i].name)+"`";
+                       ostringstream tempval;
+                       tempval<<params[i].value;
+                       endquery<<"'"<<fixInvalids(tempval.str())<<"'";
+                       if(i < params.size()-1)
+                       {
+                               query+=",";     
+                               endquery<<",";
+                       }
+               }
+               endquery<<" )";
+               query+=" )"+endquery.str();
+               printf("BaseDB: %s",query.c_str());
+               q->execute(query);
+       }
+       
+       template<typename T>
+       void
+       insert(NameValuePair<T> param)
+       {
+               string query = "INSERT INTO "+table+" (";
+               ostringstream endquery;
+               endquery<<" VALUES ( ";
+               query+=" `"+fixInvalids(param.name)+"`";
+               ostringstream tempval;
+               tempval<<param.value;
+               endquery<<"'"<<fixInvalids(tempval.str())<<"'";
+               endquery<<" )";
+               query+=" )"+endquery.str();
+               printf("BaseDB: %s",query.c_str());
+               q->execute(query);
+       }
+       
+       virtual void
+       insert(DictionaryList<string> params)
+       {
+               insert<string>(params);
+       }
+       
+       template<typename T, typename TT, typename T3>
+       void
+       update(T col, TT colval, NameValuePair<T3> qualifier)
+       {
+               ostringstream query;
+               ostringstream tempval;
+               ostringstream tempcolval;
+               tempval<<qualifier.value;
+               tempcolval<<colval;
+               query << "UPDATE "<< table <<
+                       " SET `"<<col<<"` = '"<<fixInvalids(tempcolval.str())<<
+                       "' WHERE `"<<fixInvalids(qualifier.name)<<"` = '"<<fixInvalids(tempval.str())<<"'";
+               printf("BaseDB: Update: %s",query.str().c_str());
+               q->execute(query.str());
+       }
+       
+       template<typename T, typename TT>
+       void 
+       update(NameValuePair<T> param, NameValuePair<TT> qualifier)
+       {
+               update<string,T,TT>(param.name, param.value, qualifier);
+       }
+       
+       template<typename T, typename TT>
+       void
+       update(DictionaryList<T> params, NameValuePair<TT> qualifier)
+       {
+               for(size_t i=0;i<params.size();i++)
+               {
+                       update<T,TT>(params[i],qualifier);
+               }
+       }
+       
+       virtual void update(NameValuePair<string> param, NameValuePair<string> qualifier)
+       {
+               update<string,string>(param,qualifier);
+       }
+       
+       template<typename T>
+       void deleteRow(NameValuePair<T> qualifier)
+       {
+               ostringstream query;
+               ostringstream tempval;
+               tempval<<qualifier.value;
+               query << "DELETE FROM "<< table<<
+                               " WHERE `"<<qualifier.name<<"` = '"<<fixInvalids(tempval.str())<<"'";
+               printf("BaseDB: %s: %s",__FUNCTION__, query.str().c_str());
+               q->execute(query.str());
+       }
+       
+       virtual void 
+       deleteRow(NameValuePair<string> qualifier)
+       {
+               deleteRow<string>(qualifier);
+       }
+       
+       virtual void
+       dropTable()
+       {
+               dropTable(table);
+       }
+       
+       virtual void
+       dropTable(string tablename)
+       {
+               string query="DROP TABLE IF EXISTS "+tablename;
+               printf("BaseDB: Dropping Table %s with query:? %s",tablename.c_str(),query.c_str());
+               q->execute(query);
+       }
+       
+       virtual void
+       createTable()
+       {
+               string t = tableString;
+               string query;
+               string::size_type i=t.find("%s",0);
+               if(i!=string::npos) query=t.replace(i, 2, table);
+               else query = t;
+               printf("BaseDB: Creating Table %s with query:? %s",table.c_str(),query.c_str());
+               q->execute(query);
+       }
+       
+
+       
+       string
+       fixInvalids(string filename)
+       {
+               return filename;
+       }
+       
+protected:
+       
+       void
+       fixFilename(string* filename)
+       {
+               std::string::size_type i=0;
+               while(1)
+               {
+                       i = filename->find(" ",i);
+                       if(i == string::npos)
+                               break;
+                       filename->replace(i,1,"\\ ");
+                       i+=2;
+               }
+       }
+
+       void
+       unfixFilename(string* filename)
+       {
+               std::string::size_type i=0;
+               i=filename->find("\\",0);
+               if(i == string::npos)
+                       return;
+               else
+               {
+                       filename->replace(i,1,"");
+                       unfixFilename(filename);
+               }
+               
+       }
+               
+       sqlitedatabase *db;
+       sqlitequery *q;
+       string table;
+       string tableString;
+
+}; //BaseDB class
+
+#endif
+
diff --git a/plugins/database/baseobject.h b/plugins/database/baseobject.h
new file mode 100644 (file)
index 0000000..a36ddd4
--- /dev/null
@@ -0,0 +1,37 @@
+/*\r
+ * timedate - Displays time and date and daily events\r
+ * Copyright (c) <2009>, Intel Corporation.\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify it\r
+ * under the terms and conditions of the GNU Lesser General Public License,\r
+ * version 2.1, as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for\r
+ * more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public License along with\r
+ * this program; if not, write to the Free Software Foundation, Inc., \r
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ */\r
+\r
+#ifndef _BASEOBJECTDAO_H_\r
+#define _BASEOBJECTDAO_H_\r
+\r
+#include "basedb.hpp"\r
+\r
+#include<string>\r
+#include<vector>\r
+\r
+class BaseObject\r
+{\r
+public:\r
+       BaseObject():id(-1){}\r
+       virtual ~BaseObject(){}\r
+       int id;\r
+};\r
+\r
+#endif\r
+\r
diff --git a/plugins/database/databasesink.cpp b/plugins/database/databasesink.cpp
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/plugins/database/databasesink.h b/plugins/database/databasesink.h
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/plugins/database/sqlitedatabase.cpp b/plugins/database/sqlitedatabase.cpp
new file mode 100644 (file)
index 0000000..7dccc0d
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * timedate - Displays time and date and daily events
+ * Copyright (c) <2009>, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include "sqlitedatabase.h"
+#include <stdio.h>
+#include <sqlite3.h>
+
+
+bool
+sqlitedatabase::init(const std::string & d)
+{
+       setDatabase(d);
+       return true;
+}
+
+sqlitedatabase::~sqlitedatabase()
+{
+       sqlite3_close((sqlite3 *)m_odb->db);
+}
+
+bool sqlitedatabase::Connected()
+{
+       SqliteDB * odb = grabdb();
+       if(!odb)
+       {
+               return false;
+       }
+       freedb(odb);
+       return true;
+}
+
+void sqlitedatabase::freedb(SqliteDB * odb)
+{
+       if(odb)
+       {
+               odb->busy = false;
+       }
+}
+
+SqliteDB * sqlitedatabase::grabdb()
+{
+       SqliteDB * odb = NULL;
+
+       if(!odb)
+       {
+               odb = new SqliteDB;
+               if(!odb)
+               {
+                       printf("grabdb: SqliteDB struct couldn't be created");
+                       return NULL;
+               }
+               void * p = &odb->db;
+               if(sqlite3_open(database.c_str(), (sqlite3 **)p))
+               {
+                       printf("Can't open database: %s\n", sqlite3_errmsg((sqlite3 *)odb->db));
+                       sqlite3_close((sqlite3 *)odb->db);
+                       delete odb;
+                       return NULL;
+               }
+               odb->busy = true;
+       }
+       else
+       {
+               odb->busy = true;
+       }
+       m_odb = odb;
+
+       return odb;
+}
+
+
diff --git a/plugins/database/sqlitedatabase.h b/plugins/database/sqlitedatabase.h
new file mode 100644 (file)
index 0000000..953013c
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * timedate - Displays time and date and daily events
+ * Copyright (c) <2009>, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef SQLITEDATABASE_H
+#define SQLITEDATABASE_H
+
+#include <string>
+#include <pthread.h>
+#include <stdint.h>
+#include <list>
+
+
+struct SqliteDB
+{
+       SqliteDB() : busy(false){}
+       void * db;
+       bool busy;
+};     // struct SqliteDB
+
+class sqlitedatabase
+{
+public:
+       sqlitedatabase() {}
+       virtual ~sqlitedatabase();
+               
+       bool init(const std::string & d);
+
+       virtual void setHost(const std::string &){}
+       virtual void setUser(const std::string &){}
+       virtual void setPassword(const std::string &){}
+       virtual void setDatabase(const std::string & db){database = db;}
+
+       virtual void OnInit(SqliteDB *){}
+       virtual bool Connected();
+       virtual SqliteDB * grabdb();
+       virtual void freedb(SqliteDB * odb);
+       
+protected:
+       SqliteDB *m_odb;
+       bool m_embedded;
+       std::string database;
+};
+
+#endif
+
diff --git a/plugins/database/sqlitequery.cpp b/plugins/database/sqlitequery.cpp
new file mode 100644 (file)
index 0000000..223c4d0
--- /dev/null
@@ -0,0 +1,402 @@
+/*
+ * timedate - Displays time and date and daily events
+ * Copyright (c) <2009>, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include "sqlitequery.h"
+#include <stdio.h>
+#include <sqlite3.h>
+#include <string>
+#include "utils.h"
+
+using namespace std;
+
+bool
+sqlitequery::init(sqlitedatabase * db)
+{
+       m_db = db;
+       odb = (db ? db->grabdb() : NULL);
+       return true;
+}
+
+bool
+sqlitequery::init(sqlitedatabase * dbin, const std::string & sql)
+{
+       init(dbin);
+       return execute(sql);
+}
+
+sqlitequery::~sqlitequery()
+{
+       if(result)
+       {
+               printf( "sqlite3_finalize in destructor\n");
+               sqlite3_finalize(result);
+       }
+       if(odb)
+       {
+               m_db->freedb(odb);
+       }
+}
+
+bool sqlitequery::execute(const std::string & sql)
+{
+       std::string sql_sqlite=sql;
+       m_last_query = sql_sqlite;
+       if(odb && result)
+       {
+               string err = "execute: query busy: "+sql_sqlite;
+               printf("%s\n", err.c_str());
+       }
+       if(odb && !result)
+       {
+               const char * s = NULL;
+               int err = sqlite3_busy_timeout((sqlite3 *)odb->db, 10000 );
+
+               if(err!= SQLITE_OK)
+               {
+                       printf("execute: busy timeout occured: \n");
+                       return false;
+               }
+               int rc = sqlite3_prepare_v2((sqlite3 *)odb->db, sql_sqlite.c_str(), sql_sqlite.size(), &result, &s);
+               
+               if(rc != SQLITE_OK)
+               {
+                       string err = "execute: prepare query failed: "+sql_sqlite;
+                       printf("%s\n", err.c_str());
+                       result = NULL;
+                       return false;
+               }
+               if(!result)
+               {
+                       printf( "execute: query failed\n");
+                       result = NULL;
+                       return false;
+               }
+               rc = sqlite3_step(result);
+               sqlite3_finalize(result);
+               result = NULL;
+               switch(rc)
+               {
+               case SQLITE_BUSY:
+                       printf( "execute: database busy\n");
+                       return false;
+               case SQLITE_DONE:
+               case SQLITE_ROW:
+                       return true;
+               case SQLITE_SCHEMA:
+                       printf( "execute: Schema error\n");
+                       return false;
+               case SQLITE_ERROR:
+                       printf("%s\n", sqlite3_errmsg((sqlite3 *)odb->db));
+                       return false;
+               case SQLITE_MISUSE:
+                       printf( "execute: database misuse\n");
+                       return false;
+               default:
+                       printf( "execute: unknown result code\n");
+               }
+       }
+       return false;
+}
+
+bool sqlitequery::fetchRow()
+{
+       rowcount = 0;
+       row = false;
+       if(odb && result)
+       {
+               int rc = cache_rc_valid ? cache_rc : sqlite3_step(result);
+               cache_rc_valid = false;
+               switch(rc)
+               {
+               case SQLITE_BUSY:
+                       printf( "execute: database busy\n");
+                       return false;
+               case SQLITE_DONE:
+                       return false;
+               case SQLITE_ROW:
+                       row = true;
+                       return true;
+               case SQLITE_ERROR:
+                       printf("%s\n", sqlite3_errmsg((sqlite3 *)odb->db));
+                       return false;
+               case SQLITE_MISUSE:
+                       printf( "execute: database misuse\n");
+                       return false;
+               default:
+                       printf( "execute: unknown result code\n");
+               }
+       }
+       return false;
+}
+
+void sqlitequery::freeResult()
+{
+       if(odb && result)
+       {
+               sqlite3_finalize(result);
+               result = NULL;
+               row = false;
+               cache_rc_valid = false;
+       }
+       while(m_nmap.size())
+       {
+               std::map<std::string, int>::iterator it = m_nmap.begin();
+               m_nmap.erase(it);
+       }
+}
+
+void sqlitequery::resetStatement()
+{
+       if( odb && result )
+               sqlite3_reset(result);
+}
+
+long sqlitequery::getCount(const std::string & sql)
+{
+       long l(0);
+       if(getResult(sql))
+       {
+               if(fetchRow())
+               {
+                       l = getVal(rowcount++);
+               }
+               freeResult();
+       }
+       return l;
+}
+
+bool sqlitequery::prepareStatement(const std::string & sql)
+{
+       std::string sql_sqlite=sql;
+       m_last_query = sql_sqlite;
+       if(odb && result)
+       {
+               string err = "prepareStatement: query busy: "+sql_sqlite;
+               printf("%s\n", err.c_str());
+       }
+       if(odb && !result)
+       {
+               const char * s = NULL;
+               int rc = sqlite3_prepare_v2((sqlite3 *)odb->db, sql_sqlite.c_str(), sql_sqlite.size(), &result, &s);
+               if(rc != SQLITE_OK)
+               {
+                       string err = "prepareStatement: prepare query failed: "+sql_sqlite;
+                       printf("%s\n", err.c_str());
+                       return false;
+               }
+               if(!result)
+               {
+                       printf( "prepareStatement: query failed\n");
+                       return false;
+               }
+       }
+       return result;
+}
+
+bool sqlitequery::getResult(const std::string & sql)
+{
+       if(prepareStatement(sql))
+       {
+               int i(0);
+               const char * p = sqlite3_column_name(result, i);
+               while(p)
+               {
+                       m_nmap[p] = ++i;
+                       p = sqlite3_column_name(result, i);
+               }
+               m_num_cols = i;
+               cache_rc = sqlite3_step(result);
+               cache_rc_valid = true;
+               m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
+       }
+       return result;
+}
+
+bool sqlitequery::bind(const std::string bindMatch)
+{
+       if(!odb || !result) return false;
+       
+       int r = sqlite3_bind_text(result,1,bindMatch.c_str(),bindMatch.length(),NULL);
+       
+       if(r != SQLITE_OK)
+       {
+               printf("sqlitequery::bind - error binding query\n");
+               return false;
+       }
+       
+       int i(0);
+       const char * p = sqlite3_column_name(result, i);
+       while(p)
+       {
+               m_nmap[p] = ++i;
+               p = sqlite3_column_name(result, i);
+       }
+       m_num_cols = i;
+       cache_rc = sqlite3_step(result);
+       cache_rc_valid = true;
+       m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
+       
+       return true;
+}
+
+bool sqlitequery::bind(const int value)
+{
+       if(!odb || !result) return false;
+       
+       int r = sqlite3_bind_int(result,1,value);
+       
+       if(r != SQLITE_OK)
+       {
+               printf("sqlitequery::bind - error binding query\n");
+               return false;
+       }
+       
+       int i(0);
+       const char * p = sqlite3_column_name(result, i);
+       while(p)
+       {
+               m_nmap[p] = ++i;
+               p = sqlite3_column_name(result, i);
+       }
+       
+       m_num_cols = i;
+       cache_rc = sqlite3_step(result);
+       cache_rc_valid = true;
+       m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
+       
+       return true;
+}
+
+bool sqlitequery::bind(const double value)
+{
+       if(!odb || !result) return false;
+       
+       int r = sqlite3_bind_double(result,1,value);
+       
+       if(r != SQLITE_OK)
+       {
+               printf("sqlitequery::bind - error binding query\n");
+               return false;
+       }
+       
+       int i(0);
+       const char * p = sqlite3_column_name(result, i);
+       while(p)
+       {
+               m_nmap[p] = ++i;
+               p = sqlite3_column_name(result, i);
+       }
+       m_num_cols = i;
+       cache_rc = sqlite3_step(result);
+       cache_rc_valid = true;
+       m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
+       
+       return cache_rc == SQLITE_OK;
+}
+
+int64_t sqlitequery::getBigInt(int x)
+{
+       if(odb && result && row)
+       {
+               return sqlite3_column_int64(result, x);
+       }
+       return 0;
+}
+
+int sqlitequery::GetErrno()
+{
+       if(odb)
+       {
+               return sqlite3_errcode((sqlite3 *)odb->db);
+       }
+       return 0;
+}
+
+std::string sqlitequery::GetError()
+{
+       if(odb)
+       {
+               return sqlite3_errmsg((sqlite3 *)odb->db);
+       }
+       return "";
+}
+
+double sqlitequery::getNum(int x)
+{
+       if(odb && result && row)
+       {
+               return sqlite3_column_double(result, x);
+       }
+       return 0;
+}
+
+const char * sqlitequery::getStr(int x)
+{
+       if(odb && result && row && x < sqlite3_column_count(result))
+       {
+               const unsigned char * tmp = sqlite3_column_text(result, x);
+               return tmp ? (const char *)tmp : "";
+       }
+       return "";
+}
+
+uint64_t sqlitequery::getUBigInt(int x)
+{
+       if(odb && result && row)
+       {
+               return (uint64_t)sqlite3_column_int64(result, x);
+       }
+       return 0;
+}
+
+unsigned long sqlitequery::getUVal(int x)
+{
+       if(odb && result && row)
+       {
+               return (unsigned long)sqlite3_column_int(result, x);
+       }
+       return 0;
+}
+
+long sqlitequery::getVal(int x)
+{
+       if(odb && result && row)
+       {
+               return sqlite3_column_int(result, x);
+       }
+       return 0;
+}
+
+bool sqlitequery::isNull(int x)
+{
+       if(odb && result && row)
+       {
+               if(sqlite3_column_type(result, x) == SQLITE_NULL)
+               {
+                       return true;
+               }
+       }
+       return false;
+}
+
+long sqlitequery::numRows()
+{
+       return odb && result ? m_row_count : 0;
+}
+
diff --git a/plugins/database/sqlitequery.h b/plugins/database/sqlitequery.h
new file mode 100644 (file)
index 0000000..4679619
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * timedate - Displays time and date and daily events
+ * Copyright (c) <2009>, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef SQLITEQUERY_H
+#define SQLITEQUERY_H
+
+#include "sqlitedatabase.h"
+#include <map>
+
+class sqlite3_stmt;
+
+class sqlitequery 
+{
+public:
+       sqlitequery() :  result(NULL), row(false), cache_rc(0), cache_rc_valid(false), m_row_count(0), m_db(NULL),  m_num_cols(0)  {}
+       virtual ~sqlitequery();
+
+       bool init(sqlitedatabase * dbin);
+       bool init(sqlitedatabase * dbin, const std::string & sql);
+
+       virtual bool Connected(){return odb ? true : false;}
+       virtual bool execute(const std::string & sql);
+       virtual bool fetchRow();
+       virtual long numRows();
+       virtual void freeResult();
+       virtual void resetStatement();
+       virtual long getCount(const std::string & sql);
+       virtual bool prepareStatement(const std::string & sql);
+       virtual bool getResult(const std::string & sql);
+       virtual bool bind(const std::string bindMatch);
+       virtual bool bind(const int value);
+       virtual bool bind(const double value);
+       virtual int64_t getBigInt(){return getBigInt(rowcount++);}
+       virtual int64_t getBigInt(int x);
+       virtual int GetErrno();
+       virtual std::string GetError();
+       virtual double getNum(){ return getNum(rowcount++); }
+       virtual double getNum(int x);
+       virtual const char * getStr(){return getStr(rowcount++);}
+       virtual const char * getStr(int x);
+       virtual uint64_t getUBigInt(){ return getUBigInt(rowcount++); }
+       virtual uint64_t getUBigInt(int x);
+       virtual unsigned long getUVal(){ return getUVal(rowcount++); }
+       virtual unsigned long getUVal(int x);
+       virtual long getVal(){ return getVal(rowcount++); }
+       virtual long getVal(int x);
+       virtual bool isNull(int x);
+protected:
+       sqlite3_stmt * result;
+       bool row;
+       int cache_rc;
+       bool cache_rc_valid;
+       int m_row_count;
+       
+       virtual sqlitequery & operator=(const sqlitequery &){return *this;}
+
+       sqlitedatabase * m_db;
+       std::string m_last_query;
+       short rowcount;
+       std::string m_tmpstr;
+       std::map<std::string, int> m_nmap;
+       int m_num_cols;
+       SqliteDB * odb;
+       
+private:
+       std::string sql_replace_tokens(std::string sqlstring,std::string &dest);
+};
+
+#endif
+
diff --git a/plugins/database/utils.cpp b/plugins/database/utils.cpp
new file mode 100644 (file)
index 0000000..208da86
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * timedate - Displays time and date and daily events
+ * Copyright (c) <2009>, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include "utils.h"
+#include <stdlib.h>
+
+using namespace std;
+
+string 
+DaoUtils::findReplace(string name, string tofind, string replacewith, string exclusions)
+{
+       uint i=0;
+       
+       uint exclusionPos = exclusions.find(tofind,0);
+       
+       while(1)
+       {
+               i = name.find(tofind,i);
+               
+               if(i != string::npos && exclusionPos != string::npos)
+               {
+                       if(name.substr(i-exclusionPos,exclusions.length()) == exclusions)
+                       {
+                               i+=replacewith.size();
+                               continue;
+                       }
+               }
+               
+               if(i == string::npos)
+                       break;
+               name.replace(i,tofind.size(),replacewith);
+               i+=replacewith.size();
+       }
+       return name;
+}
+
diff --git a/plugins/database/utils.h b/plugins/database/utils.h
new file mode 100644 (file)
index 0000000..c1c395d
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * timedate - Displays time and date and daily events
+ * Copyright (c) <2009>, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef _DAOUTILS_H_
+#define _DAOUTILS_H_
+
+#include <string>
+
+class DaoUtils
+{
+public:
+       DaoUtils(){}
+       virtual ~DaoUtils(){}
+
+       static std::string findReplace(std::string name, std::string tofind, std::string replacewith, std::string exclusions="");
+};
+
+
+#endif