mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-08 21:10:02 +02:00
committed Data
This commit is contained in:
107
Data/SQLite/src/Binder.cpp
Normal file
107
Data/SQLite/src/Binder.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
//
|
||||
// Binder.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/SQLite/src/Binder.cpp#4 $
|
||||
//
|
||||
// Library: SQLite
|
||||
// Package: SQLite
|
||||
// Module: Binder
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Data/SQLite/Binder.h"
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/BLOB.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "sqlite3.h"
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace SQLite {
|
||||
|
||||
|
||||
Binder::Binder(sqlite3_stmt* pStmt):
|
||||
_pStmt(pStmt)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Binder::~Binder()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const Poco::Int32 &val)
|
||||
{
|
||||
int rc = sqlite3_bind_int(_pStmt, (int) pos, val);
|
||||
checkReturn(rc);
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const Poco::Int64 &val)
|
||||
{
|
||||
int rc = sqlite3_bind_int64(_pStmt, (int) pos, val);
|
||||
checkReturn(rc);
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const double &val)
|
||||
{
|
||||
int rc = sqlite3_bind_double(_pStmt, (int) pos, val);
|
||||
checkReturn(rc);
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const std::string& val)
|
||||
{
|
||||
int rc = sqlite3_bind_text(_pStmt, (int) pos, val.c_str(), (int) val.size()*sizeof(char), SQLITE_TRANSIENT);
|
||||
checkReturn(rc);
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const Poco::Data::BLOB& val)
|
||||
{
|
||||
// convert a blob to a an unsigned char* array
|
||||
const unsigned char* pData = reinterpret_cast<const unsigned char*>(val.rawContent());
|
||||
int valSize = static_cast<int>(val.size());
|
||||
|
||||
int rc = sqlite3_bind_blob(_pStmt, static_cast<int>(pos), pData, valSize, SQLITE_STATIC); // no deep copy, do not free memory
|
||||
checkReturn(rc);
|
||||
}
|
||||
|
||||
|
||||
void Binder::checkReturn(int rc)
|
||||
{
|
||||
if (rc != SQLITE_OK)
|
||||
Utility::throwException(rc);
|
||||
}
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
306
Data/SQLite/src/Extractor.cpp
Normal file
306
Data/SQLite/src/Extractor.cpp
Normal file
@@ -0,0 +1,306 @@
|
||||
//
|
||||
// Extractor.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/SQLite/src/Extractor.cpp#4 $
|
||||
//
|
||||
// Library: SQLite
|
||||
// Package: SQLite
|
||||
// Module: Extractor
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Data/SQLite/Extractor.h"
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/BLOB.h"
|
||||
#include "Poco/Data/DataException.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "sqlite3.h"
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace SQLite {
|
||||
|
||||
|
||||
Extractor::Extractor(sqlite3_stmt* pStmt):
|
||||
_pStmt(pStmt)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Extractor::~Extractor()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::Int32& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_int(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::Int64& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_int64(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, double& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_double(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, std::string& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
const char *pBuf = reinterpret_cast<const char*>(sqlite3_column_text(_pStmt, (int) pos));
|
||||
if (!pBuf)
|
||||
val.clear();
|
||||
else
|
||||
val = std::string(pBuf);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::Data::BLOB& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
int size = sqlite3_column_bytes(_pStmt, (int) pos);
|
||||
const char* pTmp = reinterpret_cast<const char*>(sqlite3_column_blob(_pStmt, (int) pos));
|
||||
val = Poco::Data::BLOB(pTmp, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::Int8& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_int(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::UInt8& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_int(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::Int16& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_int(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::UInt16& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_int(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::UInt32& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_int(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::UInt64& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = sqlite3_column_int64(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, bool& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = (0 != sqlite3_column_int(_pStmt, (int) pos));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, float& val)
|
||||
{
|
||||
if (isNull(pos))
|
||||
return false;
|
||||
val = static_cast<float>(sqlite3_column_double(_pStmt, (int) pos));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, char& val)
|
||||
{
|
||||
if (isNull(pos)) return false;
|
||||
val = sqlite3_column_int(_pStmt, (int) pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Poco::Any& val)
|
||||
{
|
||||
if (isNull(pos)) return false;
|
||||
|
||||
bool ret = false;
|
||||
|
||||
switch (Utility::getColumnType(_pStmt, pos))
|
||||
{
|
||||
case MetaColumn::FDT_BOOL:
|
||||
{
|
||||
bool i = false;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_INT8:
|
||||
{
|
||||
Poco::Int8 i = 0;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_UINT8:
|
||||
{
|
||||
Poco::UInt8 i = 0;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_INT16:
|
||||
{
|
||||
Poco::Int16 i = 0;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_UINT16:
|
||||
{
|
||||
Poco::UInt16 i = 0;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_INT32:
|
||||
{
|
||||
Poco::Int32 i = 0;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_UINT32:
|
||||
{
|
||||
Poco::UInt32 i = 0;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_INT64:
|
||||
{
|
||||
Poco::Int64 i = 0;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_UINT64:
|
||||
{
|
||||
Poco::UInt64 i = 0;
|
||||
ret = extract(pos, i);
|
||||
val = i;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_STRING:
|
||||
{
|
||||
std::string s;
|
||||
ret = extract(pos, s);
|
||||
val = s;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_DOUBLE:
|
||||
{
|
||||
double d(0.0);
|
||||
ret = extract(pos, d);
|
||||
val = d;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_FLOAT:
|
||||
{
|
||||
float f(0.0);
|
||||
ret = extract(pos, f);
|
||||
val = f;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_BLOB:
|
||||
{
|
||||
BLOB b;
|
||||
ret = extract(pos, b);
|
||||
val = b;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw Poco::Data::UnknownTypeException("Unknown type during extraction");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::isNull(std::size_t pos)
|
||||
{
|
||||
return sqlite3_column_type(_pStmt, (int) pos) == SQLITE_NULL;
|
||||
}
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
72
Data/SQLite/src/SQLiteException.cpp
Normal file
72
Data/SQLite/src/SQLiteException.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
//
|
||||
// SQLiteException.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/SQLite/src/SQLiteException.cpp#2 $
|
||||
//
|
||||
// Library: SQLite
|
||||
// Package: SQLite
|
||||
// Module: SQLiteException
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Data/SQLite/SQLiteException.h"
|
||||
#include <typeinfo>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
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(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(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(DataTypeMismatchException, SQLiteException, "Data type mismatch")
|
||||
POCO_IMPLEMENT_EXCEPTION(InvalidLibraryUseException, SQLiteException, "Library used incorrectly")
|
||||
POCO_IMPLEMENT_EXCEPTION(OSFeaturesMissingException, SQLiteException, "Uses OS features not supported on host")
|
||||
POCO_IMPLEMENT_EXCEPTION(AuthorizationDeniedException, SQLiteException, "Authorization denied")
|
||||
POCO_IMPLEMENT_EXCEPTION(TransactionException, SQLiteException, "Transaction exception")
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
233
Data/SQLite/src/SQLiteStatementImpl.cpp
Normal file
233
Data/SQLite/src/SQLiteStatementImpl.cpp
Normal file
@@ -0,0 +1,233 @@
|
||||
//
|
||||
// SQLiteStatementImpl.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/SQLite/src/SQLiteStatementImpl.cpp#4 $
|
||||
//
|
||||
// Library: SQLite
|
||||
// Package: SQLite
|
||||
// Module: SQLiteStatementImpl
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Data/SQLite/SQLiteStatementImpl.h"
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/SQLite/SQLiteException.h"
|
||||
#include "Poco/String.h"
|
||||
#include "sqlite3.h"
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace SQLite {
|
||||
|
||||
|
||||
SQLiteStatementImpl::SQLiteStatementImpl(sqlite3* pDB):
|
||||
_pDB(pDB),
|
||||
_pStmt(0),
|
||||
_stepCalled(false),
|
||||
_nextResponse(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SQLiteStatementImpl::~SQLiteStatementImpl()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
|
||||
void SQLiteStatementImpl::compileImpl()
|
||||
{
|
||||
if (_pStmt) return;
|
||||
|
||||
std::string statement(toString());
|
||||
if (statement.empty())
|
||||
throw InvalidSQLStatementException("Empty statements are illegal");
|
||||
|
||||
sqlite3_stmt* pStmt = 0;
|
||||
const char* pSql = statement.c_str(); // The SQL to be executed
|
||||
int rc = SQLITE_OK;
|
||||
const char* pLeftover = 0;
|
||||
bool queryFound = false;
|
||||
|
||||
while (rc == SQLITE_OK && !pStmt && !queryFound)
|
||||
{
|
||||
rc = sqlite3_prepare(_pDB, pSql, -1, &pStmt, &pLeftover);
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (pStmt)
|
||||
{
|
||||
sqlite3_finalize(pStmt);
|
||||
}
|
||||
pStmt = 0;
|
||||
std::string errMsg = sqlite3_errmsg(_pDB);
|
||||
Utility::throwException(rc, errMsg);
|
||||
}
|
||||
else if (rc == SQLITE_OK && pStmt)
|
||||
{
|
||||
queryFound = true;
|
||||
}
|
||||
else if(rc == SQLITE_OK && !pStmt) // comment/whitespace ignore
|
||||
{
|
||||
pSql = pLeftover;
|
||||
if (strlen(pSql) == 0)
|
||||
{
|
||||
// empty statement or an conditional statement! like CREATE IF NOT EXISTS
|
||||
// this is valid
|
||||
queryFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clear();
|
||||
_pStmt = pStmt;
|
||||
|
||||
// prepare binding
|
||||
_pBinder = new Binder(_pStmt);
|
||||
_pExtractor = new Extractor(_pStmt);
|
||||
|
||||
int colCount = sqlite3_column_count(_pStmt);
|
||||
|
||||
for (int i = 0; i < colCount; ++i)
|
||||
{
|
||||
MetaColumn mc(i + 1, sqlite3_column_name(_pStmt, i), Utility::getColumnType(_pStmt, i));
|
||||
_columns.push_back(mc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool SQLiteStatementImpl::canBind() const
|
||||
{
|
||||
bool ret = false;
|
||||
if (!bindings().empty() && _pStmt)
|
||||
ret = (*bindings().begin())->canBind();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void SQLiteStatementImpl::bindImpl()
|
||||
{
|
||||
_stepCalled = false;
|
||||
_nextResponse = 0;
|
||||
if (_pStmt == 0) return;
|
||||
|
||||
sqlite3_reset(_pStmt);
|
||||
|
||||
// bind
|
||||
Bindings& binds = bindings();
|
||||
if (binds.empty()) return;
|
||||
|
||||
std::size_t pos = 1; // sqlite starts with 1 not 0!
|
||||
|
||||
Bindings::iterator it = binds.begin();
|
||||
Bindings::iterator itEnd = binds.end();
|
||||
for (; it != itEnd && (*it)->canBind(); ++it)
|
||||
{
|
||||
(*it)->bind(pos);
|
||||
pos += (*it)->numOfColumnsHandled();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SQLiteStatementImpl::clear()
|
||||
{
|
||||
_columns.clear();
|
||||
|
||||
if (_pStmt)
|
||||
{
|
||||
sqlite3_finalize(_pStmt);
|
||||
_pStmt=0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool SQLiteStatementImpl::hasNext()
|
||||
{
|
||||
if (_stepCalled)
|
||||
return (_nextResponse == SQLITE_ROW);
|
||||
|
||||
// _pStmt is allowed to be null for conditional SQL statements
|
||||
if (_pStmt == 0)
|
||||
{
|
||||
_stepCalled = true;
|
||||
_nextResponse = SQLITE_DONE;
|
||||
return false;
|
||||
}
|
||||
|
||||
_stepCalled = true;
|
||||
_nextResponse = sqlite3_step(_pStmt);
|
||||
|
||||
return (_nextResponse == SQLITE_ROW);
|
||||
}
|
||||
|
||||
|
||||
void SQLiteStatementImpl::next()
|
||||
{
|
||||
if (SQLITE_ROW == _nextResponse)
|
||||
{
|
||||
poco_assert (columnsReturned() == sqlite3_column_count(_pStmt));
|
||||
|
||||
Extractions& extracts = extractions();
|
||||
Extractions::iterator it = extracts.begin();
|
||||
Extractions::iterator itEnd = extracts.end();
|
||||
std::size_t pos = 0; // sqlite starts with pos 0 for results!
|
||||
for (; it != itEnd; ++it)
|
||||
{
|
||||
(*it)->extract(pos);
|
||||
pos += (*it)->numOfColumnsHandled();
|
||||
}
|
||||
_stepCalled = false;
|
||||
}
|
||||
else if (SQLITE_DONE == _nextResponse)
|
||||
{
|
||||
throw Poco::Data::DataException("No data received");
|
||||
}
|
||||
else
|
||||
{
|
||||
int rc = _nextResponse;
|
||||
Utility::throwException(rc, std::string("Iterator Error: trying to access the next value"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Poco::UInt32 SQLiteStatementImpl::columnsReturned() const
|
||||
{
|
||||
return (Poco::UInt32) _columns.size();
|
||||
}
|
||||
|
||||
|
||||
const MetaColumn& SQLiteStatementImpl::metaColumn(Poco::UInt32 pos) const
|
||||
{
|
||||
poco_assert (pos > 0 && pos <= _columns.size());
|
||||
return _columns[pos-1];
|
||||
}
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
132
Data/SQLite/src/SessionImpl.cpp
Normal file
132
Data/SQLite/src/SessionImpl.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
//
|
||||
// SessionImpl.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/SQLite/src/SessionImpl.cpp#4 $
|
||||
//
|
||||
// Library: SQLite
|
||||
// Package: SQLite
|
||||
// Module: SessionImpl
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Data/SQLite/SessionImpl.h"
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/SQLite/SQLiteStatementImpl.h"
|
||||
#include "sqlite3.h"
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace SQLite {
|
||||
|
||||
|
||||
const std::string SessionImpl::DEFERRED_BEGIN_TRANSACTION("BEGIN DEFERRED");
|
||||
const std::string SessionImpl::COMMIT_TRANSACTION("COMMIT");
|
||||
const std::string SessionImpl::ABORT_TRANSACTION("ROLLBACK");
|
||||
|
||||
|
||||
SessionImpl::SessionImpl(const std::string& fileName):
|
||||
_dbFileName(fileName),
|
||||
_pDB(0),
|
||||
_connected(false)
|
||||
{
|
||||
open();
|
||||
}
|
||||
|
||||
|
||||
SessionImpl::~SessionImpl()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
Poco::Data::StatementImpl* SessionImpl::createStatementImpl()
|
||||
{
|
||||
poco_check_ptr (_pDB);
|
||||
return new SQLiteStatementImpl(_pDB);
|
||||
}
|
||||
|
||||
|
||||
void SessionImpl::begin()
|
||||
{
|
||||
SQLiteStatementImpl tmp(_pDB);
|
||||
tmp.add(DEFERRED_BEGIN_TRANSACTION);
|
||||
tmp.execute();
|
||||
}
|
||||
|
||||
|
||||
void SessionImpl::commit()
|
||||
{
|
||||
SQLiteStatementImpl tmp(_pDB);
|
||||
tmp.add(COMMIT_TRANSACTION);
|
||||
tmp.execute();
|
||||
}
|
||||
|
||||
|
||||
void SessionImpl::rollback()
|
||||
{
|
||||
SQLiteStatementImpl tmp(_pDB);
|
||||
tmp.add(ABORT_TRANSACTION);
|
||||
tmp.execute();
|
||||
}
|
||||
|
||||
|
||||
void SessionImpl::open()
|
||||
{
|
||||
int rc = sqlite3_open(_dbFileName.c_str(), &_pDB);
|
||||
|
||||
if (rc != 0)
|
||||
{
|
||||
close();
|
||||
Utility::throwException(rc);
|
||||
}
|
||||
|
||||
_connected = true;
|
||||
}
|
||||
|
||||
|
||||
void SessionImpl::close()
|
||||
{
|
||||
if (_pDB)
|
||||
{
|
||||
sqlite3_close(_pDB);
|
||||
_pDB = 0;
|
||||
}
|
||||
|
||||
_connected = false;
|
||||
}
|
||||
|
||||
|
||||
bool SessionImpl::isConnected()
|
||||
{
|
||||
return _connected;
|
||||
}
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
78
Data/SQLite/src/SessionInstantiator.cpp
Normal file
78
Data/SQLite/src/SessionInstantiator.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// SessionInstantiator.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/SQLite/src/SessionInstantiator.cpp#2 $
|
||||
//
|
||||
// Library: SQLite
|
||||
// Package: SQLite
|
||||
// Module: SessionInstantiator
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Data/SQLite/SessionInstantiator.h"
|
||||
#include "Poco/Data/SQLite/SessionImpl.h"
|
||||
#include "Poco/Data/SessionFactory.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace SQLite {
|
||||
|
||||
|
||||
const std::string SessionInstantiator::KEY("sqlite");
|
||||
|
||||
|
||||
SessionInstantiator::SessionInstantiator()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SessionInstantiator::~SessionInstantiator()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Poco::AutoPtr<Poco::Data::SessionImpl> SessionInstantiator::create(const std::string& initString)
|
||||
{
|
||||
return Poco::AutoPtr<Poco::Data::SessionImpl>(new SessionImpl(initString));
|
||||
}
|
||||
|
||||
|
||||
void SessionInstantiator::addToFactory()
|
||||
{
|
||||
Poco::Data::SessionFactory::instance().add(KEY, new SessionInstantiator());
|
||||
}
|
||||
|
||||
|
||||
void SessionInstantiator::removeFromFactory()
|
||||
{
|
||||
Poco::Data::SessionFactory::instance().remove(KEY);
|
||||
}
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
152
Data/SQLite/src/Utility.cpp
Normal file
152
Data/SQLite/src/Utility.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
//
|
||||
// Utility.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/SQLite/src/Utility.cpp#4 $
|
||||
//
|
||||
// Library: SQLite
|
||||
// Package: SQLite
|
||||
// Module: Utility
|
||||
//
|
||||
// Implementation of Utility
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/SQLite/SQLiteException.h"
|
||||
#include "Poco/NumberFormatter.h"
|
||||
#include "Poco/String.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "sqlite3.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace SQLite {
|
||||
|
||||
|
||||
std::string Utility::lastError(sqlite3 *pDB)
|
||||
{
|
||||
return std::string(sqlite3_errmsg(pDB));
|
||||
}
|
||||
|
||||
|
||||
MetaColumn::ColumnDataType Utility::getColumnType(sqlite3_stmt* pStmt, std::size_t pos)
|
||||
{
|
||||
poco_assert_dbg (pStmt);
|
||||
|
||||
const char* pc = sqlite3_column_decltype(pStmt, (int)pos);
|
||||
std::string sqliteType = pc ? pc : "";
|
||||
Poco::toUpperInPlace(sqliteType);
|
||||
|
||||
if (sqliteType.npos != sqliteType.find("INT"))
|
||||
return MetaColumn::FDT_INT32;
|
||||
else if (sqliteType.empty() ||
|
||||
sqliteType.npos != sqliteType.find("CHAR") ||
|
||||
sqliteType.npos != sqliteType.find("CLOB") ||
|
||||
sqliteType.npos != sqliteType.find("TEXT"))
|
||||
return MetaColumn::FDT_STRING;
|
||||
else if (sqliteType.npos != sqliteType.find("REAL") ||
|
||||
sqliteType.npos != sqliteType.find("FLOA") ||
|
||||
sqliteType.npos != sqliteType.find("DOUB"))
|
||||
return MetaColumn::FDT_DOUBLE;
|
||||
else if (sqliteType.npos != sqliteType.find("BLOB"))
|
||||
return MetaColumn::FDT_BLOB;
|
||||
|
||||
throw Poco::NotFoundException();
|
||||
}
|
||||
|
||||
|
||||
void Utility::throwException(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);
|
||||
case SQLITE_INTERNAL:
|
||||
throw InternalDBErrorException(std::string("An internal logic error in SQLite"), addErrMsg);
|
||||
case SQLITE_PERM:
|
||||
throw DBAccessDeniedException(std::string("Access permission denied"), addErrMsg);
|
||||
case SQLITE_ABORT:
|
||||
throw ExecutionAbortedException(std::string("Callback routine requested an abort"), addErrMsg);
|
||||
case SQLITE_BUSY:
|
||||
throw DBLockedException(std::string("The database file is locked"), addErrMsg);
|
||||
case SQLITE_LOCKED:
|
||||
throw TableLockedException(std::string("A table in the database is locked"), addErrMsg);
|
||||
case SQLITE_NOMEM:
|
||||
throw NoMemoryException(std::string("A malloc() failed"), addErrMsg);
|
||||
case SQLITE_READONLY:
|
||||
throw ReadOnlyException(std::string("Attempt to write a readonly database"), addErrMsg);
|
||||
case SQLITE_INTERRUPT:
|
||||
throw InterruptException(std::string("Operation terminated by sqlite_interrupt()"), addErrMsg);
|
||||
case SQLITE_IOERR:
|
||||
throw IOErrorException(std::string("Some kind of disk I/O error occurred"), addErrMsg);
|
||||
case SQLITE_CORRUPT:
|
||||
throw CorruptImageException(std::string("The database disk image is malformed"), addErrMsg);
|
||||
case SQLITE_NOTFOUND:
|
||||
throw TableNotFoundException(std::string("Table or record not found"), addErrMsg);
|
||||
case SQLITE_FULL:
|
||||
throw DatabaseFullException(std::string("Insertion failed because database is full"), addErrMsg);
|
||||
case SQLITE_CANTOPEN:
|
||||
throw CantOpenDBFileException(std::string("Unable to open the database file"), addErrMsg);
|
||||
case SQLITE_PROTOCOL:
|
||||
throw LockProtocolException(std::string("Database lock protocol error"), addErrMsg);
|
||||
case SQLITE_EMPTY:
|
||||
throw InternalDBErrorException(std::string("(Internal Only) Database table is empty"), addErrMsg);
|
||||
case SQLITE_SCHEMA:
|
||||
throw SchemaDiffersException(std::string("The database schema changed"), addErrMsg);
|
||||
case SQLITE_TOOBIG:
|
||||
throw RowTooBigException(std::string("Too much data for one row of a table"), addErrMsg);
|
||||
case SQLITE_CONSTRAINT:
|
||||
throw ConstraintViolationException(std::string("Abort due to constraint violation"), addErrMsg);
|
||||
case SQLITE_MISMATCH:
|
||||
throw DataTypeMismatchException(std::string("Data type mismatch"), addErrMsg);
|
||||
case SQLITE_MISUSE:
|
||||
throw InvalidLibraryUseException(std::string("Library used incorrectly"), addErrMsg);
|
||||
case SQLITE_NOLFS:
|
||||
throw OSFeaturesMissingException(std::string("Uses OS features not supported on host"), addErrMsg);
|
||||
case SQLITE_AUTH:
|
||||
throw AuthorizationDeniedException(std::string("Authorization denied"), addErrMsg);
|
||||
case SQLITE_FORMAT:
|
||||
throw CorruptImageException(std::string("Auxiliary database format error"), addErrMsg);
|
||||
case SQLITE_NOTADB:
|
||||
throw CorruptImageException(std::string("File opened that is not a database file"), addErrMsg);
|
||||
case SQLITE_RANGE:
|
||||
throw InvalidSQLStatementException(std::string("Bind Parameter out of range (Access of invalid position 0? bind starts with 1!)"), 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("Unkown error code: ") + Poco::NumberFormatter::format(rc), addErrMsg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} } } // namespace Poco::Data::SQLite
|
64901
Data/SQLite/src/sqlite3.c
Normal file
64901
Data/SQLite/src/sqlite3.c
Normal file
File diff suppressed because it is too large
Load Diff
1885
Data/SQLite/src/sqlite3.h
Normal file
1885
Data/SQLite/src/sqlite3.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user