1 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
2 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
4 #include <creaImageIOTreeHandler.h>
5 #include <creaImageIOTree.h>
16 //=======================================================================
17 /// Concrete TreeHandler which manages a Tree stored in a sqlite database
18 class SQLiteTreeHandler : virtual public TreeHandler
21 //====================================================================
22 /// Ctor with database file name
23 SQLiteTreeHandler(const std::string& filename);
25 virtual ~SQLiteTreeHandler();
26 //====================================================================
28 //====================================================================
29 /// Returns the sqlite db file name
30 const std::string& GetFileName() const { return mFileName; }
31 //====================================================================
33 //====================================================================
35 /// Is the 'source' readable ?
36 virtual bool IsReadable() { return true; }
37 /// Is the 'source' writable ?
38 virtual bool IsWritable() { return true; }
39 //====================================================================
42 //====================================================================
43 // INITIALIZATION / FINALIZATION
44 //====================================================================
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'
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 //====================================================================
66 //====================================================================
68 //====================================================================
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 //====================================================================
78 //====================================================================
79 /// Returns the attribute requested. Useful for synchronization.
80 virtual void GetAttribute(std::string levelDescriptor,
81 std::string searchParam,
82 std::string searchVal,
85 //====================================================================
88 //====================================================================
89 /// Recursively loads the children of node 'parent' until maxlevel
91 // If parent == NULL or parent == tree then starts with the 'children' of
93 // Returns the total number of children loaded.
94 virtual int LoadChildren(tree::Node* parent, int maxlevel);
95 //====================================================================
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 ///====================================================================
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 ///====================================================================
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);
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);
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);
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);
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);
149 //======================================================================
150 /// Open the database
152 /// Import the Tree::Description from database (verifies the structure)
153 bool DBImportTreeDescription();
154 //======================================================================
155 //======================================================================
157 /// Creates a new database on disk and the tables
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 //======================================================================
163 //======================================================================
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 //======================================================================
170 //======================================================================
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 //======================================================================
178 //======================================================================
180 /// Appends to string s the SQL command to set the attributes values
182 void SQLAppendAttributesValues(tree::Node* n, std::string& s);
183 //======================================================================
185 //======================================================================
187 /// Graft the branch defined by the attributes to the parent
188 void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
189 //======================================================================
190 //======================================================================
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 //======================================================================
207 /// Inserts the Node in the database
208 void DBInsert(tree::Node* n);
209 //======================================================================
212 //======================================================================
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 //======================================================================
218 //======================================================================
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);
225 //======================================================================
231 /// The physical location associated to the DicomDatabase (directory, db file...)
232 std::string mFileName;
233 /// Is the DB writable ?
235 void SetWritable(bool w) { mWritable = w; }
236 bool GetWritable() const { return mWritable; }
241 // EO class SQLiteTreeHandler
242 //=======================================================================
245 } // EO namespace creaImageIO