1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 * This file contains the SqliteMgr class.
34 typedef SmartPtr
<Statements
> StatementsPtr
; /**< The type of a pointer to a prepared statement bucket. */
36 class StatementStrings
;
37 typedef SmartPtr
<StatementStrings
> StatementStringsPtr
; /**< The type of a pointer to a sql statement bucket. */
39 typedef std::map
<TableImpl
*, StatementsPtr
> TableStatements
; /**< The type of a TableImpl to statements pointer map. */
40 typedef std::map
<TableImpl
*, StatementStringsPtr
> TableStatementStrings
; /**< The type of a TableImpl to a sql statment pointer map. */
43 * This class interfaces between savable managers and an Sqlite database.
45 class SqliteMgr
: public Singleton
<SqliteMgr
>
48 /** Execute an insertion with the specified savable manager. */
49 void doInsert(SavableManager
* bindable
);
51 /** Execute an erase with the specified savable manager. */
52 void doErase(SavableManager
* bindable
);
54 /** Execute an update with the specified savable manager. */
55 void doUpdate(SavableManager
* bindable
);
57 /** Execute a selection with the specified savable manager. */
58 void doSelect(SavableManager
* bindable
);
60 /** Execute a lookup on the specified field with the specified savable manager. */
61 void doLookup(SavableManager
* bindable
, FieldPtr field
);
63 /** Execute a selection on multiple entries from the databse with the specified mask. */
64 void doSelectMulti(SelectionMask
* mask
);
66 /** Counts all rows that match this mask. */
67 void doCount(SelectionMask
* mask
);
70 /** Returns the query to create the specified table. */
71 std::string
tableQuery(TableImplPtr table
) const;
73 /** Whether the database is populated. */
74 bool databasePopulated();
76 /** Initialize the specified table. */
77 void initTable(TableImplPtr table
);
79 /** Whether the table matches out definitions. */
80 bool tableValid(TableImplPtr table
);
84 * This is a singleton class, use the Get method to get the instance.
91 * This is a singleton class, use the Free method to free the instance.
96 friend class Singleton
<SqliteMgr
>;
98 /** Execute the specified statement once, returns true if there are more rows available. */
99 bool doStatement(sqlite3_stmt
* stmt
);
101 /** Commits changes to table, currently clears all prepared statements. */
102 void commit(TableImpl
* table
);
105 /** Returns the prepared statement to insert an entry into the specified table. */
106 sqlite3_stmt
* getInsertStmt(TableImpl
* table
);
108 /** Returns the prepared statement to erase an entry from the specified table. */
109 sqlite3_stmt
* getEraseStmt(TableImpl
* table
);
111 /** Returns the perparedstatement to update an entry in the specified table. */
112 sqlite3_stmt
* getUpdateStmt(TableImpl
* table
);
114 /** Returns the perparedstatement to select an entry from the specified table. */
115 sqlite3_stmt
* getSelectStmt(TableImpl
* table
);
117 /** Returns the perparedstatement to lookup an entry for the specified table on the specified field. */
118 sqlite3_stmt
* getLookupStmt(TableImpl
* table
, FieldPtr field
);
121 * Returns the perparedstatement to select multiple entries from the specified table.
123 * When count is true instead of selecting fields it will perform <code>select count(*)</code>.
125 sqlite3_stmt
* getSelectMultiStmt(SelectionMask
* table
, bool count
);
128 /** Returns the prepared statements bucket for the specified table. */
129 StatementsPtr
getStatements(TableImpl
* table
);
131 /** Returns the SQL statements buckeet for the specified table. */
132 StatementStringsPtr
getStatementStrings(TableImpl
* table
);
136 * Returns the query to create the specified table.
138 * @param table The table to retreive the creation query for.
139 * @param verify Whether the query is to verify the entry in the database or to insert it.
141 std::string
creationQuery(TableImplPtr table
, bool verify
= false) const;
143 /** Returns the query to create a specific field in the table. */
144 std::string
creationString(FieldImplPtr table
) const;
147 Database
* m_db
; /**< The database used. */
148 Database::OPENDB
* m_odb
; /**< The database connection used. */
149 const char* m_leftover
; /**< The leftover from preparing a statement. */
151 TableStatements m_statements
; /**< A map containing the prepared statements per table. */
152 TableStatementStrings m_statementstrings
; /**< A map containing the SQL statements per table. */