src/include/common/client/SQLiteKeyValueStore.h

00001 /*
00002  * Funambol is a mobile platform developed by Funambol, Inc. 
00003  * Copyright (C) 2008 Funambol, Inc.
00004  * 
00005  * This program is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Affero General Public License version 3 as published by
00007  * the Free Software Foundation with the addition of the following permission 
00008  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
00009  * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
00010  * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015  * details.
00016  * 
00017  * You should have received a copy of the GNU Affero General Public License 
00018  * along with this program; if not, see http://www.gnu.org/licenses or write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00020  * MA 02110-1301 USA.
00021  * 
00022  * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
00023  * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
00024  * 
00025  * The interactive user interfaces in modified source and object code versions
00026  * of this program must display Appropriate Legal Notices, as required under
00027  * Section 5 of the GNU Affero General Public License version 3.
00028  * 
00029  * In accordance with Section 7(b) of the GNU Affero General Public License
00030  * version 3, these Appropriate Legal Notices must retain the display of the
00031  * "Powered by Funambol" logo. If the display of the logo is not reasonably 
00032  * feasible for technical reasons, the Appropriate Legal Notices must display
00033  * the words "Powered by Funambol".
00034  */
00035 
00036 #ifndef INCL_SQLLITE_KEY_VALUE_STORE
00037 #define INCL_SQLLITE_KEY_VALUE_STORE
00038 
00042 #include <sqlite3.h>
00043 
00044 #include "base/util/KeyValueStore.h"
00045 #include "base/util/KeyValuePair.h"
00046 #include "client/SQLKeyValueStore.h"
00047 
00048 BEGIN_NAMESPACE
00049 
00050 class SQLiteKeyValueStore : public SQLKeyValueStore {
00051 private:
00052     
00053     StringBuffer    path;
00054     
00055     sqlite3         * db;
00056     mutable sqlite3_stmt    * statement;
00057     
00058     
00059     
00060     int enumeration_lastReturn;
00061     int enumeration_totalRows;
00062     int enumeration_nextRow;
00063     KeyValuePair enumeration_kvp;
00064     
00065     bool isTransactional;
00066     
00067 protected:
00068 
00069     
00070     /*
00071      * Execute a query to get a value, given the key.   If a connection to
00072      * the database is not open, open it. 
00073      *
00074      * @param sql   - The sql command to execute.
00075      *
00076      * @return      - The result of the query - an Enumeration of KeyValuePair s
00077      */
00078     virtual Enumeration & query(const StringBuffer & sql) const;
00079     
00080     /*
00081      * Execute a non-select query.  If a connection to the database is not open,
00082      * open it.
00083      *
00084      * @param sql   - The sql command to execute.
00085      *
00086      * @return      - Success or Failure
00087      */
00088     virtual int execute(const StringBuffer & sql);
00089     
00090 
00091     
00092     virtual bool enumeration_hasMoreElement()
00093     {
00094         return (statement && enumeration_lastReturn == SQLITE_ROW && enumeration_nextRow < enumeration_totalRows);
00095     }
00096     
00097     virtual ArrayElement* enumeration_getNextElement()
00098     {
00099         StringBuffer sb;
00100         sb = (const char *) sqlite3_column_text(statement, 0);
00101         enumeration_kvp.setKey   (sb);
00102         sb = (const char *) sqlite3_column_text(statement, 1);
00103         enumeration_kvp.setValue (sb);
00104 
00105         enumeration_lastReturn = sqlite3_step(statement);
00106         enumeration_nextRow++;
00107 
00108         return &enumeration_kvp;
00109     }
00110     
00111 public:
00112     
00113     mutable
00114     class SQLiteKeyValueStoreEnumeration : public Enumeration
00115     {
00116     protected:
00117         friend class SQLiteKeyValueStore;
00118     
00119         SQLiteKeyValueStore & skvs;
00120         
00121         void reinit(int lastRet)
00122         {
00123             skvs.enumeration_lastReturn = lastRet;
00124             skvs.enumeration_totalRows  = 1;
00125             skvs.enumeration_nextRow    = 0;
00126         }
00127         
00128         void setTotalRows(int tr)
00129         {
00130             skvs.enumeration_totalRows = tr;
00131         }
00132         
00133         
00134     public:
00135         
00136         SQLiteKeyValueStoreEnumeration(SQLiteKeyValueStore & instance)
00137         : skvs(instance) {}
00138         
00142         virtual bool hasMoreElement() const
00143         {
00144             return skvs.enumeration_hasMoreElement();
00145         }
00146 
00150         virtual ArrayElement* getNextElement()
00151         {
00152             return skvs.enumeration_getNextElement();
00153         }
00154 
00155     } enumeration;
00156     
00157     
00158     /*
00159      * Constructor
00160      *
00161      * @param uri             - The location of the server
00162      * @param colKey          - The name of the column for the key
00163      * @param colValue        - The name of the column of the value
00164      * @param path            - The name of the full path to the database to use
00165      * @param isTransactional - A bool to enable 
00166      *
00167      */
00168     SQLiteKeyValueStore(const StringBuffer & table, const StringBuffer & colKey, const StringBuffer & colValue,
00169                         const StringBuffer & path, bool isTransactional = false);
00170     
00171     /*
00172      * Destructor
00173      *
00174      * Subclasses MUST disconnect in their own destructors
00175      */
00176     virtual ~SQLiteKeyValueStore();
00177     
00181     virtual Enumeration& getProperties();
00182     
00183     /*
00184      * Connect to the database server.  The connection should be stored
00185      * within the subclass and destroyed in disconnect();  If connect is called
00186      * while a connection exists, nothing should happen.
00187      *
00188      * @return      - Success or Failure
00189      */
00190     int connect();
00191     
00192     /*
00193      * Disconnect from the database server.  If the connection is not open,
00194      * do nothing.
00195      *
00196      * @return      - Success or Failure
00197      */
00198     int disconnect();
00199     
00207      virtual int close();
00208     
00214     int init(const char* table, const char* colKey, const char* colValue, const char* path);
00215     
00223     bool checkIfTableExists(const char* tableName);
00224 };
00225 
00226 
00227 END_NAMESPACE
00228 
00231 #endif

Generated on Wed Jan 14 17:15:36 2009 for Funambol C++ Client Library by  doxygen 1.5.2