sq_postgresql.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. #include "squirrel.h"
  2. #include "libpq-fe.h"
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "sqstdblobimpl.h"
  7. #include "dynamic_library.h"
  8. /*SquiLu
  9. local pgsql_functions = [
  10. ["ConnStatusType", "PQstatus", "const PGconn *conn"],
  11. ["void", "PQfinish", "PGconn *conn"],
  12. ["PGresult *", "PQprepare", @"PGconn *conn,
  13. const char *stmtName,
  14. const char *query,
  15. int nParams,
  16. const Oid *paramTypes"],
  17. ["PGresult *", "PQdescribePrepared", "PGconn *conn, const char *stmtName"],
  18. ["int", "PQnparams", "const PGresult *res"],
  19. ["Oid", "PQparamtype", "const PGresult *res, int param_number"],
  20. ["PGresult *", "PQexecPrepared", @"PGconn *conn,
  21. const char *stmtName,
  22. int nParams,
  23. const char * const *paramValues,
  24. const int *paramLengths,
  25. const int *paramFormats,
  26. int resultFormat"],
  27. ["PGresult *", "PQexec", "PGconn *conn, const char *command"],
  28. ["const char*", "PQgetvalue", @"const PGresult *res,
  29. int row_number,
  30. int column_number"],
  31. ["int", "PQntuples", "const PGresult *res"],
  32. ["char *", "PQcmdTuples", "const PGresult *res"],
  33. ["int", "PQnfields", "const PGresult *res"],
  34. ["int", "PQgetisnull", @"const PGresult *res,
  35. int row_number,
  36. int column_number"],
  37. ["void", "PQclear", "const PGresult *res"],
  38. ["int", "PQfnumber", "const PGresult *res, const char *column_name"],
  39. ["char *", "PQfname", "const PGresult *res, int column_number"],
  40. ["ExecStatusType", "PQresultStatus", "const PGresult *res"],
  41. ["char *", "PQerrorMessage", "const PGconn *conn"],
  42. ["void", "PQreset", "PGconn *conn"],
  43. ["PGresult *", "PQgetResult", "PGconn *conn"],
  44. ["int", "PQsetnonblocking", "PGconn *conn, int arg"],
  45. ["Oid", "PQftype", "const PGresult *res, int column_number"],
  46. ["int", "PQserverVersion", "const PGconn *conn"],
  47. // Large-object access routines
  48. ["int", "lo_open", "PGconn *conn, Oid lobjId, int mode"],
  49. ["int", "lo_close", "PGconn *conn, int fd"],
  50. ["int", "lo_read", "PGconn *conn, int fd, char *buf, size_t len"],
  51. ["int", "lo_write", "PGconn *conn, int fd, const char *buf, size_t len"],
  52. ["int", "lo_lseek", "PGconn *conn, int fd, int offset, int whence"],
  53. ["Oid", "lo_creat", "PGconn *conn, int mode"],
  54. ["Oid", "lo_create", "PGconn *conn, Oid lobjId"],
  55. ["int", "lo_tell", "PGconn *conn, int fd"],
  56. ["int", "lo_truncate", "PGconn *conn, int fd, size_t len"],
  57. ["int", "lo_unlink", "PGconn *conn, Oid lobjId"],
  58. ["Oid", "lo_import", "PGconn *conn, const char *filename"],
  59. ["Oid", "lo_import_with_oid", "PGconn *conn, const char *filename, Oid lobjId"],
  60. ["int", "lo_export", "PGconn *conn, Oid lobjId, const char *filename"],
  61. //next entry should be the last one
  62. //to make valid the test made on load_libpq function
  63. ["PGconn *", "PQconnectdb", "const char *conninfo"],
  64. ];
  65. function write_pgsql_functions_declaration(){
  66. foreach(k,v in pgsql_functions) {
  67. putsnl("typedef " + v[0] + " (*" + v[1] + "_t)(" + v[2] + ");");
  68. putsnl("static " + v[1] + "_t dl" + v[1] + " = 0;");
  69. }
  70. }
  71. function write_pgsql_functions_load(){
  72. foreach(k,v in pgsql_functions){
  73. putsnl("dl" + v[1] + " = (" + v[1] + "_t) libpq.dlsym(\"" + v[1] + "\");");
  74. putsnl("if(!dl" + v[1] + ") return false;");
  75. }
  76. }
  77. SquiLu*/
  78. static DynamicLibrary libpq;
  79. //@write_pgsql_functions_declaration();
  80. // generated-code:begin
  81. typedef ConnStatusType (*PQstatus_t)(const PGconn *conn);
  82. static PQstatus_t dlPQstatus = 0;
  83. typedef void (*PQfinish_t)(PGconn *conn);
  84. static PQfinish_t dlPQfinish = 0;
  85. typedef PGresult * (*PQprepare_t)(PGconn *conn,
  86. const char *stmtName,
  87. const char *query,
  88. int nParams,
  89. const Oid *paramTypes);
  90. static PQprepare_t dlPQprepare = 0;
  91. typedef PGresult * (*PQdescribePrepared_t)(PGconn *conn, const char *stmtName);
  92. static PQdescribePrepared_t dlPQdescribePrepared = 0;
  93. typedef int (*PQnparams_t)(const PGresult *res);
  94. static PQnparams_t dlPQnparams = 0;
  95. typedef Oid (*PQparamtype_t)(const PGresult *res, int param_number);
  96. static PQparamtype_t dlPQparamtype = 0;
  97. typedef PGresult * (*PQexecPrepared_t)(PGconn *conn,
  98. const char *stmtName,
  99. int nParams,
  100. const char * const *paramValues,
  101. const int *paramLengths,
  102. const int *paramFormats,
  103. int resultFormat);
  104. static PQexecPrepared_t dlPQexecPrepared = 0;
  105. typedef PGresult * (*PQexec_t)(PGconn *conn, const char *command);
  106. static PQexec_t dlPQexec = 0;
  107. typedef const char* (*PQgetvalue_t)(const PGresult *res,
  108. int row_number,
  109. int column_number);
  110. static PQgetvalue_t dlPQgetvalue = 0;
  111. typedef int (*PQntuples_t)(const PGresult *res);
  112. static PQntuples_t dlPQntuples = 0;
  113. typedef char * (*PQcmdTuples_t)(const PGresult *res);
  114. static PQcmdTuples_t dlPQcmdTuples = 0;
  115. typedef int (*PQnfields_t)(const PGresult *res);
  116. static PQnfields_t dlPQnfields = 0;
  117. typedef int (*PQgetisnull_t)(const PGresult *res,
  118. int row_number,
  119. int column_number);
  120. static PQgetisnull_t dlPQgetisnull = 0;
  121. typedef void (*PQclear_t)(const PGresult *res);
  122. static PQclear_t dlPQclear = 0;
  123. typedef int (*PQfnumber_t)(const PGresult *res, const char *column_name);
  124. static PQfnumber_t dlPQfnumber = 0;
  125. typedef char * (*PQfname_t)(const PGresult *res, int column_number);
  126. static PQfname_t dlPQfname = 0;
  127. typedef ExecStatusType (*PQresultStatus_t)(const PGresult *res);
  128. static PQresultStatus_t dlPQresultStatus = 0;
  129. typedef char * (*PQerrorMessage_t)(const PGconn *conn);
  130. static PQerrorMessage_t dlPQerrorMessage = 0;
  131. typedef void (*PQreset_t)(PGconn *conn);
  132. static PQreset_t dlPQreset = 0;
  133. typedef PGresult * (*PQgetResult_t)(PGconn *conn);
  134. static PQgetResult_t dlPQgetResult = 0;
  135. typedef int (*PQsetnonblocking_t)(PGconn *conn, int arg);
  136. static PQsetnonblocking_t dlPQsetnonblocking = 0;
  137. typedef Oid (*PQftype_t)(const PGresult *res, int column_number);
  138. static PQftype_t dlPQftype = 0;
  139. typedef int (*PQserverVersion_t)(const PGconn *conn);
  140. static PQserverVersion_t dlPQserverVersion = 0;
  141. typedef int (*lo_open_t)(PGconn *conn, Oid lobjId, int mode);
  142. static lo_open_t dllo_open = 0;
  143. typedef int (*lo_close_t)(PGconn *conn, int fd);
  144. static lo_close_t dllo_close = 0;
  145. typedef int (*lo_read_t)(PGconn *conn, int fd, char *buf, size_t len);
  146. static lo_read_t dllo_read = 0;
  147. typedef int (*lo_write_t)(PGconn *conn, int fd, const char *buf, size_t len);
  148. static lo_write_t dllo_write = 0;
  149. typedef int (*lo_lseek_t)(PGconn *conn, int fd, int offset, int whence);
  150. static lo_lseek_t dllo_lseek = 0;
  151. typedef Oid (*lo_creat_t)(PGconn *conn, int mode);
  152. static lo_creat_t dllo_creat = 0;
  153. typedef Oid (*lo_create_t)(PGconn *conn, Oid lobjId);
  154. static lo_create_t dllo_create = 0;
  155. typedef int (*lo_tell_t)(PGconn *conn, int fd);
  156. static lo_tell_t dllo_tell = 0;
  157. typedef int (*lo_truncate_t)(PGconn *conn, int fd, size_t len);
  158. static lo_truncate_t dllo_truncate = 0;
  159. typedef int (*lo_unlink_t)(PGconn *conn, Oid lobjId);
  160. static lo_unlink_t dllo_unlink = 0;
  161. typedef Oid (*lo_import_t)(PGconn *conn, const char *filename);
  162. static lo_import_t dllo_import = 0;
  163. typedef Oid (*lo_import_with_oid_t)(PGconn *conn, const char *filename, Oid lobjId);
  164. static lo_import_with_oid_t dllo_import_with_oid = 0;
  165. typedef int (*lo_export_t)(PGconn *conn, Oid lobjId, const char *filename);
  166. static lo_export_t dllo_export = 0;
  167. typedef PGconn * (*PQconnectdb_t)(const char *conninfo);
  168. static PQconnectdb_t dlPQconnectdb = 0;
  169. // generated-code:end
  170. #ifdef WIN32
  171. #define LIBPQ_NAME "libpq.dll"
  172. #else
  173. #define LIBPQ_NAME "libpq.so"
  174. #endif
  175. #define DONT_DELETE_MSG 1
  176. static bool load_libpq()
  177. {
  178. if(dlPQconnectdb) return true;
  179. if(libpq.open(LIBPQ_NAME))
  180. {
  181. //@write_pgsql_functions_load();
  182. // generated-code:begin
  183. dlPQstatus = (PQstatus_t) libpq.dlsym("PQstatus");
  184. if(!dlPQstatus) return false;
  185. dlPQfinish = (PQfinish_t) libpq.dlsym("PQfinish");
  186. if(!dlPQfinish) return false;
  187. dlPQprepare = (PQprepare_t) libpq.dlsym("PQprepare");
  188. if(!dlPQprepare) return false;
  189. dlPQdescribePrepared = (PQdescribePrepared_t) libpq.dlsym("PQdescribePrepared");
  190. if(!dlPQdescribePrepared) return false;
  191. dlPQnparams = (PQnparams_t) libpq.dlsym("PQnparams");
  192. if(!dlPQnparams) return false;
  193. dlPQparamtype = (PQparamtype_t) libpq.dlsym("PQparamtype");
  194. if(!dlPQparamtype) return false;
  195. dlPQexecPrepared = (PQexecPrepared_t) libpq.dlsym("PQexecPrepared");
  196. if(!dlPQexecPrepared) return false;
  197. dlPQexec = (PQexec_t) libpq.dlsym("PQexec");
  198. if(!dlPQexec) return false;
  199. dlPQgetvalue = (PQgetvalue_t) libpq.dlsym("PQgetvalue");
  200. if(!dlPQgetvalue) return false;
  201. dlPQntuples = (PQntuples_t) libpq.dlsym("PQntuples");
  202. if(!dlPQntuples) return false;
  203. dlPQcmdTuples = (PQcmdTuples_t) libpq.dlsym("PQcmdTuples");
  204. if(!dlPQcmdTuples) return false;
  205. dlPQnfields = (PQnfields_t) libpq.dlsym("PQnfields");
  206. if(!dlPQnfields) return false;
  207. dlPQgetisnull = (PQgetisnull_t) libpq.dlsym("PQgetisnull");
  208. if(!dlPQgetisnull) return false;
  209. dlPQclear = (PQclear_t) libpq.dlsym("PQclear");
  210. if(!dlPQclear) return false;
  211. dlPQfnumber = (PQfnumber_t) libpq.dlsym("PQfnumber");
  212. if(!dlPQfnumber) return false;
  213. dlPQfname = (PQfname_t) libpq.dlsym("PQfname");
  214. if(!dlPQfname) return false;
  215. dlPQresultStatus = (PQresultStatus_t) libpq.dlsym("PQresultStatus");
  216. if(!dlPQresultStatus) return false;
  217. dlPQerrorMessage = (PQerrorMessage_t) libpq.dlsym("PQerrorMessage");
  218. if(!dlPQerrorMessage) return false;
  219. dlPQreset = (PQreset_t) libpq.dlsym("PQreset");
  220. if(!dlPQreset) return false;
  221. dlPQgetResult = (PQgetResult_t) libpq.dlsym("PQgetResult");
  222. if(!dlPQgetResult) return false;
  223. dlPQsetnonblocking = (PQsetnonblocking_t) libpq.dlsym("PQsetnonblocking");
  224. if(!dlPQsetnonblocking) return false;
  225. dlPQftype = (PQftype_t) libpq.dlsym("PQftype");
  226. if(!dlPQftype) return false;
  227. dlPQserverVersion = (PQserverVersion_t) libpq.dlsym("PQserverVersion");
  228. if(!dlPQserverVersion) return false;
  229. dllo_open = (lo_open_t) libpq.dlsym("lo_open");
  230. if(!dllo_open) return false;
  231. dllo_close = (lo_close_t) libpq.dlsym("lo_close");
  232. if(!dllo_close) return false;
  233. dllo_read = (lo_read_t) libpq.dlsym("lo_read");
  234. if(!dllo_read) return false;
  235. dllo_write = (lo_write_t) libpq.dlsym("lo_write");
  236. if(!dllo_write) return false;
  237. dllo_lseek = (lo_lseek_t) libpq.dlsym("lo_lseek");
  238. if(!dllo_lseek) return false;
  239. dllo_creat = (lo_creat_t) libpq.dlsym("lo_creat");
  240. if(!dllo_creat) return false;
  241. dllo_create = (lo_create_t) libpq.dlsym("lo_create");
  242. if(!dllo_create) return false;
  243. dllo_tell = (lo_tell_t) libpq.dlsym("lo_tell");
  244. if(!dllo_tell) return false;
  245. dllo_truncate = (lo_truncate_t) libpq.dlsym("lo_truncate");
  246. if(!dllo_truncate) return false;
  247. dllo_unlink = (lo_unlink_t) libpq.dlsym("lo_unlink");
  248. if(!dllo_unlink) return false;
  249. dllo_import = (lo_import_t) libpq.dlsym("lo_import");
  250. if(!dllo_import) return false;
  251. dllo_import_with_oid = (lo_import_with_oid_t) libpq.dlsym("lo_import_with_oid");
  252. if(!dllo_import_with_oid) return false;
  253. dllo_export = (lo_export_t) libpq.dlsym("lo_export");
  254. if(!dllo_export) return false;
  255. dlPQconnectdb = (PQconnectdb_t) libpq.dlsym("PQconnectdb");
  256. if(!dlPQconnectdb) return false;
  257. // generated-code:end
  258. return true;
  259. }
  260. return false;
  261. }
  262. ////////////////////////////////////////////////////////////////////////////////
  263. static const SQChar *PostgreSQL_TAG = _SC("PostgreSQL");
  264. static SQRESULT get_pgsql_instance(HSQUIRRELVM v, SQInteger idx, PGconn **self){
  265. SQRESULT _rc_;
  266. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)self,(void*)PostgreSQL_TAG)) < 0) return _rc_;
  267. if(!*self) return sq_throwerror(v, _SC("database is closed"));
  268. return _rc_;
  269. }
  270. #define GET_pgsql_INSTANCE_AT(idx) \
  271. PGconn *self=NULL; \
  272. if((_rc_ = get_pgsql_instance(v,idx,&self)) < 0) return _rc_;
  273. #define GET_pgsql_INSTANCE() GET_pgsql_INSTANCE_AT(1)
  274. static const SQChar *PostgreSQL_Result_TAG = _SC("PostgreSQL_Result");
  275. static const SQChar *_curr_row_key = _SC("_curr_row");
  276. static SQRESULT get_pgsql_result_instance(HSQUIRRELVM v, SQInteger idx, PGresult **self){
  277. SQRESULT _rc_;
  278. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)self,(void*)PostgreSQL_Result_TAG)) < 0) return _rc_;
  279. if(!*self) return sq_throwerror(v, _SC("PGresult is closed"));
  280. return _rc_;
  281. }
  282. #define GET_pgsql_result_INSTANCE_AT(idx) \
  283. PGresult *self=NULL; \
  284. if((_rc_ = get_pgsql_result_instance(v,idx,&self)) < 0) return _rc_;
  285. #define GET_pgsql_result_INSTANCE() GET_pgsql_result_INSTANCE_AT(1)
  286. static SQRESULT sq_pgsql_result_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  287. {
  288. PGresult *self = ((PGresult *)p);
  289. if (self) dlPQclear(self);
  290. return 0;
  291. }
  292. static SQRESULT sq_pgsql_result_close(HSQUIRRELVM v){
  293. SQ_FUNC_VARS_NO_TOP(v);
  294. GET_pgsql_result_INSTANCE();
  295. dlPQclear(self);
  296. sq_setinstanceup(v, 1, 0); //next calls will fail with "Pgresult is closed"
  297. return 0;
  298. }
  299. static SQRESULT sq_pgsql_result_col_count(HSQUIRRELVM v){
  300. SQ_FUNC_VARS_NO_TOP(v);
  301. GET_pgsql_result_INSTANCE();
  302. sq_pushinteger(v, dlPQnfields(self));
  303. return 1;
  304. }
  305. static SQRESULT sq_pgsql_result_row_count(HSQUIRRELVM v){
  306. SQ_FUNC_VARS_NO_TOP(v);
  307. GET_pgsql_result_INSTANCE();
  308. sq_pushinteger(v, dlPQntuples(self));
  309. return 1;
  310. }
  311. static SQRESULT sq_pgsql_result_col_name(HSQUIRRELVM v){
  312. SQ_FUNC_VARS_NO_TOP(v);
  313. GET_pgsql_result_INSTANCE();
  314. SQ_GET_INTEGER(v, 2, col);
  315. sq_pushstring(v, dlPQfname(self, col), -1);
  316. return 1;
  317. }
  318. static SQRESULT sq_pgsql_result_col_index(HSQUIRRELVM v){
  319. SQ_FUNC_VARS_NO_TOP(v);
  320. GET_pgsql_result_INSTANCE();
  321. SQ_GET_STRING(v, 2, name);
  322. sq_pushinteger(v, dlPQfnumber(self, name));
  323. return 1;
  324. }
  325. static SQRESULT sq_pgsql_result_eof(HSQUIRRELVM v){
  326. SQ_FUNC_VARS_NO_TOP(v);
  327. GET_pgsql_result_INSTANCE();
  328. sq_pushstring(v, _curr_row_key, -1);
  329. if(sq_get(v, 1) == SQ_OK){
  330. SQ_GET_INTEGER(v, -1, curr_row);
  331. sq_pushbool(v, curr_row < dlPQntuples(self));
  332. }
  333. else sq_pushbool(v, SQTrue);
  334. return 1;
  335. }
  336. static SQRESULT sq_pgsql_result_next_row(HSQUIRRELVM v){
  337. SQ_FUNC_VARS_NO_TOP(v);
  338. GET_pgsql_result_INSTANCE();
  339. sq_pushstring(v, _curr_row_key, -1);
  340. sq_push(v, -1); //make a copy
  341. if(sq_get(v, 1) == SQ_OK){
  342. SQ_GET_INTEGER(v, -1, curr_row);
  343. if(++curr_row < dlPQntuples(self)){
  344. sq_poptop(v);
  345. sq_pushinteger(v, curr_row);
  346. sq_set(v, 1);
  347. sq_pushbool(v, SQTrue);
  348. return 1;
  349. }
  350. }
  351. sq_pushbool(v, SQFalse);
  352. return 1;
  353. }
  354. static SQRESULT sq_pgsql_result_col_value(HSQUIRRELVM v){
  355. SQ_FUNC_VARS_NO_TOP(v);
  356. GET_pgsql_result_INSTANCE();
  357. SQObjectType ptype = sq_gettype(v, 2);
  358. int col = -1;
  359. if(ptype == OT_STRING){
  360. SQ_GET_STRING(v, 2, col_name);
  361. col = dlPQfnumber(self, col_name);
  362. }
  363. else
  364. {
  365. SQ_GET_INTEGER(v, 2, idx);
  366. col = idx;
  367. }
  368. if(col < 0) return sq_throwerror(v, _SC("invalid col index/name"));
  369. sq_pushstring(v, _curr_row_key, -1);
  370. if(sq_get(v, 1) == SQ_OK){
  371. SQ_GET_INTEGER(v, -1, curr_row);
  372. if(curr_row < dlPQntuples(self)){
  373. sq_pushstring(v, dlPQgetvalue(self, curr_row, col), -1);
  374. return 1;
  375. }
  376. }
  377. return SQ_ERROR;
  378. }
  379. static SQRESULT sq_pgsql_result_row_as_array(HSQUIRRELVM v){
  380. SQ_FUNC_VARS(v);
  381. GET_pgsql_result_INSTANCE();
  382. SQ_OPT_INTEGER(v, 2, row, -1);
  383. if(row < 0){
  384. sq_pushstring(v, _curr_row_key, -1);
  385. if(sq_get(v, 1) == SQ_OK){
  386. sq_getinteger(v, -1, &row);
  387. }
  388. }
  389. int row_count = dlPQntuples(self);
  390. if(row < 0 || row >= row_count) return sq_throwerror(v, _SC("invalid row (%d)"), row);
  391. int col_count = dlPQnfields(self);
  392. sq_newarray(v, col_count);
  393. for(int i=0; i < col_count; ++i){
  394. sq_pushinteger(v, i);
  395. sq_pushstring(v, dlPQgetvalue(self, row, i), -1);
  396. sq_rawset(v, -3);
  397. }
  398. return 1;
  399. }
  400. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_pgsql_result_##name,nparams,tycheck}
  401. static SQRegFunction sq_pgsql_result_methods[] =
  402. {
  403. _DECL_FUNC(close, 1, _SC("x")),
  404. _DECL_FUNC(eof, 1, _SC("x")),
  405. _DECL_FUNC(next_row, 1, _SC("x")),
  406. _DECL_FUNC(col_count, 1, _SC("x")),
  407. _DECL_FUNC(row_count, 1, _SC("x")),
  408. _DECL_FUNC(col_name, 2, _SC("xi")),
  409. _DECL_FUNC(col_index, 2, _SC("xs")),
  410. _DECL_FUNC(col_value, 2, _SC("x i|s")),
  411. _DECL_FUNC(row_as_array, -1, _SC("xi")),
  412. {0,0}
  413. };
  414. #undef _DECL_FUNC
  415. static SQRESULT sq_pgsql_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  416. {
  417. PGconn *self = ((PGconn *)p);
  418. if (self) dlPQfinish(self);
  419. return 0;
  420. }
  421. static SQRESULT sq_pgsql_constructor(HSQUIRRELVM v)
  422. {
  423. SQ_FUNC_VARS_NO_TOP(v);
  424. SQ_GET_STRING(v, 2, szConnInfo);
  425. PGconn *self=0;
  426. if(load_libpq())
  427. {
  428. self = dlPQconnectdb(szConnInfo);
  429. if (dlPQstatus(self) == CONNECTION_BAD) return sq_throwerror(v, _SC("Failed to connect ot database !"));
  430. }
  431. else return sq_throwerror(v, _SC("Failed to load libpq !"));
  432. sq_setinstanceup(v, 1, self);
  433. sq_setreleasehook(v,1, sq_pgsql_releasehook);
  434. //save a weakref to allow statement return it's db
  435. sq_pushuserpointer(v, self);
  436. sq_weakref(v, 1);
  437. sq_setonregistrytable(v);
  438. return 1;
  439. }
  440. static SQRESULT sq_pgsql_close(HSQUIRRELVM v){
  441. SQ_FUNC_VARS_NO_TOP(v);
  442. GET_pgsql_INSTANCE();
  443. dlPQfinish(self);
  444. sq_setinstanceup(v, 1, 0); //next calls will fail with "database is closed"
  445. return 0;
  446. }
  447. static SQRESULT sq_pgsql_exec_dml(HSQUIRRELVM v){
  448. SQ_FUNC_VARS_NO_TOP(v);
  449. GET_pgsql_INSTANCE();
  450. SQ_GET_STRING(v, 2, szSQL);
  451. int result = 0;
  452. PGresult *qres = dlPQexec(self, szSQL);
  453. bool is_ok = dlPQresultStatus(qres) == PGRES_COMMAND_OK;
  454. if (is_ok) result = atoi(dlPQcmdTuples(qres));
  455. dlPQclear(qres);
  456. if (!is_ok) return sq_throwerror(v, dlPQerrorMessage(self));
  457. sq_pushinteger(v, result);
  458. return 1;
  459. }
  460. static SQRESULT sq_pgsql_exec_scalar(HSQUIRRELVM v){
  461. SQ_FUNC_VARS_NO_TOP(v);
  462. GET_pgsql_INSTANCE();
  463. SQ_GET_STRING(v, 2, szSQL);
  464. int result = 0;
  465. PGresult *qres = dlPQexec(self, szSQL);
  466. bool is_ok = (dlPQresultStatus(qres) == PGRES_TUPLES_OK) &&
  467. (dlPQntuples(qres) == 1) && (dlPQnfields(qres) > 0);
  468. if (is_ok) result = atoi(dlPQgetvalue(qres, 0, 0));
  469. dlPQclear(qres);
  470. if (!is_ok) return sq_throwerror(v, dlPQerrorMessage(self));
  471. sq_pushinteger(v, result);
  472. return 1;
  473. }
  474. static SQRESULT sq_pgsql_exec_query(HSQUIRRELVM v){
  475. SQ_FUNC_VARS_NO_TOP(v);
  476. GET_pgsql_INSTANCE();
  477. SQ_GET_STRING(v, 2, szSQL);
  478. PGresult *qres = dlPQexec(self, szSQL);
  479. if(dlPQresultStatus(qres) == PGRES_TUPLES_OK){
  480. sq_pushroottable(v);
  481. sq_pushstring(v, PostgreSQL_Result_TAG, -1);
  482. if(sq_get(v, -2) == SQ_OK){
  483. if(sq_createinstance(v, -1) == SQ_OK){
  484. sq_setinstanceup(v, -1, qres);
  485. sq_setreleasehook(v, -1, sq_pgsql_result_releasehook);
  486. sq_pushstring(v, _curr_row_key, -1);
  487. sq_pushinteger(v, -1);
  488. sq_set(v, -3);
  489. return 1;
  490. }
  491. }
  492. }
  493. return sq_throwerror(v, dlPQerrorMessage(self));
  494. }
  495. static SQRESULT sq_pgsql_error_message(HSQUIRRELVM v){
  496. SQ_FUNC_VARS_NO_TOP(v);
  497. GET_pgsql_INSTANCE();
  498. sq_pushstring(v, dlPQerrorMessage(self), -1);
  499. return 1;
  500. }
  501. static SQRESULT sq_pgsql_version(HSQUIRRELVM v){
  502. SQ_FUNC_VARS_NO_TOP(v);
  503. GET_pgsql_INSTANCE();
  504. sq_pushinteger(v, dlPQserverVersion(self));
  505. return 1;
  506. }
  507. static int
  508. inv_read = 0x40000,
  509. inv_write = 0x20000,
  510. invalidoid = 0,
  511. inv_seek_set = 0,
  512. inv_seek_curr = 1,
  513. inv_seek_end = 2;
  514. static SQRESULT sq_pgsql_get_blob_field(HSQUIRRELVM v){
  515. SQ_FUNC_VARS_NO_TOP(v);
  516. GET_pgsql_INSTANCE();
  517. SQ_GET_INTEGER(v, 2, oid);
  518. //begin_recursive_transaction();
  519. char *result = 0;
  520. int ofd = dllo_open(self, oid, inv_read);
  521. if(ofd >= 0){
  522. int blobSize = dllo_lseek(self, ofd, 0, inv_seek_end);
  523. dllo_lseek(self, ofd, 0, inv_seek_set);
  524. SQBlob blob(blobSize);
  525. result = (char*)blob.GetBuf();
  526. int numRead = 0;
  527. while(blobSize > 0){
  528. int i = dllo_read(self, ofd, result+numRead, blobSize);
  529. numRead += i;
  530. blobSize -= i;
  531. }
  532. dllo_close(self, oid);
  533. sq_pushstring(v, (const SQChar*)blob.GetBuf(), blob.Len());
  534. }
  535. //commit_recursive_transaction();
  536. if(!result) sq_pushnull(v);
  537. return 1;
  538. }
  539. static SQRESULT sq_pgsql_insert_blob_field(HSQUIRRELVM v){
  540. SQ_FUNC_VARS_NO_TOP(v);
  541. GET_pgsql_INSTANCE();
  542. int result = 0;
  543. SQ_GET_STRING(v, 2, blob);
  544. SQ_GET_BOOL(v, 3, isFileName);
  545. if(isFileName){
  546. result = dllo_import(self, blob);
  547. } else {
  548. result = dllo_creat(self, inv_write);
  549. if(result){
  550. int ofd = dllo_open(self, result, inv_write);
  551. if (ofd >= 0){
  552. int i = blob_size;
  553. const char *blopPtr = (const char *)blob;
  554. int numWriten = 0;
  555. while(i > 0){
  556. int i2 = dllo_write(self, ofd, blopPtr+numWriten, i);
  557. numWriten += i2;
  558. i -= i2;
  559. }
  560. dllo_close(self, ofd);
  561. }
  562. else return sq_throwerror(v, _SC("Failed to insert blob !"));
  563. }
  564. }
  565. if(!result) sq_pushnull(v);
  566. return 1;
  567. }
  568. static SQRESULT sq_pgsql_update_blob_field(HSQUIRRELVM v){
  569. SQ_FUNC_VARS_NO_TOP(v);
  570. GET_pgsql_INSTANCE();
  571. SQ_GET_INTEGER(v, 2, oid);
  572. SQ_GET_STRING(v, 3, blob);
  573. SQ_GET_BOOL(v, 4, isFileName);
  574. int result_oid = 0;
  575. int result_error = SQ_OK;
  576. int loid = dllo_creat(self, inv_write);
  577. int ofd = dllo_open(self, loid, inv_write);
  578. if(ofd >= 0){
  579. dllo_unlink(self, oid);
  580. result_oid = loid;
  581. if(isFileName)
  582. {
  583. char buf[2048];
  584. FILE *fp = fopen(blob, "rb");
  585. if(!fp) {
  586. sq_throwerror(v, _SC("Failed to update blob from file !"));
  587. result_error = SQ_ERROR;
  588. }
  589. else
  590. {
  591. char *charPtr = buf;
  592. int numRead;
  593. do{
  594. numRead = fread(buf, 1, sizeof(buf), fp);
  595. int numWriten = dllo_write(self, ofd, charPtr, numRead);
  596. if (numWriten != numRead) {
  597. sq_throwerror(v, _SC("Failed to update blob from file !"));
  598. result_error = SQ_ERROR;
  599. break;
  600. }
  601. } while (numRead == 0);
  602. fclose(fp);
  603. }
  604. }else{
  605. int i = blob_size;
  606. const char *blopPtr = (const char *)blob;
  607. int numWriten = 0;
  608. while(i > 0){
  609. int i2 = dllo_write(self, ofd, blopPtr+numWriten, i);
  610. numWriten += i2;
  611. i -= i2;
  612. }
  613. }
  614. dllo_close(self, ofd);
  615. }
  616. if(result_error == SQ_ERROR) return result_error;
  617. sq_pushinteger(v, result_oid);
  618. return 1;
  619. }
  620. static SQRESULT sq_pgsql_delete_blob_field(HSQUIRRELVM v){
  621. SQ_FUNC_VARS_NO_TOP(v);
  622. GET_pgsql_INSTANCE();
  623. SQ_GET_INTEGER(v, 2, oid);
  624. sq_pushinteger(v, dllo_unlink(self, oid));
  625. return 1;
  626. }
  627. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_pgsql_##name,nparams,tycheck}
  628. static SQRegFunction sq_pgsql_methods[] =
  629. {
  630. _DECL_FUNC(constructor, 2, _SC("xs")),
  631. _DECL_FUNC(close, 1, _SC("x")),
  632. _DECL_FUNC(exec_dml, 2, _SC("xs")),
  633. _DECL_FUNC(exec_scalar, 2, _SC("xs")),
  634. _DECL_FUNC(exec_query, 2, _SC("xs")),
  635. _DECL_FUNC(error_message, 1, _SC("x")),
  636. _DECL_FUNC(version, 1, _SC("x")),
  637. _DECL_FUNC(get_blob_field, 2, _SC("xi")),
  638. _DECL_FUNC(insert_blob_field, 3, _SC("xsb")),
  639. _DECL_FUNC(update_blob_field, 3, _SC("xisb")),
  640. _DECL_FUNC(delete_blob_field, 2, _SC("xi")),
  641. {0,0}
  642. };
  643. #undef _DECL_FUNC
  644. #ifdef __cplusplus
  645. extern "C" {
  646. #endif
  647. SQRESULT sqext_register_PostgreSQL(HSQUIRRELVM v)
  648. {
  649. sq_pushstring(v,PostgreSQL_TAG,-1);
  650. sq_newclass(v,SQFalse);
  651. sq_settypetag(v,-1,(void*)PostgreSQL_TAG);
  652. sq_insert_reg_funcs(v, sq_pgsql_methods);
  653. sq_newslot(v,-3,SQTrue);
  654. sq_pushstring(v,PostgreSQL_Result_TAG,-1);
  655. sq_newclass(v,SQFalse);
  656. sq_settypetag(v,-1,(void*)PostgreSQL_Result_TAG);
  657. sq_insert_reg_funcs(v, sq_pgsql_result_methods);
  658. sq_pushstring(v, _curr_row_key, -1);
  659. sq_pushnull(v);
  660. sq_newslot(v, -3, SQFalse);
  661. sq_newslot(v,-3,SQTrue);
  662. return 0;
  663. }
  664. #ifdef __cplusplus
  665. }
  666. #endif