2 * timedate - Displays time and date and daily events
3 * Copyright (c) <2009>, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 2.1, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 * You should have received a copy of the GNU Lesser General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "sqlitequery.h"
29 sqlitequery::init(sqlitedatabase * db)
32 odb = (db ? db->grabdb() : NULL);
37 sqlitequery::init(sqlitedatabase * dbin, const std::string & sql)
43 sqlitequery::~sqlitequery()
47 printf( "sqlite3_finalize in destructor\n");
48 sqlite3_finalize(result);
56 bool sqlitequery::execute(const std::string & sql)
58 std::string sql_sqlite=sql;
59 m_last_query = sql_sqlite;
62 string err = "execute: query busy: "+sql_sqlite;
63 printf("%s\n", err.c_str());
67 const char * s = NULL;
68 int err = sqlite3_busy_timeout((sqlite3 *)odb->db, 10000 );
72 printf("execute: busy timeout occured: \n");
75 int rc = sqlite3_prepare_v2((sqlite3 *)odb->db, sql_sqlite.c_str(), sql_sqlite.size(), &result, &s);
79 string err = "execute: prepare query failed: "+sql_sqlite;
80 printf("%s\n", err.c_str());
86 printf( "execute: query failed\n");
90 rc = sqlite3_step(result);
91 sqlite3_finalize(result);
96 printf( "execute: database busy\n");
102 printf( "execute: Schema error\n");
105 printf("%s\n", sqlite3_errmsg((sqlite3 *)odb->db));
108 printf( "execute: database misuse\n");
111 printf( "execute: unknown result code\n");
117 bool sqlitequery::fetchRow()
123 int rc = cache_rc_valid ? cache_rc : sqlite3_step(result);
124 cache_rc_valid = false;
128 printf( "execute: database busy\n");
136 printf("%s\n", sqlite3_errmsg((sqlite3 *)odb->db));
139 printf( "execute: database misuse\n");
142 printf( "execute: unknown result code\n");
148 void sqlitequery::freeResult()
152 sqlite3_finalize(result);
155 cache_rc_valid = false;
159 std::map<std::string, int>::iterator it = m_nmap.begin();
164 void sqlitequery::resetStatement()
167 sqlite3_reset(result);
170 long sqlitequery::getCount(const std::string & sql)
177 l = getVal(rowcount++);
184 bool sqlitequery::prepareStatement(const std::string & sql)
186 std::string sql_sqlite=sql;
187 m_last_query = sql_sqlite;
190 string err = "prepareStatement: query busy: "+sql_sqlite;
191 printf("%s\n", err.c_str());
195 const char * s = NULL;
196 int rc = sqlite3_prepare_v2((sqlite3 *)odb->db, sql_sqlite.c_str(), sql_sqlite.size(), &result, &s);
199 string err = "prepareStatement: prepare query failed: "+sql_sqlite;
200 printf("%s\n", err.c_str());
205 printf( "prepareStatement: query failed\n");
212 bool sqlitequery::getResult(const std::string & sql)
214 if(prepareStatement(sql))
217 const char * p = sqlite3_column_name(result, i);
221 p = sqlite3_column_name(result, i);
224 cache_rc = sqlite3_step(result);
225 cache_rc_valid = true;
226 m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
231 bool sqlitequery::bind(const std::string bindMatch)
233 if(!odb || !result) return false;
235 int r = sqlite3_bind_text(result,1,bindMatch.c_str(),bindMatch.length(),NULL);
239 printf("sqlitequery::bind - error binding query\n");
244 const char * p = sqlite3_column_name(result, i);
248 p = sqlite3_column_name(result, i);
251 cache_rc = sqlite3_step(result);
252 cache_rc_valid = true;
253 m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
258 bool sqlitequery::bind(const int value)
260 if(!odb || !result) return false;
262 int r = sqlite3_bind_int(result,1,value);
266 printf("sqlitequery::bind - error binding query\n");
271 const char * p = sqlite3_column_name(result, i);
275 p = sqlite3_column_name(result, i);
279 cache_rc = sqlite3_step(result);
280 cache_rc_valid = true;
281 m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
286 bool sqlitequery::bind(const double value)
288 if(!odb || !result) return false;
290 int r = sqlite3_bind_double(result,1,value);
294 printf("sqlitequery::bind - error binding query\n");
299 const char * p = sqlite3_column_name(result, i);
303 p = sqlite3_column_name(result, i);
306 cache_rc = sqlite3_step(result);
307 cache_rc_valid = true;
308 m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0;
310 return cache_rc == SQLITE_OK;
313 int64_t sqlitequery::getBigInt(int x)
315 if(odb && result && row)
317 return sqlite3_column_int64(result, x);
322 int sqlitequery::GetErrno()
326 return sqlite3_errcode((sqlite3 *)odb->db);
331 std::string sqlitequery::GetError()
335 return sqlite3_errmsg((sqlite3 *)odb->db);
340 double sqlitequery::getNum(int x)
342 if(odb && result && row)
344 return sqlite3_column_double(result, x);
349 const char * sqlitequery::getStr(int x)
351 if(odb && result && row && x < sqlite3_column_count(result))
353 const unsigned char * tmp = sqlite3_column_text(result, x);
354 return tmp ? (const char *)tmp : "";
359 uint64_t sqlitequery::getUBigInt(int x)
361 if(odb && result && row)
363 return (uint64_t)sqlite3_column_int64(result, x);
368 unsigned long sqlitequery::getUVal(int x)
370 if(odb && result && row)
372 return (unsigned long)sqlite3_column_int(result, x);
377 long sqlitequery::getVal(int x)
379 if(odb && result && row)
381 return sqlite3_column_int(result, x);
386 bool sqlitequery::isNull(int x)
388 if(odb && result && row)
390 if(sqlite3_column_type(result, x) == SQLITE_NULL)
398 long sqlitequery::numRows()
400 return odb && result ? m_row_count : 0;