6 #ifndef HAVE_BOOST_REGEX
9 #include <boost/regex.hpp>
12 using boost::regex_match;
23 monetdb_connection::monetdb_connection(std::string &conninfo)
25 dbh = mapi_mapiuri(conninfo.c_str(),
"monetdb",
"monetdb",
"sql");
26 if (mapi_error(dbh)) {
28 mapi_explain(dbh, stderr);
31 fprintf(stderr,
"command failed\n");
36 if (mapi_error(dbh)) {
37 mapi_explain(dbh, stderr);
44 void monetdb_connection::q(
const char* query)
46 MapiHdl hdl = mapi_query(dbh, query);
47 if (mapi_result_error(hdl) != NULL)
48 mapi_explain_result(hdl, stderr);
49 mapi_close_handle(hdl);
53 monetdb_connection::~monetdb_connection()
63 cerr <<
"init booltype, inttype, internaltype, arraytype here" << endl;
64 booltype = sqltype::get(
"boolean");
65 inttype = sqltype::get(
"int");
66 internaltype = sqltype::get(
"internal");
67 arraytype = sqltype::get(
"ARRAY");
69 cerr <<
"Loading tables from database: " << conninfo << endl;
71 string qry =
"select t.name, s.name, t.system, t.type from sys.tables t, sys.schemas s where t.schema_id=s.id ";
72 MapiHdl hdl = mapi_query(dbh,qry.c_str());
73 while (mapi_fetch_row(hdl)) {
74 tables.push_back(
table(mapi_fetch_field(hdl,0),mapi_fetch_field(hdl,1),strcmp(mapi_fetch_field(hdl,2),
"false")==0 ?
true :
false , atoi(mapi_fetch_field(hdl,3))==0 ?
false :
true));
76 mapi_close_handle(hdl);
77 cerr <<
" done." << endl;
79 cerr <<
"Loading columns and constraints...";
80 for (
auto t = tables.begin(); t!=tables.end(); t++) {
81 string q(
"select col.name,"
83 " from sys.columns col, sys.tables tab"
84 " where tab.name= '");
86 q +=
"' and tab.id = col.table_id";
88 hdl = mapi_query(dbh,q.c_str());
89 while (mapi_fetch_row(hdl)) {
90 column c(mapi_fetch_field(hdl,0), sqltype::get(mapi_fetch_field(hdl,1)));
91 t->columns().push_back(c);
93 mapi_close_handle(hdl);
97 cerr <<
" done." << endl;
99 cerr <<
"Loading operators...";
100 string opq(
"select f.func, a.type, b.type, c.type"
101 " from sys.functions f, sys.args a, sys.args b, sys.args c"
102 " where f.id=a.func_id and f.id=b.func_id and f.id=c.func_id and a.name='arg_1' and b.name='arg_2' and c.number=0");
103 hdl = mapi_query(dbh,opq.c_str());
104 while (mapi_fetch_row(hdl)) {
105 op o(mapi_fetch_field(hdl,0),sqltype::get(mapi_fetch_field(hdl,1)),sqltype::get(mapi_fetch_field(hdl,2)),sqltype::get(mapi_fetch_field(hdl,3)));
106 register_operator(o);
108 mapi_close_handle(hdl);
109 cerr <<
" done." << endl;
112 cerr <<
"Loading routines...";
113 string routq(
"select s.name, f.id, a.type, f.name from sys.schemas s, sys.args a, sys.types t, sys.functions f where f.schema_id = s.id and f.id=a.func_id and a.number=0 and a.type = t.sqlname and f.mod<>'aggr'");
114 hdl = mapi_query(dbh,routq.c_str());
115 while (mapi_fetch_row(hdl)) {
116 routine proc(mapi_fetch_field(hdl,0),mapi_fetch_field(hdl,1),sqltype::get(mapi_fetch_field(hdl,2)),mapi_fetch_field(hdl,3));
117 register_routine(proc);
119 mapi_close_handle(hdl);
120 cerr <<
" done." << endl;
122 cerr <<
"Loading routine parameters...";
123 for (
auto &proc : routines) {
124 string routpq (
"select a.type from sys.args a,"
126 " where f.id = a.func_id and a.number <> 0 and f.id = '");
127 routpq += proc.specific_name;
129 hdl = mapi_query(dbh,routpq.c_str());
130 while (mapi_fetch_row(hdl)) {
131 proc.argtypes.push_back(sqltype::get(mapi_fetch_field(hdl,0)));
133 mapi_close_handle(hdl);
135 cerr <<
" done."<< endl;
139 cerr <<
"Loading aggregates...";
140 string aggq(
"select s.name, f.id, a.type, f.name from sys.schemas s, sys.args a, sys.types t, sys.functions f where f.schema_id = s.id and f.id=a.func_id and a.number=0 and a.type = t.sqlname and f.mod='aggr'");
142 hdl = mapi_query(dbh,aggq.c_str());
143 while (mapi_fetch_row(hdl)) {
144 routine proc(mapi_fetch_field(hdl,0),mapi_fetch_field(hdl,1),sqltype::get(mapi_fetch_field(hdl,2)),mapi_fetch_field(hdl,3));
145 register_aggregate(proc);
147 mapi_close_handle(hdl);
148 cerr <<
" done." << endl;
150 cerr <<
"Loading aggregates parameters...";
151 for (
auto &proc: aggregates) {
152 string aggpq (
"select a.type from sys.args a, sys.functions f "
153 "where f.id = a.func_id and a.number <> 0 and f.id = '");
154 aggpq += proc.specific_name;
156 hdl = mapi_query(dbh,aggpq.c_str());
157 while (mapi_fetch_row(hdl)) {
158 proc.argtypes.push_back(sqltype::get(mapi_fetch_field(hdl,0)));
160 mapi_close_handle(hdl);
162 cerr <<
" done."<< endl;
194 void dut_monetdb::test(
const std::string &stmt)
196 MapiHdl hdl = mapi_query(dbh,
"CALL sys.settimeout(1)");
197 mapi_close_handle(hdl);
198 hdl = mapi_query(dbh,stmt.c_str());
200 if (mapi_error(dbh)!=MOK) {
203 const char *error_string = mapi_result_error(hdl);
206 error_string =
"unknown error";
208 const char *sqlstate = mapi_result_errorcode(hdl);
216 static regex re_syntax(
"^syntax error,.*", regex::extended);
218 if (mapi_error(dbh)==MERROR)
220 else if (mapi_error(dbh)==MTIMEOUT)
222 else if (regex_match(error_string, re_syntax))
228 mapi_close_handle(hdl);
232 mapi_close_handle(hdl);
schema and dut classes for MonetDB