1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/*
FALCON - The Falcon Programming Language.
FILE: sqlite3_mod.cpp
SQLite3 driver main module interface
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Sun, 23 May 2010 18:23:20 +0200
-------------------------------------------------------------------
(C) Copyright 2010: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
#include "sqlite3_mod.h"
#include <string.h>
namespace Falcon
{
/******************************************************************************
* (Input) bindings class
*****************************************************************************/
Sqlite3InBind::Sqlite3InBind( sqlite3_stmt* stmt ):
DBIInBind(true), // always changes binding
m_stmt(stmt)
{}
Sqlite3InBind::~Sqlite3InBind()
{
// nothing to do: the statement is not ours.
}
void Sqlite3InBind::onFirstBinding( int size )
{
// nothing to allocate here.
}
void Sqlite3InBind::onItemChanged( int num )
{
DBIBindItem& item = m_ibind[num];
switch( item.type() )
{
// set to null
case DBIBindItem::t_nil:
sqlite3_bind_null( m_stmt, num+1 );
break;
case DBIBindItem::t_bool:
case DBIBindItem::t_int:
sqlite3_bind_int64( m_stmt, num+1, item.asInteger() );
break;
case DBIBindItem::t_double:
sqlite3_bind_double( m_stmt, num+1, item.asDouble() );
break;
case DBIBindItem::t_string:
//TODO: Here, we could use SQLITE_STATIC for everything except for queries.
//That's because sqlite wants the variable binding to stay valid while it
//fetches each new record, as it doesn't create the recordset when the query
//is launched -- so, we must let SQLite to do its own copy in queries,
//but for everything else, the m_ibind storage would be enough. We should
//optimize this function so that it relies to SQLITE_TRANSIENT only in queries.
sqlite3_bind_text( m_stmt, num+1, item.asString(), item.asStringLen(), SQLITE_TRANSIENT );
break;
case DBIBindItem::t_buffer:
sqlite3_bind_blob( m_stmt, num+1, item.asBuffer(), item.asStringLen(), SQLITE_TRANSIENT );
break;
// the time has normally been decoded in the buffer
case DBIBindItem::t_time:
sqlite3_bind_text( m_stmt, num+1, item.asString(), item.asStringLen(), SQLITE_TRANSIENT );
break;
}
}
/******************************************************************************
* Recordset class
*****************************************************************************/
DBIRecordsetSQLite3::DBIRecordsetSQLite3( DBIHandleSQLite3 *dbh, sqlite3_stmt *res )
: DBIRecordset( dbh ),
m_pStmt( new SQLite3StatementHandler(res) ),
m_stmt( res )
{
m_pDbh = dbh->getConn();
m_pDbh->incref();
m_bAsString = dbh->options()->m_bFetchStrings;
m_row = -1; // BOF
m_columnCount = sqlite3_column_count( res );
}
DBIRecordsetSQLite3::DBIRecordsetSQLite3( DBIHandleSQLite3 *dbh, SQLite3StatementHandler *res )
: DBIRecordset( dbh ),
m_stmt( res->handle() )
{
res->incref();
m_pDbh = dbh->getConn();
m_pDbh->incref();
m_bAsString = dbh->options()->m_bFetchStrings;
m_row = -1; // BOF
m_columnCount = sqlite3_column_count( m_stmt );
}
DBIRecordsetSQLite3::~DBIRecordsetSQLite3()
{
if ( m_stmt != NULL )
close();
}
int DBIRecordsetSQLite3::getColumnCount()
{
return m_columnCount;
}
int64 DBIRecordsetSQLite3::getRowIndex()
{
return m_row;
}
int64 DBIRecordsetSQLite3::getRowCount()
{
return -1; // we don't know
}
bool DBIRecordsetSQLite3::getColumnName( int nCol, String& name )
{
if( m_stmt == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_RSET, __LINE__ ) );
if ( nCol < 0 || nCol >= m_columnCount )
{
return false;
}
name.bufferize( sqlite3_column_name( m_stmt, nCol ) );
return true;
}
bool DBIRecordsetSQLite3::fetchRow()
{
if( m_stmt == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_RSET, __LINE__ ) );
int res = sqlite3_step( m_stmt );
if( res == SQLITE_DONE )
return false;
else if ( res != SQLITE_ROW )
DBIHandleSQLite3::throwError( FALCON_DBI_ERROR_FETCH, res );
// more data incoming
m_row++;
return true;
}
bool DBIRecordsetSQLite3::discard( int64 ncount )
{
while ( ncount > 0 )
{
if( ! fetchRow() )
{
return false;
}
--ncount;
}
return true;
}
void DBIRecordsetSQLite3::close()
{
if( m_stmt != 0 )
{
m_pDbh->decref();
m_pStmt->decref();
m_pStmt = 0;
m_stmt = 0;
}
}
bool DBIRecordsetSQLite3::getColumnValue( int nCol, Item& value )
{
if( m_stmt == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_RSET, __LINE__ ) );
if ( nCol < 0 || nCol >= m_columnCount )
{
return false;
}
switch ( sqlite3_column_type(m_stmt, nCol) )
{
case SQLITE_NULL:
value.setNil();
return true;
case SQLITE_INTEGER:
if( m_bAsString )
{
value = new CoreString( (const char*)sqlite3_column_text(m_stmt, nCol), -1 );
}
else
{
value.setInteger( sqlite3_column_int64(m_stmt, nCol) );
}
return true;
case SQLITE_FLOAT:
if( m_bAsString )
{
value = new CoreString( (const char*)sqlite3_column_text( m_stmt, nCol ), -1 );
}
else
{
value.setNumeric( sqlite3_column_double( m_stmt, nCol ) );
}
return true;
case SQLITE_BLOB:
{
int len = sqlite3_column_bytes( m_stmt, nCol );
MemBuf* mb = new MemBuf_1( len );
memcpy( mb->data(), (byte*) sqlite3_column_blob( m_stmt, nCol ), len );
value = mb;
}
return true;
case SQLITE_TEXT:
{
CoreString* cs = new CoreString;
cs->fromUTF8( (const char*) sqlite3_column_text( m_stmt, nCol ) );
value = cs;
}
return true;
}
return false;
}
/******************************************************************************
* DB Statement class
*****************************************************************************/
DBIStatementSQLite3::DBIStatementSQLite3( DBIHandleSQLite3 *dbh, sqlite3_stmt* stmt ):
DBIStatement( dbh ),
m_pStmt( new SQLite3StatementHandler( stmt ) ),
m_statement( stmt ),
m_inBind( stmt ),
m_bFirst( false )
{
m_pDbh = dbh->getConn();
m_pDbh->incref();
}
DBIStatementSQLite3::DBIStatementSQLite3( DBIHandleSQLite3 *dbh, SQLite3StatementHandler* pStmt ):
DBIStatement( dbh ),
m_pStmt( pStmt ),
m_statement( pStmt->handle() ),
m_inBind( pStmt->handle() ),
m_bFirst( false )
{
pStmt->incref();
m_pDbh = dbh->getConn();
m_pDbh->incref();
}
DBIStatementSQLite3::~DBIStatementSQLite3()
{
close();
}
DBIRecordset* DBIStatementSQLite3::execute( ItemArray* params )
{
if( m_statement == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_STMT, __LINE__ ) );
// clear previous recordset
int res;
if ( m_bFirst )
{
m_bFirst = false;
}
else
{
res = sqlite3_reset( m_statement );
if( res != SQLITE_OK )
{
DBIHandleSQLite3::throwError( FALCON_DBI_ERROR_EXEC, res );
}
}
if( params != 0 )
{
m_inBind.bind(*params);
}
else
{
m_inBind.unbind();
}
res = sqlite3_step( m_statement );
if( res != SQLITE_OK
&& res != SQLITE_DONE
&& res != SQLITE_ROW )
{
DBIHandleSQLite3::throwError( FALCON_DBI_ERROR_EXEC, res );
}
/*
if ( sqlite3_column_count( m_statement ) != 0 )
{
// we do have a recorset
return new DBIRecordsetSQLite3( static_cast<DBIHandleSQLite3*>( m_dbh ), m_pStmt );
}
*/
return 0;
}
void DBIStatementSQLite3::reset()
{
if( m_statement == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_STMT, __LINE__ ) );
int res = sqlite3_reset( m_statement );
if( res != SQLITE_OK )
{
DBIHandleSQLite3::throwError( FALCON_DBI_ERROR_RESET, res );
}
}
void DBIStatementSQLite3::close()
{
if( m_statement != 0 )
{
m_pDbh->decref();
m_pStmt->decref();
m_pStmt = 0;
m_statement = 0;
}
}
/******************************************************************************
* DB Handler class
*****************************************************************************/
DBIHandleSQLite3::DBIHandleSQLite3():
m_bInTrans(false)
{
m_conn = NULL;
}
DBIHandleSQLite3::DBIHandleSQLite3( sqlite3 *conn ):
m_bInTrans(false)
{
m_conn = conn;
m_connRef = new SQLite3Handler( m_conn );
sqlite3_extended_result_codes( conn, 1 );
}
DBIHandleSQLite3::~DBIHandleSQLite3()
{
close();
}
void DBIHandleSQLite3::options( const String& params )
{
if( m_settings.parse( params ) )
{
// To turn off the autocommit.
if( ! m_settings.m_bAutocommit )
{
begin();
}
}
else
{
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_OPTPARAMS, __LINE__ )
.extra( params ) );
}
}
const DBISettingParams* DBIHandleSQLite3::options() const
{
return &m_settings;
}
DBIRecordset *DBIHandleSQLite3::query( const String &sql, ItemArray* params )
{
sqlite3_stmt* pStmt = int_prepare( sql );
int res;
if( params != 0 )
{
Sqlite3InBind binds( pStmt );
binds.bind(*params);
res = sqlite3_step( pStmt );
}
else
{
res = sqlite3_step( pStmt );
}
if( res != SQLITE_OK
&& res != SQLITE_DONE
&& res != SQLITE_ROW )
{
throwError( FALCON_DBI_ERROR_QUERY, res );
}
// do we have a recordset?
int count = sqlite3_column_count( pStmt );
m_nLastAffected = sqlite3_changes( m_conn );
if( count == 0 )
{
sqlite3_finalize( pStmt );
return 0;
}
else
{
sqlite3_reset( pStmt );
// the bindings must stay with the recordset...
return new DBIRecordsetSQLite3( this, pStmt );
}
}
DBIStatement* DBIHandleSQLite3::prepare( const String &query )
{
sqlite3_stmt* pStmt = int_prepare( query );
return new DBIStatementSQLite3( this, pStmt );
}
sqlite3_stmt* DBIHandleSQLite3::int_prepare( const String &sql ) const
{
if( m_conn == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_DB, __LINE__ ) );
AutoCString zSql( sql );
sqlite3_stmt* pStmt = 0;
int res = sqlite3_prepare_v2( m_conn, zSql.c_str(), zSql.length(), &pStmt, 0 );
if( res != SQLITE_OK )
{
throwError( FALCON_DBI_ERROR_QUERY, res );
}
return pStmt;
}
void DBIHandleSQLite3::begin()
{
if( m_conn == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_DB, __LINE__ ) );
if( !m_bInTrans )
{
char* error;
int res = sqlite3_exec( m_conn, "BEGIN TRANSACTION", 0, 0, &error );
if( res != 0 )
throwError( FALCON_DBI_ERROR_TRANSACTION, res, error );
m_bInTrans = true;
}
}
void DBIHandleSQLite3::commit()
{
if( m_conn == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_DB, __LINE__ ) );
// Sqlite doesn't ignore COMMIT out of transaction.
// We do; so we must filter them
if( m_bInTrans )
{
char* error;
int res = sqlite3_exec( m_conn, "COMMIT", 0, 0, &error );
if( res != 0 )
throwError( FALCON_DBI_ERROR_TRANSACTION, res, error );
m_bInTrans = false;
}
}
void DBIHandleSQLite3::rollback()
{
if( m_conn == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_DB, __LINE__ ) );
// Sqlite doesn't ignore COMMIT out of transaction.
// We do; so we must filter them
if( m_bInTrans )
{
char* error;
int res = sqlite3_exec( m_conn, "ROLLBACK", 0, 0, &error );
if( res != 0 )
throwError( FALCON_DBI_ERROR_TRANSACTION, res, error );
m_bInTrans = false;
}
}
void DBIHandleSQLite3::selectLimited( const String& query,
int64 nBegin, int64 nCount, String& result )
{
String sBegin, sCount;
if ( nBegin > 0 )
{
sBegin = " OFFSET ";
sBegin.N( nBegin );
}
if( nCount > 0 )
{
sCount.N( nCount );
}
result = "SELECT " + query;
if( nCount != 0 || nBegin != 0 )
{
result += " LIMIT " + sCount + sBegin;
}
}
void DBIHandleSQLite3::throwError( int falconError, int sql3Error, char* edesc )
{
String err = String("(").N(sql3Error).A(") ");
if( edesc == 0 )
err += errorDesc( sql3Error );
else
{
err.A(edesc);
err.bufferize();
sqlite3_free( edesc ); // got from sqlite3_malloc, must be freed
}
throw new DBIError( ErrorParam(falconError, __LINE__ )
.extra(err) );
}
String DBIHandleSQLite3::errorDesc( int error )
{
switch( error & 0xFF )
{
case SQLITE_OK : return "Successful result";
case SQLITE_ERROR : return "SQL error or missing database";
case SQLITE_INTERNAL : return "Internal logic error in SQLite";
case SQLITE_PERM : return "Access permission denied";
case SQLITE_ABORT : return "Callback routine requested an abort";
case SQLITE_BUSY : return "The database file is locked";
case SQLITE_LOCKED : return "A table in the database is locked";
case SQLITE_NOMEM : return "A malloc() failed";
case SQLITE_READONLY : return "Attempt to write a readonly database";
case SQLITE_INTERRUPT : return "Operation terminated by sqlite3_interrupt()";
case SQLITE_IOERR : return "Some kind of disk I/O error occurred";
case SQLITE_CORRUPT : return "The database disk image is malformed";
case SQLITE_NOTFOUND : return "NOT USED. Table or record not found";
case SQLITE_FULL : return "Insertion failed because database is full";
case SQLITE_CANTOPEN : return "Unable to open the database file";
case SQLITE_PROTOCOL : return "NOT USED. Database lock protocol error";
case SQLITE_EMPTY : return "Database is empty";
case SQLITE_SCHEMA : return "The database schema changed";
case SQLITE_TOOBIG : return "String or BLOB exceeds size limit";
case SQLITE_CONSTRAINT : return "Abort due to constraint violation";
case SQLITE_MISMATCH : return "Data type mismatch";
case SQLITE_MISUSE : return "Library used incorrectly";
case SQLITE_NOLFS : return "Uses OS features not supported on host";
case SQLITE_AUTH : return "Authorization denied";
case SQLITE_FORMAT : return "Auxiliary database format error";
case SQLITE_RANGE : return "2nd parameter to sqlite3_bind out of range";
case SQLITE_NOTADB : return "File opened that is not a database file";
case SQLITE_ROW : return "sqlite3_step() has another row ready";
case SQLITE_DONE : return "sqlite3_step() has finished executing";
}
return "Unknown error";
/*
case SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
case SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
case SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
case SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
case SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
case SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
case SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
case SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
case SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
case SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
case SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
case SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
case SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
case SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
case SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
case SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
case SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
case SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8) )
*/
}
int64 DBIHandleSQLite3::getLastInsertedId( const String& )
{
if( m_conn == 0 )
throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CLOSED_DB, __LINE__ ) );
return sqlite3_last_insert_rowid( m_conn );
}
void DBIHandleSQLite3::close()
{
if ( m_conn != NULL )
{
if( m_bInTrans )
{
sqlite3_exec( m_conn, "COMMIT", 0, 0, 0 );
m_bInTrans = false;
}
m_connRef->decref();
m_conn = NULL;
}
}
}
/* end of sqlite3_mod.cpp */
|