Skip to content

Commit ec84a9a

Browse files
committed
added DateTime to samples
added DateTime to samples fixed TypeHandler::prepare for AutoPtr and SharedPtr
1 parent 7327be4 commit ec84a9a

5 files changed

Lines changed: 46 additions & 28 deletions

File tree

‎CHANGELOG‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Release 1.5.0 (2012-12-17)
1414
- fixed GH #16: NetworkInterface::firstAddress() should not throw on unconfigured interfaces
1515
- Android compile/build support (by Rangel Reale)
1616
- TypeHandler::prepare() now takes const-reference
17+
- fixed GH #27: Poco::URI::decode() doesn't properly handle '+'
1718

1819
Release 1.5.0 (2012-10-14)
1920
==========================

‎Data/include/Poco/Data/TypeHandler.h‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,6 @@ class TypeHandler<Poco::Tuple<T0, NullTypeList> >
19911991
};
19921992

19931993

1994-
19951994
template <class K, class V>
19961995
class TypeHandler<std::pair<K, V> >: public AbstractTypeHandler
19971996
{
@@ -2055,12 +2054,10 @@ class TypeHandler<Poco::AutoPtr<T> >: public AbstractTypeHandler
20552054
TypeHandler<T>::extract(pos, *obj, *obj, pExt);
20562055
}
20572056

2058-
static void prepare(std::size_t pos, const Poco::AutoPtr<T>& obj, AbstractPreparator* pPreparator)
2057+
static void prepare(std::size_t pos, const Poco::AutoPtr<T>&, AbstractPreparator* pPreparator)
20592058
{
20602059
poco_assert_dbg (pPreparator != 0);
2061-
if (!obj)
2062-
obj = new T();
2063-
TypeHandler<T>::prepare(pos, *obj, pPreparator);
2060+
TypeHandler<T>::prepare(pos, T(), pPreparator);
20642061
}
20652062

20662063
private:
@@ -2097,11 +2094,10 @@ class TypeHandler<Poco::SharedPtr<T> >: public AbstractTypeHandler
20972094
TypeHandler<T>::extract(pos, *obj, *obj, pExt);
20982095
}
20992096

2100-
static void prepare(std::size_t pos, const Poco::SharedPtr<T>& obj, AbstractPreparator* pPreparator)
2097+
static void prepare(std::size_t pos, const Poco::SharedPtr<T>&, AbstractPreparator* pPreparator)
21012098
{
21022099
poco_assert_dbg (pPreparator != 0);
2103-
// *obj will trigger a nullpointer exception if empty
2104-
TypeHandler<T>::prepare(pos, *obj, pPreparator);
2100+
TypeHandler<T>::prepare(pos, T(), pPreparator);
21052101
}
21062102

21072103
private:

‎Data/samples/RecordSet/src/RecordSet.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333

3434
#include "Poco/SharedPtr.h"
35+
#include "Poco/DateTime.h"
3536
#include "Poco/Data/SessionFactory.h"
3637
#include "Poco/Data/Session.h"
3738
#include "Poco/Data/RecordSet.h"
@@ -41,6 +42,7 @@
4142

4243

4344
using namespace Poco::Data::Keywords;
45+
using Poco::DateTime;
4446
using Poco::Data::Session;
4547
using Poco::Data::Statement;
4648
using Poco::Data::RecordSet;
@@ -66,11 +68,11 @@ int main(int argc, char** argv)
6668
session << "DROP TABLE IF EXISTS Person", now;
6769

6870
// (re)create table
69-
session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))", now;
71+
session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE)", now;
7072

7173
// insert some rows
72-
session << "INSERT INTO Person VALUES('Bart Simpson', 'Springfield', 12)", now;
73-
session << "INSERT INTO Person VALUES('Lisa Simpson', 'Springfield', 10)", now;
74+
session << "INSERT INTO Person VALUES('Bart Simpson', 'Springfield', 12, ?)", use(DateTime(1980, 4, 1)), now;
75+
session << "INSERT INTO Person VALUES('Lisa Simpson', 'Springfield', 10, ?)", use(DateTime(1982, 5, 9)), now;
7476

7577
// a simple query
7678
Statement select(session);

‎Data/samples/RowFormatter/src/RowFormatter.cpp‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434

3535
#include "Poco/SharedPtr.h"
36+
#include "Poco/DateTime.h"
3637
#include "Poco/Data/SessionFactory.h"
3738
#include "Poco/Data/Session.h"
3839
#include "Poco/Data/Statement.h"
@@ -43,6 +44,7 @@
4344

4445

4546
using namespace Poco::Data::Keywords;
47+
using Poco::DateTime;
4648
using Poco::Data::Session;
4749
using Poco::Data::Statement;
4850
using Poco::Data::RecordSet;
@@ -111,13 +113,13 @@ int main(int argc, char** argv)
111113
session << "DROP TABLE IF EXISTS Simpsons", now;
112114

113115
// (re)create table
114-
session << "CREATE TABLE Simpsons (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))", now;
116+
session << "CREATE TABLE Simpsons (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE)", now;
115117

116118
// insert some rows
117-
session << "INSERT INTO Simpsons VALUES('Homer Simpson', 'Springfield', 42)", now;
118-
session << "INSERT INTO Simpsons VALUES('Marge Simpson', 'Springfield', 38)", now;
119-
session << "INSERT INTO Simpsons VALUES('Bart Simpson', 'Springfield', 12)", now;
120-
session << "INSERT INTO Simpsons VALUES('Lisa Simpson', 'Springfield', 10)", now;
119+
session << "INSERT INTO Simpsons VALUES('Homer Simpson', 'Springfield', 42, ?)", use(DateTime(1956, 3, 1)), now;
120+
session << "INSERT INTO Simpsons VALUES('Marge Simpson', 'Springfield', 38, ?)", use(DateTime(1954, 10, 1)), now;
121+
session << "INSERT INTO Simpsons VALUES('Bart Simpson', 'Springfield', 12, ?)", use(DateTime(1980, 4, 1)), now;
122+
session << "INSERT INTO Simpsons VALUES('Lisa Simpson', 'Springfield', 10, ?)", use(DateTime(1982, 5, 9)), now;
121123

122124
// create a statement and print the column names and data as HTML table
123125
HTMLTableFormatter tf;

‎Data/samples/TypeHandler/src/TypeHandler.cpp‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
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"
@@ -41,15 +42,18 @@
4142

4243

4344
using namespace Poco::Data::Keywords;
45+
using Poco::DateTime;
46+
using Poco::DateTimeFormatter;
4447
using Poco::Data::Session;
4548
using Poco::Data::Statement;
4649

4750

4851
struct 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

Comments
 (0)