]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSQLiteTreeHandler.h
341b81bb014fc0de2060c664f96237d1d6d120c6
[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         * \ingroup Model
13         */
14   //=======================================================================
15   /// Concrete TreeHandler which manages a tree stored in a sqlite database
16   class SQLiteTreeHandler : virtual public TreeHandler
17   {
18   public:
19     ///====================================================================
20     /// Ctor with database file name 
21     SQLiteTreeHandler(const std::string& filename);
22     /// Dtor
23     virtual ~SQLiteTreeHandler();
24     ///====================================================================
25
26     ///====================================================================
27     /// Returns the sqlite db file name 
28     const std::string& GetFileName() const { return mFileName; }
29     ///====================================================================
30  
31     ///====================================================================
32     /// QUERY METHODS
33     /// Is the 'source' readable ?
34     virtual bool IsReadable() { return true; }
35     /// Is the 'source' writable ?
36     virtual bool IsWritable() { return true; }
37     ///====================================================================
38
39
40     ///====================================================================
41     /// INITIALIZATION / FINALIZATION
42     ///====================================================================
43
44     ///====================================================================
45     /// Opens an existing 'source' 
46     /// Default mode is read only 
47     /// If IsWritable and writable==true then opens in read/write mode
48     virtual bool Open(bool writable = false);
49     /// Closes the 'source'
50     virtual bool Close();
51     /// Creates a new 'source' 
52     /// Default mode is read only 
53     /// If IsWritable and writable==true then opens in read/write mode
54     virtual bool Create(bool writable = false);
55     /// Destroys the 'source'
56     virtual bool Destroy();
57     ///====================================================================
58
59
60     ///====================================================================
61     // READ METHODS
62     ///====================================================================
63
64
65     ///====================================================================
66     /// Returns the number of children of the Node *WITHOUT LOADING THEM*
67     /// REM : The Tree itself is a Node and asking for its number of 
68     ///       children returns the number of children of level 1.
69     virtual unsigned int GetNumberOfChildren(tree::Node* n);
70     ///====================================================================
71
72     ///====================================================================
73     /// Recursively loads the children of node 'parent' until maxlevel 
74     /// is reached.
75     /// If parent == NULL or parent == tree then starts with the 'children' of 
76     /// the tree itself.
77     /// Returns the total number of children loaded.
78     virtual int LoadChildren(tree::Node* parent, int maxlevel);
79     ///====================================================================
80
81     ///====================================================================
82     /// Unloads the Node and its descendants
83     /// WITHOUT altering the source, e.g. the database
84     virtual void UnLoad(tree::Node* n);
85     ///====================================================================
86
87
88     ///====================================================================
89     /// WRITE METHODS : WORK ONLY IN WRITE MODE
90     ///====================================================================
91     /// Adds a branch in the tree with the attributes provided
92     /// returns the Level in the tree where the branch was connected 
93     /// (-1 for error, 0 for top level, etc. ) 
94     /// Of course the branch is loaded on exit
95     virtual int AddBranch( const AttributeMapType& attr );
96     /// Removes the node and its descendants 
97     virtual bool Remove(tree::Node*);
98     //====================================================================
99     
100
101     
102   protected:
103     //======================================================================
104     /// Open the database
105     bool DBOpen();
106     /// Import the Tree::Description from database (verifies the structure)
107     bool DBImportTreeDescription();
108     //======================================================================
109     //======================================================================
110     // Creation
111     /// Creates a new database on disk and the tables
112     bool DBCreate();
113     /// Appends to string s the SQL command to create the attributes of a given level
114     void AppendAttributesSQLDefinition(int level, std::string& s);
115     //======================================================================
116
117     //======================================================================
118     tree::Node* DBGetParent( const AttributeMapType& attr);
119     //======================================================================
120
121     //======================================================================
122     int DBLoadChildren( tree::Node* parent, int numberoflevels = 1);
123     //======================================================================
124
125     /*
126     /// 
127     int DBQueryNumberOfChildren(tree::Node* n);
128     
129     // Insertion
130     bool DBInsert(tree::Node* alien_node) ; //, UpdateSummary& summary);
131     
132     //
133     std::string DBGetNodeId(tree::Node* node, const std::string& parent_id);
134
135     tree::Node* GetNodeFromTypeId(int level, 
136                                   const std::string& id);
137
138     // 
139     tree::Node* DBGetOrCreateNode(tree::Node* alien_node, 
140                                       tree::Node* internal_parent,
141                                       std::string parentid,
142                                   std::string& created_id);
143     //                                UpdateSummary& summary);
144     
145     tree::Node* DBGetOrCreateParent(tree::Node* alien_node,
146                                     std::string& parent_id);
147                                     //                             UpdateSummary& summary);
148     
149     void DBRecursiveGetOrCreateNode(tree::Node* alien_node, 
150                                          tree::Node* parent, 
151                                     const std::string& parent_id);
152     //                                   UpdateSummary& summary);
153     
154
155     // 
156     void DBRecursiveRemoveNode(tree::Node* node);
157
158
159     void BuildSQLFieldsValues(tree::Node* n,
160                               std::string& str);
161
162
163     tree::Node* GetChildrenLike(tree::Node* internal_parent,
164                                tree::Node* alien_node);
165     */
166   private:
167     /// The DB
168     CppSQLite3DB* mDB;
169    /// The physical location associated to the DicomDatabase (directory, db file...)
170     std::string mFileName;
171     /// Is the DB writable ?
172     bool mWritable;
173     void SetWritable(bool w) { mWritable; }
174     bool GetWritable() const { return mWritable; }
175
176     /*
177     struct TypeId
178     {
179       int type;
180       std::string id;
181       bool operator< (const TypeId& o) const 
182       {
183         return ((type<o.type) ||
184                 ((type==o.type)&&id<o.id));
185       }
186     };
187     
188     typedef std::map<TypeId,tree::Node*> TypeIdToNodeMapType;
189     TypeIdToNodeMapType mTypeIdToNodeMap;
190     */
191   };
192   // EO class SQLiteTreeHandler
193   //=======================================================================
194
195
196 } // EO namespace creaImageIO
197
198 // EOF
199 #endif  
200