2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
29 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
30 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
32 #include <creaImageIOTreeHandler.h>
33 #include <creaImageIOTree.h>
44 //=======================================================================
45 /// Concrete TreeHandler which manages a Tree stored in a sqlite database
46 class SQLiteTreeHandler : virtual public TreeHandler
49 //====================================================================
50 /// Ctor with database file name
51 SQLiteTreeHandler(const std::string& filename);
53 virtual ~SQLiteTreeHandler();
54 //====================================================================
56 //====================================================================
57 /// Returns the sqlite db file name
58 const std::string& GetFileName() const { return mFileName; }
59 //====================================================================
61 //====================================================================
63 /// Is the 'source' readable ?
64 virtual bool IsReadable() { return true; }
65 /// Is the 'source' writable ?
66 virtual bool IsWritable() { return true; }
67 //====================================================================
70 //====================================================================
71 // INITIALIZATION / FINALIZATION
72 //====================================================================
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'
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 //====================================================================
94 //====================================================================
96 //====================================================================
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 //====================================================================
106 //====================================================================
107 /// Returns the attribute requested. Useful for synchronization.
108 virtual void GetAttribute(std::string levelDescriptor,
109 std::string searchParam,
110 std::string searchVal,
112 std::string& result);
113 //====================================================================
116 //====================================================================
117 /// Recursively loads the children of node 'parent' until maxlevel
119 // If parent == NULL or parent == tree then starts with the 'children' of
121 // Returns the total number of children loaded.
122 virtual int LoadChildren(tree::Node* parent, int maxlevel);
123 //====================================================================
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 ///====================================================================
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 ///====================================================================
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);
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);
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);
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);
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);
177 //======================================================================
178 /// Open the database
180 /// Import the Tree::Description from database (verifies the structure)
181 bool DBImportTreeDescription();
182 //======================================================================
183 //======================================================================
185 /// Creates a new database on disk and the tables
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 //======================================================================
191 //======================================================================
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 //======================================================================
198 //======================================================================
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 //======================================================================
206 //======================================================================
208 /// Appends to string s the SQL command to set the attributes values
210 void SQLAppendAttributesValues(tree::Node* n, std::string& s);
211 //======================================================================
213 //======================================================================
215 /// Graft the branch defined by the attributes to the parent
216 void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
217 //======================================================================
218 //======================================================================
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 //======================================================================
235 /// Inserts the Node in the database
236 void DBInsert(tree::Node* n);
237 //======================================================================
240 //======================================================================
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 //======================================================================
246 //======================================================================
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);
253 //======================================================================
259 /// The physical location associated to the DicomDatabase (directory, db file...)
260 std::string mFileName;
261 /// Is the DB writable ?
263 void SetWritable(bool w) { mWritable = w; }
264 bool GetWritable() const { return mWritable; }
266 const std::string convert(const std::string &i_word);
270 // EO class SQLiteTreeHandler
271 //=======================================================================
274 } // EO namespace creaImageIO