]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSQLiteTreeHandler.h
Documentation Updated
[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    * \ingroup Model
14    */
15   //=======================================================================
16   /// Concrete TreeHandler which manages a Tree stored in a sqlite database
17   class SQLiteTreeHandler : virtual public TreeHandler
18   {
19   public:
20     //====================================================================
21     /// Ctor with database file name 
22     SQLiteTreeHandler(const std::string& filename);
23     /// Dtor
24     virtual ~SQLiteTreeHandler();
25     //====================================================================
26
27     //====================================================================
28     /// Returns the sqlite db file name 
29     const std::string& GetFileName() const { return mFileName; }
30     //====================================================================
31  
32     //====================================================================
33     // QUERY METHODS
34     /// Is the 'source' readable ?
35     virtual bool IsReadable() { return true; }
36     /// Is the 'source' writable ?
37     virtual bool IsWritable() { return true; }
38     //====================================================================
39
40
41     //====================================================================
42     // INITIALIZATION / FINALIZATION
43     //====================================================================
44
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'
51     virtual bool Close();
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     //====================================================================
59
60
61     //====================================================================
62     // READ METHODS
63     //====================================================================
64
65
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     //====================================================================
72
73     //====================================================================
74     /// Recursively loads the children of node 'parent' until maxlevel 
75     // is reached.
76     // If parent == NULL or parent == tree then starts with the 'children' of 
77     // the tree itself.
78     // Returns the total number of children loaded.
79     virtual int LoadChildren(tree::Node* parent, int maxlevel);
80     //====================================================================
81
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     ///====================================================================
87
88
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     //====================================================================
104     
105
106     
107   protected:
108     //======================================================================
109     /// Open the database
110     bool DBOpen();
111     /// Import the Tree::Description from database (verifies the structure)
112     bool DBImportTreeDescription();
113     //======================================================================
114     //======================================================================
115     // Creation
116     /// Creates a new database on disk and the tables
117     bool DBCreate();
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     //======================================================================
121
122     //======================================================================
123
124     /// Returns the parent to which the branch defined by the attributes 
125     // provided must be grafted 
126     tree::Node* DBGetParent( const AttributeMapType& attr);
127     //======================================================================
128
129     //======================================================================
130
131     /// Loads the children of Node parent
132     // Can recurse to numberoflevels levels
133     // \return The total number of Node loaded (may be at different levels)
134     int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
135     //======================================================================
136
137     //======================================================================
138
139     /// Appends to string s the SQL command to set the attributes values 
140     // of node n
141     void SQLAppendAttributesValues(tree::Node* n, std::string& s);
142     //======================================================================
143
144     //======================================================================
145
146     /// Graft the branch defined by the attributes to the parent
147     void DBGraftToParent( tree::Node* parent, const AttributeMapType& attr);
148     //======================================================================
149     //======================================================================
150
151     /// Sets an attribute of a Node and updates the database
152     bool DBSetAttribute(tree::Node*, 
153                         const std::string& key,
154                         const std::string& value);
155     //======================================================================
156     //======================================================================
157
158     /// Inserts the Node in the database
159     void DBInsert(tree::Node* n);
160     //======================================================================
161     /*
162     /// 
163     int DBQueryNumberOfChildren(tree::Node* n);
164     
165     // Insertion
166     bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
167     
168     //
169     std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
170
171     tree::Node* GetNodeFromTypeId(int level, 
172                                   const std::string& id);
173
174     // 
175     tree::Node* DBGetOrCreateNode(tree::Node* alien_node, 
176                                       tree::Node* internal_parent,
177                                       std::string parentid,
178                                   std::string& created_id);
179     //                                UpdateSummary& summary);
180     
181     tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
182                                     std::string& parent_id);
183                                     //                             UpdateSummary& summary);
184     
185     void DBRecursiveGetOrCreateNode(tree::Node* alien_node, 
186                                          tree::Node* parent, 
187                                     const std::string& parent_id);
188     //                                   UpdateSummary& summary);
189     
190
191     // 
192     void DBRecursiveRemoveNode(tree::Node* node);
193
194
195     void BuildSQLFieldsValues(tree::Node* n,
196                               std::string& str);
197
198
199     tree::Node* GetChildrenLike(tree::Node* internal_parent,
200                                tree::Node* alien_node);
201     */
202   private:
203     /// The DB
204     CppSQLite3DB* mDB;
205    /// The physical location associated to the DicomDatabase (directory, db file...)
206     std::string mFileName;
207     /// Is the DB writable ?
208     bool mWritable;
209     void SetWritable(bool w) { mWritable; }
210     bool GetWritable() const { return mWritable; }
211
212     /*
213     struct TypeId
214     {
215       int type;
216       std::string id;
217       bool operator< (const TypeId& o) const 
218       {
219         return ((type<o.type) ||
220                 ((type==o.type)&&id<o.id));
221       }
222     };
223     
224     typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
225     TypeIdToNodeMapType mTypeIdToNodeMap;
226     */
227   };
228   // EO class SQLiteTreeHandler
229   //=======================================================================
230
231
232 } // EO namespace creaImageIO
233
234 // EOF
235 #endif  
236