creaImageIOSQLiteTreeHandler.h

Go to the documentation of this file.
00001 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
00002 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
00003 
00004 #include <creaImageIOTreeHandler.h>
00005 #include <creaImageIOTree.h>
00006 
00007 class CppSQLite3DB;
00008 
00009 namespace creaImageIO
00010 {
00011 
00012   
00016   //=======================================================================
00018   class SQLiteTreeHandler : virtual public TreeHandler
00019   {
00020   public:
00021     //====================================================================
00023     SQLiteTreeHandler(const std::string& filename);
00025     virtual ~SQLiteTreeHandler();
00026     //====================================================================
00027 
00028     //====================================================================
00030     const std::string& GetFileName() const { return mFileName; }
00031     //====================================================================
00032  
00033     //====================================================================
00034     // QUERY METHODS
00036     virtual bool IsReadable() { return true; }
00038     virtual bool IsWritable() { return true; }
00039     //====================================================================
00040 
00041 
00042     //====================================================================
00043     // INITIALIZATION / FINALIZATION
00044     //====================================================================
00045 
00046     //====================================================================
00048     // Default mode is read only 
00049     // If IsWritable and writable==true then opens in read/write mode
00050     virtual bool Open(bool writable = false);
00052     virtual bool Close();
00054     // Default mode is read only 
00055     // If IsWritable and writable==true then opens in read/write mode
00056     virtual bool Create(bool writable = false);
00058     virtual bool Destroy();
00060         virtual void BeginTransaction();
00062         virtual void EndTransaction();
00063     //====================================================================
00064 
00065 
00066     //====================================================================
00067     // READ METHODS
00068     //====================================================================
00069 
00070 
00071     //====================================================================
00073     // REM : The Tree itself is a Node and asking for its number of 
00074     //       children returns the number of children of level 1.
00075     virtual unsigned int GetNumberOfChildren(tree::Node* n);
00076     //====================================================================
00077 
00078         //====================================================================
00080         virtual void GetAttribute(std::string levelDescriptor,
00081                                                                            std::string searchParam, 
00082                                                                            std::string searchVal, 
00083                                                                            std::string key, 
00084                                                                            std::string& result);
00085     //====================================================================
00086 
00087 
00088     //====================================================================
00090     // is reached.
00091     // If parent == NULL or parent == tree then starts with the 'children' of 
00092     // the tree itself.
00093     // Returns the total number of children loaded.
00094     virtual int LoadChildren(tree::Node* parent, int maxlevel);
00095     //====================================================================
00096 
00097     //====================================================================
00099     // WITHOUT altering the source, e.g. the database
00100     virtual void UnLoad(tree::Node* n);
00102         
00103         //====================================================================
00105     virtual void GetTopLevelNodeId(const std::string& searchParam, 
00106                                                                                         const std::string& searchValue, 
00107                                                                                         std::string& parent_id);
00109 
00110     //====================================================================
00111     // WRITE METHODS : WORK ONLY IN WRITE MODE
00112     //====================================================================
00114     // returns the Level in the tree where the branch was connected 
00115     // (-1 for error, 0 for top level, etc. ) 
00116     // Of course the branch is loaded on exit
00117     virtual int AddBranch( const AttributeMapType& attr );
00118     // Removes the node and its descendants 
00119     virtual bool Remove(tree::Node*);
00120     // Sets an attribute of a Node
00121     virtual bool SetAttribute(tree::Node*, 
00122                               const std::string& key,
00123                               const std::string& value);
00124         // Sets an attribute
00125     virtual void SetAttribute(const std::string& levelDescriptor, 
00126                               const std::string& key,
00127                               const std::string& value,
00128                                   const std::string& searchParam, 
00129                                   const std::string& searchVal);
00130         //Deletes the tuple that matches the parameters given
00131         virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value);
00132         //Deletes the entries that match the parameters given
00133         virtual void RemoveEntries(const std::string i_table, 
00134                 const std::string i_attribute, 
00135                 const std::string i_operand, 
00136                 const std::string i_val);
00137  
00138     //====================================================================
00140         void getAllAttributes(std::string i_filename, std::map<std::string, std::string> &i_results);
00141         
00143         void GetAttributes(std::string name, std::string i_id, std::string i_value, tree::LevelDescriptor::AttributeDescriptorListType i_attr, std::vector<std::string> &i_results);
00144         
00146         void GetUpLevelNodeId(int level, const std::string& searchParam, const std::string& searchValue, std::string& parent_id);
00147 
00148   protected:
00149     //======================================================================
00151     bool DBOpen();
00153     bool DBImportTreeDescription();
00154     //======================================================================
00155     //======================================================================
00156     // Creation
00158     bool DBCreate();
00160     void SQLAppendAttributesDefinition(int level, std::string& s);
00161     //======================================================================
00162 
00163     //======================================================================
00164 
00166     // provided must be grafted 
00167     tree::Node* DBGetParent( const AttributeMapType& attr);
00168     //======================================================================
00169 
00170     //======================================================================
00171 
00173     // Can recurse to numberoflevels levels
00174     // \return The total number of Node loaded (may be at different levels)
00175     int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
00176     //======================================================================
00177 
00178     //======================================================================
00179 
00181     // of node n
00182     void SQLAppendAttributesValues(tree::Node* n, std::string& s);
00183     //======================================================================
00184 
00185     //======================================================================
00186 
00188     void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
00189     //======================================================================
00190     //======================================================================
00191 
00193     bool DBSetAttribute(tree::Node*, 
00194                         const std::string& key,
00195                         const std::string& value);
00196     //======================================================================
00197     //======================================================================
00199         void DBSetAttribute(const std::string& levelDescriptor, 
00200                               const std::string& key,
00201                               const std::string& value,
00202                                   const std::string& searchParam, 
00203                                   const std::string& searchVal);
00204     //======================================================================
00205     //======================================================================
00206         
00208     void DBInsert(tree::Node* n);
00209     //======================================================================
00210 
00211         
00212         //======================================================================
00213 
00215         void DBDelete(std::string levelDescriptor, std::string key, std::string value);
00216     //======================================================================
00217 
00218         //======================================================================
00219 
00221     void DBRecursiveRemoveNode(tree::Node* node);
00223         void DBRecursiveRemoveNode(int level, std::string parentId);
00224  
00225         //======================================================================
00226 
00227   
00228   private:
00230     CppSQLite3DB* mDB;
00232     std::string mFileName;
00234     bool mWritable;
00235     void SetWritable(bool w) { mWritable = w; }
00236     bool GetWritable() const { return mWritable; }
00237         bool mIsAdding;
00238         const std::string convert(const std::string &i_word);
00239 
00240    
00241   };
00242   // EO class SQLiteTreeHandler
00243   //=======================================================================
00244 
00245 
00246 } // EO namespace creaImageIO
00247 
00248 // EOF
00249 #endif  
00250