added DateTime to samples

added DateTime to samples
fixed TypeHandler::prepare for AutoPtr and SharedPtr
This commit is contained in:
aleks-f 2012-12-06 22:35:08 -06:00
parent 7327be4735
commit ec84a9a8c6
5 changed files with 46 additions and 28 deletions

View File

@ -14,6 +14,7 @@ Release 1.5.0 (2012-12-17)
- fixed GH #16: NetworkInterface::firstAddress() should not throw on unconfigured interfaces - fixed GH #16: NetworkInterface::firstAddress() should not throw on unconfigured interfaces
- Android compile/build support (by Rangel Reale) - Android compile/build support (by Rangel Reale)
- TypeHandler::prepare() now takes const-reference - TypeHandler::prepare() now takes const-reference
- fixed GH #27: Poco::URI::decode() doesn't properly handle '+'
Release 1.5.0 (2012-10-14) Release 1.5.0 (2012-10-14)
========================== ==========================

View File

@ -1991,7 +1991,6 @@ private:
}; };
template <class K, class V> template <class K, class V>
class TypeHandler<std::pair<K, V> >: public AbstractTypeHandler class TypeHandler<std::pair<K, V> >: public AbstractTypeHandler
{ {
@ -2055,12 +2054,10 @@ public:
TypeHandler<T>::extract(pos, *obj, *obj, pExt); TypeHandler<T>::extract(pos, *obj, *obj, pExt);
} }
static void prepare(std::size_t pos, const Poco::AutoPtr<T>& obj, AbstractPreparator* pPreparator) static void prepare(std::size_t pos, const Poco::AutoPtr<T>&, AbstractPreparator* pPreparator)
{ {
poco_assert_dbg (pPreparator != 0); poco_assert_dbg (pPreparator != 0);
if (!obj) TypeHandler<T>::prepare(pos, T(), pPreparator);
obj = new T();
TypeHandler<T>::prepare(pos, *obj, pPreparator);
} }
private: private:
@ -2097,11 +2094,10 @@ public:
TypeHandler<T>::extract(pos, *obj, *obj, pExt); TypeHandler<T>::extract(pos, *obj, *obj, pExt);
} }
static void prepare(std::size_t pos, const Poco::SharedPtr<T>& obj, AbstractPreparator* pPreparator) static void prepare(std::size_t pos, const Poco::SharedPtr<T>&, AbstractPreparator* pPreparator)
{ {
poco_assert_dbg (pPreparator != 0); poco_assert_dbg (pPreparator != 0);
// *obj will trigger a nullpointer exception if empty TypeHandler<T>::prepare(pos, T(), pPreparator);
TypeHandler<T>::prepare(pos, *obj, pPreparator);
} }
private: private:

View File

@ -32,6 +32,7 @@
#include "Poco/SharedPtr.h" #include "Poco/SharedPtr.h"
#include "Poco/DateTime.h"
#include "Poco/Data/SessionFactory.h" #include "Poco/Data/SessionFactory.h"
#include "Poco/Data/Session.h" #include "Poco/Data/Session.h"
#include "Poco/Data/RecordSet.h" #include "Poco/Data/RecordSet.h"
@ -41,6 +42,7 @@
using namespace Poco::Data::Keywords; using namespace Poco::Data::Keywords;
using Poco::DateTime;
using Poco::Data::Session; using Poco::Data::Session;
using Poco::Data::Statement; using Poco::Data::Statement;
using Poco::Data::RecordSet; using Poco::Data::RecordSet;
@ -66,11 +68,11 @@ int main(int argc, char** argv)
session << "DROP TABLE IF EXISTS Person", now; session << "DROP TABLE IF EXISTS Person", now;
// (re)create table // (re)create table
session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))", now; session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE)", now;
// insert some rows // insert some rows
session << "INSERT INTO Person VALUES('Bart Simpson', 'Springfield', 12)", now; session << "INSERT INTO Person VALUES('Bart Simpson', 'Springfield', 12, ?)", use(DateTime(1980, 4, 1)), now;
session << "INSERT INTO Person VALUES('Lisa Simpson', 'Springfield', 10)", now; session << "INSERT INTO Person VALUES('Lisa Simpson', 'Springfield', 10, ?)", use(DateTime(1982, 5, 9)), now;
// a simple query // a simple query
Statement select(session); Statement select(session);

View File

@ -33,6 +33,7 @@
#include "Poco/SharedPtr.h" #include "Poco/SharedPtr.h"
#include "Poco/DateTime.h"
#include "Poco/Data/SessionFactory.h" #include "Poco/Data/SessionFactory.h"
#include "Poco/Data/Session.h" #include "Poco/Data/Session.h"
#include "Poco/Data/Statement.h" #include "Poco/Data/Statement.h"
@ -43,6 +44,7 @@
using namespace Poco::Data::Keywords; using namespace Poco::Data::Keywords;
using Poco::DateTime;
using Poco::Data::Session; using Poco::Data::Session;
using Poco::Data::Statement; using Poco::Data::Statement;
using Poco::Data::RecordSet; using Poco::Data::RecordSet;
@ -111,13 +113,13 @@ int main(int argc, char** argv)
session << "DROP TABLE IF EXISTS Simpsons", now; session << "DROP TABLE IF EXISTS Simpsons", now;
// (re)create table // (re)create table
session << "CREATE TABLE Simpsons (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))", now; session << "CREATE TABLE Simpsons (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE)", now;
// insert some rows // insert some rows
session << "INSERT INTO Simpsons VALUES('Homer Simpson', 'Springfield', 42)", now; session << "INSERT INTO Simpsons VALUES('Homer Simpson', 'Springfield', 42, ?)", use(DateTime(1956, 3, 1)), now;
session << "INSERT INTO Simpsons VALUES('Marge Simpson', 'Springfield', 38)", now; session << "INSERT INTO Simpsons VALUES('Marge Simpson', 'Springfield', 38, ?)", use(DateTime(1954, 10, 1)), now;
session << "INSERT INTO Simpsons VALUES('Bart Simpson', 'Springfield', 12)", now; session << "INSERT INTO Simpsons VALUES('Bart Simpson', 'Springfield', 12, ?)", use(DateTime(1980, 4, 1)), now;
session << "INSERT INTO Simpsons VALUES('Lisa Simpson', 'Springfield', 10)", now; session << "INSERT INTO Simpsons VALUES('Lisa Simpson', 'Springfield', 10, ?)", use(DateTime(1982, 5, 9)), now;
// create a statement and print the column names and data as HTML table // create a statement and print the column names and data as HTML table
HTMLTableFormatter tf; HTMLTableFormatter tf;

View File

@ -32,6 +32,7 @@
#include "Poco/SharedPtr.h" #include "Poco/SharedPtr.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/Data/SessionFactory.h" #include "Poco/Data/SessionFactory.h"
#include "Poco/Data/Session.h" #include "Poco/Data/Session.h"
#include "Poco/Data/TypeHandler.h" #include "Poco/Data/TypeHandler.h"
@ -41,15 +42,18 @@
using namespace Poco::Data::Keywords; using namespace Poco::Data::Keywords;
using Poco::DateTime;
using Poco::DateTimeFormatter;
using Poco::Data::Session; using Poco::Data::Session;
using Poco::Data::Statement; using Poco::Data::Statement;
struct Person struct Person
{ {
std::string name; std::string name;
std::string address; std::string address;
int age; int age;
DateTime birthday;
}; };
@ -73,6 +77,7 @@ public:
TypeHandler<std::string>::bind(pos++, person.name, pBinder, dir); TypeHandler<std::string>::bind(pos++, person.name, pBinder, dir);
TypeHandler<std::string>::bind(pos++, person.address, pBinder, dir); TypeHandler<std::string>::bind(pos++, person.address, pBinder, dir);
TypeHandler<int>::bind(pos++, person.age, pBinder, dir); TypeHandler<int>::bind(pos++, person.age, pBinder, dir);
TypeHandler<DateTime>::bind(pos++, person.birthday, pBinder, dir);
} }
static void extract(std::size_t pos, Person& person, const Person& deflt, AbstractExtractor* pExtr) static void extract(std::size_t pos, Person& person, const Person& deflt, AbstractExtractor* pExtr)
@ -80,6 +85,7 @@ public:
TypeHandler<std::string>::extract(pos++, person.name, deflt.name, pExtr); TypeHandler<std::string>::extract(pos++, person.name, deflt.name, pExtr);
TypeHandler<std::string>::extract(pos++, person.address, deflt.address, pExtr); TypeHandler<std::string>::extract(pos++, person.address, deflt.address, pExtr);
TypeHandler<int>::extract(pos++, person.age, deflt.age, pExtr); TypeHandler<int>::extract(pos++, person.age, deflt.age, pExtr);
TypeHandler<DateTime>::extract(pos++, person.birthday, deflt.birthday, pExtr);
} }
static void prepare(std::size_t pos, const Person& person, AbstractPreparator* pPrep) static void prepare(std::size_t pos, const Person& person, AbstractPreparator* pPrep)
@ -87,6 +93,7 @@ public:
TypeHandler<std::string>::prepare(pos++, person.name, pPrep); TypeHandler<std::string>::prepare(pos++, person.name, pPrep);
TypeHandler<std::string>::prepare(pos++, person.address, pPrep); TypeHandler<std::string>::prepare(pos++, person.address, pPrep);
TypeHandler<int>::prepare(pos++, person.age, pPrep); TypeHandler<int>::prepare(pos++, person.age, pPrep);
TypeHandler<DateTime>::prepare(pos++, person.birthday, pPrep);
} }
}; };
@ -106,49 +113,59 @@ int main(int argc, char** argv)
session << "DROP TABLE IF EXISTS Person", now; session << "DROP TABLE IF EXISTS Person", now;
// (re)create table // (re)create table
session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))", now; session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE)", now;
// insert some rows // insert some rows
Person person = Person person =
{ {
"Bart Simpson", "Bart Simpson",
"Springfield", "Springfield",
12 10,
DateTime(1980, 4, 1)
}; };
Statement insert(session); Statement insert(session);
insert << "INSERT INTO Person VALUES(?, ?, ?)", insert << "INSERT INTO Person VALUES(?, ?, ?, ?)",
use(person); use(person);
insert.execute(); insert.execute();
person.name = "Lisa Simpson"; person.name = "Lisa Simpson";
person.address = "Springfield"; person.address = "Springfield";
person.age = 10; person.age = 8;
person.birthday = DateTime(1982, 5, 9);
insert.execute(); insert.execute();
// a simple query // a simple query
Statement select(session); Statement select(session);
select << "SELECT Name, Address, Age FROM Person", select << "SELECT Name, Address, Age, Birthday FROM Person",
into(person), into(person),
range(0, 1); // iterate over result set one row at a time range(0, 1); // iterate over result set one row at a time
while (!select.done()) while (!select.done())
{ {
select.execute(); select.execute();
std::cout << person.name << " " << person.address << " " << person.age << std::endl; std::cout << person.name << "\t"
<< person.address << "\t"
<< person.age << "\t"
<< DateTimeFormatter::format(person.birthday, "%b %d %Y")
<< std::endl;
} }
// another query - store the result in a container // another query - store the result in a container
std::vector<Person> persons; std::vector<Person> persons;
session << "SELECT Name, Address, Age FROM Person", session << "SELECT Name, Address, Age, Birthday FROM Person",
into(persons), into(persons),
now; now;
for (std::vector<Person>::const_iterator it = persons.begin(); it != persons.end(); ++it) for (std::vector<Person>::const_iterator it = persons.begin(); it != persons.end(); ++it)
{ {
std::cout << it->name << " " << it->address << " " << it->age << std::endl; std::cout << it->name << "\t"
<< it->address << "\t"
<< it->age << "\t"
<< DateTimeFormatter::format(it->birthday, "%b %d %Y")
<< std::endl;
} }
return 0; return 0;