GenericIterable.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _STORAGE_GENERICITERABLE_H
00014 #define _STORAGE_GENERICITERABLE_H
00015 
00022 #include <support/IIterable.h>
00023 #include <support/IRandomIterator.h>
00024 #include <support/Locker.h>
00025 #include <support/SortedVector.h>
00026 
00027 #if _SUPPORTS_NAMESPACE
00028 namespace palmos {
00029 namespace storage {
00030 using namespace support;
00031 #endif
00032 
00037 
00038 
00040 
00042 
00044 B_CONST_STRING_VALUE_LARGE(BV_ITERABLE_SELECT,      "select", );
00045 
00047 
00052 B_CONST_STRING_VALUE_LARGE(BV_ITERABLE_DATABASE,    "database", );
00053 
00055 
00063 B_CONST_STRING_VALUE_LARGE(BV_ITERABLE_FILTER,      "filter", );
00064 
00066 
00069 B_CONST_STRING_VALUE_LARGE(BV_ITERABLE_WHERE,       "where", );
00070 
00072 B_CONST_STRING_VALUE_SMALL(BV_ITERABLE_WHERE_LHS,   "lhs", );
00074 B_CONST_STRING_VALUE_SMALL(BV_ITERABLE_WHERE_RHS,   "rhs", );
00076 B_CONST_STRING_VALUE_SMALL(BV_ITERABLE_WHERE_OP,    "op", );
00077 
00079 
00085 B_CONST_STRING_VALUE_LARGE(BV_ITERABLE_ORDER_BY,    "order_by", );
00086 
00087 enum {
00088     B_ORDER_BY_ASCENDING    = 0,
00089     B_ORDER_BY_DESCENDING   = 1,
00090 
00091     B_ORDER_BY_CASELESS     = 0,
00092     B_ORDER_BY_CASED        = 2
00093 };
00094 
00096 
00099 B_CONST_STRING_VALUE_SMALL(BV_ITERABLE_SQL,         "sql", );
00100 
00102 
00103 // ==========================================================================
00104 // ==========================================================================
00105 
00107 
00109 class SSQLBuilder
00110 {
00111 public:
00112     // --------------------------------------------------------------
00116                                 SSQLBuilder();
00117     virtual                     ~SSQLBuilder();
00118 
00120 
00121     // --------------------------------------------------------------
00126 
00128             status_t            BuildSQLString( SString* outSql,
00129                                                 SValue* inoutArgs,
00130                                                 const SString& dbname) const;
00131 
00133             status_t            BuildSQLSubStrings( SString* outSelect,
00134                                                     SString* outWhere,
00135                                                     SString* outOrderBy,
00136                                                     SValue* inoutArgs) const;
00137 
00139 
00145     virtual void                MapSQLColumn(SValue* inoutColumn) const;
00146 
00148 
00160     virtual void                MapSQLOrderColumn(SValue* inoutColumnName, SValue* inoutOrder) const;
00161 
00163 
00166     virtual SValue              CreateSQLFilter(const SValue& filter) const;
00167 
00169 
00170 private:
00171                                 SSQLBuilder(const SSQLBuilder& o);
00172             SSQLBuilder&            operator=(const SSQLBuilder& o);
00173 };
00174 
00175 // ==========================================================================
00176 // ==========================================================================
00177 
00179 
00191 class BGenericIterable : public BnIterable, public SSQLBuilder
00192 {
00193 public:
00194     // --------------------------------------------------------------
00198                                     BGenericIterable();
00199                                     BGenericIterable(const SContext& context);
00200 protected:
00201     virtual                         ~BGenericIterable();
00202 public:
00203 
00204 
00206 
00212     virtual lock_status_t           Lock() const;
00213 
00215     virtual void                    Unlock() const;
00216 
00218 
00219     // ---------------------------------------------------------------------
00223 
00224 
00226     virtual sptr<IIterator>         NewIterator(const SValue& args = B_UNDEFINED_VALUE, status_t* error = NULL);
00228 
00229     // ---------------------------------------------------------------------
00235 
00236             class GenericIterator;
00237 
00239 
00241     virtual sptr<GenericIterator>   NewGenericIterator(const SValue& args) = 0;
00242 
00244 
00245     // --------------------------------------------------------------
00249 
00250     typedef bool                    (*for_each_iterator_func)(  const sptr<BGenericIterable>& iterable,
00251                                                                 const sptr<GenericIterator>& iterator,
00252                                                                 void* cookie);
00253 
00255             void                    ForEachIteratorLocked(for_each_iterator_func func, void* cookie);
00256 
00258             void                    PushIteratorChangedLocked();
00259 
00261 
00262 private:
00263                                     BGenericIterable(const BGenericIterable& copyFrom);
00264                                     BGenericIterable& operator=(const BGenericIterable& copyFrom);
00265 
00266     friend class GenericIterator;
00267 
00268             mutable SLocker         m_lock;
00269             SSortedVector<wptr<GenericIterator> >
00270                                     m_iterators;
00271 };
00272 
00273 // --------------------------------------------------------------------------
00274 
00276 
00279 class BGenericIterable::GenericIterator : public BnRandomIterator, public SSQLBuilder
00280 {
00281 public:
00282     // --------------------------------------------------------------
00286                                 GenericIterator(const SContext& context, const sptr<BGenericIterable>& owner);
00287 protected:
00288     virtual                     ~GenericIterator();
00290     virtual void                InitAtom();
00292     virtual status_t            FinishAtom(const void* id);
00293 public:
00294 
00296 
00300     virtual SValue              Inspect(const sptr<IBinder>& caller, const SValue& which, uint32_t flags);
00302     virtual status_t            StatusCheck() const;
00303 
00305     inline  const sptr<BGenericIterable>&
00306                                 Owner() const { return m_owner; }
00307 
00309 
00310     // ---------------------------------------------------------------------
00316 
00318 
00322     virtual SValue              Options() const;
00324     virtual status_t            Next(IIterator::ValueList* keys, IIterator::ValueList* values, uint32_t flags, size_t count);
00326     virtual status_t            Remove();
00327 
00329     virtual size_t              Count() const;
00331     virtual size_t              Position() const;
00333     virtual void                SetPosition(size_t p);
00334 
00336 
00337     // ---------------------------------------------------------------------
00342 
00344 
00348     virtual void                MapSQLColumn(SValue* inoutColumn) const;
00350     virtual void                MapSQLOrderColumn(SValue* inoutColumnName, SValue* inoutOrder) const;
00352     virtual SValue              CreateSQLFilter(const SValue& filter) const;
00353 
00355 
00356     // --------------------------------------------------------------
00361 
00363 
00366     virtual status_t            ParseArgs(const SValue& args);
00367 
00369     virtual status_t            NextLocked(uint32_t flags, SValue* key, SValue* entry) = 0;
00371 
00372     virtual status_t            RemoveLocked();
00373 
00375 
00376 private:
00377             const sptr<BGenericIterable>
00378                                 m_owner;        // this is effectively public.
00379 };
00380 
00381 // ==========================================================================
00382 // ==========================================================================
00383 
00386 #if _SUPPORTS_NAMESPACE
00387 } } // namespace palmos::storage
00388 #endif
00389 
00390 #endif // _STORAGE_GENERICITERABLE_H