Missed a few places still referring to com.versabanq.versaplex.
[versaplex.git] / vxodbc / vxhelpers.h
blobbc1069235fcd5bcd0e55dd94e80ac7a6ab84f749
1 #ifndef __VXHELPERS_H
2 #define __VXHELPERS_H
4 #include <wvdbusconn.h>
5 #include "pgtypes.h"
8 class VxResultSet
10 int maxcol;
12 int vxtype_to_pgtype(WvStringParm vxtype)
14 if (vxtype == "String")
15 return PG_TYPE_VARCHAR;
16 else if (vxtype == "Int64")
17 return PG_TYPE_INT8;
18 else if (vxtype == "Int32")
19 return PG_TYPE_INT4;
20 else if (vxtype == "Int16")
21 return PG_TYPE_INT2;
22 else if (vxtype == "UInt8")
23 return PG_TYPE_CHAR;
24 else if (vxtype == "Bool")
25 return PG_TYPE_BOOL;
26 else if (vxtype == "Double")
27 return PG_TYPE_FLOAT8;
28 else if (vxtype == "Uuid")
29 return PG_TYPE_VARCHAR;
30 else if (vxtype == "Binary")
31 return PG_TYPE_BYTEA;
32 else if (vxtype == "DateTime")
33 return VX_TYPE_DATETIME;
34 else if (vxtype == "Decimal")
35 return PG_TYPE_NUMERIC;
37 public:
38 QResultClass *res;
40 VxResultSet()
42 res = QR_Constructor();
43 maxcol = -1;
44 assert(res);
45 assert(*this == res);
48 void set_field_info(int col, const char *colname, OID type, int typesize)
50 mylog("Col#%d is '%s', type=%d, size=%d\n", col, colname, type, typesize);
51 if (maxcol < col)
53 // FIXME: This will destroy old data
54 mylog("!!!!!! Resizing colinfo array, destroying data !!!!!!\n");
55 maxcol = col;
56 QR_set_num_fields(res, maxcol+1);
58 QR_set_field_info_v(res, col, colname, type, typesize);
59 assert(!strcmp(QR_get_fieldname(res, col), colname));
62 operator QResultClass* () const
64 return res;
67 int numcols() const
69 return maxcol+1;
72 void runquery(WvDBusConn &conn, const char *func, const char *query)
74 WvDBusMsg msg("vx.versaplexd", "/db", "vx.db", func);
75 msg.append(query);
76 WvDBusMsg reply = conn.send_and_wait(msg, 50000);
78 if (reply.iserror())
79 mylog("DBus error: '%s'\n", ((WvString)reply).cstr());
80 else
82 WvDBusMsg::Iter top(reply);
83 WvDBusMsg::Iter colinfo(top.getnext().open());
84 WvDBusMsg::Iter data(top.getnext().open().getnext().open());
85 WvDBusMsg::Iter flags(top.getnext().open());
87 int num_cols;
88 // Sadly there's no better way to get the number of columns
89 for (num_cols = 0; colinfo.next(); num_cols++) { }
90 colinfo.rewind();
91 // Allocate space for the column info data.
92 QR_set_num_fields(res, num_cols);
93 maxcol = num_cols - 1;
95 for (int colnum = 0; colinfo.next(); colnum++)
97 WvDBusMsg::Iter i(colinfo.open());
99 int colsize = i.getnext();
100 WvString colname = i.getnext();
101 int coltype = vxtype_to_pgtype(i.getnext());
103 set_field_info(colnum, colname, coltype, colsize);
106 for (data.rewind(); data.next(); )
108 TupleField *tuple = QR_AddNew(res);
110 WvDBusMsg::Iter cols(data.open());
111 for (int colnum = 0;
112 cols.next() && colnum < numcols();
113 colnum++)
114 set_tuplefield_string(&tuple[colnum], *cols);
121 class VxStatement
123 StatementClass *stmt;
124 RETCODE ret;
125 public:
126 VxStatement(StatementClass *_stmt)
128 stmt = _stmt;
129 ret = SQL_SUCCESS;
132 ~VxStatement()
135 * things need to think that this statement is finished so the
136 * results can be retrieved.
138 stmt->status = STMT_FINISHED;
140 /* set up the current tuple pointer for SQLFetch */
141 stmt->currTuple = -1;
142 SC_set_rowset_start(stmt, -1, FALSE);
143 SC_set_current_col(stmt, -1);
145 if (stmt->internal)
146 ret = DiscardStatementSvp(stmt, ret, FALSE);
149 void reinit()
151 ret = SC_initialize_and_recycle(stmt);
154 void set_result(VxResultSet &rs)
156 SC_set_Result(stmt, rs);
158 /* the binding structure for a statement is not set up until
159 * a statement is actually executed, so we'll have to do this
160 * ourselves.
162 extend_column_bindings(SC_get_ARDF(stmt), rs.numcols());
165 RETCODE retcode() const
167 return ret;
170 void seterr()
172 ret = SQL_ERROR;
175 void setok()
177 ret = SQL_SUCCESS;
180 bool isok() const
182 return ret == SQL_SUCCESS;
185 WvDBusConn &dbus()
187 WvDBusConn *db = SC_get_conn(stmt)->dbus;
188 assert(db);
189 return *db;
193 #endif // __VXHELPERS_H