📄 sqlexecutor.cpp.svn-base
字号:
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>(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 { const Column<int>& col1 = rset.column<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 { const Column<int>& col1 = rset.column<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); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -