1 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
2 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
4 #include <creaImageIOTreeHandler.h>
15 //=======================================================================
16 /// Concrete TreeHandler which manages a tree stored in a sqlite database
17 class SQLiteTreeHandler : virtual public TreeHandler
20 ///====================================================================
21 /// Ctor with database file name
22 SQLiteTreeHandler(const std::string& filename);
24 virtual ~SQLiteTreeHandler();
25 ///====================================================================
27 ///====================================================================
28 /// Returns the sqlite db file name
29 const std::string& GetFileName() const { return mFileName; }
30 ///====================================================================
32 ///====================================================================
34 /// Is the 'source' readable ?
35 virtual bool IsReadable() { return true; }
36 /// Is the 'source' writable ?
37 virtual bool IsWritable() { return true; }
38 ///====================================================================
41 ///====================================================================
42 /// INITIALIZATION / FINALIZATION
43 ///====================================================================
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'
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 ///====================================================================
61 ///====================================================================
63 ///====================================================================
66 ///====================================================================
67 /// Returns the number of children of the Node *WITHOUT LOADING THEM*
68 /// REM : The Tree itself is a Node and asking for its number of
69 /// children returns the number of children of level 1.
70 virtual unsigned int GetNumberOfChildren(tree::Node* n);
71 ///====================================================================
73 ///====================================================================
74 /// Recursively loads the children of node 'parent' until maxlevel
76 /// If parent == NULL or parent == tree then starts with the 'children' of
78 /// Returns the total number of children loaded.
79 virtual int LoadChildren(tree::Node* parent, int maxlevel);
80 ///====================================================================
82 ///====================================================================
83 /// Unloads the Node and its descendants
84 /// WITHOUT altering the source, e.g. the database
85 virtual void UnLoad(tree::Node* n);
86 ///====================================================================
89 ///====================================================================
90 /// WRITE METHODS : WORK ONLY IN WRITE MODE
91 ///====================================================================
92 /// Adds a branch in the tree with the attributes provided
93 /// returns the Level in the tree where the branch was connected
94 /// (-1 for error, 0 for top level, etc. )
95 /// Of course the branch is loaded on exit
96 virtual int AddBranch( const AttributeMapType& attr );
97 /// Removes the node and its descendants
98 virtual bool Remove(tree::Node*);
99 /// Sets an attribute of a Node
100 virtual bool SetAttribute(tree::Node*,
101 const std::string& key,
102 const std::string& value);
103 //====================================================================
108 //======================================================================
109 /// Open the database
111 /// Import the Tree::Description from database (verifies the structure)
112 bool DBImportTreeDescription();
113 //======================================================================
114 //======================================================================
116 /// Creates a new database on disk and the tables
118 /// Appends to string s the SQL command to create the attributes of a given level
119 void SQLAppendAttributesDefinition(int level, std::string& s);
120 //======================================================================
122 //======================================================================
123 /// Returns the parent to which the branch defined by the attributes
124 /// provided must be grafted
125 tree::Node* DBGetParent( const AttributeMapType& attr);
126 //======================================================================
128 //======================================================================
129 /// Loads the children of Node parent
130 /// Can recurse to numberoflevels levels
131 /// \return The total number of Node loaded (may be at different levels)
132 int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
133 //======================================================================
135 //======================================================================
136 /// Appends to string s the SQL command to set the attributes values
138 void SQLAppendAttributesValues(tree::Node* n, std::string& s);
139 //======================================================================
141 //======================================================================
142 /// Graft the branch defined by the attributes to the parent
143 void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
144 //======================================================================
145 //======================================================================
146 /// Sets an attribute of a Node and updates the database
147 bool DBSetAttribute(tree::Node*,
148 const std::string& key,
149 const std::string& value);
150 //======================================================================
151 //======================================================================
152 /// Inserts the Node in the database
153 void DBInsert(tree::Node* n);
154 //======================================================================
157 int DBQueryNumberOfChildren(tree::Node* n);
160 bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
163 std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
165 tree::Node* GetNodeFromTypeId(int level,
166 const std::string& id);
169 tree::Node* DBGetOrCreateNode(tree::Node* alien_node,
170 tree::Node* internal_parent,
171 std::string parentid,
172 std::string& created_id);
173 // UpdateSummary& summary);
175 tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
176 std::string& parent_id);
177 // UpdateSummary& summary);
179 void DBRecursiveGetOrCreateNode(tree::Node* alien_node,
181 const std::string& parent_id);
182 // UpdateSummary& summary);
186 void DBRecursiveRemoveNode(tree::Node* node);
189 void BuildSQLFieldsValues(tree::Node* n,
193 tree::Node* GetChildrenLike(tree::Node* internal_parent,
194 tree::Node* alien_node);
199 /// The physical location associated to the DicomDatabase (directory, db file...)
200 std::string mFileName;
201 /// Is the DB writable ?
203 void SetWritable(bool w) { mWritable; }
204 bool GetWritable() const { return mWritable; }
211 bool operator< (const TypeId& o) const
213 return ((type<o.type) ||
214 ((type==o.type)&&id<o.id));
218 typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
219 TypeIdToNodeMapType mTypeIdToNodeMap;
222 // EO class SQLiteTreeHandler
223 //=======================================================================
226 } // EO namespace creaImageIO