00001 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__ 00002 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__ 00003 00004 #include <creaImageIOTreeHandler.h> 00005 00006 class CppSQLite3DB; 00007 00008 namespace creaImageIO 00009 { 00010 00011 00015 //======================================================================= 00017 class SQLiteTreeHandler : virtual public TreeHandler 00018 { 00019 public: 00020 //==================================================================== 00022 SQLiteTreeHandler(const std::string& filename); 00024 virtual ~SQLiteTreeHandler(); 00025 //==================================================================== 00026 00027 //==================================================================== 00029 const std::string& GetFileName() const { return mFileName; } 00030 //==================================================================== 00031 00032 //==================================================================== 00033 // QUERY METHODS 00035 virtual bool IsReadable() { return true; } 00037 virtual bool IsWritable() { return true; } 00038 //==================================================================== 00039 00040 00041 //==================================================================== 00042 // INITIALIZATION / FINALIZATION 00043 //==================================================================== 00044 00045 //==================================================================== 00047 // Default mode is read only 00048 // If IsWritable and writable==true then opens in read/write mode 00049 virtual bool Open(bool writable = false); 00051 virtual bool Close(); 00053 // Default mode is read only 00054 // If IsWritable and writable==true then opens in read/write mode 00055 virtual bool Create(bool writable = false); 00057 virtual bool Destroy(); 00059 virtual void BeginTransaction(); 00061 virtual void EndTransaction(); 00062 //==================================================================== 00063 00064 00065 //==================================================================== 00066 // READ METHODS 00067 //==================================================================== 00068 00069 00070 //==================================================================== 00072 // REM : The Tree itself is a Node and asking for its number of 00073 // children returns the number of children of level 1. 00074 virtual unsigned int GetNumberOfChildren(tree::Node* n); 00075 //==================================================================== 00076 00077 //==================================================================== 00079 virtual void GetAttribute(std::string levelDescriptor, 00080 std::string searchParam, 00081 std::string searchVal, 00082 std::string key, 00083 std::string& result); 00084 //==================================================================== 00085 00086 00087 //==================================================================== 00089 // is reached. 00090 // If parent == NULL or parent == tree then starts with the 'children' of 00091 // the tree itself. 00092 // Returns the total number of children loaded. 00093 virtual int LoadChildren(tree::Node* parent, int maxlevel); 00094 //==================================================================== 00095 00096 //==================================================================== 00098 // WITHOUT altering the source, e.g. the database 00099 virtual void UnLoad(tree::Node* n); 00101 00102 //==================================================================== 00104 virtual void GetTopLevelNodeId(const std::string& searchParam, 00105 const std::string& searchValue, 00106 std::string& parent_id); 00108 00109 //==================================================================== 00110 // WRITE METHODS : WORK ONLY IN WRITE MODE 00111 //==================================================================== 00113 // returns the Level in the tree where the branch was connected 00114 // (-1 for error, 0 for top level, etc. ) 00115 // Of course the branch is loaded on exit 00116 virtual int AddBranch( const AttributeMapType& attr ); 00117 // Removes the node and its descendants 00118 virtual bool Remove(tree::Node*); 00119 // Sets an attribute of a Node 00120 virtual bool SetAttribute(tree::Node*, 00121 const std::string& key, 00122 const std::string& value); 00123 // Sets an attribute 00124 virtual void SetAttribute(const std::string& levelDescriptor, 00125 const std::string& key, 00126 const std::string& value, 00127 const std::string& searchParam, 00128 const std::string& searchVal); 00129 //Deletes the tuple that matches the parameters given 00130 virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value); 00131 //Deletes the entries that match the parameters given 00132 virtual void RemoveEntries(const std::string i_table, 00133 const std::string i_attribute, 00134 const std::string i_operand, 00135 const std::string i_val); 00136 00137 //==================================================================== 00138 00139 00140 00141 protected: 00142 //====================================================================== 00144 bool DBOpen(); 00146 bool DBImportTreeDescription(); 00147 //====================================================================== 00148 //====================================================================== 00149 // Creation 00151 bool DBCreate(); 00153 void SQLAppendAttributesDefinition(int level, std::string& s); 00154 //====================================================================== 00155 00156 //====================================================================== 00157 00159 // provided must be grafted 00160 tree::Node* DBGetParent( const AttributeMapType& attr); 00161 //====================================================================== 00162 00163 //====================================================================== 00164 00166 // Can recurse to numberoflevels levels 00167 // \return The total number of Node loaded (may be at different levels) 00168 int DBLoadChildren( tree::Node* parent, int numberoflevels = 1); 00169 //====================================================================== 00170 00171 //====================================================================== 00172 00174 // of node n 00175 void SQLAppendAttributesValues(tree::Node* n, std::string& s); 00176 //====================================================================== 00177 00178 //====================================================================== 00179 00181 void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr); 00182 //====================================================================== 00183 //====================================================================== 00184 00186 bool DBSetAttribute(tree::Node*, 00187 const std::string& key, 00188 const std::string& value); 00189 //====================================================================== 00190 //====================================================================== 00192 void DBSetAttribute(const std::string& levelDescriptor, 00193 const std::string& key, 00194 const std::string& value, 00195 const std::string& searchParam, 00196 const std::string& searchVal); 00197 //====================================================================== 00198 //====================================================================== 00199 00201 void DBInsert(tree::Node* n); 00202 //====================================================================== 00203 00204 00205 //====================================================================== 00206 00208 void DBDelete(std::string levelDescriptor, std::string key, std::string value); 00209 //====================================================================== 00210 00211 //====================================================================== 00212 00214 void DBRecursiveRemoveNode(tree::Node* node); 00216 void DBRecursiveRemoveNode(int level, std::string parentId); 00217 00218 //====================================================================== 00219 00220 /* 00222 int DBQueryNumberOfChildren(tree::Node* n); 00223 00224 // Insertion 00225 bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary); 00226 00227 // 00228 std::string DBGetNodeId(tree::Node* node, const std::string& parent_id); 00229 00230 tree::Node* GetNodeFromTypeId(int level, 00231 const std::string& id); 00232 00233 // 00234 tree::Node* DBGetOrCreateNode(tree::Node* alien_node, 00235 tree::Node* internal_parent, 00236 std::string parentid, 00237 std::string& created_id); 00238 // UpdateSummary& summary); 00239 00240 tree::Node* DBGetOrCreateParent(tree::Node* alien_node, 00241 std::string& parent_id); 00242 // UpdateSummary& summary); 00243 00244 void DBRecursiveGetOrCreateNode(tree::Node* alien_node, 00245 tree::Node* parent, 00246 const std::string& parent_id); 00247 // UpdateSummary& summary); 00248 00249 00250 */ 00251 00252 /* 00253 00254 void BuildSQLFieldsValues(tree::Node* n, 00255 std::string& str); 00256 00257 00258 tree::Node* GetChildrenLike(tree::Node* internal_parent, 00259 tree::Node* alien_node); 00260 */ 00261 private: 00263 CppSQLite3DB* mDB; 00265 std::string mFileName; 00267 bool mWritable; 00268 void SetWritable(bool w) { mWritable; } 00269 bool GetWritable() const { return mWritable; } 00270 bool mIsAdding; 00271 00272 /* 00273 struct TypeId 00274 { 00275 int type; 00276 std::string id; 00277 bool operator< (const TypeId& o) const 00278 { 00279 return ((type<o.type) || 00280 ((type==o.type)&&id<o.id)); 00281 } 00282 }; 00283 00284 typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType; 00285 TypeIdToNodeMapType mTypeIdToNodeMap; 00286 */ 00287 }; 00288 // EO class SQLiteTreeHandler 00289 //======================================================================= 00290 00291 00292 } // EO namespace creaImageIO 00293 00294 // EOF 00295 #endif 00296