]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSQLiteTreeHandler.h
0f45f7093503360e45ff9d0300c6b9dc79019e9e
[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     //====================================================================
100     // WRITE METHODS : WORK ONLY IN WRITE MODE
101     //====================================================================
102     /// Adds a branch in the tree with the attributes provided
103     // returns the Level in the tree where the branch was connected 
104     // (-1 for error, 0 for top level, etc. ) 
105     // Of course the branch is loaded on exit
106     virtual int AddBranch( const AttributeMapType& attr );
107     // Removes the node and its descendants 
108     virtual bool Remove(tree::Node*);
109     // Sets an attribute of a Node
110     virtual bool SetAttribute(tree::Node*, 
111                               const std::string& key,
112                               const std::string& value);
113         // Sets an attribute
114     virtual void SetAttribute(const std::string& levelDescriptor, 
115                               const std::string& key,
116                               const std::string& value,
117                                   const std::string& searchParam, 
118                                   const std::string& searchVal);
119         //Deletes the tuple that matches the parameters given
120         virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value);
121  
122     //====================================================================
123     
124
125     
126   protected:
127     //======================================================================
128     /// Open the database
129     bool DBOpen();
130     /// Import the Tree::Description from database (verifies the structure)
131     bool DBImportTreeDescription();
132     //======================================================================
133     //======================================================================
134     // Creation
135     /// Creates a new database on disk and the tables
136     bool DBCreate();
137     /// Appends to string s the SQL command to create the attributes of a given level
138     void SQLAppendAttributesDefinition(int level, std::string& s);
139     //======================================================================
140
141     //======================================================================
142
143     /// Returns the parent to which the branch defined by the attributes 
144     // provided must be grafted 
145     tree::Node* DBGetParent( const AttributeMapType& attr);
146     //======================================================================
147
148     //======================================================================
149
150     /// Loads the children of Node parent
151     // Can recurse to numberoflevels levels
152     // \return The total number of Node loaded (may be at different levels)
153     int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
154     //======================================================================
155
156     //======================================================================
157
158     /// Appends to string s the SQL command to set the attributes values 
159     // of node n
160     void SQLAppendAttributesValues(tree::Node* n, std::string& s);
161     //======================================================================
162
163     //======================================================================
164
165     /// Graft the branch defined by the attributes to the parent
166     void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
167     //======================================================================
168     //======================================================================
169
170     /// Sets an attribute of a Node and updates the database
171     bool DBSetAttribute(tree::Node*, 
172                         const std::string& key,
173                         const std::string& value);
174     //======================================================================
175     //======================================================================
176         /// Sets an attribute and updates the database
177         void DBSetAttribute(const std::string& levelDescriptor, 
178                               const std::string& key,
179                               const std::string& value,
180                                   const std::string& searchParam, 
181                                   const std::string& searchVal);
182     //======================================================================
183     //======================================================================
184         
185     /// Inserts the Node in the database
186     void DBInsert(tree::Node* n);
187     //======================================================================
188
189         
190         //======================================================================
191
192     /// Deletes the tuple that matches the value specified in the given key and that belongs to the given level
193         void DBDelete(std::string levelDescriptor, std::string key, std::string value);
194     //======================================================================
195
196
197         /// Recursively Removes the nodes whose parent is given as a parameter
198     void DBRecursiveRemoveNode(tree::Node* node);
199     /*
200     /// 
201     int DBQueryNumberOfChildren(tree::Node* n);
202     
203     // Insertion
204     bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
205     
206     //
207     std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
208
209     tree::Node* GetNodeFromTypeId(int level, 
210                                   const std::string& id);
211
212     // 
213     tree::Node* DBGetOrCreateNode(tree::Node* alien_node, 
214                                       tree::Node* internal_parent,
215                                       std::string parentid,
216                                   std::string& created_id);
217     //                                UpdateSummary& summary);
218     
219     tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
220                                     std::string& parent_id);
221                                     //                             UpdateSummary& summary);
222     
223     void DBRecursiveGetOrCreateNode(tree::Node* alien_node, 
224                                          tree::Node* parent, 
225                                     const std::string& parent_id);
226     //                                   UpdateSummary& summary);
227     
228
229         */
230
231         /*
232
233     void BuildSQLFieldsValues(tree::Node* n,
234                               std::string& str);
235
236
237     tree::Node* GetChildrenLike(tree::Node* internal_parent,
238                                tree::Node* alien_node);
239     */
240   private:
241     /// The DB
242     CppSQLite3DB* mDB;
243    /// The physical location associated to the DicomDatabase (directory, db file...)
244     std::string mFileName;
245     /// Is the DB writable ?
246     bool mWritable;
247     void SetWritable(bool w) { mWritable; }
248     bool GetWritable() const { return mWritable; }
249         bool mIsAdding;
250
251     /*
252     struct TypeId
253     {
254       int type;
255       std::string id;
256       bool operator< (const TypeId& o) const 
257       {
258         return ((type<o.type) ||
259                 ((type==o.type)&&id<o.id));
260       }
261     };
262     
263     typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
264     TypeIdToNodeMapType mTypeIdToNodeMap;
265     */
266   };
267   // EO class SQLiteTreeHandler
268   //=======================================================================
269
270
271 } // EO namespace creaImageIO
272
273 // EOF
274 #endif  
275