You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3138 lines
86 KiB

25 years ago
25 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
25 years ago
27 years ago
27 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
26 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
26 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2002 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Jouni Ahto <jouni.ahto@exdec.fi> |
  16. | Andrew Avdeev <andy@rsc.mv.ru> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. /* TODO: Arrays, roles?
  21. A lot... */
  22. /*
  23. Changes:
  24. 2001-05-31: Jeremy Bettis <jeremy@deadbeef.com>
  25. - If a blob handle was expected and something else was
  26. received create a blob and add the value to it.
  27. - If the incoming argument to a bind parameter is NULL
  28. then store a NULL in the database.
  29. - More verbose date errors.
  30. 1999-09-21: Ivo Panacek <ivop@regionet.cz>
  31. - added COMPILE_DL section
  32. - more verbose php_info_ibase function
  33. mostly stolen from pgsql.c for now
  34. 1999-10-05: Ivo Panacek <ivop@regionet.cz>
  35. - safe rinit/rfinish: check for NULL so
  36. rfinish could be called repeatedly
  37. emalloc & co. replaced with malloc & co.
  38. */
  39. #ifdef HAVE_CONFIG_H
  40. #include "config.h"
  41. #endif
  42. #include "php.h"
  43. #include "php_ini.h"
  44. #include "ext/standard/php_standard.h"
  45. #include "php_globals.h"
  46. #include "php_interbase.h"
  47. #if HAVE_IBASE
  48. #include <ibase.h>
  49. #include <time.h>
  50. #include "ext/standard/fsock.h"
  51. #include "ext/standard/info.h"
  52. #ifdef SQL_INT64
  53. #include <math.h>
  54. #endif
  55. #ifndef SQL_DIALECT_CURRENT
  56. #define SQL_DIALECT_CURRENT 1
  57. #endif
  58. /*
  59. #define IBDEBUG(a) php_printf("::: %s (%d)\n", a, __LINE__);
  60. */
  61. #define IBDEBUG(a)
  62. #define SAFE_STRING(s) ((s)?(s):"")
  63. /* {{{ extension definition structures */
  64. function_entry ibase_functions[] = {
  65. PHP_FE(ibase_connect, NULL)
  66. PHP_FE(ibase_pconnect, NULL)
  67. PHP_FE(ibase_close, NULL)
  68. PHP_FE(ibase_query, NULL)
  69. PHP_FE(ibase_fetch_row, NULL)
  70. PHP_FE(ibase_fetch_assoc, NULL)
  71. PHP_FE(ibase_fetch_object, NULL)
  72. PHP_FE(ibase_free_result, NULL)
  73. PHP_FE(ibase_prepare, NULL)
  74. PHP_FE(ibase_execute, NULL)
  75. PHP_FE(ibase_free_query, NULL)
  76. PHP_FE(ibase_timefmt, NULL)
  77. PHP_FE(ibase_num_fields, NULL)
  78. PHP_FE(ibase_field_info, NULL)
  79. PHP_FE(ibase_trans, NULL)
  80. PHP_FE(ibase_commit, NULL)
  81. PHP_FE(ibase_rollback, NULL)
  82. PHP_FE(ibase_blob_info, NULL)
  83. PHP_FE(ibase_blob_create, NULL)
  84. PHP_FE(ibase_blob_add, NULL)
  85. PHP_FE(ibase_blob_cancel, NULL)
  86. PHP_FE(ibase_blob_close, NULL)
  87. PHP_FE(ibase_blob_open, NULL)
  88. PHP_FE(ibase_blob_get, NULL)
  89. PHP_FE(ibase_blob_echo, NULL)
  90. PHP_FE(ibase_blob_import, NULL)
  91. PHP_FE(ibase_errmsg, NULL)
  92. #ifdef SQL_DIALECT_V6
  93. PHP_FE(ibase_add_user, NULL)
  94. PHP_FE(ibase_modify_user, NULL)
  95. PHP_FE(ibase_delete_user, NULL)
  96. #endif
  97. {NULL, NULL, NULL}
  98. };
  99. zend_module_entry ibase_module_entry =
  100. {
  101. STANDARD_MODULE_HEADER,
  102. "interbase",
  103. ibase_functions,
  104. PHP_MINIT(ibase),
  105. PHP_MSHUTDOWN(ibase),
  106. PHP_RINIT(ibase),
  107. PHP_RSHUTDOWN(ibase),
  108. PHP_MINFO(ibase),
  109. NO_VERSION_YET,
  110. STANDARD_MODULE_PROPERTIES
  111. };
  112. #ifdef COMPILE_DL_INTERBASE
  113. ZEND_GET_MODULE(ibase)
  114. #define DL_MALLOC(size) malloc(size)
  115. #define DL_STRDUP(str) strdup(str)
  116. #define DL_FREE(ptr) free(ptr)
  117. #else
  118. #define DL_MALLOC(size) emalloc(size)
  119. #define DL_STRDUP(str) estrdup(str)
  120. #define DL_FREE(ptr) efree(ptr)
  121. #endif
  122. /* True globals, no need for thread safety */
  123. static int le_blob, le_link, le_plink, le_result, le_query, le_trans;
  124. ZEND_DECLARE_MODULE_GLOBALS(ibase)
  125. /* }}} */
  126. /* {{{ internal macros, functions and structures */
  127. #define IB_STATUS (IBG(status))
  128. /* Fill ib_link and trans_n with the correct database link and transaction slot number. */
  129. static void get_link_trans(INTERNAL_FUNCTION_PARAMETERS, zval **link_id, ibase_db_link **ib_link, int *trans_n, int *trans_id) {
  130. int type;
  131. void *resource = NULL;
  132. ibase_tr_link *ib_trans;
  133. IBDEBUG("Transaction or database link?");
  134. if ((resource = zend_list_find(Z_LVAL_PP(link_id), &type))) {
  135. IBDEBUG("Found in list");
  136. if (type == le_trans) {
  137. /* Transaction resource. Fetch it, database link resource
  138. is stored in ib_trans->link_rsrc. */
  139. IBDEBUG("Type is le_trans");
  140. *trans_id = (Z_LVAL_PP(link_id));
  141. ZEND_FETCH_RESOURCE(ib_trans, ibase_tr_link *, link_id, -1, "InterBase transaction", le_trans);
  142. *trans_n = ib_trans->trans_num;
  143. ZEND_FETCH_RESOURCE2(resource, ibase_db_link *, NULL, ib_trans->link_rsrc, "InterBase link", le_link, le_plink);
  144. } else {
  145. IBDEBUG("Type is le_[p]link");
  146. /* Database link resource, use default transaction (=0). */
  147. *trans_n = 0;
  148. ZEND_FETCH_RESOURCE2(resource, ibase_db_link *, link_id, -1, "InterBase link", le_link, le_plink);
  149. }
  150. }
  151. *ib_link = resource;
  152. }
  153. #define RESET_ERRMSG { IBG(errmsg)[0] = '\0';}
  154. #define TEST_ERRMSG ( IBG(errmsg)[0] != '\0')
  155. /* sql variables union
  156. used for convert and binding input variables
  157. */
  158. typedef struct {
  159. union {
  160. short sval;
  161. float fval;
  162. ISC_QUAD qval;
  163. #ifdef ISC_TIMESTAMP
  164. ISC_TIMESTAMP tsval;
  165. ISC_DATE dtval;
  166. ISC_TIME tmval;
  167. #endif
  168. } val;
  169. short sqlind;
  170. } BIND_BUF;
  171. /* get blob identifier from argument
  172. on empty unset argument ib_blob set to NULL
  173. */
  174. #define GET_BLOB_ID_ARG(blob_arg, ib_blob)\
  175. {\
  176. if (Z_TYPE_P(blob_arg) == IS_STRING && Z_STRLEN_P(blob_arg) == 0) {\
  177. ib_blob = NULL;\
  178. } else if (Z_TYPE_P(blob_arg) != IS_STRING\
  179. || Z_STRLEN_P(blob_arg) != sizeof(ibase_blob_handle)\
  180. || ((ibase_blob_handle *)(Z_STRVAL_P(blob_arg)))->bl_handle != 0){\
  181. _php_ibase_module_error("Invalid blob id");\
  182. RETURN_FALSE;\
  183. } else {\
  184. ib_blob = (ibase_blob_handle *)Z_STRVAL_P(blob_arg);\
  185. }\
  186. }
  187. /* get blob handle from argument
  188. note: blob already open when handle active
  189. */
  190. #define GET_BLOB_HANDLE_ARG(blob_arg, blob_ptr) \
  191. { \
  192. int type; \
  193. convert_to_long(blob_arg); \
  194. blob_ptr = (ibase_blob_handle *) zend_list_find(Z_LVAL_P(blob_arg), &type); \
  195. if (type!=le_blob) { \
  196. _php_ibase_module_error("%d is not blob handle", Z_LVAL_P(blob_arg)); \
  197. RETURN_FALSE; \
  198. } \
  199. }
  200. /* blob information struct */
  201. typedef struct {
  202. ISC_LONG max_segment; /* Length of longest segment */
  203. ISC_LONG num_segments; /* Total number of segments */
  204. ISC_LONG total_length; /* Total length of blob */
  205. int bl_stream; /* blob is stream ? */
  206. } IBASE_BLOBINFO;
  207. /* }}} */
  208. /* error handling ---------------------------- */
  209. /* {{{ proto string ibase_errmsg(void)
  210. Return error message */
  211. PHP_FUNCTION(ibase_errmsg)
  212. {
  213. if (ZEND_NUM_ARGS() != 0) {
  214. WRONG_PARAM_COUNT;
  215. }
  216. if (IBG(errmsg[0])) {
  217. RETURN_STRING(IBG(errmsg), 1);
  218. }
  219. RETURN_FALSE;
  220. }
  221. /* }}} */
  222. /* {{{ _php_ibase_error(TSRMLS_D)
  223. print interbase error and save it for ibase_errmsg() */
  224. static void _php_ibase_error(TSRMLS_D)
  225. {
  226. char *s;
  227. ISC_STATUS *statusp;
  228. s = IBG(errmsg);
  229. statusp = IB_STATUS;
  230. while ((s - IBG(errmsg)) < MAX_ERRMSG - (IBASE_MSGSIZE + 2) && isc_interprete(s, &statusp)) {
  231. strcat(IBG(errmsg), " ");
  232. s = IBG(errmsg) + strlen(IBG(errmsg));
  233. }
  234. php_error(E_WARNING, "%s(): %s", get_active_function_name(TSRMLS_C), IBG(errmsg));
  235. }
  236. /* }}} */
  237. /* {{{ _php_ibase_module_error()
  238. print php interbase module error and save it for ibase_errmsg() */
  239. static void _php_ibase_module_error(char *msg, ...)
  240. {
  241. va_list ap;
  242. TSRMLS_FETCH();
  243. va_start(ap, msg);
  244. /* vsnprintf NUL terminates the buf and writes at most n-1 chars+NUL */
  245. vsnprintf(IBG(errmsg), MAX_ERRMSG, msg, ap);
  246. va_end(ap);
  247. php_error(E_WARNING, "%s(): %s", get_active_function_name(TSRMLS_C), IBG(errmsg));
  248. }
  249. /* }}} */
  250. /* destructors ---------------------- */
  251. /* {{{ _php_ibase_free_xsqlda() */
  252. /* not actual destructor ... */
  253. static void _php_ibase_free_xsqlda(XSQLDA *sqlda)
  254. {
  255. int i;
  256. XSQLVAR *var;
  257. IBDEBUG("Free XSQLDA?");
  258. if (sqlda) {
  259. IBDEBUG("Freeing XSQLDA...");
  260. var = sqlda->sqlvar;
  261. for (i = 0; i < sqlda->sqld; i++, var++) {
  262. efree(var->sqldata);
  263. if(var->sqlind)
  264. efree(var->sqlind);
  265. }
  266. efree(sqlda);
  267. }
  268. }
  269. /* }}} */
  270. /* {{{ _php_ibase_commit_link() */
  271. static void _php_ibase_commit_link(ibase_db_link *link)
  272. {
  273. int i;
  274. TSRMLS_FETCH();
  275. IBDEBUG("Checking transactions to close...");
  276. if (link->trans[0] != NULL) { /* commit default */
  277. IBDEBUG("Committing default transaction...");
  278. if (isc_commit_transaction(IB_STATUS, &link->trans[0])) {
  279. _php_ibase_error(TSRMLS_C);
  280. }
  281. link->trans[0] = NULL;
  282. }
  283. for (i = 1; i < IBASE_TRANS_ON_LINK; i++) {
  284. if (link->trans[i] != NULL) {
  285. IBDEBUG("Rolling back other transactions...");
  286. if (isc_rollback_transaction(IB_STATUS, &link->trans[i])) {
  287. _php_ibase_error(TSRMLS_C);
  288. }
  289. link->trans[i] = NULL;
  290. }
  291. }
  292. }
  293. /* }}} */
  294. static void php_ibase_commit_link_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  295. {
  296. ibase_db_link *link = (ibase_db_link *)rsrc->ptr;
  297. _php_ibase_commit_link(link);
  298. }
  299. /* {{{ _php_ibase_close_link() */
  300. static void _php_ibase_close_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  301. {
  302. ibase_db_link *link = (ibase_db_link *)rsrc->ptr;
  303. _php_ibase_commit_link(link);
  304. IBDEBUG("Closing normal link...");
  305. isc_detach_database(IB_STATUS, &link->link);
  306. IBG(num_links)--;
  307. efree(link);
  308. }
  309. /* }}} */
  310. /* {{{ _php_ibase_close_plink() */
  311. static void _php_ibase_close_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  312. {
  313. ibase_db_link *link = (ibase_db_link *)rsrc->ptr;
  314. _php_ibase_commit_link(link);
  315. IBDEBUG("Closing permanent link...");
  316. isc_detach_database(IB_STATUS, &link->link);
  317. IBG(num_persistent)--;
  318. IBG(num_links)--;
  319. free(link);
  320. }
  321. /* }}} */
  322. /* {{{ _php_ibase_free_result() */
  323. static void _php_ibase_free_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  324. {
  325. char tr_items[] = {isc_info_tra_id };
  326. char tmp[32]; /* should be enough as on the Api doc */
  327. ibase_result *ib_result = (ibase_result *)rsrc->ptr;
  328. IBDEBUG("Freeing result...");
  329. if (ib_result){
  330. _php_ibase_free_xsqlda(ib_result->out_sqlda);
  331. isc_transaction_info(IB_STATUS, &ib_result->trans,sizeof(tr_items), tr_items, sizeof(tmp), tmp );
  332. /* we have a transaction still open and we really want to drop the statement ? */
  333. if ( !(IB_STATUS[0] && IB_STATUS[1]) && ib_result->drop_stmt && ib_result->stmt ) {
  334. IBDEBUG("Dropping statement handle (free_result)...");
  335. if (isc_dsql_free_statement(IB_STATUS, &ib_result->stmt, DSQL_drop)) {
  336. _php_ibase_error(TSRMLS_C);
  337. }
  338. } else {
  339. /* Shouldn't be here unless query was select and had parameter
  340. placeholders, in which case ibase_execute handles this???
  341. (Testing seems to confirm the decision was a right one.)
  342. */
  343. IBDEBUG("Closing statement handle...");
  344. /*
  345. if (isc_dsql_free_statement(IB_STATUS, &ib_result->stmt, DSQL_close)) {
  346. _php_ibase_error();
  347. }
  348. */
  349. }
  350. if (ib_result->out_array) {
  351. efree(ib_result->out_array);
  352. }
  353. efree(ib_result);
  354. }
  355. }
  356. /* }}} */
  357. /* {{{ _php_ibase_free_query() */
  358. static void _php_ibase_free_query(ibase_query *ib_query)
  359. {
  360. char tr_items[] = {isc_info_tra_id };
  361. char tmp[32] ; /* ...should be enough as on the Api doc */
  362. TSRMLS_FETCH();
  363. IBDEBUG("Freeing query...");
  364. if (ib_query) {
  365. if (ib_query->in_sqlda) {
  366. efree(ib_query->in_sqlda);
  367. }
  368. if (ib_query->out_sqlda) {
  369. efree(ib_query->out_sqlda);
  370. }
  371. isc_transaction_info(IB_STATUS, &ib_query->trans,sizeof(tr_items), tr_items, sizeof(tmp), tmp );
  372. /* we have the trans still open and a statement to drop? */
  373. if ( !(IB_STATUS[0] && IB_STATUS[1]) && ib_query->stmt) {
  374. IBDEBUG("Dropping statement handle (free_query)...");
  375. if (isc_dsql_free_statement(IB_STATUS, &ib_query->stmt, DSQL_drop)){
  376. _php_ibase_error(TSRMLS_C);
  377. }
  378. }
  379. if (ib_query->in_array) {
  380. efree(ib_query->in_array);
  381. }
  382. if (ib_query->out_array) {
  383. efree(ib_query->out_array);
  384. }
  385. efree(ib_query);
  386. }
  387. }
  388. /* }}} */
  389. static void php_ibase_free_query_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  390. {
  391. ibase_query *query = (ibase_query *)rsrc->ptr;
  392. _php_ibase_free_query(query);
  393. }
  394. /* {{{ _php_ibase_free_blob() */
  395. static void _php_ibase_free_blob(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  396. {
  397. ibase_blob_handle *ib_blob = (ibase_blob_handle *)rsrc->ptr;
  398. if (ib_blob->bl_handle != NULL) { /* blob open*/
  399. if (isc_cancel_blob(IB_STATUS, &ib_blob->bl_handle)) {
  400. php_error(E_ERROR, "You can lose data. Close any blob after reading of writing it. Use ibase_blob_close() before calling ibase_close()");
  401. }
  402. }
  403. efree(ib_blob);
  404. }
  405. /* }}} */
  406. /* {{{ _php_ibase_free_trans() */
  407. static void _php_ibase_free_trans(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  408. {
  409. ibase_tr_link *ib_trans = (ibase_tr_link *)rsrc->ptr;
  410. ibase_db_link *ib_link;
  411. int type;
  412. void *ptr;
  413. ptr = zend_list_find(ib_trans->link_rsrc, &type); /* check if the link is still there */
  414. if (ptr && (type==le_link || type==le_plink)) {
  415. ib_link = (ibase_db_link *) zend_fetch_resource(NULL TSRMLS_CC, ib_trans->link_rsrc, "InterBase link", NULL, 2, le_link, le_plink);
  416. if (ib_link) {
  417. if (ib_link->trans[ib_trans->trans_num] != NULL) {
  418. IBDEBUG("Rolling back unhandled transaction...");
  419. if (isc_rollback_transaction(IB_STATUS, &ib_link->trans[ib_trans->trans_num])) {
  420. _php_ibase_error(TSRMLS_C);
  421. }
  422. ib_link->trans[ib_trans->trans_num] = NULL;
  423. }
  424. }
  425. }
  426. efree(ib_trans);
  427. }
  428. /* }}} */
  429. /* {{{ startup, shutdown and info functions */
  430. PHP_INI_BEGIN()
  431. STD_PHP_INI_BOOLEAN("ibase.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateInt, allow_persistent, zend_ibase_globals, ibase_globals)
  432. STD_PHP_INI_ENTRY_EX("ibase.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_persistent, zend_ibase_globals, ibase_globals, display_link_numbers)
  433. STD_PHP_INI_ENTRY_EX("ibase.max_links", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_links, zend_ibase_globals, ibase_globals, display_link_numbers)
  434. STD_PHP_INI_ENTRY("ibase.default_user", NULL, PHP_INI_ALL, OnUpdateString, default_user, zend_ibase_globals, ibase_globals)
  435. STD_PHP_INI_ENTRY("ibase.default_password", NULL, PHP_INI_ALL, OnUpdateString, default_password, zend_ibase_globals, ibase_globals)
  436. STD_PHP_INI_ENTRY("ibase.timestampformat", "%m/%d/%Y %H:%M:%S", PHP_INI_ALL, OnUpdateString, cfg_timestampformat, zend_ibase_globals, ibase_globals)
  437. STD_PHP_INI_ENTRY("ibase.dateformat", "%m/%d/%Y", PHP_INI_ALL, OnUpdateString, cfg_dateformat, zend_ibase_globals, ibase_globals)
  438. STD_PHP_INI_ENTRY("ibase.timeformat", "%H:%M:%S", PHP_INI_ALL, OnUpdateString, cfg_timeformat, zend_ibase_globals, ibase_globals)
  439. PHP_INI_END()
  440. static void php_ibase_init_globals(zend_ibase_globals *ibase_globals)
  441. {
  442. ibase_globals->timestampformat = NULL;
  443. ibase_globals->dateformat = NULL;
  444. ibase_globals->timeformat = NULL;
  445. ibase_globals->errmsg = NULL;
  446. ibase_globals->num_persistent = 0;
  447. }
  448. PHP_MINIT_FUNCTION(ibase)
  449. {
  450. ZEND_INIT_MODULE_GLOBALS(ibase, php_ibase_init_globals, NULL);
  451. REGISTER_INI_ENTRIES();
  452. le_result = zend_register_list_destructors_ex(_php_ibase_free_result, NULL, "interbase result", module_number);
  453. le_query = zend_register_list_destructors_ex(php_ibase_free_query_rsrc, NULL, "interbase query", module_number);
  454. le_blob = zend_register_list_destructors_ex(_php_ibase_free_blob, NULL, "interbase blob", module_number);
  455. le_link = zend_register_list_destructors_ex(_php_ibase_close_link, NULL, "interbase link", module_number);
  456. le_plink = zend_register_list_destructors_ex(php_ibase_commit_link_rsrc, _php_ibase_close_plink, "interbase link persistent", module_number);
  457. le_trans = zend_register_list_destructors_ex(_php_ibase_free_trans, NULL, "interbase transaction", module_number);
  458. REGISTER_LONG_CONSTANT("IBASE_DEFAULT", PHP_IBASE_DEFAULT, CONST_PERSISTENT);
  459. REGISTER_LONG_CONSTANT("IBASE_TEXT", PHP_IBASE_TEXT, CONST_PERSISTENT);
  460. REGISTER_LONG_CONSTANT("IBASE_UNIXTIME", PHP_IBASE_UNIXTIME, CONST_PERSISTENT);
  461. REGISTER_LONG_CONSTANT("IBASE_READ", PHP_IBASE_READ, CONST_PERSISTENT);
  462. REGISTER_LONG_CONSTANT("IBASE_COMMITTED", PHP_IBASE_COMMITTED, CONST_PERSISTENT);
  463. REGISTER_LONG_CONSTANT("IBASE_CONSISTENCY", PHP_IBASE_CONSISTENCY, CONST_PERSISTENT);
  464. REGISTER_LONG_CONSTANT("IBASE_NOWAIT", PHP_IBASE_NOWAIT, CONST_PERSISTENT);
  465. REGISTER_LONG_CONSTANT("IBASE_TIMESTAMP", PHP_IBASE_TIMESTAMP, CONST_PERSISTENT);
  466. REGISTER_LONG_CONSTANT("IBASE_DATE", PHP_IBASE_DATE, CONST_PERSISTENT);
  467. REGISTER_LONG_CONSTANT("IBASE_TIME", PHP_IBASE_TIME, CONST_PERSISTENT);
  468. return SUCCESS;
  469. }
  470. PHP_RINIT_FUNCTION(ibase)
  471. {
  472. IBG(default_link)= -1;
  473. IBG(num_links) = IBG(num_persistent);
  474. if (IBG(timestampformat))
  475. DL_FREE(IBG(timestampformat));
  476. IBG(timestampformat) = DL_STRDUP(IBG(cfg_timestampformat));
  477. if (IBG(dateformat))
  478. DL_FREE(IBG(dateformat));
  479. IBG(dateformat) = DL_STRDUP(IBG(cfg_dateformat));
  480. if (IBG(timeformat))
  481. DL_FREE(IBG(timeformat));
  482. IBG(timeformat) = DL_STRDUP(IBG(cfg_timeformat));
  483. if (IBG(errmsg))
  484. DL_FREE(IBG(errmsg));
  485. IBG(errmsg) = DL_MALLOC(sizeof(char)*MAX_ERRMSG+1);
  486. return SUCCESS;
  487. }
  488. PHP_MSHUTDOWN_FUNCTION(ibase)
  489. {
  490. UNREGISTER_INI_ENTRIES();
  491. return SUCCESS;
  492. }
  493. PHP_RSHUTDOWN_FUNCTION(ibase)
  494. {
  495. if (IBG(timestampformat))
  496. DL_FREE(IBG(timestampformat));
  497. IBG(timestampformat) = NULL;
  498. if (IBG(dateformat))
  499. DL_FREE(IBG(dateformat));
  500. IBG(dateformat) = NULL;
  501. if (IBG(timeformat))
  502. DL_FREE(IBG(timeformat));
  503. IBG(timeformat) = NULL;
  504. if (IBG(errmsg))
  505. DL_FREE(IBG(errmsg));
  506. IBG(errmsg) = NULL;
  507. return SUCCESS;
  508. }
  509. PHP_MINFO_FUNCTION(ibase)
  510. {
  511. char tmp[32];
  512. php_info_print_table_start();
  513. php_info_print_table_row(2, "Interbase Support", "enabled");
  514. php_info_print_table_row(2, "Revision", "$Revision$");
  515. #ifdef COMPILE_DL_INTERBASE
  516. php_info_print_table_row(2, "Dynamic Module", "yes");
  517. #endif
  518. php_info_print_table_row(2, "Allow Persistent Links", (IBG(allow_persistent)?"Yes":"No") );
  519. if (IBG(max_persistent) == -1) {
  520. snprintf(tmp, 31, "%ld/unlimited", IBG(num_persistent));
  521. } else {
  522. snprintf(tmp, 31, "%ld/%ld", IBG(num_persistent), IBG(max_persistent));
  523. }
  524. tmp[31]=0;
  525. php_info_print_table_row(2, "Persistent Links", tmp );
  526. if (IBG(max_links) == -1) {
  527. snprintf(tmp, 31, "%ld/unlimited", IBG(num_links));
  528. } else {
  529. snprintf(tmp, 31, "%ld/%ld", IBG(num_links), IBG(max_links));
  530. }
  531. tmp[31]=0;
  532. php_info_print_table_row(2, "Total Links", tmp );
  533. php_info_print_table_row(2, "Timestamp Format", IBG(timestampformat) );
  534. php_info_print_table_row(2, "Date Format", IBG(dateformat) );
  535. php_info_print_table_row(2, "Time Format", IBG(timeformat) );
  536. php_info_print_table_end();
  537. }
  538. /* }}} */
  539. /* {{{ _php_ibase_attach_db() */
  540. static int _php_ibase_attach_db(char *server, char *uname, char *passwd, char *charset, int buffers, char *role, isc_db_handle *db)
  541. {
  542. char dpb_buffer[256], *dpb, *p;
  543. int dpb_length, len;
  544. TSRMLS_FETCH();
  545. dpb = dpb_buffer;
  546. *dpb++ = isc_dpb_version1;
  547. if (uname != NULL && (len = strlen(uname))) {
  548. *dpb++ = isc_dpb_user_name;
  549. *dpb++ = len;
  550. for (p = uname; *p;) {
  551. *dpb++ = *p++;
  552. }
  553. }
  554. if (passwd != NULL && (len = strlen(passwd))) {
  555. *dpb++ = isc_dpb_password;
  556. *dpb++ = strlen(passwd);
  557. for (p = passwd; *p;) {
  558. *dpb++ = *p++;
  559. }
  560. }
  561. if (charset != NULL && (len = strlen(charset))) {
  562. *dpb++ = isc_dpb_lc_ctype;
  563. *dpb++ = strlen(charset);
  564. for (p = charset; *p;) {
  565. *dpb++ = *p++;
  566. }
  567. }
  568. if (buffers) {
  569. *dpb++ = isc_dpb_num_buffers;
  570. *dpb++ = 1;
  571. *dpb++ = buffers;
  572. }
  573. #ifdef isc_dpb_sql_role_name
  574. if (role != NULL && (len = strlen(role))) {
  575. *dpb++ = isc_dpb_sql_role_name;
  576. *dpb++ = strlen(role);
  577. for (p = role; *p;) {
  578. *dpb++ = *p++;
  579. }
  580. }
  581. #endif
  582. dpb_length = dpb - dpb_buffer;
  583. if (isc_attach_database(IB_STATUS, (short) strlen(server), server, db, (short) dpb_length, dpb_buffer)) {
  584. _php_ibase_error(TSRMLS_C);
  585. return FAILURE;
  586. }
  587. return SUCCESS;
  588. }
  589. /* }}} */
  590. /* {{{ _php_ibase_connect() */
  591. static void _php_ibase_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  592. {
  593. pval ***args;
  594. char *ib_server = NULL, *ib_uname, *ib_passwd, *ib_charset = NULL, *ib_buffers = NULL, *ib_dialect = NULL, *ib_role = NULL;
  595. int i, ib_uname_len, ib_passwd_len;
  596. isc_db_handle db_handle = NULL;
  597. char *hashed_details;
  598. int hashed_details_length = 0;
  599. ibase_db_link *ib_link = NULL;
  600. RESET_ERRMSG;
  601. ib_uname = IBG(default_user);
  602. ib_passwd = IBG(default_password);
  603. ib_uname_len = ib_uname ? strlen(ib_uname) : 0;
  604. ib_passwd_len = ib_passwd ? strlen(ib_passwd) : 0;
  605. if(ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 7){
  606. WRONG_PARAM_COUNT;
  607. }
  608. args = (pval ***) emalloc(sizeof(pval **)*ZEND_NUM_ARGS());
  609. if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
  610. efree(args);
  611. RETURN_FALSE;
  612. }
  613. switch(ZEND_NUM_ARGS()) {
  614. case 7:
  615. convert_to_string_ex(args[6]);
  616. ib_role = Z_STRVAL_PP(args[6]);
  617. hashed_details_length += Z_STRLEN_PP(args[6]);
  618. /* fallout */
  619. case 6:
  620. convert_to_string_ex(args[5]);
  621. ib_dialect = Z_STRVAL_PP(args[5]);
  622. hashed_details_length += Z_STRLEN_PP(args[5]);
  623. /* fallout */
  624. case 5:
  625. convert_to_string_ex(args[4]);
  626. ib_buffers = Z_STRVAL_PP(args[4]);
  627. hashed_details_length += Z_STRLEN_PP(args[4]);
  628. /* fallout */
  629. case 4:
  630. convert_to_string_ex(args[3]);
  631. ib_charset = Z_STRVAL_PP(args[3]);
  632. hashed_details_length += Z_STRLEN_PP(args[3]);
  633. /* fallout */
  634. case 3:
  635. convert_to_string_ex(args[2]);
  636. ib_passwd = Z_STRVAL_PP(args[2]);
  637. hashed_details_length += Z_STRLEN_PP(args[2]);
  638. /* fallout */
  639. case 2:
  640. convert_to_string_ex(args[1]);
  641. ib_uname = Z_STRVAL_PP(args[1]);
  642. hashed_details_length += Z_STRLEN_PP(args[1]);
  643. /* fallout */
  644. case 1:
  645. convert_to_string_ex(args[0]);
  646. ib_server = Z_STRVAL_PP(args[0]);
  647. hashed_details_length += Z_STRLEN_PP(args[0]);
  648. } /* case */
  649. efree(args);
  650. hashed_details = (char *) emalloc(hashed_details_length+strlen("ibase_%s_%s_%s_%s_%s_%s_%s")+1);
  651. sprintf(hashed_details, "ibase_%s_%s_%s_%s_%s_%s_%s", SAFE_STRING(ib_server), SAFE_STRING(ib_uname), SAFE_STRING(ib_passwd), SAFE_STRING(ib_charset), SAFE_STRING(ib_buffers), SAFE_STRING(ib_dialect), SAFE_STRING(ib_role));
  652. if (persistent) {
  653. list_entry *le;
  654. int open_new_connection = 1;
  655. if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length+1, (void **) &le) != FAILURE) {
  656. char tmp_1[] = {isc_info_base_level, isc_info_end};
  657. char tmp_2[8]; /* Enough? Hope so... */
  658. if (Z_TYPE_P(le) != le_plink) {
  659. RETURN_FALSE;
  660. }
  661. /* Check if connection has timed out */
  662. ib_link = (ibase_db_link *) le->ptr;
  663. if (!isc_database_info(IB_STATUS, &ib_link->link, sizeof(tmp_1), tmp_1, sizeof(tmp_2), tmp_2)) {
  664. open_new_connection = 0;
  665. }
  666. }
  667. /* There was no previous connection to use or it has timed out */
  668. if (open_new_connection) {
  669. list_entry new_le;
  670. if ((IBG(max_links) != -1) && (IBG(num_links) >= IBG(max_links))) {
  671. _php_ibase_module_error("Too many open links (%d)", IBG(num_links));
  672. efree(hashed_details);
  673. RETURN_FALSE;
  674. }
  675. if ((IBG(max_persistent) != -1) && (IBG(num_persistent) >= IBG(max_persistent))) {
  676. _php_ibase_module_error("Too many open persistent links (%d)", IBG(num_persistent));
  677. efree(hashed_details);
  678. RETURN_FALSE;
  679. }
  680. /* create the ib_link */
  681. if (_php_ibase_attach_db(ib_server, ib_uname, ib_passwd, ib_charset,
  682. (ib_buffers ? strtoul(ib_buffers, NULL, 0) : 0),
  683. ib_role, &db_handle) == FAILURE) {
  684. efree(hashed_details);
  685. RETURN_FALSE;
  686. }
  687. ib_link = (ibase_db_link *) malloc(sizeof(ibase_db_link));
  688. ib_link->link = db_handle;
  689. ib_link->dialect = (ib_dialect ? (unsigned short) strtoul(ib_dialect, NULL, 10) : SQL_DIALECT_CURRENT);
  690. for (i = 0; i < IBASE_TRANS_ON_LINK; i++)
  691. ib_link->trans[i] = NULL;
  692. /* hash it up */
  693. Z_TYPE(new_le) = le_plink;
  694. new_le.ptr = ib_link;
  695. if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(list_entry), NULL)==FAILURE) {
  696. efree(hashed_details);
  697. free(ib_link);
  698. RETURN_FALSE;
  699. }
  700. IBG(num_links)++;
  701. IBG(num_persistent)++;
  702. }
  703. ZEND_REGISTER_RESOURCE(return_value, ib_link, le_plink);
  704. } else {
  705. list_entry *index_ptr, new_index_ptr;
  706. /* first we check the hash for the hashed_details key. if it exists,
  707. * it should point us to the right offset where the actual ib_link sits.
  708. * if it doesn't, open a new ib_link, add it to the resource list,
  709. * and add a pointer to it with hashed_details as the key.
  710. */
  711. if (zend_hash_find(&EG(regular_list), hashed_details, hashed_details_length+1, (void **) &index_ptr)==SUCCESS) {
  712. int type, xlink;
  713. void *ptr;
  714. if (Z_TYPE_P(index_ptr) != le_index_ptr) {
  715. RETURN_FALSE;
  716. }
  717. xlink = (int) index_ptr->ptr;
  718. ptr = zend_list_find(xlink, &type); /* check if the xlink is still there */
  719. if (ptr && (type==le_link || type==le_plink)) {
  720. zend_list_addref(xlink);
  721. Z_LVAL_P(return_value) = xlink;
  722. Z_TYPE_P(return_value) = IS_RESOURCE;
  723. IBG(default_link) = Z_LVAL_P(return_value);
  724. efree(hashed_details);
  725. return;
  726. } else {
  727. zend_hash_del(&EG(regular_list), hashed_details, hashed_details_length+1);
  728. }
  729. }
  730. if ((IBG(max_links) != -1) && (IBG(num_links) >= IBG(max_links))) {
  731. _php_ibase_module_error("Too many open links (%d)", IBG(num_links));
  732. efree(hashed_details);
  733. RETURN_FALSE;
  734. }
  735. /* create the ib_link */
  736. if (_php_ibase_attach_db(ib_server, ib_uname, ib_passwd, ib_charset,
  737. (ib_buffers ? strtoul(ib_buffers, NULL, 0) : 0),
  738. ib_role, &db_handle) == FAILURE) {
  739. efree(hashed_details);
  740. RETURN_FALSE;
  741. }
  742. ib_link = (ibase_db_link *) emalloc(sizeof(ibase_db_link));
  743. ib_link->link = db_handle;
  744. ib_link->dialect = (ib_dialect ? (unsigned short) strtoul(ib_dialect, NULL, 10) : SQL_DIALECT_CURRENT);
  745. for (i = 0; i < IBASE_TRANS_ON_LINK; i++)
  746. ib_link->trans[i] = NULL;
  747. ZEND_REGISTER_RESOURCE(return_value, ib_link, le_link);
  748. /* add it to the hash */
  749. new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
  750. Z_TYPE(new_index_ptr) = le_index_ptr;
  751. if (zend_hash_update(&EG(regular_list), hashed_details, hashed_details_length+1, (void *) &new_index_ptr, sizeof(list_entry), NULL)==FAILURE) {
  752. efree(hashed_details);
  753. RETURN_FALSE;
  754. }
  755. IBG(num_links)++;
  756. }
  757. efree(hashed_details);
  758. zend_list_addref(Z_LVAL_P(return_value));
  759. IBG(default_link) = Z_LVAL_P(return_value);
  760. }
  761. /* }}} */
  762. /* {{{ proto int ibase_connect(string database [, string username] [, string password] [, string charset] [, int buffers] [, int dialect] [, string role])
  763. Open a connection to an InterBase database */
  764. PHP_FUNCTION(ibase_connect)
  765. {
  766. _php_ibase_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  767. }
  768. /* }}} */
  769. /* {{{ proto int ibase_pconnect(string database [, string username] [, string password] [, string charset] [, int buffers] [, int dialect] [, string role])
  770. Open a persistent connection to an InterBase database */
  771. PHP_FUNCTION(ibase_pconnect)
  772. {
  773. _php_ibase_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  774. }
  775. /* }}} */
  776. /* {{{ proto int ibase_close([int link_identifier])
  777. Close an InterBase connection */
  778. PHP_FUNCTION(ibase_close)
  779. {
  780. zval **link_arg;
  781. ibase_db_link *ib_link;
  782. int link_id;
  783. RESET_ERRMSG;
  784. switch (ZEND_NUM_ARGS()) {
  785. case 0:
  786. link_id = IBG(default_link);
  787. break;
  788. case 1:
  789. if (zend_get_parameters_ex(1, &link_arg) == FAILURE) {
  790. RETURN_FALSE;
  791. }
  792. convert_to_long_ex(link_arg);
  793. link_id = Z_LVAL_PP(link_arg);
  794. break;
  795. default:
  796. WRONG_PARAM_COUNT;
  797. break;
  798. }
  799. ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, link_arg, link_id, "InterBase link", le_link, le_plink);
  800. zend_list_delete(link_id);
  801. RETURN_TRUE;
  802. }
  803. /* }}} */
  804. /* {{{ _php_ibase_alloc_array() */
  805. static int _php_ibase_alloc_array(ibase_array **ib_arrayp, int *array_cntp,
  806. XSQLDA *sqlda, isc_db_handle link, isc_tr_handle trans)
  807. {
  808. #define IB_ARRAY (*ib_arrayp)
  809. int i, dim, ar_cnt, ar_length;
  810. XSQLVAR *var;
  811. TSRMLS_FETCH();
  812. IB_ARRAY = NULL;
  813. ar_cnt = 0; /* find arrays */
  814. var = sqlda->sqlvar;
  815. for (i = 0; i < sqlda->sqld; i++, var++) {
  816. if ((var->sqltype & ~1) == SQL_ARRAY)
  817. ar_cnt++;
  818. }
  819. if (ar_cnt) { /* have arrays ? */
  820. *array_cntp = ar_cnt;
  821. IB_ARRAY = emalloc(sizeof(ibase_array)*ar_cnt);
  822. ar_cnt = 0;
  823. var = sqlda->sqlvar;
  824. for (i = 0; i < sqlda->sqld; i++, var++) {
  825. if ((var->sqltype & ~1) == SQL_ARRAY) {
  826. ISC_ARRAY_DESC *ar_desc = &IB_ARRAY[ar_cnt].ar_desc;
  827. if (isc_array_lookup_bounds(IB_STATUS, &link, &trans,
  828. var->relname, var->sqlname, ar_desc)) {
  829. _php_ibase_error(TSRMLS_C);
  830. efree(IB_ARRAY);
  831. IB_ARRAY = NULL;
  832. return FAILURE;
  833. }
  834. switch (ar_desc->array_desc_dtype) {
  835. case blr_text:
  836. case blr_text2:
  837. IB_ARRAY[ar_cnt].el_type = SQL_TEXT;
  838. IB_ARRAY[ar_cnt].el_size = ar_desc->array_desc_length+1;
  839. break;
  840. case blr_short:
  841. IB_ARRAY[ar_cnt].el_type = SQL_SHORT;
  842. IB_ARRAY[ar_cnt].el_size = sizeof(short);
  843. break;
  844. case blr_long:
  845. IB_ARRAY[ar_cnt].el_type = SQL_LONG;
  846. IB_ARRAY[ar_cnt].el_size = sizeof(long);
  847. break;
  848. case blr_float:
  849. IB_ARRAY[ar_cnt].el_type = SQL_FLOAT;
  850. IB_ARRAY[ar_cnt].el_size = sizeof(float);
  851. break;
  852. case blr_double:
  853. IB_ARRAY[ar_cnt].el_type = SQL_DOUBLE;
  854. IB_ARRAY[ar_cnt].el_size = sizeof(double);
  855. break;
  856. case blr_date:
  857. IB_ARRAY[ar_cnt].el_type = SQL_DATE;
  858. IB_ARRAY[ar_cnt].el_size = sizeof(ISC_QUAD);
  859. break;
  860. case blr_varying:
  861. case blr_varying2: /* changed to SQL_TEXT ? */
  862. /* sql_type = SQL_VARYING; Why? FIXME: ??? */
  863. IB_ARRAY[ar_cnt].el_type = SQL_TEXT;
  864. IB_ARRAY[ar_cnt].el_size = ar_desc->array_desc_length+sizeof(short);
  865. break;
  866. default:
  867. _php_ibase_module_error("Unexpected array type %d in relation '%s' column '%s'",
  868. ar_desc->array_desc_dtype, var->relname, var->sqlname);
  869. efree(IB_ARRAY);
  870. IB_ARRAY = NULL;
  871. return FAILURE;
  872. } /* switch array_desc_type */
  873. ar_length = 0; /* calculate elements count */
  874. for (dim = 0; dim < ar_desc->array_desc_dimensions; dim++) {
  875. ar_length += 1 + ar_desc->array_desc_bounds[dim].array_bound_upper
  876. - ar_desc->array_desc_bounds[dim].array_bound_lower;
  877. }
  878. IB_ARRAY[ar_cnt].ar_size = IB_ARRAY[ar_cnt].el_size * ar_length;
  879. ar_cnt++;
  880. } /* if SQL_ARRAY */
  881. } /* for column */
  882. } /* if array_cnt */
  883. return SUCCESS;
  884. #undef IB_ARRAY
  885. }
  886. /* }}} */
  887. /* {{{ _php_ibase_alloc_query() */
  888. /* allocate and prepare query */
  889. static int _php_ibase_alloc_query(ibase_query **ib_queryp, isc_db_handle link, isc_tr_handle trans, char *query, unsigned short dialect)
  890. {
  891. #define IB_QUERY (*ib_queryp)
  892. TSRMLS_FETCH();
  893. IB_QUERY = emalloc(sizeof(ibase_query));
  894. IB_QUERY->link = link;
  895. IB_QUERY->trans = trans;
  896. IB_QUERY->stmt = NULL;
  897. IB_QUERY->out_sqlda = NULL;
  898. IB_QUERY->in_sqlda = NULL;
  899. IB_QUERY->in_array = NULL;
  900. IB_QUERY->in_array_cnt = 0;
  901. IB_QUERY->out_array = NULL;
  902. IB_QUERY->out_array_cnt = 0;
  903. IB_QUERY->dialect = dialect;
  904. if (isc_dsql_allocate_statement(IB_STATUS, &link, &IB_QUERY->stmt)) {
  905. _php_ibase_error(TSRMLS_C);
  906. goto _php_ibase_alloc_query_error;
  907. }
  908. IB_QUERY->out_sqlda = (XSQLDA *) emalloc(XSQLDA_LENGTH(0));
  909. IB_QUERY->out_sqlda->sqln = 0;
  910. IB_QUERY->out_sqlda->version = SQLDA_VERSION1;
  911. if (isc_dsql_prepare(IB_STATUS, &IB_QUERY->trans, &IB_QUERY->stmt, 0, query, dialect, IB_QUERY->out_sqlda)) {
  912. _php_ibase_error(TSRMLS_C);
  913. goto _php_ibase_alloc_query_error;
  914. }
  915. /* not enough output variables ? */
  916. if (IB_QUERY->out_sqlda->sqld > IB_QUERY->out_sqlda->sqln) {
  917. IB_QUERY->out_sqlda = erealloc(IB_QUERY->out_sqlda, XSQLDA_LENGTH(IB_QUERY->out_sqlda->sqld));
  918. IB_QUERY->out_sqlda->sqln = IB_QUERY->out_sqlda->sqld;
  919. IB_QUERY->out_sqlda->version = SQLDA_VERSION1;
  920. if (isc_dsql_describe(IB_STATUS, &IB_QUERY->stmt, SQLDA_VERSION1, IB_QUERY->out_sqlda)) {
  921. _php_ibase_error(TSRMLS_C);
  922. goto _php_ibase_alloc_query_error;
  923. }
  924. }
  925. /* maybe have input placeholders? */
  926. IB_QUERY->in_sqlda = emalloc(XSQLDA_LENGTH(0));
  927. IB_QUERY->in_sqlda->sqln = 0;
  928. IB_QUERY->in_sqlda->version = SQLDA_VERSION1;
  929. if (isc_dsql_describe_bind(IB_STATUS, &IB_QUERY->stmt, SQLDA_VERSION1, IB_QUERY->in_sqlda)) {
  930. _php_ibase_error(TSRMLS_C);
  931. goto _php_ibase_alloc_query_error;
  932. }
  933. /* not enough input variables ? */
  934. if (IB_QUERY->in_sqlda->sqln < IB_QUERY->in_sqlda->sqld) {
  935. IB_QUERY->in_sqlda = erealloc(IB_QUERY->in_sqlda, XSQLDA_LENGTH(IB_QUERY->in_sqlda->sqld));
  936. IB_QUERY->in_sqlda->sqln = IB_QUERY->in_sqlda->sqld;
  937. IB_QUERY->in_sqlda->version = SQLDA_VERSION1;
  938. if (isc_dsql_describe_bind(IB_STATUS, &IB_QUERY->stmt, SQLDA_VERSION1, IB_QUERY->in_sqlda)) {
  939. _php_ibase_error(TSRMLS_C);
  940. goto _php_ibase_alloc_query_error;
  941. }
  942. }
  943. /* allocate arrays... */
  944. if (_php_ibase_alloc_array(&IB_QUERY->in_array, &IB_QUERY->in_array_cnt,
  945. IB_QUERY->in_sqlda, link, trans) == FAILURE) {
  946. goto _php_ibase_alloc_query_error; /* error report already done */
  947. }
  948. if (_php_ibase_alloc_array(&IB_QUERY->out_array, &IB_QUERY->out_array_cnt,
  949. IB_QUERY->out_sqlda, link, trans) == FAILURE) {
  950. goto _php_ibase_alloc_query_error;
  951. }
  952. /* no, haven't placeholders at all */
  953. if (IB_QUERY->in_sqlda->sqld == 0) {
  954. efree(IB_QUERY->in_sqlda);
  955. IB_QUERY->in_sqlda = NULL;
  956. }
  957. if (IB_QUERY->out_sqlda->sqld == 0) {
  958. efree(IB_QUERY->out_sqlda);
  959. IB_QUERY->out_sqlda = NULL;
  960. }
  961. return SUCCESS;
  962. _php_ibase_alloc_query_error:
  963. if (IB_QUERY->out_sqlda)
  964. efree(IB_QUERY->out_sqlda);
  965. if (IB_QUERY->in_sqlda)
  966. efree(IB_QUERY->in_sqlda);
  967. if (IB_QUERY->out_array)
  968. efree(IB_QUERY->out_array);
  969. efree(IB_QUERY);
  970. IB_QUERY = NULL;
  971. return FAILURE;
  972. #undef IB_QUERY
  973. }
  974. /* }}} */
  975. /* {{{ _php_ibase_bind()
  976. Bind parameter placeholders in a previously prepared query */
  977. static int _php_ibase_bind(XSQLDA *sqlda, pval **b_vars, BIND_BUF *buf, ibase_query *ib_query)
  978. {
  979. XSQLVAR *var;
  980. pval *b_var;
  981. int i;
  982. var = sqlda->sqlvar;
  983. for (i = 0; i < sqlda->sqld; var++, i++) { /* binded vars */
  984. buf[i].sqlind = 0;
  985. var->sqlind = &buf[i].sqlind;
  986. b_var = b_vars[i];
  987. if (Z_TYPE_P(b_var) == IS_NULL) {
  988. static char nothing[64];
  989. static short null_flag = -1;
  990. var->sqldata = nothing;
  991. var->sqltype |= 1;
  992. var->sqlind = &null_flag;
  993. if (var->sqllen > 64)
  994. var->sqllen = 64;
  995. } else
  996. switch(var->sqltype & ~1) {
  997. case SQL_TEXT: /* direct to variable */
  998. case SQL_VARYING:
  999. convert_to_string(b_var);
  1000. var->sqldata = (void ISC_FAR *)Z_STRVAL_P(b_var);
  1001. var->sqllen = Z_STRLEN_P(b_var);
  1002. var->sqltype = SQL_TEXT + (var->sqltype & 1);
  1003. break;
  1004. case SQL_SHORT:
  1005. convert_to_long(b_var);
  1006. if (Z_LVAL_P(b_var) > SHRT_MAX || Z_LVAL_P(b_var) < SHRT_MIN) {
  1007. _php_ibase_module_error("Field %*s overflow", var->aliasname_length, var->aliasname);
  1008. return FAILURE;
  1009. }
  1010. buf[i].val.sval = (short)Z_LVAL_P(b_var);
  1011. var->sqldata = (void ISC_FAR *)(&buf[i].val.sval);
  1012. break;
  1013. case SQL_LONG:
  1014. if (var->sqlscale < 0) {
  1015. /*
  1016. DECIMAL or NUMERIC field stored internally as scaled integer.
  1017. Coerce it to string and let InterBase's internal routines
  1018. handle it.
  1019. */
  1020. convert_to_string(b_var);
  1021. var->sqldata = (void ISC_FAR *)Z_STRVAL_P(b_var);
  1022. var->sqllen = Z_STRLEN_P(b_var);
  1023. var->sqltype = SQL_TEXT;
  1024. } else {
  1025. convert_to_long(b_var);
  1026. var->sqldata = (void ISC_FAR *)(&Z_LVAL_P(b_var));
  1027. }
  1028. break;
  1029. case SQL_FLOAT:
  1030. convert_to_double(b_var);
  1031. buf[i].val.fval = (float)Z_DVAL_P(b_var);
  1032. var->sqldata = (void ISC_FAR *)(&buf[i].val.fval);
  1033. break;
  1034. case SQL_DOUBLE: /* direct to variable */
  1035. convert_to_double(b_var);
  1036. var->sqldata = (void ISC_FAR *)(&Z_DVAL_P(b_var));
  1037. break;
  1038. #ifdef SQL_INT64
  1039. case SQL_INT64:
  1040. /*
  1041. Just let InterBase's internal routines handle it.
  1042. Besides, it might even have originally been a string
  1043. to avoid rounding errors...
  1044. */
  1045. convert_to_string(b_var);
  1046. var->sqldata = (void ISC_FAR *)Z_STRVAL_P(b_var);
  1047. var->sqllen = Z_STRLEN_P(b_var);
  1048. var->sqltype = SQL_TEXT;
  1049. break;
  1050. #endif
  1051. #ifndef SQL_TIMESTAMP
  1052. case SQL_DATE:
  1053. #else
  1054. case SQL_TIMESTAMP:
  1055. case SQL_TYPE_DATE:
  1056. case SQL_TYPE_TIME:
  1057. #endif
  1058. #ifndef HAVE_STRPTIME
  1059. #ifndef SQL_TIMESTAMP
  1060. /* Parsing doesn't seem to happen with older versions... */
  1061. {
  1062. struct tm t;
  1063. int n;
  1064. t.tm_year = t.tm_mon = t.tm_mday = t.tm_hour =
  1065. t.tm_min = t.tm_sec = 0;
  1066. convert_to_string(b_var);
  1067. n = sscanf(Z_STRVAL_P(b_var), "%d%*[/]%d%*[/]%d %d%*[:]%d%*[:]%d",
  1068. &t.tm_mon, &t.tm_mday, &t.tm_year, &t.tm_hour, &t.tm_min, &t.tm_sec);
  1069. if(n != 3 && n != 6){
  1070. _php_ibase_module_error("Invalid date/time format: Expected 3 or 6 fields, got %d. Use format m/d/Y H:i:s. You gave '%s'", n, Z_STRVAL_P(b_var));
  1071. return FAILURE;
  1072. }
  1073. t.tm_year -= 1900;
  1074. t.tm_mon--;
  1075. isc_encode_date(&t, &buf[i].val.qval);
  1076. var->sqldata = (void ISC_FAR *)(&buf[i].val.qval);
  1077. }
  1078. #else
  1079. /*
  1080. Once again, InterBase's internal parsing routines
  1081. seems to be a good solution... Might change this on
  1082. platforms that have strptime()? Code is there and works,
  1083. but the functions existence is not yet tested...
  1084. ask Sascha?
  1085. */
  1086. convert_to_string(b_var);
  1087. var->sqldata = (void ISC_FAR *)Z_STRVAL_P(b_var);
  1088. var->sqllen = Z_STRLEN_P(b_var);
  1089. var->sqltype = SQL_TEXT;
  1090. #endif
  1091. #else
  1092. {
  1093. struct tm t;
  1094. convert_to_string(b_var);
  1095. #ifndef SQL_TIMESTAMP
  1096. strptime(Z_STRVAL_P(b_var), IBG(timestampformat), &t);
  1097. isc_encode_date(&t, &buf[i].val.qval);
  1098. var->sqldata = (void ISC_FAR *)(&buf[i].val.qval);
  1099. #else
  1100. switch (var->sqltype & ~1) {
  1101. case SQL_TIMESTAMP:
  1102. strptime(Z_STRVAL_P(b_var), IBG(timestampformat), &t);
  1103. isc_encode_timestamp(&t, &buf[i].val.tsval);
  1104. var->sqldata = (void ISC_FAR *)(&buf[i].val.tsval);
  1105. break;
  1106. case SQL_TYPE_DATE:
  1107. strptime(Z_STRVAL_P(b_var), IBG(dateformat), &t);
  1108. isc_encode_sql_date(&t, &buf[i].val.dtval);
  1109. var->sqldata = (void ISC_FAR *)(&buf[i].val.dtval);
  1110. break;
  1111. case SQL_TYPE_TIME:
  1112. strptime(Z_STRVAL_P(b_var), IBG(timeformat), &t);
  1113. isc_encode_sql_time(&t, &buf[i].val.tmval);
  1114. var->sqldata = (void ISC_FAR *)(&buf[i].val.tmval);
  1115. break;
  1116. }
  1117. #endif
  1118. }
  1119. #endif
  1120. break;
  1121. case SQL_BLOB:
  1122. {
  1123. ibase_blob_handle *ib_blob_id;
  1124. if (Z_TYPE_P(b_var) != IS_STRING
  1125. || Z_STRLEN_P(b_var) != sizeof(ibase_blob_handle)
  1126. || ((ibase_blob_handle *)(Z_STRVAL_P(b_var)))->bl_handle != 0) {
  1127. ibase_blob_handle *ib_blob;
  1128. TSRMLS_FETCH();
  1129. ib_blob = (ibase_blob_handle *) emalloc(sizeof(ibase_blob_handle));
  1130. ib_blob->trans_handle = ib_query->trans;
  1131. ib_blob->link = ib_query->link;
  1132. ib_blob->bl_handle = NULL;
  1133. if (isc_create_blob(IB_STATUS, &ib_blob->link, &ib_blob->trans_handle, &ib_blob->bl_handle, &ib_blob->bl_qd)) {
  1134. efree(ib_blob);
  1135. _php_ibase_error(TSRMLS_C);
  1136. return FAILURE;
  1137. }
  1138. convert_to_string(b_var);
  1139. if (isc_put_segment(IB_STATUS, &ib_blob->bl_handle, (unsigned short) Z_STRLEN_P(b_var), Z_STRVAL_P(b_var))) {
  1140. _php_ibase_error(TSRMLS_C);
  1141. return FAILURE;
  1142. }
  1143. if (isc_close_blob(IB_STATUS, &ib_blob->bl_handle)) {
  1144. _php_ibase_error(TSRMLS_C);
  1145. return FAILURE;
  1146. }
  1147. ib_blob_id = ib_blob;
  1148. var->sqldata = (void ISC_FAR *)&ib_blob_id->bl_qd;
  1149. /*
  1150. _php_ibase_module_error("Invalid blob id string");
  1151. return FAILURE;
  1152. */
  1153. } else {
  1154. ib_blob_id = (ibase_blob_handle *)Z_STRVAL_P(b_var);
  1155. var->sqldata = (void ISC_FAR *)&ib_blob_id->bl_qd;
  1156. }
  1157. }
  1158. break;
  1159. case SQL_ARRAY:
  1160. _php_ibase_module_error("Binding arrays not supported yet");
  1161. return FAILURE;
  1162. break;
  1163. } /*switch*/
  1164. } /*for*/
  1165. return SUCCESS;
  1166. }
  1167. /* }}} */
  1168. /* {{{ _php_ibase_alloc_xsqlda() */
  1169. static void _php_ibase_alloc_xsqlda(XSQLDA *sqlda)
  1170. {
  1171. int i;
  1172. XSQLVAR *var = sqlda->sqlvar;
  1173. for (i = 0; i < sqlda->sqld; i++, var++) {
  1174. switch(var->sqltype & ~1) {
  1175. case SQL_TEXT:
  1176. var->sqldata = emalloc(sizeof(char)*(var->sqllen));
  1177. break;
  1178. case SQL_VARYING:
  1179. var->sqldata = emalloc(sizeof(char)*(var->sqllen+sizeof(short)));
  1180. break;
  1181. case SQL_SHORT:
  1182. var->sqldata = emalloc(sizeof(short));
  1183. break;
  1184. case SQL_LONG:
  1185. var->sqldata = emalloc(sizeof(long));
  1186. break;
  1187. case SQL_FLOAT:
  1188. var->sqldata = emalloc(sizeof(float));
  1189. break;
  1190. case SQL_DOUBLE:
  1191. var->sqldata = emalloc(sizeof(double));
  1192. break;
  1193. #ifdef SQL_INT64
  1194. case SQL_INT64:
  1195. var->sqldata = emalloc(sizeof(ISC_INT64));
  1196. break;
  1197. #endif
  1198. #ifdef SQL_TIMESTAMP
  1199. case SQL_TIMESTAMP:
  1200. var->sqldata = emalloc(sizeof(ISC_TIMESTAMP));
  1201. break;
  1202. case SQL_TYPE_DATE:
  1203. var->sqldata = emalloc(sizeof(ISC_DATE));
  1204. break;
  1205. case SQL_TYPE_TIME:
  1206. var->sqldata = emalloc(sizeof(ISC_TIME));
  1207. break;
  1208. #else
  1209. case SQL_DATE:
  1210. #endif
  1211. case SQL_BLOB:
  1212. case SQL_ARRAY:
  1213. var->sqldata = emalloc(sizeof(ISC_QUAD));
  1214. break;
  1215. } /*switch*/
  1216. if (var->sqltype & 1) /* sql NULL flag */
  1217. var->sqlind = emalloc(sizeof(short));
  1218. else
  1219. var->sqlind = NULL;
  1220. } /* for*/
  1221. }
  1222. /* }}} */
  1223. /* {{{ _php_ibase_exec() */
  1224. static int _php_ibase_exec(ibase_result **ib_resultp, ibase_query *ib_query, int argc, pval **args)
  1225. {
  1226. #define IB_RESULT (*ib_resultp)
  1227. XSQLDA *in_sqlda = NULL, *out_sqlda = NULL;
  1228. BIND_BUF *bind_buf = NULL;
  1229. int rv = FAILURE;
  1230. TSRMLS_FETCH();
  1231. IB_RESULT = NULL;
  1232. /* allocate sqlda and output buffers */
  1233. if (ib_query->out_sqlda) { /* output variables in select, select for update */
  1234. IBDEBUG("Query wants XSQLDA for output");
  1235. IB_RESULT = emalloc(sizeof(ibase_result));
  1236. IB_RESULT->link = ib_query->link;
  1237. IB_RESULT->trans = ib_query->trans;
  1238. IB_RESULT->stmt = ib_query->stmt;
  1239. IB_RESULT->drop_stmt = 0; /* when free result close but not drop!*/
  1240. out_sqlda = IB_RESULT->out_sqlda = emalloc(XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
  1241. memcpy(out_sqlda, ib_query->out_sqlda, XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
  1242. _php_ibase_alloc_xsqlda(out_sqlda);
  1243. if (ib_query->out_array) {
  1244. IB_RESULT->out_array = emalloc(sizeof(ibase_array)*ib_query->out_array_cnt);
  1245. memcpy(IB_RESULT->out_array, ib_query->out_array, sizeof(ibase_array)*ib_query->out_array_cnt);
  1246. } else {
  1247. IB_RESULT->out_array = NULL;
  1248. }
  1249. }
  1250. if (ib_query->in_sqlda) { /* has placeholders */
  1251. IBDEBUG("Query wants XSQLDA for input");
  1252. if (ib_query->in_sqlda->sqld != argc) {
  1253. _php_ibase_module_error("Placeholders (%d) and variables (%d) mismatch", ib_query->in_sqlda->sqld, argc);
  1254. goto _php_ibase_exec_error; /* yes mommy, goto! */
  1255. }
  1256. in_sqlda = emalloc(XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
  1257. memcpy(in_sqlda, ib_query->in_sqlda, XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
  1258. bind_buf = emalloc(sizeof(BIND_BUF) * ib_query->in_sqlda->sqld);
  1259. if (_php_ibase_bind(in_sqlda, args, bind_buf, ib_query) == FAILURE) {
  1260. IBDEBUG("Could not bind input XSQLDA");
  1261. goto _php_ibase_exec_error;
  1262. }
  1263. }
  1264. if (isc_dsql_execute(IB_STATUS, &ib_query->trans, &ib_query->stmt, ib_query->dialect, in_sqlda)) {
  1265. IBDEBUG("Could not execute query");
  1266. _php_ibase_error(TSRMLS_C);
  1267. goto _php_ibase_exec_error;
  1268. }
  1269. rv = SUCCESS;
  1270. _php_ibase_exec_error: /* I'm a bad boy... */
  1271. if (in_sqlda){
  1272. efree(in_sqlda);
  1273. }
  1274. if (bind_buf)
  1275. efree(bind_buf);
  1276. if (rv == FAILURE) {
  1277. if (IB_RESULT) {
  1278. efree(IB_RESULT);
  1279. IB_RESULT = NULL;
  1280. }
  1281. if (out_sqlda)
  1282. _php_ibase_free_xsqlda(out_sqlda);
  1283. }
  1284. return rv;
  1285. #undef IB_RESULT
  1286. }
  1287. /* }}} */
  1288. /* {{{ proto int ibase_trans([int trans_args [, int link_identifier]])
  1289. Start transaction */
  1290. PHP_FUNCTION(ibase_trans)
  1291. {
  1292. pval ***args;
  1293. char tpb[20], *tpbp = NULL;
  1294. long trans_argl = 0;
  1295. int tpb_len = 0, argn, link_id, trans_n = 0, i;
  1296. ibase_db_link *ib_link;
  1297. ibase_tr_link *ib_trans;
  1298. RESET_ERRMSG;
  1299. link_id = IBG(default_link);
  1300. /* TODO: multi-databases trans */
  1301. argn = ZEND_NUM_ARGS();
  1302. if (argn < 0 || argn > 20) {
  1303. WRONG_PARAM_COUNT;
  1304. }
  1305. if (argn) {
  1306. args = (pval ***) emalloc(sizeof(pval **)*argn);
  1307. if (zend_get_parameters_array_ex(argn, args) == FAILURE) {
  1308. efree(args);
  1309. RETURN_FALSE;
  1310. }
  1311. /* Handle all database links, although we don't support multibase
  1312. transactions yet, so only the last one is will be used. */
  1313. for (i = argn-1; i > 0 && Z_TYPE_PP(args[i]) == IS_RESOURCE; i--) {
  1314. ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, args[i], -1, "InterBase link", le_link, le_plink);
  1315. link_id = Z_LVAL_PP(args[i]);
  1316. }
  1317. /* First argument is transaction parameters */
  1318. convert_to_long_ex(args[0]);
  1319. trans_argl = Z_LVAL_PP(args[0]);
  1320. efree(args);
  1321. }
  1322. if (argn < 2) {
  1323. ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, NULL, link_id, "InterBase link", le_link, le_plink);
  1324. }
  1325. if (trans_argl ) {
  1326. tpb[tpb_len++] = isc_tpb_version3;
  1327. tpbp = tpb;
  1328. /* access mode */
  1329. if (trans_argl & PHP_IBASE_READ) /* READ ONLY TRANSACTION */
  1330. tpb[tpb_len++] = isc_tpb_read;
  1331. else
  1332. tpb[tpb_len++] = isc_tpb_write;
  1333. /* isolation level */
  1334. if (trans_argl & PHP_IBASE_COMMITTED) {
  1335. tpb[tpb_len++] = isc_tpb_read_committed;
  1336. } else if (trans_argl & PHP_IBASE_CONSISTENCY)
  1337. tpb[tpb_len++] = isc_tpb_consistency;
  1338. else
  1339. tpb[tpb_len++] = isc_tpb_concurrency;
  1340. /* lock resolution */
  1341. if (trans_argl & PHP_IBASE_NOWAIT)
  1342. tpb[tpb_len++] = isc_tpb_nowait;
  1343. else
  1344. tpb[tpb_len++] = isc_tpb_wait;
  1345. }
  1346. /* find empty transaction slot */
  1347. for (trans_n = 0; trans_n < IBASE_TRANS_ON_LINK
  1348. && ib_link->trans[trans_n]; trans_n++)
  1349. ;
  1350. if (trans_n == IBASE_TRANS_ON_LINK) {
  1351. _php_ibase_module_error("Too many transactions on link");
  1352. RETURN_FALSE;
  1353. }
  1354. if (isc_start_transaction(IB_STATUS, &ib_link->trans[trans_n], 1, &ib_link->link, tpb_len, tpbp)) {
  1355. _php_ibase_error(TSRMLS_C);
  1356. RETURN_FALSE;
  1357. }
  1358. ib_trans = (ibase_tr_link *) emalloc(sizeof(ibase_tr_link));
  1359. ib_trans->trans_num = trans_n;
  1360. ib_trans->link_rsrc = link_id;
  1361. ZEND_REGISTER_RESOURCE(return_value, ib_trans, le_trans);
  1362. }
  1363. /* }}} */
  1364. /* {{{ _php_ibase_def_trans() */
  1365. /* open default transaction */
  1366. static int _php_ibase_def_trans(ibase_db_link * ib_link, int trans_n)
  1367. {
  1368. TSRMLS_FETCH();
  1369. if (trans_n == 0 && ib_link->trans[0] == NULL) {
  1370. if (isc_start_transaction(IB_STATUS, &ib_link->trans[0], 1, &ib_link->link, 0, NULL)) {
  1371. _php_ibase_error(TSRMLS_C);
  1372. return FAILURE;
  1373. }
  1374. }
  1375. return SUCCESS;
  1376. }
  1377. /*}}}*/
  1378. /* {{{ _php_ibase_trans_end() */
  1379. #define COMMIT 1
  1380. #define ROLLBACK 0
  1381. static void _php_ibase_trans_end(INTERNAL_FUNCTION_PARAMETERS, int commit)
  1382. {
  1383. pval **link_trans_arg = NULL;
  1384. int link_id = 0, trans_n = 0, trans_id = 0;
  1385. ibase_db_link *ib_link;
  1386. RESET_ERRMSG;
  1387. switch (ZEND_NUM_ARGS()) {
  1388. case 0:
  1389. link_id = IBG(default_link);
  1390. ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, link_trans_arg, link_id, "InterBase link", le_link, le_plink);
  1391. break;
  1392. case 1:
  1393. if (zend_get_parameters_ex(1, &link_trans_arg) == FAILURE) {
  1394. RETURN_FALSE;
  1395. }
  1396. get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, link_trans_arg, &ib_link, &trans_n, &trans_id);
  1397. break;
  1398. default:
  1399. WRONG_PARAM_COUNT;
  1400. break;
  1401. }
  1402. if (ib_link->trans[trans_n] == NULL) {
  1403. php_error(E_WARNING, "Trying to commit or rollback an already handled transaction");
  1404. RETURN_FALSE;
  1405. }
  1406. if (commit) {
  1407. if (isc_commit_transaction(IB_STATUS, &ib_link->trans[trans_n])) {
  1408. _php_ibase_error(TSRMLS_C);
  1409. RETURN_FALSE;
  1410. }
  1411. } else {
  1412. if (isc_rollback_transaction(IB_STATUS, &ib_link->trans[trans_n])) {
  1413. _php_ibase_error(TSRMLS_C);
  1414. RETURN_FALSE;
  1415. }
  1416. }
  1417. ib_link->trans[trans_n] = NULL;
  1418. /* Don't try to destroy imnplicitly opened transaction from list... */
  1419. if (trans_id) {
  1420. zend_list_delete(trans_id);
  1421. }
  1422. RETURN_TRUE;
  1423. }
  1424. /* }}} */
  1425. /* {{{ proto int ibase_commit([int link_identifier, ] int trans_number)
  1426. Commit transaction */
  1427. PHP_FUNCTION(ibase_commit)
  1428. {
  1429. _php_ibase_trans_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, COMMIT);
  1430. }
  1431. /* }}} */
  1432. /* {{{ proto int ibase_rollback([int link_identifier, ] int trans_number)
  1433. Roolback transaction */
  1434. PHP_FUNCTION(ibase_rollback)
  1435. {
  1436. _php_ibase_trans_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, ROLLBACK);
  1437. }
  1438. /* }}} */
  1439. /* {{{ proto int ibase_query([int link_identifier, ] string query [, int bind_args])
  1440. Execute a query */
  1441. PHP_FUNCTION(ibase_query)
  1442. {
  1443. pval ***args, **bind_args = NULL, **dummy = NULL;
  1444. int i, link_id = 0, trans_n = 0, bind_n = 0, trans_id = 0;
  1445. char *query;
  1446. ibase_db_link *ib_link;
  1447. ibase_query *ib_query;
  1448. ibase_result *ib_result;
  1449. RESET_ERRMSG;
  1450. if (ZEND_NUM_ARGS() < 1) {
  1451. WRONG_PARAM_COUNT;
  1452. }
  1453. args = (pval ***) emalloc(sizeof(pval **)*ZEND_NUM_ARGS());
  1454. if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
  1455. efree(args);
  1456. RETURN_FALSE;
  1457. }
  1458. i = 0;
  1459. if (Z_TYPE_PP(args[i]) == IS_RESOURCE) { /* link or transaction argument */
  1460. get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, args[i], &ib_link, &trans_n, &trans_id);
  1461. i++; /* next arg */
  1462. } else {
  1463. link_id = IBG(default_link);
  1464. ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, dummy, link_id, "InterBase link", le_link, le_plink);
  1465. }
  1466. if (Z_TYPE_PP(args[i]) == IS_STRING) { /* query argument */
  1467. convert_to_string_ex(args[i]);
  1468. query = Z_STRVAL_PP(args[i]);
  1469. i++; /* next arg */
  1470. } else {
  1471. _php_ibase_module_error("Query argument missed");
  1472. efree(args);
  1473. RETURN_FALSE;
  1474. }
  1475. if (ZEND_NUM_ARGS() > i) { /* have variables to bind */
  1476. /* XXX Remove or fix??? Variable placeholders and binding makes
  1477. absolutely no sense if not using a prepared SQL statement.
  1478. */
  1479. bind_n = ZEND_NUM_ARGS() - i;
  1480. bind_args = args[i];
  1481. }
  1482. /* open default transaction */
  1483. if (_php_ibase_def_trans(ib_link, trans_n) == FAILURE) {
  1484. efree(args);
  1485. RETURN_FALSE;
  1486. }
  1487. if (_php_ibase_alloc_query(&ib_query, ib_link->link, ib_link->trans[trans_n], query, ib_link->dialect) == FAILURE) {
  1488. efree(args);
  1489. RETURN_FALSE;
  1490. }
  1491. if (_php_ibase_exec(&ib_result, ib_query, bind_n, bind_args) == FAILURE) {
  1492. _php_ibase_free_query(ib_query);
  1493. efree(args);
  1494. RETURN_FALSE;
  1495. }
  1496. efree(args);
  1497. if (ib_result) { /* select statement */
  1498. ib_result->drop_stmt = 1; /* drop stmt when free result */
  1499. ib_query->stmt = NULL; /* keep stmt when free query */
  1500. _php_ibase_free_query(ib_query);
  1501. ZEND_REGISTER_RESOURCE(return_value, ib_result, le_result);
  1502. } else {
  1503. _php_ibase_free_query(ib_query);
  1504. RETURN_TRUE;
  1505. }
  1506. }
  1507. /* }}} */
  1508. /* {{{ _php_ibase_var_pval() */
  1509. static int _php_ibase_var_pval(pval *val, void *data, int type, int len, int scale, int flag TSRMLS_DC)
  1510. {
  1511. char string_data[255];
  1512. switch(type & ~1) {
  1513. case SQL_VARYING:
  1514. len = ((IBASE_VCHAR *) data)->var_len;
  1515. data = ((IBASE_VCHAR *) data)->var_str;
  1516. /* fallout */
  1517. case SQL_TEXT:
  1518. Z_STRVAL_P(val) = (char *)emalloc(sizeof(char)*(len+1));
  1519. memcpy(Z_STRVAL_P(val), data, len);
  1520. Z_STRVAL_P(val)[len] = '\0';
  1521. if (PG(magic_quotes_runtime)) {
  1522. Z_STRVAL_P(val) = php_addslashes(Z_STRVAL_P(val), len, &len, 1 TSRMLS_CC);
  1523. }
  1524. Z_TYPE_P(val) = IS_STRING;
  1525. Z_STRLEN_P(val) = len;
  1526. break;
  1527. case SQL_LONG:
  1528. if (scale) {
  1529. int j, f = 1;
  1530. float n = (float) *(long *)(data);
  1531. for (j = 0; j < -scale; j++) {
  1532. f *= 10;
  1533. }
  1534. Z_TYPE_P(val) = IS_STRING;
  1535. Z_STRLEN_P(val) = sprintf(string_data, "%.*f", -scale, n / f);
  1536. Z_STRVAL_P(val) = estrdup(string_data);
  1537. } else {
  1538. Z_TYPE_P(val) = IS_LONG;
  1539. Z_LVAL_P(val) = *(long *)(data);
  1540. }
  1541. break;
  1542. case SQL_SHORT:
  1543. Z_TYPE_P(val) = IS_LONG;
  1544. Z_LVAL_P(val) = *(short *)(data);
  1545. break;
  1546. case SQL_FLOAT:
  1547. Z_TYPE_P(val) = IS_DOUBLE;
  1548. Z_DVAL_P(val) = *(float *)(data);
  1549. break;
  1550. case SQL_DOUBLE:
  1551. if (scale) {
  1552. Z_TYPE_P(val) = IS_STRING;
  1553. Z_STRLEN_P(val) = sprintf(string_data, "%.*f", -scale, *(double *)data);
  1554. Z_STRVAL_P(val) = estrdup(string_data);
  1555. /*
  1556. Z_STRVAL_P(val) = string_data;
  1557. */
  1558. } else {
  1559. Z_TYPE_P(val) = IS_DOUBLE;
  1560. Z_DVAL_P(val) = *(double *)data;
  1561. }
  1562. break;
  1563. #ifdef SQL_INT64
  1564. case SQL_INT64:
  1565. val->type = IS_STRING;
  1566. if (scale) {
  1567. int j, f = 1;
  1568. double number = (double) ((ISC_INT64) (*((ISC_INT64 *)data)));
  1569. char dt[20];
  1570. for (j = 0; j < -scale; j++) {
  1571. f *= 10;
  1572. }
  1573. sprintf(dt, "%%0.%df", -scale);
  1574. val->value.str.len = sprintf (string_data, dt, number/f );
  1575. } else {
  1576. val->value.str.len = sprintf (string_data, "%Ld",
  1577. (ISC_INT64) (*((ISC_INT64 *)data)));
  1578. }
  1579. val->value.str.val = estrdup(string_data);
  1580. break;
  1581. #endif
  1582. #ifndef SQL_TIMESTAMP
  1583. case SQL_DATE:
  1584. #else
  1585. case SQL_TIMESTAMP:
  1586. case SQL_TYPE_DATE:
  1587. case SQL_TYPE_TIME:
  1588. #endif
  1589. {
  1590. struct tm t;
  1591. char *format = NULL;
  1592. long timestamp = -1;
  1593. #ifndef SQL_TIMESTAMP
  1594. isc_decode_date((ISC_QUAD *) data, &t);
  1595. format = IBG(timestampformat);
  1596. #else
  1597. switch (type & ~1) {
  1598. case SQL_TIMESTAMP:
  1599. isc_decode_timestamp((ISC_TIMESTAMP *) data, &t);
  1600. format = IBG(timestampformat);
  1601. break;
  1602. case SQL_TYPE_DATE:
  1603. isc_decode_sql_date((ISC_DATE *) data, &t);
  1604. format = IBG(dateformat);
  1605. break;
  1606. case SQL_TYPE_TIME:
  1607. isc_decode_sql_time((ISC_TIME *) data, &t);
  1608. format = IBG(timeformat);
  1609. break;
  1610. }
  1611. #endif
  1612. /*
  1613. XXX - Might have to remove this later - seems that isc_decode_date()
  1614. always sets tm_isdst to 0, sometimes incorrectly (InterBase 6 bug?)
  1615. */
  1616. t.tm_isdst = -1;
  1617. timestamp = mktime(&t);
  1618. #if HAVE_TM_ZONE
  1619. t.tm_zone = tzname[0];
  1620. #endif
  1621. if (flag & PHP_IBASE_UNIXTIME) {
  1622. Z_TYPE_P(val) = IS_LONG;
  1623. Z_LVAL_P(val) = timestamp;
  1624. } else {
  1625. Z_TYPE_P(val) = IS_STRING;
  1626. #if HAVE_STRFTIME
  1627. Z_STRLEN_P(val) = strftime(string_data, sizeof(string_data), format, &t);
  1628. #else
  1629. /* FIXME */
  1630. if (!t.tm_hour && !t.tm_min && !t.tm_sec)
  1631. Z_STRLEN_P(val) = sprintf(string_data, "%02d/%02d/%4d", t.tm_mon+1, t.tm_mday, t.tm_year+1900);
  1632. else
  1633. Z_STRLEN_P(val) = sprintf(string_data, "%02d/%02d/%4d %02d:%02d:%02d",
  1634. t.tm_mon+1, t.tm_mday, t.tm_year+1900, t.tm_hour, t.tm_min, t.tm_sec);
  1635. #endif
  1636. Z_STRVAL_P(val) = estrdup(string_data);
  1637. /*
  1638. Z_STRVAL_P(val) = string_data;
  1639. */
  1640. break;
  1641. }
  1642. }
  1643. default:
  1644. return FAILURE;
  1645. } /* switch (type) */
  1646. return SUCCESS;
  1647. }
  1648. /* }}} */
  1649. /* {{{ _php_ibase_arr_pval() */
  1650. /* create multidimension array - resursion function
  1651. (*datap) argument changed */
  1652. static int _php_ibase_arr_pval(pval *ar_pval, char **datap, ibase_array *ib_array, int dim, int flag TSRMLS_DC)
  1653. {
  1654. pval tmp;
  1655. int i, dim_len, l_bound, u_bound;
  1656. if (dim > 16) { /* InterBase limit */
  1657. _php_ibase_module_error("Too many dimensions");
  1658. return FAILURE;
  1659. }
  1660. u_bound = ib_array->ar_desc.array_desc_bounds[dim].array_bound_upper;
  1661. l_bound = ib_array->ar_desc.array_desc_bounds[dim].array_bound_lower;
  1662. dim_len = 1 + u_bound - l_bound;
  1663. if (dim < ib_array->ar_desc.array_desc_dimensions - 1) { /* array again */
  1664. for (i = 0; i < dim_len; i++) {
  1665. /* recursion here */
  1666. if (_php_ibase_arr_pval(ar_pval, datap, ib_array, dim+1, flag TSRMLS_CC) == FAILURE) {
  1667. return FAILURE;
  1668. }
  1669. }
  1670. } else { /* data at last */
  1671. array_init(ar_pval);
  1672. for (i = 0; i < dim_len; i++) {
  1673. if (_php_ibase_var_pval(&tmp, *datap, ib_array->el_type,
  1674. ib_array->ar_desc.array_desc_length,
  1675. ib_array->ar_desc.array_desc_scale,
  1676. flag TSRMLS_CC) == FAILURE){
  1677. return FAILURE;
  1678. }
  1679. /* FIXME ??? */
  1680. zend_hash_index_update(Z_ARRVAL_P(ar_pval),
  1681. l_bound + i,
  1682. (void *) &tmp, sizeof(pval), NULL);
  1683. *datap += ib_array->el_size;
  1684. }
  1685. }
  1686. return SUCCESS;
  1687. }
  1688. /* }}} */
  1689. /* {{{ _php_ibase_fetch_hash() */
  1690. #define FETCH_ROW 2
  1691. #define FETCH_ARRAY 4
  1692. static void _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int fetch_type)
  1693. {
  1694. zval **result_arg, **flag_arg;
  1695. long flag = 0;
  1696. int i, arr_cnt;
  1697. ibase_result *ib_result;
  1698. XSQLVAR *var;
  1699. RESET_ERRMSG;
  1700. switch(ZEND_NUM_ARGS()) {
  1701. case 1:
  1702. if (ZEND_NUM_ARGS()==1 && zend_get_parameters_ex(1, &result_arg)==FAILURE) {
  1703. RETURN_FALSE;
  1704. }
  1705. break;
  1706. case 2:
  1707. if (ZEND_NUM_ARGS()==2 && zend_get_parameters_ex(2, &result_arg, &flag_arg)==FAILURE) {
  1708. RETURN_FALSE;
  1709. }
  1710. convert_to_long_ex(flag_arg);
  1711. flag = Z_LVAL_PP(flag_arg);
  1712. break;
  1713. default:
  1714. WRONG_PARAM_COUNT;
  1715. break;
  1716. }
  1717. ZEND_FETCH_RESOURCE(ib_result, ibase_result *, result_arg, -1, "InterBase result", le_result);
  1718. if (ib_result->out_sqlda == NULL) {
  1719. _php_ibase_module_error("Trying to fetch results from a non-select query");
  1720. RETURN_FALSE;
  1721. }
  1722. if (isc_dsql_fetch(IB_STATUS, &ib_result->stmt, 1, ib_result->out_sqlda) == 100L) {
  1723. RETURN_FALSE; /* end of cursor */
  1724. }
  1725. if (IB_STATUS[0] && IB_STATUS[1]) { /* error in fetch */
  1726. _php_ibase_error(TSRMLS_C);
  1727. RETURN_FALSE;
  1728. }
  1729. if (array_init(return_value)==FAILURE) {
  1730. RETURN_FALSE;
  1731. }
  1732. arr_cnt = 0;
  1733. var = ib_result->out_sqlda->sqlvar;
  1734. for (i = 0; i < ib_result->out_sqlda->sqld; i++, var++) {
  1735. if (((var->sqltype & 1) == 0) || *var->sqlind != -1) {
  1736. pval *tmp;
  1737. tmp = emalloc(sizeof(pval));
  1738. switch(var->sqltype & ~1) {
  1739. case SQL_VARYING:
  1740. case SQL_TEXT:
  1741. case SQL_SHORT:
  1742. case SQL_LONG:
  1743. case SQL_FLOAT:
  1744. case SQL_DOUBLE:
  1745. #ifdef SQL_INT64
  1746. case SQL_INT64:
  1747. #endif
  1748. #ifndef SQL_TIMESTAMP
  1749. case SQL_DATE:
  1750. #else
  1751. case SQL_TIMESTAMP:
  1752. case SQL_TYPE_DATE:
  1753. case SQL_TYPE_TIME:
  1754. #endif
  1755. _php_ibase_var_pval(tmp, var->sqldata, var->sqltype, var->sqllen, var->sqlscale, flag TSRMLS_CC);
  1756. break;
  1757. case SQL_BLOB:
  1758. if (flag & PHP_IBASE_TEXT) { /* text ? */
  1759. int stat;
  1760. isc_blob_handle bl_handle = NULL;
  1761. ISC_LONG max_len = 0, cur_len = 0;
  1762. char bl_items[1], *bl_data, bl_info[20], *p;
  1763. if (isc_open_blob(IB_STATUS, &ib_result->link, &ib_result->trans, &bl_handle, (ISC_QUAD ISC_FAR *) var->sqldata)) {
  1764. _php_ibase_error(TSRMLS_C);
  1765. RETURN_FALSE;
  1766. }
  1767. bl_items[0] = isc_info_blob_total_length;
  1768. if (isc_blob_info(IB_STATUS, &bl_handle, sizeof(bl_items), bl_items, sizeof(bl_info), bl_info)) {
  1769. _php_ibase_error(TSRMLS_C);
  1770. RETURN_FALSE;
  1771. }
  1772. /* find total length of blob's data */
  1773. for (p = bl_info; *p != isc_info_end && p < bl_info+sizeof(bl_info);) {
  1774. unsigned short item_len, item = *p++;
  1775. item_len = (short) isc_vax_integer(p, 2);
  1776. p += 2;
  1777. if (item == isc_info_blob_total_length)
  1778. max_len = isc_vax_integer(p, item_len);
  1779. p += item_len;
  1780. }
  1781. bl_data = emalloc(max_len+1);
  1782. for (cur_len = stat = 0; stat == 0 && cur_len < max_len; ) {
  1783. unsigned short seg_len;
  1784. unsigned short max_seg = (unsigned short) (max_len - cur_len > USHRT_MAX ? USHRT_MAX : max_len-cur_len);
  1785. stat = isc_get_segment(IB_STATUS, &bl_handle, &seg_len, max_seg, &bl_data[cur_len]);
  1786. cur_len += seg_len;
  1787. if (cur_len > max_len) { /* never!*/
  1788. efree(bl_data);
  1789. _php_ibase_module_error("PHP module internal error");
  1790. RETURN_FALSE;
  1791. }
  1792. }
  1793. if (IB_STATUS[0] && IB_STATUS[1] && (IB_STATUS[1] != isc_segstr_eof)) {
  1794. efree(bl_data);
  1795. _php_ibase_error(TSRMLS_C);
  1796. RETURN_FALSE;
  1797. }
  1798. bl_data[cur_len] = '\0';
  1799. if (isc_close_blob(IB_STATUS, &bl_handle)) {
  1800. efree(bl_data);
  1801. _php_ibase_error(TSRMLS_C);
  1802. RETURN_FALSE;
  1803. }
  1804. Z_TYPE_P(tmp) = IS_STRING;
  1805. Z_STRLEN_P(tmp) = cur_len;
  1806. Z_STRVAL_P(tmp) = estrndup(bl_data, cur_len);
  1807. efree(bl_data);
  1808. } else { /* blob id only */
  1809. ISC_QUAD *bl_qd = (ISC_QUAD ISC_FAR *) var->sqldata;
  1810. ibase_blob_handle *ib_blob_id;
  1811. ib_blob_id = (ibase_blob_handle *) emalloc(sizeof(ibase_blob_handle));
  1812. ib_blob_id->link = ib_result->link;
  1813. ib_blob_id->trans_handle = ib_result->trans;
  1814. ib_blob_id->bl_qd.gds_quad_high = bl_qd->gds_quad_high;
  1815. ib_blob_id->bl_qd.gds_quad_low = bl_qd->gds_quad_low;
  1816. ib_blob_id->bl_handle = NULL;
  1817. Z_TYPE_P(tmp) = IS_STRING;
  1818. Z_STRLEN_P(tmp) = sizeof(ibase_blob_handle);
  1819. Z_STRVAL_P(tmp) = estrndup((char *)ib_blob_id, sizeof(ibase_blob_handle));
  1820. efree(ib_blob_id);
  1821. }
  1822. break;
  1823. case SQL_ARRAY:{
  1824. ISC_QUAD ar_qd = *(ISC_QUAD ISC_FAR *) var->sqldata;
  1825. ibase_array *ib_array = &ib_result->out_array[arr_cnt];
  1826. void *ar_data;
  1827. char *tmp_ptr;
  1828. ar_data = emalloc(ib_array->ar_size);
  1829. if (isc_array_get_slice(IB_STATUS, &ib_result->link, &ib_result->trans,
  1830. &ar_qd, &ib_array->ar_desc, ar_data, &ib_array->ar_size)) {
  1831. _php_ibase_error(TSRMLS_C);
  1832. RETURN_FALSE;
  1833. }
  1834. tmp_ptr = ar_data; /* avoid changes in _arr_pval */
  1835. if (_php_ibase_arr_pval(tmp, &tmp_ptr, ib_array, 0, flag TSRMLS_CC) == FAILURE) {
  1836. RETURN_FALSE;
  1837. }
  1838. efree(ar_data);
  1839. }
  1840. break;
  1841. default:
  1842. break;
  1843. } /*switch*/
  1844. if (fetch_type & FETCH_ROW) {
  1845. switch (Z_TYPE_P(tmp)) {
  1846. case IS_STRING:
  1847. add_index_stringl(return_value, i, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), 0);
  1848. break;
  1849. case IS_LONG:
  1850. add_index_long(return_value, i, Z_LVAL_P(tmp));
  1851. break;
  1852. case IS_DOUBLE:
  1853. add_index_double(return_value, i, Z_DVAL_P(tmp));
  1854. break;
  1855. }
  1856. } else {
  1857. switch (Z_TYPE_P(tmp)) {
  1858. case IS_STRING:
  1859. add_assoc_stringl(return_value, var->aliasname, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), 0);
  1860. break;
  1861. case IS_LONG:
  1862. add_assoc_long(return_value, var->aliasname, Z_LVAL_P(tmp));
  1863. break;
  1864. case IS_DOUBLE:
  1865. add_assoc_double(return_value, var->aliasname, Z_DVAL_P(tmp));
  1866. break;
  1867. }
  1868. }
  1869. efree(tmp);
  1870. } else {
  1871. if (fetch_type & FETCH_ROW) {
  1872. add_index_null(return_value, i);
  1873. } else {
  1874. add_assoc_null(return_value, var->aliasname);
  1875. }
  1876. }
  1877. if ((var->sqltype & ~1) == SQL_ARRAY) {
  1878. arr_cnt++;
  1879. }
  1880. } /*for field*/
  1881. }
  1882. /* }}} */
  1883. /* {{{ proto array ibase_fetch_row(int result [, int blob_flag])
  1884. Fetch a row from the results of a query */
  1885. PHP_FUNCTION(ibase_fetch_row)
  1886. {
  1887. _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, FETCH_ROW);
  1888. }
  1889. /* }}} */
  1890. /* {{{ proto array ibase_fetch_assoc(int result [, int blob_flag])
  1891. Fetch a row from the results of a query */
  1892. PHP_FUNCTION(ibase_fetch_assoc)
  1893. {
  1894. _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, FETCH_ARRAY);
  1895. }
  1896. /* }}} */
  1897. /* {{{ proto object ibase_fetch_object(int result [, int blob_flag])
  1898. Fetch a object from the results of a query */
  1899. PHP_FUNCTION(ibase_fetch_object)
  1900. {
  1901. _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, FETCH_ARRAY);
  1902. if (Z_TYPE_P(return_value) == IS_ARRAY) {
  1903. object_and_properties_init(return_value, &zend_standard_class_def, Z_ARRVAL_P(return_value));
  1904. }
  1905. }
  1906. /* }}} */
  1907. /* {{{ proto int ibase_free_result(int result)
  1908. Free the memory used by a result */
  1909. PHP_FUNCTION(ibase_free_result)
  1910. {
  1911. zval **result_arg;
  1912. ibase_result *ib_result;
  1913. RESET_ERRMSG;
  1914. if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result_arg)==FAILURE) {
  1915. WRONG_PARAM_COUNT;
  1916. }
  1917. ZEND_FETCH_RESOURCE(ib_result, ibase_result *, result_arg, -1, "InterBase result", le_result);
  1918. zend_list_delete(Z_LVAL_PP(result_arg));
  1919. RETURN_TRUE;
  1920. }
  1921. /* }}} */
  1922. /* {{{ proto int ibase_prepare([int link_identifier, ] string query)
  1923. Prepare a query for later execution */
  1924. PHP_FUNCTION(ibase_prepare)
  1925. {
  1926. zval **link_arg, **query_arg;
  1927. int link_id, trans_n = 0, trans_id = 0;
  1928. ibase_db_link *ib_link;
  1929. ibase_query *ib_query;
  1930. char *query;
  1931. RESET_ERRMSG;
  1932. switch (ZEND_NUM_ARGS()) {
  1933. case 1:
  1934. if (zend_get_parameters_ex(1, &query_arg) == FAILURE) {
  1935. RETURN_FALSE;
  1936. }
  1937. link_id = IBG(default_link);
  1938. ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, NULL, link_id, "InterBase link", le_link, le_plink);
  1939. break;
  1940. case 2:
  1941. if (zend_get_parameters_ex(2, &link_arg, &query_arg) == FAILURE) {
  1942. RETURN_FALSE;
  1943. }
  1944. get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, link_arg, &ib_link, &trans_n, &trans_id);
  1945. break;
  1946. default:
  1947. WRONG_PARAM_COUNT;
  1948. break;
  1949. }
  1950. convert_to_string_ex(query_arg);
  1951. query = Z_STRVAL_PP(query_arg);
  1952. /* open default transaction */
  1953. if (_php_ibase_def_trans(ib_link, trans_n) == FAILURE) {
  1954. RETURN_FALSE;
  1955. }
  1956. if (_php_ibase_alloc_query(&ib_query, ib_link->link, ib_link->trans[trans_n], query, ib_link->dialect) == FAILURE) {
  1957. RETURN_FALSE;
  1958. }
  1959. ib_query->cursor_open = 0;
  1960. zend_list_addref(link_id);
  1961. ZEND_REGISTER_RESOURCE(return_value, ib_query, le_query);
  1962. }
  1963. /* }}} */
  1964. /* {{{ proto int ibase_execute(int query [, int bind_args [, int ...]])
  1965. Execute a previously prepared query */
  1966. PHP_FUNCTION(ibase_execute)
  1967. {
  1968. pval ***args, **bind_args = NULL;
  1969. ibase_query *ib_query;
  1970. ibase_result *ib_result;
  1971. RESET_ERRMSG;
  1972. if (ZEND_NUM_ARGS() < 1) {
  1973. WRONG_PARAM_COUNT;
  1974. }
  1975. args = (pval ***)emalloc(ZEND_NUM_ARGS() * sizeof(pval **));
  1976. if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
  1977. efree(args);
  1978. RETURN_FALSE;
  1979. }
  1980. ZEND_FETCH_RESOURCE(ib_query, ibase_query *, args[0], -1, "InterBase query", le_query);
  1981. if (ZEND_NUM_ARGS() > 1) { /* have variables to bind */
  1982. bind_args = args[1];
  1983. }
  1984. /* Have we used this cursor before and it's still open? */
  1985. if (ib_query->cursor_open) {
  1986. IBDEBUG("Implicitly closing a cursor");
  1987. if (isc_dsql_free_statement(IB_STATUS, &ib_query->stmt, DSQL_close)){
  1988. efree(args);
  1989. _php_ibase_error(TSRMLS_C);
  1990. }
  1991. }
  1992. if ( _php_ibase_exec(&ib_result, ib_query, ZEND_NUM_ARGS()-1, bind_args) == FAILURE) {
  1993. efree(args);
  1994. RETURN_FALSE;
  1995. }
  1996. efree(args);
  1997. if (ib_result) { /* select statement */
  1998. ib_query->cursor_open = 1;
  1999. ZEND_REGISTER_RESOURCE(return_value, ib_result, le_result);
  2000. } else {
  2001. ib_query->cursor_open = 0;
  2002. RETURN_TRUE;
  2003. }
  2004. }
  2005. /* }}} */
  2006. /* {{{ proto int ibase_free_query(int query)
  2007. Free memory used by a query */
  2008. PHP_FUNCTION(ibase_free_query)
  2009. {
  2010. pval **query_arg;
  2011. ibase_query *ib_query;
  2012. RESET_ERRMSG;
  2013. if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &query_arg) == FAILURE) {
  2014. WRONG_PARAM_COUNT;
  2015. }
  2016. ZEND_FETCH_RESOURCE(ib_query, ibase_query *, query_arg, -1, "InterBase query", le_query);
  2017. zend_list_delete(Z_LVAL_PP(query_arg));
  2018. RETURN_TRUE;
  2019. }
  2020. /* }}} */
  2021. #if HAVE_STRFTIME
  2022. /* {{{ proto int ibase_timefmt(string format)
  2023. Sets the format of timestamp, date and time columns returned from queries */
  2024. PHP_FUNCTION(ibase_timefmt)
  2025. {
  2026. pval ***args;
  2027. char *fmt = NULL;
  2028. int type = PHP_IBASE_TIMESTAMP;
  2029. RESET_ERRMSG; /* ??? */
  2030. if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2){
  2031. WRONG_PARAM_COUNT;
  2032. }
  2033. args = (pval ***) emalloc(sizeof(pval **)*ZEND_NUM_ARGS());
  2034. if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
  2035. efree(args);
  2036. RETURN_FALSE;
  2037. }
  2038. switch (ZEND_NUM_ARGS()) {
  2039. case 2:
  2040. convert_to_long_ex(args[1]);
  2041. type = Z_LVAL_PP(args[1]);
  2042. case 1:
  2043. convert_to_string_ex(args[0]);
  2044. fmt = Z_STRVAL_PP(args[0]);
  2045. }
  2046. switch (type) {
  2047. case PHP_IBASE_TIMESTAMP:
  2048. if (IBG(timestampformat))
  2049. DL_FREE(IBG(timestampformat));
  2050. IBG(timestampformat) = DL_STRDUP(fmt);
  2051. break;
  2052. case PHP_IBASE_DATE:
  2053. if (IBG(dateformat))
  2054. DL_FREE(IBG(dateformat));
  2055. IBG(dateformat) = DL_STRDUP(fmt);
  2056. break;
  2057. case PHP_IBASE_TIME:
  2058. if (IBG(timeformat))
  2059. DL_FREE(IBG(timeformat));
  2060. IBG(timeformat) = DL_STRDUP(fmt);
  2061. break;
  2062. }
  2063. efree(args);
  2064. RETURN_TRUE;
  2065. }
  2066. /* }}} */
  2067. #endif
  2068. /* {{{ proto int ibase_num_fields(int result)
  2069. Get the number of fields in result */
  2070. PHP_FUNCTION(ibase_num_fields)
  2071. {
  2072. pval **result;
  2073. ibase_result *ib_result;
  2074. RESET_ERRMSG;
  2075. if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &result)==FAILURE) {
  2076. WRONG_PARAM_COUNT;
  2077. }
  2078. ZEND_FETCH_RESOURCE(ib_result, ibase_result *, result, -1, "InterBase result", le_result);
  2079. if (ib_result->out_sqlda == NULL) {
  2080. _php_ibase_module_error("Trying to get the number of fields from a non-select query");
  2081. RETURN_FALSE;
  2082. }
  2083. RETURN_LONG(ib_result->out_sqlda->sqld);
  2084. }
  2085. /* }}} */
  2086. /* {{{ proto array ibase_field_info(int result, int field_number)
  2087. Get information about a field */
  2088. PHP_FUNCTION(ibase_field_info)
  2089. {
  2090. pval *ret_val;
  2091. zval **result_arg, **field_arg;
  2092. ibase_result *ib_result;
  2093. char buf[30], *s;
  2094. int len;
  2095. XSQLVAR *var;
  2096. RESET_ERRMSG;
  2097. if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &result_arg, &field_arg)==FAILURE) {
  2098. WRONG_PARAM_COUNT;
  2099. }
  2100. ZEND_FETCH_RESOURCE(ib_result, ibase_result *, result_arg, -1, "InterBase result", le_result);
  2101. if (ib_result->out_sqlda == NULL) {
  2102. _php_ibase_module_error("Trying to get field info from a non-select query");
  2103. RETURN_FALSE;
  2104. }
  2105. convert_to_long_ex(field_arg);
  2106. if (Z_LVAL_PP(field_arg)<0 || Z_LVAL_PP(field_arg)>=ib_result->out_sqlda->sqld)
  2107. RETURN_FALSE;
  2108. if (array_init(return_value)==FAILURE)
  2109. RETURN_FALSE;
  2110. var = ib_result->out_sqlda->sqlvar + Z_LVAL_PP(field_arg);
  2111. add_get_index_stringl(return_value, 0, var->sqlname, var->sqlname_length, (void **) &ret_val, 1);
  2112. add_assoc_stringl(return_value, "name", var->sqlname, var->sqlname_length, 1);
  2113. add_get_index_stringl(return_value, 1, var->aliasname, var->aliasname_length, (void **) &ret_val, 1);
  2114. add_assoc_stringl(return_value, "alias", var->aliasname, var->aliasname_length, 1);
  2115. add_get_index_stringl(return_value, 2, var->relname, var->relname_length, (void **) &ret_val, 1);
  2116. add_assoc_stringl(return_value, "relation", var->relname, var->relname_length, 1);
  2117. len = sprintf(buf, "%d", var->sqllen);
  2118. add_get_index_stringl(return_value, 3, buf, len, (void **) &ret_val, 1);
  2119. add_assoc_stringl(return_value, "length", buf, len, 1);
  2120. switch (var->sqltype & ~1) {
  2121. case SQL_TEXT: s = "TEXT"; break;
  2122. case SQL_VARYING: s = "VARYING"; break;
  2123. case SQL_SHORT: s = "SHORT"; break;
  2124. case SQL_LONG: s = "LONG"; break;
  2125. case SQL_FLOAT: s = "FLOAT"; break;
  2126. case SQL_DOUBLE: s = "DOUBLE"; break;
  2127. case SQL_D_FLOAT: s = "D_FLOAT"; break;
  2128. #ifdef SQL_INT64
  2129. case SQL_INT64: s = "INT64"; break;
  2130. #endif
  2131. #ifdef SQL_TIMESTAMP
  2132. case SQL_TIMESTAMP: s = "TIMESTAMP"; break;
  2133. case SQL_TYPE_DATE: s = "DATE"; break;
  2134. case SQL_TYPE_TIME: s = "TIME"; break;
  2135. #else
  2136. case SQL_DATE: s = "DATE"; break;
  2137. #endif
  2138. case SQL_BLOB: s = "BLOB"; break;
  2139. case SQL_ARRAY: s = "ARRAY"; break;
  2140. case SQL_QUAD: s = "QUAD"; break;
  2141. default:
  2142. sprintf(buf, "unknown (%d)", var->sqltype & ~1);
  2143. s = buf;
  2144. break;
  2145. }
  2146. add_get_index_stringl(return_value, 4, s, strlen(s), (void **) &ret_val, 1);
  2147. add_assoc_stringl(return_value, "type", s, strlen(s), 1);
  2148. }
  2149. /* }}} */
  2150. /* blobs ----------------------------------- */
  2151. /* {{{ _php_ibase_blob_info(isc_blob_handle bl_handle, IBASE_BLOBINFO *bl_info) */
  2152. static int _php_ibase_blob_info(isc_blob_handle bl_handle, IBASE_BLOBINFO *bl_info)
  2153. {
  2154. static char bl_items[] = {
  2155. isc_info_blob_num_segments,
  2156. isc_info_blob_max_segment,
  2157. isc_info_blob_total_length,
  2158. isc_info_blob_type
  2159. };
  2160. char bl_inf[sizeof(long)*8], *p;
  2161. TSRMLS_FETCH();
  2162. bl_info->max_segment = 0;
  2163. bl_info->num_segments = 0;
  2164. bl_info->total_length = 0;
  2165. bl_info->bl_stream = 0;
  2166. if (isc_blob_info(IB_STATUS, &bl_handle, sizeof(bl_items), bl_items, sizeof(bl_inf), bl_inf)) {
  2167. _php_ibase_error(TSRMLS_C);
  2168. return FAILURE;
  2169. }
  2170. for (p = bl_inf; *p != isc_info_end && p < bl_inf+sizeof(bl_inf);) {
  2171. unsigned short item_len;
  2172. int item = *p++;
  2173. item_len = (short)isc_vax_integer(p, 2);
  2174. p += 2;
  2175. switch (item) {
  2176. case isc_info_blob_num_segments:
  2177. bl_info->num_segments = isc_vax_integer(p, item_len);
  2178. break;
  2179. case isc_info_blob_max_segment:
  2180. bl_info->max_segment = isc_vax_integer(p, item_len);
  2181. break;
  2182. case isc_info_blob_total_length:
  2183. bl_info->total_length = isc_vax_integer(p, item_len);
  2184. break;
  2185. case isc_info_blob_type:
  2186. bl_info->bl_stream = isc_vax_integer(p, item_len);
  2187. break;
  2188. case isc_info_end:
  2189. break;
  2190. case isc_info_truncated:
  2191. case isc_info_error: /* hmm. don't think so...*/
  2192. _php_ibase_module_error("PHP module internal error");
  2193. return FAILURE;
  2194. } /*switch*/
  2195. p += item_len;
  2196. } /*for*/
  2197. return SUCCESS;
  2198. }
  2199. /* }}} */
  2200. /* {{{ proto int ibase_blob_create([int link_identifier])
  2201. Create blob for adding data */
  2202. PHP_FUNCTION(ibase_blob_create)
  2203. {
  2204. zval **link_arg;
  2205. int trans_n = 0, trans_id = 0, link_id;
  2206. ibase_db_link *ib_link;
  2207. ibase_blob_handle *ib_blob;
  2208. RESET_ERRMSG;
  2209. switch (ZEND_NUM_ARGS()) {
  2210. case 0:
  2211. link_id = IBG(default_link);
  2212. ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, NULL, link_id, "InterBase link", le_link, le_plink);
  2213. break;
  2214. case 1:
  2215. if (zend_get_parameters_ex(1, &link_arg) == FAILURE) {
  2216. RETURN_FALSE;
  2217. }
  2218. get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, link_arg, &ib_link, &trans_n, &trans_id);
  2219. break;
  2220. default:
  2221. WRONG_PARAM_COUNT;
  2222. break;
  2223. }
  2224. /* open default transaction */
  2225. if(_php_ibase_def_trans(ib_link, trans_n) == FAILURE){
  2226. RETURN_FALSE;
  2227. }
  2228. ib_blob = (ibase_blob_handle *) emalloc(sizeof(ibase_blob_handle));
  2229. ib_blob->trans_handle = ib_link->trans[trans_n];
  2230. ib_blob->link = ib_link->link;
  2231. ib_blob->bl_handle = NULL;
  2232. if (isc_create_blob(IB_STATUS, &ib_blob->link, &ib_blob->trans_handle, &ib_blob->bl_handle, &ib_blob->bl_qd)) {
  2233. efree(ib_blob);
  2234. _php_ibase_error(TSRMLS_C);
  2235. RETURN_FALSE;
  2236. }
  2237. RETURN_LONG(zend_list_insert(ib_blob, le_blob));
  2238. }
  2239. /* }}} */
  2240. /* {{{ proto int ibase_blob_open(string blob_id)
  2241. Open blob for retriving data parts */
  2242. PHP_FUNCTION(ibase_blob_open)
  2243. {
  2244. pval *blob_arg;
  2245. ibase_blob_handle *ib_blob, *ib_blob_id;
  2246. RESET_ERRMSG;
  2247. if (ZEND_NUM_ARGS()!=1 || getParameters(ht, 1, &blob_arg)==FAILURE) {
  2248. WRONG_PARAM_COUNT;
  2249. }
  2250. ib_blob = (ibase_blob_handle *) emalloc(sizeof(ibase_blob_handle));
  2251. GET_BLOB_ID_ARG(blob_arg, ib_blob_id);
  2252. if (ib_blob_id == NULL) { /* blob IS NULL or argument unset */
  2253. RETURN_FALSE;
  2254. }
  2255. ib_blob->link = ib_blob_id->link;
  2256. ib_blob->trans_handle = ib_blob_id->trans_handle;
  2257. ib_blob->bl_qd.gds_quad_high = ib_blob_id->bl_qd.gds_quad_high;
  2258. ib_blob->bl_qd.gds_quad_low = ib_blob_id->bl_qd.gds_quad_low;
  2259. ib_blob->bl_handle = NULL;
  2260. if (isc_open_blob(IB_STATUS, &ib_blob->link, &ib_blob->trans_handle, &ib_blob->bl_handle, &ib_blob->bl_qd)) {
  2261. efree(ib_blob);
  2262. _php_ibase_error(TSRMLS_C);
  2263. RETURN_FALSE;
  2264. }
  2265. RETURN_LONG(zend_list_insert(ib_blob, le_blob));
  2266. }
  2267. /* }}} */
  2268. /* {{{ proto int ibase_blob_add(int blob_id, string data)
  2269. Add data into created blob */
  2270. PHP_FUNCTION(ibase_blob_add)
  2271. {
  2272. pval *blob_arg, *string_arg;
  2273. ibase_blob_handle *ib_blob;
  2274. RESET_ERRMSG;
  2275. if (ZEND_NUM_ARGS()!=2 || getParameters(ht, 2, &blob_arg, &string_arg)==FAILURE) {
  2276. WRONG_PARAM_COUNT;
  2277. }
  2278. GET_BLOB_HANDLE_ARG(blob_arg, ib_blob);
  2279. convert_to_string(string_arg);
  2280. if (isc_put_segment(IB_STATUS, &ib_blob->bl_handle, (unsigned short) Z_STRLEN_P(string_arg), Z_STRVAL_P(string_arg))) {
  2281. _php_ibase_error(TSRMLS_C);
  2282. RETURN_FALSE;
  2283. }
  2284. RETURN_TRUE;
  2285. }
  2286. /* }}} */
  2287. /* {{{ proto string ibase_blob_get(int blob_id, int len)
  2288. Get len bytes data from open blob */
  2289. PHP_FUNCTION(ibase_blob_get)
  2290. {
  2291. pval *blob_arg, *len_arg;
  2292. int stat;
  2293. char *bl_data;
  2294. unsigned short max_len = 0, cur_len, seg_len;
  2295. ibase_blob_handle *ib_blob;
  2296. RESET_ERRMSG;
  2297. if (ZEND_NUM_ARGS() != 2 || getParameters(ht, 2, &blob_arg, &len_arg) == FAILURE) {
  2298. WRONG_PARAM_COUNT;
  2299. }
  2300. convert_to_long(len_arg);
  2301. max_len = (unsigned short) Z_LVAL_P(len_arg);
  2302. GET_BLOB_HANDLE_ARG(blob_arg, ib_blob);
  2303. if (ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low) { /*not null ?*/
  2304. bl_data = emalloc(max_len+1);
  2305. for (cur_len = stat = 0; stat == 0; ) {
  2306. stat = isc_get_segment(IB_STATUS, &ib_blob->bl_handle, &seg_len, (unsigned short) (max_len-cur_len), &bl_data[cur_len]);
  2307. cur_len += seg_len;
  2308. if (cur_len > max_len) { /* never!*/
  2309. efree(bl_data);
  2310. _php_ibase_module_error("PHP module internal error");
  2311. RETURN_FALSE;
  2312. }
  2313. }
  2314. bl_data[cur_len] = '\0';
  2315. if (IB_STATUS[0] && (IB_STATUS[1] != isc_segstr_eof && IB_STATUS[1] != isc_segment)) {
  2316. efree(bl_data);
  2317. _php_ibase_error(TSRMLS_C);
  2318. RETURN_FALSE;
  2319. }
  2320. RETURN_STRINGL(bl_data, cur_len, 0);
  2321. } else { /* null blob */
  2322. RETURN_STRING("", 1); /* empty string */
  2323. }
  2324. }
  2325. /* }}} */
  2326. #define BLOB_CLOSE 1
  2327. #define BLOB_CANCEL 2
  2328. /* {{{ _php_ibase_blob_end() */
  2329. /* Close or Cancel created or Close open blob */
  2330. static void _php_ibase_blob_end(INTERNAL_FUNCTION_PARAMETERS, int bl_end)
  2331. {
  2332. pval *blob_arg;
  2333. ibase_blob_handle *ib_blob;
  2334. RESET_ERRMSG;
  2335. if (ZEND_NUM_ARGS()!=1 || getParameters(ht, 1, &blob_arg)==FAILURE) {
  2336. WRONG_PARAM_COUNT;
  2337. }
  2338. GET_BLOB_HANDLE_ARG(blob_arg, ib_blob);
  2339. if (bl_end == BLOB_CLOSE) { /* return id here */
  2340. if (ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low) { /*not null ?*/
  2341. if (isc_close_blob(IB_STATUS, &ib_blob->bl_handle)) {
  2342. _php_ibase_error(TSRMLS_C);
  2343. RETURN_FALSE;
  2344. }
  2345. }
  2346. ib_blob->bl_handle = NULL;
  2347. RETVAL_STRINGL((char *)ib_blob, sizeof(ibase_blob_handle), 1);
  2348. zend_list_delete(Z_LVAL_P(blob_arg));
  2349. } else { /* discard created blob */
  2350. if (isc_cancel_blob(IB_STATUS, &ib_blob->bl_handle)) {
  2351. _php_ibase_error(TSRMLS_C);
  2352. RETURN_FALSE;
  2353. }
  2354. ib_blob->bl_handle = NULL;
  2355. zend_list_delete(Z_LVAL_P(blob_arg));
  2356. RETURN_TRUE;
  2357. }
  2358. }
  2359. /* }}} */
  2360. /* {{{ proto int ibase_blob_close(int blob_id)
  2361. Close blob */
  2362. PHP_FUNCTION(ibase_blob_close)
  2363. {
  2364. _php_ibase_blob_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, BLOB_CLOSE);
  2365. }
  2366. /* }}} */
  2367. /* {{{ proto int ibase_blob_cancel(int blob_id)
  2368. Cancel creating blob */
  2369. PHP_FUNCTION(ibase_blob_cancel)
  2370. {
  2371. _php_ibase_blob_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, BLOB_CANCEL);
  2372. }
  2373. /* }}} */
  2374. /* {{{ proto object ibase_blob_info(string blob_id_str)
  2375. Return blob length and other useful info */
  2376. PHP_FUNCTION(ibase_blob_info)
  2377. {
  2378. pval *blob_arg, *result_var;
  2379. ibase_blob_handle *ib_blob_id;
  2380. IBASE_BLOBINFO bl_info;
  2381. RESET_ERRMSG;
  2382. if (ZEND_NUM_ARGS()!=1 || getParameters(ht, 1, &blob_arg)==FAILURE) {
  2383. WRONG_PARAM_COUNT;
  2384. }
  2385. GET_BLOB_ID_ARG(blob_arg, ib_blob_id);
  2386. if (array_init(return_value)==FAILURE){
  2387. RETURN_FALSE;
  2388. }
  2389. if (ib_blob_id->bl_qd.gds_quad_high || ib_blob_id->bl_qd.gds_quad_low) { /*not null ?*/
  2390. if (isc_open_blob(IB_STATUS, &ib_blob_id->link, &ib_blob_id->trans_handle,
  2391. &ib_blob_id->bl_handle, &ib_blob_id->bl_qd)) {
  2392. _php_ibase_error(TSRMLS_C);
  2393. RETURN_FALSE;
  2394. }
  2395. if (_php_ibase_blob_info(ib_blob_id->bl_handle, &bl_info)) {
  2396. RETURN_FALSE;
  2397. }
  2398. if (isc_close_blob(IB_STATUS, &ib_blob_id->bl_handle)) {
  2399. _php_ibase_error(TSRMLS_C);
  2400. RETURN_FALSE;
  2401. }
  2402. ib_blob_id->bl_handle = NULL;
  2403. } else { /* null blob, all values to zero */
  2404. bl_info.max_segment = 0;
  2405. bl_info.num_segments = 0;
  2406. bl_info.total_length = 0;
  2407. bl_info.bl_stream = 0;
  2408. }
  2409. /* FIXME */
  2410. add_get_index_long(return_value, 0, bl_info.total_length, (void **)&result_var);
  2411. /*
  2412. zend_hash_pointer_update(Z_ARRVAL_P(return_value), "length", sizeof("length"), result_var);
  2413. */
  2414. add_get_index_long(return_value, 1, bl_info.num_segments, (void **)&result_var);
  2415. /*
  2416. zend_hash_pointer_update(Z_ARRVAL_P(return_value), "numseg", sizeof("numseg"), result_var);
  2417. */
  2418. add_get_index_long(return_value, 2, bl_info.max_segment, (void **)&result_var);
  2419. /*
  2420. zend_hash_pointer_update(Z_ARRVAL_P(return_value), "maxseg", sizeof("maxseg"), result_var);
  2421. */
  2422. add_get_index_long(return_value, 3, bl_info.bl_stream, (void **)&result_var);
  2423. /*
  2424. zend_hash_pointer_update(Z_ARRVAL_P(return_value), "stream", sizeof("stream"), result_var);
  2425. */
  2426. add_get_index_long(return_value, 4,
  2427. (!ib_blob_id->bl_qd.gds_quad_high
  2428. && !ib_blob_id->bl_qd.gds_quad_low),
  2429. (void **)&result_var);
  2430. /*
  2431. zend_hash_pointer_update(Z_ARRVAL_P(return_value), "isnull", sizeof("isnull"), result_var);
  2432. */
  2433. }
  2434. /* }}} */
  2435. /* {{{ proto int ibase_blob_echo(string blob_id_str)
  2436. Output blob contents to browser */
  2437. PHP_FUNCTION(ibase_blob_echo)
  2438. {
  2439. pval *blob_arg;
  2440. char bl_data[IBASE_BLOB_SEG];
  2441. unsigned short seg_len;
  2442. ibase_blob_handle *ib_blob_id;
  2443. RESET_ERRMSG;
  2444. if (ZEND_NUM_ARGS()!=1 || getParameters(ht, 1, &blob_arg)==FAILURE) {
  2445. WRONG_PARAM_COUNT;
  2446. }
  2447. GET_BLOB_ID_ARG(blob_arg, ib_blob_id);
  2448. if (ib_blob_id) { /*not null ?*/
  2449. if (isc_open_blob(IB_STATUS, &ib_blob_id->link, &ib_blob_id->trans_handle,
  2450. &ib_blob_id->bl_handle, &ib_blob_id->bl_qd)) {
  2451. _php_ibase_error(TSRMLS_C);
  2452. RETURN_FALSE;
  2453. }
  2454. while (!isc_get_segment(IB_STATUS, &ib_blob_id->bl_handle, &seg_len, sizeof(bl_data), bl_data)
  2455. || IB_STATUS[1] == isc_segment) {
  2456. PHPWRITE(bl_data, seg_len);
  2457. }
  2458. if (IB_STATUS[0] && (IB_STATUS[1] != isc_segstr_eof)) {
  2459. _php_ibase_error(TSRMLS_C);
  2460. RETURN_FALSE;
  2461. }
  2462. if (isc_close_blob(IB_STATUS, &ib_blob_id->bl_handle)) {
  2463. _php_ibase_error(TSRMLS_C);
  2464. RETURN_FALSE;
  2465. }
  2466. ib_blob_id->bl_handle = NULL;
  2467. } /* not null ? */
  2468. RETURN_TRUE;
  2469. }
  2470. /* }}} */
  2471. /*
  2472. extern int le_fp, le_pp;
  2473. extern int wsa_fp;
  2474. */
  2475. /*to handle reading and writing to windows sockets*/
  2476. /* {{{ proto string ibase_blob_import([link_identifier, ] int file_id)
  2477. Create blob, copy file in it, and close it */
  2478. PHP_FUNCTION(ibase_blob_import)
  2479. {
  2480. zval **link_arg, **file_arg;
  2481. int trans_n = 0, link_id = 0, trans_id = 0, size;
  2482. unsigned short b;
  2483. ibase_blob_handle ib_blob;
  2484. ibase_db_link *ib_link;
  2485. char bl_data[IBASE_BLOB_SEG]; /* FIXME? blob_seg_size parameter? */
  2486. int type;
  2487. php_stream * stream;
  2488. RESET_ERRMSG;
  2489. switch (ZEND_NUM_ARGS()) {
  2490. case 1:
  2491. if (zend_get_parameters_ex(1, &file_arg) == FAILURE) {
  2492. RETURN_FALSE;
  2493. }
  2494. link_id = IBG(default_link);
  2495. ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, NULL, link_id, "InterBase link", le_link, le_plink);
  2496. break;
  2497. case 2:
  2498. if (zend_get_parameters_ex(2, &link_arg, &file_arg) == FAILURE) {
  2499. RETURN_FALSE;
  2500. }
  2501. get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, link_arg, &ib_link, &trans_n, &trans_id);
  2502. break;
  2503. default:
  2504. WRONG_PARAM_COUNT;
  2505. break;
  2506. }
  2507. /* open default transaction */
  2508. if (_php_ibase_def_trans(ib_link, trans_n) == FAILURE) {
  2509. RETURN_FALSE;
  2510. }
  2511. stream = (php_stream*)zend_fetch_resource(file_arg TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream());
  2512. ZEND_VERIFY_RESOURCE(stream);
  2513. ib_blob.link = ib_link->link;
  2514. ib_blob.trans_handle = ib_link->trans[trans_n];
  2515. ib_blob.bl_handle = NULL;
  2516. ib_blob.bl_qd.gds_quad_high = 0;
  2517. ib_blob.bl_qd.gds_quad_low = 0;
  2518. if (isc_create_blob(IB_STATUS, &ib_blob.link, &ib_blob.trans_handle, &ib_blob.bl_handle, &ib_blob.bl_qd)) {
  2519. _php_ibase_error(TSRMLS_C);
  2520. RETURN_FALSE;
  2521. }
  2522. size = 0;
  2523. while(b = php_stream_read(stream, bl_data, sizeof(bl_data)) > 0) {
  2524. if (isc_put_segment(IB_STATUS, &ib_blob.bl_handle, b, bl_data)) {
  2525. _php_ibase_error(TSRMLS_C);
  2526. RETURN_FALSE;
  2527. }
  2528. size += b;
  2529. }
  2530. if (isc_close_blob(IB_STATUS, &ib_blob.bl_handle)) {
  2531. _php_ibase_error(TSRMLS_C);
  2532. RETURN_FALSE;
  2533. }
  2534. ib_blob.bl_handle = NULL;
  2535. RETVAL_STRINGL((char *)&ib_blob, sizeof(ibase_blob_handle), 1);
  2536. }
  2537. /* }}} */
  2538. #ifdef SQL_DIALECT_V6
  2539. /* {{{ _php_ibase_user() */
  2540. static void _php_ibase_user(INTERNAL_FUNCTION_PARAMETERS, int operation)
  2541. {
  2542. pval **args[8];
  2543. char *ib_server, *dba_user_name, *dba_password, *user_name, *user_password = NULL,
  2544. *first_name = NULL, *middle_name = NULL, *last_name = NULL;
  2545. char service_name_buffer[128], *service_name = service_name_buffer;
  2546. char spb_buffer[128], *spb = spb_buffer;
  2547. unsigned short spb_length;
  2548. isc_svc_handle service_handle = NULL;
  2549. RESET_ERRMSG;
  2550. switch (operation) {
  2551. case isc_action_svc_add_user:
  2552. case isc_action_svc_modify_user:
  2553. // 5 to 8 parameters for ADD or MODIFY operation
  2554. if(ZEND_NUM_ARGS() < 5 || ZEND_NUM_ARGS() > 8) {
  2555. WRONG_PARAM_COUNT;
  2556. }
  2557. break;
  2558. case isc_action_svc_delete_user:
  2559. // 4 parameters for DELETE operation
  2560. if (ZEND_NUM_ARGS() != 4) {
  2561. WRONG_PARAM_COUNT;
  2562. }
  2563. }
  2564. if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
  2565. RETURN_FALSE;
  2566. }
  2567. switch(ZEND_NUM_ARGS()) {
  2568. case 8:
  2569. convert_to_string_ex(args[7]);
  2570. last_name = (*args[7])->value.str.val;
  2571. /* fallout */
  2572. case 7:
  2573. convert_to_string_ex(args[6]);
  2574. middle_name = (*args[6])->value.str.val;
  2575. /* fallout */
  2576. case 6:
  2577. convert_to_string_ex(args[5]);
  2578. first_name = (*args[5])->value.str.val;
  2579. /* fallout */
  2580. }
  2581. if (operation != isc_action_svc_delete_user) {
  2582. // Parameter not available for DELETE operation
  2583. convert_to_string_ex(args[4]);
  2584. user_password = (*args[4])->value.str.val;
  2585. }
  2586. convert_to_string_ex(args[3]);
  2587. user_name = (*args[3])->value.str.val;
  2588. convert_to_string_ex(args[2]);
  2589. dba_password = (*args[2])->value.str.val;
  2590. convert_to_string_ex(args[1]);
  2591. dba_user_name = (*args[1])->value.str.val;
  2592. convert_to_string_ex(args[0]);
  2593. ib_server = (*args[0])->value.str.val;
  2594. /*
  2595. zend_printf("server : %s<br>", ib_server);
  2596. zend_printf("admin : %s<br>", dba_user_name);
  2597. zend_printf("admin pwd: %s<br>", dba_password);
  2598. zend_printf("user : %s<br>", user_name);
  2599. zend_printf("user pwd : %s<br>", user_password);
  2600. zend_printf("fname : %s<br>", first_name);
  2601. zend_printf("mname : %s<br>", middle_name);
  2602. zend_printf("lname : %s<br>", last_name);
  2603. */
  2604. // Build buffer for isc_service_attach()
  2605. *spb++ = isc_spb_version;
  2606. *spb++ = isc_spb_current_version;
  2607. *spb++ = isc_spb_user_name;
  2608. *spb++ = strlen(dba_user_name);
  2609. strcpy(spb, dba_user_name);
  2610. spb += strlen(dba_user_name);
  2611. *spb++ = isc_spb_password;
  2612. *spb++ = strlen(dba_password);
  2613. strcpy(spb, dba_password);
  2614. spb += strlen(dba_password);
  2615. spb_length = spb - spb_buffer;
  2616. // Attach to the Service Manager
  2617. sprintf(service_name, "%s:service_mgr", ib_server);
  2618. if (isc_service_attach(IB_STATUS, 0, service_name,
  2619. &service_handle, spb_length, spb_buffer)) {
  2620. _php_ibase_error(TSRMLS_C);
  2621. RETURN_FALSE;
  2622. }
  2623. else {
  2624. char request[128], *x, *p = request;
  2625. // Identify cluster (here, isc_action_svc_*_user)
  2626. *p++ = operation;
  2627. // Argument for username
  2628. *p++ = isc_spb_sec_username;
  2629. ADD_SPB_LENGTH(p, strlen(user_name));
  2630. for (x = user_name ; *x; ) *p++ = *x++;
  2631. // Argument for password
  2632. if (user_password) {
  2633. *p++ = isc_spb_sec_password;
  2634. ADD_SPB_LENGTH(p, strlen(user_password));
  2635. for (x = user_password ; *x; ) *p++ = *x++;
  2636. }
  2637. // Argument for first name
  2638. if (first_name) {
  2639. *p++ = isc_spb_sec_firstname;
  2640. ADD_SPB_LENGTH(p, strlen(first_name));
  2641. for (x = first_name ; *x; ) *p++ = *x++;
  2642. }
  2643. // Argument for middle name
  2644. if (middle_name) {
  2645. *p++ = isc_spb_sec_middlename;
  2646. ADD_SPB_LENGTH(p, strlen(middle_name));
  2647. for (x = middle_name ; *x; ) *p++ = *x++;
  2648. }
  2649. // Argument for last name
  2650. if (last_name) {
  2651. *p++ = isc_spb_sec_lastname;
  2652. ADD_SPB_LENGTH(p, strlen(last_name));
  2653. for (x = last_name ; *x; ) *p++ = *x++;
  2654. }
  2655. // Let's go update: start Service Manager
  2656. if (isc_service_start(IB_STATUS, &service_handle,
  2657. NULL, (unsigned short) (p - request), request)) {
  2658. _php_ibase_error(TSRMLS_C);
  2659. isc_service_detach(IB_STATUS, &service_handle);
  2660. RETURN_FALSE;
  2661. }
  2662. else {
  2663. // Detach from Service Manager
  2664. isc_service_detach(IB_STATUS, &service_handle);
  2665. }
  2666. }
  2667. RETURN_TRUE;
  2668. }
  2669. /* }}} */
  2670. /* {{{ proto int ibase_add_user(string server, string dba_user_name, string dba_password, string user_name, string password [, string first_name] [, string middle_name] [, string last_name])
  2671. Add an user to security database (only for IB6 or later) */
  2672. PHP_FUNCTION(ibase_add_user)
  2673. {
  2674. _php_ibase_user(INTERNAL_FUNCTION_PARAM_PASSTHRU, isc_action_svc_add_user);
  2675. }
  2676. /* }}} */
  2677. /* {{{ proto int ibase_modify_user(string server, string dba_user_name, string dba_password, string user_name, string password [, string first_name] [, string middle_name] [, string last_name])
  2678. Modify an user in security database (only for IB6 or later) */
  2679. PHP_FUNCTION(ibase_modify_user)
  2680. {
  2681. _php_ibase_user(INTERNAL_FUNCTION_PARAM_PASSTHRU, isc_action_svc_modify_user);
  2682. }
  2683. /* }}} */
  2684. /* {{{ proto int ibase_delete_user(string server, string dba_user_name, string dba_password, string username)
  2685. Delete an user from security database (only for IB6 or later) */
  2686. PHP_FUNCTION(ibase_delete_user)
  2687. {
  2688. _php_ibase_user(INTERNAL_FUNCTION_PARAM_PASSTHRU, isc_action_svc_delete_user);
  2689. }
  2690. /* }}} */
  2691. #endif /* SQL_DIALECT_V6 */
  2692. #endif /* HAVE_IBASE */
  2693. /*
  2694. * Local variables:
  2695. * tab-width: 4
  2696. * c-basic-offset: 4
  2697. * End:
  2698. * vim600: sw=4 ts=4 fdm=marker
  2699. * vim<600: sw=4 ts=4
  2700. */