rearrange code between lib and cli
[sqlgg.git] / impl / pqxx_traits.hpp
blobd6b159e873233ebcae87a8979364945c7075caa4
1 /*
2 Postgresql/pqxx C++ traits for sqlgg
3 by ygrek
4 2014-06-08
6 This is free and unencumbered software released into the public domain.
8 Anyone is free to copy, modify, publish, use, compile, sell, or
9 distribute this software, either in source code form or as a compiled
10 binary, for any purpose, commercial or non-commercial, and by any
11 means.
13 For more information, please refer to <http://unlicense.org/>
16 #include <pqxx/pqxx>
17 #define SQLGG_STR(x) x
19 #include <assert.h>
20 #include <string.h>
21 #include <stdlib.h>
23 #include <string>
24 #include <vector>
26 #if defined(SQLGG_DEBUG)
27 #include <iostream>
28 using namespace std;
29 #endif
31 struct pqxx_traits
33 typedef int Int;
34 typedef std::string Text;
35 typedef Text Any;
37 typedef pqxx::result::const_iterator row;
38 typedef pqxx::work& connection;
40 static void get_column(row r, int index, Int& data)
42 // nothing
45 static void get_column(row r, int index, Text& data)
49 typedef pqxx::prepare::declaration stmt_decl;
51 static void set_param(stmt_decl const& stmt, const Text& val, int index)
53 stmt("varchar",pqxx::prepare::treat_string);
56 static void set_param(stmt_decl const& stmt, const Int& val, int index)
58 stmt("INTEGER",pqxx::prepare::treat_direct);
61 template<typename T>
62 static void set_param(pqxx::prepare::invocation& stmt, const T& val, int index)
64 stmt(val);
67 template<class Container, class Binder, class Params>
68 static bool do_select(connection db, Container& result, const char* sql, Binder binder, Params params)
70 const char* name = "sqlgg_stmt";
71 cout << "start prepare" << endl;
72 pqxx::prepare::declaration decl = db.conn().prepare(name,sql);
73 params.set_params(decl);
75 cout << "start invoke" << endl;
76 pqxx::prepare::invocation call = db.prepared(name);
77 cout << "set params" << endl;
78 params.set_params(call);
79 cout << "exec" << endl;
80 result = call.exec();
81 cout << "execed" << endl;
83 db.conn().unprepare(name);
85 return true;
88 struct no_params
90 void set_params(pqxx::prepare::declaration const&) {}
91 void set_params(pqxx::prepare::invocation&) {}
92 enum { count = 0 };
95 template<class T>
96 struct no_binder
98 void get(row,T&) {}
99 void bind(row,T&) {}
100 enum { count = 0 };
103 template<class Params>
104 static bool do_execute(connection db, const char* sql, Params params)
106 pqxx::result R;
107 return do_select(db,R,sql,no_binder<int>(),params);
110 }; // mysql_traits