3232
3333
3434#include " Poco/SharedPtr.h"
35+ #include " Poco/DateTimeFormatter.h"
3536#include " Poco/Data/SessionFactory.h"
3637#include " Poco/Data/Session.h"
3738#include " Poco/Data/TypeHandler.h"
4142
4243
4344using namespace Poco ::Data::Keywords;
45+ using Poco::DateTime;
46+ using Poco::DateTimeFormatter;
4447using Poco::Data::Session;
4548using Poco::Data::Statement;
4649
4750
4851struct Person
4952{
50- std::string name;
51- std::string address;
52- int age;
53+ std::string name;
54+ std::string address;
55+ int age;
56+ DateTime birthday;
5357};
5458
5559
@@ -73,20 +77,23 @@ class TypeHandler<Person>
7377 TypeHandler<std::string>::bind (pos++, person.name , pBinder, dir);
7478 TypeHandler<std::string>::bind (pos++, person.address , pBinder, dir);
7579 TypeHandler<int >::bind (pos++, person.age , pBinder, dir);
80+ TypeHandler<DateTime>::bind (pos++, person.birthday , pBinder, dir);
7681 }
7782
7883 static void extract (std::size_t pos, Person& person, const Person& deflt, AbstractExtractor* pExtr)
7984 {
8085 TypeHandler<std::string>::extract (pos++, person.name , deflt.name , pExtr);
8186 TypeHandler<std::string>::extract (pos++, person.address , deflt.address , pExtr);
8287 TypeHandler<int >::extract (pos++, person.age , deflt.age , pExtr);
88+ TypeHandler<DateTime>::extract (pos++, person.birthday , deflt.birthday , pExtr);
8389 }
8490
8591 static void prepare (std::size_t pos, const Person& person, AbstractPreparator* pPrep)
8692 {
8793 TypeHandler<std::string>::prepare (pos++, person.name , pPrep);
8894 TypeHandler<std::string>::prepare (pos++, person.address , pPrep);
8995 TypeHandler<int >::prepare (pos++, person.age , pPrep);
96+ TypeHandler<DateTime>::prepare (pos++, person.birthday , pPrep);
9097 }
9198};
9299
@@ -106,49 +113,59 @@ int main(int argc, char** argv)
106113 session << " DROP TABLE IF EXISTS Person" , now;
107114
108115 // (re)create table
109- session << " CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))" , now;
116+ session << " CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE )" , now;
110117
111118 // insert some rows
112119 Person person =
113120 {
114121 " Bart Simpson" ,
115122 " Springfield" ,
116- 12
123+ 10 ,
124+ DateTime (1980 , 4 , 1 )
117125 };
118126
119127 Statement insert (session);
120- insert << " INSERT INTO Person VALUES(?, ?, ?)" ,
128+ insert << " INSERT INTO Person VALUES(?, ?, ?, ? )" ,
121129 use (person);
122130
123131 insert.execute ();
124132
125133 person.name = " Lisa Simpson" ;
126134 person.address = " Springfield" ;
127- person.age = 10 ;
128-
135+ person.age = 8 ;
136+ person.birthday = DateTime (1982 , 5 , 9 );
137+
129138 insert.execute ();
130139
131140 // a simple query
132141 Statement select (session);
133- select << " SELECT Name, Address, Age FROM Person" ,
142+ select << " SELECT Name, Address, Age, Birthday FROM Person" ,
134143 into (person),
135144 range (0 , 1 ); // iterate over result set one row at a time
136145
137146 while (!select.done ())
138147 {
139148 select.execute ();
140- std::cout << person.name << " " << person.address << " " << person.age << std::endl;
149+ std::cout << person.name << " \t "
150+ << person.address << " \t "
151+ << person.age << " \t "
152+ << DateTimeFormatter::format (person.birthday , " %b %d %Y" )
153+ << std::endl;
141154 }
142155
143156 // another query - store the result in a container
144157 std::vector<Person> persons;
145- session << " SELECT Name, Address, Age FROM Person" ,
158+ session << " SELECT Name, Address, Age, Birthday FROM Person" ,
146159 into (persons),
147160 now;
148161
149162 for (std::vector<Person>::const_iterator it = persons.begin (); it != persons.end (); ++it)
150163 {
151- std::cout << it->name << " " << it->address << " " << it->age << std::endl;
164+ std::cout << it->name << " \t "
165+ << it->address << " \t "
166+ << it->age << " \t "
167+ << DateTimeFormatter::format (it->birthday , " %b %d %Y" )
168+ << std::endl;
152169 }
153170
154171 return 0 ;
0 commit comments