creaImageIO_lib
CppSQLite3DB Class Reference

#include <CppSQLite3.h>

Public Member Functions

 CppSQLite3DB ()
 
virtual ~CppSQLite3DB ()
 
void open (const char *szFile)
 
void close ()
 
bool tableExists (const char *szTable)
 
int execDML (const char *szSQL)
 
CppSQLite3Query execQuery (const char *szSQL)
 
int execScalar (const char *szSQL)
 
CppSQLite3Table getTable (const char *szSQL)
 
CppSQLite3Statement compileStatement (const char *szSQL)
 
sqlite_int64 lastRowId ()
 
void interrupt ()
 
void setBusyTimeout (int nMillisecs)
 

Static Public Member Functions

static const char * SQLiteVersion ()
 

Private Member Functions

 CppSQLite3DB (const CppSQLite3DB &db)
 
CppSQLite3DBoperator= (const CppSQLite3DB &db)
 
sqlite3_stmt * compile (const char *szSQL)
 
void checkDB ()
 

Private Attributes

sqlite3 * mpDB
 
int mnBusyTimeoutMs
 

Detailed Description

Definition at line 527 of file CppSQLite3.h.

Constructor & Destructor Documentation

CppSQLite3DB::CppSQLite3DB ( )

Definition at line 2215 of file CppSQLite3.cpp.

References mnBusyTimeoutMs, and mpDB.

2217 {
2218 
2219  mpDB = 0;
2220 
2221  mnBusyTimeoutMs = 60000; // 60 seconds
2222 
2223 }
CppSQLite3DB::~CppSQLite3DB ( )
virtual

Definition at line 2243 of file CppSQLite3.cpp.

References close().

2245 {
2246 
2247  close();
2248 
2249 }

Here is the call graph for this function:

CppSQLite3DB::CppSQLite3DB ( const CppSQLite3DB db)
private

Definition at line 2229 of file CppSQLite3.cpp.

References mnBusyTimeoutMs, and mpDB.

2231 {
2232 
2233  mpDB = db.mpDB;
2234 
2235  mnBusyTimeoutMs = 60000; // 60 seconds
2236 
2237 }

Member Function Documentation

void CppSQLite3DB::checkDB ( )
private

Definition at line 2553 of file CppSQLite3.cpp.

References CPPSQLITE_ERROR, DONT_DELETE_MSG, and mpDB.

Referenced by compile(), compileStatement(), execDML(), execQuery(), and getTable().

2555 {
2556 
2557  if (!mpDB)
2558 
2559  {
2560 
2562 
2563  "Database not open",
2564 
2565  DONT_DELETE_MSG);
2566 
2567  }
2568 
2569 }

Here is the caller graph for this function:

void CppSQLite3DB::close ( )

Definition at line 2299 of file CppSQLite3.cpp.

References mpDB.

Referenced by ~CppSQLite3DB().

2301 {
2302 
2303  if (mpDB)
2304 
2305  {
2306 
2307  sqlite3_close(mpDB);
2308 
2309  mpDB = 0;
2310 
2311  }
2312 
2313 }

Here is the caller graph for this function:

sqlite3_stmt * CppSQLite3DB::compile ( const char *  szSQL)
private

Definition at line 2575 of file CppSQLite3.cpp.

References checkDB(), and mpDB.

Referenced by compileStatement(), and execQuery().

2577 {
2578 
2579  checkDB();
2580 
2581 
2582 
2583  char* szError=0;
2584 
2585  const char* szTail=0;
2586 
2587  sqlite3_stmt* pVM;
2588 
2589 
2590 
2591  int nRet = sqlite3_prepare(mpDB, szSQL, -1, &pVM, &szTail);
2592 
2593 
2594 
2595  if (nRet != SQLITE_OK)
2596 
2597  {
2598 
2599  throw CppSQLite3Exception(nRet, szError);
2600 
2601  }
2602 
2603 
2604 
2605  return pVM;
2606 
2607 }

Here is the call graph for this function:

Here is the caller graph for this function:

CppSQLite3Statement CppSQLite3DB::compileStatement ( const char *  szSQL)

Definition at line 2319 of file CppSQLite3.cpp.

References checkDB(), compile(), and mpDB.

2321 {
2322 
2323  checkDB();
2324 
2325 
2326 
2327  sqlite3_stmt* pVM = compile(szSQL);
2328 
2329  return CppSQLite3Statement(mpDB, pVM);
2330 
2331 }

Here is the call graph for this function:

int CppSQLite3DB::execDML ( const char *  szSQL)

Definition at line 2359 of file CppSQLite3.cpp.

References checkDB(), and mpDB.

Referenced by creaImageIO::SQLiteTreeHandler::DBCreate(), and creaImageIO::SQLiteTreeHandler::DBOpen().

2361 {
2362 
2363  checkDB();
2364 
2365 
2366 
2367  char* szError=0;
2368 
2369 
2370 
2371  int nRet = sqlite3_exec(mpDB, szSQL, 0, 0, &szError);
2372 
2373 
2374 
2375  if (nRet == SQLITE_OK)
2376 
2377  {
2378 
2379  return sqlite3_changes(mpDB);
2380 
2381  }
2382 
2383  else
2384 
2385  {
2386 
2387  throw CppSQLite3Exception(nRet, szError);
2388 
2389  }
2390 
2391 }

Here is the call graph for this function:

Here is the caller graph for this function:

CppSQLite3Query CppSQLite3DB::execQuery ( const char *  szSQL)

Definition at line 2397 of file CppSQLite3.cpp.

References checkDB(), compile(), DONT_DELETE_MSG, and mpDB.

Referenced by execScalar().

2399 {
2400 
2401  checkDB();
2402 
2403 
2404 
2405  sqlite3_stmt* pVM = compile(szSQL);
2406 
2407 
2408 
2409  int nRet = sqlite3_step(pVM);
2410 
2411 
2412 
2413  if (nRet == SQLITE_DONE)
2414 
2415  {
2416 
2417  // no rows
2418 
2419  return CppSQLite3Query(mpDB, pVM, true/*eof*/);
2420 
2421  }
2422 
2423  else if (nRet == SQLITE_ROW)
2424 
2425  {
2426 
2427  // at least 1 row
2428 
2429  return CppSQLite3Query(mpDB, pVM, false/*eof*/);
2430 
2431  }
2432 
2433  else
2434 
2435  {
2436 
2437  nRet = sqlite3_finalize(pVM);
2438 
2439  const char* szError= sqlite3_errmsg(mpDB);
2440 
2441  throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
2442 
2443  }
2444 
2445 }

Here is the call graph for this function:

Here is the caller graph for this function:

int CppSQLite3DB::execScalar ( const char *  szSQL)

Definition at line 2451 of file CppSQLite3.cpp.

References CPPSQLITE_ERROR, DONT_DELETE_MSG, CppSQLite3Query::eof(), execQuery(), CppSQLite3Query::fieldValue(), and CppSQLite3Query::numFields().

Referenced by tableExists().

2453 {
2454 
2455  CppSQLite3Query q = execQuery(szSQL);
2456 
2457 
2458 
2459  if (q.eof() || q.numFields() < 1)
2460 
2461  {
2462 
2464 
2465  "Invalid scalar query",
2466 
2467  DONT_DELETE_MSG);
2468 
2469  }
2470 
2471 
2472 
2473  return atoi(q.fieldValue(0));
2474 
2475 }

Here is the call graph for this function:

Here is the caller graph for this function:

CppSQLite3Table CppSQLite3DB::getTable ( const char *  szSQL)

Definition at line 2481 of file CppSQLite3.cpp.

References checkDB(), and mpDB.

2483 {
2484 
2485  checkDB();
2486 
2487 
2488 
2489  char* szError=0;
2490 
2491  char** paszResults=0;
2492 
2493  int nRet;
2494 
2495  int nRows(0);
2496 
2497  int nCols(0);
2498 
2499 
2500 
2501  nRet = sqlite3_get_table(mpDB, szSQL, &paszResults, &nRows, &nCols, &szError);
2502 
2503 
2504 
2505  if (nRet == SQLITE_OK)
2506 
2507  {
2508 
2509  return CppSQLite3Table(paszResults, nRows, nCols);
2510 
2511  }
2512 
2513  else
2514 
2515  {
2516 
2517  throw CppSQLite3Exception(nRet, szError);
2518 
2519  }
2520 
2521 }

Here is the call graph for this function:

void CppSQLite3DB::interrupt ( )
inline

Definition at line 579 of file CppSQLite3.h.

References mpDB.

579 { sqlite3_interrupt(mpDB); }
sqlite_int64 CppSQLite3DB::lastRowId ( )

Definition at line 2527 of file CppSQLite3.cpp.

References mpDB.

Referenced by creaImageIO::SQLiteTreeHandler::DBInsert().

2529 {
2530 
2531  return sqlite3_last_insert_rowid(mpDB);
2532 
2533 }

Here is the caller graph for this function:

void CppSQLite3DB::open ( const char *  szFile)

Definition at line 2271 of file CppSQLite3.cpp.

References DONT_DELETE_MSG, mnBusyTimeoutMs, mpDB, and setBusyTimeout().

Referenced by creaImageIO::Synchronizer::CreateDB(), creaImageIO::TimestampDatabaseHandler::DBCreate(), creaImageIO::SQLiteTreeHandler::DBCreate(), creaImageIO::TimestampDatabaseHandler::DBOpen(), creaImageIO::SQLiteTreeHandler::DBOpen(), and creaImageIO::Synchronizer::Initialize().

2273 {
2274 
2275  int nRet = sqlite3_open(szFile, &mpDB);
2276 
2277 
2278 
2279  if (nRet != SQLITE_OK)
2280 
2281  {
2282 
2283  const char* szError = sqlite3_errmsg(mpDB);
2284 
2285  throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
2286 
2287  }
2288 
2289 
2290 
2292 
2293 }

Here is the call graph for this function:

Here is the caller graph for this function:

CppSQLite3DB & CppSQLite3DB::operator= ( const CppSQLite3DB db)
private

Definition at line 2255 of file CppSQLite3.cpp.

References mnBusyTimeoutMs, and mpDB.

2257 {
2258 
2259  mpDB = db.mpDB;
2260 
2261  mnBusyTimeoutMs = 60000; // 60 seconds
2262 
2263  return *this;
2264 
2265 }
void CppSQLite3DB::setBusyTimeout ( int  nMillisecs)

Definition at line 2539 of file CppSQLite3.cpp.

References mnBusyTimeoutMs, and mpDB.

Referenced by open().

2541 {
2542 
2543  mnBusyTimeoutMs = nMillisecs;
2544 
2545  sqlite3_busy_timeout(mpDB, mnBusyTimeoutMs);
2546 
2547 }

Here is the caller graph for this function:

static const char* CppSQLite3DB::SQLiteVersion ( )
inlinestatic

Definition at line 587 of file CppSQLite3.h.

Referenced by creaImageIO::TimestampDatabaseHandler::TimestampDatabaseHandler().

587 { return SQLITE_VERSION; }

Here is the caller graph for this function:

bool CppSQLite3DB::tableExists ( const char *  szTable)

Definition at line 2337 of file CppSQLite3.cpp.

References execScalar().

Referenced by creaImageIO::SQLiteTreeHandler::DBImportTreeDescription().

2339 {
2340 
2341  char szSQL[128];
2342 
2343  sprintf(szSQL,
2344 
2345  "select count(*) from sqlite_master where type='table' and name='%s'",
2346 
2347  szTable);
2348 
2349  int nRet = execScalar(szSQL);
2350 
2351  return (nRet > 0);
2352 
2353 }

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

int CppSQLite3DB::mnBusyTimeoutMs
private

Definition at line 611 of file CppSQLite3.h.

Referenced by CppSQLite3DB(), open(), operator=(), and setBusyTimeout().

sqlite3* CppSQLite3DB::mpDB
private

The documentation for this class was generated from the following files: