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
94 ( const std::map<std::string,std::string>& attr );
95 /// Removes the node and its descendants
96 virtual bool Remove(tree::Node*);
97 ///====================================================================
104 void DBImportTreeDescription();
107 /// Creates a new database on disk and the tables
109 /// Creates the default tree description for a new database
110 void BuildDefaultTreeDescription();
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 void DBExportTreeDescription();
117 bool DBStructureIsValid();
119 int DBQueryNumberOfChildren(tree::Node* n);
122 bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
125 std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
127 tree::Node* GetNodeFromTypeId(int level,
128 const std::string& id);
131 tree::Node* DBGetOrCreateNode(tree::Node* alien_node,
132 tree::Node* internal_parent,
133 std::string parentid,
134 std::string& created_id);
135 // UpdateSummary& summary);
137 tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
138 std::string& parent_id);
139 // UpdateSummary& summary);
141 void DBRecursiveGetOrCreateNode(tree::Node* alien_node,
143 const std::string& parent_id);
144 // UpdateSummary& summary);
148 void DBRecursiveRemoveNode(tree::Node* node);
151 void BuildSQLFieldsValues(tree::Node* n,
155 tree::Node* GetChildrenLike(tree::Node* internal_parent,
156 tree::Node* alien_node);
161 /// The physical location associated to the DicomDatabase (directory, db file...)
162 std::string mFileName;
163 /// Is the DB writable ?
165 void SetWritable(bool w) { mWritable; }
166 bool GetWritable() const { return mWritable; }
172 bool operator< (const TypeId& o) const
174 return ((type<o.type) ||
175 ((type==o.type)&&id<o.id));
179 typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
180 TypeIdToNodeMapType mTypeIdToNodeMap;
182 // EO class SQLiteTreeHandler
183 //=======================================================================
186 } // EO namespace creaImageIO