1 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
2 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
4 #include <creaImageIOTreeHandler.h>
12 //=======================================================================
13 /// Concrete TreeHandler which manages a tree stored in a sqlite database
14 class SQLiteTreeHandler : virtual public TreeHandler
17 ///====================================================================
18 /// Ctor with database file name
19 SQLiteTreeHandler(const std::string& filename);
21 virtual ~SQLiteTreeHandler();
22 ///====================================================================
24 ///====================================================================
25 /// Returns the sqlite db file name
26 const std::string& GetFileName() const { return mFileName; }
27 ///====================================================================
29 ///====================================================================
31 /// Is the 'source' readable ?
32 virtual bool IsReadable() { return true; }
33 /// Is the 'source' writable ?
34 virtual bool IsWritable() { return true; }
35 ///====================================================================
38 ///====================================================================
39 /// INITIALIZATION / FINALIZATION
40 ///====================================================================
42 ///====================================================================
43 /// Opens an existing 'source'
44 /// Default mode is read only
45 /// If IsWritable and writable==true then opens in read/write mode
46 virtual bool Open(bool writable = false);
47 /// Closes the 'source'
49 /// Creates a new 'source'
50 /// Default mode is read only
51 /// If IsWritable and writable==true then opens in read/write mode
52 virtual bool Create(bool writable = false);
53 /// Destroys the 'source'
54 virtual bool Destroy();
55 ///====================================================================
58 ///====================================================================
60 ///====================================================================
63 ///====================================================================
64 /// Returns the number of children of the Node *WITHOUT LOADING THEM*
65 /// REM : The Tree itself is a Node and asking for its number of
66 /// children returns the number of children of level 1.
67 virtual unsigned int GetNumberOfChildren(tree::Node* n);
68 ///====================================================================
70 ///====================================================================
71 /// Recursively loads the children of node 'parent' until maxlevel
73 /// If parent == NULL or parent == tree then starts with the 'children' of
75 /// Returns the total number of children loaded.
76 virtual int LoadChildren(tree::Node* parent, int maxlevel);
77 ///====================================================================
79 ///====================================================================
80 /// Unloads the Node and its descendants
81 /// WITHOUT altering the source, e.g. the database
82 virtual void UnLoad(tree::Node* n);
83 ///====================================================================
86 ///====================================================================
87 /// WRITE METHODS : WORK ONLY IN WRITE MODE
88 ///====================================================================
89 /// Adds a branch in the tree with the attributes provided
90 /// returns the Level in the tree where the branch was connected
91 /// (-1 for error, 0 for top level, etc. )
92 /// Of course the branch is loaded on exit
93 virtual int AddBranch( const AttributeMapType& attr );
94 /// Removes the node and its descendants
95 virtual bool Remove(tree::Node*);
96 //====================================================================
101 //======================================================================
102 /// Open the database
104 /// Import the Tree::Description from database (verifies the structure)
105 bool DBImportTreeDescription();
106 //======================================================================
107 //======================================================================
109 /// Creates a new database on disk and the tables
111 /// Appends to string s the SQL command to create the attributes of a given level
112 void AppendAttributesSQLDefinition(int level, std::string& s);
113 //======================================================================
115 //======================================================================
116 tree::Node* DBGetParent( const AttributeMapType& attr);
117 //======================================================================
119 //======================================================================
120 int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
121 //======================================================================
125 int DBQueryNumberOfChildren(tree::Node* n);
128 bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
131 std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
133 tree::Node* GetNodeFromTypeId(int level,
134 const std::string& id);
137 tree::Node* DBGetOrCreateNode(tree::Node* alien_node,
138 tree::Node* internal_parent,
139 std::string parentid,
140 std::string& created_id);
141 // UpdateSummary& summary);
143 tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
144 std::string& parent_id);
145 // UpdateSummary& summary);
147 void DBRecursiveGetOrCreateNode(tree::Node* alien_node,
149 const std::string& parent_id);
150 // UpdateSummary& summary);
154 void DBRecursiveRemoveNode(tree::Node* node);
157 void BuildSQLFieldsValues(tree::Node* n,
161 tree::Node* GetChildrenLike(tree::Node* internal_parent,
162 tree::Node* alien_node);
167 /// The physical location associated to the DicomDatabase (directory, db file...)
168 std::string mFileName;
169 /// Is the DB writable ?
171 void SetWritable(bool w) { mWritable; }
172 bool GetWritable() const { return mWritable; }
179 bool operator< (const TypeId& o) const
181 return ((type<o.type) ||
182 ((type==o.type)&&id<o.id));
186 typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
187 TypeIdToNodeMapType mTypeIdToNodeMap;
190 // EO class SQLiteTreeHandler
191 //=======================================================================
194 } // EO namespace creaImageIO