src/include/common/client/SQLKeyValueStore.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_SQL_KEY_VALUE_STORE
00037 #define INCL_SQL_KEY_VALUE_STORE
00038 
00042 #include "base/util/KeyValueStore.h"
00043 #include "base/util/ArrayListEnumeration.h"
00044 #include "base/util/Enumeration.h"
00045 
00046 BEGIN_NAMESPACE
00047 
00057 class SQLKeyValueStore : public KeyValueStore {
00058 private:
00059     
00060     StringBuffer    table,
00061                     colKey,
00062                     colValue;
00063          
00064     ArrayListEnumeration toSet;
00065     ArrayListEnumeration toDel;
00066     
00067 protected:
00068 
00069     
00070     /*
00071      * Execute a query to get a value, given the key.
00072      *
00073      * The sql query passed to this function 
00074      *
00075      * @param sql   - The sql command to execute.  This MUST select the key, and
00076      * the value in that order, as the first 2 columns selected.  Additional
00077      * columns will be ignored.
00078      *
00079      * @return      - The result of the query - an Enumeration of KeyValuePair s
00080      */
00081     virtual Enumeration& query(const StringBuffer & sql) const = 0;
00082     
00083     /*
00084      * Execute a non-select query.
00085      *
00086      * The sql query passed to this function MUST NOT return any data (e.g. not a select)
00087      *
00088      * @param sql   - The sql command to execute.
00089      *
00090      * @return      - Success or Failure
00091      */
00092     virtual int execute(const StringBuffer & sql) = 0;
00093     
00094     /*
00095      * Get the name of the key column
00096      *
00097      * @return      - A StringBuffer with the name of the column used for the key
00098      */
00099     virtual StringBuffer sqlColKey() const;
00100     
00101     /*
00102      * Get the name of the value column
00103      *
00104      * @return      - A StringBuffer with the name of the column used for the value
00105      */
00106     virtual StringBuffer sqlColValue() const;
00107 
00108     /*
00109      * Get the query to remove a property
00110      *
00111      * @param key   - The key to be removed
00112      *
00113      * @return      - A StringBuffer containing the query
00114      */
00115     virtual StringBuffer sqlRemovePropertyString(const StringBuffer & key) const;
00116 
00117     /*
00118      * Get the query to set a property
00119      *
00120      * @param key   - The key to have its value updated
00121      * @param value - The value to be saved
00122      *
00123      * @return      - A StringBuffer containing the query
00124      */
00125     virtual StringBuffer sqlSetPropertyString(const StringBuffer & key, const StringBuffer & value) const;
00126     
00127     /*
00128      * Get the query to get a property
00129      *
00130      * @param key   - The key to retreive
00131      *
00132      * @return      - A StringBuffer containing the query
00133      */
00134     virtual StringBuffer sqlGetPropertyString(const StringBuffer & key) const;
00135     
00136     /*
00137      * Get the query to get all properties
00138      *
00139      * @return      - A StringBuffer containing the query
00140      */
00141     virtual StringBuffer sqlGetAllString() const;
00142         
00143     /*
00144      * Get the query to count properties
00145      *
00146      * @return      - A StringBuffer containing the query
00147      */
00148     virtual StringBuffer sqlCountAllString() const;
00149     
00150 public:
00151     
00152     /*
00153      * Constructor
00154      *
00155      * @param table     - The table to be used
00156      * @param colKey    - The column of the key
00157      * @param colValue  - The column of the value
00158      *
00159      */
00160     SQLKeyValueStore(const StringBuffer & table, const StringBuffer & colKey, const StringBuffer & colValue);
00161     
00162     /*
00163      * Destructor
00164      *
00165      * Subclasses MUST call disconnect() at the same at which
00166      * disconnect() is defined
00167      */
00168     virtual ~SQLKeyValueStore();
00169     
00170     /*
00171      * Connect to the database server.  The connection should be stored
00172      * within the subclass and destroyed in disconnect();  If connect is called
00173      * while a connection exists, nothing should happen.
00174      *
00175      * @return      - Success or Failure
00176      */
00177     virtual int connect() = 0;
00178     
00179     /*
00180      * Disconnect from the database server.  If the connection is not open,
00181      * do nothing.
00182      *
00183      * @return      - Success or Failure
00184      */
00185     virtual int disconnect() = 0;
00186     
00187     /*
00188      * Returns the value of the given property
00189      *
00190      *@param prop - the property name
00191      *
00192      *@return   A NULL StringBuffer in the returned implies that
00193      *          the property was not set. Otherwise the value it was
00194      *          set to is returned (which can be "", the empty string).
00195      */
00196     virtual StringBuffer readPropertyValue(const char *prop) const;
00197 
00198     /*
00199      * Sets a property value.
00200      *
00201      * The value might be cached inside the implementation of this
00202      * interface. To ensure that it is stored persistently and to do
00203      * error checking, call save().
00204      *
00205      * @param prop      - the property name
00206      * @param value     - the property value (zero terminated string)
00207      *
00208      * @return int 0 on success, an error code otherwise
00209      */
00210     virtual int setPropertyValue(const char *prop, const char *value);
00211     
00219     virtual int removeProperty(const char *prop);
00220      
00224     virtual Enumeration& getProperties() const;
00225 
00233     virtual int save() = 0;
00234 };
00235 
00236 
00237 END_NAMESPACE
00238 
00241 #endif

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