]> Creatis software - creaImageIO.git/blob - src/creaImageIOSQLiteTreeHandler.h
Feature #1764
[creaImageIO.git] / src / creaImageIOSQLiteTreeHandler.h
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28
29 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
30 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
31
32 #include <creaImageIOTreeHandler.h>
33 #include <creaImageIOTree.h>
34
35 class CppSQLite3DB;
36
37 namespace creaImageIO
38 {
39
40   
41   /**
42    * \ingroup Model
43    */
44   //=======================================================================
45   /// Concrete TreeHandler which manages a Tree stored in a sqlite database
46   class SQLiteTreeHandler : virtual public TreeHandler
47   {
48   public:
49     //====================================================================
50     /// Ctor with database file name 
51     SQLiteTreeHandler(const std::string& filename);
52     /// Dtor
53     virtual ~SQLiteTreeHandler();
54     //====================================================================
55
56     //====================================================================
57     /// Returns the sqlite db file name 
58     const std::string& GetFileName() const { return mFileName; }
59     //====================================================================
60  
61     //====================================================================
62     // QUERY METHODS
63     /// Is the 'source' readable ?
64     virtual bool IsReadable() { return true; }
65     /// Is the 'source' writable ?
66     virtual bool IsWritable() { return true; }
67     //====================================================================
68
69
70     //====================================================================
71     // INITIALIZATION / FINALIZATION
72     //====================================================================
73
74     //====================================================================
75     /// Opens an existing 'source' 
76     // Default mode is read only 
77     // If IsWritable and writable==true then opens in read/write mode
78     virtual bool Open(bool writable = false);
79     /// Closes the 'source'
80     virtual bool Close();
81     /// Creates a new 'source' 
82     // Default mode is read only 
83     // If IsWritable and writable==true then opens in read/write mode
84     virtual bool Create(bool writable = false);
85     /// Destroys the 'source'
86     virtual bool Destroy();
87         /// Begins a transaction
88         virtual void BeginTransaction();
89         ///Commits results and ends transaction
90         virtual void EndTransaction();
91     //====================================================================
92
93
94     //====================================================================
95     // READ METHODS
96     //====================================================================
97
98
99     //====================================================================
100     /// Returns the number of children of the Node *WITHOUT LOADING THEM*
101     // REM : The Tree itself is a Node and asking for its number of 
102     //       children returns the number of children of level 1.
103     virtual unsigned int GetNumberOfChildren(tree::Node* n);
104     //====================================================================
105
106         //====================================================================
107     /// Returns the attribute requested. Useful for synchronization.
108         virtual void GetAttribute(std::string levelDescriptor,
109                                                                            std::string searchParam, 
110                                                                            std::string searchVal, 
111                                                                            std::string key, 
112                                                                            std::string& result);
113     //====================================================================
114
115
116     //====================================================================
117     /// Recursively loads the children of node 'parent' until maxlevel 
118     // is reached.
119     // If parent == NULL or parent == tree then starts with the 'children' of 
120     // the tree itself.
121     // Returns the total number of children loaded.
122     virtual int LoadChildren(tree::Node* parent, int maxlevel);
123     //====================================================================
124
125     //====================================================================
126     /// Unloads the Node and its descendants
127     // WITHOUT altering the source, e.g. the database
128     virtual void UnLoad(tree::Node* n);
129     ///====================================================================
130         
131         //====================================================================
132     /// Returns the top level node id for the given search param and search value
133     virtual void GetTopLevelNodeId(const std::string& searchParam, 
134                                                                                         const std::string& searchValue, 
135                                                                                         std::string& parent_id);
136     ///====================================================================
137
138     //====================================================================
139     // WRITE METHODS : WORK ONLY IN WRITE MODE
140     //====================================================================
141     /// Adds a branch in the tree with the attributes provided
142     // returns the Level in the tree where the branch was connected 
143     // (-1 for error, 0 for top level, etc. ) 
144     // Of course the branch is loaded on exit
145     virtual int AddBranch( const AttributeMapType& attr );
146     // Removes the node and its descendants 
147     virtual bool Remove(tree::Node*);
148     // Sets an attribute of a Node
149     virtual bool SetAttribute(tree::Node*, 
150                               const std::string& key,
151                               const std::string& value);
152         // Sets an attribute
153     virtual void SetAttribute(const std::string& levelDescriptor, 
154                               const std::string& key,
155                               const std::string& value,
156                                   const std::string& searchParam, 
157                                   const std::string& searchVal);
158         //Deletes the tuple that matches the parameters given
159         virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value);
160         //Deletes the entries that match the parameters given
161         virtual void RemoveEntries(const std::string i_table, 
162                 const std::string i_attribute, 
163                 const std::string i_operand, 
164                 const std::string i_val);
165  
166     //====================================================================
167     /// get all attributes from database for a given file
168         void getAllAttributes(std::string i_filename, std::map<std::string, std::string> &i_results);
169         
170         /// get selected attributes from database for a given file
171         void GetAttributes(std::string name, std::string i_id, std::string i_value, tree::LevelDescriptor::AttributeDescriptorListType i_attr, std::vector<std::string> &i_results);
172         
173         /// get Id for the up level node
174         void GetUpLevelNodeId(int level, const std::string& searchParam, const std::string& searchValue, std::string& parent_id);
175
176   protected:
177     //======================================================================
178     /// Open the database
179     bool DBOpen();
180     /// Import the Tree::Description from database (verifies the structure)
181     bool DBImportTreeDescription();
182     //======================================================================
183     //======================================================================
184     // Creation
185     /// Creates a new database on disk and the tables
186     bool DBCreate();
187     /// Appends to string s the SQL command to create the attributes of a given level
188     void SQLAppendAttributesDefinition(int level, std::string& s);
189     //======================================================================
190
191     //======================================================================
192
193     /// Returns the parent to which the branch defined by the attributes 
194     // provided must be grafted 
195     tree::Node* DBGetParent( const AttributeMapType& attr);
196     //======================================================================
197
198     //======================================================================
199
200     /// Loads the children of Node parent
201     // Can recurse to numberoflevels levels
202     // \return The total number of Node loaded (may be at different levels)
203     int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
204     //======================================================================
205
206     //======================================================================
207
208     /// Appends to string s the SQL command to set the attributes values 
209     // of node n
210     void SQLAppendAttributesValues(tree::Node* n, std::string& s);
211     //======================================================================
212
213     //======================================================================
214
215     /// Graft the branch defined by the attributes to the parent
216     void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
217     //======================================================================
218     //======================================================================
219
220     /// Sets an attribute of a Node and updates the database
221     bool DBSetAttribute(tree::Node*, 
222                         const std::string& key,
223                         const std::string& value);
224     //======================================================================
225     //======================================================================
226         /// Sets an attribute and updates the database
227         void DBSetAttribute(const std::string& levelDescriptor, 
228                               const std::string& key,
229                               const std::string& value,
230                                   const std::string& searchParam, 
231                                   const std::string& searchVal);
232     //======================================================================
233     //======================================================================
234         
235     /// Inserts the Node in the database
236     void DBInsert(tree::Node* n);
237     //======================================================================
238
239         
240         //======================================================================
241
242     /// Deletes the tuple that matches the value specified in the given key and that belongs to the given level
243         void DBDelete(std::string levelDescriptor, std::string key, std::string value);
244     //======================================================================
245
246         //======================================================================
247
248         /// Recursively Removes the nodes whose parent is given as a parameter
249     void DBRecursiveRemoveNode(tree::Node* node);
250         /// Recursively Removes the nodes found in the given level with the given parent id
251         void DBRecursiveRemoveNode(int level, std::string parentId);
252  
253         //======================================================================
254
255   
256   private:
257     /// The DB
258     CppSQLite3DB* mDB;
259    /// The physical location associated to the DicomDatabase (directory, db file...)
260     std::string mFileName;
261     /// Is the DB writable ?
262     bool mWritable;
263     void SetWritable(bool w) { mWritable = w; }
264     bool GetWritable() const { return mWritable; }
265         bool mIsAdding;
266         const std::string convert(const std::string &i_word);
267
268    
269   };
270   // EO class SQLiteTreeHandler
271   //=======================================================================
272
273
274 } // EO namespace creaImageIO
275
276 // EOF
277 #endif  
278