00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "sqlitepp/sqlitepp.h"
00022
00023 namespace SQLitePP {
00024
00025 sqlite3_stmt* prepareStatement(const char* sqlcode, sqlite3* db)
00026 {
00027 const char* tail;
00028 sqlite3_stmt* stmt;
00029 int rcode = sqlite3_prepare(db,
00030 sqlcode, -1,
00031 &stmt, &tail);
00032 if(rcode != SQLITE_OK) {
00033 showSqlite3Error(__PRETTY_FUNCTION__, db);
00034 }
00035 return stmt;
00036 }
00037
00038
00039 void showSqlite3Error(const char* infunction, sqlite3* db)
00040 {
00041 std::cerr << infunction << ":" << sqlite3_errmsg(db) << std::endl;
00042 abort();
00043 }
00044
00045 template<>
00046 int ResultIterator::get<int>(int column)
00047 {
00048 return sqlite3_column_int(m_stmt, column);
00049 }
00050
00051 template<>
00052 unsigned int ResultIterator::get<unsigned int>(int column)
00053 {
00054 return sqlite3_column_int(m_stmt, column);
00055 }
00056
00057 template<>
00058 const char* ResultIterator::get<const char*>(int column)
00059 {
00060 return reinterpret_cast<const char*>(sqlite3_column_text(m_stmt, column));
00061 }
00062
00063 template<>
00064 Blob ResultIterator::get<Blob>(int column)
00065 {
00066 return std::make_pair(sqlite3_column_blob(m_stmt, column),
00067 sqlite3_column_bytes(m_stmt, column));
00068 }
00069
00070 template<>
00071 std::string ResultIterator::get<std::string>(int column)
00072 {
00073 std::string s;
00074 s.assign(reinterpret_cast<const char*>(sqlite3_column_blob(m_stmt, column)),
00075 sqlite3_column_bytes(m_stmt, column));
00076 return s;
00077 }
00078
00079 template<>
00080 schma::UnicodePtr ResultIterator::get<schma::UnicodePtr>(int column)
00081 {
00082 return schma::UnicodePtr(new UnicodeString(reinterpret_cast<const char*>(sqlite3_column_text(m_stmt, column))));
00083 }
00084
00085 SqliteDB::SqliteDB(const std::string& filename)
00086 {
00087 int rcode = sqlite3_open(filename.c_str(), &m_dbhandle);
00088 if(rcode != SQLITE_OK) {
00089 std::cerr << sqlite3_errmsg(m_dbhandle) << std::endl;
00090 p_assert(rcode == SQLITE_OK, "db could not be opened");
00091 }
00092 sqlite3_stmt* stmt;
00093 const char* tail;
00094 sqlite3_prepare(m_dbhandle, "pragma synchronous = OFF", -1,
00095 &stmt, &tail);
00096 sqlite3_step(stmt);
00097 sqlite3_finalize(stmt);
00098 }
00099
00100 }