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