]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSQLiteTreeHandler.h
move directory
[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         /// Begins a transaction
59         virtual void BeginTransaction();
60         ///Commits results and ends transaction
61         virtual void EndTransaction();
62     //====================================================================
63
64
65     //====================================================================
66     // READ METHODS
67     //====================================================================
68
69
70     //====================================================================
71     /// Returns the number of children of the Node *WITHOUT LOADING THEM*
72     // REM : The Tree itself is a Node and asking for its number of 
73     //       children returns the number of children of level 1.
74     virtual unsigned int GetNumberOfChildren(tree::Node* n);
75     //====================================================================
76
77         //====================================================================
78     /// Returns the attribute requested. Useful for synchronization.
79         virtual void GetAttribute(std::string levelDescriptor,
80                                                                            std::string searchParam, 
81                                                                            std::string searchVal, 
82                                                                            std::string key, 
83                                                                            std::string& result);
84     //====================================================================
85
86
87     //====================================================================
88     /// Recursively loads the children of node 'parent' until maxlevel 
89     // is reached.
90     // If parent == NULL or parent == tree then starts with the 'children' of 
91     // the tree itself.
92     // Returns the total number of children loaded.
93     virtual int LoadChildren(tree::Node* parent, int maxlevel);
94     //====================================================================
95
96     //====================================================================
97     /// Unloads the Node and its descendants
98     // WITHOUT altering the source, e.g. the database
99     virtual void UnLoad(tree::Node* n);
100     ///====================================================================
101         
102         //====================================================================
103     /// Returns the top level node id for the given search param and search value
104     virtual void GetTopLevelNodeId(const std::string& searchParam, 
105                                                                                         const std::string& searchValue, 
106                                                                                         std::string& parent_id);
107     ///====================================================================
108
109     //====================================================================
110     // WRITE METHODS : WORK ONLY IN WRITE MODE
111     //====================================================================
112     /// Adds a branch in the tree with the attributes provided
113     // returns the Level in the tree where the branch was connected 
114     // (-1 for error, 0 for top level, etc. ) 
115     // Of course the branch is loaded on exit
116     virtual int AddBranch( const AttributeMapType& attr );
117     // Removes the node and its descendants 
118     virtual bool Remove(tree::Node*);
119     // Sets an attribute of a Node
120     virtual bool SetAttribute(tree::Node*, 
121                               const std::string& key,
122                               const std::string& value);
123         // Sets an attribute
124     virtual void SetAttribute(const std::string& levelDescriptor, 
125                               const std::string& key,
126                               const std::string& value,
127                                   const std::string& searchParam, 
128                                   const std::string& searchVal);
129         //Deletes the tuple that matches the parameters given
130         virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value);
131         //Deletes the entries that match the parameters given
132         virtual void RemoveEntries(const std::string i_table, 
133                 const std::string i_attribute, 
134                 const std::string i_operand, 
135                 const std::string i_val);
136  
137     //====================================================================
138     
139
140     
141   protected:
142     //======================================================================
143     /// Open the database
144     bool DBOpen();
145     /// Import the Tree::Description from database (verifies the structure)
146     bool DBImportTreeDescription();
147     //======================================================================
148     //======================================================================
149     // Creation
150     /// Creates a new database on disk and the tables
151     bool DBCreate();
152     /// Appends to string s the SQL command to create the attributes of a given level
153     void SQLAppendAttributesDefinition(int level, std::string& s);
154     //======================================================================
155
156     //======================================================================
157
158     /// Returns the parent to which the branch defined by the attributes 
159     // provided must be grafted 
160     tree::Node* DBGetParent( const AttributeMapType& attr);
161     //======================================================================
162
163     //======================================================================
164
165     /// Loads the children of Node parent
166     // Can recurse to numberoflevels levels
167     // \return The total number of Node loaded (may be at different levels)
168     int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
169     //======================================================================
170
171     //======================================================================
172
173     /// Appends to string s the SQL command to set the attributes values 
174     // of node n
175     void SQLAppendAttributesValues(tree::Node* n, std::string& s);
176     //======================================================================
177
178     //======================================================================
179
180     /// Graft the branch defined by the attributes to the parent
181     void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
182     //======================================================================
183     //======================================================================
184
185     /// Sets an attribute of a Node and updates the database
186     bool DBSetAttribute(tree::Node*, 
187                         const std::string& key,
188                         const std::string& value);
189     //======================================================================
190     //======================================================================
191         /// Sets an attribute and updates the database
192         void DBSetAttribute(const std::string& levelDescriptor, 
193                               const std::string& key,
194                               const std::string& value,
195                                   const std::string& searchParam, 
196                                   const std::string& searchVal);
197     //======================================================================
198     //======================================================================
199         
200     /// Inserts the Node in the database
201     void DBInsert(tree::Node* n);
202     //======================================================================
203
204         
205         //======================================================================
206
207     /// Deletes the tuple that matches the value specified in the given key and that belongs to the given level
208         void DBDelete(std::string levelDescriptor, std::string key, std::string value);
209     //======================================================================
210
211         //======================================================================
212
213         /// Recursively Removes the nodes whose parent is given as a parameter
214     void DBRecursiveRemoveNode(tree::Node* node);
215         /// Recursively Removes the nodes found in the given level with the given parent id
216         void DBRecursiveRemoveNode(int level, std::string parentId);
217  
218         //======================================================================
219
220   
221   private:
222     /// The DB
223     CppSQLite3DB* mDB;
224    /// The physical location associated to the DicomDatabase (directory, db file...)
225     std::string mFileName;
226     /// Is the DB writable ?
227     bool mWritable;
228     void SetWritable(bool w) { mWritable = w; }
229     bool GetWritable() const { return mWritable; }
230         bool mIsAdding;
231
232    
233   };
234   // EO class SQLiteTreeHandler
235   //=======================================================================
236
237
238 } // EO namespace creaImageIO
239
240 // EOF
241 #endif  
242