1 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
2 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
4 #include <creaImageIOTreeHandler.h>
14 //=======================================================================
15 /// Concrete TreeHandler which manages a tree stored in a sqlite database
16 class SQLiteTreeHandler : virtual public TreeHandler
19 ///====================================================================
20 /// Ctor with database file name
21 SQLiteTreeHandler(const std::string& filename);
23 virtual ~SQLiteTreeHandler();
24 ///====================================================================
26 ///====================================================================
27 /// Returns the sqlite db file name
28 const std::string& GetFileName() const { return mFileName; }
29 ///====================================================================
31 ///====================================================================
33 /// Is the 'source' readable ?
34 virtual bool IsReadable() { return true; }
35 /// Is the 'source' writable ?
36 virtual bool IsWritable() { return true; }
37 ///====================================================================
40 ///====================================================================
41 /// INITIALIZATION / FINALIZATION
42 ///====================================================================
44 ///====================================================================
45 /// Opens an existing 'source'
46 /// Default mode is read only
47 /// If IsWritable and writable==true then opens in read/write mode
48 virtual bool Open(bool writable = false);
49 /// Closes the 'source'
51 /// Creates a new 'source'
52 /// Default mode is read only
53 /// If IsWritable and writable==true then opens in read/write mode
54 virtual bool Create(bool writable = false);
55 /// Destroys the 'source'
56 virtual bool Destroy();
57 ///====================================================================
60 ///====================================================================
62 ///====================================================================
65 ///====================================================================
66 /// Returns the number of children of the Node *WITHOUT LOADING THEM*
67 /// REM : The Tree itself is a Node and asking for its number of
68 /// children returns the number of children of level 1.
69 virtual unsigned int GetNumberOfChildren(tree::Node* n);
70 ///====================================================================
72 ///====================================================================
73 /// Recursively loads the children of node 'parent' until maxlevel
75 /// If parent == NULL or parent == tree then starts with the 'children' of
77 /// Returns the total number of children loaded.
78 virtual int LoadChildren(tree::Node* parent, int maxlevel);
79 ///====================================================================
81 ///====================================================================
82 /// Unloads the Node and its descendants
83 /// WITHOUT altering the source, e.g. the database
84 virtual void UnLoad(tree::Node* n);
85 ///====================================================================
88 ///====================================================================
89 /// WRITE METHODS : WORK ONLY IN WRITE MODE
90 ///====================================================================
91 /// Adds a branch in the tree with the attributes provided
92 /// returns the Level in the tree where the branch was connected
93 /// (-1 for error, 0 for top level, etc. )
94 /// Of course the branch is loaded on exit
95 virtual int AddBranch( const AttributeMapType& attr );
96 /// Removes the node and its descendants
97 virtual bool Remove(tree::Node*);
98 //====================================================================
103 //======================================================================
104 /// Open the database
106 /// Import the Tree::Description from database (verifies the structure)
107 bool DBImportTreeDescription();
108 //======================================================================
109 //======================================================================
111 /// Creates a new database on disk and the tables
113 /// Appends to string s the SQL command to create the attributes of a given level
114 void AppendAttributesSQLDefinition(int level, std::string& s);
115 //======================================================================
117 //======================================================================
118 tree::Node* DBGetParent( const AttributeMapType& attr);
119 //======================================================================
121 //======================================================================
122 int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
123 //======================================================================
127 int DBQueryNumberOfChildren(tree::Node* n);
130 bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
133 std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
135 tree::Node* GetNodeFromTypeId(int level,
136 const std::string& id);
139 tree::Node* DBGetOrCreateNode(tree::Node* alien_node,
140 tree::Node* internal_parent,
141 std::string parentid,
142 std::string& created_id);
143 // UpdateSummary& summary);
145 tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
146 std::string& parent_id);
147 // UpdateSummary& summary);
149 void DBRecursiveGetOrCreateNode(tree::Node* alien_node,
151 const std::string& parent_id);
152 // UpdateSummary& summary);
156 void DBRecursiveRemoveNode(tree::Node* node);
159 void BuildSQLFieldsValues(tree::Node* n,
163 tree::Node* GetChildrenLike(tree::Node* internal_parent,
164 tree::Node* alien_node);
169 /// The physical location associated to the DicomDatabase (directory, db file...)
170 std::string mFileName;
171 /// Is the DB writable ?
173 void SetWritable(bool w) { mWritable; }
174 bool GetWritable() const { return mWritable; }
181 bool operator< (const TypeId& o) const
183 return ((type<o.type) ||
184 ((type==o.type)&&id<o.id));
188 typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
189 TypeIdToNodeMapType mTypeIdToNodeMap;
192 // EO class SQLiteTreeHandler
193 //=======================================================================
196 } // EO namespace creaImageIO