]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSQLiteTreeHandler.h
Starting version 2
[creaImageIO.git] / src2 / creaImageIOSQLiteTreeHandler.h
1 #ifndef __creaImageIOSQLiteTreeHandler_h_INCLUDED__
2 #define __creaImageIOSQLiteTreeHandler_h_INCLUDED__
3
4 #include <creaImageIOTreeHandler.h>
5
6 class CppSQLite3DB;
7
8 namespace creaImageIO
9 {
10
11
12   //=======================================================================
13   /// Concrete TreeHandler which manages a tree stored in a sqlite database
14   class SQLiteTreeHandler : virtual public TreeHandler
15   {
16   public:
17     ///====================================================================
18     /// Ctor with database file name 
19     SQLiteTreeHandler(const std::string& filename);
20     /// Dtor
21     virtual ~SQLiteTreeHandler();
22     ///====================================================================
23
24     ///====================================================================
25     /// Returns the sqlite db file name 
26     const std::string& GetFileName() const { return mFileName; }
27     ///====================================================================
28  
29     ///====================================================================
30     /// QUERY METHODS
31     /// Is the 'source' readable ?
32     virtual bool IsReadable() { return true; }
33     /// Is the 'source' writable ?
34     virtual bool IsWritable() { return true; }
35     ///====================================================================
36
37
38     ///====================================================================
39     /// INITIALIZATION / FINALIZATION
40     ///====================================================================
41
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'
48     virtual bool Close();
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     ///====================================================================
56
57
58     ///====================================================================
59     // READ METHODS
60     ///====================================================================
61
62
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     ///====================================================================
69
70     ///====================================================================
71     /// Recursively loads the children of node 'parent' until maxlevel 
72     /// is reached.
73     /// If parent == NULL or parent == tree then starts with the 'children' of 
74     /// the tree itself.
75     /// Returns the total number of children loaded.
76     virtual int LoadChildren(tree::Node* parent, int maxlevel);
77     ///====================================================================
78
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     ///====================================================================
84
85
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
94     ( const std::map<std::string,std::string>& attr );
95     /// Removes the node and its descendants 
96     virtual bool Remove(tree::Node*);
97     ///====================================================================
98
99
100
101   protected:
102     // Open 
103     bool DBOpen();
104     void DBImportTreeDescription();
105
106     // Creation
107     /// Creates a new database on disk and the tables
108     bool DBCreate();
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();
114     //
115
116     // Test
117     bool DBStructureIsValid();
118
119     int DBQueryNumberOfChildren(tree::Node* n);
120     
121     // Insertion
122     bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
123     
124     //
125     std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
126
127     tree::Node* GetNodeFromTypeId(int level, 
128                                   const std::string& id);
129
130     // 
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);
136     
137     tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
138                                     std::string& parent_id);
139                                     //                             UpdateSummary& summary);
140     
141     void DBRecursiveGetOrCreateNode(tree::Node* alien_node, 
142                                          tree::Node* parent, 
143                                     const std::string& parent_id);
144     //                                   UpdateSummary& summary);
145     
146
147     // 
148     void DBRecursiveRemoveNode(tree::Node* node);
149
150
151     void BuildSQLFieldsValues(tree::Node* n,
152                               std::string& str);
153
154
155     tree::Node* GetChildrenLike(tree::Node* internal_parent,
156                                tree::Node* alien_node);
157
158   private:
159     /// The DB
160     CppSQLite3DB* mDB;
161    /// The physical location associated to the DicomDatabase (directory, db file...)
162     std::string mFileName;
163     /// Is the DB writable ?
164     bool mWritable;
165     void SetWritable(bool w) { mWritable; }
166     bool GetWritable() const { return mWritable; }
167
168     struct TypeId
169     {
170       int type;
171       std::string id;
172       bool operator< (const TypeId& o) const 
173       {
174         return ((type<o.type) ||
175                 ((type==o.type)&&id<o.id));
176       }
177     };
178     
179     typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
180     TypeIdToNodeMapType mTypeIdToNodeMap;
181   };
182   // EO class SQLiteTreeHandler
183   //=======================================================================
184
185
186 } // EO namespace creaImageIO
187
188 // EOF
189 #endif  
190