initial commit

This commit is contained in:
2025-09-01 20:47:58 +08:00
commit 12c51b5ddb
843 changed files with 475321 additions and 0 deletions
@@ -0,0 +1,220 @@
//
// Binder.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: Binder
//
// Definition of the Binder class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Data_SQLite_Binder_INCLUDED
#define Data_SQLite_Binder_INCLUDED
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/AbstractBinder.h"
#include "Poco/Data/LOB.h"
#include "Poco/Any.h"
#include "Poco/DynamicAny.h"
#include "sqlite3.h"
namespace Poco {
namespace Data {
namespace SQLite {
class SQLite_API Binder: public Poco::Data::AbstractBinder
/// Binds placeholders in the sql query to the provided values. Performs data types mapping.
{
public:
Binder(sqlite3_stmt* pStmt);
/// Creates the Binder.
~Binder();
/// Destroys the Binder.
void bind(std::size_t pos, const Poco::Int8 &val, Direction dir);
/// Binds an Int8.
void bind(std::size_t pos, const Poco::UInt8 &val, Direction dir);
/// Binds an UInt8.
void bind(std::size_t pos, const Poco::Int16 &val, Direction dir);
/// Binds an Int16.
void bind(std::size_t pos, const Poco::UInt16 &val, Direction dir);
/// Binds an UInt16.
void bind(std::size_t pos, const Poco::Int32 &val, Direction dir);
/// Binds an Int32.
void bind(std::size_t pos, const Poco::UInt32 &val, Direction dir);
/// Binds an UInt32.
void bind(std::size_t pos, const Poco::Int64 &val, Direction dir);
/// Binds an Int64.
void bind(std::size_t pos, const Poco::UInt64 &val, Direction dir);
/// Binds an UInt64.
#ifndef POCO_LONG_IS_64_BIT
void bind(std::size_t pos, const long &val, Direction dir);
/// Binds a long
void bind(std::size_t pos, const unsigned long &val, Direction dir);
/// Binds an unsigned long
#endif
void bind(std::size_t pos, const bool &val, Direction dir);
/// Binds a boolean.
void bind(std::size_t pos, const float &val, Direction dir);
/// Binds a float.
void bind(std::size_t pos, const double &val, Direction dir);
/// Binds a double.
void bind(std::size_t pos, const char &val, Direction dir);
/// Binds a single character.
void bind(std::size_t pos, const char* const &pVal, Direction dir);
/// Binds a const char ptr.
void bind(std::size_t pos, const std::string& val, Direction dir);
/// Binds a string.
void bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir);
/// Binds a BLOB.
void bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir);
/// Binds a CLOB.
void bind(std::size_t pos, const Date& val, Direction dir);
/// Binds a Date.
void bind(std::size_t pos, const Time& val, Direction dir);
/// Binds a Time.
void bind(std::size_t pos, const DateTime& val, Direction dir);
/// Binds a DateTime.
void bind(std::size_t pos, const NullData& val, Direction dir);
/// Binds a null.
private:
void checkReturn(int rc);
/// Checks the SQLite return code and throws an appropriate exception
/// if error has occurred.
template <typename T>
void bindLOB(std::size_t pos, const Poco::Data::LOB<T>& val, Direction dir)
{
// convert a blob to a an unsigned char* array
const T* pData = reinterpret_cast<const T*>(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);
}
sqlite3_stmt* _pStmt;
};
//
// inlines
//
inline void Binder::bind(std::size_t pos, const Poco::Int8 &val, Direction dir)
{
Poco::Int32 tmp = val;
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const Poco::UInt8 &val, Direction dir)
{
Poco::Int32 tmp = val;
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const Poco::Int16 &val, Direction dir)
{
Poco::Int32 tmp = val;
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const Poco::UInt16 &val, Direction dir)
{
Poco::Int32 tmp = val;
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const Poco::UInt32 &val, Direction dir)
{
Poco::Int32 tmp = static_cast<Poco::Int32>(val);
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const Poco::UInt64 &val, Direction dir)
{
Poco::Int64 tmp = static_cast<Poco::Int64>(val);
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const bool &val, Direction dir)
{
Poco::Int32 tmp = (val ? 1 : 0);
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const float &val, Direction dir)
{
double tmp = val;
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const char &val, Direction dir)
{
Poco::Int32 tmp = val;
bind(pos, tmp, dir);
}
inline void Binder::bind(std::size_t pos, const char* const &pVal, Direction dir)
{
std::string val(pVal);
bind(pos, val, dir);
}
inline void Binder::bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir)
{
bindLOB<Poco::Data::BLOB::ValueType>(pos, val, dir);
}
inline void Binder::bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir)
{
bindLOB<Poco::Data::CLOB::ValueType>(pos, val, dir);
}
} } } // namespace Poco::Data::SQLite
#endif // Data_SQLite_Binder_INCLUDED
@@ -0,0 +1,79 @@
//
// Connector.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: Connector
//
// Definition of the Connector class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Data_SQLite_Connector_INCLUDED
#define Data_SQLite_Connector_INCLUDED
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/Connector.h"
namespace Poco {
namespace Data {
namespace SQLite {
class SQLite_API Connector: public Poco::Data::Connector
/// Connector instantiates SqLite SessionImpl objects.
{
public:
static const std::string KEY;
/// Keyword for creating SQLite sessions ("sqlite").
Connector();
/// Creates the Connector.
~Connector();
/// Destroys the Connector.
const std::string& name() const;
/// Returns the name associated with this connector.
Poco::AutoPtr<Poco::Data::SessionImpl> createSession(const std::string& connectionString,
std::size_t timeout = Poco::Data::SessionImpl::LOGIN_TIMEOUT_DEFAULT);
/// Creates a SQLite SessionImpl object and initializes it with the given connectionString.
static void registerConnector();
/// Registers the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory.
static void unregisterConnector();
/// Unregisters the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory.
static void enableSharedCache(bool flag = true);
/// Enables or disables SQlite shared cache mode
/// (see http://www.sqlite.org/sharedcache.html for a discussion).
static void enableSoftHeapLimit(int limit);
/// Sets a soft upper limit to the amount of memory allocated
/// by SQLite. For more information, please see the SQLite
/// sqlite_soft_heap_limit() function (http://www.sqlite.org/c3ref/soft_heap_limit.html).
};
///
/// inlines
///
inline const std::string& Connector::name() const
{
return KEY;
}
} } } // namespace Poco::Data::SQLite
#endif // Data_SQLite_Connector_INCLUDED
@@ -0,0 +1,313 @@
//
// Extractor.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: Extractor
//
// Definition of the Extractor class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Data_SQLite_Extractor_INCLUDED
#define Data_SQLite_Extractor_INCLUDED
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/SQLite/Utility.h"
#include "Poco/Data/AbstractExtractor.h"
#include "Poco/Data/MetaColumn.h"
#include "Poco/Data/DataException.h"
#include "Poco/Data/Constants.h"
#include "Poco/Data/Date.h"
#include "Poco/Data/Time.h"
#include "Poco/Data/Date.h"
#include "Poco/Data/Time.h"
#include "Poco/Any.h"
#include "Poco/DynamicAny.h"
#include "sqlite3.h"
#include <vector>
#include <utility>
namespace Poco {
namespace Data {
namespace SQLite {
class SQLite_API Extractor: public Poco::Data::AbstractExtractor
/// Extracts and converts data values form the result row returned by SQLite.
/// If NULL is received, the incoming val value is not changed and false is returned
{
public:
typedef std::vector<std::pair<bool, bool> > NullIndVec;
/// Type for null indicators container.
Extractor(sqlite3_stmt* pStmt);
/// Creates the Extractor.
~Extractor();
/// Destroys the Extractor.
bool extract(std::size_t pos, Poco::Int8& val);
/// Extracts an Int8.
bool extract(std::size_t pos, Poco::UInt8& val);
/// Extracts an UInt8.
bool extract(std::size_t pos, Poco::Int16& val);
/// Extracts an Int16.
bool extract(std::size_t pos, Poco::UInt16& val);
/// Extracts an UInt16.
bool extract(std::size_t pos, Poco::Int32& val);
/// Extracts an Int32.
bool extract(std::size_t pos, Poco::UInt32& val);
/// Extracts an UInt32.
bool extract(std::size_t pos, Poco::Int64& val);
/// Extracts an Int64.
bool extract(std::size_t pos, Poco::UInt64& val);
/// Extracts an UInt64.
#ifndef POCO_LONG_IS_64_BIT
bool extract(std::size_t pos, long& val);
/// Extracts a long.
bool extract(std::size_t pos, unsigned long& val);
/// Extracts an unsigned long.
#endif
bool extract(std::size_t pos, bool& val);
/// Extracts a boolean.
bool extract(std::size_t pos, float& val);
/// Extracts a float.
bool extract(std::size_t pos, double& val);
/// Extracts a double.
bool extract(std::size_t pos, char& val);
/// Extracts a single character.
bool extract(std::size_t pos, std::string& val);
/// Extracts a string.
bool extract(std::size_t pos, Poco::Data::BLOB& val);
/// Extracts a BLOB.
bool extract(std::size_t pos, Poco::Data::CLOB& val);
/// Extracts a CLOB.
bool extract(std::size_t pos, Poco::Data::Date& val);
/// Extracts a Date.
bool extract(std::size_t pos, Poco::Data::Time& val);
/// Extracts a Time.
bool extract(std::size_t pos, Poco::DateTime& val);
/// Extracts a DateTime.
bool extract(std::size_t pos, Poco::Any& val);
/// Extracts an Any.
bool extract(std::size_t pos, Poco::DynamicAny& val);
/// Extracts a DynamicAny.
bool isNull(std::size_t pos, std::size_t row = POCO_DATA_INVALID_ROW);
/// Returns true if the current row value at pos column is null.
/// Because of the loss of information about null-ness of the
/// underlying database values due to the nature of SQLite engine,
/// (once null value is converted to default value, SQLite API
/// treats it as non-null), a null indicator container member
/// variable is used to cache the indicators of the underlying nulls
/// thus rendering this function idempotent.
/// The container is a vector of [bool, bool] pairs.
/// The vector index corresponds to the column position, the first
/// bool value in the pair is true if the null indicator has
/// been set and the second bool value in the pair is true if
/// the column is actually null.
/// The row argument, needed for connectors with bulk capabilities,
/// is ignored in this implementation.
void reset();
/// Clears the cached nulls indicator vector.
private:
template <typename T>
bool extractImpl(std::size_t pos, T& val)
/// Utility function for extraction of Any and DynamicAny.
{
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;
}
case MetaColumn::FDT_DATE:
{
Date d;
ret = extract(pos, d);
val = d;
break;
}
case MetaColumn::FDT_TIME:
{
Time t;
ret = extract(pos, t);
val = t;
break;
}
case MetaColumn::FDT_TIMESTAMP:
{
DateTime dt;
ret = extract(pos, dt);
val = dt;
break;
}
default:
throw Poco::Data::UnknownTypeException("Unknown type during extraction");
}
return ret;
}
template <typename T>
bool extractLOB(std::size_t pos, Poco::Data::LOB<T>& val)
{
if (isNull(pos)) return false;
int size = sqlite3_column_bytes(_pStmt, (int) pos);
const T* pTmp = reinterpret_cast<const T*>(sqlite3_column_blob(_pStmt, (int) pos));
val = Poco::Data::LOB<T>(pTmp, size);
return true;
}
sqlite3_stmt* _pStmt;
NullIndVec _nulls;
};
///
/// inlines
///
inline void Extractor::reset()
{
_nulls.clear();
}
inline bool Extractor::extract(std::size_t pos, Poco::Data::BLOB& val)
{
return extractLOB<Poco::Data::BLOB::ValueType>(pos, val);
}
inline bool Extractor::extract(std::size_t pos, Poco::Data::CLOB& val)
{
return extractLOB<Poco::Data::CLOB::ValueType>(pos, val);
}
} } } // namespace Poco::Data::SQLite
#endif // Data_SQLite_Extractor_INCLUDED
@@ -0,0 +1,195 @@
//
// Notifier.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: Notifier
//
// Definition of Notifier.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQLite_Notifier_INCLUDED
#define SQLite_Notifier_INCLUDED
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/SQLite/Utility.h"
#include "Poco/Data/Session.h"
#include "Poco/Mutex.h"
#include "Poco/Types.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/BasicEvent.h"
#include <map>
namespace Poco {
namespace Data {
namespace SQLite {
class SQLite_API Notifier
/// Notifier is a wrapper for SQLite callback calls. It supports event callbacks
/// for insert, update, delete, commit and rollback events. While (un)registering
/// callbacks is thread-safe, execution of the callbacks themselves are not;
/// it is the user's responsibility to ensure the thread-safey of the functions
/// they provide as callback target. Additionally, commit callbacks may prevent
/// database transactions from succeeding (see sqliteCommitCallbackFn documentation
/// for details).
///
/// There can be only one set of callbacks per session (i.e. registering a new
/// callback automatically unregisters the previous one). All callbacks are
/// registered and enabled at Notifier contruction time and can be disabled
/// at a later point time.
{
public:
typedef unsigned char EnabledEventType;
/// A type definition for events-enabled bitmap.
typedef Poco::BasicEvent<void> Event;
//
// Events
//
Event update;
Event insert;
Event erase;
Event commit;
Event rollback;
// Event types.
static const EnabledEventType SQLITE_NOTIFY_UPDATE = 1;
static const EnabledEventType SQLITE_NOTIFY_COMMIT = 2;
static const EnabledEventType SQLITE_NOTIFY_ROLLBACK = 4;
Notifier(const Session& session,
EnabledEventType enabled = SQLITE_NOTIFY_UPDATE | SQLITE_NOTIFY_COMMIT | SQLITE_NOTIFY_ROLLBACK);
/// Creates a Notifier and enables all callbacks.
Notifier(const Session& session,
const Any& value,
EnabledEventType enabled = SQLITE_NOTIFY_UPDATE | SQLITE_NOTIFY_COMMIT | SQLITE_NOTIFY_ROLLBACK);
/// Creates a Notifier, assigns the value to the internal storage and and enables all callbacks.
~Notifier();
/// Disables all callbacks and destroys the Notifier.
bool enableUpdate();
/// Enables update callbacks.
bool disableUpdate();
/// Disables update callbacks.
bool updateEnabled() const;
/// Returns true if update callbacks are enabled, false otherwise.
bool enableCommit();
/// Enables commit callbacks.
bool disableCommit();
/// Disables commit callbacks.
bool commitEnabled() const;
/// Returns true if update callbacks are enabled, false otherwise.
bool enableRollback();
/// Enables rollback callbacks.
bool disableRollback();
/// Disables rollback callbacks.
bool rollbackEnabled() const;
/// Returns true if rollback callbacks are enabled, false otherwise.
bool enableAll();
/// Enables all callbacks.
bool disableAll();
/// Disables all callbacks.
static void sqliteUpdateCallbackFn(void* pVal, int opCode, const char* pDB, const char* pTable, Poco::Int64 row);
/// Update callback event dispatcher. Determines the type of the event, updates the row number
/// and triggers the event.
static int sqliteCommitCallbackFn(void* pVal);
/// Commit callback event dispatcher. If an exception occurs, it is catched inside this function,
/// non-zero value is returned, which causes SQLite engine to turn commit into a rollback.
/// Therefore, callers should check for return value - if it is zero, callback completed succesfuly
/// and transaction was committed.
static void sqliteRollbackCallbackFn(void* pVal);
/// Rollback callback event dispatcher.
bool operator == (const Notifier& other) const;
/// Equality operator. Compares value, row and database handles and
/// returns true iff all are equal.
Poco::Int64 getRow() const;
/// Returns the row number.
void setRow(Poco::Int64 row);
/// Sets the row number.
const Poco::Dynamic::Var& getValue() const;
/// Returns the value.
template <typename T>
inline void setValue(const T& val)
/// Sets the value.
{
_value = val;
}
private:
Notifier();
Notifier(const Notifier&);
Notifier& operator=(const Notifier&);
const Session& _session;
Poco::Dynamic::Var _value;
Poco::Int64 _row;
EnabledEventType _enabledEvents;
Poco::Mutex _mutex;
};
//
// inlines
//
inline bool Notifier::operator == (const Notifier& other) const
{
return _value == other._value &&
_row == other._row &&
Utility::dbHandle(_session) == Utility::dbHandle(other._session);
}
inline Poco::Int64 Notifier::getRow() const
{
return _row;
}
inline void Notifier::setRow(Poco::Int64 row)
/// Sets the row number.
{
_row = row;
}
inline const Poco::Dynamic::Var& Notifier::getValue() const
{
return _value;
}
} } } // namespace Poco::Data::SQLite
#endif // SQLite_Notifier_INCLUDED
@@ -0,0 +1,62 @@
//
// SQLite.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: SQLite
//
// Basic definitions for the Poco SQLite library.
// This file must be the first file included by every other SQLite
// header file.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQLite_SQLite_INCLUDED
#define SQLite_SQLite_INCLUDED
#include "Poco/Foundation.h"
//
// The following block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the SQLite_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// SQLite_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32) && defined(POCO_DLL)
#if defined(SQLite_EXPORTS)
#define SQLite_API __declspec(dllexport)
#else
#define SQLite_API __declspec(dllimport)
#endif
#endif
#if !defined(SQLite_API)
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
#define SQLite_API __attribute__ ((visibility ("default")))
#else
#define SQLite_API
#endif
#endif
//
// Automatically link SQLite library.
//
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(SQLite_EXPORTS)
#pragma comment(lib, "PocoDataSQLite" POCO_LIB_SUFFIX)
#endif
#endif
#endif // SQLite_SQLite_INCLUDED
@@ -0,0 +1,60 @@
//
// SQLiteException.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: SQLiteException
//
// Definition of SQLiteException.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQLite_SQLiteException_INCLUDED
#define SQLite_SQLiteException_INCLUDED
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/DataException.h"
namespace Poco {
namespace Data {
namespace SQLite {
POCO_DECLARE_EXCEPTION(SQLite_API, SQLiteException, Poco::Data::DataException)
POCO_DECLARE_EXCEPTION(SQLite_API, InvalidSQLStatementException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, InternalDBErrorException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, DBAccessDeniedException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, ExecutionAbortedException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, DBLockedException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, TableLockedException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, NoMemoryException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, ReadOnlyException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, InterruptException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, IOErrorException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, CorruptImageException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, TableNotFoundException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, DatabaseFullException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, CantOpenDBFileException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, LockProtocolException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, SchemaDiffersException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, RowTooBigException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, ConstraintViolationException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, DataTypeMismatchException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, ParameterCountMismatchException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, InvalidLibraryUseException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, OSFeaturesMissingException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, AuthorizationDeniedException, SQLiteException)
POCO_DECLARE_EXCEPTION(SQLite_API, TransactionException, SQLiteException)
} } } // namespace Poco::Data::SQLite
#endif
@@ -0,0 +1,155 @@
//
// SQLiteStatementImpl.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: SQLiteStatementImpl
//
// Definition of the SQLiteStatementImpl class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Data_SQLite_SQLiteStatementImpl_INCLUDED
#define Data_SQLite_SQLiteStatementImpl_INCLUDED
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/SQLite/Binder.h"
#include "Poco/Data/SQLite/Extractor.h"
#include "Poco/Data/StatementImpl.h"
#include "Poco/Data/MetaColumn.h"
#include "Poco/SharedPtr.h"
extern "C"
{
typedef struct sqlite3 sqlite3;
typedef struct sqlite3_stmt sqlite3_stmt;
}
namespace Poco {
namespace Data {
namespace SQLite {
class SQLite_API SQLiteStatementImpl: public Poco::Data::StatementImpl
/// Implements statement functionality needed for SQLite
{
public:
SQLiteStatementImpl(Poco::Data::SessionImpl& rSession, sqlite3* pDB);
/// Creates the SQLiteStatementImpl.
~SQLiteStatementImpl();
/// Destroys the SQLiteStatementImpl.
protected:
std::size_t columnsReturned() const;
/// Returns number of columns returned by query.
int affectedRowCount() const;
/// Returns the number of affected rows.
/// Used to find out the number of rows affected by insert, delete or update.
/// All changes are counted, even if they are later undone by a ROLLBACK or ABORT.
/// Changes associated with creating and dropping tables are not counted.
const MetaColumn& metaColumn(std::size_t pos) const;
/// Returns column meta data.
bool hasNext();
/// Returns true if a call to next() will return data.
std::size_t next();
/// Retrieves the next row from the resultset and returns 1.
/// Will throw, if the resultset is empty.
bool canBind() const;
/// Returns true if a valid statement is set and we can bind.
bool canCompile() const;
/// Returns true if statement can compile.
void compileImpl();
/// Compiles the statement, doesn't bind yet.
/// Returns true if the statement was succesfully compiled.
/// The way SQLite handles batches of statmeents is by compiling
/// one at a time and returning a pointer to the next one.
/// The remainder of the statement is kept in a string
/// buffer pointed to by _pLeftover member.
void bindImpl();
/// Binds parameters
AbstractExtraction::ExtractorPtr extractor();
/// Returns the concrete extractor used by the statement.
AbstractBinding::BinderPtr binder();
/// Returns the concrete binder used by the statement.
private:
void clear();
/// Removes the _pStmt
typedef Poco::SharedPtr<Binder> BinderPtr;
typedef Poco::SharedPtr<Extractor> ExtractorPtr;
typedef Poco::Data::AbstractBindingVec Bindings;
typedef Poco::Data::AbstractExtractionVec Extractions;
typedef std::vector<Poco::Data::MetaColumn> MetaColumnVec;
typedef std::vector<MetaColumnVec> MetaColumnVecVec;
typedef Poco::SharedPtr<std::string> StrPtr;
typedef Bindings::iterator BindIt;
sqlite3* _pDB;
sqlite3_stmt* _pStmt;
bool _stepCalled;
int _nextResponse;
BinderPtr _pBinder;
ExtractorPtr _pExtractor;
MetaColumnVecVec _columns;
int _affectedRowCount;
StrPtr _pLeftover;
BindIt _bindBegin;
bool _canBind;
bool _isExtracted;
bool _canCompile;
static const int POCO_SQLITE_INV_ROW_CNT;
};
//
// inlines
//
inline AbstractExtraction::ExtractorPtr SQLiteStatementImpl::extractor()
{
return _pExtractor;
}
inline AbstractBinding::BinderPtr SQLiteStatementImpl::binder()
{
return _pBinder;
}
inline bool SQLiteStatementImpl::canBind() const
{
return _canBind;
}
inline bool SQLiteStatementImpl::canCompile() const
{
return _canCompile;
}
} } } // namespace Poco::Data::SQLite
#endif // Data_SQLite_SQLiteStatementImpl_INCLUDED
@@ -0,0 +1,168 @@
//
// SessionImpl.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: SessionImpl
//
// Definition of the SessionImpl class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Data_SQLite_SessionImpl_INCLUDED
#define Data_SQLite_SessionImpl_INCLUDED
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/SQLite/Connector.h"
#include "Poco/Data/SQLite/Binder.h"
#include "Poco/Data/AbstractSessionImpl.h"
#include "Poco/SharedPtr.h"
#include "Poco/Mutex.h"
extern "C"
{
typedef struct sqlite3 sqlite3;
}
namespace Poco {
namespace Data {
namespace SQLite {
class SQLite_API SessionImpl: public Poco::Data::AbstractSessionImpl<SessionImpl>
/// Implements SessionImpl interface.
{
public:
SessionImpl(const std::string& fileName,
std::size_t loginTimeout = LOGIN_TIMEOUT_DEFAULT);
/// Creates the SessionImpl. Opens a connection to the database.
~SessionImpl();
/// Destroys the SessionImpl.
Poco::Data::StatementImpl* createStatementImpl();
/// Returns an SQLite StatementImpl.
void open(const std::string& connect = "");
/// Opens a connection to the Database.
///
/// An in-memory system database (sys), with a single table (dual)
/// containing single field (dummy) is attached to the database.
/// The in-memory system database is used to force change count
/// to be reset to zero on every new query (or batch of queries)
/// execution. Without this functionality, select statements
/// executions that do not return any rows return the count of
/// changes effected by the most recent insert, update or delete.
/// In-memory system database can be queried and updated but can not
/// be dropped. It may be used for other purposes
/// in the future.
void close();
/// Closes the session.
bool isConnected();
/// Returns true if connected, false otherwise.
void setConnectionTimeout(std::size_t timeout);
/// Sets the session connection timeout value.
/// Timeout value is in seconds.
std::size_t getConnectionTimeout();
/// Returns the session connection timeout value.
/// Timeout value is in seconds.
void begin();
/// Starts a transaction.
void commit();
/// Commits and ends a transaction.
void rollback();
/// Aborts a transaction.
bool canTransact();
/// Returns true if session has transaction capabilities.
bool isTransaction();
/// Returns true iff a transaction is a transaction is in progress, false otherwise.
void setTransactionIsolation(Poco::UInt32 ti);
/// Sets the transaction isolation level.
Poco::UInt32 getTransactionIsolation();
/// Returns the transaction isolation level.
bool hasTransactionIsolation(Poco::UInt32 ti);
/// Returns true iff the transaction isolation level corresponding
/// to the supplied bitmask is supported.
bool isTransactionIsolation(Poco::UInt32 ti);
/// Returns true iff the transaction isolation level corresponds
/// to the supplied bitmask.
void autoCommit(const std::string&, bool val);
/// Sets autocommit property for the session.
bool isAutoCommit(const std::string& name="");
/// Returns autocommit property value.
const std::string& connectorName() const;
/// Returns the name of the connector.
protected:
void setConnectionTimeout(const std::string& prop, const Poco::Any& value);
Poco::Any getConnectionTimeout(const std::string& prop);
private:
std::string _connector;
sqlite3* _pDB;
bool _connected;
bool _isTransaction;
int _timeout;
Poco::Mutex _mutex;
static const std::string DEFERRED_BEGIN_TRANSACTION;
static const std::string COMMIT_TRANSACTION;
static const std::string ABORT_TRANSACTION;
};
//
// inlines
//
inline bool SessionImpl::canTransact()
{
return true;
}
inline bool SessionImpl::isTransaction()
{
return _isTransaction;
}
inline const std::string& SessionImpl::connectorName() const
{
return _connector;
}
inline std::size_t SessionImpl::getConnectionTimeout()
{
return static_cast<std::size_t>(_timeout/1000);
}
} } } // namespace Poco::Data::SQLite
#endif // Data_SQLite_SessionImpl_INCLUDED
@@ -0,0 +1,240 @@
//
// Utility.h
//
// Library: Data/SQLite
// Package: SQLite
// Module: Utility
//
// Definition of Utility.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQLite_Utility_INCLUDED
#define SQLite_Utility_INCLUDED
#include "Poco/Data/SQLite/SQLite.h"
#include "Poco/Data/MetaColumn.h"
#include "Poco/Data/Session.h"
#include "Poco/Mutex.h"
#include "Poco/Types.h"
#include <map>
extern "C"
{
typedef struct sqlite3 sqlite3;
typedef struct sqlite3_stmt sqlite3_stmt;
typedef struct sqlite3_mutex* _pMutex;
}
namespace Poco {
namespace Data {
namespace SQLite {
class SQLite_API Utility
/// Various utility functions for SQLite.
{
public:
static const std::string SQLITE_DATE_FORMAT;
static const std::string SQLITE_TIME_FORMAT;
typedef std::map<std::string, MetaColumn::ColumnDataType> TypeMap;
static const int THREAD_MODE_SINGLE;
static const int THREAD_MODE_MULTI;
static const int THREAD_MODE_SERIAL;
static const int OPERATION_INSERT;
static const int OPERATION_DELETE;
static const int OPERATION_UPDATE;
static sqlite3* dbHandle(const Session& session);
/// Returns native DB handle.
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(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);
/// Returns column data type.
static bool fileToMemory(sqlite3* pInMemory, const std::string& fileName);
/// Loads the contents of a database file on disk into an opened
/// database in memory.
///
/// Returns true if succesful.
static bool fileToMemory(const Session& session, const std::string& fileName);
/// Loads the contents of a database file on disk into an opened
/// database in memory.
///
/// Returns true if succesful.
static bool memoryToFile(const std::string& fileName, sqlite3* pInMemory);
/// Saves the contents of an opened database in memory to the
/// database on disk.
///
/// Returns true if succesful.
static bool memoryToFile(const std::string& fileName, const Session& session);
/// Saves the contents of an opened database in memory to the
/// database on disk.
///
/// Returns true if succesful.
static bool isThreadSafe();
/// Returns true if SQLite was compiled in multi-thread or serialized mode.
/// See http://www.sqlite.org/c3ref/threadsafe.html for details.
///
/// Returns true if succesful
static bool setThreadMode(int mode);
/// Sets the threading mode to single, multi or serialized.
/// See http://www.sqlite.org/threadsafe.html for details.
///
/// Returns true if succesful
static int getThreadMode();
/// Returns the thread mode.
typedef void(*UpdateCallbackType)(void*, int, const char*, const char*, Poco::Int64);
/// Update callback function type.
typedef int(*CommitCallbackType)(void*);
/// Commit callback function type.
typedef void(*RollbackCallbackType)(void*);
/// Rollback callback function type.
template <typename T, typename CBT>
static bool registerUpdateHandler(sqlite3* pDB, CBT callbackFn, T* pParam)
/// Registers the callback for (1)(insert, delete, update), (2)(commit) or
/// or (3)(rollback) events. Only one function per group can be registered
/// at a time. Registration is not thread-safe. Storage pointed to by pParam
/// must remain valid as long as registration is active. Registering with
/// callbackFn set to zero disables notifications.
///
/// See http://www.sqlite.org/c3ref/update_hook.html and
/// http://www.sqlite.org/c3ref/commit_hook.html for details.
{
typedef std::pair<CBT, T*> CBPair;
typedef std::multimap<sqlite3*, CBPair> CBMap;
typedef typename CBMap::iterator CBMapIt;
typedef std::pair<CBMapIt, CBMapIt> CBMapItPair;
static CBMap retMap;
T* pRet = reinterpret_cast<T*>(eventHookRegister(pDB, callbackFn, pParam));
if (pRet == 0)
{
if (retMap.find(pDB) == retMap.end())
{
retMap.insert(std::make_pair(pDB, CBPair(callbackFn, pParam)));
return true;
}
}
else
{
CBMapItPair retMapRange = retMap.equal_range(pDB);
for (CBMapIt it = retMapRange.first; it != retMapRange.second; ++it)
{
poco_assert (it->second.first != 0);
if ((callbackFn == 0) && (*pRet == *it->second.second))
{
retMap.erase(it);
return true;
}
if ((callbackFn == it->second.first) && (*pRet == *it->second.second))
{
it->second.second = pParam;
return true;
}
}
}
return false;
}
template <typename T, typename CBT>
static bool registerUpdateHandler(const Session& session, CBT callbackFn, T* pParam)
/// Registers the callback by calling registerUpdateHandler(sqlite3*, CBT, T*).
{
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
/// static TypeMap member.
///
/// Note: SQLite is type-agnostic and it is the end-user responsibility
/// to ensure that column declared data type corresponds to the type of
/// data actually held in the database.
///
/// Column types are case-insensitive.
Utility(const Utility&);
Utility& operator = (const Utility&);
static void* eventHookRegister(sqlite3* pDB, UpdateCallbackType callbackFn, void* pParam);
static void* eventHookRegister(sqlite3* pDB, CommitCallbackType callbackFn, void* pParam);
static void* eventHookRegister(sqlite3* pDB, RollbackCallbackType callbackFn, void* pParam);
static TypeMap _types;
static Poco::Mutex _mutex;
static int _threadMode;
};
//
// inlines
//
inline std::string Utility::lastError(const Session& session)
{
poco_assert_dbg ((0 == icompare(session.connector(), 0, 6, "sqlite")));
return lastError(dbHandle(session));
}
inline bool Utility::memoryToFile(const std::string& fileName, const Session& session)
{
poco_assert_dbg ((0 == icompare(session.connector(), 0, 6, "sqlite")));
return memoryToFile(fileName, dbHandle(session));
}
inline bool Utility::fileToMemory(const Session& session, const std::string& fileName)
{
poco_assert_dbg ((0 == icompare(session.connector(), 0, 6, "sqlite")));
return fileToMemory(dbHandle(session), fileName);
}
} } } // namespace Poco::Data::SQLite
#endif // SQLite_Utility_INCLUDED