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 protected:
00066 
00067     
00068     /*
00069      * Execute a query to get a value, given the key.   If a connection to
00070      * the database is not open, open it. 
00071      *
00072      * @param sql   - The sql command to execute.
00073      *
00074      * @return      - The result of the query - an Enumeration of KeyValuePair s
00075      */
00076     virtual Enumeration & query(const StringBuffer & sql) const;
00077     
00078     /*
00079      * Execute a non-select query.  If a connection to the database is not open,
00080      * open it.
00081      *
00082      * @param sql   - The sql command to execute.
00083      *
00084      * @return      - Success or Failure
00085      */
00086     virtual int execute(const StringBuffer & sql);
00087     
00088     virtual bool enumeration_hasMoreElement()
00089     {
00090         return (statement && enumeration_lastReturn == SQLITE_ROW && enumeration_nextRow < enumeration_totalRows);
00091     }
00092     
00093     virtual ArrayElement* enumeration_getNextElement()
00094     {
00095         StringBuffer sb;
00096         sb = (const char *) sqlite3_column_text(statement, 0);
00097         enumeration_kvp.setKey   (sb);
00098         sb = (const char *) sqlite3_column_text(statement, 1);
00099         enumeration_kvp.setValue (sb);
00100 
00101         enumeration_lastReturn = sqlite3_step(statement);
00102         enumeration_nextRow++;
00103 
00104         return &enumeration_kvp;
00105     }
00106     
00107 public:
00108     
00109     mutable
00110     class SQLiteKeyValueStoreEnumeration : public Enumeration
00111     {
00112     protected:
00113         friend class SQLiteKeyValueStore;
00114     
00115         SQLiteKeyValueStore & skvs;
00116         
00117         void reinit(int lastRet)
00118         {
00119             skvs.enumeration_lastReturn = lastRet;
00120             skvs.enumeration_totalRows  = 1;
00121             skvs.enumeration_nextRow    = 0;
00122         }
00123         
00124         void setTotalRows(int tr)
00125         {
00126             skvs.enumeration_totalRows = tr;
00127         }
00128         
00129         
00130     public:
00131         
00132         SQLiteKeyValueStoreEnumeration(SQLiteKeyValueStore & instance)
00133         : skvs(instance) {}
00134         
00138         virtual bool hasMoreElement() const
00139         {
00140             return skvs.enumeration_hasMoreElement();
00141         }
00142 
00146         virtual ArrayElement* getNextElement()
00147         {
00148             return skvs.enumeration_getNextElement();
00149         }
00150 
00151     } enumeration;
00152     
00153     
00154     /*
00155      * Constructor
00156      *
00157      * @param uri       - The location of the server
00158      * @param colKey    - The name of the column for the key
00159      * @param colValue  - The name of the column of the value
00160      * @param path      - The name of the full path to the database to use
00161      *
00162      */
00163     SQLiteKeyValueStore(const StringBuffer & table, const StringBuffer & colKey,   const StringBuffer & colValue,
00164                         const StringBuffer & path);
00165     
00166     /*
00167      * Destructor
00168      *
00169      * Subclasses MUST disconnect in their own destructors
00170      */
00171     virtual ~SQLiteKeyValueStore();
00172     
00176     virtual Enumeration& getProperties() const;
00177     
00178     /*
00179      * Connect to the database server.  The connection should be stored
00180      * within the subclass and destroyed in disconnect();  If connect is called
00181      * while a connection exists, nothing should happen.
00182      *
00183      * @return      - Success or Failure
00184      */
00185     int connect();
00186     
00187     /*
00188      * Disconnect from the database server.  If the connection is not open,
00189      * do nothing.
00190      *
00191      * @return      - Success or Failure
00192      */
00193     int disconnect();
00194     
00202     virtual int save();
00203 };
00204 
00205 
00206 END_NAMESPACE
00207 
00210 #endif

Generated on Tue Jun 10 17:20:21 2008 for Funambol C++ Client Library by  doxygen 1.5.2