]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSQLiteTreeHandler.h
d3a055e55eca70ff2d2e337f864c60b6c51dd184
[creaImageIO.git] / src2 / creaImageIOSQLiteTreeHandler.h
1 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
2 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
3
4 #include <creaImageIOTreeHandler.h>
5
6 class CppSQLite3DB;
7
8 namespace creaImageIO
9 {
10
11   
12   /**
13    * \ingroup Model
14    */
15   //=======================================================================
16   /// Concrete TreeHandler which manages a Tree stored in a sqlite database
17   class SQLiteTreeHandler : virtual public TreeHandler
18   {
19   public:
20     //====================================================================
21     /// Ctor with database file name 
22     SQLiteTreeHandler(const std::string& filename);
23     /// Dtor
24     virtual ~SQLiteTreeHandler();
25     //====================================================================
26
27     //====================================================================
28     /// Returns the sqlite db file name 
29     const std::string& GetFileName() const { return mFileName; }
30     //====================================================================
31  
32     //====================================================================
33     // QUERY METHODS
34     /// Is the 'source' readable ?
35     virtual bool IsReadable() { return true; }
36     /// Is the 'source' writable ?
37     virtual bool IsWritable() { return true; }
38     //====================================================================
39
40
41     //====================================================================
42     // INITIALIZATION / FINALIZATION
43     //====================================================================
44
45     //====================================================================
46     /// Opens an existing 'source' 
47     // Default mode is read only 
48     // If IsWritable and writable==true then opens in read/write mode
49     virtual bool Open(bool writable = false);
50     /// Closes the 'source'
51     virtual bool Close();
52     /// Creates a new 'source' 
53     // Default mode is read only 
54     // If IsWritable and writable==true then opens in read/write mode
55     virtual bool Create(bool writable = false);
56     /// Destroys the 'source'
57     virtual bool Destroy();
58     //====================================================================
59
60
61     //====================================================================
62     // READ METHODS
63     //====================================================================
64
65
66     //====================================================================
67     /// Returns the number of children of the Node *WITHOUT LOADING THEM*
68     // REM : The Tree itself is a Node and asking for its number of 
69     //       children returns the number of children of level 1.
70     virtual unsigned int GetNumberOfChildren(tree::Node* n);
71     //====================================================================
72
73         //====================================================================
74     /// Returns the attribute requested. Useful for synchronization.
75         virtual void GetAttribute(std::string levelDescriptor,
76                                                                            std::string searchParam, 
77                                                                            std::string searchVal, 
78                                                                            std::string key, 
79                                                                            std::string& result);
80     //====================================================================
81
82
83     //====================================================================
84     /// Recursively loads the children of node 'parent' until maxlevel 
85     // is reached.
86     // If parent == NULL or parent == tree then starts with the 'children' of 
87     // the tree itself.
88     // Returns the total number of children loaded.
89     virtual int LoadChildren(tree::Node* parent, int maxlevel);
90     //====================================================================
91
92     //====================================================================
93     /// Unloads the Node and its descendants
94     // WITHOUT altering the source, e.g. the database
95     virtual void UnLoad(tree::Node* n);
96     ///====================================================================
97         
98         //====================================================================
99     /// Returns the top level node id for the given search param and search value
100     virtual void GetTopLevelNodeId(const std::string& searchParam, 
101                                                                                         const std::string& searchValue, 
102                                                                                         std::string& parent_id);
103     ///====================================================================
104
105     //====================================================================
106     // WRITE METHODS : WORK ONLY IN WRITE MODE
107     //====================================================================
108     /// Adds a branch in the tree with the attributes provided
109     // returns the Level in the tree where the branch was connected 
110     // (-1 for error, 0 for top level, etc. ) 
111     // Of course the branch is loaded on exit
112     virtual int AddBranch( const AttributeMapType& attr );
113     // Removes the node and its descendants 
114     virtual bool Remove(tree::Node*);
115     // Sets an attribute of a Node
116     virtual bool SetAttribute(tree::Node*, 
117                               const std::string& key,
118                               const std::string& value);
119         // Sets an attribute
120     virtual void SetAttribute(const std::string& levelDescriptor, 
121                               const std::string& key,
122                               const std::string& value,
123                                   const std::string& searchParam, 
124                                   const std::string& searchVal);
125         //Deletes the tuple that matches the parameters given
126         virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value);
127         //Deletes the entries that match the parameters given
128         virtual void RemoveEntries(const std::string i_table, 
129                 const std::string i_attribute, 
130                 const std::string i_operand, 
131                 const std::string i_val);
132  
133     //====================================================================
134     
135
136     
137   protected:
138     //======================================================================
139     /// Open the database
140     bool DBOpen();
141     /// Import the Tree::Description from database (verifies the structure)
142     bool DBImportTreeDescription();
143     //======================================================================
144     //======================================================================
145     // Creation
146     /// Creates a new database on disk and the tables
147     bool DBCreate();
148     /// Appends to string s the SQL command to create the attributes of a given level
149     void SQLAppendAttributesDefinition(int level, std::string& s);
150     //======================================================================
151
152     //======================================================================
153
154     /// Returns the parent to which the branch defined by the attributes 
155     // provided must be grafted 
156     tree::Node* DBGetParent( const AttributeMapType& attr);
157     //======================================================================
158
159     //======================================================================
160
161     /// Loads the children of Node parent
162     // Can recurse to numberoflevels levels
163     // \return The total number of Node loaded (may be at different levels)
164     int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
165     //======================================================================
166
167     //======================================================================
168
169     /// Appends to string s the SQL command to set the attributes values 
170     // of node n
171     void SQLAppendAttributesValues(tree::Node* n, std::string& s);
172     //======================================================================
173
174     //======================================================================
175
176     /// Graft the branch defined by the attributes to the parent
177     void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
178     //======================================================================
179     //======================================================================
180
181     /// Sets an attribute of a Node and updates the database
182     bool DBSetAttribute(tree::Node*, 
183                         const std::string& key,
184                         const std::string& value);
185     //======================================================================
186     //======================================================================
187         /// Sets an attribute and updates the database
188         void DBSetAttribute(const std::string& levelDescriptor, 
189                               const std::string& key,
190                               const std::string& value,
191                                   const std::string& searchParam, 
192                                   const std::string& searchVal);
193     //======================================================================
194     //======================================================================
195         
196     /// Inserts the Node in the database
197     void DBInsert(tree::Node* n);
198     //======================================================================
199
200         
201         //======================================================================
202
203     /// Deletes the tuple that matches the value specified in the given key and that belongs to the given level
204         void DBDelete(std::string levelDescriptor, std::string key, std::string value);
205     //======================================================================
206
207         //======================================================================
208
209         /// Recursively Removes the nodes whose parent is given as a parameter
210     void DBRecursiveRemoveNode(tree::Node* node);
211         /// Recursively Removes the nodes found in the given level with the given parent id
212         void DBRecursiveRemoveNode(int level, std::string parentId);
213  
214         //======================================================================
215
216     /*
217     /// 
218     int DBQueryNumberOfChildren(tree::Node* n);
219     
220     // Insertion
221     bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
222     
223     //
224     std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
225
226     tree::Node* GetNodeFromTypeId(int level, 
227                                   const std::string& id);
228
229     // 
230     tree::Node* DBGetOrCreateNode(tree::Node* alien_node, 
231                                       tree::Node* internal_parent,
232                                       std::string parentid,
233                                   std::string& created_id);
234     //                                UpdateSummary& summary);
235     
236     tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
237                                     std::string& parent_id);
238                                     //                             UpdateSummary& summary);
239     
240     void DBRecursiveGetOrCreateNode(tree::Node* alien_node, 
241                                          tree::Node* parent, 
242                                     const std::string& parent_id);
243     //                                   UpdateSummary& summary);
244     
245
246         */
247
248         /*
249
250     void BuildSQLFieldsValues(tree::Node* n,
251                               std::string& str);
252
253
254     tree::Node* GetChildrenLike(tree::Node* internal_parent,
255                                tree::Node* alien_node);
256     */
257   private:
258     /// The DB
259     CppSQLite3DB* mDB;
260    /// The physical location associated to the DicomDatabase (directory, db file...)
261     std::string mFileName;
262     /// Is the DB writable ?
263     bool mWritable;
264     void SetWritable(bool w) { mWritable; }
265     bool GetWritable() const { return mWritable; }
266         bool mIsAdding;
267
268     /*
269     struct TypeId
270     {
271       int type;
272       std::string id;
273       bool operator< (const TypeId& o) const 
274       {
275         return ((type<o.type) ||
276                 ((type==o.type)&&id<o.id));
277       }
278     };
279     
280     typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
281     TypeIdToNodeMapType mTypeIdToNodeMap;
282     */
283   };
284   // EO class SQLiteTreeHandler
285   //=======================================================================
286
287
288 } // EO namespace creaImageIO
289
290 // EOF
291 #endif  
292