ibase_query.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.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: Ard Biesheuvel <a.k.biesheuvel@its.tudelft.nl> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_ini.h"
  23. #if HAVE_IBASE
  24. #include "ext/standard/php_standard.h"
  25. #include "php_interbase.h"
  26. #include "php_ibase_includes.h"
  27. #define ISC_LONG_MIN INT_MIN
  28. #define ISC_LONG_MAX INT_MAX
  29. #define QUERY_RESULT 1
  30. #define EXECUTE_RESULT 2
  31. #define FETCH_ROW 1
  32. #define FETCH_ARRAY 2
  33. typedef struct {
  34. ISC_ARRAY_DESC ar_desc;
  35. ISC_LONG ar_size; /* size of entire array in bytes */
  36. unsigned short el_type, el_size;
  37. } ibase_array;
  38. typedef struct {
  39. ibase_db_link *link;
  40. ibase_trans *trans;
  41. struct _ib_query *query;
  42. isc_stmt_handle stmt;
  43. unsigned short type;
  44. unsigned char has_more_rows, statement_type;
  45. XSQLDA *out_sqlda;
  46. ibase_array out_array[1]; /* last member */
  47. } ibase_result;
  48. typedef struct _ib_query {
  49. ibase_db_link *link;
  50. ibase_trans *trans;
  51. ibase_result *result;
  52. zend_resource *result_res;
  53. isc_stmt_handle stmt;
  54. XSQLDA *in_sqlda, *out_sqlda;
  55. ibase_array *in_array, *out_array;
  56. unsigned short in_array_cnt, out_array_cnt;
  57. unsigned short dialect;
  58. char statement_type;
  59. char *query;
  60. zend_resource *trans_res;
  61. } ibase_query;
  62. typedef struct {
  63. unsigned short vary_length;
  64. char vary_string[1];
  65. } IBVARY;
  66. /* sql variables union
  67. * used for convert and binding input variables
  68. */
  69. typedef struct {
  70. union {
  71. short sval;
  72. float fval;
  73. ISC_LONG lval;
  74. ISC_QUAD qval;
  75. ISC_TIMESTAMP tsval;
  76. ISC_DATE dtval;
  77. ISC_TIME tmval;
  78. } val;
  79. short sqlind;
  80. } BIND_BUF;
  81. static int le_result, le_query;
  82. #define LE_RESULT "Firebird/InterBase result"
  83. #define LE_QUERY "Firebird/InterBase query"
  84. static void _php_ibase_free_xsqlda(XSQLDA *sqlda) /* {{{ */
  85. {
  86. int i;
  87. XSQLVAR *var;
  88. IBDEBUG("Free XSQLDA?");
  89. if (sqlda) {
  90. IBDEBUG("Freeing XSQLDA...");
  91. var = sqlda->sqlvar;
  92. for (i = 0; i < sqlda->sqld; i++, var++) {
  93. efree(var->sqldata);
  94. if (var->sqlind) {
  95. efree(var->sqlind);
  96. }
  97. }
  98. efree(sqlda);
  99. }
  100. }
  101. /* }}} */
  102. static void _php_ibase_free_stmt_handle(ibase_db_link *link, isc_stmt_handle stmt) /* {{{ */
  103. {
  104. static char info[] = { isc_info_base_level, isc_info_end };
  105. if (stmt) {
  106. char res_buf[8];
  107. IBDEBUG("Dropping statement handle (free_stmt_handle)...");
  108. /* Only free statement if db-connection is still open */
  109. if (SUCCESS == isc_database_info(IB_STATUS, &link->handle,
  110. sizeof(info), info, sizeof(res_buf), res_buf)) {
  111. if (isc_dsql_free_statement(IB_STATUS, &stmt, DSQL_drop)) {
  112. _php_ibase_error();
  113. }
  114. }
  115. }
  116. }
  117. /* }}} */
  118. static void _php_ibase_free_result(zend_resource *rsrc) /* {{{ */
  119. {
  120. ibase_result *ib_result = (ibase_result *) rsrc->ptr;
  121. IBDEBUG("Freeing result by dtor...");
  122. if (ib_result) {
  123. _php_ibase_free_xsqlda(ib_result->out_sqlda);
  124. if (ib_result->query != NULL) {
  125. IBDEBUG("query still valid; don't drop statement handle");
  126. ib_result->query->result = NULL; /* Indicate to query, that result is released */
  127. } else {
  128. _php_ibase_free_stmt_handle(ib_result->link, ib_result->stmt);
  129. }
  130. efree(ib_result);
  131. }
  132. }
  133. /* }}} */
  134. static void _php_ibase_free_query(ibase_query *ib_query) /* {{{ */
  135. {
  136. IBDEBUG("Freeing query...");
  137. if (ib_query->in_sqlda) {
  138. efree(ib_query->in_sqlda);
  139. }
  140. if (ib_query->out_sqlda) {
  141. efree(ib_query->out_sqlda);
  142. }
  143. if (ib_query->result != NULL) {
  144. IBDEBUG("result still valid; don't drop statement handle");
  145. ib_query->result->query = NULL; /* Indicate to result, that query is released */
  146. } else {
  147. _php_ibase_free_stmt_handle(ib_query->link, ib_query->stmt);
  148. }
  149. if (ib_query->in_array) {
  150. efree(ib_query->in_array);
  151. }
  152. if (ib_query->out_array) {
  153. efree(ib_query->out_array);
  154. }
  155. if (ib_query->query) {
  156. efree(ib_query->query);
  157. }
  158. }
  159. /* }}} */
  160. static void php_ibase_free_query_rsrc(zend_resource *rsrc) /* {{{ */
  161. {
  162. ibase_query *ib_query = (ibase_query *)rsrc->ptr;
  163. if (ib_query != NULL) {
  164. IBDEBUG("Preparing to free query by dtor...");
  165. _php_ibase_free_query(ib_query);
  166. efree(ib_query);
  167. }
  168. }
  169. /* }}} */
  170. void php_ibase_query_minit(INIT_FUNC_ARGS) /* {{{ */
  171. {
  172. le_result = zend_register_list_destructors_ex(_php_ibase_free_result, NULL,
  173. "interbase result", module_number);
  174. le_query = zend_register_list_destructors_ex(php_ibase_free_query_rsrc, NULL,
  175. "interbase query", module_number);
  176. }
  177. /* }}} */
  178. static int _php_ibase_alloc_array(ibase_array **ib_arrayp, XSQLDA *sqlda, /* {{{ */
  179. isc_db_handle link, isc_tr_handle trans, unsigned short *array_cnt)
  180. {
  181. unsigned short i, n;
  182. ibase_array *ar;
  183. /* first check if we have any arrays at all */
  184. for (i = *array_cnt = 0; i < sqlda->sqld; ++i) {
  185. if ((sqlda->sqlvar[i].sqltype & ~1) == SQL_ARRAY) {
  186. ++*array_cnt;
  187. }
  188. }
  189. if (! *array_cnt) return SUCCESS;
  190. ar = safe_emalloc(sizeof(ibase_array), *array_cnt, 0);
  191. for (i = n = 0; i < sqlda->sqld; ++i) {
  192. unsigned short dim;
  193. zend_ulong ar_size = 1;
  194. XSQLVAR *var = &sqlda->sqlvar[i];
  195. if ((var->sqltype & ~1) == SQL_ARRAY) {
  196. ibase_array *a = &ar[n++];
  197. ISC_ARRAY_DESC *ar_desc = &a->ar_desc;
  198. if (isc_array_lookup_bounds(IB_STATUS, &link, &trans, var->relname,
  199. var->sqlname, ar_desc)) {
  200. _php_ibase_error();
  201. efree(ar);
  202. return FAILURE;
  203. }
  204. switch (ar_desc->array_desc_dtype) {
  205. case blr_text:
  206. case blr_text2:
  207. a->el_type = SQL_TEXT;
  208. a->el_size = ar_desc->array_desc_length;
  209. break;
  210. case blr_short:
  211. a->el_type = SQL_SHORT;
  212. a->el_size = sizeof(short);
  213. break;
  214. case blr_long:
  215. a->el_type = SQL_LONG;
  216. a->el_size = sizeof(ISC_LONG);
  217. break;
  218. case blr_float:
  219. a->el_type = SQL_FLOAT;
  220. a->el_size = sizeof(float);
  221. break;
  222. case blr_double:
  223. a->el_type = SQL_DOUBLE;
  224. a->el_size = sizeof(double);
  225. break;
  226. case blr_int64:
  227. a->el_type = SQL_INT64;
  228. a->el_size = sizeof(ISC_INT64);
  229. break;
  230. case blr_timestamp:
  231. a->el_type = SQL_TIMESTAMP;
  232. a->el_size = sizeof(ISC_TIMESTAMP);
  233. break;
  234. case blr_sql_date:
  235. a->el_type = SQL_TYPE_DATE;
  236. a->el_size = sizeof(ISC_DATE);
  237. break;
  238. case blr_sql_time:
  239. a->el_type = SQL_TYPE_TIME;
  240. a->el_size = sizeof(ISC_TIME);
  241. break;
  242. case blr_varying:
  243. case blr_varying2:
  244. /**
  245. * IB has a strange way of handling VARCHAR arrays. It doesn't store
  246. * the length in the first short, as with VARCHAR fields. It does,
  247. * however, expect the extra short to be allocated for each element.
  248. */
  249. a->el_type = SQL_TEXT;
  250. a->el_size = ar_desc->array_desc_length + sizeof(short);
  251. break;
  252. case blr_quad:
  253. case blr_blob_id:
  254. case blr_cstring:
  255. case blr_cstring2:
  256. /**
  257. * These types are mentioned as array types in the manual, but I
  258. * wouldn't know how to create an array field with any of these
  259. * types. I assume these types are not applicable to arrays, and
  260. * were mentioned erroneously.
  261. */
  262. default:
  263. _php_ibase_module_error("Unsupported array type %d in relation '%s' column '%s'",
  264. ar_desc->array_desc_dtype, var->relname, var->sqlname);
  265. efree(ar);
  266. return FAILURE;
  267. } /* switch array_desc_type */
  268. /* calculate elements count */
  269. for (dim = 0; dim < ar_desc->array_desc_dimensions; dim++) {
  270. ar_size *= 1 + ar_desc->array_desc_bounds[dim].array_bound_upper
  271. -ar_desc->array_desc_bounds[dim].array_bound_lower;
  272. }
  273. a->ar_size = a->el_size * ar_size;
  274. } /* if SQL_ARRAY */
  275. } /* for column */
  276. *ib_arrayp = ar;
  277. return SUCCESS;
  278. }
  279. /* }}} */
  280. /* allocate and prepare query */
  281. static int _php_ibase_alloc_query(ibase_query *ib_query, ibase_db_link *link, /* {{{ */
  282. ibase_trans *trans, char *query, unsigned short dialect, zend_resource *trans_res)
  283. {
  284. static char info_type[] = {isc_info_sql_stmt_type};
  285. char result[8];
  286. /* Return FAILURE, if querystring is empty */
  287. if (*query == '\0') {
  288. php_error_docref(NULL, E_WARNING, "Querystring empty.");
  289. return FAILURE;
  290. }
  291. ib_query->link = link;
  292. ib_query->trans = trans;
  293. ib_query->result_res = NULL;
  294. ib_query->result = NULL;
  295. ib_query->stmt = 0;
  296. ib_query->in_array = NULL;
  297. ib_query->out_array = NULL;
  298. ib_query->dialect = dialect;
  299. ib_query->query = estrdup(query);
  300. ib_query->trans_res = trans_res;
  301. ib_query->out_sqlda = NULL;
  302. ib_query->in_sqlda = NULL;
  303. if (isc_dsql_allocate_statement(IB_STATUS, &link->handle, &ib_query->stmt)) {
  304. _php_ibase_error();
  305. goto _php_ibase_alloc_query_error;
  306. }
  307. ib_query->out_sqlda = (XSQLDA *) emalloc(XSQLDA_LENGTH(1));
  308. ib_query->out_sqlda->sqln = 1;
  309. ib_query->out_sqlda->version = SQLDA_CURRENT_VERSION;
  310. if (isc_dsql_prepare(IB_STATUS, &ib_query->trans->handle, &ib_query->stmt,
  311. 0, query, dialect, ib_query->out_sqlda)) {
  312. _php_ibase_error();
  313. goto _php_ibase_alloc_query_error;
  314. }
  315. /* find out what kind of statement was prepared */
  316. if (isc_dsql_sql_info(IB_STATUS, &ib_query->stmt, sizeof(info_type),
  317. info_type, sizeof(result), result)) {
  318. _php_ibase_error();
  319. goto _php_ibase_alloc_query_error;
  320. }
  321. ib_query->statement_type = result[3];
  322. /* not enough output variables ? */
  323. if (ib_query->out_sqlda->sqld > ib_query->out_sqlda->sqln) {
  324. ib_query->out_sqlda = erealloc(ib_query->out_sqlda, XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
  325. ib_query->out_sqlda->sqln = ib_query->out_sqlda->sqld;
  326. ib_query->out_sqlda->version = SQLDA_CURRENT_VERSION;
  327. if (isc_dsql_describe(IB_STATUS, &ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->out_sqlda)) {
  328. _php_ibase_error();
  329. goto _php_ibase_alloc_query_error;
  330. }
  331. }
  332. /* maybe have input placeholders? */
  333. ib_query->in_sqlda = emalloc(XSQLDA_LENGTH(1));
  334. ib_query->in_sqlda->sqln = 1;
  335. ib_query->in_sqlda->version = SQLDA_CURRENT_VERSION;
  336. if (isc_dsql_describe_bind(IB_STATUS, &ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->in_sqlda)) {
  337. _php_ibase_error();
  338. goto _php_ibase_alloc_query_error;
  339. }
  340. /* not enough input variables ? */
  341. if (ib_query->in_sqlda->sqln < ib_query->in_sqlda->sqld) {
  342. ib_query->in_sqlda = erealloc(ib_query->in_sqlda, XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
  343. ib_query->in_sqlda->sqln = ib_query->in_sqlda->sqld;
  344. ib_query->in_sqlda->version = SQLDA_CURRENT_VERSION;
  345. if (isc_dsql_describe_bind(IB_STATUS, &ib_query->stmt,
  346. SQLDA_CURRENT_VERSION, ib_query->in_sqlda)) {
  347. _php_ibase_error();
  348. goto _php_ibase_alloc_query_error;
  349. }
  350. }
  351. /* no, haven't placeholders at all */
  352. if (ib_query->in_sqlda->sqld == 0) {
  353. efree(ib_query->in_sqlda);
  354. ib_query->in_sqlda = NULL;
  355. } else if (FAILURE == _php_ibase_alloc_array(&ib_query->in_array, ib_query->in_sqlda,
  356. link->handle, trans->handle, &ib_query->in_array_cnt)) {
  357. goto _php_ibase_alloc_query_error;
  358. }
  359. if (ib_query->out_sqlda->sqld == 0) {
  360. efree(ib_query->out_sqlda);
  361. ib_query->out_sqlda = NULL;
  362. } else if (FAILURE == _php_ibase_alloc_array(&ib_query->out_array, ib_query->out_sqlda,
  363. link->handle, trans->handle, &ib_query->out_array_cnt)) {
  364. goto _php_ibase_alloc_query_error;
  365. }
  366. return SUCCESS;
  367. _php_ibase_alloc_query_error:
  368. if (ib_query->out_sqlda) {
  369. efree(ib_query->out_sqlda);
  370. }
  371. if (ib_query->in_sqlda) {
  372. efree(ib_query->in_sqlda);
  373. }
  374. if (ib_query->out_array) {
  375. efree(ib_query->out_array);
  376. }
  377. if (ib_query->query) {
  378. efree(ib_query->query);
  379. }
  380. return FAILURE;
  381. }
  382. /* }}} */
  383. static int _php_ibase_bind_array(zval *val, char *buf, zend_ulong buf_size, /* {{{ */
  384. ibase_array *array, int dim)
  385. {
  386. zval null_val, *pnull_val = &null_val;
  387. int u_bound = array->ar_desc.array_desc_bounds[dim].array_bound_upper,
  388. l_bound = array->ar_desc.array_desc_bounds[dim].array_bound_lower,
  389. dim_len = 1 + u_bound - l_bound;
  390. ZVAL_NULL(pnull_val);
  391. if (dim < array->ar_desc.array_desc_dimensions) {
  392. zend_ulong slice_size = buf_size / dim_len;
  393. unsigned short i;
  394. zval *subval = val;
  395. if (Z_TYPE_P(val) == IS_ARRAY) {
  396. zend_hash_internal_pointer_reset(Z_ARRVAL_P(val));
  397. }
  398. for (i = 0; i < dim_len; ++i) {
  399. if (Z_TYPE_P(val) == IS_ARRAY &&
  400. (subval = zend_hash_get_current_data(Z_ARRVAL_P(val))) == NULL)
  401. {
  402. subval = pnull_val;
  403. }
  404. if (_php_ibase_bind_array(subval, buf, slice_size, array, dim+1) == FAILURE)
  405. {
  406. return FAILURE;
  407. }
  408. buf += slice_size;
  409. if (Z_TYPE_P(val) == IS_ARRAY) {
  410. zend_hash_move_forward(Z_ARRVAL_P(val));
  411. }
  412. }
  413. if (Z_TYPE_P(val) == IS_ARRAY) {
  414. zend_hash_internal_pointer_reset(Z_ARRVAL_P(val));
  415. }
  416. } else {
  417. /* expect a single value */
  418. if (Z_TYPE_P(val) == IS_NULL) {
  419. memset(buf, 0, buf_size);
  420. } else if (array->ar_desc.array_desc_scale < 0) {
  421. /* no coercion for array types */
  422. double l;
  423. convert_to_double(val);
  424. if (Z_DVAL_P(val) > 0) {
  425. l = Z_DVAL_P(val) * pow(10, -array->ar_desc.array_desc_scale) + .5;
  426. } else {
  427. l = Z_DVAL_P(val) * pow(10, -array->ar_desc.array_desc_scale) - .5;
  428. }
  429. switch (array->el_type) {
  430. case SQL_SHORT:
  431. if (l > SHRT_MAX || l < SHRT_MIN) {
  432. _php_ibase_module_error("Array parameter exceeds field width");
  433. return FAILURE;
  434. }
  435. *(short*) buf = (short) l;
  436. break;
  437. case SQL_LONG:
  438. if (l > ISC_LONG_MAX || l < ISC_LONG_MIN) {
  439. _php_ibase_module_error("Array parameter exceeds field width");
  440. return FAILURE;
  441. }
  442. *(ISC_LONG*) buf = (ISC_LONG) l;
  443. break;
  444. case SQL_INT64:
  445. {
  446. long double l;
  447. convert_to_string(val);
  448. if (!sscanf(Z_STRVAL_P(val), "%Lf", &l)) {
  449. _php_ibase_module_error("Cannot convert '%s' to long double",
  450. Z_STRVAL_P(val));
  451. return FAILURE;
  452. }
  453. if (l > 0) {
  454. *(ISC_INT64 *) buf = (ISC_INT64) (l * pow(10,
  455. -array->ar_desc.array_desc_scale) + .5);
  456. } else {
  457. *(ISC_INT64 *) buf = (ISC_INT64) (l * pow(10,
  458. -array->ar_desc.array_desc_scale) - .5);
  459. }
  460. }
  461. break;
  462. }
  463. } else {
  464. struct tm t = { 0, 0, 0, 0, 0, 0 };
  465. switch (array->el_type) {
  466. #ifndef HAVE_STRPTIME
  467. unsigned short n;
  468. #endif
  469. #if (SIZEOF_ZEND_LONG < 8)
  470. ISC_INT64 l;
  471. #endif
  472. case SQL_SHORT:
  473. convert_to_long(val);
  474. if (Z_LVAL_P(val) > SHRT_MAX || Z_LVAL_P(val) < SHRT_MIN) {
  475. _php_ibase_module_error("Array parameter exceeds field width");
  476. return FAILURE;
  477. }
  478. *(short *) buf = (short) Z_LVAL_P(val);
  479. break;
  480. case SQL_LONG:
  481. convert_to_long(val);
  482. #if (SIZEOF_ZEND_LONG > 4)
  483. if (Z_LVAL_P(val) > ISC_LONG_MAX || Z_LVAL_P(val) < ISC_LONG_MIN) {
  484. _php_ibase_module_error("Array parameter exceeds field width");
  485. return FAILURE;
  486. }
  487. #endif
  488. *(ISC_LONG *) buf = (ISC_LONG) Z_LVAL_P(val);
  489. break;
  490. case SQL_INT64:
  491. #if (SIZEOF_ZEND_LONG >= 8)
  492. convert_to_long(val);
  493. *(zend_long *) buf = Z_LVAL_P(val);
  494. #else
  495. convert_to_string(val);
  496. if (!sscanf(Z_STRVAL_P(val), "%" LL_MASK "d", &l)) {
  497. _php_ibase_module_error("Cannot convert '%s' to long integer",
  498. Z_STRVAL_P(val));
  499. return FAILURE;
  500. } else {
  501. *(ISC_INT64 *) buf = l;
  502. }
  503. #endif
  504. break;
  505. case SQL_FLOAT:
  506. convert_to_double(val);
  507. *(float*) buf = (float) Z_DVAL_P(val);
  508. break;
  509. case SQL_DOUBLE:
  510. convert_to_double(val);
  511. *(double*) buf = Z_DVAL_P(val);
  512. break;
  513. case SQL_TIMESTAMP:
  514. convert_to_string(val);
  515. #ifdef HAVE_STRPTIME
  516. strptime(Z_STRVAL_P(val), INI_STR("ibase.timestampformat"), &t);
  517. #else
  518. n = sscanf(Z_STRVAL_P(val), "%d%*[/]%d%*[/]%d %d%*[:]%d%*[:]%d",
  519. &t.tm_mon, &t.tm_mday, &t.tm_year, &t.tm_hour, &t.tm_min, &t.tm_sec);
  520. if (n != 3 && n != 6) {
  521. _php_ibase_module_error("Invalid date/time format (expected 3 or 6 fields, got %d."
  522. " Use format 'm/d/Y H:i:s'. You gave '%s')", n, Z_STRVAL_P(val));
  523. return FAILURE;
  524. }
  525. t.tm_year -= 1900;
  526. t.tm_mon--;
  527. #endif
  528. isc_encode_timestamp(&t, (ISC_TIMESTAMP * ) buf);
  529. break;
  530. case SQL_TYPE_DATE:
  531. convert_to_string(val);
  532. #ifdef HAVE_STRPTIME
  533. strptime(Z_STRVAL_P(val), INI_STR("ibase.dateformat"), &t);
  534. #else
  535. n = sscanf(Z_STRVAL_P(val), "%d%*[/]%d%*[/]%d", &t.tm_mon, &t.tm_mday, &t.tm_year);
  536. if (n != 3) {
  537. _php_ibase_module_error("Invalid date format (expected 3 fields, got %d. "
  538. "Use format 'm/d/Y' You gave '%s')", n, Z_STRVAL_P(val));
  539. return FAILURE;
  540. }
  541. t.tm_year -= 1900;
  542. t.tm_mon--;
  543. #endif
  544. isc_encode_sql_date(&t, (ISC_DATE *) buf);
  545. break;
  546. case SQL_TYPE_TIME:
  547. convert_to_string(val);
  548. #ifdef HAVE_STRPTIME
  549. strptime(Z_STRVAL_P(val), INI_STR("ibase.timeformat"), &t);
  550. #else
  551. n = sscanf(Z_STRVAL_P(val), "%d%*[:]%d%*[:]%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
  552. if (n != 3) {
  553. _php_ibase_module_error("Invalid time format (expected 3 fields, got %d. "
  554. "Use format 'H:i:s'. You gave '%s')", n, Z_STRVAL_P(val));
  555. return FAILURE;
  556. }
  557. #endif
  558. isc_encode_sql_time(&t, (ISC_TIME *) buf);
  559. break;
  560. default:
  561. convert_to_string(val);
  562. strlcpy(buf, Z_STRVAL_P(val), buf_size);
  563. }
  564. }
  565. }
  566. return SUCCESS;
  567. }
  568. /* }}} */
  569. static int _php_ibase_bind(XSQLDA *sqlda, zval *b_vars, BIND_BUF *buf, /* {{{ */
  570. ibase_query *ib_query)
  571. {
  572. int i, array_cnt = 0, rv = SUCCESS;
  573. for (i = 0; i < sqlda->sqld; ++i) { /* bound vars */
  574. zval *b_var = &b_vars[i];
  575. XSQLVAR *var = &sqlda->sqlvar[i];
  576. var->sqlind = &buf[i].sqlind;
  577. /* check if a NULL should be inserted */
  578. switch (Z_TYPE_P(b_var)) {
  579. int force_null;
  580. case IS_STRING:
  581. force_null = 0;
  582. /* for these types, an empty string can be handled like a NULL value */
  583. switch (var->sqltype & ~1) {
  584. case SQL_SHORT:
  585. case SQL_LONG:
  586. case SQL_INT64:
  587. case SQL_FLOAT:
  588. case SQL_DOUBLE:
  589. case SQL_TIMESTAMP:
  590. case SQL_TYPE_DATE:
  591. case SQL_TYPE_TIME:
  592. force_null = (Z_STRLEN_P(b_var) == 0);
  593. }
  594. if (! force_null) break;
  595. case IS_NULL:
  596. buf[i].sqlind = -1;
  597. if (var->sqltype & SQL_ARRAY) ++array_cnt;
  598. continue;
  599. }
  600. /* if we make it to this point, we must provide a value for the parameter */
  601. buf[i].sqlind = 0;
  602. var->sqldata = (void*)&buf[i].val;
  603. switch (var->sqltype & ~1) {
  604. struct tm t;
  605. case SQL_TIMESTAMP:
  606. case SQL_TYPE_DATE:
  607. case SQL_TYPE_TIME:
  608. if (Z_TYPE_P(b_var) == IS_LONG) {
  609. struct tm *res;
  610. res = php_gmtime_r(&Z_LVAL_P(b_var), &t);
  611. if (!res) {
  612. return FAILURE;
  613. }
  614. } else {
  615. #ifdef HAVE_STRPTIME
  616. char *format = INI_STR("ibase.timestampformat");
  617. convert_to_string(b_var);
  618. switch (var->sqltype & ~1) {
  619. case SQL_TYPE_DATE:
  620. format = INI_STR("ibase.dateformat");
  621. break;
  622. case SQL_TYPE_TIME:
  623. format = INI_STR("ibase.timeformat");
  624. }
  625. if (!strptime(Z_STRVAL_P(b_var), format, &t)) {
  626. /* strptime() cannot handle it, so let IB have a try */
  627. break;
  628. }
  629. #else /* ifndef HAVE_STRPTIME */
  630. break; /* let IB parse it as a string */
  631. #endif
  632. }
  633. switch (var->sqltype & ~1) {
  634. default: /* == case SQL_TIMESTAMP */
  635. isc_encode_timestamp(&t, &buf[i].val.tsval);
  636. break;
  637. case SQL_TYPE_DATE:
  638. isc_encode_sql_date(&t, &buf[i].val.dtval);
  639. break;
  640. case SQL_TYPE_TIME:
  641. isc_encode_sql_time(&t, &buf[i].val.tmval);
  642. break;
  643. }
  644. continue;
  645. case SQL_BLOB:
  646. convert_to_string(b_var);
  647. if (Z_STRLEN_P(b_var) != BLOB_ID_LEN ||
  648. !_php_ibase_string_to_quad(Z_STRVAL_P(b_var), &buf[i].val.qval)) {
  649. ibase_blob ib_blob = { 0, BLOB_INPUT };
  650. if (isc_create_blob(IB_STATUS, &ib_query->link->handle,
  651. &ib_query->trans->handle, &ib_blob.bl_handle, &ib_blob.bl_qd)) {
  652. _php_ibase_error();
  653. return FAILURE;
  654. }
  655. if (_php_ibase_blob_add(b_var, &ib_blob) != SUCCESS) {
  656. return FAILURE;
  657. }
  658. if (isc_close_blob(IB_STATUS, &ib_blob.bl_handle)) {
  659. _php_ibase_error();
  660. return FAILURE;
  661. }
  662. buf[i].val.qval = ib_blob.bl_qd;
  663. }
  664. continue;
  665. case SQL_ARRAY:
  666. if (Z_TYPE_P(b_var) != IS_ARRAY) {
  667. convert_to_string(b_var);
  668. if (Z_STRLEN_P(b_var) != BLOB_ID_LEN ||
  669. !_php_ibase_string_to_quad(Z_STRVAL_P(b_var), &buf[i].val.qval)) {
  670. _php_ibase_module_error("Parameter %d: invalid array ID",i+1);
  671. rv = FAILURE;
  672. }
  673. } else {
  674. /* convert the array data into something IB can understand */
  675. ibase_array *ar = &ib_query->in_array[array_cnt];
  676. void *array_data = emalloc(ar->ar_size);
  677. ISC_QUAD array_id = { 0, 0 };
  678. if (FAILURE == _php_ibase_bind_array(b_var, array_data, ar->ar_size,
  679. ar, 0)) {
  680. _php_ibase_module_error("Parameter %d: failed to bind array argument", i+1);
  681. efree(array_data);
  682. rv = FAILURE;
  683. continue;
  684. }
  685. if (isc_array_put_slice(IB_STATUS, &ib_query->link->handle, &ib_query->trans->handle,
  686. &array_id, &ar->ar_desc, array_data, &ar->ar_size)) {
  687. _php_ibase_error();
  688. efree(array_data);
  689. return FAILURE;
  690. }
  691. buf[i].val.qval = array_id;
  692. efree(array_data);
  693. }
  694. ++array_cnt;
  695. continue;
  696. } /* switch */
  697. /* we end up here if none of the switch cases handled the field */
  698. convert_to_string(b_var);
  699. var->sqldata = Z_STRVAL_P(b_var);
  700. var->sqllen = Z_STRLEN_P(b_var);
  701. var->sqltype = SQL_TEXT;
  702. } /* for */
  703. return rv;
  704. }
  705. /* }}} */
  706. static void _php_ibase_alloc_xsqlda(XSQLDA *sqlda) /* {{{ */
  707. {
  708. int i;
  709. for (i = 0; i < sqlda->sqld; i++) {
  710. XSQLVAR *var = &sqlda->sqlvar[i];
  711. switch (var->sqltype & ~1) {
  712. case SQL_TEXT:
  713. var->sqldata = safe_emalloc(sizeof(char), var->sqllen, 0);
  714. break;
  715. case SQL_VARYING:
  716. var->sqldata = safe_emalloc(sizeof(char), var->sqllen + sizeof(short), 0);
  717. break;
  718. case SQL_SHORT:
  719. var->sqldata = emalloc(sizeof(short));
  720. break;
  721. case SQL_LONG:
  722. var->sqldata = emalloc(sizeof(ISC_LONG));
  723. break;
  724. case SQL_FLOAT:
  725. var->sqldata = emalloc(sizeof(float));
  726. break;
  727. case SQL_DOUBLE:
  728. var->sqldata = emalloc(sizeof(double));
  729. break;
  730. case SQL_INT64:
  731. var->sqldata = emalloc(sizeof(ISC_INT64));
  732. break;
  733. case SQL_TIMESTAMP:
  734. var->sqldata = emalloc(sizeof(ISC_TIMESTAMP));
  735. break;
  736. case SQL_TYPE_DATE:
  737. var->sqldata = emalloc(sizeof(ISC_DATE));
  738. break;
  739. case SQL_TYPE_TIME:
  740. var->sqldata = emalloc(sizeof(ISC_TIME));
  741. break;
  742. case SQL_BLOB:
  743. case SQL_ARRAY:
  744. var->sqldata = emalloc(sizeof(ISC_QUAD));
  745. break;
  746. } /* switch */
  747. if (var->sqltype & 1) { /* sql NULL flag */
  748. var->sqlind = emalloc(sizeof(short));
  749. } else {
  750. var->sqlind = NULL;
  751. }
  752. } /* for */
  753. }
  754. /* }}} */
  755. static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resultp, /* {{{ */
  756. ibase_query *ib_query, zval *args)
  757. {
  758. XSQLDA *in_sqlda = NULL, *out_sqlda = NULL;
  759. BIND_BUF *bind_buf = NULL;
  760. int i, rv = FAILURE;
  761. static char info_count[] = { isc_info_sql_records };
  762. char result[64];
  763. ISC_STATUS isc_result;
  764. int argc = ib_query->in_sqlda ? ib_query->in_sqlda->sqld : 0;
  765. RESET_ERRMSG;
  766. for (i = 0; i < argc; ++i) {
  767. SEPARATE_ZVAL(&args[i]);
  768. }
  769. switch (ib_query->statement_type) {
  770. isc_tr_handle tr;
  771. ibase_tr_list **l;
  772. ibase_trans *trans;
  773. case isc_info_sql_stmt_start_trans:
  774. /* a SET TRANSACTION statement should be executed with a NULL trans handle */
  775. tr = 0;
  776. if (isc_dsql_execute_immediate(IB_STATUS, &ib_query->link->handle, &tr, 0,
  777. ib_query->query, ib_query->dialect, NULL)) {
  778. _php_ibase_error();
  779. goto _php_ibase_exec_error;
  780. }
  781. trans = (ibase_trans *) emalloc(sizeof(ibase_trans));
  782. trans->handle = tr;
  783. trans->link_cnt = 1;
  784. trans->affected_rows = 0;
  785. trans->db_link[0] = ib_query->link;
  786. if (ib_query->link->tr_list == NULL) {
  787. ib_query->link->tr_list = (ibase_tr_list *) emalloc(sizeof(ibase_tr_list));
  788. ib_query->link->tr_list->trans = NULL;
  789. ib_query->link->tr_list->next = NULL;
  790. }
  791. /* link the transaction into the connection-transaction list */
  792. for (l = &ib_query->link->tr_list; *l != NULL; l = &(*l)->next);
  793. *l = (ibase_tr_list *) emalloc(sizeof(ibase_tr_list));
  794. (*l)->trans = trans;
  795. (*l)->next = NULL;
  796. RETVAL_RES(zend_register_resource(trans, le_trans));
  797. Z_TRY_ADDREF_P(return_value);
  798. return SUCCESS;
  799. case isc_info_sql_stmt_commit:
  800. case isc_info_sql_stmt_rollback:
  801. if (isc_dsql_execute_immediate(IB_STATUS, &ib_query->link->handle,
  802. &ib_query->trans->handle, 0, ib_query->query, ib_query->dialect, NULL)) {
  803. _php_ibase_error();
  804. goto _php_ibase_exec_error;
  805. }
  806. if (ib_query->trans->handle == 0 && ib_query->trans_res != NULL) {
  807. /* transaction was released by the query and was a registered resource,
  808. so we have to release it */
  809. zend_list_delete(ib_query->trans_res);
  810. ib_query->trans_res = NULL;
  811. }
  812. RETVAL_TRUE;
  813. return SUCCESS;
  814. default:
  815. RETVAL_FALSE;
  816. }
  817. /* allocate sqlda and output buffers */
  818. if (ib_query->out_sqlda) { /* output variables in select, select for update */
  819. ibase_result *res;
  820. IBDEBUG("Query wants XSQLDA for output");
  821. res = emalloc(sizeof(ibase_result)+sizeof(ibase_array)*max(0,ib_query->out_array_cnt-1));
  822. res->link = ib_query->link;
  823. res->trans = ib_query->trans;
  824. res->stmt = ib_query->stmt;
  825. /* ib_result and ib_query point at each other to handle release of statement handle properly */
  826. res->query = ib_query;
  827. ib_query->result = res;
  828. res->statement_type = ib_query->statement_type;
  829. res->has_more_rows = 1;
  830. out_sqlda = res->out_sqlda = emalloc(XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
  831. memcpy(out_sqlda, ib_query->out_sqlda, XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
  832. _php_ibase_alloc_xsqlda(out_sqlda);
  833. if (ib_query->out_array) {
  834. memcpy(&res->out_array, ib_query->out_array, sizeof(ibase_array)*ib_query->out_array_cnt);
  835. }
  836. *ib_resultp = res;
  837. }
  838. if (ib_query->in_sqlda) { /* has placeholders */
  839. IBDEBUG("Query wants XSQLDA for input");
  840. in_sqlda = emalloc(XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
  841. memcpy(in_sqlda, ib_query->in_sqlda, XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
  842. bind_buf = safe_emalloc(sizeof(BIND_BUF), ib_query->in_sqlda->sqld, 0);
  843. if (_php_ibase_bind(in_sqlda, args, bind_buf, ib_query) == FAILURE) {
  844. IBDEBUG("Could not bind input XSQLDA");
  845. goto _php_ibase_exec_error;
  846. }
  847. }
  848. if (ib_query->statement_type == isc_info_sql_stmt_exec_procedure) {
  849. isc_result = isc_dsql_execute2(IB_STATUS, &ib_query->trans->handle,
  850. &ib_query->stmt, SQLDA_CURRENT_VERSION, in_sqlda, out_sqlda);
  851. } else {
  852. isc_result = isc_dsql_execute(IB_STATUS, &ib_query->trans->handle,
  853. &ib_query->stmt, SQLDA_CURRENT_VERSION, in_sqlda);
  854. }
  855. if (isc_result) {
  856. IBDEBUG("Could not execute query");
  857. _php_ibase_error();
  858. goto _php_ibase_exec_error;
  859. }
  860. ib_query->trans->affected_rows = 0;
  861. switch (ib_query->statement_type) {
  862. unsigned long affected_rows;
  863. case isc_info_sql_stmt_insert:
  864. case isc_info_sql_stmt_update:
  865. case isc_info_sql_stmt_delete:
  866. case isc_info_sql_stmt_exec_procedure:
  867. if (isc_dsql_sql_info(IB_STATUS, &ib_query->stmt, sizeof(info_count),
  868. info_count, sizeof(result), result)) {
  869. _php_ibase_error();
  870. goto _php_ibase_exec_error;
  871. }
  872. affected_rows = 0;
  873. if (result[0] == isc_info_sql_records) {
  874. unsigned i = 3, result_size = isc_vax_integer(&result[1],2);
  875. while (result[i] != isc_info_end && i < result_size) {
  876. short len = (short)isc_vax_integer(&result[i+1],2);
  877. if (result[i] != isc_info_req_select_count) {
  878. affected_rows += isc_vax_integer(&result[i+3],len);
  879. }
  880. i += len+3;
  881. }
  882. }
  883. ib_query->trans->affected_rows = affected_rows;
  884. if (!ib_query->out_sqlda) { /* no result set is being returned */
  885. if (affected_rows) {
  886. RETVAL_LONG(affected_rows);
  887. } else {
  888. RETVAL_TRUE;
  889. }
  890. break;
  891. }
  892. default:
  893. RETVAL_TRUE;
  894. }
  895. rv = SUCCESS;
  896. _php_ibase_exec_error:
  897. if (in_sqlda) {
  898. efree(in_sqlda);
  899. }
  900. if (bind_buf)
  901. efree(bind_buf);
  902. if (rv == FAILURE) {
  903. if (*ib_resultp) {
  904. efree(*ib_resultp);
  905. *ib_resultp = NULL;
  906. }
  907. if (out_sqlda) {
  908. _php_ibase_free_xsqlda(out_sqlda);
  909. }
  910. }
  911. return rv;
  912. }
  913. /* }}} */
  914. /* {{{ proto mixed ibase_query([resource link_identifier, [ resource link_identifier, ]] string query [, mixed bind_arg [, mixed bind_arg [, ...]]])
  915. Execute a query */
  916. PHP_FUNCTION(ibase_query)
  917. {
  918. zval *zlink, *ztrans, *bind_args = NULL;
  919. char *query;
  920. size_t query_len;
  921. int bind_i, bind_num;
  922. zend_resource *trans_res = NULL;
  923. ibase_db_link *ib_link = NULL;
  924. ibase_trans *trans = NULL;
  925. ibase_query ib_query = { NULL, NULL, 0, 0 };
  926. ibase_result *result = NULL;
  927. RESET_ERRMSG;
  928. RETVAL_FALSE;
  929. switch (ZEND_NUM_ARGS()) {
  930. zend_long l;
  931. default:
  932. if (SUCCESS == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 3, "rrs",
  933. &zlink, &ztrans, &query, &query_len)) {
  934. ib_link = (ibase_db_link*)zend_fetch_resource2_ex(zlink, LE_LINK, le_link, le_plink);
  935. trans = (ibase_trans*)zend_fetch_resource_ex(ztrans, LE_TRANS, le_trans);
  936. trans_res = Z_RES_P(ztrans);
  937. bind_i = 3;
  938. break;
  939. }
  940. case 2:
  941. if (SUCCESS == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, 2, "rs",
  942. &zlink, &query, &query_len)) {
  943. _php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, zlink, &ib_link, &trans);
  944. if (trans != NULL) {
  945. trans_res = Z_RES_P(zlink);
  946. }
  947. bind_i = 2;
  948. break;
  949. }
  950. /* the statement is 'CREATE DATABASE ...' if the link argument is IBASE_CREATE */
  951. if (SUCCESS == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  952. "ls", &l, &query, &query_len) && l == PHP_IBASE_CREATE) {
  953. isc_db_handle db = 0;
  954. isc_tr_handle trans = 0;
  955. if (((l = INI_INT("ibase.max_links")) != -1) && (IBG(num_links) >= l)) {
  956. _php_ibase_module_error("CREATE DATABASE is not allowed: maximum link count "
  957. "(" ZEND_LONG_FMT ") reached", l);
  958. } else if (isc_dsql_execute_immediate(IB_STATUS, &db, &trans, (short)query_len,
  959. query, SQL_DIALECT_CURRENT, NULL)) {
  960. _php_ibase_error();
  961. } else if (!db) {
  962. _php_ibase_module_error("Connection to created database could not be "
  963. "established");
  964. } else {
  965. /* register the link as a resource; unfortunately, we cannot register
  966. it in the hash table, because we don't know the connection params */
  967. ib_link = (ibase_db_link *) emalloc(sizeof(ibase_db_link));
  968. ib_link->handle = db;
  969. ib_link->dialect = SQL_DIALECT_CURRENT;
  970. ib_link->tr_list = NULL;
  971. ib_link->event_head = NULL;
  972. RETVAL_RES(zend_register_resource(ib_link, le_link));
  973. Z_TRY_ADDREF_P(return_value);
  974. Z_TRY_ADDREF_P(return_value);
  975. IBG(default_link) = Z_RES_P(return_value);
  976. }
  977. return;
  978. }
  979. case 1:
  980. case 0:
  981. if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() ? 1 : 0, "s", &query,
  982. &query_len)) {
  983. ib_link = (ibase_db_link *)zend_fetch_resource2(IBG(default_link), LE_LINK, le_link, le_plink);
  984. bind_i = 1;
  985. break;
  986. }
  987. return;
  988. }
  989. /* open default transaction */
  990. if (ib_link == NULL || FAILURE == _php_ibase_def_trans(ib_link, &trans)
  991. || FAILURE == _php_ibase_alloc_query(&ib_query, ib_link, trans, query, ib_link->dialect, trans_res)) {
  992. return;
  993. }
  994. do {
  995. int bind_n = ZEND_NUM_ARGS() - bind_i,
  996. expected_n = ib_query.in_sqlda ? ib_query.in_sqlda->sqld : 0;
  997. if (bind_n != expected_n) {
  998. php_error_docref(NULL, (bind_n < expected_n) ? E_WARNING : E_NOTICE,
  999. "Statement expects %d arguments, %d given", expected_n, bind_n);
  1000. if (bind_n < expected_n) {
  1001. break;
  1002. }
  1003. } else if (bind_n > 0) {
  1004. if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &bind_args, &bind_num) == FAILURE) {
  1005. return;
  1006. }
  1007. }
  1008. if (FAILURE == _php_ibase_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, &result, &ib_query,
  1009. &bind_args[bind_i])) {
  1010. break;
  1011. }
  1012. if (result != NULL) { /* statement returns a result */
  1013. result->type = QUERY_RESULT;
  1014. /* EXECUTE PROCEDURE returns only one row => statement can be released immediately */
  1015. if (ib_query.statement_type != isc_info_sql_stmt_exec_procedure) {
  1016. ib_query.stmt = 0; /* keep stmt when free query */
  1017. }
  1018. RETVAL_RES(zend_register_resource(result, le_result));
  1019. Z_TRY_ADDREF_P(return_value);
  1020. }
  1021. } while (0);
  1022. _php_ibase_free_query(&ib_query);
  1023. }
  1024. /* }}} */
  1025. /* {{{ proto int ibase_affected_rows( [ resource link_identifier ] )
  1026. Returns the number of rows affected by the previous INSERT, UPDATE or DELETE statement */
  1027. PHP_FUNCTION(ibase_affected_rows)
  1028. {
  1029. ibase_trans *trans = NULL;
  1030. ibase_db_link *ib_link;
  1031. zval *arg = NULL;
  1032. RESET_ERRMSG;
  1033. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &arg) == FAILURE) {
  1034. return;
  1035. }
  1036. if (!arg) {
  1037. ib_link = (ibase_db_link *)zend_fetch_resource2(IBG(default_link), LE_LINK, le_link, le_plink);
  1038. if (ib_link->tr_list == NULL || ib_link->tr_list->trans == NULL) {
  1039. RETURN_FALSE;
  1040. }
  1041. trans = ib_link->tr_list->trans;
  1042. } else {
  1043. /* one id was passed, could be db or trans id */
  1044. _php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, arg, &ib_link, &trans);
  1045. if (trans == NULL) {
  1046. ib_link = (ibase_db_link *)zend_fetch_resource2_ex(arg, LE_LINK, le_link, le_plink);
  1047. if (ib_link->tr_list == NULL || ib_link->tr_list->trans == NULL) {
  1048. RETURN_FALSE;
  1049. }
  1050. trans = ib_link->tr_list->trans;
  1051. }
  1052. }
  1053. RETURN_LONG(trans->affected_rows);
  1054. }
  1055. /* }}} */
  1056. /* {{{ proto int ibase_num_rows( resource result_identifier )
  1057. Return the number of rows that are available in a result */
  1058. #if abies_0
  1059. PHP_FUNCTION(ibase_num_rows)
  1060. {
  1061. /**
  1062. * As this function relies on the InterBase API function isc_dsql_sql_info()
  1063. * which has a couple of limitations (which I hope will be fixed in future
  1064. * releases of Firebird), this function is fairly useless. I'm leaving it
  1065. * in place for people who can live with the limitations, which I only
  1066. * found out about after I had implemented it anyway.
  1067. *
  1068. * Currently, there's no way to determine how many rows can be fetched from
  1069. * a cursor. The only number that _can_ be determined is the number of rows
  1070. * that have already been pre-fetched by the client library.
  1071. * This implies the following:
  1072. * - num_rows() always returns zero before the first fetch;
  1073. * - num_rows() for SELECT ... FOR UPDATE is broken -> never returns a
  1074. * higher number than the number of records fetched so far (no pre-fetch);
  1075. * - the result of num_rows() for other statements is merely a lower bound
  1076. * on the number of records => calling ibase_num_rows() again after a couple
  1077. * of fetches will most likely return a new (higher) figure for large result
  1078. * sets.
  1079. */
  1080. zval *result_arg;
  1081. ibase_result *ib_result;
  1082. static char info_count[] = {isc_info_sql_records};
  1083. char result[64];
  1084. RESET_ERRMSG;
  1085. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &result_arg) == FAILURE) {
  1086. return;
  1087. }
  1088. ib_result = (ibase_result *)zend_fetch_resource_ex(&result_arg, LE_RESULT, le_result);
  1089. if (isc_dsql_sql_info(IB_STATUS, &ib_result->stmt, sizeof(info_count), info_count, sizeof(result), result)) {
  1090. _php_ibase_error();
  1091. RETURN_FALSE;
  1092. }
  1093. if (result[0] == isc_info_sql_records) {
  1094. unsigned i = 3, result_size = isc_vax_integer(&result[1],2);
  1095. while (result[i] != isc_info_end && i < result_size) {
  1096. short len = (short)isc_vax_integer(&result[i+1],2);
  1097. if (result[i] == isc_info_req_select_count) {
  1098. RETURN_LONG(isc_vax_integer(&result[i+3],len));
  1099. }
  1100. i += len+3;
  1101. }
  1102. }
  1103. }
  1104. #endif
  1105. /* }}} */
  1106. static int _php_ibase_var_zval(zval *val, void *data, int type, int len, /* {{{ */
  1107. int scale, int flag)
  1108. {
  1109. static ISC_INT64 const scales[] = { 1, 10, 100, 1000,
  1110. 10000,
  1111. 100000,
  1112. 1000000,
  1113. 10000000,
  1114. 100000000,
  1115. 1000000000,
  1116. LL_LIT(10000000000),
  1117. LL_LIT(100000000000),
  1118. LL_LIT(1000000000000),
  1119. LL_LIT(10000000000000),
  1120. LL_LIT(100000000000000),
  1121. LL_LIT(1000000000000000),
  1122. LL_LIT(10000000000000000),
  1123. LL_LIT(100000000000000000),
  1124. LL_LIT(1000000000000000000)
  1125. };
  1126. switch (type & ~1) {
  1127. unsigned short l;
  1128. zend_long n;
  1129. char string_data[255];
  1130. struct tm t;
  1131. char *format;
  1132. case SQL_VARYING:
  1133. len = ((IBVARY *) data)->vary_length;
  1134. data = ((IBVARY *) data)->vary_string;
  1135. /* no break */
  1136. case SQL_TEXT:
  1137. ZVAL_STRINGL(val, (char*)data, len);
  1138. break;
  1139. case SQL_SHORT:
  1140. n = *(short *) data;
  1141. goto _sql_long;
  1142. case SQL_INT64:
  1143. #if (SIZEOF_ZEND_LONG >= 8)
  1144. n = *(zend_long *) data;
  1145. goto _sql_long;
  1146. #else
  1147. if (scale == 0) {
  1148. l = slprintf(string_data, sizeof(string_data), "%" LL_MASK "d", *(ISC_INT64 *) data);
  1149. ZVAL_STRINGL(val,string_data,l);
  1150. } else {
  1151. ISC_INT64 n = *(ISC_INT64 *) data, f = scales[-scale];
  1152. if (n >= 0) {
  1153. l = slprintf(string_data, sizeof(string_data), "%" LL_MASK "d.%0*" LL_MASK "d", n / f, -scale, n % f);
  1154. } else if (n <= -f) {
  1155. l = slprintf(string_data, sizeof(string_data), "%" LL_MASK "d.%0*" LL_MASK "d", n / f, -scale, -n % f);
  1156. } else {
  1157. l = slprintf(string_data, sizeof(string_data), "-0.%0*" LL_MASK "d", -scale, -n % f);
  1158. }
  1159. ZVAL_STRINGL(val,string_data,l);
  1160. }
  1161. break;
  1162. #endif
  1163. case SQL_LONG:
  1164. n = *(ISC_LONG *) data;
  1165. _sql_long:
  1166. if (scale == 0) {
  1167. ZVAL_LONG(val,n);
  1168. } else {
  1169. zend_long f = (zend_long) scales[-scale];
  1170. if (n >= 0) {
  1171. l = slprintf(string_data, sizeof(string_data), ZEND_LONG_FMT ".%0*" ZEND_LONG_FMT_SPEC, n / f, -scale, n % f);
  1172. } else if (n <= -f) {
  1173. l = slprintf(string_data, sizeof(string_data), ZEND_LONG_FMT ".%0*" ZEND_LONG_FMT_SPEC, n / f, -scale, -n % f);
  1174. } else {
  1175. l = slprintf(string_data, sizeof(string_data), "-0.%0*" ZEND_LONG_FMT_SPEC, -scale, -n % f);
  1176. }
  1177. ZVAL_STRINGL(val, string_data, l);
  1178. }
  1179. break;
  1180. case SQL_FLOAT:
  1181. ZVAL_DOUBLE(val, *(float *) data);
  1182. break;
  1183. case SQL_DOUBLE:
  1184. ZVAL_DOUBLE(val, *(double *) data);
  1185. break;
  1186. case SQL_DATE: /* == case SQL_TIMESTAMP: */
  1187. format = INI_STR("ibase.timestampformat");
  1188. isc_decode_timestamp((ISC_TIMESTAMP *) data, &t);
  1189. goto format_date_time;
  1190. case SQL_TYPE_DATE:
  1191. format = INI_STR("ibase.dateformat");
  1192. isc_decode_sql_date((ISC_DATE *) data, &t);
  1193. goto format_date_time;
  1194. case SQL_TYPE_TIME:
  1195. format = INI_STR("ibase.timeformat");
  1196. isc_decode_sql_time((ISC_TIME *) data, &t);
  1197. format_date_time:
  1198. /*
  1199. XXX - Might have to remove this later - seems that isc_decode_date()
  1200. always sets tm_isdst to 0, sometimes incorrectly (InterBase 6 bug?)
  1201. */
  1202. t.tm_isdst = -1;
  1203. #if HAVE_TM_ZONE
  1204. t.tm_zone = tzname[0];
  1205. #endif
  1206. if (flag & PHP_IBASE_UNIXTIME) {
  1207. ZVAL_LONG(val, mktime(&t));
  1208. } else {
  1209. #if HAVE_STRFTIME
  1210. l = strftime(string_data, sizeof(string_data), format, &t);
  1211. #else
  1212. switch (type & ~1) {
  1213. default:
  1214. l = slprintf(string_data, sizeof(string_data), "%02d/%02d/%4d %02d:%02d:%02d", t.tm_mon+1, t.tm_mday,
  1215. t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec);
  1216. break;
  1217. case SQL_TYPE_DATE:
  1218. l = slprintf(string_data, sizeof(string_data), "%02d/%02d/%4d", t.tm_mon + 1, t.tm_mday, t.tm_year+1900);
  1219. break;
  1220. case SQL_TYPE_TIME:
  1221. l = slprintf(string_data, sizeof(string_data), "%02d:%02d:%02d", t.tm_hour, t.tm_min, t.tm_sec);
  1222. break;
  1223. }
  1224. #endif
  1225. ZVAL_STRINGL(val, string_data, l);
  1226. break;
  1227. }
  1228. } /* switch (type) */
  1229. return SUCCESS;
  1230. }
  1231. /* }}} */
  1232. static int _php_ibase_arr_zval(zval *ar_zval, char *data, zend_ulong data_size, /* {{{ */
  1233. ibase_array *ib_array, int dim, int flag)
  1234. {
  1235. /**
  1236. * Create multidimension array - recursion function
  1237. */
  1238. int
  1239. u_bound = ib_array->ar_desc.array_desc_bounds[dim].array_bound_upper,
  1240. l_bound = ib_array->ar_desc.array_desc_bounds[dim].array_bound_lower,
  1241. dim_len = 1 + u_bound - l_bound;
  1242. unsigned short i;
  1243. if (dim < ib_array->ar_desc.array_desc_dimensions) { /* array again */
  1244. zend_ulong slice_size = data_size / dim_len;
  1245. array_init(ar_zval);
  1246. for (i = 0; i < dim_len; ++i) {
  1247. zval slice_zval;
  1248. /* recursion here */
  1249. if (FAILURE == _php_ibase_arr_zval(&slice_zval, data, slice_size, ib_array, dim + 1,
  1250. flag)) {
  1251. return FAILURE;
  1252. }
  1253. data += slice_size;
  1254. add_index_zval(ar_zval, l_bound + i, &slice_zval);
  1255. }
  1256. } else { /* data at last */
  1257. if (FAILURE == _php_ibase_var_zval(ar_zval, data, ib_array->el_type,
  1258. ib_array->ar_desc.array_desc_length, ib_array->ar_desc.array_desc_scale, flag)) {
  1259. return FAILURE;
  1260. }
  1261. /* fix for peculiar handling of VARCHAR arrays;
  1262. truncate the field to the cstring length */
  1263. if (ib_array->ar_desc.array_desc_dtype == blr_varying ||
  1264. ib_array->ar_desc.array_desc_dtype == blr_varying2) {
  1265. Z_STRLEN_P(ar_zval) = strlen(Z_STRVAL_P(ar_zval));
  1266. }
  1267. }
  1268. return SUCCESS;
  1269. }
  1270. /* }}} */
  1271. static void _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int fetch_type) /* {{{ */
  1272. {
  1273. zval *result_arg;
  1274. zend_long flag = 0;
  1275. zend_long i, array_cnt = 0;
  1276. ibase_result *ib_result;
  1277. RESET_ERRMSG;
  1278. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &result_arg, &flag)) {
  1279. return;
  1280. }
  1281. ib_result = (ibase_result *)zend_fetch_resource_ex(result_arg, LE_RESULT, le_result);
  1282. if (ib_result->out_sqlda == NULL || !ib_result->has_more_rows) {
  1283. RETURN_FALSE;
  1284. }
  1285. if (ib_result->statement_type != isc_info_sql_stmt_exec_procedure) {
  1286. if (isc_dsql_fetch(IB_STATUS, &ib_result->stmt, 1, ib_result->out_sqlda)) {
  1287. ib_result->has_more_rows = 0;
  1288. if (IB_STATUS[0] && IB_STATUS[1]) { /* error in fetch */
  1289. _php_ibase_error();
  1290. }
  1291. RETURN_FALSE;
  1292. }
  1293. } else {
  1294. ib_result->has_more_rows = 0;
  1295. }
  1296. array_init(return_value);
  1297. for (i = 0; i < ib_result->out_sqlda->sqld; ++i) {
  1298. XSQLVAR *var = &ib_result->out_sqlda->sqlvar[i];
  1299. char buf[METADATALENGTH+4], *alias = var->aliasname;
  1300. if (! (fetch_type & FETCH_ROW)) {
  1301. int i = 0;
  1302. char const *base = "FIELD"; /* use 'FIELD' if name is empty */
  1303. /**
  1304. * Ensure no two columns have identical names:
  1305. * keep generating new names until we find one that is unique.
  1306. */
  1307. switch (*alias) {
  1308. void *p;
  1309. default:
  1310. i = 1;
  1311. base = alias;
  1312. while ((p = zend_symtable_str_find_ptr(
  1313. Z_ARRVAL_P(return_value), alias, strlen(alias))) != NULL) {
  1314. case '\0':
  1315. snprintf(alias = buf, sizeof(buf), "%s_%02d", base, i++);
  1316. }
  1317. }
  1318. }
  1319. if (((var->sqltype & 1) == 0) || *var->sqlind != -1) {
  1320. zval result;
  1321. switch (var->sqltype & ~1) {
  1322. default:
  1323. _php_ibase_var_zval(&result, var->sqldata, var->sqltype, var->sqllen,
  1324. var->sqlscale, flag);
  1325. break;
  1326. case SQL_BLOB:
  1327. if (flag & PHP_IBASE_FETCH_BLOBS) { /* fetch blob contents into hash */
  1328. ibase_blob blob_handle;
  1329. zend_ulong max_len = 0;
  1330. static char bl_items[] = {isc_info_blob_total_length};
  1331. char bl_info[20];
  1332. unsigned short i;
  1333. blob_handle.bl_handle = 0;
  1334. blob_handle.bl_qd = *(ISC_QUAD *) var->sqldata;
  1335. if (isc_open_blob(IB_STATUS, &ib_result->link->handle, &ib_result->trans->handle,
  1336. &blob_handle.bl_handle, &blob_handle.bl_qd)) {
  1337. _php_ibase_error();
  1338. goto _php_ibase_fetch_error;
  1339. }
  1340. if (isc_blob_info(IB_STATUS, &blob_handle.bl_handle, sizeof(bl_items),
  1341. bl_items, sizeof(bl_info), bl_info)) {
  1342. _php_ibase_error();
  1343. goto _php_ibase_fetch_error;
  1344. }
  1345. /* find total length of blob's data */
  1346. for (i = 0; i < sizeof(bl_info); ) {
  1347. unsigned short item_len;
  1348. char item = bl_info[i++];
  1349. if (item == isc_info_end || item == isc_info_truncated ||
  1350. item == isc_info_error || i >= sizeof(bl_info)) {
  1351. _php_ibase_module_error("Could not determine BLOB size (internal error)"
  1352. );
  1353. goto _php_ibase_fetch_error;
  1354. }
  1355. item_len = (unsigned short) isc_vax_integer(&bl_info[i], 2);
  1356. if (item == isc_info_blob_total_length) {
  1357. max_len = isc_vax_integer(&bl_info[i+2], item_len);
  1358. break;
  1359. }
  1360. i += item_len+2;
  1361. }
  1362. if (max_len == 0) {
  1363. ZVAL_STRING(&result, "");
  1364. } else if (SUCCESS != _php_ibase_blob_get(&result, &blob_handle,
  1365. max_len)) {
  1366. goto _php_ibase_fetch_error;
  1367. }
  1368. if (isc_close_blob(IB_STATUS, &blob_handle.bl_handle)) {
  1369. _php_ibase_error();
  1370. goto _php_ibase_fetch_error;
  1371. }
  1372. } else { /* blob id only */
  1373. ISC_QUAD bl_qd = *(ISC_QUAD *) var->sqldata;
  1374. ZVAL_NEW_STR(&result, _php_ibase_quad_to_string(bl_qd));
  1375. }
  1376. break;
  1377. case SQL_ARRAY:
  1378. if (flag & PHP_IBASE_FETCH_ARRAYS) { /* array can be *huge* so only fetch if asked */
  1379. ISC_QUAD ar_qd = *(ISC_QUAD *) var->sqldata;
  1380. ibase_array *ib_array = &ib_result->out_array[array_cnt++];
  1381. void *ar_data = emalloc(ib_array->ar_size);
  1382. if (isc_array_get_slice(IB_STATUS, &ib_result->link->handle,
  1383. &ib_result->trans->handle, &ar_qd, &ib_array->ar_desc,
  1384. ar_data, &ib_array->ar_size)) {
  1385. _php_ibase_error();
  1386. efree(ar_data);
  1387. goto _php_ibase_fetch_error;
  1388. }
  1389. if (FAILURE == _php_ibase_arr_zval(&result, ar_data, ib_array->ar_size, ib_array,
  1390. 0, flag)) {
  1391. efree(ar_data);
  1392. goto _php_ibase_fetch_error;
  1393. }
  1394. efree(ar_data);
  1395. } else { /* blob id only */
  1396. ISC_QUAD ar_qd = *(ISC_QUAD *) var->sqldata;
  1397. ZVAL_NEW_STR(&result, _php_ibase_quad_to_string(ar_qd));
  1398. }
  1399. break;
  1400. _php_ibase_fetch_error:
  1401. zval_ptr_dtor_nogc(&result);
  1402. RETURN_FALSE;
  1403. } /* switch */
  1404. if (fetch_type & FETCH_ROW) {
  1405. add_index_zval(return_value, i, &result);
  1406. } else {
  1407. add_assoc_zval(return_value, alias, &result);
  1408. }
  1409. } else {
  1410. if (fetch_type & FETCH_ROW) {
  1411. add_index_null(return_value, i);
  1412. } else {
  1413. add_assoc_null(return_value, alias);
  1414. }
  1415. }
  1416. } /* for field */
  1417. }
  1418. /* }}} */
  1419. /* {{{ proto array ibase_fetch_row(resource result [, int fetch_flags])
  1420. Fetch a row from the results of a query */
  1421. PHP_FUNCTION(ibase_fetch_row)
  1422. {
  1423. _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, FETCH_ROW);
  1424. }
  1425. /* }}} */
  1426. /* {{{ proto array ibase_fetch_assoc(resource result [, int fetch_flags])
  1427. Fetch a row from the results of a query */
  1428. PHP_FUNCTION(ibase_fetch_assoc)
  1429. {
  1430. _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, FETCH_ARRAY);
  1431. }
  1432. /* }}} */
  1433. /* {{{ proto object ibase_fetch_object(resource result [, int fetch_flags])
  1434. Fetch a object from the results of a query */
  1435. PHP_FUNCTION(ibase_fetch_object)
  1436. {
  1437. _php_ibase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, FETCH_ARRAY);
  1438. if (Z_TYPE_P(return_value) == IS_ARRAY) {
  1439. convert_to_object(return_value);
  1440. }
  1441. }
  1442. /* }}} */
  1443. /* {{{ proto bool ibase_name_result(resource result, string name)
  1444. Assign a name to a result for use with ... WHERE CURRENT OF <name> statements */
  1445. PHP_FUNCTION(ibase_name_result)
  1446. {
  1447. zval *result_arg;
  1448. char *name_arg;
  1449. size_t name_arg_len;
  1450. ibase_result *ib_result;
  1451. RESET_ERRMSG;
  1452. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &result_arg, &name_arg, &name_arg_len) == FAILURE) {
  1453. return;
  1454. }
  1455. ib_result = (ibase_result *)zend_fetch_resource_ex(result_arg, LE_RESULT, le_result);
  1456. if (isc_dsql_set_cursor_name(IB_STATUS, &ib_result->stmt, name_arg, 0)) {
  1457. _php_ibase_error();
  1458. RETURN_FALSE;
  1459. }
  1460. RETURN_TRUE;
  1461. }
  1462. /* }}} */
  1463. /* {{{ proto bool ibase_free_result(resource result)
  1464. Free the memory used by a result */
  1465. PHP_FUNCTION(ibase_free_result)
  1466. {
  1467. zval *result_arg;
  1468. ibase_result *ib_result;
  1469. RESET_ERRMSG;
  1470. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &result_arg) == FAILURE) {
  1471. return;
  1472. }
  1473. ib_result = (ibase_result *)zend_fetch_resource_ex(result_arg, LE_RESULT, le_result);
  1474. zend_list_delete(Z_RES_P(result_arg));
  1475. RETURN_TRUE;
  1476. }
  1477. /* }}} */
  1478. /* {{{ proto resource ibase_prepare(resource link_identifier[, string query [, resource trans_identifier ]])
  1479. Prepare a query for later execution */
  1480. PHP_FUNCTION(ibase_prepare)
  1481. {
  1482. zval *link_arg, *trans_arg;
  1483. ibase_db_link *ib_link;
  1484. ibase_trans *trans = NULL;
  1485. size_t query_len;
  1486. zend_resource *trans_res = NULL;
  1487. ibase_query *ib_query;
  1488. char *query;
  1489. RESET_ERRMSG;
  1490. if (ZEND_NUM_ARGS() == 1) {
  1491. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query, &query_len) == FAILURE) {
  1492. return;
  1493. }
  1494. ib_link = (ibase_db_link *)zend_fetch_resource2(IBG(default_link), LE_LINK, le_link, le_plink);
  1495. } else if (ZEND_NUM_ARGS() == 2) {
  1496. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &link_arg, &query, &query_len) == FAILURE) {
  1497. return;
  1498. }
  1499. _php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, link_arg, &ib_link, &trans);
  1500. if (trans != NULL) {
  1501. trans_res = Z_RES_P(link_arg);
  1502. }
  1503. } else {
  1504. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rrs", &link_arg, &trans_arg, &query, &query_len) == FAILURE) {
  1505. return;
  1506. }
  1507. ib_link = (ibase_db_link *)zend_fetch_resource2_ex(link_arg, LE_LINK, le_link, le_plink);
  1508. trans = (ibase_trans *)zend_fetch_resource_ex(trans_arg, LE_TRANS, le_trans);
  1509. trans_res = Z_RES_P(trans_arg);
  1510. }
  1511. if (FAILURE == _php_ibase_def_trans(ib_link, &trans)) {
  1512. RETURN_FALSE;
  1513. }
  1514. ib_query = (ibase_query *) emalloc(sizeof(ibase_query));
  1515. if (FAILURE == _php_ibase_alloc_query(ib_query, ib_link, trans, query, ib_link->dialect, trans_res)) {
  1516. efree(ib_query);
  1517. RETURN_FALSE;
  1518. }
  1519. RETVAL_RES(zend_register_resource(ib_query, le_query));
  1520. Z_TRY_ADDREF_P(return_value);
  1521. }
  1522. /* }}} */
  1523. /* {{{ proto mixed ibase_execute(resource query [, mixed bind_arg [, mixed bind_arg [, ...]]])
  1524. Execute a previously prepared query */
  1525. PHP_FUNCTION(ibase_execute)
  1526. {
  1527. zval *query, *args = NULL;
  1528. ibase_query *ib_query;
  1529. ibase_result *result = NULL;
  1530. int bind_n = 0;
  1531. RESET_ERRMSG;
  1532. RETVAL_FALSE;
  1533. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|r*", &query, &args, &bind_n)) {
  1534. return;
  1535. }
  1536. ib_query = (ibase_query *)zend_fetch_resource_ex(query, LE_QUERY, le_query);
  1537. do {
  1538. int expected_n = ib_query->in_sqlda ? ib_query->in_sqlda->sqld : 0;
  1539. if (bind_n != expected_n) {
  1540. php_error_docref(NULL, (bind_n < expected_n) ? E_WARNING : E_NOTICE,
  1541. "Statement expects %d arguments, %d given", expected_n, bind_n);
  1542. if (bind_n < expected_n) {
  1543. break;
  1544. }
  1545. }
  1546. /* Have we used this cursor before and it's still open (exec proc has no cursor) ? */
  1547. if (ib_query->result_res != NULL
  1548. && ib_query->statement_type != isc_info_sql_stmt_exec_procedure) {
  1549. IBDEBUG("Implicitly closing a cursor");
  1550. if (isc_dsql_free_statement(IB_STATUS, &ib_query->stmt, DSQL_close)) {
  1551. _php_ibase_error();
  1552. break;
  1553. }
  1554. zend_list_delete(ib_query->result_res);
  1555. ib_query->result_res = NULL;
  1556. }
  1557. if (FAILURE == _php_ibase_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, &result, ib_query,
  1558. args)) {
  1559. break;
  1560. }
  1561. /* free the query if trans handle was released */
  1562. if (ib_query->trans->handle == 0) {
  1563. zend_list_delete(Z_RES_P(query));
  1564. }
  1565. if (result != NULL) {
  1566. zval *ret;
  1567. result->type = EXECUTE_RESULT;
  1568. if (ib_query->statement_type == isc_info_sql_stmt_exec_procedure) {
  1569. result->stmt = 0;
  1570. }
  1571. ret = zend_list_insert(result, le_result);
  1572. ib_query->result_res = Z_RES_P(ret);
  1573. ZVAL_COPY_VALUE(return_value, ret);
  1574. Z_TRY_ADDREF_P(return_value);
  1575. Z_TRY_ADDREF_P(return_value);
  1576. }
  1577. } while (0);
  1578. }
  1579. /* }}} */
  1580. /* {{{ proto bool ibase_free_query(resource query)
  1581. Free memory used by a query */
  1582. PHP_FUNCTION(ibase_free_query)
  1583. {
  1584. zval *query_arg;
  1585. ibase_query *ib_query;
  1586. RESET_ERRMSG;
  1587. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &query_arg) == FAILURE) {
  1588. return;
  1589. }
  1590. ib_query = (ibase_query *)zend_fetch_resource_ex(query_arg, LE_QUERY, le_query);
  1591. if (!ib_query) {
  1592. RETURN_FALSE;
  1593. }
  1594. zend_list_close(Z_RES_P(query_arg));
  1595. RETURN_TRUE;
  1596. }
  1597. /* }}} */
  1598. /* {{{ proto int ibase_num_fields(resource query_result)
  1599. Get the number of fields in result */
  1600. PHP_FUNCTION(ibase_num_fields)
  1601. {
  1602. zval *result;
  1603. int type;
  1604. XSQLDA *sqlda;
  1605. RESET_ERRMSG;
  1606. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &result) == FAILURE) {
  1607. return;
  1608. }
  1609. type = Z_RES_P(result)->type;
  1610. if (type == le_query) {
  1611. ibase_query *ib_query;
  1612. ib_query = (ibase_query *)zend_fetch_resource_ex(result, LE_QUERY, le_query);
  1613. sqlda = ib_query->out_sqlda;
  1614. } else {
  1615. ibase_result *ib_result;
  1616. ib_result = (ibase_result *)zend_fetch_resource_ex(result, LE_RESULT, le_result);
  1617. sqlda = ib_result->out_sqlda;
  1618. }
  1619. if (sqlda == NULL) {
  1620. RETURN_LONG(0);
  1621. } else {
  1622. RETURN_LONG(sqlda->sqld);
  1623. }
  1624. }
  1625. /* }}} */
  1626. static void _php_ibase_field_info(zval *return_value, XSQLVAR *var) /* {{{ */
  1627. {
  1628. unsigned short len;
  1629. char buf[16], *s = buf;
  1630. array_init(return_value);
  1631. add_index_stringl(return_value, 0, var->sqlname, var->sqlname_length);
  1632. add_assoc_stringl(return_value, "name", var->sqlname, var->sqlname_length);
  1633. add_index_stringl(return_value, 1, var->aliasname, var->aliasname_length);
  1634. add_assoc_stringl(return_value, "alias", var->aliasname, var->aliasname_length);
  1635. add_index_stringl(return_value, 2, var->relname, var->relname_length);
  1636. add_assoc_stringl(return_value, "relation", var->relname, var->relname_length);
  1637. len = slprintf(buf, 16, "%d", var->sqllen);
  1638. add_index_stringl(return_value, 3, buf, len);
  1639. add_assoc_stringl(return_value, "length", buf, len);
  1640. if (var->sqlscale < 0) {
  1641. unsigned short precision = 0;
  1642. switch (var->sqltype & ~1) {
  1643. case SQL_SHORT:
  1644. precision = 4;
  1645. break;
  1646. case SQL_LONG:
  1647. precision = 9;
  1648. break;
  1649. case SQL_INT64:
  1650. precision = 18;
  1651. break;
  1652. }
  1653. len = slprintf(buf, 16, "NUMERIC(%d,%d)", precision, -var->sqlscale);
  1654. add_index_stringl(return_value, 4, s, len);
  1655. add_assoc_stringl(return_value, "type", s, len);
  1656. } else {
  1657. switch (var->sqltype & ~1) {
  1658. case SQL_TEXT:
  1659. s = "CHAR";
  1660. break;
  1661. case SQL_VARYING:
  1662. s = "VARCHAR";
  1663. break;
  1664. case SQL_SHORT:
  1665. s = "SMALLINT";
  1666. break;
  1667. case SQL_LONG:
  1668. s = "INTEGER";
  1669. break;
  1670. case SQL_FLOAT:
  1671. s = "FLOAT"; break;
  1672. case SQL_DOUBLE:
  1673. case SQL_D_FLOAT:
  1674. s = "DOUBLE PRECISION"; break;
  1675. case SQL_INT64:
  1676. s = "BIGINT";
  1677. break;
  1678. case SQL_TIMESTAMP:
  1679. s = "TIMESTAMP";
  1680. break;
  1681. case SQL_TYPE_DATE:
  1682. s = "DATE";
  1683. break;
  1684. case SQL_TYPE_TIME:
  1685. s = "TIME";
  1686. break;
  1687. case SQL_BLOB:
  1688. s = "BLOB";
  1689. break;
  1690. case SQL_ARRAY:
  1691. s = "ARRAY";
  1692. break;
  1693. /* FIXME: provide more detailed information about the field type, field size
  1694. * and array dimensions */
  1695. case SQL_QUAD:
  1696. s = "QUAD";
  1697. break;
  1698. }
  1699. add_index_string(return_value, 4, s);
  1700. add_assoc_string(return_value, "type", s);
  1701. }
  1702. }
  1703. /* }}} */
  1704. /* {{{ proto array ibase_field_info(resource query_result, int field_number)
  1705. Get information about a field */
  1706. PHP_FUNCTION(ibase_field_info)
  1707. {
  1708. zval *result_arg;
  1709. zend_long field_arg;
  1710. int type;
  1711. XSQLDA *sqlda;
  1712. RESET_ERRMSG;
  1713. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &result_arg, &field_arg) == FAILURE) {
  1714. return;
  1715. }
  1716. type = Z_RES_P(result_arg)->type;
  1717. if (type == le_query) {
  1718. ibase_query *ib_query;
  1719. ib_query= (ibase_query *)zend_fetch_resource_ex(result_arg, LE_QUERY, le_query);
  1720. sqlda = ib_query->out_sqlda;
  1721. } else {
  1722. ibase_result *ib_result;
  1723. ib_result = (ibase_result *)zend_fetch_resource_ex(result_arg, LE_RESULT, le_result);
  1724. sqlda = ib_result->out_sqlda;
  1725. }
  1726. if (sqlda == NULL) {
  1727. _php_ibase_module_error("Trying to get field info from a non-select query");
  1728. RETURN_FALSE;
  1729. }
  1730. if (field_arg < 0 || field_arg >= sqlda->sqld) {
  1731. RETURN_FALSE;
  1732. }
  1733. _php_ibase_field_info(return_value, sqlda->sqlvar + field_arg);
  1734. }
  1735. /* }}} */
  1736. /* {{{ proto int ibase_num_params(resource query)
  1737. Get the number of params in a prepared query */
  1738. PHP_FUNCTION(ibase_num_params)
  1739. {
  1740. zval *result;
  1741. ibase_query *ib_query;
  1742. RESET_ERRMSG;
  1743. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &result) == FAILURE) {
  1744. return;
  1745. }
  1746. ib_query = (ibase_query *)zend_fetch_resource_ex(result, LE_QUERY, le_query);
  1747. if (ib_query->in_sqlda == NULL) {
  1748. RETURN_LONG(0);
  1749. } else {
  1750. RETURN_LONG(ib_query->in_sqlda->sqld);
  1751. }
  1752. }
  1753. /* }}} */
  1754. /* {{{ proto array ibase_param_info(resource query, int field_number)
  1755. Get information about a parameter */
  1756. PHP_FUNCTION(ibase_param_info)
  1757. {
  1758. zval *result_arg;
  1759. zend_long field_arg;
  1760. ibase_query *ib_query;
  1761. RESET_ERRMSG;
  1762. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &result_arg, &field_arg) == FAILURE) {
  1763. return;
  1764. }
  1765. ib_query = (ibase_query *)zend_fetch_resource_ex(result_arg, LE_QUERY, le_query);
  1766. if (ib_query->in_sqlda == NULL) {
  1767. RETURN_FALSE;
  1768. }
  1769. if (field_arg < 0 || field_arg >= ib_query->in_sqlda->sqld) {
  1770. RETURN_FALSE;
  1771. }
  1772. _php_ibase_field_info(return_value,ib_query->in_sqlda->sqlvar + field_arg);
  1773. }
  1774. /* }}} */
  1775. #endif /* HAVE_IBASE */
  1776. /*
  1777. * Local variables:
  1778. * tab-width: 4
  1779. * c-basic-offset: 4
  1780. * End:
  1781. * vim600: sw=4 ts=4 fdm=marker
  1782. * vim<600: sw=4 ts=4
  1783. */