hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TypeHandler.h
Go to the documentation of this file.
1 #ifndef TYPE_HANDLER
2 #define TYPE_HANDLER
3 
4 #include <Poco/SharedPtr.h>
5 #include <Poco/DateTimeFormatter.h>
6 #include <Poco/Data/SessionFactory.h>
7 #include <Poco/Data/Session.h>
8 #include <Poco/Data/TypeHandler.h>
9 #include <Poco/Data/SQLite/Connector.h>
10 #include <vector>
11 #include <iostream>
12 
13 
14 using namespace Poco::Data::Keywords;
15 using Poco::DateTime;
16 using Poco::DateTimeFormatter;
18 using Poco::Data::Statement;
19 
20 
21 struct Object {
22 public:
23  int id;
24  std::string name;
25 public:
26  Object ():id(0),name(""){}
27  Object (int _id,const std::string& _name):id(_id),name(_name){}
28  ~Object () {
29  id = 0;
30  name = "";
31  }
32 };
33 
34 
35 namespace Poco {
36 namespace Data {
37 
38 template <>
39 class TypeHandler<Object> {
40 public:
41  static std::size_t size()
42  {
43  return 2;
44  }
45 
46  static void bind(std::size_t pos, const Object& person, AbstractBinder* pBinder, AbstractBinder::Direction dir)
47  {
48  TypeHandler<int>::bind(pos++, person.id, pBinder, dir);
49  TypeHandler<std::string>::bind(pos++, person.name, pBinder, dir);
50  }
51 
52  static void extract(std::size_t pos, Object& person, const Object& deflt, AbstractExtractor* pExtr)
53  {
54  TypeHandler<int>::extract(pos++, person.id, deflt.id, pExtr);
55  TypeHandler<std::string>::extract(pos++, person.name, deflt.name, pExtr);
56  }
57 
58  static void prepare(std::size_t pos, const Object& person, AbstractPreparator* pPrep)
59  {
60  TypeHandler<int>::prepare(pos++, person.id, pPrep);
61  TypeHandler<std::string>::prepare(pos++, person.name, pPrep);
62  }
63 };
64 
65 
66 } } // namespace Poco::Data
67 
68 #endif
69 
70 /*int main(int argc, char** argv)
71 {
72  // register SQLite connector
73  Poco::Data::SQLite::Connector::registerConnector();
74 
75  // create a session
76  Session session("SQLite", "sample.db");
77 
78  // drop sample table, if it exists
79  session << "DROP TABLE IF EXISTS Person", now;
80 
81  // (re)create table
82  session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE)", now;
83 
84  // insert some rows
85  Person person =
86  {
87  "Bart Simpson",
88  "Springfield",
89  10,
90  DateTime(1980, 4, 1)
91  };
92 
93  Statement insert(session);
94  insert << "INSERT INTO Person VALUES(?, ?, ?, ?)",
95  use(person);
96 
97  insert.execute();
98 
99  person.name = "Lisa Simpson";
100  person.address = "Springfield";
101  person.age = 8;
102  person.birthday = DateTime(1982, 5, 9);
103 
104  insert.execute();
105 
106  // a simple query
107  Statement select(session);
108  select << "SELECT Name, Address, Age, Birthday FROM Person",
109  into(person),
110  range(0, 1); // iterate over result set one row at a time
111 
112  while (!select.done())
113  {
114  select.execute();
115  std::cout << person.name << "\t"
116  << person.address << "\t"
117  << person.age << "\t"
118  << DateTimeFormatter::format(person.birthday, "%b %d %Y")
119  << std::endl;
120  }
121 
122  // another query - store the result in a container
123  std::vector<Person> persons;
124  session << "SELECT Name, Address, Age, Birthday FROM Person",
125  into(persons),
126  now;
127 
128  for (std::vector<Person>::const_iterator it = persons.begin(); it != persons.end(); ++it)
129  {
130  std::cout << it->name << "\t"
131  << it->address << "\t"
132  << it->age << "\t"
133  << DateTimeFormatter::format(it->birthday, "%b %d %Y")
134  << std::endl;
135  }
136 
137  return 0;
138 }*/