sqlitepp.cpp

Go to the documentation of this file.
00001 /*
00002   Phrasehunter - index and query text corpora
00003   Copyright (C) 2006  Torsten Marek (shlomme@gmx.de) &
00004   Armin Schmidt (armin.sch@gmail.com)
00005   
00006   This program is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU General Public License
00008   as published by the Free Software Foundation; either version 2
00009   of the License, or (at your option) any later version.
00010   
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014   GNU General Public License for more details.
00015   
00016   You should have received a copy of the GNU General Public License
00017   along with this program; if not, write to the Free Software
00018   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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 }

Generated on Thu Dec 21 16:14:41 2006 for The Phrasehunter by  doxygen 1.5.1