minor
[sqlgg.git] / impl / pqxx_traits.hpp
blob675a25bffb2d14893531951af1e78fba69869f16
2 #include <pqxx/pqxx>
3 #define SQLGG_STR(x) x
5 #include <assert.h>
6 #include <string.h>
7 #include <stdlib.h>
9 #include <string>
10 #include <vector>
12 #if defined(SQLGG_DEBUG)
13 #include <iostream>
14 using namespace std;
15 #endif
17 struct pqxx_traits
19 typedef int Int;
20 typedef std::string Text;
21 typedef Text Any;
23 typedef pqxx::result::const_iterator row;
24 typedef pqxx::work& connection;
26 static void get_column(row r, int index, Int& data)
28 // nothing
31 static void get_column(row r, int index, Text& data)
35 typedef pqxx::prepare::declaration stmt_decl;
37 static void set_param(stmt_decl const& stmt, const Text& val, int index)
39 stmt("varchar",pqxx::prepare::treat_string);
42 static void set_param(stmt_decl const& stmt, const Int& val, int index)
44 stmt("INTEGER",pqxx::prepare::treat_direct);
47 template<typename T>
48 static void set_param(pqxx::prepare::invocation& stmt, const T& val, int index)
50 stmt(val);
53 template<class Container, class Binder, class Params>
54 static bool do_select(connection db, Container& result, const char* sql, Binder binder, Params params)
56 const char* name = "sqlgg_stmt";
57 cout << "start prepare" << endl;
58 pqxx::prepare::declaration decl = db.conn().prepare(name,sql);
59 params.set_params(decl);
61 cout << "start invoke" << endl;
62 pqxx::prepare::invocation call = db.prepared(name);
63 cout << "set params" << endl;
64 params.set_params(call);
65 cout << "exec" << endl;
66 result = call.exec();
67 cout << "execed" << endl;
69 db.conn().unprepare(name);
71 return true;
74 struct no_params
76 void set_params(pqxx::prepare::declaration const&) {}
77 void set_params(pqxx::prepare::invocation&) {}
78 enum { count = 0 };
81 template<class T>
82 struct no_binder
84 void get(row,T&) {}
85 void bind(row,T&) {}
86 enum { count = 0 };
89 template<class Params>
90 static bool do_execute(connection db, const char* sql, Params params)
92 pqxx::result R;
93 return do_select(db,R,sql,no_binder<int>(),params);
96 }; // mysql_traits