php_sybase_ct.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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: Zeev Suraski <zeev@zend.com> |
  16. | Tom May <tom@go2net.com> |
  17. | Timm Friebe <php_sybase_ct@thekid.de> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "php.h"
  25. #include "php_sybase_ct.h"
  26. #include "ext/standard/php_standard.h"
  27. #include "ext/standard/info.h"
  28. #include "php_globals.h"
  29. #include "php_ini.h"
  30. /* True globals, no need for thread safety */
  31. static int le_link, le_plink, le_result;
  32. #if HAVE_SYBASE_CT
  33. ZEND_DECLARE_MODULE_GLOBALS(sybase)
  34. static PHP_GINIT_FUNCTION(sybase);
  35. static PHP_GSHUTDOWN_FUNCTION(sybase);
  36. /* {{{ arginfo */
  37. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_connect, 0, 0, 0)
  38. ZEND_ARG_INFO(0, host)
  39. ZEND_ARG_INFO(0, user)
  40. ZEND_ARG_INFO(0, password)
  41. ZEND_ARG_INFO(0, charset)
  42. ZEND_ARG_INFO(0, appname)
  43. ZEND_ARG_INFO(0, new)
  44. ZEND_END_ARG_INFO()
  45. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_pconnect, 0, 0, 0)
  46. ZEND_ARG_INFO(0, host)
  47. ZEND_ARG_INFO(0, user)
  48. ZEND_ARG_INFO(0, password)
  49. ZEND_ARG_INFO(0, charset)
  50. ZEND_ARG_INFO(0, appname)
  51. ZEND_END_ARG_INFO()
  52. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_close, 0, 0, 0)
  53. ZEND_ARG_INFO(0, link_id)
  54. ZEND_END_ARG_INFO()
  55. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_select_db, 0, 0, 1)
  56. ZEND_ARG_INFO(0, database)
  57. ZEND_ARG_INFO(0, link_id)
  58. ZEND_END_ARG_INFO()
  59. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_query, 0, 0, 1)
  60. ZEND_ARG_INFO(0, query)
  61. ZEND_ARG_INFO(0, link_id)
  62. ZEND_END_ARG_INFO()
  63. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_unbuffered_query, 0, 0, 1)
  64. ZEND_ARG_INFO(0, query)
  65. ZEND_ARG_INFO(0, link_id)
  66. ZEND_END_ARG_INFO()
  67. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_free_result, 0, 0, 1)
  68. ZEND_ARG_INFO(0, result)
  69. ZEND_END_ARG_INFO()
  70. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_get_last_message, 0, 0, 1)
  71. ZEND_ARG_INFO(0, d)
  72. ZEND_END_ARG_INFO()
  73. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_num_rows, 0, 0, 1)
  74. ZEND_ARG_INFO(0, result)
  75. ZEND_END_ARG_INFO()
  76. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_num_fields, 0, 0, 1)
  77. ZEND_ARG_INFO(0, result)
  78. ZEND_END_ARG_INFO()
  79. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_row, 0, 0, 1)
  80. ZEND_ARG_INFO(0, result)
  81. ZEND_END_ARG_INFO()
  82. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_object, 0, 0, 1)
  83. ZEND_ARG_INFO(0, result)
  84. ZEND_ARG_INFO(0, object)
  85. ZEND_END_ARG_INFO()
  86. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_array, 0, 0, 1)
  87. ZEND_ARG_INFO(0, result)
  88. ZEND_END_ARG_INFO()
  89. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_assoc, 0, 0, 1)
  90. ZEND_ARG_INFO(0, result)
  91. ZEND_END_ARG_INFO()
  92. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_data_seek, 0, 0, 2)
  93. ZEND_ARG_INFO(0, result)
  94. ZEND_ARG_INFO(0, offset)
  95. ZEND_END_ARG_INFO()
  96. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_field, 0, 0, 1)
  97. ZEND_ARG_INFO(0, result)
  98. ZEND_ARG_INFO(0, offset)
  99. ZEND_END_ARG_INFO()
  100. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_field_seek, 0, 0, 2)
  101. ZEND_ARG_INFO(0, result)
  102. ZEND_ARG_INFO(0, offset)
  103. ZEND_END_ARG_INFO()
  104. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_result, 0, 0, 3)
  105. ZEND_ARG_INFO(0, result)
  106. ZEND_ARG_INFO(0, row)
  107. ZEND_ARG_INFO(0, field)
  108. ZEND_END_ARG_INFO()
  109. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_affected_rows, 0, 0, 0)
  110. ZEND_ARG_INFO(0, link_id)
  111. ZEND_END_ARG_INFO()
  112. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_min_client_severity, 0, 0, 1)
  113. ZEND_ARG_INFO(0, severity)
  114. ZEND_END_ARG_INFO()
  115. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_min_server_severity, 0, 0, 1)
  116. ZEND_ARG_INFO(0, severity)
  117. ZEND_END_ARG_INFO()
  118. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_deadlock_retry_count, 0, 0, 1)
  119. ZEND_ARG_INFO(0, retry_count)
  120. ZEND_END_ARG_INFO()
  121. ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_set_message_handler, 0, 0, 1)
  122. ZEND_ARG_INFO(0, error_func)
  123. ZEND_ARG_INFO(0, connection)
  124. ZEND_END_ARG_INFO()
  125. /* }}} */
  126. const zend_function_entry sybase_functions[] = {
  127. PHP_FE(sybase_connect, arginfo_sybase_connect)
  128. PHP_FE(sybase_pconnect, arginfo_sybase_pconnect)
  129. PHP_FE(sybase_close, arginfo_sybase_close)
  130. PHP_FE(sybase_select_db, arginfo_sybase_select_db)
  131. PHP_FE(sybase_query, arginfo_sybase_query)
  132. PHP_FE(sybase_unbuffered_query, arginfo_sybase_unbuffered_query)
  133. PHP_FE(sybase_free_result, arginfo_sybase_free_result)
  134. PHP_FE(sybase_get_last_message, arginfo_sybase_get_last_message)
  135. PHP_FE(sybase_num_rows, arginfo_sybase_num_rows)
  136. PHP_FE(sybase_num_fields, arginfo_sybase_num_fields)
  137. PHP_FE(sybase_fetch_row, arginfo_sybase_fetch_row)
  138. PHP_FE(sybase_fetch_array, arginfo_sybase_fetch_array)
  139. PHP_FE(sybase_fetch_assoc, arginfo_sybase_fetch_assoc)
  140. PHP_FE(sybase_fetch_object, arginfo_sybase_fetch_object)
  141. PHP_FE(sybase_data_seek, arginfo_sybase_data_seek)
  142. PHP_FE(sybase_fetch_field, arginfo_sybase_fetch_field)
  143. PHP_FE(sybase_field_seek, arginfo_sybase_field_seek)
  144. PHP_FE(sybase_result, arginfo_sybase_result)
  145. PHP_FE(sybase_affected_rows, arginfo_sybase_affected_rows)
  146. PHP_FE(sybase_min_client_severity, arginfo_sybase_min_client_severity)
  147. PHP_FE(sybase_min_server_severity, arginfo_sybase_min_server_severity)
  148. PHP_FE(sybase_set_message_handler, arginfo_sybase_set_message_handler)
  149. PHP_FE(sybase_deadlock_retry_count, arginfo_sybase_deadlock_retry_count)
  150. #if !defined(PHP_WIN32) && !defined(HAVE_MSSQL)
  151. PHP_FALIAS(mssql_connect, sybase_connect, arginfo_sybase_connect)
  152. PHP_FALIAS(mssql_pconnect, sybase_pconnect, arginfo_sybase_pconnect)
  153. PHP_FALIAS(mssql_close, sybase_close, arginfo_sybase_close)
  154. PHP_FALIAS(mssql_select_db, sybase_select_db, arginfo_sybase_select_db)
  155. PHP_FALIAS(mssql_query, sybase_query, arginfo_sybase_query)
  156. PHP_FALIAS(mssql_unbuffered_query, sybase_unbuffered_query, arginfo_sybase_unbuffered_query)
  157. PHP_FALIAS(mssql_free_result, sybase_free_result, arginfo_sybase_free_result)
  158. PHP_FALIAS(mssql_get_last_message, sybase_get_last_message, arginfo_sybase_get_last_message)
  159. PHP_FALIAS(mssql_num_rows, sybase_num_rows, arginfo_sybase_num_rows)
  160. PHP_FALIAS(mssql_num_fields, sybase_num_fields, arginfo_sybase_num_fields)
  161. PHP_FALIAS(mssql_fetch_row, sybase_fetch_row, arginfo_sybase_fetch_row)
  162. PHP_FALIAS(mssql_fetch_array, sybase_fetch_array, arginfo_sybase_fetch_array)
  163. PHP_FALIAS(mssql_fetch_assoc, sybase_fetch_assoc, arginfo_sybase_fetch_assoc)
  164. PHP_FALIAS(mssql_fetch_object, sybase_fetch_object, arginfo_sybase_fetch_object)
  165. PHP_FALIAS(mssql_data_seek, sybase_data_seek, arginfo_sybase_data_seek)
  166. PHP_FALIAS(mssql_fetch_field, sybase_fetch_field, arginfo_sybase_fetch_field)
  167. PHP_FALIAS(mssql_field_seek, sybase_field_seek, arginfo_sybase_field_seek)
  168. PHP_FALIAS(mssql_result, sybase_result, arginfo_sybase_result)
  169. PHP_FALIAS(mssql_affected_rows, sybase_affected_rows, arginfo_sybase_affected_rows)
  170. PHP_FALIAS(mssql_min_client_severity, sybase_min_client_severity, arginfo_sybase_min_client_severity)
  171. PHP_FALIAS(mssql_min_server_severity, sybase_min_server_severity, arginfo_sybase_min_server_severity)
  172. PHP_FALIAS(mssql_set_message_handler, sybase_set_message_handler, arginfo_sybase_set_message_handler)
  173. PHP_FALIAS(mssql_deadlock_retry_count, sybase_deadlock_retry_count, arginfo_sybase_deadlock_retry_count)
  174. #endif
  175. PHP_FE_END
  176. };
  177. zend_module_entry sybase_module_entry = {
  178. STANDARD_MODULE_HEADER,
  179. "sybase_ct",
  180. sybase_functions,
  181. PHP_MINIT(sybase),
  182. PHP_MSHUTDOWN(sybase),
  183. PHP_RINIT(sybase),
  184. PHP_RSHUTDOWN(sybase),
  185. PHP_MINFO(sybase),
  186. NO_VERSION_YET,
  187. PHP_MODULE_GLOBALS(sybase),
  188. PHP_GINIT(sybase),
  189. PHP_GSHUTDOWN(sybase),
  190. NULL,
  191. STANDARD_MODULE_PROPERTIES_EX
  192. };
  193. /* static CS_CONTEXT *context; */
  194. #ifdef COMPILE_DL_SYBASE_CT
  195. ZEND_GET_MODULE(sybase)
  196. #endif
  197. ZEND_DECLARE_MODULE_GLOBALS(sybase)
  198. #define CHECK_LINK(link) { if (link==-1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: A link to the server could not be established"); RETURN_FALSE; } }
  199. static int _clean_invalid_results(zend_rsrc_list_entry *le TSRMLS_DC)
  200. {
  201. if (Z_TYPE_P(le) == le_result) {
  202. sybase_link *sybase_ptr = ((sybase_result *) le->ptr)->sybase_ptr;
  203. if (!sybase_ptr->valid) {
  204. return 1;
  205. }
  206. }
  207. return 0;
  208. }
  209. #define efree_n(x) { efree(x); x = NULL; }
  210. #define efree_if(x) if (x) efree_n(x)
  211. #ifdef PHP_SYBASE_DEBUG
  212. #define FREE_SYBASE_RESULT(result) \
  213. if (result) { \
  214. fprintf(stderr, "_free_sybase_result(%p) called from line #%d\n", result, __LINE__); \
  215. fflush(stderr); \
  216. _free_sybase_result(result); \
  217. result = NULL; \
  218. }
  219. #else
  220. #define FREE_SYBASE_RESULT(result) \
  221. if (result) { \
  222. _free_sybase_result(result); \
  223. result = NULL; \
  224. }
  225. #endif
  226. static void _free_sybase_result(sybase_result *result)
  227. {
  228. int i, j;
  229. if (result->data) {
  230. for (i = 0; i < (result->store ? result->num_rows : MIN(1, result->num_rows)); i++) {
  231. for (j=0; j<result->num_fields; j++) {
  232. zval_dtor(&result->data[i][j]);
  233. }
  234. efree(result->data[i]);
  235. }
  236. efree(result->data);
  237. }
  238. if (result->fields) {
  239. for (i=0; i<result->num_fields; i++) {
  240. STR_FREE(result->fields[i].name);
  241. STR_FREE(result->fields[i].column_source);
  242. }
  243. efree(result->fields);
  244. }
  245. if (result->tmp_buffer) {
  246. for (i=0; i<result->num_fields; i++) {
  247. efree(result->tmp_buffer[i]);
  248. }
  249. efree(result->tmp_buffer);
  250. }
  251. efree_if(result->lengths);
  252. efree_if(result->indicators);
  253. efree_if(result->datafmt);
  254. efree_if(result->numerics);
  255. efree_if(result->types);
  256. efree(result);
  257. }
  258. /* Forward declaration */
  259. static int php_sybase_finish_results (sybase_result *result TSRMLS_DC);
  260. static void php_free_sybase_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  261. {
  262. sybase_result *result = (sybase_result *)rsrc->ptr;
  263. /* Check to see if we've read all rows */
  264. if (result->sybase_ptr && result->sybase_ptr->active_result_index) {
  265. if (result->sybase_ptr->cmd) {
  266. ct_cancel(NULL, result->sybase_ptr->cmd, CS_CANCEL_ALL);
  267. }
  268. php_sybase_finish_results(result TSRMLS_CC);
  269. }
  270. FREE_SYBASE_RESULT(result);
  271. }
  272. static void _close_sybase_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  273. {
  274. sybase_link *sybase_ptr = (sybase_link *)rsrc->ptr;
  275. CS_INT con_status;
  276. sybase_ptr->valid = 0;
  277. if (sybase_ptr->callback_name != NULL) {
  278. zval_ptr_dtor(&sybase_ptr->callback_name);
  279. sybase_ptr->callback_name= NULL;
  280. }
  281. zend_hash_apply(&EG(regular_list), (apply_func_t) _clean_invalid_results TSRMLS_CC);
  282. /* Non-persistent connections will always be connected or we wouldn't
  283. * get here, but since we want to check the death status anyway
  284. * we might as well double-check the connect status.
  285. */
  286. if (ct_con_props(sybase_ptr->connection, CS_GET, CS_CON_STATUS,
  287. &con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
  288. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to get connection status on close");
  289. /* Assume the worst. */
  290. con_status = CS_CONSTAT_CONNECTED | CS_CONSTAT_DEAD;
  291. }
  292. if (con_status & CS_CONSTAT_CONNECTED) {
  293. if ((con_status & CS_CONSTAT_DEAD) || ct_close(sybase_ptr->connection, CS_UNUSED)!=CS_SUCCEED) {
  294. ct_close(sybase_ptr->connection, CS_FORCE_CLOSE);
  295. }
  296. }
  297. ct_cmd_drop(sybase_ptr->cmd);
  298. ct_con_drop(sybase_ptr->connection);
  299. efree(sybase_ptr);
  300. SybCtG(num_links)--;
  301. }
  302. static void _close_sybase_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  303. {
  304. sybase_link *sybase_ptr = (sybase_link *)rsrc->ptr;
  305. CS_INT con_status;
  306. /* Persistent connections may have been closed before a failed
  307. * reopen attempt.
  308. */
  309. if (ct_con_props(sybase_ptr->connection, CS_GET, CS_CON_STATUS,
  310. &con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
  311. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to get connection status on close");
  312. /* Assume the worst. */
  313. con_status = CS_CONSTAT_CONNECTED | CS_CONSTAT_DEAD;
  314. }
  315. if (con_status & CS_CONSTAT_CONNECTED) {
  316. if ((con_status & CS_CONSTAT_DEAD) || ct_close(sybase_ptr->connection, CS_UNUSED)!=CS_SUCCEED) {
  317. ct_close(sybase_ptr->connection, CS_FORCE_CLOSE);
  318. }
  319. }
  320. ct_con_drop(sybase_ptr->connection);
  321. free(sybase_ptr);
  322. SybCtG(num_persistent)--;
  323. SybCtG(num_links)--;
  324. }
  325. static CS_RETCODE CS_PUBLIC _client_message_handler(CS_CONTEXT *context, CS_CONNECTION *connection, CS_CLIENTMSG *errmsg)
  326. {
  327. TSRMLS_FETCH();
  328. if (CS_SEVERITY(errmsg->msgnumber) >= SybCtG(min_client_severity)) {
  329. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Client message: %s (severity %ld)", errmsg->msgstring, (long)CS_SEVERITY(errmsg->msgnumber));
  330. }
  331. STR_FREE(SybCtG(server_message));
  332. SybCtG(server_message) = estrdup(errmsg->msgstring);
  333. /* If this is a timeout message, return CS_FAIL to cancel the
  334. * operation and mark the connection as dead.
  335. */
  336. if (CS_SEVERITY(errmsg->msgnumber) == CS_SV_RETRY_FAIL &&
  337. CS_NUMBER(errmsg->msgnumber) == 63 &&
  338. CS_ORIGIN(errmsg->msgnumber) == 2 &&
  339. CS_LAYER(errmsg->msgnumber) == 1)
  340. {
  341. return CS_FAIL;
  342. }
  343. return CS_SUCCEED;
  344. }
  345. static int _call_message_handler(zval *callback_name, CS_SERVERMSG *srvmsg TSRMLS_DC)
  346. {
  347. int handled = 0;
  348. zval *msgnumber, *severity, *state, *line, *text, *retval = NULL;
  349. zval **args[5];
  350. /* Border case - empty fcall */
  351. if (NULL == callback_name) return 0;
  352. /* Build arguments */
  353. MAKE_STD_ZVAL(msgnumber);
  354. ZVAL_LONG(msgnumber, srvmsg->msgnumber);
  355. args[0] = &msgnumber;
  356. MAKE_STD_ZVAL(severity);
  357. ZVAL_LONG(severity, srvmsg->severity);
  358. args[1] = &severity;
  359. MAKE_STD_ZVAL(state);
  360. ZVAL_LONG(state, srvmsg->state);
  361. args[2] = &state;
  362. MAKE_STD_ZVAL(line);
  363. ZVAL_LONG(line, srvmsg->line);
  364. args[3] = &line;
  365. MAKE_STD_ZVAL(text);
  366. ZVAL_STRING(text, srvmsg->text, 1);
  367. args[4] = &text;
  368. if (call_user_function_ex(EG(function_table), NULL, callback_name, &retval, 5, args, 0, NULL TSRMLS_CC) == FAILURE) {
  369. zval expr_copy;
  370. int use_copy;
  371. zend_make_printable_zval(callback_name, &expr_copy, &use_copy);
  372. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Cannot call the messagehandler %s", Z_STRVAL(expr_copy));
  373. zval_dtor(&expr_copy);
  374. }
  375. if (retval) {
  376. handled = ((Z_TYPE_P(retval) != IS_BOOL) || (Z_BVAL_P(retval) != 0));
  377. zval_ptr_dtor(&retval);
  378. } else {
  379. handled = 0;
  380. }
  381. zval_ptr_dtor(&msgnumber);
  382. zval_ptr_dtor(&severity);
  383. zval_ptr_dtor(&state);
  384. zval_ptr_dtor(&line);
  385. zval_ptr_dtor(&text);
  386. return handled;
  387. }
  388. static CS_RETCODE CS_PUBLIC _server_message_handler(CS_CONTEXT *context, CS_CONNECTION *connection, CS_SERVERMSG *srvmsg)
  389. {
  390. sybase_link *sybase;
  391. int handled = 0;
  392. TSRMLS_FETCH();
  393. /* Remember the last server message in any case */
  394. STR_FREE(SybCtG(server_message));
  395. SybCtG(server_message) = estrdup(srvmsg->text);
  396. /* Retrieve sybase link */
  397. if (ct_con_props(connection, CS_GET, CS_USERDATA, &sybase, CS_SIZEOF(sybase), NULL) != CS_SUCCEED) {
  398. sybase = NULL;
  399. }
  400. /* If this is a deadlock message, set the connection's deadlock flag
  401. * so we will retry the request. Sorry about the bare constant here,
  402. * but it's not defined anywhere and it's a "well-known" number.
  403. */
  404. if (sybase && (srvmsg->msgnumber == 1205)) {
  405. sybase->deadlock = 1;
  406. }
  407. /* Check mininum server severity level */
  408. if (srvmsg->severity < SybCtG(min_server_severity)) {
  409. return CS_SUCCEED;
  410. }
  411. /* Call global message handler */
  412. handled = handled | _call_message_handler(SybCtG(callback_name), srvmsg TSRMLS_CC);
  413. /* Call link specific message handler */
  414. if (sybase) {
  415. handled = handled | _call_message_handler(sybase->callback_name, srvmsg TSRMLS_CC);
  416. }
  417. /* Spit out a warning if neither of them has handled this message */
  418. if (!handled) {
  419. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Server message: %s (severity %ld, procedure %s)",
  420. srvmsg->text, (long)srvmsg->severity, ((srvmsg->proclen>0) ? srvmsg->proc : "N/A"));
  421. }
  422. return CS_SUCCEED;
  423. }
  424. PHP_INI_BEGIN()
  425. STD_PHP_INI_BOOLEAN("sybct.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong, allow_persistent, zend_sybase_globals, sybase_globals)
  426. STD_PHP_INI_ENTRY_EX("sybct.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_sybase_globals, sybase_globals, display_link_numbers)
  427. STD_PHP_INI_ENTRY_EX("sybct.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_sybase_globals, sybase_globals, display_link_numbers)
  428. STD_PHP_INI_ENTRY("sybct.min_server_severity", "10", PHP_INI_ALL, OnUpdateLong, min_server_severity, zend_sybase_globals, sybase_globals)
  429. STD_PHP_INI_ENTRY("sybct.min_client_severity", "10", PHP_INI_ALL, OnUpdateLong, min_client_severity, zend_sybase_globals, sybase_globals)
  430. STD_PHP_INI_ENTRY("sybct.login_timeout", "-1", PHP_INI_ALL, OnUpdateLong, login_timeout, zend_sybase_globals, sybase_globals)
  431. STD_PHP_INI_ENTRY("sybct.hostname", NULL, PHP_INI_ALL, OnUpdateString, hostname, zend_sybase_globals, sybase_globals)
  432. STD_PHP_INI_ENTRY_EX("sybct.deadlock_retry_count", "0", PHP_INI_ALL, OnUpdateLong, deadlock_retry_count, zend_sybase_globals, sybase_globals, display_link_numbers)
  433. PHP_INI_END()
  434. static PHP_GINIT_FUNCTION(sybase)
  435. {
  436. long opt;
  437. if (cs_ctx_alloc(CTLIB_VERSION, &sybase_globals->context)!=CS_SUCCEED || ct_init(sybase_globals->context, CTLIB_VERSION)!=CS_SUCCEED) {
  438. return;
  439. }
  440. /* Initialize message handlers */
  441. if (ct_callback(sybase_globals->context, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *)_server_message_handler)!=CS_SUCCEED) {
  442. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to set server message handler");
  443. }
  444. if (ct_callback(sybase_globals->context, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *)_client_message_handler)!=CS_SUCCEED) {
  445. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to set client message handler");
  446. }
  447. /* Set datetime conversion format to "Nov 3 1998 8:06PM".
  448. * This is the default format for the ct-lib that comes with
  449. * Sybase ASE 11.5.1 for Solaris, but the Linux libraries that
  450. * come with 11.0.3.3 default to "03/11/98" which is singularly
  451. * useless. This levels the playing field for all platforms.
  452. */
  453. {
  454. CS_INT dt_convfmt = CS_DATES_SHORT;
  455. if (cs_dt_info(sybase_globals->context, CS_SET, NULL, CS_DT_CONVFMT, CS_UNUSED, &dt_convfmt, sizeof(dt_convfmt), NULL)!=CS_SUCCEED) {
  456. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to set datetime conversion format");
  457. }
  458. }
  459. /* Set the timeout, which is per context and can't be set with
  460. * ct_con_props(), so set it globally from the config value if
  461. * requested. The default is CS_NO_LIMIT.
  462. *
  463. * Note that despite some noise in the documentation about using
  464. * signals to implement timeouts, they are actually implemented
  465. * by using poll() or select() on Solaris and Linux.
  466. */
  467. if (cfg_get_long("sybct.timeout", &opt)==SUCCESS) {
  468. CS_INT cs_timeout = opt;
  469. if (ct_config(sybase_globals->context, CS_SET, CS_TIMEOUT, &cs_timeout, CS_UNUSED, NULL)!=CS_SUCCEED) {
  470. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update the timeout");
  471. }
  472. }
  473. sybase_globals->num_persistent=0;
  474. sybase_globals->callback_name = NULL;
  475. }
  476. static PHP_GSHUTDOWN_FUNCTION(sybase)
  477. {
  478. ct_exit(sybase_globals->context, CS_UNUSED);
  479. cs_ctx_drop(sybase_globals->context);
  480. }
  481. PHP_MINIT_FUNCTION(sybase)
  482. {
  483. REGISTER_INI_ENTRIES();
  484. le_link = zend_register_list_destructors_ex(_close_sybase_link, NULL, "sybase-ct link", module_number);
  485. le_plink = zend_register_list_destructors_ex(NULL, _close_sybase_plink, "sybase-ct link persistent", module_number);
  486. le_result = zend_register_list_destructors_ex(php_free_sybase_result, NULL, "sybase-ct result", module_number);
  487. return SUCCESS;
  488. }
  489. PHP_RINIT_FUNCTION(sybase)
  490. {
  491. SybCtG(default_link)=-1;
  492. SybCtG(num_links) = SybCtG(num_persistent);
  493. SybCtG(appname) = estrndup("PHP " PHP_VERSION, sizeof("PHP " PHP_VERSION));
  494. SybCtG(server_message) = STR_EMPTY_ALLOC();
  495. return SUCCESS;
  496. }
  497. PHP_MSHUTDOWN_FUNCTION(sybase)
  498. {
  499. UNREGISTER_INI_ENTRIES();
  500. #if 0
  501. ct_exit(context, CS_UNUSED);
  502. cs_ctx_drop(context);
  503. #endif
  504. return SUCCESS;
  505. }
  506. PHP_RSHUTDOWN_FUNCTION(sybase)
  507. {
  508. efree(SybCtG(appname));
  509. SybCtG(appname) = NULL;
  510. if (SybCtG(callback_name)) {
  511. zval_ptr_dtor(&SybCtG(callback_name));
  512. SybCtG(callback_name)= NULL;
  513. }
  514. STR_FREE(SybCtG(server_message));
  515. SybCtG(server_message) = NULL;
  516. return SUCCESS;
  517. }
  518. static int php_sybase_do_connect_internal(sybase_link *sybase, char *host, char *user, char *passwd, char *charset, char *appname TSRMLS_DC)
  519. {
  520. CS_LOCALE *tmp_locale;
  521. long packetsize;
  522. /* set a CS_CONNECTION record */
  523. if (ct_con_alloc(SybCtG(context), &sybase->connection)!=CS_SUCCEED) {
  524. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to allocate connection record");
  525. return 0;
  526. }
  527. /* Note - this saves a copy of sybase, not a pointer to it. */
  528. if (ct_con_props(sybase->connection, CS_SET, CS_USERDATA, &sybase, CS_SIZEOF(sybase), NULL)!=CS_SUCCEED) {
  529. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to set userdata");
  530. ct_con_drop(sybase->connection);
  531. return 0;
  532. }
  533. if (user) {
  534. ct_con_props(sybase->connection, CS_SET, CS_USERNAME, user, CS_NULLTERM, NULL);
  535. }
  536. if (passwd) {
  537. ct_con_props(sybase->connection, CS_SET, CS_PASSWORD, passwd, CS_NULLTERM, NULL);
  538. }
  539. if (appname) {
  540. ct_con_props(sybase->connection, CS_SET, CS_APPNAME, appname, CS_NULLTERM, NULL);
  541. } else {
  542. ct_con_props(sybase->connection, CS_SET, CS_APPNAME, SybCtG(appname), CS_NULLTERM, NULL);
  543. }
  544. if (SybCtG(hostname)) {
  545. ct_con_props(sybase->connection, CS_SET, CS_HOSTNAME, SybCtG(hostname), CS_NULLTERM, NULL);
  546. }
  547. if (charset) {
  548. if (cs_loc_alloc(SybCtG(context), &tmp_locale)!=CS_SUCCEED) {
  549. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to allocate locale information");
  550. } else {
  551. if (cs_locale(SybCtG(context), CS_SET, tmp_locale, CS_LC_ALL, NULL, CS_NULLTERM, NULL)!=CS_SUCCEED) {
  552. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to load default locale data");
  553. } else {
  554. if (cs_locale(SybCtG(context), CS_SET, tmp_locale, CS_SYB_CHARSET, charset, CS_NULLTERM, NULL)!=CS_SUCCEED) {
  555. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update character set");
  556. } else {
  557. if (ct_con_props(sybase->connection, CS_SET, CS_LOC_PROP, tmp_locale, CS_UNUSED, NULL)!=CS_SUCCEED) {
  558. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update connection properties");
  559. }
  560. }
  561. }
  562. }
  563. }
  564. if (cfg_get_long("sybct.packet_size", &packetsize) == SUCCESS) {
  565. if (ct_con_props(sybase->connection, CS_SET, CS_PACKETSIZE, (CS_VOID *)&packetsize, CS_UNUSED, NULL) != CS_SUCCEED) {
  566. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update connection packetsize");
  567. }
  568. }
  569. /* Set the login timeout. Actually, the login timeout is per context
  570. * and not per connection, but we will update the context here to
  571. * allow for code such as the following:
  572. *
  573. * ini_set('sybct.login_timeout', $timeout);
  574. * sybase_connect(...)
  575. *
  576. * Note that preceding calls to sybase_connect() will now use the
  577. * updated value and not the default one!
  578. *
  579. * The default value for CS_LOGIN_TIMEOUT is 60 (1 minute).
  580. */
  581. if (SybCtG(login_timeout) != -1) {
  582. CS_INT cs_login_timeout = SybCtG(login_timeout);
  583. if (ct_config(SybCtG(context), CS_SET, CS_LOGIN_TIMEOUT, &cs_login_timeout, CS_UNUSED, NULL)!=CS_SUCCEED) {
  584. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update the login timeout");
  585. }
  586. }
  587. sybase->valid = 1;
  588. sybase->dead = 0;
  589. sybase->active_result_index = 0;
  590. sybase->callback_name = NULL;
  591. /* create the link */
  592. if (ct_connect(sybase->connection, host, CS_NULLTERM)!=CS_SUCCEED) {
  593. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to connect");
  594. ct_con_drop(sybase->connection);
  595. return 0;
  596. }
  597. if (ct_cmd_alloc(sybase->connection, &sybase->cmd)!=CS_SUCCEED) {
  598. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to allocate command record");
  599. ct_close(sybase->connection, CS_UNUSED);
  600. ct_con_drop(sybase->connection);
  601. return 0;
  602. }
  603. return 1;
  604. }
  605. static void php_sybase_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  606. {
  607. char *user = NULL, *passwd = NULL, *host = NULL, *charset = NULL, *appname = NULL;
  608. char *hashed_details;
  609. int hashed_details_length, len;
  610. zend_bool new = 0;
  611. sybase_link *sybase_ptr;
  612. host= user= passwd= charset= appname= NULL;
  613. if (persistent) {
  614. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!s!s!", &host, &len, &user, &len, &passwd, &len, &charset, &len, &appname, &len) == FAILURE) {
  615. return;
  616. }
  617. } else {
  618. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!s!s!b", &host, &len, &user, &len, &passwd, &len, &charset, &len, &appname, &len, &new) == FAILURE) {
  619. return;
  620. }
  621. }
  622. hashed_details_length = spprintf(
  623. &hashed_details,
  624. 0,
  625. "sybase_%s_%s_%s_%s_%s",
  626. host ? host : "",
  627. user ? user : "",
  628. passwd ? passwd : "",
  629. charset ? charset : "",
  630. appname ? appname : ""
  631. );
  632. if (!SybCtG(allow_persistent)) {
  633. persistent=0;
  634. }
  635. if (persistent) {
  636. zend_rsrc_list_entry *le;
  637. /* try to find if we already have this link in our persistent list */
  638. if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length+1, (void **) &le)==FAILURE) { /* we don't */
  639. zend_rsrc_list_entry new_le;
  640. if (SybCtG(max_links)!=-1 && SybCtG(num_links)>=SybCtG(max_links)) {
  641. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Too many open links (%ld)", SybCtG(num_links));
  642. efree(hashed_details);
  643. RETURN_FALSE;
  644. }
  645. if (SybCtG(max_persistent)!=-1 && SybCtG(num_persistent)>=SybCtG(max_persistent)) {
  646. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Too many open persistent links (%ld)", SybCtG(num_persistent));
  647. efree(hashed_details);
  648. RETURN_FALSE;
  649. }
  650. sybase_ptr = (sybase_link *) malloc(sizeof(sybase_link));
  651. if (!sybase_ptr) {
  652. efree(hashed_details);
  653. RETURN_FALSE;
  654. }
  655. if (!php_sybase_do_connect_internal(sybase_ptr, host, user, passwd, charset, appname TSRMLS_CC)) {
  656. free(sybase_ptr);
  657. efree(hashed_details);
  658. RETURN_FALSE;
  659. }
  660. /* hash it up */
  661. Z_TYPE(new_le) = le_plink;
  662. new_le.ptr = sybase_ptr;
  663. if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
  664. ct_close(sybase_ptr->connection, CS_UNUSED);
  665. ct_con_drop(sybase_ptr->connection);
  666. free(sybase_ptr);
  667. efree(hashed_details);
  668. RETURN_FALSE;
  669. }
  670. SybCtG(num_persistent)++;
  671. SybCtG(num_links)++;
  672. } else { /* we do */
  673. CS_INT con_status;
  674. if (Z_TYPE_P(le) != le_plink) {
  675. efree(hashed_details);
  676. RETURN_FALSE;
  677. }
  678. sybase_ptr = (sybase_link *) le->ptr;
  679. /* If the link has died, close it and overwrite it with a new one. */
  680. if (ct_con_props(sybase_ptr->connection, CS_GET, CS_CON_STATUS,
  681. &con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
  682. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to get connection status");
  683. efree(hashed_details);
  684. RETURN_FALSE;
  685. }
  686. if (!(con_status & CS_CONSTAT_CONNECTED) || (con_status & CS_CONSTAT_DEAD) || sybase_ptr->dead) {
  687. sybase_link sybase;
  688. if (con_status & CS_CONSTAT_CONNECTED) {
  689. ct_close(sybase_ptr->connection, CS_FORCE_CLOSE);
  690. }
  691. /* Create a new connection, then replace the old
  692. * connection. If we fail to create a new connection,
  693. * put the old one back so there will be a connection,
  694. * even if it is a non-functional one. This is because
  695. * code may still be holding an id for this connection
  696. * so we can't free the CS_CONNECTION.
  697. * (This is actually totally hokey, it would be better
  698. * to just ct_con_drop() the connection and set
  699. * sybase_ptr->connection to NULL, then test it for
  700. * NULL before trying to use it elsewhere . . .)
  701. */
  702. memcpy(&sybase, sybase_ptr, sizeof(sybase_link));
  703. if (!php_sybase_do_connect_internal(sybase_ptr, host, user, passwd, charset, appname TSRMLS_CC)) {
  704. memcpy(sybase_ptr, &sybase, sizeof(sybase_link));
  705. efree(hashed_details);
  706. RETURN_FALSE;
  707. }
  708. ct_con_drop(sybase.connection); /* drop old connection */
  709. }
  710. }
  711. ZEND_REGISTER_RESOURCE(return_value, sybase_ptr, le_plink);
  712. } else { /* non persistent */
  713. zend_rsrc_list_entry *index_ptr, new_index_ptr;
  714. /* first we check the hash for the hashed_details key. if it exists,
  715. * it should point us to the right offset where the actual sybase link sits.
  716. * if it doesn't, open a new sybase link, add it to the resource list,
  717. * and add a pointer to it with hashed_details as the key.
  718. */
  719. if (!new && zend_hash_find(&EG(regular_list), hashed_details, hashed_details_length+1, (void **) &index_ptr)==SUCCESS) {
  720. int type, link;
  721. void *ptr;
  722. if (Z_TYPE_P(index_ptr) != le_index_ptr) {
  723. efree(hashed_details);
  724. RETURN_FALSE;
  725. }
  726. link = (int) index_ptr->ptr;
  727. ptr = zend_list_find(link, &type); /* check if the link is still there */
  728. if (ptr && (type==le_link || type==le_plink)) {
  729. zend_list_addref(link);
  730. Z_LVAL_P(return_value) = SybCtG(default_link) = link;
  731. Z_TYPE_P(return_value) = IS_RESOURCE;
  732. efree(hashed_details);
  733. return;
  734. } else {
  735. zend_hash_del(&EG(regular_list), hashed_details, hashed_details_length+1);
  736. }
  737. }
  738. if (SybCtG(max_links)!=-1 && SybCtG(num_links)>=SybCtG(max_links)) {
  739. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Too many open links (%ld)", SybCtG(num_links));
  740. efree(hashed_details);
  741. RETURN_FALSE;
  742. }
  743. sybase_ptr = (sybase_link *) emalloc(sizeof(sybase_link));
  744. if (!php_sybase_do_connect_internal(sybase_ptr, host, user, passwd, charset, appname TSRMLS_CC)) {
  745. efree(sybase_ptr);
  746. efree(hashed_details);
  747. RETURN_FALSE;
  748. }
  749. /* add it to the list */
  750. ZEND_REGISTER_RESOURCE(return_value, sybase_ptr, le_link);
  751. /* add it to the hash */
  752. new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
  753. Z_TYPE(new_index_ptr) = le_index_ptr;
  754. if (zend_hash_update(&EG(regular_list), hashed_details, hashed_details_length+1, (void *) &new_index_ptr, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
  755. ct_close(sybase_ptr->connection, CS_UNUSED);
  756. ct_con_drop(sybase_ptr->connection);
  757. efree(sybase_ptr);
  758. efree(hashed_details);
  759. RETURN_FALSE;
  760. }
  761. SybCtG(num_links)++;
  762. }
  763. efree(hashed_details);
  764. SybCtG(default_link)=Z_LVAL_P(return_value);
  765. zend_list_addref(SybCtG(default_link));
  766. }
  767. static int php_sybase_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
  768. {
  769. if (SybCtG(default_link)==-1) { /* no link opened yet, implicitly open one */
  770. ht = 0;
  771. php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  772. }
  773. return SybCtG(default_link);
  774. }
  775. /* {{{ proto int sybase_connect([string host [, string user [, string password [, string charset [, string appname [, bool new]]]]]])
  776. Open Sybase server connection */
  777. PHP_FUNCTION(sybase_connect)
  778. {
  779. php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  780. }
  781. /* }}} */
  782. /* {{{ proto int sybase_pconnect([string host [, string user [, string password [, string charset [, string appname]]]]])
  783. Open persistent Sybase connection */
  784. PHP_FUNCTION(sybase_pconnect)
  785. {
  786. php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  787. }
  788. /* }}} */
  789. inline static int php_sybase_connection_id(zval *sybase_link_index, int *id TSRMLS_DC)
  790. {
  791. if (NULL == sybase_link_index) {
  792. if (-1 == SybCtG(default_link)) {
  793. return FAILURE;
  794. }
  795. *id = SybCtG(default_link);
  796. } else {
  797. *id = -1; /* explicit resource number */
  798. }
  799. return SUCCESS;
  800. }
  801. /* {{{ proto bool sybase_close([resource link_id])
  802. Close Sybase connection */
  803. PHP_FUNCTION(sybase_close)
  804. {
  805. zval *sybase_link_index = NULL;
  806. sybase_link *sybase_ptr;
  807. int id;
  808. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &sybase_link_index) == FAILURE) {
  809. return;
  810. }
  811. if (php_sybase_connection_id(sybase_link_index, &id TSRMLS_CC) == FAILURE) {
  812. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: No connection to close");
  813. RETURN_FALSE;
  814. }
  815. ZEND_FETCH_RESOURCE2(sybase_ptr, sybase_link *, &sybase_link_index, id, "Sybase-Link", le_link, le_plink);
  816. if (id == -1) {
  817. zend_list_delete(Z_RESVAL_P(sybase_link_index));
  818. }
  819. if (id != -1 || (sybase_link_index && Z_RESVAL_P(sybase_link_index) == SybCtG(default_link))) {
  820. zend_list_delete(SybCtG(default_link));
  821. SybCtG(default_link) = -1;
  822. }
  823. RETURN_TRUE;
  824. }
  825. /* }}} */
  826. static int exec_cmd(sybase_link *sybase_ptr, char *cmdbuf)
  827. {
  828. CS_RETCODE retcode;
  829. CS_INT restype;
  830. int failure=0;
  831. /* Fail if we already marked this connection dead. */
  832. if (sybase_ptr->dead) {
  833. return FAILURE;
  834. }
  835. /*
  836. ** Get a command handle, store the command string in it, and
  837. ** send it to the server.
  838. */
  839. if (ct_command(sybase_ptr->cmd, CS_LANG_CMD, cmdbuf, CS_NULLTERM, CS_UNUSED)!=CS_SUCCEED) {
  840. sybase_ptr->dead = 1;
  841. return FAILURE;
  842. }
  843. if (ct_send(sybase_ptr->cmd)!=CS_SUCCEED) {
  844. sybase_ptr->dead = 1;
  845. return FAILURE;
  846. }
  847. while ((retcode = ct_results(sybase_ptr->cmd, &restype))==CS_SUCCEED) {
  848. switch ((int) restype) {
  849. case CS_CMD_SUCCEED:
  850. case CS_CMD_DONE:
  851. break;
  852. case CS_CMD_FAIL:
  853. failure=1;
  854. break;
  855. case CS_STATUS_RESULT:
  856. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_CURRENT);
  857. break;
  858. default:
  859. failure=1;
  860. break;
  861. }
  862. if (failure) {
  863. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  864. return FAILURE;
  865. }
  866. }
  867. switch (retcode) {
  868. case CS_END_RESULTS:
  869. return SUCCESS;
  870. break;
  871. case CS_FAIL:
  872. /* Hopefully this either cleans up the connection, or the
  873. * connection ends up marked dead so it will be reopened
  874. * if it is persistent. We may want to do
  875. * ct_close(CS_FORCE_CLOSE) if ct_cancel() fails; see the
  876. * doc for ct_results()==CS_FAIL.
  877. */
  878. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  879. /* Don't take chances with the vagaries of ct-lib. Mark it
  880. * dead ourselves.
  881. */
  882. sybase_ptr->dead = 1;
  883. return FAILURE;
  884. default:
  885. return FAILURE;
  886. }
  887. }
  888. /* {{{ proto bool sybase_select_db(string database [, resource link_id])
  889. Select Sybase database */
  890. PHP_FUNCTION(sybase_select_db)
  891. {
  892. zval *sybase_link_index = NULL;
  893. char *db, *cmdbuf;
  894. int id, len;
  895. sybase_link *sybase_ptr;
  896. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &len, &sybase_link_index) == FAILURE) {
  897. return;
  898. }
  899. if (php_sybase_connection_id(sybase_link_index, &id TSRMLS_CC) == FAILURE) {
  900. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: No connection");
  901. RETURN_FALSE;
  902. }
  903. ZEND_FETCH_RESOURCE2(sybase_ptr, sybase_link *, &sybase_link_index, id, "Sybase-Link", le_link, le_plink);
  904. spprintf(&cmdbuf, 4 + len + 1, "use %s", db);
  905. if (exec_cmd(sybase_ptr, cmdbuf) == FAILURE) {
  906. efree(cmdbuf);
  907. RETURN_FALSE;
  908. } else {
  909. efree(cmdbuf);
  910. RETURN_TRUE;
  911. }
  912. }
  913. /* }}} */
  914. static int php_sybase_finish_results(sybase_result *result TSRMLS_DC)
  915. {
  916. int i, fail;
  917. CS_RETCODE retcode;
  918. CS_INT restype;
  919. efree_n(result->datafmt);
  920. efree_n(result->lengths);
  921. efree_n(result->indicators);
  922. efree_n(result->numerics);
  923. efree_n(result->types);
  924. for (i=0; i<result->num_fields; i++) {
  925. efree(result->tmp_buffer[i]);
  926. }
  927. efree_n(result->tmp_buffer);
  928. /* Indicate we have read all rows */
  929. result->sybase_ptr->active_result_index= 0;
  930. /* The only restype we should get now is CS_CMD_DONE, possibly
  931. * followed by a CS_STATUS_RESULT/CS_CMD_SUCCEED/CS_CMD_DONE
  932. * sequence if the command was a stored procedure call. But we
  933. * still need to read and discard unexpected results. We might
  934. * want to return a failure in this case because the application
  935. * won't be getting all the results it asked for.
  936. */
  937. fail = 0;
  938. while ((retcode = ct_results(result->sybase_ptr->cmd, &restype))==CS_SUCCEED) {
  939. switch ((int) restype) {
  940. case CS_CMD_SUCCEED:
  941. case CS_CMD_DONE:
  942. break;
  943. case CS_CMD_FAIL:
  944. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Command failed, canceling rest");
  945. ct_cancel(NULL, result->sybase_ptr->cmd, CS_CANCEL_ALL);
  946. fail = 1;
  947. break;
  948. case CS_COMPUTE_RESULT:
  949. case CS_CURSOR_RESULT:
  950. case CS_PARAM_RESULT:
  951. case CS_ROW_RESULT:
  952. /* Unexpected results, cancel them. */
  953. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Sybase: Unexpected results, canceling current");
  954. ct_cancel(NULL, result->sybase_ptr->cmd, CS_CANCEL_CURRENT);
  955. break;
  956. case CS_STATUS_RESULT:
  957. /* Status result from a stored procedure, cancel it but do not tell user */
  958. ct_cancel(NULL, result->sybase_ptr->cmd, CS_CANCEL_CURRENT);
  959. break;
  960. default:
  961. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Sybase: Unexpected results, canceling all");
  962. ct_cancel(NULL, result->sybase_ptr->cmd, CS_CANCEL_ALL);
  963. break;
  964. }
  965. if (fail) {
  966. break;
  967. }
  968. }
  969. switch (retcode) {
  970. case CS_END_RESULTS:
  971. /* Normal. */
  972. break;
  973. case CS_FAIL:
  974. /* Hopefully this either cleans up the connection, or the
  975. * connection ends up marked dead so it will be reopened
  976. * if it is persistent. We may want to do
  977. * ct_close(CS_FORCE_CLOSE) if ct_cancel() fails; see the
  978. * doc for ct_results()==CS_FAIL.
  979. */
  980. ct_cancel(NULL, result->sybase_ptr->cmd, CS_CANCEL_ALL);
  981. /* Don't take chances with the vagaries of ct-lib. Mark it
  982. * dead ourselves.
  983. */
  984. result->sybase_ptr->dead = 1;
  985. case CS_CANCELED:
  986. default:
  987. retcode = CS_FAIL;
  988. break;
  989. }
  990. return retcode;
  991. }
  992. #define RETURN_DOUBLE_VAL(result, buf, length) \
  993. if ((length - 1) <= EG(precision)) { \
  994. errno = 0; \
  995. Z_DVAL(result) = zend_strtod(buf, NULL); \
  996. if (errno != ERANGE) { \
  997. Z_TYPE(result) = IS_DOUBLE; \
  998. } else { \
  999. ZVAL_STRINGL(&result, buf, length- 1, 1); \
  1000. } \
  1001. } else { \
  1002. ZVAL_STRINGL(&result, buf, length- 1, 1); \
  1003. }
  1004. static int php_sybase_fetch_result_row(sybase_result *result, int numrows TSRMLS_DC)
  1005. {
  1006. int i, j;
  1007. CS_INT retcode;
  1008. /* We've already fetched everything */
  1009. if (result->last_retcode == CS_END_DATA || result->last_retcode == CS_END_RESULTS) {
  1010. return result->last_retcode;
  1011. }
  1012. if (numrows!=-1) numrows+= result->num_rows;
  1013. while ((retcode=ct_fetch(result->sybase_ptr->cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, NULL))==CS_SUCCEED || retcode == CS_ROW_FAIL) {
  1014. result->num_rows++;
  1015. i= result->store ? result->num_rows- 1 : 0;
  1016. if (i >= result->blocks_initialized*SYBASE_ROWS_BLOCK) {
  1017. result->data = (zval **) safe_erealloc(result->data, SYBASE_ROWS_BLOCK*(++result->blocks_initialized), sizeof(zval *), 0);
  1018. }
  1019. if (result->store || 1 == result->num_rows) {
  1020. result->data[i] = (zval *) safe_emalloc(sizeof(zval), result->num_fields, 0);
  1021. }
  1022. for (j = 0; j < result->num_fields; j++) {
  1023. /* If we are in non-storing mode, free the previous result */
  1024. if (!result->store && result->num_rows > 1 && Z_TYPE(result->data[i][j]) == IS_STRING) {
  1025. efree(Z_STRVAL(result->data[i][j]));
  1026. }
  1027. if (result->indicators[j] == -1) { /* null value */
  1028. ZVAL_NULL(&result->data[i][j]);
  1029. } else {
  1030. switch (result->numerics[j]) {
  1031. case 1: {
  1032. /* This indicates a long */
  1033. ZVAL_LONG(&result->data[i][j], strtol(result->tmp_buffer[j], NULL, 10));
  1034. break;
  1035. }
  1036. case 2: {
  1037. /* This indicates a float */
  1038. RETURN_DOUBLE_VAL(result->data[i][j], result->tmp_buffer[j], result->lengths[j]);
  1039. break;
  1040. }
  1041. case 3: {
  1042. /* This indicates either a long or a float, which ever fits */
  1043. errno = 0;
  1044. Z_LVAL(result->data[i][j]) = strtol(result->tmp_buffer[j], NULL, 10);
  1045. if (errno == ERANGE) {
  1046. /* An overflow occurred, so try to fit it into a double */
  1047. RETURN_DOUBLE_VAL(result->data[i][j], result->tmp_buffer[j], result->lengths[j]);
  1048. break;
  1049. }
  1050. Z_TYPE(result->data[i][j]) = IS_LONG;
  1051. break;
  1052. }
  1053. default: {
  1054. /* This indicates anything else, return it as string
  1055. * FreeTDS doesn't correctly set result->indicators[j] correctly
  1056. * for NULL fields in some version in conjunction with ASE 12.5
  1057. * but instead sets result->lengths[j] to 0, which would lead to
  1058. * a negative memory allocation (and thus a segfault).
  1059. */
  1060. if (result->lengths[j] < 1) {
  1061. ZVAL_NULL(&result->data[i][j]);
  1062. } else {
  1063. ZVAL_STRINGL(&result->data[i][j], result->tmp_buffer[j], result->lengths[j]- 1, 1);
  1064. }
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. if (numrows!=-1 && result->num_rows>=numrows) break;
  1071. }
  1072. if (retcode==CS_ROW_FAIL) {
  1073. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Error reading row %d", result->num_rows);
  1074. return retcode;
  1075. }
  1076. result->last_retcode= retcode;
  1077. switch (retcode) {
  1078. case CS_END_DATA:
  1079. retcode = php_sybase_finish_results(result TSRMLS_CC);
  1080. break;
  1081. case CS_ROW_FAIL:
  1082. case CS_SUCCEED:
  1083. break;
  1084. default:
  1085. FREE_SYBASE_RESULT(result);
  1086. result = NULL;
  1087. retcode = CS_FAIL; /* Just to be sure */
  1088. break;
  1089. }
  1090. return retcode;
  1091. }
  1092. static sybase_result * php_sybase_fetch_result_set(sybase_link *sybase_ptr, int buffered, int store TSRMLS_DC)
  1093. {
  1094. int num_fields;
  1095. sybase_result *result;
  1096. int i, j;
  1097. CS_INT retcode;
  1098. /* The following (if unbuffered) is more or less the equivalent of mysql_store_result().
  1099. * fetch all rows from the server into the row buffer, thus:
  1100. * 1) Being able to fire up another query without explicitly reading all rows
  1101. * 2) Having numrows accessible
  1102. */
  1103. if (ct_res_info(sybase_ptr->cmd, CS_NUMDATA, &num_fields, CS_UNUSED, NULL)!=CS_SUCCEED) {
  1104. return NULL;
  1105. }
  1106. result = (sybase_result *) emalloc(sizeof(sybase_result));
  1107. result->data = (zval **) safe_emalloc(sizeof(zval *), SYBASE_ROWS_BLOCK, 0);
  1108. result->fields = NULL;
  1109. result->sybase_ptr = sybase_ptr;
  1110. result->cur_field=result->cur_row=result->num_rows=0;
  1111. result->num_fields = num_fields;
  1112. result->last_retcode = 0;
  1113. result->store= store;
  1114. result->blocks_initialized= 1;
  1115. result->tmp_buffer = (char **) safe_emalloc(sizeof(char *), num_fields, 0);
  1116. result->lengths = (CS_INT *) safe_emalloc(sizeof(CS_INT), num_fields, 0);
  1117. result->indicators = (CS_SMALLINT *) safe_emalloc(sizeof(CS_INT), num_fields, 0);
  1118. result->datafmt = (CS_DATAFMT *) safe_emalloc(sizeof(CS_DATAFMT), num_fields, 0);
  1119. result->numerics = (unsigned char *) safe_emalloc(sizeof(unsigned char), num_fields, 0);
  1120. result->types = (CS_INT *) safe_emalloc(sizeof(CS_INT), num_fields, 0);
  1121. for (i=0; i<num_fields; i++) {
  1122. ct_describe(sybase_ptr->cmd, i+1, &result->datafmt[i]);
  1123. result->types[i] = result->datafmt[i].datatype;
  1124. switch (result->datafmt[i].datatype) {
  1125. case CS_CHAR_TYPE:
  1126. case CS_VARCHAR_TYPE:
  1127. case CS_TEXT_TYPE:
  1128. case CS_IMAGE_TYPE:
  1129. result->datafmt[i].maxlength++;
  1130. result->numerics[i] = 0;
  1131. break;
  1132. case CS_BINARY_TYPE:
  1133. case CS_VARBINARY_TYPE:
  1134. result->datafmt[i].maxlength *= 2;
  1135. result->datafmt[i].maxlength++;
  1136. result->numerics[i] = 0;
  1137. break;
  1138. case CS_BIT_TYPE:
  1139. case CS_TINYINT_TYPE:
  1140. result->datafmt[i].maxlength = 4;
  1141. result->numerics[i] = 1;
  1142. break;
  1143. case CS_SMALLINT_TYPE:
  1144. result->datafmt[i].maxlength = 7;
  1145. result->numerics[i] = 1;
  1146. break;
  1147. case CS_INT_TYPE:
  1148. result->datafmt[i].maxlength = 12;
  1149. result->numerics[i] = 1;
  1150. break;
  1151. case CS_REAL_TYPE:
  1152. case CS_FLOAT_TYPE:
  1153. result->datafmt[i].maxlength = 24;
  1154. result->numerics[i] = 2;
  1155. break;
  1156. case CS_MONEY_TYPE:
  1157. case CS_MONEY4_TYPE:
  1158. result->datafmt[i].maxlength = 24;
  1159. result->numerics[i] = 2;
  1160. break;
  1161. case CS_DATETIME_TYPE:
  1162. case CS_DATETIME4_TYPE:
  1163. result->datafmt[i].maxlength = 30;
  1164. result->numerics[i] = 0;
  1165. break;
  1166. case CS_NUMERIC_TYPE:
  1167. case CS_DECIMAL_TYPE:
  1168. result->datafmt[i].maxlength = result->datafmt[i].precision + 3;
  1169. /* numeric(10) vs numeric(10, 1) */
  1170. result->numerics[i] = (result->datafmt[i].scale == 0) ? 3 : 2;
  1171. break;
  1172. default:
  1173. result->datafmt[i].maxlength++;
  1174. result->numerics[i] = 0;
  1175. break;
  1176. }
  1177. result->tmp_buffer[i] = (char *)emalloc(result->datafmt[i].maxlength);
  1178. result->datafmt[i].datatype = CS_CHAR_TYPE;
  1179. result->datafmt[i].format = CS_FMT_NULLTERM;
  1180. ct_bind(sybase_ptr->cmd, i+1, &result->datafmt[i], result->tmp_buffer[i], &result->lengths[i], &result->indicators[i]);
  1181. }
  1182. result->fields = (sybase_field *) safe_emalloc(sizeof(sybase_field), num_fields, 0);
  1183. j=0;
  1184. for (i=0; i<num_fields; i++) {
  1185. char computed_buf[16];
  1186. if (result->datafmt[i].namelen>0) {
  1187. result->fields[i].name = estrndup(result->datafmt[i].name, result->datafmt[i].namelen);
  1188. } else {
  1189. if (j>0) {
  1190. snprintf(computed_buf, 16, "computed%d", j);
  1191. } else {
  1192. strcpy(computed_buf, "computed");
  1193. }
  1194. result->fields[i].name = estrdup(computed_buf);
  1195. j++;
  1196. }
  1197. result->fields[i].column_source = STR_EMPTY_ALLOC();
  1198. result->fields[i].max_length = result->datafmt[i].maxlength-1;
  1199. result->fields[i].numeric = result->numerics[i];
  1200. Z_TYPE(result->fields[i]) = result->types[i];
  1201. }
  1202. if (buffered) {
  1203. retcode = CS_SUCCEED;
  1204. } else {
  1205. if ((retcode = php_sybase_fetch_result_row(result, -1 TSRMLS_CC)) == CS_FAIL) {
  1206. return NULL;
  1207. }
  1208. }
  1209. result->last_retcode = retcode;
  1210. return result;
  1211. }
  1212. static void php_sybase_query (INTERNAL_FUNCTION_PARAMETERS, int buffered)
  1213. {
  1214. zval *sybase_link_index = NULL;
  1215. zend_bool store = 1;
  1216. char *query;
  1217. int len, id, deadlock_count;
  1218. sybase_link *sybase_ptr;
  1219. sybase_result *result;
  1220. CS_INT restype;
  1221. CS_RETCODE retcode;
  1222. enum {
  1223. Q_RESULT, /* Success with results. */
  1224. Q_SUCCESS, /* Success but no results. */
  1225. Q_FAILURE, /* Failure, no results. */
  1226. } status;
  1227. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|rb", &query, &len, &sybase_link_index, &store) == FAILURE) {
  1228. return;
  1229. }
  1230. if (!store && !buffered) {
  1231. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Sybase: Cannot use non-storing mode with buffered queries");
  1232. store = 1;
  1233. }
  1234. if (php_sybase_connection_id(sybase_link_index, &id TSRMLS_CC) == FAILURE) {
  1235. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: No connection");
  1236. RETURN_FALSE;
  1237. }
  1238. ZEND_FETCH_RESOURCE2(sybase_ptr, sybase_link *, &sybase_link_index, id, "Sybase-Link", le_link, le_plink);
  1239. /* Fail if we already marked this connection dead. */
  1240. if (sybase_ptr->dead) {
  1241. RETURN_FALSE;
  1242. }
  1243. /* Check to see if a previous sybase_unbuffered_query has read all rows */
  1244. if (sybase_ptr->active_result_index) {
  1245. zval *tmp = NULL;
  1246. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Sybase: Called without first fetching all rows from a previous unbuffered query");
  1247. if (sybase_ptr->cmd) {
  1248. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  1249. }
  1250. /* Get the resultset and free it */
  1251. ALLOC_ZVAL(tmp);
  1252. Z_LVAL_P(tmp)= sybase_ptr->active_result_index;
  1253. Z_TYPE_P(tmp)= IS_RESOURCE;
  1254. INIT_PZVAL(tmp);
  1255. ZEND_FETCH_RESOURCE(result, sybase_result *, &tmp, -1, "Sybase result", le_result);
  1256. if (result) {
  1257. php_sybase_finish_results(result TSRMLS_CC);
  1258. }
  1259. zval_ptr_dtor(&tmp);
  1260. zend_list_delete(sybase_ptr->active_result_index);
  1261. sybase_ptr->active_result_index= 0;
  1262. }
  1263. /* Repeat until we don't deadlock. */
  1264. deadlock_count= 0;
  1265. for (;;) {
  1266. result = NULL;
  1267. sybase_ptr->deadlock = 0;
  1268. sybase_ptr->affected_rows = 0;
  1269. /* On Solaris 11.5, ct_command() can be moved outside the
  1270. * loop, but not on Linux 11.0.
  1271. */
  1272. if (ct_command(sybase_ptr->cmd, CS_LANG_CMD, query, CS_NULLTERM, CS_UNUSED)!=CS_SUCCEED) {
  1273. /* If this didn't work, the connection is screwed but
  1274. * ct-lib might not set CS_CONSTAT_DEAD. So set our own
  1275. * flag. This happens sometimes when the database is restarted
  1276. * and/or its machine is rebooted, and ct_command() returns
  1277. * CS_BUSY for some reason.
  1278. */
  1279. sybase_ptr->dead = 1;
  1280. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Connection is dead");
  1281. RETURN_FALSE;
  1282. }
  1283. if (ct_send(sybase_ptr->cmd)!=CS_SUCCEED) {
  1284. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  1285. sybase_ptr->dead = 1;
  1286. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Cannot send command");
  1287. RETURN_FALSE;
  1288. }
  1289. /* Use the first result set or succeed/fail status and discard the
  1290. * others. Applications really shouldn't be making calls that
  1291. * return multiple result sets, but if they do then we need to
  1292. * properly read or cancel them or the connection will become
  1293. * unusable.
  1294. */
  1295. if (ct_results(sybase_ptr->cmd, &restype)!=CS_SUCCEED) {
  1296. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  1297. sybase_ptr->dead = 1;
  1298. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Cannot read results");
  1299. RETURN_FALSE;
  1300. }
  1301. switch ((int) restype) {
  1302. case CS_CMD_FAIL:
  1303. default:
  1304. status = Q_FAILURE;
  1305. break;
  1306. case CS_CMD_SUCCEED:
  1307. case CS_CMD_DONE: {
  1308. CS_INT row_count;
  1309. if (ct_res_info(sybase_ptr->cmd, CS_ROW_COUNT, &row_count, CS_UNUSED, NULL)==CS_SUCCEED) {
  1310. sybase_ptr->affected_rows = (long)row_count;
  1311. }
  1312. }
  1313. /* Fall through */
  1314. case CS_COMPUTEFMT_RESULT:
  1315. case CS_ROWFMT_RESULT:
  1316. case CS_DESCRIBE_RESULT:
  1317. case CS_MSG_RESULT:
  1318. buffered= 0; /* These queries have no need for buffering */
  1319. status = Q_SUCCESS;
  1320. break;
  1321. case CS_COMPUTE_RESULT:
  1322. case CS_CURSOR_RESULT:
  1323. case CS_PARAM_RESULT:
  1324. case CS_ROW_RESULT:
  1325. case CS_STATUS_RESULT:
  1326. result = php_sybase_fetch_result_set(sybase_ptr, buffered, store TSRMLS_CC);
  1327. if (result == NULL) {
  1328. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  1329. RETURN_FALSE;
  1330. }
  1331. status = Q_RESULT;
  1332. break;
  1333. }
  1334. /* Check for left-over results */
  1335. if (!buffered && status != Q_RESULT) {
  1336. while ((retcode = ct_results(sybase_ptr->cmd, &restype))==CS_SUCCEED) {
  1337. switch ((int) restype) {
  1338. case CS_CMD_SUCCEED:
  1339. case CS_CMD_DONE:
  1340. break;
  1341. case CS_CMD_FAIL:
  1342. status = Q_FAILURE;
  1343. break;
  1344. case CS_COMPUTE_RESULT:
  1345. case CS_CURSOR_RESULT:
  1346. case CS_PARAM_RESULT:
  1347. case CS_ROW_RESULT:
  1348. if (status != Q_RESULT) {
  1349. result = php_sybase_fetch_result_set(sybase_ptr, buffered, store TSRMLS_CC);
  1350. if (result == NULL) {
  1351. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  1352. sybase_ptr->dead = 1;
  1353. RETURN_FALSE;
  1354. }
  1355. status = Q_RESULT;
  1356. retcode = result->last_retcode;
  1357. } else {
  1358. /* Unexpected results, cancel them. */
  1359. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_CURRENT);
  1360. }
  1361. break;
  1362. case CS_STATUS_RESULT:
  1363. /* Unexpected results, cancel them. */
  1364. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_CURRENT);
  1365. break;
  1366. default:
  1367. status = Q_FAILURE;
  1368. break;
  1369. }
  1370. if (status == Q_FAILURE) {
  1371. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  1372. }
  1373. if (retcode == CS_END_RESULTS) {
  1374. break;
  1375. }
  1376. }
  1377. switch (retcode) {
  1378. case CS_END_RESULTS:
  1379. /* Normal. */
  1380. break;
  1381. case CS_FAIL:
  1382. /* Hopefully this either cleans up the connection, or the
  1383. * connection ends up marked dead so it will be reopened
  1384. * if it is persistent. We may want to do
  1385. * ct_close(CS_FORCE_CLOSE) if ct_cancel() fails; see the
  1386. * doc for ct_results()==CS_FAIL.
  1387. */
  1388. ct_cancel(NULL, sybase_ptr->cmd, CS_CANCEL_ALL);
  1389. /* Don't take chances with the vagaries of ct-lib. Mark it
  1390. * dead ourselves.
  1391. */
  1392. sybase_ptr->dead = 1;
  1393. case CS_CANCELED:
  1394. default:
  1395. status = Q_FAILURE;
  1396. break;
  1397. }
  1398. }
  1399. /* Retry deadlocks up until deadlock_retry_count times */
  1400. if (sybase_ptr->deadlock && SybCtG(deadlock_retry_count) != -1 && ++deadlock_count > SybCtG(deadlock_retry_count)) {
  1401. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Retried deadlock %d times [max: %ld], giving up", deadlock_count- 1, SybCtG(deadlock_retry_count));
  1402. FREE_SYBASE_RESULT(result);
  1403. break;
  1404. }
  1405. /* If query completed without deadlock, break out of the loop.
  1406. * Sometimes deadlock results in failures and sometimes not,
  1407. * it seems to depend on the server flavor. But we want to
  1408. * retry all deadlocks.
  1409. */
  1410. if (sybase_ptr->dead || sybase_ptr->deadlock == 0) {
  1411. break;
  1412. }
  1413. /* Get rid of any results we may have fetched. This happens:
  1414. * e.g., our result set may be a stored procedure status which
  1415. * is returned even if the stored procedure deadlocks. As an
  1416. * optimization, we could try not to fetch results in known
  1417. * deadlock conditions, but deadlock is (should be) rare.
  1418. */
  1419. FREE_SYBASE_RESULT(result);
  1420. }
  1421. if (status == Q_SUCCESS) {
  1422. RETURN_TRUE;
  1423. }
  1424. if (status == Q_FAILURE) {
  1425. FREE_SYBASE_RESULT(result);
  1426. RETURN_FALSE;
  1427. }
  1428. /* Indicate we have data in case of buffered queries */
  1429. id= ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  1430. sybase_ptr->active_result_index= buffered ? id : 0;
  1431. }
  1432. /* {{{ proto int sybase_query(string query [, resource link_id])
  1433. Send Sybase query */
  1434. PHP_FUNCTION(sybase_query)
  1435. {
  1436. php_sybase_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1437. }
  1438. /* }}} */
  1439. /* {{{ proto int sybase_unbuffered_query(string query [, resource link_id])
  1440. Send Sybase query */
  1441. PHP_FUNCTION(sybase_unbuffered_query)
  1442. {
  1443. php_sybase_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1444. }
  1445. /* {{{ proto bool sybase_free_result(resource result)
  1446. Free result memory */
  1447. PHP_FUNCTION(sybase_free_result)
  1448. {
  1449. zval *sybase_result_index = NULL;
  1450. sybase_result *result;
  1451. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &sybase_result_index) == FAILURE) {
  1452. return;
  1453. }
  1454. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1455. /* Did we fetch up until the end? */
  1456. if (result->last_retcode != CS_END_DATA && result->last_retcode != CS_END_RESULTS) {
  1457. /* php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: canceling the rest of the results"); */
  1458. ct_cancel(NULL, result->sybase_ptr->cmd, CS_CANCEL_ALL);
  1459. php_sybase_finish_results(result TSRMLS_CC);
  1460. }
  1461. zend_list_delete(Z_LVAL_P(sybase_result_index));
  1462. RETURN_TRUE;
  1463. }
  1464. /* }}} */
  1465. /* {{{ proto string sybase_get_last_message(void)
  1466. Returns the last message from server (over min_message_severity) */
  1467. PHP_FUNCTION(sybase_get_last_message)
  1468. {
  1469. RETURN_STRING(SybCtG(server_message), 1);
  1470. }
  1471. /* }}} */
  1472. /* {{{ proto int sybase_num_rows(resource result)
  1473. Get number of rows in result */
  1474. PHP_FUNCTION(sybase_num_rows)
  1475. {
  1476. zval *sybase_result_index = NULL;
  1477. sybase_result *result;
  1478. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &sybase_result_index) == FAILURE) {
  1479. return;
  1480. }
  1481. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1482. Z_LVAL_P(return_value) = result->num_rows;
  1483. Z_TYPE_P(return_value) = IS_LONG;
  1484. }
  1485. /* }}} */
  1486. /* {{{ proto int sybase_num_fields(resource result)
  1487. Get number of fields in result */
  1488. PHP_FUNCTION(sybase_num_fields)
  1489. {
  1490. zval *sybase_result_index = NULL;
  1491. sybase_result *result;
  1492. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &sybase_result_index) == FAILURE) {
  1493. return;
  1494. }
  1495. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1496. Z_LVAL_P(return_value) = result->num_fields;
  1497. Z_TYPE_P(return_value) = IS_LONG;
  1498. }
  1499. /* }}} */
  1500. /* {{{ proto array sybase_fetch_row(resource result)
  1501. Get row as enumerated array */
  1502. PHP_FUNCTION(sybase_fetch_row)
  1503. {
  1504. zval *sybase_result_index = NULL;
  1505. int i;
  1506. sybase_result *result;
  1507. zval *field_content;
  1508. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &sybase_result_index) == FAILURE) {
  1509. return;
  1510. }
  1511. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1512. /* Unbuffered? */
  1513. if (result->last_retcode != CS_END_DATA && result->last_retcode != CS_END_RESULTS) {
  1514. php_sybase_fetch_result_row(result, 1 TSRMLS_CC);
  1515. }
  1516. /* At the end? */
  1517. if (result->cur_row >= result->num_rows) {
  1518. RETURN_FALSE;
  1519. }
  1520. array_init(return_value);
  1521. for (i=0; i<result->num_fields; i++) {
  1522. ALLOC_ZVAL(field_content);
  1523. *field_content = result->data[result->store ? result->cur_row : 0][i];
  1524. INIT_PZVAL(field_content);
  1525. zval_copy_ctor(field_content);
  1526. zend_hash_index_update(Z_ARRVAL_P(return_value), i, (void *) &field_content, sizeof(zval* ), NULL);
  1527. }
  1528. result->cur_row++;
  1529. }
  1530. /* }}} */
  1531. static void php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int numerics)
  1532. {
  1533. zval *sybase_result_index = NULL;
  1534. sybase_result *result;
  1535. int i, j;
  1536. zval *tmp;
  1537. char name[32];
  1538. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &sybase_result_index) == FAILURE) {
  1539. return;
  1540. }
  1541. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1542. /* Unbuffered ? Fetch next row */
  1543. if (result->last_retcode != CS_END_DATA && result->last_retcode != CS_END_RESULTS) {
  1544. php_sybase_fetch_result_row(result, 1 TSRMLS_CC);
  1545. }
  1546. /* At the end? */
  1547. if (result->cur_row >= result->num_rows) {
  1548. RETURN_FALSE;
  1549. }
  1550. array_init(return_value);
  1551. j= 1;
  1552. for (i=0; i<result->num_fields; i++) {
  1553. ALLOC_ZVAL(tmp);
  1554. *tmp = result->data[result->store ? result->cur_row : 0][i];
  1555. INIT_PZVAL(tmp);
  1556. zval_copy_ctor(tmp);
  1557. if (numerics) {
  1558. zend_hash_index_update(Z_ARRVAL_P(return_value), i, (void *) &tmp, sizeof(zval *), NULL);
  1559. Z_ADDREF_P(tmp);
  1560. }
  1561. if (zend_hash_exists(Z_ARRVAL_P(return_value), result->fields[i].name, strlen(result->fields[i].name)+1)) {
  1562. snprintf(name, 32, "%s%d", result->fields[i].name, j);
  1563. result->fields[i].name= estrdup(name);
  1564. j++;
  1565. }
  1566. zend_hash_update(Z_ARRVAL_P(return_value), result->fields[i].name, strlen(result->fields[i].name)+1, (void *) &tmp, sizeof(zval *), NULL);
  1567. }
  1568. result->cur_row++;
  1569. }
  1570. /* {{{ proto object sybase_fetch_object(resource result [, mixed object])
  1571. Fetch row as object */
  1572. PHP_FUNCTION(sybase_fetch_object)
  1573. {
  1574. zval *object = NULL;
  1575. zval *sybase_result_index = NULL;
  1576. zend_class_entry *ce = NULL;
  1577. sybase_result *result;
  1578. /* Was a second parameter given? */
  1579. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z", &sybase_result_index, &object) == FAILURE) {
  1580. return;
  1581. }
  1582. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1583. ce = ZEND_STANDARD_CLASS_DEF_PTR;
  1584. if (NULL != object) {
  1585. switch (Z_TYPE_P(object)) {
  1586. case IS_OBJECT: {
  1587. ce = Z_OBJCE_P(object);
  1588. break;
  1589. }
  1590. case IS_NULL: {
  1591. /* Use default (ZEND_STANDARD_CLASS_DEF_PTR) */
  1592. break;
  1593. }
  1594. default: {
  1595. zend_class_entry **pce = NULL;
  1596. convert_to_string(object);
  1597. if (zend_lookup_class(Z_STRVAL_P(object), Z_STRLEN_P(object), &pce TSRMLS_CC) == FAILURE) {
  1598. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Sybase: Class %s has not been declared", Z_STRVAL_P(object));
  1599. /* Use default (ZEND_STANDARD_CLASS_DEF_PTR) */
  1600. } else {
  1601. ce = *pce;
  1602. }
  1603. }
  1604. }
  1605. }
  1606. /* Reset no. of arguments to 1 so that we can use INTERNAL_FUNCTION_PARAM_PASSTHRU */
  1607. ht= 1;
  1608. php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1609. if (Z_TYPE_P(return_value) == IS_ARRAY) {
  1610. object_and_properties_init(return_value, ce, Z_ARRVAL_P(return_value));
  1611. }
  1612. }
  1613. /* }}} */
  1614. /* {{{ proto array sybase_fetch_array(resource result)
  1615. Fetch row as array */
  1616. PHP_FUNCTION(sybase_fetch_array)
  1617. {
  1618. php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1619. }
  1620. /* }}} */
  1621. /* {{{ proto array sybase_fetch_assoc(resource result)
  1622. Fetch row as array without numberic indices */
  1623. PHP_FUNCTION(sybase_fetch_assoc)
  1624. {
  1625. php_sybase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1626. }
  1627. /* }}} */
  1628. /* {{{ proto bool sybase_data_seek(resource result, int offset)
  1629. Move internal row pointer */
  1630. PHP_FUNCTION(sybase_data_seek)
  1631. {
  1632. zval *sybase_result_index = NULL;
  1633. long offset;
  1634. sybase_result *result;
  1635. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &sybase_result_index, &offset) == FAILURE) {
  1636. return;
  1637. }
  1638. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1639. /* Unbuffered ? */
  1640. if (result->last_retcode != CS_END_DATA && result->last_retcode != CS_END_RESULTS && offset >= result->num_rows) {
  1641. php_sybase_fetch_result_row(result, offset+ 1 TSRMLS_CC);
  1642. }
  1643. if (offset < 0 || offset >= result->num_rows) {
  1644. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Bad row offset %ld, must be betweem 0 and %d", offset, result->num_rows - 1);
  1645. RETURN_FALSE;
  1646. }
  1647. result->cur_row = offset;
  1648. RETURN_TRUE;
  1649. }
  1650. /* }}} */
  1651. static char *php_sybase_get_field_name(CS_INT type)
  1652. {
  1653. switch (type) {
  1654. case CS_CHAR_TYPE:
  1655. case CS_VARCHAR_TYPE:
  1656. case CS_TEXT_TYPE:
  1657. return "string";
  1658. break;
  1659. case CS_IMAGE_TYPE:
  1660. return "image";
  1661. break;
  1662. case CS_BINARY_TYPE:
  1663. case CS_VARBINARY_TYPE:
  1664. return "blob";
  1665. break;
  1666. case CS_BIT_TYPE:
  1667. return "bit";
  1668. break;
  1669. case CS_TINYINT_TYPE:
  1670. case CS_SMALLINT_TYPE:
  1671. case CS_INT_TYPE:
  1672. return "int";
  1673. break;
  1674. case CS_REAL_TYPE:
  1675. case CS_FLOAT_TYPE:
  1676. case CS_NUMERIC_TYPE:
  1677. case CS_DECIMAL_TYPE:
  1678. return "real";
  1679. break;
  1680. case CS_MONEY_TYPE:
  1681. case CS_MONEY4_TYPE:
  1682. return "money";
  1683. break;
  1684. case CS_DATETIME_TYPE:
  1685. case CS_DATETIME4_TYPE:
  1686. return "datetime";
  1687. break;
  1688. default:
  1689. return "unknown";
  1690. break;
  1691. }
  1692. }
  1693. /* {{{ proto object sybase_fetch_field(resource result [, int offset])
  1694. Get field information */
  1695. PHP_FUNCTION(sybase_fetch_field)
  1696. {
  1697. zval *sybase_result_index = NULL;
  1698. long field_offset = -1;
  1699. sybase_result *result;
  1700. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &sybase_result_index, &field_offset) == FAILURE) {
  1701. return;
  1702. }
  1703. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1704. if (field_offset == -1) {
  1705. field_offset = result->cur_field;
  1706. result->cur_field++;
  1707. }
  1708. if (field_offset < 0 || field_offset >= result->num_fields) {
  1709. if (ZEND_NUM_ARGS() == 2) { /* field specified explicitly */
  1710. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Bad column offset");
  1711. }
  1712. RETURN_FALSE;
  1713. }
  1714. object_init(return_value);
  1715. add_property_string(return_value, "name", result->fields[field_offset].name, 1);
  1716. add_property_long(return_value, "max_length", result->fields[field_offset].max_length);
  1717. add_property_string(return_value, "column_source", result->fields[field_offset].column_source, 1);
  1718. add_property_long(return_value, "numeric", result->fields[field_offset].numeric);
  1719. add_property_string(return_value, "type", php_sybase_get_field_name(Z_TYPE(result->fields[field_offset])), 1);
  1720. }
  1721. /* }}} */
  1722. /* {{{ proto bool sybase_field_seek(resource result, int offset)
  1723. Set field offset */
  1724. PHP_FUNCTION(sybase_field_seek)
  1725. {
  1726. zval *sybase_result_index = NULL;
  1727. long field_offset;
  1728. sybase_result *result;
  1729. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &sybase_result_index, &field_offset) == FAILURE) {
  1730. return;
  1731. }
  1732. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1733. if (field_offset < 0 || field_offset >= result->num_fields) {
  1734. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Bad column offset");
  1735. RETURN_FALSE;
  1736. }
  1737. result->cur_field = field_offset;
  1738. RETURN_TRUE;
  1739. }
  1740. /* }}} */
  1741. /* {{{ proto string sybase_result(resource result, int row, mixed field)
  1742. Get result data */
  1743. PHP_FUNCTION(sybase_result)
  1744. {
  1745. zval *field;
  1746. zval *sybase_result_index = NULL;
  1747. long row;
  1748. int field_offset = 0;
  1749. sybase_result *result;
  1750. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &sybase_result_index, &row, &field) == FAILURE) {
  1751. return;
  1752. }
  1753. ZEND_FETCH_RESOURCE(result, sybase_result *, &sybase_result_index, -1, "Sybase result", le_result);
  1754. /* Unbuffered ? */
  1755. if (result->last_retcode != CS_END_DATA && result->last_retcode != CS_END_RESULTS && row >= result->num_rows) {
  1756. php_sybase_fetch_result_row(result, row TSRMLS_CC);
  1757. }
  1758. if (row < 0 || row >= result->num_rows) {
  1759. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Bad row offset (%ld)", row);
  1760. RETURN_FALSE;
  1761. }
  1762. switch(Z_TYPE_P(field)) {
  1763. case IS_STRING: {
  1764. int i;
  1765. for (i = 0; i < result->num_fields; i++) {
  1766. if (strcasecmp(result->fields[i].name, Z_STRVAL_P(field)) == 0) {
  1767. field_offset = i;
  1768. break;
  1769. }
  1770. }
  1771. if (i >= result->num_fields) { /* no match found */
  1772. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: %s field not found in result", Z_STRVAL_P(field));
  1773. RETURN_FALSE;
  1774. }
  1775. break;
  1776. }
  1777. default:
  1778. convert_to_long(field);
  1779. field_offset = Z_LVAL_P(field);
  1780. if (field_offset < 0 || field_offset >= result->num_fields) {
  1781. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Bad column offset specified");
  1782. RETURN_FALSE;
  1783. }
  1784. break;
  1785. }
  1786. *return_value = result->data[row][field_offset];
  1787. zval_copy_ctor(return_value);
  1788. }
  1789. /* }}} */
  1790. /* {{{ proto int sybase_affected_rows([resource link_id])
  1791. Get number of affected rows in last query */
  1792. PHP_FUNCTION(sybase_affected_rows)
  1793. {
  1794. zval *sybase_link_index = NULL;
  1795. sybase_link *sybase_ptr;
  1796. int id;
  1797. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &sybase_link_index) == FAILURE) {
  1798. return;
  1799. }
  1800. if (php_sybase_connection_id(sybase_link_index, &id TSRMLS_CC) == FAILURE) {
  1801. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: No connection");
  1802. RETURN_FALSE;
  1803. }
  1804. ZEND_FETCH_RESOURCE2(sybase_ptr, sybase_link *, &sybase_link_index, id, "Sybase-Link", le_link, le_plink);
  1805. Z_LVAL_P(return_value) = sybase_ptr->affected_rows;
  1806. Z_TYPE_P(return_value) = IS_LONG;
  1807. }
  1808. /* }}} */
  1809. PHP_MINFO_FUNCTION(sybase)
  1810. {
  1811. char buf[32];
  1812. php_info_print_table_start();
  1813. php_info_print_table_header(2, "Sybase_CT Support", "enabled" );
  1814. snprintf(buf, sizeof(buf), "%ld", SybCtG(num_persistent));
  1815. php_info_print_table_row(2, "Active Persistent Links", buf);
  1816. snprintf(buf, sizeof(buf), "%ld", SybCtG(num_links));
  1817. php_info_print_table_row(2, "Active Links", buf);
  1818. snprintf(buf, sizeof(buf), "%ld", SybCtG(min_server_severity));
  1819. php_info_print_table_row(2, "Min server severity", buf);
  1820. snprintf(buf, sizeof(buf), "%ld", SybCtG(min_client_severity));
  1821. php_info_print_table_row(2, "Min client severity", buf);
  1822. php_info_print_table_row(2, "Application Name", SybCtG(appname));
  1823. snprintf(buf, sizeof(buf), "%ld", SybCtG(deadlock_retry_count));
  1824. php_info_print_table_row(2, "Deadlock retry count", buf);
  1825. php_info_print_table_end();
  1826. DISPLAY_INI_ENTRIES();
  1827. }
  1828. /* {{{ proto void sybase_min_client_severity(int severity)
  1829. Sets minimum client severity */
  1830. PHP_FUNCTION(sybase_min_client_severity)
  1831. {
  1832. long severity;
  1833. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &severity) == FAILURE) {
  1834. return;
  1835. }
  1836. SybCtG(min_client_severity) = severity;
  1837. }
  1838. /* }}} */
  1839. /* {{{ proto void sybase_min_server_severity(int severity)
  1840. Sets minimum server severity */
  1841. PHP_FUNCTION(sybase_min_server_severity)
  1842. {
  1843. long severity;
  1844. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &severity) == FAILURE) {
  1845. return;
  1846. }
  1847. SybCtG(min_server_severity) = severity;
  1848. }
  1849. /* }}} */
  1850. /* {{{ proto void sybase_deadlock_retry_count(int retry_count)
  1851. Sets deadlock retry count */
  1852. PHP_FUNCTION(sybase_deadlock_retry_count)
  1853. {
  1854. long retry_count;
  1855. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &retry_count) == FAILURE) {
  1856. return;
  1857. }
  1858. SybCtG(deadlock_retry_count) = retry_count;
  1859. }
  1860. /* }}} */
  1861. /* {{{ proto bool sybase_set_message_handler(mixed error_func [, resource connection])
  1862. Set the error handler, to be called when a server message is raised.
  1863. If error_func is NULL the handler will be deleted */
  1864. PHP_FUNCTION(sybase_set_message_handler)
  1865. {
  1866. zend_fcall_info fci = empty_fcall_info;
  1867. zend_fcall_info_cache cache = empty_fcall_info_cache;
  1868. zval *sybase_link_index= NULL;
  1869. sybase_link *sybase_ptr;
  1870. zval **callback;
  1871. int id;
  1872. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!|r", &fci, &cache, &sybase_link_index) == FAILURE) {
  1873. return;
  1874. }
  1875. if (php_sybase_connection_id(sybase_link_index, &id TSRMLS_CC) == FAILURE) {
  1876. /* Doesn't matter if we're not connected yet, use default */
  1877. callback= &SybCtG(callback_name);
  1878. } else if (-1 == id) {
  1879. /* Connection-based message handler */
  1880. ZEND_FETCH_RESOURCE2(sybase_ptr, sybase_link *, &sybase_link_index, id, "Sybase-Link", le_link, le_plink);
  1881. callback= &sybase_ptr->callback_name;
  1882. } else {
  1883. /* Default message handler */
  1884. callback= &SybCtG(callback_name);
  1885. }
  1886. /* Clean old callback */
  1887. if (*callback) {
  1888. zval_ptr_dtor(callback);
  1889. *callback = NULL;
  1890. }
  1891. if (ZEND_FCI_INITIALIZED(fci)) {
  1892. ALLOC_ZVAL(*callback);
  1893. **callback = *fci.function_name;
  1894. INIT_PZVAL(*callback);
  1895. zval_copy_ctor(*callback);
  1896. } else {
  1897. callback= NULL;
  1898. }
  1899. RETURN_TRUE;
  1900. }
  1901. /* }}} */
  1902. #endif
  1903. /*
  1904. * Local variables:
  1905. * tab-width: 4
  1906. * c-basic-offset: 4
  1907. * End:
  1908. */