Merge pull request #2021 from pocoproject/fix/sqlite-exception

Fix/sqlite exception
This commit is contained in:
Günter Obiltschnig 2017-12-18 10:24:05 +01:00 committed by GitHub
commit 9b0a3c2d68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 109 additions and 77 deletions

View File

@ -30,6 +30,7 @@ extern "C"
{
typedef struct sqlite3 sqlite3;
typedef struct sqlite3_stmt sqlite3_stmt;
typedef struct sqlite3_mutex* _pMutex;
}
@ -57,13 +58,13 @@ public:
static sqlite3* dbHandle(const Session& session);
/// Returns native DB handle.
static std::string lastError(sqlite3* pDb);
static std::string lastError(sqlite3* pDB);
/// Retreives the last error code from sqlite and converts it to a string.
static std::string lastError(const Session& session);
/// Retreives the last error code from sqlite and converts it to a string.
static void throwException(int rc, const std::string& addErrMsg = std::string());
static void throwException(sqlite3* pDB, int rc, const std::string& addErrMsg = std::string());
/// Throws for an error code the appropriate exception
static MetaColumn::ColumnDataType getColumnType(sqlite3_stmt* pStmt, std::size_t pos);
@ -174,6 +175,17 @@ public:
return registerUpdateHandler(dbHandle(session), callbackFn, pParam);
}
class SQLiteMutex
{
public:
SQLiteMutex(sqlite3* pDB);
~SQLiteMutex();
private:
SQLiteMutex();
sqlite3_mutex* _pMutex;
};
private:
Utility();
/// Maps SQLite column declared types to Poco::Data types through

View File

@ -113,14 +113,14 @@ void Binder::bind(std::size_t pos, const DateTime& val, Direction dir)
void Binder::bind(std::size_t pos, const NullData&, Direction)
{
sqlite3_bind_null(_pStmt, pos);
sqlite3_bind_null(_pStmt, static_cast<int>(pos));
}
void Binder::checkReturn(int rc)
{
if (rc != SQLITE_OK)
Utility::throwException(rc);
Utility::throwException(sqlite3_db_handle(_pStmt), rc);
}

View File

@ -228,7 +228,7 @@ bool Extractor::isNull(std::size_t pos, std::size_t)
if (!_nulls[pos].first)
{
_nulls[pos].first = true;
_nulls[pos].second = (SQLITE_NULL == sqlite3_column_type(_pStmt, pos));
_nulls[pos].second = (SQLITE_NULL == sqlite3_column_type(_pStmt, static_cast<int>(pos)));
}
return _nulls[pos].second;

View File

@ -21,29 +21,29 @@ namespace Data {
namespace SQLite {
POCO_IMPLEMENT_EXCEPTION(SQLiteException, Poco::Data::DataException, "Generic SQLite error")
POCO_IMPLEMENT_EXCEPTION(InvalidSQLStatementException, SQLiteException, "SQL Statement invalid")
POCO_IMPLEMENT_EXCEPTION(InternalDBErrorException, SQLiteException, "Internal error")
POCO_IMPLEMENT_EXCEPTION(DBAccessDeniedException, SQLiteException, "Access permission denied")
POCO_IMPLEMENT_EXCEPTION(ExecutionAbortedException, SQLiteException, "Execution of SQL statement aborted")
POCO_IMPLEMENT_EXCEPTION(DBLockedException, SQLiteException, "The database is locked")
POCO_IMPLEMENT_EXCEPTION(TableLockedException, SQLiteException, "A table in the database is locked")
POCO_IMPLEMENT_EXCEPTION(SQLiteException, Poco::Data::DataException, "SQLite exception")
POCO_IMPLEMENT_EXCEPTION(InvalidSQLStatementException, SQLiteException, "Invalid SQL statement")
POCO_IMPLEMENT_EXCEPTION(InternalDBErrorException, SQLiteException, "Internal DB error")
POCO_IMPLEMENT_EXCEPTION(DBAccessDeniedException, SQLiteException, "DB access denied")
POCO_IMPLEMENT_EXCEPTION(ExecutionAbortedException, SQLiteException, "Execution aborted")
POCO_IMPLEMENT_EXCEPTION(DBLockedException, SQLiteException, "DB locked")
POCO_IMPLEMENT_EXCEPTION(TableLockedException, SQLiteException, "Table locked")
POCO_IMPLEMENT_EXCEPTION(NoMemoryException, SQLiteException, "Out of Memory")
POCO_IMPLEMENT_EXCEPTION(ReadOnlyException, SQLiteException, "Attempt to write a readonly database")
POCO_IMPLEMENT_EXCEPTION(InterruptException, SQLiteException, "Operation terminated by an interrupt")
POCO_IMPLEMENT_EXCEPTION(IOErrorException, SQLiteException, "Some kind of disk I/O error occurred")
POCO_IMPLEMENT_EXCEPTION(CorruptImageException, SQLiteException, "The database disk image is malformed")
POCO_IMPLEMENT_EXCEPTION(ReadOnlyException, SQLiteException, "Read only")
POCO_IMPLEMENT_EXCEPTION(InterruptException, SQLiteException, "Interrupt")
POCO_IMPLEMENT_EXCEPTION(IOErrorException, SQLiteException, "I/O error")
POCO_IMPLEMENT_EXCEPTION(CorruptImageException, SQLiteException, "Corrupt image")
POCO_IMPLEMENT_EXCEPTION(TableNotFoundException, SQLiteException, "Table not found")
POCO_IMPLEMENT_EXCEPTION(DatabaseFullException, SQLiteException, "Insertion failed because database is full")
POCO_IMPLEMENT_EXCEPTION(CantOpenDBFileException, SQLiteException, "Unable to open the database file")
POCO_IMPLEMENT_EXCEPTION(LockProtocolException, SQLiteException, "Database lock protocol error")
POCO_IMPLEMENT_EXCEPTION(SchemaDiffersException, SQLiteException, "The database schema changed")
POCO_IMPLEMENT_EXCEPTION(RowTooBigException, SQLiteException, "Too much data for one row of a table")
POCO_IMPLEMENT_EXCEPTION(ConstraintViolationException, SQLiteException, "Abort due to constraint violation")
POCO_IMPLEMENT_EXCEPTION(DatabaseFullException, SQLiteException, "Database full")
POCO_IMPLEMENT_EXCEPTION(CantOpenDBFileException, SQLiteException, "Can't open DB file")
POCO_IMPLEMENT_EXCEPTION(LockProtocolException, SQLiteException, "Lock protocol")
POCO_IMPLEMENT_EXCEPTION(SchemaDiffersException, SQLiteException, "Schema differs")
POCO_IMPLEMENT_EXCEPTION(RowTooBigException, SQLiteException, "Row too big")
POCO_IMPLEMENT_EXCEPTION(ConstraintViolationException, SQLiteException, "Constraint violation")
POCO_IMPLEMENT_EXCEPTION(DataTypeMismatchException, SQLiteException, "Data type mismatch")
POCO_IMPLEMENT_EXCEPTION(ParameterCountMismatchException, SQLiteException, "Parameter count mismatch")
POCO_IMPLEMENT_EXCEPTION(InvalidLibraryUseException, SQLiteException, "Library used incorrectly")
POCO_IMPLEMENT_EXCEPTION(OSFeaturesMissingException, SQLiteException, "Uses OS features not supported on host")
POCO_IMPLEMENT_EXCEPTION(InvalidLibraryUseException, SQLiteException, "Invalid library use")
POCO_IMPLEMENT_EXCEPTION(OSFeaturesMissingException, SQLiteException, "OS features missing")
POCO_IMPLEMENT_EXCEPTION(AuthorizationDeniedException, SQLiteException, "Authorization denied")
POCO_IMPLEMENT_EXCEPTION(TransactionException, SQLiteException, "Transaction exception")

View File

@ -88,7 +88,7 @@ void SQLiteStatementImpl::compileImpl()
if (pStmt) sqlite3_finalize(pStmt);
pStmt = 0;
std::string errMsg = sqlite3_errmsg(_pDB);
Utility::throwException(rc, errMsg);
Utility::throwException(_pDB, rc, errMsg);
}
else if (rc == SQLITE_OK && pStmt)
{
@ -158,13 +158,13 @@ void SQLiteStatementImpl::bindImpl()
sqlite3_reset(_pStmt);
int paramCount = sqlite3_bind_parameter_count(_pStmt);
BindIt bindEnd = bindings().end();
if (0 == paramCount || bindEnd == _bindBegin)
if (0 == paramCount)
{
_canBind = false;
return;
}
BindIt bindEnd = bindings().end();
std::size_t availableCount = 0;
Bindings::difference_type bindCount = 0;
Bindings::iterator it = _bindBegin;
@ -175,6 +175,9 @@ void SQLiteStatementImpl::bindImpl()
else break;
}
if (availableCount < paramCount)
throw ParameterCountMismatchException();
/**/
Bindings::difference_type remainingBindCount = bindEnd - _bindBegin;
if (bindCount < remainingBindCount)
{
@ -183,7 +186,7 @@ void SQLiteStatementImpl::bindImpl()
}
else if (bindCount > remainingBindCount)
throw ParameterCountMismatchException();
/**/
std::size_t boundRowCount;
if (_bindBegin != bindings().end())
{
@ -245,7 +248,7 @@ bool SQLiteStatementImpl::hasNext()
_affectedRowCount += sqlite3_changes(_pDB);
if (_nextResponse != SQLITE_ROW && _nextResponse != SQLITE_OK && _nextResponse != SQLITE_DONE)
Utility::throwException(_nextResponse);
Utility::throwException(_pDB, _nextResponse);
_pExtractor->reset();//clear the cached null indicators
@ -273,7 +276,7 @@ std::size_t SQLiteStatementImpl::next()
if (_affectedRowCount == POCO_SQLITE_INV_ROW_CNT) _affectedRowCount = 0;
if (extracts.begin() != extracts.end())
{
_affectedRowCount += (*extracts.begin())->numOfRowsHandled();
_affectedRowCount += static_cast<int>((*extracts.begin())->numOfRowsHandled());
}
}
else if (SQLITE_DONE == _nextResponse)
@ -282,7 +285,7 @@ std::size_t SQLiteStatementImpl::next()
}
else
{
Utility::throwException(_nextResponse, std::string("Iterator Error: trying to access the next value"));
Utility::throwException(_pDB, _nextResponse, std::string("Iterator Error: trying to access the next value"));
}
return 1u;

View File

@ -182,14 +182,14 @@ void SessionImpl::open(const std::string& connect)
{
ActiveConnector connector(connectionString(), &_pDB);
ActiveResult<int> result = connector.connect();
if (!result.tryWait(getLoginTimeout() * 1000))
if (!result.tryWait(static_cast<long>(getLoginTimeout() * 1000)))
throw ConnectionFailedException("Timed out.");
int rc = result.data();
if (rc != 0)
{
close();
Utility::throwException(rc);
Utility::throwException(_pDB, rc);
}
}
catch (SQLiteException& ex)
@ -221,9 +221,9 @@ bool SessionImpl::isConnected()
void SessionImpl::setConnectionTimeout(std::size_t timeout)
{
int tout = 1000 * timeout;
int tout = static_cast<int>(1000 * timeout);
int rc = sqlite3_busy_timeout(_pDB, tout);
if (rc != 0) Utility::throwException(rc);
if (rc != 0) Utility::throwException(_pDB, rc);
_timeout = tout;
}

View File

@ -59,6 +59,18 @@ Utility::TypeMap Utility::_types;
Poco::Mutex Utility::_mutex;
Utility::SQLiteMutex::SQLiteMutex(sqlite3* pDB): _pMutex(sqlite3_db_mutex(pDB))
{
sqlite3_mutex_enter(_pMutex);
}
Utility::SQLiteMutex::~SQLiteMutex()
{
sqlite3_mutex_leave(_pMutex);
}
Utility::Utility()
{
if (_types.empty())
@ -125,7 +137,11 @@ Utility::Utility()
std::string Utility::lastError(sqlite3* pDB)
{
return std::string(sqlite3_errmsg(pDB));
std::string errStr;
SQLiteMutex m(pDB);
const char* pErr = sqlite3_errmsg(pDB);
if (pErr) errStr = pErr;
return errStr;
}
@ -151,74 +167,74 @@ MetaColumn::ColumnDataType Utility::getColumnType(sqlite3_stmt* pStmt, std::size
}
void Utility::throwException(int rc, const std::string& addErrMsg)
void Utility::throwException(sqlite3* pDB, int rc, const std::string& addErrMsg)
{
switch (rc)
{
case SQLITE_OK:
break;
case SQLITE_ERROR:
throw InvalidSQLStatementException(std::string("SQL error or missing database"), addErrMsg);
throw InvalidSQLStatementException(lastError(pDB), addErrMsg);
case SQLITE_INTERNAL:
throw InternalDBErrorException(std::string("An internal logic error in SQLite"), addErrMsg);
throw InternalDBErrorException(lastError(pDB), addErrMsg);
case SQLITE_PERM:
throw DBAccessDeniedException(std::string("Access permission denied"), addErrMsg);
throw DBAccessDeniedException(lastError(pDB), addErrMsg);
case SQLITE_ABORT:
throw ExecutionAbortedException(std::string("Callback routine requested an abort"), addErrMsg);
throw ExecutionAbortedException(lastError(pDB), addErrMsg);
case SQLITE_BUSY:
case SQLITE_BUSY_RECOVERY:
#if defined(SQLITE_BUSY_SNAPSHOT)
case SQLITE_BUSY_SNAPSHOT:
#endif
throw DBLockedException(std::string("The database file is locked"), addErrMsg);
throw DBLockedException(lastError(pDB), addErrMsg);
case SQLITE_LOCKED:
throw TableLockedException(std::string("A table in the database is locked"), addErrMsg);
throw TableLockedException(lastError(pDB), addErrMsg);
case SQLITE_NOMEM:
throw NoMemoryException(std::string("A malloc() failed"), addErrMsg);
throw NoMemoryException(lastError(pDB), addErrMsg);
case SQLITE_READONLY:
throw ReadOnlyException(std::string("Attempt to write a readonly database"), addErrMsg);
throw ReadOnlyException(lastError(pDB), addErrMsg);
case SQLITE_INTERRUPT:
throw InterruptException(std::string("Operation terminated by sqlite_interrupt()"), addErrMsg);
throw InterruptException(lastError(pDB), addErrMsg);
case SQLITE_IOERR:
throw IOErrorException(std::string("Some kind of disk I/O error occurred"), addErrMsg);
throw IOErrorException(lastError(pDB), addErrMsg);
case SQLITE_CORRUPT:
throw CorruptImageException(std::string("The database disk image is malformed"), addErrMsg);
throw CorruptImageException(lastError(pDB), addErrMsg);
case SQLITE_NOTFOUND:
throw TableNotFoundException(std::string("Table or record not found"), addErrMsg);
throw TableNotFoundException(lastError(pDB), addErrMsg);
case SQLITE_FULL:
throw DatabaseFullException(std::string("Insertion failed because database is full"), addErrMsg);
throw DatabaseFullException(lastError(pDB), addErrMsg);
case SQLITE_CANTOPEN:
throw CantOpenDBFileException(std::string("Unable to open the database file"), addErrMsg);
throw CantOpenDBFileException(lastError(pDB), addErrMsg);
case SQLITE_PROTOCOL:
throw LockProtocolException(std::string("Database lock protocol error"), addErrMsg);
throw LockProtocolException(lastError(pDB), addErrMsg);
case SQLITE_EMPTY:
throw InternalDBErrorException(std::string("(Internal Only) Database table is empty"), addErrMsg);
throw InternalDBErrorException(lastError(pDB), addErrMsg);
case SQLITE_SCHEMA:
throw SchemaDiffersException(std::string("The database schema changed"), addErrMsg);
throw SchemaDiffersException(lastError(pDB), addErrMsg);
case SQLITE_TOOBIG:
throw RowTooBigException(std::string("Too much data for one row of a table"), addErrMsg);
throw RowTooBigException(lastError(pDB), addErrMsg);
case SQLITE_CONSTRAINT:
throw ConstraintViolationException(std::string("Abort due to constraint violation"), addErrMsg);
throw ConstraintViolationException(lastError(pDB), addErrMsg);
case SQLITE_MISMATCH:
throw DataTypeMismatchException(std::string("Data type mismatch"), addErrMsg);
throw DataTypeMismatchException(lastError(pDB), addErrMsg);
case SQLITE_MISUSE:
throw InvalidLibraryUseException(std::string("Library used incorrectly"), addErrMsg);
throw InvalidLibraryUseException(lastError(pDB), addErrMsg);
case SQLITE_NOLFS:
throw OSFeaturesMissingException(std::string("Uses OS features not supported on host"), addErrMsg);
throw OSFeaturesMissingException(lastError(pDB), addErrMsg);
case SQLITE_AUTH:
throw AuthorizationDeniedException(std::string("Authorization denied"), addErrMsg);
throw AuthorizationDeniedException(lastError(pDB), addErrMsg);
case SQLITE_FORMAT:
throw CorruptImageException(std::string("Auxiliary database format error"), addErrMsg);
throw CorruptImageException(lastError(pDB), addErrMsg);
case SQLITE_NOTADB:
throw CorruptImageException(std::string("File opened that is not a database file"), addErrMsg);
throw CorruptImageException(lastError(pDB), addErrMsg);
case SQLITE_RANGE:
throw InvalidSQLStatementException(std::string("Bind Parameter out of range (Access of invalid position 0? bind starts with 1!)"), addErrMsg);
throw InvalidSQLStatementException(lastError(pDB), addErrMsg);
case SQLITE_ROW:
break; // sqlite_step() has another row ready
case SQLITE_DONE:
break; // sqlite_step() has finished executing
default:
throw SQLiteException(std::string("Unknown error code: ") + Poco::NumberFormatter::format(rc), addErrMsg);
throw SQLiteException(Poco::format("Unknown error code: %d", rc), addErrMsg);
}
}

View File

@ -427,7 +427,7 @@ void SQLiteTest::testNullCharPointer()
bind("firstname"),
bind("Address"), bind(pc), now;
fail ("must fail");
} catch (NullPointerException&) { }
} catch (NullPointerException&) { }
tmp << "SELECT COUNT(*) FROM PERSON", into(count), now;
assert (count == 1);
@ -466,7 +466,7 @@ void SQLiteTest::testInsertCharPointer()
bind("firstname"),
bind("Address"),
bind(133132));
std::free((void*) pc); pc = 0;
assert (1 == stmt.execute());
@ -2190,10 +2190,12 @@ void SQLiteTest::testAsync()
assert (stmt1.wait() == rowCount);
stmt1.execute();
try {
try
{
stmt1.execute();
fail ("must fail");
} catch (InvalidAccessException&)
}
catch (InvalidAccessException&)
{
stmt1.wait();
stmt1.execute();
@ -2206,10 +2208,12 @@ void SQLiteTest::testAsync()
assert (stmt.execute() == 0);
assert (stmt.isAsync());
try {
try
{
result = stmt.executeAsync();
fail ("must fail");
} catch (InvalidAccessException&)
}
catch (InvalidAccessException&)
{
stmt.wait();
result = stmt.executeAsync();
@ -2319,7 +2323,6 @@ void SQLiteTest::testPair()
std::string tableName("Simpsons");
std::pair<std::string, int> junior = std::make_pair("Junior", 12);
std::pair<std::string, int> senior = std::make_pair("Senior", 99);
int count = 0;
std::string result;
@ -2329,7 +2332,6 @@ void SQLiteTest::testPair()
tmp << "SELECT name FROM sqlite_master WHERE tbl_name=?", use(tableName), into(result), now;
assert (result == tableName);
// these are fine
tmp << "INSERT INTO Simpsons VALUES(?, ?)", use(junior), now;
tmp << "INSERT INTO Simpsons VALUES(?, ?)", useRef(senior), now;
@ -2343,7 +2345,6 @@ void SQLiteTest::testPair()
assert (ret[0].second == 99 || ret[1].second == 99);
assert (ret[0].first == "Junior" || ret[1].first == "Junior");
assert (ret[0].first == "Senior" || ret[1].first == "Senior");
}
@ -2500,12 +2501,12 @@ void SQLiteTest::testBindingCount()
tmp << "CREATE TABLE Ints (int0 INTEGER)", now;
int i = 42;
try { tmp << "INSERT INTO Ints VALUES (?)", now; }
try { tmp << "INSERT INTO Ints VALUES (?)", now; fail("must fail"); }
catch (ParameterCountMismatchException&) { }
tmp << "INSERT INTO Ints VALUES (?)", use(i), now;
i = 0;
try { tmp << "SELECT int0 from Ints where int0 = ?", into(i), now; }
try { tmp << "SELECT int0 from Ints where int0 = ?", into(i), now; fail("must fail"); }
catch (ParameterCountMismatchException&) { }
tmp << "SELECT int0 from Ints where int0 = ?", bind(42), into(i), now;
assert (42 == i);
@ -2513,8 +2514,8 @@ void SQLiteTest::testBindingCount()
tmp << "DROP TABLE IF EXISTS Ints", now;
tmp << "CREATE TABLE Ints (int0 INTEGER, int1 INTEGER, int2 INTEGER)", now;
try { tmp << "INSERT INTO Ints VALUES (?,?,?)", bind(42), bind(42), now; }
catch (ParameterCountMismatchException&) { }
try { tmp << "INSERT INTO Ints VALUES (?,?,?)", bind(42), bind(42), now; fail("must fail"); }
catch (ParameterCountMismatchException& ex) { }
}