]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSQLiteTreeHandler.h
*** empty log message ***
[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( const AttributeMapType& attr );
94     /// Removes the node and its descendants 
95     virtual bool Remove(tree::Node*);
96     //====================================================================
97     
98
99     
100   protected:
101     //======================================================================
102     /// Open the database
103     bool DBOpen();
104     /// Import the Tree::Description from database (verifies the structure)
105     bool DBImportTreeDescription();
106     //======================================================================
107     //======================================================================
108     // Creation
109     /// Creates a new database on disk and the tables
110     bool DBCreate();
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     //======================================================================
114
115     //======================================================================
116     tree::Node* DBGetParent( const AttributeMapType& attr);
117     //======================================================================
118
119     //======================================================================
120     int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
121     //======================================================================
122
123     /*
124     /// 
125     int DBQueryNumberOfChildren(tree::Node* n);
126     
127     // Insertion
128     bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
129     
130     //
131     std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
132
133     tree::Node* GetNodeFromTypeId(int level, 
134                                   const std::string& id);
135
136     // 
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);
142     
143     tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
144                                     std::string& parent_id);
145                                     //                             UpdateSummary& summary);
146     
147     void DBRecursiveGetOrCreateNode(tree::Node* alien_node, 
148                                          tree::Node* parent, 
149                                     const std::string& parent_id);
150     //                                   UpdateSummary& summary);
151     
152
153     // 
154     void DBRecursiveRemoveNode(tree::Node* node);
155
156
157     void BuildSQLFieldsValues(tree::Node* n,
158                               std::string& str);
159
160
161     tree::Node* GetChildrenLike(tree::Node* internal_parent,
162                                tree::Node* alien_node);
163     */
164   private:
165     /// The DB
166     CppSQLite3DB* mDB;
167    /// The physical location associated to the DicomDatabase (directory, db file...)
168     std::string mFileName;
169     /// Is the DB writable ?
170     bool mWritable;
171     void SetWritable(bool w) { mWritable; }
172     bool GetWritable() const { return mWritable; }
173
174     /*
175     struct TypeId
176     {
177       int type;
178       std::string id;
179       bool operator< (const TypeId& o) const 
180       {
181         return ((type<o.type) ||
182                 ((type==o.type)&&id<o.id));
183       }
184     };
185     
186     typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
187     TypeIdToNodeMapType mTypeIdToNodeMap;
188     */
189   };
190   // EO class SQLiteTreeHandler
191   //=======================================================================
192
193
194 } // EO namespace creaImageIO
195
196 // EOF
197 #endif  
198