mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-01 09:24:55 +02:00
1958 lines
68 KiB
C++
1958 lines
68 KiB
C++
//
|
|
// SQLExecutor.cpp
|
|
//
|
|
// $Id: //poco/Main/DataConnectors/ODBC/testsuite/src/SQLExecutor.cpp#14 $
|
|
//
|
|
// 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 "CppUnit/TestCase.h"
|
|
#include "SQLExecutor.h"
|
|
#include "Poco/String.h"
|
|
#include "Poco/Format.h"
|
|
#include "Poco/Tuple.h"
|
|
#include "Poco/Any.h"
|
|
#include "Poco/Exception.h"
|
|
#include "Poco/Data/Common.h"
|
|
#include "Poco/Data/BLOB.h"
|
|
#include "Poco/Data/StatementImpl.h"
|
|
#include "Poco/Data/RecordSet.h"
|
|
#include "Poco/Data/ODBC/Connector.h"
|
|
#include "Poco/Data/ODBC/Utility.h"
|
|
#include "Poco/Data/ODBC/Diagnostics.h"
|
|
#include "Poco/Data/ODBC/Preparation.h"
|
|
#include "Poco/Data/ODBC/ODBCException.h"
|
|
#include "Poco/Data/ODBC/ODBCStatementImpl.h"
|
|
#include <sqltypes.h>
|
|
#include <iostream>
|
|
|
|
|
|
using namespace Poco::Data;
|
|
using Poco::Data::ODBC::Utility;
|
|
using Poco::Data::ODBC::Preparation;
|
|
using Poco::Data::ODBC::ConnectionException;
|
|
using Poco::Data::ODBC::StatementException;
|
|
using Poco::Data::ODBC::DataTruncatedException;
|
|
using Poco::Data::ODBC::StatementDiagnostics;
|
|
using Poco::format;
|
|
using Poco::Tuple;
|
|
using Poco::Any;
|
|
using Poco::AnyCast;
|
|
using Poco::NotFoundException;
|
|
using Poco::InvalidAccessException;
|
|
using Poco::BadCastException;
|
|
using Poco::RangeException;
|
|
|
|
|
|
struct Person
|
|
{
|
|
std::string lastName;
|
|
std::string firstName;
|
|
std::string address;
|
|
int age;
|
|
Person(){age = 0;}
|
|
Person(const std::string& ln, const std::string& fn, const std::string& adr, int a):lastName(ln), firstName(fn), address(adr), age(a)
|
|
{
|
|
}
|
|
bool operator==(const Person& other) const
|
|
{
|
|
return lastName == other.lastName && firstName == other.firstName && address == other.address && age == other.age;
|
|
}
|
|
|
|
bool operator < (const Person& p) const
|
|
{
|
|
if (age < p.age)
|
|
return true;
|
|
if (lastName < p.lastName)
|
|
return true;
|
|
if (firstName < p.firstName)
|
|
return true;
|
|
return (address < p.address);
|
|
}
|
|
|
|
const std::string& operator () () const
|
|
/// This method is required so we can extract data to a map!
|
|
{
|
|
// we choose the lastName as examplary key
|
|
return lastName;
|
|
}
|
|
};
|
|
|
|
|
|
namespace Poco {
|
|
namespace Data {
|
|
|
|
|
|
template <>
|
|
class TypeHandler<Person>
|
|
{
|
|
public:
|
|
static void bind(std::size_t pos, const Person& obj, AbstractBinder* pBinder)
|
|
{
|
|
// the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Address VARCHAR, Age INTEGER(3))
|
|
poco_assert_dbg (pBinder != 0);
|
|
pBinder->bind(pos++, obj.lastName);
|
|
pBinder->bind(pos++, obj.firstName);
|
|
pBinder->bind(pos++, obj.address);
|
|
pBinder->bind(pos++, obj.age);
|
|
}
|
|
|
|
static void prepare(std::size_t pos, const Person& obj, AbstractPreparation* pPrepare)
|
|
{
|
|
// the table is defined as Person (LastName VARCHAR(30), FirstName VARCHAR, Address VARCHAR, Age INTEGER(3))
|
|
poco_assert_dbg (pPrepare != 0);
|
|
pPrepare->prepare(pos++, obj.lastName);
|
|
pPrepare->prepare(pos++, obj.firstName);
|
|
pPrepare->prepare(pos++, obj.address);
|
|
pPrepare->prepare(pos++, obj.age);
|
|
}
|
|
|
|
static std::size_t size()
|
|
{
|
|
return 4;
|
|
}
|
|
|
|
static void extract(std::size_t pos, Person& obj, const Person& defVal, AbstractExtractor* pExt)
|
|
{
|
|
poco_assert_dbg (pExt != 0);
|
|
if (!pExt->extract(pos++, obj.lastName))
|
|
obj.lastName = defVal.lastName;
|
|
if (!pExt->extract(pos++, obj.firstName))
|
|
obj.firstName = defVal.firstName;
|
|
if (!pExt->extract(pos++, obj.address))
|
|
obj.address = defVal.address;
|
|
if (!pExt->extract(pos++, obj.age))
|
|
obj.age = defVal.age;
|
|
}
|
|
|
|
private:
|
|
TypeHandler();
|
|
~TypeHandler();
|
|
TypeHandler(const TypeHandler&);
|
|
TypeHandler& operator=(const TypeHandler&);
|
|
};
|
|
|
|
|
|
} } // namespace Poco::Data
|
|
|
|
|
|
SQLExecutor::SQLExecutor(const std::string& name, Poco::Data::Session* pSession):
|
|
CppUnit::TestCase(name),
|
|
_pSession(pSession)
|
|
{
|
|
}
|
|
|
|
|
|
SQLExecutor::~SQLExecutor()
|
|
{
|
|
}
|
|
|
|
|
|
void SQLExecutor::bareboneODBCTest(const std::string& dbConnString,
|
|
const std::string& tableCreateString,
|
|
SQLExecutor::DataBinding bindMode,
|
|
SQLExecutor::DataExtraction extractMode)
|
|
{
|
|
SQLRETURN rc;
|
|
SQLHENV henv = SQL_NULL_HENV;
|
|
SQLHDBC hdbc = SQL_NULL_HDBC;
|
|
SQLHSTMT hstmt = SQL_NULL_HSTMT;
|
|
|
|
// Environment begin
|
|
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
// Connection begin
|
|
rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
POCO_SQLCHAR connectOutput[512] = {0};
|
|
SQLSMALLINT result;
|
|
rc = SQLDriverConnect(hdbc
|
|
, NULL
|
|
,(POCO_SQLCHAR*) dbConnString.c_str()
|
|
,(SQLSMALLINT) SQL_NTS
|
|
, connectOutput
|
|
, sizeof(connectOutput)
|
|
, &result
|
|
, SQL_DRIVER_NOPROMPT);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
// Statement begin
|
|
rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
std::string sql = "DROP TABLE Test";
|
|
POCO_SQLCHAR* pStr = (POCO_SQLCHAR*) sql.c_str();
|
|
SQLExecDirect(hstmt, pStr, (SQLINTEGER) sql.length());
|
|
//no return code check - ignore drop errors
|
|
|
|
sql = tableCreateString;
|
|
pStr = (POCO_SQLCHAR*) sql.c_str();
|
|
rc = SQLPrepare(hstmt, pStr, (SQLINTEGER) sql.length());
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLExecute(hstmt);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
sql = "INSERT INTO Test VALUES (?,?,?,?,?)";
|
|
pStr = (POCO_SQLCHAR*) sql.c_str();
|
|
rc = SQLPrepare(hstmt, pStr, (SQLINTEGER) sql.length());
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
std::string str[3] = { "111", "222", "333" };
|
|
int fourth = 4;
|
|
float fifth = 1.5;
|
|
SQLLEN li = SQL_NTS;
|
|
SQLINTEGER size = (SQLINTEGER) str[0].size();
|
|
|
|
if (SQLExecutor::PB_AT_EXEC == bindMode)
|
|
li = SQL_LEN_DATA_AT_EXEC(size);
|
|
|
|
rc = SQLBindParameter(hstmt,
|
|
(SQLUSMALLINT) 1,
|
|
SQL_PARAM_INPUT,
|
|
SQL_C_CHAR,
|
|
SQL_VARCHAR,
|
|
(SQLUINTEGER) size,
|
|
0,
|
|
(SQLPOINTER) str[0].c_str(),
|
|
size,
|
|
&li);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
size = (SQLINTEGER) str[1].size();
|
|
if (SQLExecutor::PB_AT_EXEC == bindMode)
|
|
li = SQL_LEN_DATA_AT_EXEC(size);
|
|
|
|
rc = SQLBindParameter(hstmt,
|
|
(SQLUSMALLINT) 2,
|
|
SQL_PARAM_INPUT,
|
|
SQL_C_CHAR,
|
|
SQL_VARCHAR,
|
|
(SQLUINTEGER) size,
|
|
0,
|
|
(SQLPOINTER) str[1].c_str(),
|
|
size,
|
|
&li);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
size = (SQLINTEGER) str[2].size();
|
|
if (SQLExecutor::PB_AT_EXEC == bindMode)
|
|
li = SQL_LEN_DATA_AT_EXEC(size);
|
|
else li = size;
|
|
|
|
rc = SQLBindParameter(hstmt,
|
|
(SQLUSMALLINT) 3,
|
|
SQL_PARAM_INPUT,
|
|
SQL_C_BINARY,
|
|
SQL_LONGVARBINARY,
|
|
(SQLUINTEGER) size,
|
|
0,
|
|
(SQLPOINTER) str[2].data(),
|
|
size,
|
|
&li);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLBindParameter(hstmt,
|
|
(SQLUSMALLINT) 4,
|
|
SQL_PARAM_INPUT,
|
|
SQL_C_SLONG,
|
|
SQL_INTEGER,
|
|
0,
|
|
0,
|
|
(SQLPOINTER) &fourth,
|
|
0,
|
|
0);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLBindParameter(hstmt,
|
|
(SQLUSMALLINT) 5,
|
|
SQL_PARAM_INPUT,
|
|
SQL_C_FLOAT,
|
|
SQL_REAL,
|
|
0,
|
|
1,
|
|
(SQLPOINTER) &fifth,
|
|
0,
|
|
0);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
size = 0;
|
|
if (SQLExecutor::PB_AT_EXEC == bindMode)
|
|
li = SQL_LEN_DATA_AT_EXEC((int) sizeof(int));
|
|
|
|
rc = SQLExecute(hstmt);
|
|
assert (SQL_NEED_DATA == rc || SQL_SUCCEEDED(rc));
|
|
|
|
if (SQL_NEED_DATA == rc)
|
|
{
|
|
SQLPOINTER pParam = 0;
|
|
int i = 0;
|
|
rc = SQLParamData(hstmt, &pParam);
|
|
do
|
|
{
|
|
SQLINTEGER dataSize = 0;
|
|
// Data size should be ignored for non-null,
|
|
// non-variable length fields, but SQLite ODBC
|
|
// driver insists on it always being the actual
|
|
// data length
|
|
if (i < 3)
|
|
dataSize = (SQLINTEGER) str[i++].size();
|
|
else if (3 == i) dataSize = sizeof(int);
|
|
else if (4 == i) dataSize = sizeof(float);
|
|
|
|
rc = SQLPutData(hstmt, pParam, dataSize);
|
|
}while (SQL_NEED_DATA == (rc = SQLParamData(hstmt, &pParam)));
|
|
}
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
sql = "SELECT * FROM Test";
|
|
pStr = (POCO_SQLCHAR*) sql.c_str();
|
|
rc = SQLPrepare(hstmt, pStr, (SQLINTEGER) sql.length());
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
char chr[3][5] = {{ 0 }};
|
|
SQLLEN lengths[5] = { 0 };
|
|
fourth = 0;
|
|
fifth = 0.0f;
|
|
|
|
if (SQLExecutor::DE_BOUND == extractMode)
|
|
{
|
|
rc = SQLBindCol(hstmt,
|
|
(SQLUSMALLINT) 1,
|
|
SQL_C_CHAR,
|
|
(SQLPOINTER) chr[0],
|
|
(SQLINTEGER) 4,
|
|
&lengths[0]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLBindCol(hstmt,
|
|
(SQLUSMALLINT) 2,
|
|
SQL_C_CHAR,
|
|
(SQLPOINTER) chr[1],
|
|
(SQLINTEGER) 4,
|
|
&lengths[1]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLBindCol(hstmt,
|
|
(SQLUSMALLINT) 3,
|
|
SQL_C_BINARY,
|
|
(SQLPOINTER) chr[2],
|
|
(SQLINTEGER) 4,
|
|
&lengths[2]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLBindCol(hstmt,
|
|
(SQLUSMALLINT) 4,
|
|
SQL_C_SLONG,
|
|
(SQLPOINTER) &fourth,
|
|
(SQLINTEGER) 0,
|
|
&lengths[3]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLBindCol(hstmt,
|
|
(SQLUSMALLINT) 5,
|
|
SQL_C_FLOAT,
|
|
(SQLPOINTER) &fifth,
|
|
(SQLINTEGER) 0,
|
|
&lengths[4]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
}
|
|
|
|
rc = SQLExecute(hstmt);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
rc = SQLFetch(hstmt);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
if (SQLExecutor::DE_MANUAL == extractMode)
|
|
{
|
|
rc = SQLGetData(hstmt,
|
|
(SQLUSMALLINT) 1,
|
|
SQL_C_CHAR,
|
|
chr[0],
|
|
4,
|
|
&lengths[0]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLGetData(hstmt,
|
|
(SQLUSMALLINT) 2,
|
|
SQL_C_CHAR,
|
|
chr[1],
|
|
4,
|
|
&lengths[1]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLGetData(hstmt,
|
|
(SQLUSMALLINT) 3,
|
|
SQL_C_BINARY,
|
|
chr[2],
|
|
3,
|
|
&lengths[2]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLGetData(hstmt,
|
|
(SQLUSMALLINT) 4,
|
|
SQL_C_SLONG,
|
|
&fourth,
|
|
4,
|
|
&lengths[3]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLGetData(hstmt,
|
|
(SQLUSMALLINT) 5,
|
|
SQL_C_FLOAT,
|
|
&fifth,
|
|
5,
|
|
&lengths[4]);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
}
|
|
|
|
assert (0 == strncmp("111", chr[0], 3));
|
|
assert (0 == strncmp("222", chr[1], 3));
|
|
assert (0 == strncmp("333", chr[2], 3));
|
|
assert (4 == fourth);
|
|
assert (1.5 == fifth);
|
|
|
|
rc = SQLCloseCursor(hstmt);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
sql = "DROP TABLE Test";
|
|
pStr = (POCO_SQLCHAR*) sql.c_str();
|
|
rc = SQLExecDirect(hstmt, pStr, (SQLINTEGER) sql.length());
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
rc = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
// Connection end
|
|
rc = SQLDisconnect(hdbc);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
rc = SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
|
|
// Environment end
|
|
rc = SQLFreeHandle(SQL_HANDLE_ENV, henv);
|
|
assert (SQL_SUCCEEDED(rc));
|
|
}
|
|
|
|
|
|
void SQLExecutor::simpleAccess()
|
|
{
|
|
std::string funct = "simpleAccess()";
|
|
std::string lastName = "lastName";
|
|
std::string firstName("firstName");
|
|
std::string address("Address");
|
|
int age = 133132;
|
|
int count = 0;
|
|
std::string result;
|
|
|
|
count = 0;
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(lastName), use(firstName), use(address), use(age), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
assert (count == 1);
|
|
|
|
try { *_pSession << "SELECT LastName FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (lastName == result);
|
|
|
|
try { *_pSession << "SELECT Age FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == age);
|
|
}
|
|
|
|
|
|
void SQLExecutor::complexType()
|
|
{
|
|
std::string funct = "complexType()";
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(p1), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(p2), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
Person c1;
|
|
try { *_pSession << "SELECT * FROM PERSON WHERE LastName = 'LN1'", into(c1), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (c1 == p1);
|
|
}
|
|
|
|
|
|
void SQLExecutor::simpleAccessVector()
|
|
{
|
|
std::string funct = "simpleAccessVector()";
|
|
std::vector<std::string> lastNames;
|
|
std::vector<std::string> firstNames;
|
|
std::vector<std::string> addresses;
|
|
std::vector<int> ages;
|
|
std::string tableName("Person");
|
|
lastNames.push_back("LN1");
|
|
lastNames.push_back("LN2");
|
|
firstNames.push_back("FN1");
|
|
firstNames.push_back("FN2");
|
|
addresses.push_back("ADDR1");
|
|
addresses.push_back("ADDR2");
|
|
ages.push_back(1);
|
|
ages.push_back(2);
|
|
int count = 0;
|
|
std::string result;
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(lastNames), use(firstNames), use(addresses), use(ages), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::vector<std::string> lastNamesR;
|
|
std::vector<std::string> firstNamesR;
|
|
std::vector<std::string> addressesR;
|
|
std::vector<int> agesR;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(lastNamesR), into(firstNamesR), into(addressesR), into(agesR), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ages == agesR);
|
|
assert (lastNames == lastNamesR);
|
|
assert (firstNames == firstNamesR);
|
|
assert (addresses == addressesR);
|
|
}
|
|
|
|
|
|
void SQLExecutor::complexTypeVector()
|
|
{
|
|
std::string funct = "complexTypeVector()";
|
|
std::vector<Person> people;
|
|
people.push_back(Person("LN1", "FN1", "ADDR1", 1));
|
|
people.push_back(Person("LN2", "FN2", "ADDR2", 2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::vector<Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result == people);
|
|
}
|
|
|
|
|
|
void SQLExecutor::insertVector()
|
|
{
|
|
std::string funct = "insertVector()";
|
|
std::vector<std::string> str;
|
|
str.push_back("s1");
|
|
str.push_back("s2");
|
|
str.push_back("s3");
|
|
str.push_back("s3");
|
|
int count = 100;
|
|
|
|
{
|
|
Statement stmt((*_pSession << "INSERT INTO Strings VALUES (?)", use(str)));
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 0);
|
|
|
|
try { stmt.execute(); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 4);
|
|
}
|
|
count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 4);
|
|
}
|
|
|
|
|
|
void SQLExecutor::insertEmptyVector()
|
|
{
|
|
std::string funct = "insertEmptyVector()";
|
|
std::vector<std::string> str;
|
|
|
|
try
|
|
{
|
|
*_pSession << "INSERT INTO Strings VALUES (?)", use(str), now;
|
|
fail("empty collections should not work");
|
|
}
|
|
catch (Poco::Exception&)
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::simpleAccessList()
|
|
{
|
|
std::string funct = "simpleAccessList()";
|
|
std::list<std::string> lastNames;
|
|
std::list<std::string> firstNames;
|
|
std::list<std::string> addresses;
|
|
std::list<int> ages;
|
|
std::string tableName("Person");
|
|
lastNames.push_back("LN1");
|
|
lastNames.push_back("LN2");
|
|
firstNames.push_back("FN1");
|
|
firstNames.push_back("FN2");
|
|
addresses.push_back("ADDR1");
|
|
addresses.push_back("ADDR2");
|
|
ages.push_back(1);
|
|
ages.push_back(2);
|
|
int count = 0;
|
|
std::string result;
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(lastNames), use(firstNames), use(addresses), use(ages), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::list<std::string> lastNamesR;
|
|
std::list<std::string> firstNamesR;
|
|
std::list<std::string> addressesR;
|
|
std::list<int> agesR;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(lastNamesR), into(firstNamesR), into(addressesR), into(agesR), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ages == agesR);
|
|
assert (lastNames == lastNamesR);
|
|
assert (firstNames == firstNamesR);
|
|
assert (addresses == addressesR);
|
|
}
|
|
|
|
|
|
void SQLExecutor::complexTypeList()
|
|
{
|
|
std::string funct = "complexTypeList()";
|
|
std::list<Person> people;
|
|
people.push_back(Person("LN1", "FN1", "ADDR1", 1));
|
|
people.push_back(Person("LN2", "FN2", "ADDR2", 2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::list<Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result == people);
|
|
}
|
|
|
|
|
|
void SQLExecutor::insertList()
|
|
{
|
|
std::string funct = "insertList()";
|
|
std::list<std::string> str;
|
|
str.push_back("s1");
|
|
str.push_back("s2");
|
|
str.push_back("s3");
|
|
str.push_back("s3");
|
|
int count = 100;
|
|
|
|
{
|
|
Statement stmt((*_pSession << "INSERT INTO Strings VALUES (?)", use(str)));
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 0);
|
|
|
|
try { stmt.execute(); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 4);
|
|
}
|
|
count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 4);
|
|
}
|
|
|
|
|
|
void SQLExecutor::insertEmptyList()
|
|
{
|
|
std::string funct = "insertEmptyList()";
|
|
std::list<std::string> str;
|
|
|
|
try
|
|
{
|
|
*_pSession << "INSERT INTO Strings VALUES (?)", use(str), now;
|
|
fail("empty collections should not work");
|
|
}
|
|
catch (Poco::Exception&)
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::simpleAccessDeque()
|
|
{
|
|
std::string funct = "simpleAccessDeque()";
|
|
std::deque<std::string> lastNames;
|
|
std::deque<std::string> firstNames;
|
|
std::deque<std::string> addresses;
|
|
std::deque<int> ages;
|
|
std::string tableName("Person");
|
|
lastNames.push_back("LN1");
|
|
lastNames.push_back("LN2");
|
|
firstNames.push_back("FN1");
|
|
firstNames.push_back("FN2");
|
|
addresses.push_back("ADDR1");
|
|
addresses.push_back("ADDR2");
|
|
ages.push_back(1);
|
|
ages.push_back(2);
|
|
int count = 0;
|
|
std::string result;
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(lastNames), use(firstNames), use(addresses), use(ages), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::deque<std::string> lastNamesR;
|
|
std::deque<std::string> firstNamesR;
|
|
std::deque<std::string> addressesR;
|
|
std::deque<int> agesR;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(lastNamesR), into(firstNamesR), into(addressesR), into(agesR), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ages == agesR);
|
|
assert (lastNames == lastNamesR);
|
|
assert (firstNames == firstNamesR);
|
|
assert (addresses == addressesR);
|
|
}
|
|
|
|
|
|
void SQLExecutor::complexTypeDeque()
|
|
{
|
|
std::string funct = "complexTypeDeque()";
|
|
std::deque<Person> people;
|
|
people.push_back(Person("LN1", "FN1", "ADDR1", 1));
|
|
people.push_back(Person("LN2", "FN2", "ADDR2", 2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::deque<Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result == people);
|
|
}
|
|
|
|
|
|
void SQLExecutor::insertDeque()
|
|
{
|
|
std::string funct = "insertDeque()";
|
|
std::deque<std::string> str;
|
|
str.push_back("s1");
|
|
str.push_back("s2");
|
|
str.push_back("s3");
|
|
str.push_back("s3");
|
|
int count = 100;
|
|
|
|
{
|
|
Statement stmt((*_pSession << "INSERT INTO Strings VALUES (?)", use(str)));
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 0);
|
|
|
|
try { stmt.execute(); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 4);
|
|
}
|
|
count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 4);
|
|
}
|
|
|
|
|
|
void SQLExecutor::insertEmptyDeque()
|
|
{
|
|
std::string funct = "insertEmptyDeque()";
|
|
std::deque<std::string> str;
|
|
|
|
try
|
|
{
|
|
*_pSession << "INSERT INTO Strings VALUES (?)", use(str), now;
|
|
fail("empty collections should not work");
|
|
}
|
|
catch (Poco::Exception&)
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::insertSingleBulk()
|
|
{
|
|
std::string funct = "insertSingleBulk()";
|
|
int x = 0;
|
|
Statement stmt((*_pSession << "INSERT INTO Strings VALUES (?)", use(x)));
|
|
|
|
for (x = 0; x < 100; ++x)
|
|
{
|
|
int i = stmt.execute();
|
|
assert (i == 0);
|
|
}
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 100);
|
|
|
|
try { *_pSession << "SELECT SUM(str) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == ((0+99)*100/2));
|
|
}
|
|
|
|
|
|
void SQLExecutor::floats()
|
|
{
|
|
std::string funct = "floats()";
|
|
float data = 1.5f;
|
|
float ret = 0.0f;
|
|
|
|
try { *_pSession << "INSERT INTO Strings VALUES (?)", use(data), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 1);
|
|
|
|
try { *_pSession << "SELECT str FROM Strings", into(ret), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ret == data);
|
|
}
|
|
|
|
|
|
void SQLExecutor::doubles()
|
|
{
|
|
std::string funct = "floats()";
|
|
double data = 1.5;
|
|
double ret = 0.0;
|
|
|
|
try { *_pSession << "INSERT INTO Strings VALUES (?)", use(data), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 1);
|
|
|
|
try { *_pSession << "SELECT str FROM Strings", into(ret), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ret == data);
|
|
}
|
|
|
|
|
|
void SQLExecutor::insertSingleBulkVec()
|
|
{
|
|
std::string funct = "insertSingleBulkVec()";
|
|
std::vector<int> data;
|
|
|
|
for (int x = 0; x < 100; ++x)
|
|
data.push_back(x);
|
|
|
|
Statement stmt((*_pSession << "INSERT INTO Strings VALUES (?)", use(data)));
|
|
stmt.execute();
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
assert (count == 100);
|
|
try { *_pSession << "SELECT SUM(str) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == ((0+99)*100/2));
|
|
}
|
|
|
|
|
|
void SQLExecutor::limits()
|
|
{
|
|
std::string funct = "limit()";
|
|
std::vector<int> data;
|
|
for (int x = 0; x < 100; ++x)
|
|
{
|
|
data.push_back(x);
|
|
}
|
|
|
|
try { *_pSession << "INSERT INTO Strings VALUES (?)", use(data), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
std::vector<int> retData;
|
|
try { *_pSession << "SELECT * FROM Strings", into(retData), limit(50), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (retData.size() == 50);
|
|
for (int x = 0; x < 50; ++x)
|
|
{
|
|
assert(data[x] == retData[x]);
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::limitZero()
|
|
{
|
|
std::string funct = "limitZero()";
|
|
std::vector<int> data;
|
|
for (int x = 0; x < 100; ++x)
|
|
{
|
|
data.push_back(x);
|
|
}
|
|
|
|
try { *_pSession << "INSERT INTO Strings VALUES (?)", use(data), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
std::vector<int> retData;
|
|
try { *_pSession << "SELECT * FROM Strings", into(retData), limit(0), now; }// stupid test, but at least we shouldn't crash
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (retData.size() == 0);
|
|
}
|
|
|
|
|
|
void SQLExecutor::limitOnce()
|
|
{
|
|
std::string funct = "limitOnce()";
|
|
std::vector<int> data;
|
|
for (int x = 0; x < 101; ++x)
|
|
{
|
|
data.push_back(x);
|
|
}
|
|
|
|
try { *_pSession << "INSERT INTO Strings VALUES (?)", use(data), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
std::vector<int> retData;
|
|
Statement stmt = (*_pSession << "SELECT * FROM Strings", into(retData), limit(50), now);
|
|
assert (!stmt.done());
|
|
assert (retData.size() == 50);
|
|
stmt.execute();
|
|
assert (!stmt.done());
|
|
assert (retData.size() == 100);
|
|
stmt.execute();
|
|
assert (stmt.done());
|
|
assert (retData.size() == 101);
|
|
|
|
for (int x = 0; x < 101; ++x)
|
|
{
|
|
assert(data[x] == retData[x]);
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::limitPrepare()
|
|
{
|
|
std::string funct = "limitPrepare()";
|
|
std::vector<int> data;
|
|
for (int x = 0; x < 100; ++x)
|
|
{
|
|
data.push_back(x);
|
|
}
|
|
|
|
try { *_pSession << "INSERT INTO Strings VALUES (?)", use(data), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
std::vector<int> retData;
|
|
Statement stmt = (*_pSession << "SELECT * FROM Strings", into(retData), limit(50));
|
|
assert (retData.size() == 0);
|
|
assert (!stmt.done());
|
|
|
|
try { stmt.execute(); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (!stmt.done());
|
|
assert (retData.size() == 50);
|
|
|
|
try { stmt.execute(); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (stmt.done());
|
|
assert (retData.size() == 100);
|
|
|
|
try { stmt.execute(); }// will restart execution!
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
assert (!stmt.done());
|
|
assert (retData.size() == 150);
|
|
for (int x = 0; x < 150; ++x)
|
|
{
|
|
assert(data[x%100] == retData[x]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void SQLExecutor::prepare()
|
|
{
|
|
std::string funct = "prepare()";
|
|
std::vector<int> data;
|
|
for (int x = 0; x < 100; x += 2)
|
|
{
|
|
data.push_back(x);
|
|
}
|
|
|
|
{
|
|
Statement stmt((*_pSession << "INSERT INTO Strings VALUES (?)", use(data)));
|
|
}
|
|
// stmt should not have been executed when destroyed
|
|
int count = 100;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Strings", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 0);
|
|
}
|
|
|
|
|
|
void SQLExecutor::setSimple()
|
|
{
|
|
std::string funct = "setSimple()";
|
|
std::set<std::string> lastNames;
|
|
std::set<std::string> firstNames;
|
|
std::set<std::string> addresses;
|
|
std::set<int> ages;
|
|
std::string tableName("Person");
|
|
lastNames.insert("LN1");
|
|
lastNames.insert("LN2");
|
|
firstNames.insert("FN1");
|
|
firstNames.insert("FN2");
|
|
addresses.insert("ADDR1");
|
|
addresses.insert("ADDR2");
|
|
ages.insert(1);
|
|
ages.insert(2);
|
|
int count = 0;
|
|
std::string result;
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(lastNames), use(firstNames), use(addresses), use(ages), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::set<std::string> lastNamesR;
|
|
std::set<std::string> firstNamesR;
|
|
std::set<std::string> addressesR;
|
|
std::set<int> agesR;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(lastNamesR), into(firstNamesR), into(addressesR), into(agesR), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ages == agesR);
|
|
assert (lastNames == lastNamesR);
|
|
assert (firstNames == firstNamesR);
|
|
assert (addresses == addressesR);
|
|
}
|
|
|
|
|
|
void SQLExecutor::setComplex()
|
|
{
|
|
std::string funct = "setComplex()";
|
|
std::set<Person> people;
|
|
people.insert(Person("LN1", "FN1", "ADDR1", 1));
|
|
people.insert(Person("LN2", "FN2", "ADDR2", 2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::set<Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result == people);
|
|
}
|
|
|
|
|
|
void SQLExecutor::setComplexUnique()
|
|
{
|
|
std::string funct = "setComplexUnique()";
|
|
std::vector<Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
people.push_back(p1);
|
|
people.push_back(p1);
|
|
people.push_back(p1);
|
|
people.push_back(p1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.push_back(p2);
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 5);
|
|
|
|
std::set<Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result.size() == 2);
|
|
assert (*result.begin() == p1);
|
|
assert (*++result.begin() == p2);
|
|
}
|
|
|
|
void SQLExecutor::multiSetSimple()
|
|
{
|
|
std::string funct = "multiSetSimple()";
|
|
std::multiset<std::string> lastNames;
|
|
std::multiset<std::string> firstNames;
|
|
std::multiset<std::string> addresses;
|
|
std::multiset<int> ages;
|
|
std::string tableName("Person");
|
|
lastNames.insert("LN1");
|
|
lastNames.insert("LN2");
|
|
firstNames.insert("FN1");
|
|
firstNames.insert("FN2");
|
|
addresses.insert("ADDR1");
|
|
addresses.insert("ADDR2");
|
|
ages.insert(1);
|
|
ages.insert(2);
|
|
int count = 0;
|
|
std::string result;
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(lastNames), use(firstNames), use(addresses), use(ages), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::multiset<std::string> lastNamesR;
|
|
std::multiset<std::string> firstNamesR;
|
|
std::multiset<std::string> addressesR;
|
|
std::multiset<int> agesR;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(lastNamesR), into(firstNamesR), into(addressesR), into(agesR), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ages.size() == agesR.size());
|
|
assert (lastNames.size() == lastNamesR.size());
|
|
assert (firstNames.size() == firstNamesR.size());
|
|
assert (addresses.size() == addressesR.size());
|
|
}
|
|
|
|
|
|
void SQLExecutor::multiSetComplex()
|
|
{
|
|
std::string funct = "multiSetComplex()";
|
|
std::multiset<Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
people.insert(p1);
|
|
people.insert(p1);
|
|
people.insert(p1);
|
|
people.insert(p1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(p2);
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 5);
|
|
|
|
std::multiset<Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result.size() == people.size());
|
|
}
|
|
|
|
|
|
void SQLExecutor::mapComplex()
|
|
{
|
|
std::string funct = "mapComplex()";
|
|
std::map<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN2", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
|
|
std::map<std::string, Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result == people);
|
|
}
|
|
|
|
|
|
void SQLExecutor::mapComplexUnique()
|
|
{
|
|
std::string funct = "mapComplexUnique()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN2", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 5);
|
|
|
|
std::map<std::string, Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result.size() == 2);
|
|
}
|
|
|
|
|
|
void SQLExecutor::multiMapComplex()
|
|
{
|
|
std::string funct = "multiMapComplex()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN2", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 5);
|
|
|
|
std::multimap<std::string, Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result.size() == people.size());
|
|
}
|
|
|
|
|
|
void SQLExecutor::selectIntoSingle()
|
|
{
|
|
std::string funct = "selectIntoSingle()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
Person result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), limit(1), now; }// will return 1 object into one single result
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result == p1);
|
|
}
|
|
|
|
|
|
void SQLExecutor::selectIntoSingleStep()
|
|
{
|
|
std::string funct = "selectIntoSingleStep()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
Person result;
|
|
Statement stmt = (*_pSession << "SELECT * FROM PERSON", into(result), limit(1));
|
|
stmt.execute();
|
|
assert (result == p1);
|
|
assert (!stmt.done());
|
|
stmt.execute();
|
|
assert (result == p2);
|
|
assert (stmt.done());
|
|
}
|
|
|
|
|
|
void SQLExecutor::selectIntoSingleFail()
|
|
{
|
|
std::string funct = "selectIntoSingleFail()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), limit(2, true), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
Person result;
|
|
try
|
|
{
|
|
*_pSession << "SELECT * FROM PERSON", into(result), limit(1, true), now; // will fail now
|
|
fail("hardLimit is set: must fail");
|
|
}
|
|
catch(Poco::Data::LimitException&)
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::lowerLimitOk()
|
|
{
|
|
std::string funct = "lowerLimitOk()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
Person result;
|
|
try
|
|
{
|
|
*_pSession << "SELECT * FROM PERSON", into(result), lowerLimit(2), now; // will return 2 objects into one single result but only room for one!
|
|
fail("Not enough space for results");
|
|
}
|
|
catch(Poco::Exception&)
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::singleSelect()
|
|
{
|
|
std::string funct = "singleSelect()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
Person result;
|
|
Statement stmt = (*_pSession << "SELECT * FROM PERSON", into(result), limit(1));
|
|
stmt.execute();
|
|
assert (result == p1);
|
|
assert (!stmt.done());
|
|
stmt.execute();
|
|
assert (result == p2);
|
|
assert (stmt.done());
|
|
}
|
|
|
|
|
|
void SQLExecutor::lowerLimitFail()
|
|
{
|
|
std::string funct = "lowerLimitFail()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
Person result;
|
|
try
|
|
{
|
|
*_pSession << "SELECT * FROM PERSON", into(result), lowerLimit(3), now; // will fail
|
|
fail("should fail. not enough data");
|
|
}
|
|
catch(Poco::Exception&)
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::combinedLimits()
|
|
{
|
|
std::string funct = "combinedLimits()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
std::vector <Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), lowerLimit(2), upperLimit(2), now; }// will return 2 objects
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result.size() == 2);
|
|
assert (result[0] == p1);
|
|
assert (result[1] == p2);
|
|
}
|
|
|
|
|
|
|
|
void SQLExecutor::ranges()
|
|
{
|
|
std::string funct = "range()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
std::vector <Person> result;
|
|
try { *_pSession << "SELECT * FROM PERSON", into(result), range(2, 2), now; }// will return 2 objects
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (result.size() == 2);
|
|
assert (result[0] == p1);
|
|
assert (result[1] == p2);
|
|
}
|
|
|
|
|
|
void SQLExecutor::combinedIllegalLimits()
|
|
{
|
|
std::string funct = "combinedIllegalLimits()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
Person result;
|
|
try
|
|
{
|
|
*_pSession << "SELECT * FROM PERSON", into(result), lowerLimit(3), upperLimit(2), now;
|
|
fail("lower > upper is not allowed");
|
|
}
|
|
catch(LimitException&)
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::illegalRange()
|
|
{
|
|
std::string funct = "illegalRange()";
|
|
std::multimap<std::string, Person> people;
|
|
Person p1("LN1", "FN1", "ADDR1", 1);
|
|
Person p2("LN2", "FN2", "ADDR2", 2);
|
|
people.insert(std::make_pair("LN1", p1));
|
|
people.insert(std::make_pair("LN1", p2));
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(people), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 2);
|
|
Person result;
|
|
try
|
|
{
|
|
*_pSession << "SELECT * FROM PERSON", into(result), range(3, 2), now;
|
|
fail("lower > upper is not allowed");
|
|
}
|
|
catch(LimitException&)
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
void SQLExecutor::emptyDB()
|
|
{
|
|
std::string funct = "emptyDB()";
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 0);
|
|
|
|
Person result;
|
|
Statement stmt = (*_pSession << "SELECT * FROM PERSON", into(result), limit(1));
|
|
stmt.execute();
|
|
assert (result.firstName.empty());
|
|
assert (stmt.done());
|
|
}
|
|
|
|
|
|
void SQLExecutor::blob(int bigSize)
|
|
{
|
|
std::string funct = "blob()";
|
|
std::string lastName("lastname");
|
|
std::string firstName("firstname");
|
|
std::string address("Address");
|
|
|
|
BLOB img("0123456789", 10);
|
|
int count = 0;
|
|
try { *_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(lastName), use(firstName), use(address), use(img), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 1);
|
|
|
|
BLOB res;
|
|
assert (res.size() == 0);
|
|
try { *_pSession << "SELECT Image FROM Person", into(res), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (res == img);
|
|
|
|
BLOB big;
|
|
std::vector<char> v(bigSize, 'x');
|
|
big.assignRaw(&v[0], v.size());
|
|
|
|
assert (big.size() == bigSize);
|
|
|
|
try { *_pSession << "DELETE FROM PERSON", now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "INSERT INTO PERSON VALUES(?,?,?,?)", use(lastName), use(firstName), use(address), use(big), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try { *_pSession << "SELECT Image FROM Person", into(res), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (res == big);
|
|
}
|
|
|
|
|
|
void SQLExecutor::blobStmt()
|
|
{
|
|
std::string funct = "blobStmt()";
|
|
std::string lastName("lastname");
|
|
std::string firstName("firstname");
|
|
std::string address("Address");
|
|
BLOB blob("0123456789", 10);
|
|
|
|
int count = 0;
|
|
Statement ins = (*_pSession << "INSERT INTO PERSON VALUES (?,?,?,?)", use(lastName), use(firstName), use(address), use(blob));
|
|
ins.execute();
|
|
try { *_pSession << "SELECT COUNT(*) FROM PERSON", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (count == 1);
|
|
|
|
BLOB res;
|
|
poco_assert (res.size() == 0);
|
|
Statement stmt = (*_pSession << "SELECT Image FROM Person", into(res));
|
|
try { stmt.execute(); }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
poco_assert (res == blob);
|
|
}
|
|
|
|
void SQLExecutor::tuples()
|
|
{
|
|
typedef Tuple<int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int> TupleType;
|
|
std::string funct = "tuples()";
|
|
TupleType t(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
|
|
|
|
try { *_pSession << "INSERT INTO Tuples VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", use(t), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
TupleType ret(-10,-11,-12,-13,-14,-15,-16,-17,-18,-19);
|
|
assert (ret != t);
|
|
try { *_pSession << "SELECT * FROM Tuples", into(ret), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ret == t);
|
|
}
|
|
|
|
void SQLExecutor::tupleVector()
|
|
{
|
|
typedef Tuple<int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int> TupleType;
|
|
std::string funct = "tupleVector()";
|
|
TupleType t(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
|
|
Tuple<int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int>
|
|
t10(10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29);
|
|
TupleType t100(100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119);
|
|
std::vector<TupleType> v;
|
|
v.push_back(t);
|
|
v.push_back(t10);
|
|
v.push_back(t100);
|
|
|
|
try { *_pSession << "INSERT INTO Tuples VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", use(v), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
int count = 0;
|
|
try { *_pSession << "SELECT COUNT(*) FROM Tuples", into(count), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (v.size() == count);
|
|
|
|
std::vector<Tuple<int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int> > ret;
|
|
try { *_pSession << "SELECT * FROM Tuples", into(ret), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
assert (ret == v);
|
|
}
|
|
|
|
|
|
void SQLExecutor::internalExtraction()
|
|
{
|
|
std::string funct = "internalExtraction()";
|
|
std::vector<Tuple<int, double, std::string> > v;
|
|
v.push_back(Tuple<int, double, std::string>(1, 1.5f, "3"));
|
|
v.push_back(Tuple<int, double, std::string>(2, 2.5f, "4"));
|
|
v.push_back(Tuple<int, double, std::string>(3, 3.5f, "5"));
|
|
v.push_back(Tuple<int, double, std::string>(4, 4.5f, "6"));
|
|
|
|
try { *_pSession << "INSERT INTO Vectors VALUES (?,?,?)", use(v), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try
|
|
{
|
|
Statement stmt = (*_pSession << "SELECT * FROM Vectors", now);
|
|
RecordSet rset(stmt);
|
|
|
|
assert (3 == rset.columnCount());
|
|
assert (4 == rset.rowCount());
|
|
|
|
int curVal = 3;
|
|
do
|
|
{
|
|
assert (rset["str0"] == curVal);
|
|
++curVal;
|
|
} while (rset.moveNext());
|
|
|
|
rset.moveFirst();
|
|
assert (rset["str0"] == "3");
|
|
rset.moveLast();
|
|
assert (rset["str0"] == "6");
|
|
|
|
RecordSet rset2(rset);
|
|
assert (3 == rset2.columnCount());
|
|
assert (4 == rset2.rowCount());
|
|
|
|
int i = rset.value<int>(0,0);
|
|
assert (1 == i);
|
|
|
|
std::string s = rset.value(0,0);
|
|
assert ("1" == s);
|
|
|
|
int a = rset.value<int>(0,2);
|
|
assert (3 == a);
|
|
|
|
try
|
|
{
|
|
double d = rset.value<double>(1,1);
|
|
assert (2.5 == d);
|
|
}
|
|
catch (BadCastException&)
|
|
{
|
|
float f = rset.value<float>(1,1);
|
|
assert (2.5 == f);
|
|
}
|
|
|
|
s = rset.value<std::string>(2,2);
|
|
assert ("5" == s);
|
|
i = rset.value("str0", 2);
|
|
assert (5 == i);
|
|
|
|
const Column<int>& col = rset.column<int, std::vector<int> >(0);
|
|
Column<int>::Iterator it = col.begin();
|
|
Column<int>::Iterator end = col.end();
|
|
for (int i = 1; it != end; ++it, ++i)
|
|
assert (*it == i);
|
|
|
|
rset = (*_pSession << "SELECT COUNT(*) AS cnt FROM Vectors", now);
|
|
|
|
//various results for COUNT(*) are received from different drivers
|
|
try
|
|
{
|
|
//this is what most drivers will return
|
|
int i = rset.value<int>(0,0);
|
|
assert (4 == i);
|
|
}
|
|
catch(BadCastException&)
|
|
{
|
|
try
|
|
{
|
|
//this is for Oracle
|
|
double i = rset.value<double>(0,0);
|
|
assert (4 == int(i));
|
|
}
|
|
catch(BadCastException&)
|
|
{
|
|
//this is for PostgreSQL
|
|
Poco::Int64 big = rset.value<Poco::Int64>(0,0);
|
|
assert (4 == big);
|
|
}
|
|
}
|
|
|
|
s = rset.value("cnt", 0).convert<std::string>();
|
|
assert ("4" == s);
|
|
|
|
try { rset.column<int, std::vector<int> >(100); fail ("must fail"); }
|
|
catch (RangeException&) { }
|
|
|
|
try { rset.value<std::string>(0,0); fail ("must fail"); }
|
|
catch (BadCastException&) { }
|
|
|
|
stmt = (*_pSession << "DELETE FROM Vectors", now);
|
|
rset = stmt;
|
|
|
|
try { rset.column<int, std::vector<int> >(0); fail ("must fail"); }
|
|
catch (RangeException&) { }
|
|
}
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
}
|
|
|
|
|
|
void SQLExecutor::internalStorageType()
|
|
{
|
|
std::string funct = "internalStorageType()";
|
|
std::vector<Statement::Manipulator> manips;
|
|
manips.push_back(list);
|
|
manips.push_back(deque);
|
|
manips.push_back(vector);
|
|
|
|
std::vector<Tuple<int, double, std::string> > v;
|
|
v.push_back(Tuple<int, double, std::string>(1, 1.5f, "3"));
|
|
v.push_back(Tuple<int, double, std::string>(2, 2.5f, "4"));
|
|
v.push_back(Tuple<int, double, std::string>(3, 3.5f, "5"));
|
|
v.push_back(Tuple<int, double, std::string>(4, 4.5f, "6"));
|
|
|
|
try { *_pSession << "INSERT INTO Vectors VALUES (?,?,?)", use(v), now; }
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
|
|
try
|
|
{
|
|
std::vector<Statement::Manipulator>::iterator it = manips.begin();
|
|
std::vector<Statement::Manipulator>::iterator end = manips.end();
|
|
|
|
for (; it != end; ++it)
|
|
{
|
|
Statement stmt = (*_pSession << "SELECT * FROM Vectors", *it, now);
|
|
RecordSet rset(stmt);
|
|
|
|
assert (3 == rset.columnCount());
|
|
assert (4 == rset.rowCount());
|
|
|
|
int curVal = 3;
|
|
do
|
|
{
|
|
assert (rset["str0"] == curVal);
|
|
++curVal;
|
|
} while (rset.moveNext());
|
|
|
|
rset.moveFirst();
|
|
assert (rset["str0"] == "3");
|
|
rset.moveLast();
|
|
assert (rset["str0"] == "6");
|
|
|
|
try
|
|
{
|
|
stmt = (*_pSession << "SELECT * FROM Vectors", now, *it);
|
|
fail ("must fail");
|
|
}
|
|
catch(InvalidAccessException&){}
|
|
|
|
try
|
|
{
|
|
stmt = (*_pSession << "SELECT * FROM Vectors", into(v), now, *it);
|
|
fail ("must fail");
|
|
}
|
|
catch(InvalidAccessException&){}
|
|
|
|
try
|
|
{
|
|
stmt = (*_pSession << "SELECT * FROM Vectors", into(v), *it, now);
|
|
fail ("must fail");
|
|
}
|
|
catch(InvalidAccessException&){}
|
|
}
|
|
}
|
|
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
|
|
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
|
|
}
|