1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-16 15:17:13 +02:00

Major plugin refactor and cleanup.

Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
This commit is contained in:
Sandu Liviu Catalin
2021-01-30 08:51:39 +02:00
parent e0e34b4030
commit 4a6bfc086c
6219 changed files with 1209835 additions and 454916 deletions

View File

@ -0,0 +1,259 @@
//
// Binder.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: Binder
//
// Definition of the Binder class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_Binder_INCLUDED
#define SQL_PostgreSQL_Binder_INCLUDED
#include "Poco/Data/PostgreSQL/PostgreSQL.h"
#include "Poco/Data/PostgreSQL/PostgreSQLTypes.h"
#include "Poco/Data/PostgreSQL/PostgreSQLException.h"
#include "Poco/Data/AbstractBinder.h"
#include "Poco/Data/MetaColumn.h"
#include "Poco/Data/LOB.h"
#include "Poco/Types.h"
#include <libpq-fe.h>
namespace Poco {
namespace Data {
namespace PostgreSQL {
class PostgreSQL_API Binder: public Poco::Data::AbstractBinder
/// Binds INPUT (only) placeholders in the sql query to the provided values.
/// Allows data type mapping at statement execution time.
{
public:
using Ptr = SharedPtr<Binder>;
Binder();
/// Creates the Binder.
virtual ~Binder();
/// Destroys the Binder.
virtual void bind(std::size_t pos, const Poco::Int8& val, Direction dir = PD_IN);
/// Binds an Int8.
virtual void bind(std::size_t pos, const Poco::UInt8& val, Direction dir = PD_IN);
/// Binds an UInt8.
virtual void bind(std::size_t pos, const Poco::Int16& val, Direction dir = PD_IN);
/// Binds an Int16.
virtual void bind(std::size_t pos, const Poco::UInt16& val, Direction dir = PD_IN);
/// Binds an UInt16.
virtual void bind(std::size_t pos, const Poco::Int32& val, Direction dir = PD_IN);
/// Binds an Int32.
virtual void bind(std::size_t pos, const Poco::UInt32& val, Direction dir = PD_IN);
/// Binds an UInt32.
virtual void bind(std::size_t pos, const Poco::Int64& val, Direction dir = PD_IN);
/// Binds an Int64.
virtual void bind(std::size_t pos, const Poco::UInt64& val, Direction dir = PD_IN);
/// Binds an UInt64.
#ifndef POCO_INT64_IS_LONG
virtual void bind(std::size_t pos, const long& val, Direction dir = PD_IN);
/// Binds a long.
virtual void bind(std::size_t pos, const unsigned long& val, Direction dir = PD_IN);
/// Binds an unsigned long.
#endif
virtual void bind(std::size_t pos, const bool& val, Direction dir = PD_IN);
/// Binds a boolean.
virtual void bind(std::size_t pos, const float& val, Direction dir = PD_IN);
/// Binds a float.
virtual void bind(std::size_t pos, const double& val, Direction dir = PD_IN);
/// Binds a double.
virtual void bind(std::size_t pos, const char& val, Direction dir = PD_IN);
/// Binds a single character.
virtual void bind(std::size_t pos, const std::string& val, Direction dir = PD_IN);
/// Binds a string.
virtual void bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir = PD_IN);
/// Binds a BLOB.
virtual void bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir = PD_IN);
/// Binds a CLOB.
virtual void bind(std::size_t pos, const DateTime& val, Direction dir = PD_IN);
/// Binds a DateTime.
virtual void bind(std::size_t pos, const Date& val, Direction dir = PD_IN);
/// Binds a Date.
virtual void bind(std::size_t pos, const Time& val, Direction dir = PD_IN);
/// Binds a Time.
virtual void bind(std::size_t pos, const NullData& val, Direction dir = PD_IN);
/// Binds a null.
virtual void bind(std::size_t pos, const std::vector<Poco::Int8>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Poco::Int8>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Poco::Int8>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Poco::UInt8>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Poco::UInt8>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Poco::UInt8>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Poco::Int16>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Poco::Int16>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Poco::Int16>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Poco::UInt16>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Poco::UInt16>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Poco::UInt16>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Poco::Int32>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Poco::Int32>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Poco::Int32>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Poco::UInt32>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Poco::UInt32>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Poco::UInt32>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Poco::Int64>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Poco::Int64>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Poco::Int64>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Poco::UInt64>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Poco::UInt64>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Poco::UInt64>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<bool>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<bool>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<bool>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<float>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<float>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<float>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<double>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<double>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<double>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<char>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<char>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<char>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<BLOB>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<BLOB>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<BLOB>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<CLOB>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<CLOB>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<CLOB>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<DateTime>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<DateTime>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<DateTime>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Date>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Date>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Date>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<Time>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<Time>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<Time>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<NullData>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<NullData>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<NullData>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::vector<std::string>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::deque<std::string>& val, Direction dir = PD_IN);
virtual void bind(std::size_t pos, const std::list<std::string>& val, Direction dir = PD_IN);
std::size_t size() const;
/// Return count of bound parameters
InputParameterVector bindVector() const;
/// Return the vector of bound parameters.
void updateBindVectorToCurrentValues();
/// obtain the current version of the bound data and update the internal representation
private:
Binder(const Binder&);
/// Don't copy the binder
virtual void bind(std::size_t, const char* const&, Direction)
/// Binds a const char ptr.
/// This is a private no-op in this implementation
/// due to security risk.
{
}
void realBind(std::size_t aPosition, Poco::Data::MetaColumn::ColumnDataType aFieldType, const void* aBufferPtr, std::size_t aLength);
/// Common bind implementation
private:
InputParameterVector _bindVector;
};
} } } // namespace Poco::Data::PostgreSQL
#endif // Data_PostgreSQL_Binder_INCLUDED

View File

@ -0,0 +1,63 @@
//
// Connector.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: Connector
//
// Definition of the Connector class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_Connector_INCLUDED
#define SQL_PostgreSQL_Connector_INCLUDED
#include "Poco/Data/PostgreSQL/PostgreSQL.h"
#include "Poco/Data/SessionImpl.h"
#include "Poco/Data/Connector.h"
#include "Poco/AutoPtr.h"
#include <string>
namespace Poco {
namespace Data {
namespace PostgreSQL {
class PostgreSQL_API Connector: public Poco::Data::Connector
/// Connector instantiates PostgreSQL SessionImpl objects.
{
public:
static const std::string KEY;
Connector();
/// Creates the Connector.
virtual ~Connector();
/// Destroys the Connector.
virtual const std::string& name() const;
/// Returns the name associated with this connector.
virtual Poco::Data::SessionImpl::Ptr createSession(const std::string& aConnectionString,
std::size_t aTimeout = Poco::Data::SessionImpl::LOGIN_TIMEOUT_DEFAULT);
/// Creates a PostgreSQL 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
};
} } } // namespace Poco::Data::PostgreSQL
#endif // Data_PostgreSQL_Connector_INCLUDED

View File

@ -0,0 +1,358 @@
//
// Extractor.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: Extractor
//
// Definition of the Extractor class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_Extractor_INCLUDED
#define SQL_PostgreSQL_Extractor_INCLUDED
#include "Poco/Data/PostgreSQL/PostgreSQL.h"
#include "Poco/Data/PostgreSQL/PostgreSQLTypes.h"
#include "Poco/Data/PostgreSQL/StatementExecutor.h"
#include "Poco/Data/AbstractExtractor.h"
#include "Poco/Data/LOB.h"
#include "Poco/Types.h"
#include "Poco/Any.h"
#include "Poco/DynamicAny.h"
#include "Poco/Dynamic/Var.h"
namespace Poco {
namespace Data {
namespace PostgreSQL {
class PostgreSQL_API Extractor: public Poco::Data::AbstractExtractor
/// Extracts and converts data values from the result row returned by PostgreSQL.
/// If NULL is received, the incoming val value is not changed and false is returned
{
public:
using Ptr = SharedPtr<Extractor>;
Extractor(StatementExecutor& st);
/// Creates the Extractor.
virtual ~Extractor();
/// Destroys the Extractor.
virtual bool extract(std::size_t pos, Poco::Int8& val);
/// Extracts an Int8.
virtual bool extract(std::size_t pos, Poco::UInt8& val);
/// Extracts an UInt8.
virtual bool extract(std::size_t pos, Poco::Int16& val);
/// Extracts an Int16.
virtual bool extract(std::size_t pos, Poco::UInt16& val);
/// Extracts an UInt16.
virtual bool extract(std::size_t pos, Poco::Int32& val);
/// Extracts an Int32.
virtual bool extract(std::size_t pos, Poco::UInt32& val);
/// Extracts an UInt32.
virtual bool extract(std::size_t pos, Poco::Int64& val);
/// Extracts an Int64.
virtual bool extract(std::size_t pos, Poco::UInt64& val);
/// Extracts an UInt64.
#ifndef POCO_INT64_IS_LONG
virtual bool extract(std::size_t pos, long& val);
/// Extracts a long. Returns false if null was received.
virtual bool extract(std::size_t pos, unsigned long& val);
/// Extracts an unsigned long. Returns false if null was received.
#endif
virtual bool extract(std::size_t pos, bool& val);
/// Extracts a boolean.
virtual bool extract(std::size_t pos, float& val);
/// Extracts a float.
virtual bool extract(std::size_t pos, double& val);
/// Extracts a double.
virtual bool extract(std::size_t pos, char& val);
/// Extracts a single character.
virtual bool extract(std::size_t pos, std::string& val);
/// Extracts a string.
virtual bool extract(std::size_t pos, Poco::Data::BLOB& val);
/// Extracts a BLOB.
virtual bool extract(std::size_t pos, Poco::Data::CLOB& val);
/// Extracts a CLOB.
virtual bool extract(std::size_t pos, DateTime& val);
/// Extracts a DateTime. Returns false if null was received.
virtual bool extract(std::size_t pos, Date& val);
/// Extracts a Date. Returns false if null was received.
virtual bool extract(std::size_t pos, Time& val);
/// Extracts a Time. Returns false if null was received.
virtual bool extract(std::size_t pos, Any& val);
/// Extracts an Any. Returns false if null was received.
virtual bool extract(std::size_t pos, Dynamic::Var& val);
/// Extracts a Dynamic::Var. Returns false if null was received.
virtual bool isNull(std::size_t col, std::size_t row);
/// Returns true if the value at [col,row] position is null.
virtual void reset();
/// Resets any information internally cached by the extractor.
////////////
// Not implemented extract functions
////////////
virtual bool extract(std::size_t pos, std::vector<Poco::Int8>& val);
/// Extracts an Int8 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::Int8>& val);
/// Extracts an Int8 deque.
virtual bool extract(std::size_t pos, std::list<Poco::Int8>& val);
/// Extracts an Int8 list.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt8>& val);
/// Extracts an UInt8 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt8>& val);
/// Extracts an UInt8 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt8>& val);
/// Extracts an UInt8 list.
virtual bool extract(std::size_t pos, std::vector<Poco::Int16>& val);
/// Extracts an Int16 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::Int16>& val);
/// Extracts an Int16 deque.
virtual bool extract(std::size_t pos, std::list<Poco::Int16>& val);
/// Extracts an Int16 list.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt16>& val);
/// Extracts an UInt16 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt16>& val);
/// Extracts an UInt16 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt16>& val);
/// Extracts an UInt16 list.
virtual bool extract(std::size_t pos, std::vector<Poco::Int32>& val);
/// Extracts an Int32 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::Int32>& val);
/// Extracts an Int32 deque.
virtual bool extract(std::size_t pos, std::list<Poco::Int32>& val);
/// Extracts an Int32 list.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt32>& val);
/// Extracts an UInt32 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt32>& val);
/// Extracts an UInt32 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt32>& val);
/// Extracts an UInt32 list.
virtual bool extract(std::size_t pos, std::vector<Poco::Int64>& val);
/// Extracts an Int64 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::Int64>& val);
/// Extracts an Int64 deque.
virtual bool extract(std::size_t pos, std::list<Poco::Int64>& val);
/// Extracts an Int64 list.
virtual bool extract(std::size_t pos, std::vector<Poco::UInt64>& val);
/// Extracts an UInt64 vector.
virtual bool extract(std::size_t pos, std::deque<Poco::UInt64>& val);
/// Extracts an UInt64 deque.
virtual bool extract(std::size_t pos, std::list<Poco::UInt64>& val);
/// Extracts an UInt64 list.
#ifndef POCO_INT64_IS_LONG
virtual bool extract(std::size_t pos, std::vector<long>& val);
/// Extracts a long vector.
virtual bool extract(std::size_t pos, std::deque<long>& val);
/// Extracts a long deque.
virtual bool extract(std::size_t pos, std::list<long>& val);
/// Extracts a long list.
#endif
virtual bool extract(std::size_t pos, std::vector<bool>& val);
/// Extracts a boolean vector.
virtual bool extract(std::size_t pos, std::deque<bool>& val);
/// Extracts a boolean deque.
virtual bool extract(std::size_t pos, std::list<bool>& val);
/// Extracts a boolean list.
virtual bool extract(std::size_t pos, std::vector<float>& val);
/// Extracts a float vector.
virtual bool extract(std::size_t pos, std::deque<float>& val);
/// Extracts a float deque.
virtual bool extract(std::size_t pos, std::list<float>& val);
/// Extracts a float list.
virtual bool extract(std::size_t pos, std::vector<double>& val);
/// Extracts a double vector.
virtual bool extract(std::size_t pos, std::deque<double>& val);
/// Extracts a double deque.
virtual bool extract(std::size_t pos, std::list<double>& val);
/// Extracts a double list.
virtual bool extract(std::size_t pos, std::vector<char>& val);
/// Extracts a character vector.
virtual bool extract(std::size_t pos, std::deque<char>& val);
/// Extracts a character deque.
virtual bool extract(std::size_t pos, std::list<char>& val);
/// Extracts a character list.
virtual bool extract(std::size_t pos, std::vector<std::string>& val);
/// Extracts a string vector.
virtual bool extract(std::size_t pos, std::deque<std::string>& val);
/// Extracts a string deque.
virtual bool extract(std::size_t pos, std::list<std::string>& val);
/// Extracts a string list.
virtual bool extract(std::size_t pos, std::vector<BLOB>& val);
/// Extracts a BLOB vector.
virtual bool extract(std::size_t pos, std::deque<BLOB>& val);
/// Extracts a BLOB deque.
virtual bool extract(std::size_t pos, std::list<BLOB>& val);
/// Extracts a BLOB list.
virtual bool extract(std::size_t pos, std::vector<CLOB>& val);
/// Extracts a CLOB vector.
virtual bool extract(std::size_t pos, std::deque<CLOB>& val);
/// Extracts a CLOB deque.
virtual bool extract(std::size_t pos, std::list<CLOB>& val);
/// Extracts a CLOB list.
virtual bool extract(std::size_t pos, std::vector<DateTime>& val);
/// Extracts a DateTime vector.
virtual bool extract(std::size_t pos, std::deque<DateTime>& val);
/// Extracts a DateTime deque.
virtual bool extract(std::size_t pos, std::list<DateTime>& val);
/// Extracts a DateTime list.
virtual bool extract(std::size_t pos, std::vector<Date>& val);
/// Extracts a Date vector.
virtual bool extract(std::size_t pos, std::deque<Date>& val);
/// Extracts a Date deque.
virtual bool extract(std::size_t pos, std::list<Date>& val);
/// Extracts a Date list.
virtual bool extract(std::size_t pos, std::vector<Time>& val);
/// Extracts a Time vector.
virtual bool extract(std::size_t pos, std::deque<Time>& val);
/// Extracts a Time deque.
virtual bool extract(std::size_t pos, std::list<Time>& val);
/// Extracts a Time list.
virtual bool extract(std::size_t pos, std::vector<Any>& val);
/// Extracts an Any vector.
virtual bool extract(std::size_t pos, std::deque<Any>& val);
/// Extracts an Any deque.
virtual bool extract(std::size_t pos, std::list<Any>& val);
/// Extracts an Any list.
virtual bool extract(std::size_t pos, std::vector<Dynamic::Var>& val);
/// Extracts a Dynamic::Var vector.
virtual bool extract(std::size_t pos, std::deque<Dynamic::Var>& val);
/// Extracts a Dynamic::Var deque.
virtual bool extract(std::size_t pos, std::list<Dynamic::Var>& val);
/// Extracts a Dynamic::Var list.
private:
const OutputParameter& extractPreamble(std::size_t aPosition) const;
bool isColumnNull(const OutputParameter& anOutputParameter) const;
template <typename T>
bool extractStringImpl(std::size_t pos, T& val)
/// Utility function for extraction of Any and DynamicAny.
{
OutputParameter outputParameter = extractPreamble(pos);
if (isColumnNull(outputParameter))
{
return false;
}
std::string tempString; // since the postgreSQL API in use is all about strings...
bool returnValue = extract(pos, tempString);
if (returnValue)
{
val = tempString;
}
return returnValue;
}
// Prevent VC8 warning "operator= could not be generated"
Extractor& operator=(const Extractor&);
private:
StatementExecutor& _statementExecutor;
};
} } } // namespace Poco::Data::PostgreSQL
#endif // SQL_PostgreSQL_Extractor_INCLUDED

View File

@ -0,0 +1,61 @@
//
// PostgreSQL.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: PostgreSQL
//
// Basic definitions for the PostgreSQL library.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_INCLUDED
#define SQL_PostgreSQL_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 ODBC_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
// ODBC_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32) && defined(POCO_DLL)
#if defined(PostgreSQL_EXPORTS)
#define PostgreSQL_API __declspec(dllexport)
#else
#define PostgreSQL_API __declspec(dllimport)
#endif
#endif
#if !defined(PostgreSQL_API)
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
#define PostgreSQL_API __attribute__ ((visibility ("default")))
#else
#define PostgreSQL_API
#endif
#endif
//
// Automatically link Data library.
//
#if defined(_MSC_VER) && !defined(POCO_NO_AUTOMATIC_LIBS)
#if !defined(PostgreSQL_EXPORTS)
#pragma comment(lib, "PocoDataPostgreSQL" POCO_LIB_SUFFIX)
#endif
#pragma comment(lib, "libpq")
#endif
#endif // SQL_PostgreSQL_INCLUDED

View File

@ -0,0 +1,135 @@
//
// PostgreSQLException.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: PostgreSQLException
//
// Definition of the PostgreSQLException class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_PostgreSQLException_INCLUDED
#define SQL_PostgreSQL_PostgreSQLException_INCLUDED
#include "Poco/Data/PostgreSQL/PostgreSQL.h"
#include "Poco/Data/DataException.h"
#include <typeinfo>
#include <string>
namespace Poco {
namespace Data {
namespace PostgreSQL {
class PostgreSQL_API PostgreSQLException: public Poco::Data::DataException
/// Base class for all PostgreSQL exceptions
{
public:
explicit PostgreSQLException(const std::string& aMessage);
/// Creates PostgreSQLException.
PostgreSQLException(const PostgreSQLException& exc);
/// Creates PostgreSQLException.
~PostgreSQLException() noexcept;
/// Destroys PostgreSQLexception.
PostgreSQLException& operator = (const PostgreSQLException& exc);
/// Assignment operator.
const char* name() const noexcept;
/// Returns exception name.
const char* className() const noexcept;
/// Returns the name of the exception class.
Poco::Exception* clone() const;
/// Creates an exact copy of the exception.
///
/// The copy can later be thrown again by
/// invoking rethrow() on it.
void rethrow() const;
/// (Re)Throws the exception.
///
/// This is useful for temporarily storing a
/// copy of an exception (see clone()), then
/// throwing it again.
};
class ConnectionException: public PostgreSQLException
/// ConnectionException
{
public:
ConnectionException(const std::string& aMessage);
/// Creates ConnectionException from string.
};
class TransactionException: public ConnectionException
/// TrabsactionException
{
public:
TransactionException(const std::string& aMessage);
/// Creates TransactionException from string.
};
class StatementException: public PostgreSQLException
/// StatementException
{
public:
StatementException(const std::string& aMessage);
/// Creates StatementException from string.
};
//
// inlines
//
inline PostgreSQLException& PostgreSQLException::operator = (const PostgreSQLException& exc)
{
Poco::Data::DataException::operator = (exc);
return *this;
}
inline const char* PostgreSQLException::name() const noexcept
{
return "PostgreSQL";
}
inline const char* PostgreSQLException::className() const noexcept
{
return typeid(*this).name();
}
inline Poco::Exception* PostgreSQLException::clone() const
{
return new PostgreSQLException(*this);
}
inline void PostgreSQLException::rethrow() const
{
throw *this;
}
} } } // namespace Poco::Data::PostgreSQL
#endif //SQL_PostgreSQL_PostgreSQLException_INCLUDED

View File

@ -0,0 +1,100 @@
//
// PostgreSQLStatementImpl.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: PostgreSQLStatementImpl
//
// Definition of the PostgreSQLStatementImpl class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_PostgreSQLStatementImpl_INCLUDED
#define SQL_PostgreSQL_PostgreSQLStatementImpl_INCLUDED
#include "Poco/Data/PostgreSQL/PostgreSQL.h"
#include "Poco/Data/PostgreSQL/SessionImpl.h"
#include "Poco/Data/PostgreSQL/Binder.h"
#include "Poco/Data/PostgreSQL/Extractor.h"
#include "Poco/Data/PostgreSQL/StatementExecutor.h"
#include "Poco/Data/StatementImpl.h"
#include "Poco/SharedPtr.h"
#include "Poco/Format.h"
namespace Poco {
namespace Data {
namespace PostgreSQL {
class PostgreSQL_API PostgreSQLStatementImpl: public Poco::Data::StatementImpl
/// Implements statement functionality needed for PostgreSQL
{
public:
PostgreSQLStatementImpl(SessionImpl& aSessionImpl);
/// Creates the PostgreSQLStatementImpl.
~PostgreSQLStatementImpl();
/// Destroys the PostgreSQLStatementImpl.
protected:
virtual std::size_t columnsReturned() const;
/// Returns number of columns returned by query.
virtual int affectedRowCount() const;
/// Returns the number of affected rows.
/// Used to find out the number of rows affected by insert, delete or update.
virtual const MetaColumn& metaColumn(std::size_t aPosition) const;
/// Returns column meta data.
virtual bool hasNext();
/// Returns true if a call to next() will return data.
virtual std::size_t next();
/// Retrieves the next row from the resultset.
/// Will throw, if the resultset is empty.
virtual bool canBind() const;
/// Returns true if a valid statement is set and we can bind.
virtual bool canCompile() const;
/// Returns true if another compile is possible.
virtual void compileImpl();
/// Compiles the statement, doesn't bind yet
virtual void bindImpl();
/// Binds parameters
virtual Poco::Data::AbstractExtractor::Ptr extractor();
/// Returns the concrete extractor used by the statement.
virtual Poco::Data::AbstractBinder::Ptr binder();
/// Returns the concrete binder used by the statement.
private:
enum NextState
{
NEXT_DONTKNOW,
NEXT_TRUE,
NEXT_FALSE
};
StatementExecutor _statementExecutor;
Binder::Ptr _pBinder;
Extractor::Ptr _pExtractor;
NextState _hasNext;
};
} } } // namespace Poco::Data::PostgreSQL
#endif // SQL_PostgreSQL_PostgreSQLStatementImpl_INCLUDED

View File

@ -0,0 +1,423 @@
//
// PostgreSQLTypes.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: PostgreSQLTypes
//
// Definition of the SessionHandle class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_Types_INCLUDED
#define SQL_PostgreSQL_Types_INCLUDED
#include "Poco/Data/MetaColumn.h"
#include <vector>
#include <libpq-fe.h>
namespace Poco {
namespace Data {
namespace PostgreSQL {
/// Oid constants duplicated from PostgreSQL "include/postgresql/server/catalog/pg_type.h"
/// because PostgreSQL compile time definitions are too onerous to reproduce for this module
const Oid BOOLOID = 16;
const Oid INT2OID = 21;
const Oid INT4OID = 23;
const Oid INT8OID = 20;
const Oid FLOAT8OID = 701; // double
const Oid FLOAT4OID = 700;
const Oid NUMERICOID = 1700;
const Oid CHAROID = 18;
const Oid BPCHAROID = 1042; // fixed length char
const Oid VARCHAROID = 1043;
const Oid BYTEAOID = 17; // BLOB
const Oid TEXTOID = 25; // CLOB
const Oid DATEOID = 1082;
const Oid TIMEOID = 1083;
const Oid TIMETZOID = 1266;
const Oid TIMESTAMPOID = 1114;
const Oid TIMESTAMPZOID = 1184;
// future use
const Oid BITOID = 1560;
const Oid VARYBITOID = 1562;
const Oid CASHOID = 790;
const Oid MACADDROID = 829;
const Oid UUIDOID = 2950;
Poco::Data::MetaColumn::ColumnDataType oidToColumnDataType(const Oid anOID);
class InputParameter
/// PostgreSQL class to record values for input parameters to SQL statements
{
public:
using CDT = Poco::Data::MetaColumn::ColumnDataType;
InputParameter(CDT fieldType, const void* dataPtr, std::size_t dataLength);
InputParameter();
~InputParameter();
CDT fieldType() const;
const void* pData() const;
std::size_t size() const;
bool isBinary() const;
void setStringVersionRepresentation(const std::string& aString);
void setNonStringVersionRepresentation(const void* aPtr, std::size_t theSize);
const void* pInternalRepresentation() const;
private:
CDT _fieldType;
const void* _pData;
std::size_t _size;
bool _isBinary;
std::string _stringVersionRepresentation;
void* _pNonStringVersionRepresentation;
};
using InputParameterVector = std::vector <InputParameter>;
class OutputParameter
/// PostgreSQL class to record values for output parameters to capture the results
{
public:
using CDT = Poco::Data::MetaColumn::ColumnDataType;
OutputParameter(CDT aFieldType, Oid internalFieldType, std::size_t rowNumber,
const char* dataPtr, std::size_t size, bool isNull);
OutputParameter();
~OutputParameter();
void setValues(CDT fieldType, Oid internalFieldType, std::size_t rowNumber,
const char* dataPtr, std::size_t size, bool isNull);
CDT fieldType() const;
Oid internalFieldType() const;
std::size_t rowNumber() const;
const char* pData() const;
std::size_t size() const;
bool isNull() const;
private:
CDT _fieldType;
Oid _internalFieldType;
std::size_t _rowNumber;
const char* _pData;
std::size_t _size;
bool _isNull;
};
using OutputParameterVector = std::vector <OutputParameter>;
class PQConnectionInfoOptionsFree
/// PostgreSQL connection Info Options free (RAII)
{
public:
explicit PQConnectionInfoOptionsFree(PQconninfoOption* aConnectionInfoOptionPtr);
~PQConnectionInfoOptionsFree();
private:
PQConnectionInfoOptionsFree(const PQConnectionInfoOptionsFree&);
PQConnectionInfoOptionsFree& operator = (const PQConnectionInfoOptionsFree&);
private:
PQconninfoOption* _pConnectionInfoOption;
};
class PQResultClear
/// PostgreSQL statement result free (RAII)
{
public:
explicit PQResultClear(PGresult* aPQResultPtr);
~PQResultClear();
private:
PQResultClear(const PQResultClear&);
PQResultClear& operator = (const PQResultClear&);
private:
PGresult* _pPQResult;
};
class PGCancelFree
/// PostgreSQL Cancel Info Options free (RAII)
{
public:
explicit PGCancelFree(PGcancel* aStatementCancelPtr);
~PGCancelFree();
private:
PGCancelFree(const PGCancelFree&);
PGCancelFree& operator = (const PGCancelFree&);
private:
PGcancel* _pPGCancel;
};
//
// inlines
//
inline InputParameter::InputParameter(Poco::Data::MetaColumn::ColumnDataType fieldType,
const void* aDataPtr, std::size_t theSize):
_fieldType(fieldType),
_pData(aDataPtr),
_size(theSize),
_isBinary(Poco::Data::MetaColumn::FDT_BLOB == _fieldType || Poco::Data::MetaColumn::FDT_CLOB == _fieldType),
_pNonStringVersionRepresentation(0)
{
}
inline InputParameter::InputParameter(): _fieldType(Poco::Data::MetaColumn::FDT_UNKNOWN),
_pData(0),
_size(0),
_isBinary(false),
_pNonStringVersionRepresentation(0)
{
}
inline InputParameter::~InputParameter()
{
}
inline const void* InputParameter::pData() const
{
return _pData;
}
inline Poco::Data::MetaColumn::ColumnDataType InputParameter::fieldType() const
{
return _fieldType;
}
inline std::size_t InputParameter::size() const
{
return _size;
}
inline bool InputParameter::isBinary() const
{
return _isBinary;
}
inline void InputParameter::setStringVersionRepresentation(const std::string& aString)
{
_pNonStringVersionRepresentation = 0;
_stringVersionRepresentation = aString;
_size = _stringVersionRepresentation.size();
}
inline void InputParameter::setNonStringVersionRepresentation(const void* aPtr, std::size_t theDataLength)
{
_stringVersionRepresentation = std::string();
_pNonStringVersionRepresentation = const_cast<void *> (aPtr);
_size = theDataLength;
}
inline const void* InputParameter::pInternalRepresentation() const
{
switch (_fieldType)
{
case Poco::Data::MetaColumn::FDT_BOOL:
case Poco::Data::MetaColumn::FDT_INT8:
case Poco::Data::MetaColumn::FDT_UINT8:
case Poco::Data::MetaColumn::FDT_INT16:
case Poco::Data::MetaColumn::FDT_UINT16:
case Poco::Data::MetaColumn::FDT_INT32:
case Poco::Data::MetaColumn::FDT_UINT32:
case Poco::Data::MetaColumn::FDT_INT64:
case Poco::Data::MetaColumn::FDT_UINT64:
case Poco::Data::MetaColumn::FDT_FLOAT:
case Poco::Data::MetaColumn::FDT_DOUBLE:
case Poco::Data::MetaColumn::FDT_STRING:
case Poco::Data::MetaColumn::FDT_DATE:
case Poco::Data::MetaColumn::FDT_TIME:
case Poco::Data::MetaColumn::FDT_TIMESTAMP:
return _stringVersionRepresentation.c_str();
case Poco::Data::MetaColumn::FDT_BLOB:
case Poco::Data::MetaColumn::FDT_CLOB:
return _pNonStringVersionRepresentation;
case Poco::Data::MetaColumn::FDT_UNKNOWN:
default: return 0;
}
}
inline OutputParameter::OutputParameter(Poco::Data::MetaColumn::ColumnDataType aFieldType,
Oid anInternalFieldType,
std::size_t aRowNumber,
const char* aDataPtr,
std::size_t theSize,
bool anIsNull):
_fieldType(aFieldType),
_internalFieldType(anInternalFieldType),
_rowNumber(aRowNumber),
_pData(aDataPtr),
_size(theSize),
_isNull(anIsNull)
{
}
inline OutputParameter::OutputParameter():
_fieldType(Poco::Data::MetaColumn::FDT_UNKNOWN),
_internalFieldType(static_cast<Oid>(-1)),
_rowNumber(0),
_pData(0),
_size(0),
_isNull(true)
{
}
inline OutputParameter::~OutputParameter()
{
}
inline void OutputParameter::setValues(Poco::Data::MetaColumn::ColumnDataType aFieldType,
Oid anInternalFieldType,
std::size_t aRowNumber,
const char* aDataPtr,
std::size_t theSize,
bool anIsNull)
{
_fieldType = aFieldType;
_internalFieldType = anInternalFieldType;
_rowNumber = aRowNumber;
_pData = aDataPtr;
_size = theSize;
_isNull = anIsNull;
}
inline Poco::Data::MetaColumn::ColumnDataType OutputParameter::fieldType() const
{
return _fieldType;
}
inline Oid OutputParameter::internalFieldType() const
{
return _internalFieldType;
}
inline std::size_t OutputParameter::rowNumber() const
{
return _rowNumber;
}
inline const char* OutputParameter::pData() const
{
return _pData;
}
inline std::size_t OutputParameter::size() const
{
return _size;
}
inline bool OutputParameter::isNull() const
{
return _isNull;
}
inline PQConnectionInfoOptionsFree::PQConnectionInfoOptionsFree(PQconninfoOption* aConnectionInfoOptionPtr):
_pConnectionInfoOption(aConnectionInfoOptionPtr)
{
}
inline PQConnectionInfoOptionsFree::~PQConnectionInfoOptionsFree()
{
if (_pConnectionInfoOption)
{
PQconninfoFree(_pConnectionInfoOption);
_pConnectionInfoOption = 0;
}
}
inline PQResultClear::PQResultClear(PGresult* aPQResultPtr):
_pPQResult(aPQResultPtr)
{
}
inline PQResultClear::~PQResultClear()
{
if (_pPQResult)
{
PQclear(_pPQResult);
_pPQResult = 0;
}
}
// PGCancelFree
inline PGCancelFree::PGCancelFree(PGcancel* aStatementCancelPtr):
_pPGCancel(aStatementCancelPtr)
{
}
inline PGCancelFree::~PGCancelFree()
{
if (_pPGCancel)
{
PQfreeCancel(_pPGCancel);
_pPGCancel = 0;
}
}
} } } // namespace Poco::Data::PostgreSQL
#endif // SQL_PostgreSQL_Types_INCLUDED

View File

@ -0,0 +1,322 @@
//
// SesssionHandle.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: SessionHandle
//
// Definition of the SessionHandle class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_SessionHandle_INCLUDED
#define SQL_PostgreSQL_SessionHandle_INCLUDED
#include "Poco/Mutex.h"
#include "Poco/Types.h"
#include <map>
#include <string>
#include <vector>
#include <libpq-fe.h>
namespace Poco {
namespace Data {
namespace PostgreSQL {
class SessionParameters
/// PostgreSQL session parameters
{
public:
enum HowToDisplay
{
HTD_ASIS, // as is
HTD_HIDE, // do not display (e.g. passwords)
HID_DEBUG // debug use only
};
SessionParameters(const std::string& aKeyword,
const std::string& anEnvironmentVariable,
const std::string& aCompiledDefault,
const std::string& aCurrentValue,
const std::string& aDisplayLabel,
const std::string& aHowToDisplay,
int aDisplaySize);
~SessionParameters();
std::string keyword() const;
std::string enviromentVariable() const;
std::string compiledDefault() const;
std::string currentValue() const;
std::string displayLabel() const;
HowToDisplay howToDisplay() const;
int displaySize() const;
private:
std::string _keyword; // The keyword of the option
std::string _environmentVariable; // Fallback environment variable name
std::string _compiledDefault; // Fallback compiled in default value
std::string _currentValue; // Option's current value, or NULL
std::string _displayLabel; // Label for field in a connect dialog
HowToDisplay _howToDisplay; // Indicates how to display this field
int _displaySize; // Field size in characters for connect dialog
};
using SessionParametersMap = std::map<std::string, SessionParameters>;
class SessionHandle
/// PostgreSQL connection(session) handle
{
public:
explicit SessionHandle();
/// Creates session handle
~SessionHandle();
/// Destroy handle, close connection
void connect(const std::string& aConnectionString);
/// Connect to server
void connect(const char* aConnectionString);
void connect(const char* aHost, const char* aUser, const char* aPassword,
const char* aDatabase, unsigned short aPort, unsigned int aConnectionTimeout);
bool isConnected() const;
/// is a connection established?
void disconnect();
/// Close connection
bool reset();
/// reset the connection
std::string lastError() const;
/// last error on the connection
void startTransaction();
/// Start transaction
bool isTransaction();
/// Returns true iff a transaction is a transaction is in progress, false otherwise.
void commit();
/// Commit transaction
void rollback();
/// Rollback trabsaction
bool isAutoCommit();
/// is the connection in auto commit mode?
void setAutoCommit(bool aShouldAutoCommit = true);
/// is the connection in auto commit mode?
bool isAsynchronousCommit();
/// is the connection in Asynchronous commit mode?
void setAsynchronousCommit(bool aShouldAsynchronousCommit = true);
/// is the connection in Asynchronous commit mode?
void cancel();
/// Attempts to cancel in-process statements
void setTransactionIsolation(Poco::UInt32 aTI);
/// Sets the transaction isolation level.
Poco::UInt32 transactionIsolation();
/// Returns the transaction isolation level.
bool hasTransactionIsolation(Poco::UInt32 aTI);
/// Returns true iff the transaction isolation level corresponding
/// to the supplied bitmask is supported.
void deallocatePreparedStatement(const std::string& aPreparedStatementToDeAllocate);
/// deallocates a previously prepared statement
int serverVersion() const;
/// remote server version
int serverProcessID() const;
/// the process ID of the remotee server process
int protocoVersion() const;
/// the protocol version between the client and the server
std::string clientEncoding() const;
/// returns the client encoding
int libpqVersion() const;
/// returns the version of libpq
static SessionParametersMap connectionDefaultParameters();
/// returns the default parameters used on a connection
SessionParametersMap connectionParameters() const;
/// returns the parameters used on the connection
std::string connectionString() const;
/// returns the string used to connect
operator PGconn* ();
/// Get the PostgreSQL connection pointer
Poco::FastMutex& mutex();
/// Get the sessionHandle mutex to protect the connection pointer
private:
static SessionParametersMap setConnectionInfoParameters(PQconninfoOption* aConnectionInfoOptionsPtr);
void deallocateStoredPreparedStatements();
void deallocatePreparedStatementNoLock(const std::string& aPreparedStatementToDeAllocate);
bool isConnectedNoLock() const;
std::string lastErrorNoLock() const;
SessionHandle(const SessionHandle&);
SessionHandle& operator= (const SessionHandle&);
private:
mutable Poco::FastMutex _sessionMutex;
PGconn* _pConnection;
std::string _connectionString;
bool _inTransaction;
bool _isAutoCommit;
bool _isAsynchronousCommit;
Poco::UInt32 _tranactionIsolationLevel;
std::vector <std::string> _preparedStatementsToBeDeallocated;
// static const std::string POSTGRESQL_READ_UNCOMMITTED; // NOT SUPPORTED
static const std::string POSTGRESQL_READ_COMMITTED;
static const std::string POSTGRESQL_REPEATABLE_READ;
static const std::string POSTGRESQL_SERIALIZABLE;
};
//
// inlines
//
inline SessionParameters::SessionParameters(const std::string& aKeyword,
const std::string& anEnvironmentVariable,
const std::string& aCompiledDefault,
const std::string& aCurrentValue,
const std::string& aDisplayLabel,
const std::string& aHowToDisplay,
int aDisplaySize):_keyword(aKeyword),
_environmentVariable(anEnvironmentVariable),
_compiledDefault(aCompiledDefault),
_currentValue(aCurrentValue),
_displayLabel(aDisplayLabel),
_howToDisplay(HTD_ASIS),
_displaySize(aDisplaySize)
{
if (aHowToDisplay == "*")
{
_howToDisplay = HTD_HIDE;
}
if (aHowToDisplay == "D")
{
_howToDisplay = HID_DEBUG;
}
}
inline SessionParameters::~SessionParameters()
{
}
inline std::string SessionParameters::keyword() const
{
return _keyword;
}
inline std::string SessionParameters::enviromentVariable() const
{
return _environmentVariable;
}
inline std::string SessionParameters::compiledDefault() const
{
return _compiledDefault;
}
inline std::string SessionParameters::currentValue() const
{
return _currentValue;
}
inline std::string SessionParameters::displayLabel() const
{
return _displayLabel;
}
inline SessionParameters::HowToDisplay SessionParameters::howToDisplay() const
{
return _howToDisplay;
}
inline int SessionParameters::displaySize() const
{
return _displaySize;
}
inline SessionHandle::operator PGconn * ()
{
return _pConnection;
}
inline Poco::FastMutex&SessionHandle::mutex()
{
return _sessionMutex;
}
inline std::string SessionHandle::connectionString() const
{
return _connectionString;
}
inline bool SessionHandle::isTransaction()
{
return _inTransaction;
}
inline bool SessionHandle::isAutoCommit()
{
return _isAutoCommit;
}
inline bool SessionHandle::isAsynchronousCommit()
{
return _isAsynchronousCommit;
}
} } } // namespace Poco::Data::PostgreSQL
#endif // SQL_PostgreSQL_SessionHandle_INCLUDED

View File

@ -0,0 +1,165 @@
//
// SessionImpl.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: SessionImpl
//
// Definition of the SessionImpl class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_SessionImpl_INCLUDED
#define SQL_PostgreSQL_SessionImpl_INCLUDED
#include "Poco/Data/PostgreSQL/PostgreSQL.h"
#include "Poco/Data/PostgreSQL/SessionHandle.h"
#include "Poco/Data/AbstractSessionImpl.h"
#include "Poco/Data/StatementImpl.h"
#include <string>
namespace Poco {
namespace Data {
namespace PostgreSQL {
class PostgreSQL_API SessionImpl: public Poco::Data::AbstractSessionImpl<SessionImpl>
/// Implements SessionImpl interface
{
public:
SessionImpl(const std::string& aConnectionString,
std::size_t aLoginTimeout = LOGIN_TIMEOUT_DEFAULT);
/// Creates the SessionImpl. Opens a connection to the database
///
/// Connection string format:
/// <str> == <assignment> | <assignment> ' ' <str>
/// <assignment> == <name> '=' <value>
/// <name> == 'host' | 'port' | 'user' | 'password' | 'dbname' | 'connect_timeout'
/// <value> == [~;]*
///
/// consult postgres documentation for other parameters
~SessionImpl();
/// Destroys the SessionImpl.
void setConnectionTimeout(std::size_t aTimeout);
/// Sets the session connection timeout value.
std::size_t getConnectionTimeout() const;
/// Returns the session connection timeout value.
void open(const std::string& aConnectionString = std::string());
/// Opens a connection to the database.
void close();
/// Closes the connection.
void reset();
/// Do nothing
bool isConnected() const;
/// Returns true if connected, false otherwise.
Poco::Data::StatementImpl::Ptr createStatementImpl();
/// Returns an PostgreSQL StatementImpl
void begin();
/// Starts a transaction
void commit();
/// Commits and ends a transaction
void rollback();
/// Aborts a transaction
bool canTransact() const;
/// Returns true if session has transaction capabilities.
bool isTransaction() const;
/// Returns true iff a transaction is a transaction is in progress, false otherwise.
void setTransactionIsolation(Poco::UInt32 aTI);
/// Sets the transaction isolation level.
Poco::UInt32 getTransactionIsolation() const;
/// Returns the transaction isolation level.
bool hasTransactionIsolation(Poco::UInt32 aTI) const;
/// Returns true iff the transaction isolation level corresponding
/// to the supplied bitmask is supported.
bool isTransactionIsolation(Poco::UInt32 aTI) const;
/// Returns true iff the transaction isolation level corresponds
/// to the supplied bitmask.
void setAutoCommit(const std::string&, bool aValue);
/// Sets autocommit property for the session.
bool isAutoCommit(const std::string& aName = std::string()) const;
/// Returns autocommit property value.
void setAsynchronousCommit(const std::string&, bool aValue);
/// Sets asynchronousCommit property for the session.
bool isAsynchronousCommit(const std::string& aName = std::string()) const;
/// is the connection in Asynchronous commit mode?
SessionHandle& handle();
/// Get handle
const std::string& connectorName() const;
/// Returns the name of the connector.
private:
std::string _connectorName;
mutable SessionHandle _sessionHandle;
std::size_t _timeout;
};
//
// inlines
//
inline bool SessionImpl::canTransact() const
{
return true;
}
inline SessionHandle& SessionImpl::handle()
{
return _sessionHandle;
}
inline const std::string& SessionImpl::connectorName() const
{
return _connectorName;
}
inline bool SessionImpl::isTransactionIsolation(Poco::UInt32 aTI) const
{
return getTransactionIsolation() == aTI;
}
inline std::size_t SessionImpl::getConnectionTimeout() const
{
return _timeout;
}
} } } // namespace Poco::Data::PostgreSQL
#endif // SQL_PostgreSQL_SessionImpl_INCLUDED

View File

@ -0,0 +1,121 @@
//
// StatementExecutor.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: StatementExecutor
//
// Definition of the StatementExecutor class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_StatementExecutor_INCLUDED
#define SQL_PostgreSQL_StatementExecutor_INCLUDED
#include "Poco/Data/PostgreSQL/PostgreSQLException.h"
#include "Poco/Data/PostgreSQL/PostgreSQLTypes.h"
#include "Poco/Data/PostgreSQL/SessionHandle.h"
#include "Poco/Data/MetaColumn.h"
#include <libpq-fe.h>
#include <string>
#include <vector>
namespace Poco {
namespace Data {
namespace PostgreSQL {
class StatementExecutor
/// PostgreSQL statement executor.
{
public:
enum State
{
STMT_INITED,
STMT_COMPILED,
STMT_EXECUTED
};
explicit StatementExecutor(SessionHandle& aSessionHandle);
/// Creates the StatementExecutor.
~StatementExecutor();
/// Destroys the StatementExecutor.
State state() const;
/// Returns the current state.
void prepare(const std::string& aSQLStatement);
/// Prepares the statement for execution.
void bindParams(const InputParameterVector& anInputParameterVector);
/// Binds the params - REQUIRED if the statement has input parameters/placeholders
/// Pointer and list elements must stay valid for the lifetime of the StatementExecutor!
void execute();
/// Executes the statement.
bool fetch();
/// Fetches the data for the current row
std::size_t getAffectedRowCount() const;
/// get the count of rows affected by the statement
std::size_t columnsReturned() const;
/// get the count of columns returned by the statement
const MetaColumn& metaColumn(std::size_t aPosition) const;
/// Returns the reference to the specified metacolumn - 0 based
const OutputParameter& resultColumn(std::size_t aPosition) const;
/// Returns the reference to the specified result - 0 based
operator PGresult* ();
/// Cast operator to native result handle type.
private:
void clearResults();
StatementExecutor(const StatementExecutor&);
StatementExecutor& operator= (const StatementExecutor&);
private:
typedef std::vector<MetaColumn> ColVec;
SessionHandle& _sessionHandle;
State _state;
PGresult* _pResultHandle;
std::string _SQLStatement;
std::string _preparedStatementName; // UUID based to allow multiple prepared statements per transaction.
std::size_t _countPlaceholdersInSQLStatement;
ColVec _resultColumns;
InputParameterVector _inputParameterVector;
OutputParameterVector _outputParameterVector;
std::size_t _currentRow; // current row of the result
std::size_t _affectedRowCount;
};
//
// inlines
//
inline StatementExecutor::operator PGresult* ()
{
return _pResultHandle;
}
} } } // namespace Poco::Data::PostgreSQL
#endif // SQL_PostgreSQL_StatementExecutor_INCLUDED

View File

@ -0,0 +1,81 @@
//
// Utility.h
//
// Library: Data/PostgreSQL
// Package: PostgreSQL
// Module: Utility
//
// Definition of Utility.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_PostgreSQL_Utility_INCLUDED
#define SQL_PostgreSQL_Utility_INCLUDED
#include "Poco/Data/PostgreSQL/PostgreSQL.h"
#include "Poco/Data/PostgreSQL/SessionHandle.h"
#include "Poco/Data/Session.h"
namespace Poco {
namespace Data {
namespace PostgreSQL {
class PostgreSQL_API Utility
/// Various utility functions for PostgreSQL.
{
public:
static std::string serverInfo(SessionHandle* aHandlePtr);
/// Returns server info.
static std::string serverInfo(Poco::Data::Session& aSession);
/// Returns server info.
static int serverVersion(SessionHandle* aHandlePtr);
/// Returns server version.
static int serverVersion(Poco::Data::Session& aSession);
/// Returns server version.
static std::string hostInfo(SessionHandle* aHandlePtr);
/// Returns host info.
static std::string hostInfo(Poco::Data::Session& aSession);
/// Returns host info.
static std::string sessionEncoding(SessionHandle* aHandlePtr);
/// Returns session encoding.
static std::string sessionEncoding(Poco::Data::Session& aSession);
/// Returns session encoding.
static bool hasMicrosecond() { return true; }
/// Rturns true if microseconds are suported.
static SessionHandle* handle(Poco::Data::Session& aSession);
/// Returns native PostgreSQL handle for the session.
};
//
// inlines
//
inline SessionHandle* Utility::handle(Session& aSession)
{
return Poco::AnyCast< SessionHandle* >(aSession.getProperty("handle"));
}
} } } // namespace Poco::Data::PostgreSQL
#endif // SQL_PostgreSQL_Utility_INCLUDED

View File

@ -0,0 +1,24 @@
/*-------------------------------------------------------------------------
*
* libpq-fs.h
* definitions for using Inversion file system routines (ie, large objects)
*
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/include/libpq/libpq-fs.h
*
*-------------------------------------------------------------------------
*/
#ifndef LIBPQ_FS_H
#define LIBPQ_FS_H
/*
* Read/write mode flags for inversion (large object) calls
*/
#define INV_WRITE 0x00020000
#define INV_READ 0x00040000
#endif /* LIBPQ_FS_H */