php_odbc.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860
  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: Stig Sæther Bakken <ssb@php.net> |
  16. | Andreas Karajannis <Andreas.Karajannis@gmd.de> |
  17. | Frank M. Kromann <frank@kromann.info> Support for DB/2 CLI |
  18. | Kevin N. Shallow <kshallow@tampabay.rr.com> Birdstep Support|
  19. | Daniel R. Kalowsky <kalowsky@php.net> |
  20. +----------------------------------------------------------------------+
  21. */
  22. /* $Id$ */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "php.h"
  27. #include "php_globals.h"
  28. #include "ext/standard/info.h"
  29. #include "ext/standard/php_string.h"
  30. #include "ext/standard/php_standard.h"
  31. #include "php_odbc.h"
  32. #include "php_odbc_includes.h"
  33. #include "php_globals.h"
  34. #if HAVE_UODBC
  35. #include <fcntl.h>
  36. #include "ext/standard/head.h"
  37. #include "php_ini.h"
  38. #ifdef PHP_WIN32
  39. #include <winsock2.h>
  40. #define ODBC_TYPE "Win32"
  41. #define PHP_ODBC_TYPE ODBC_TYPE
  42. #endif
  43. /*
  44. * not defined elsewhere
  45. */
  46. #ifndef TRUE
  47. #define TRUE 1
  48. #define FALSE 0
  49. #endif
  50. void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent);
  51. static int le_result, le_conn, le_pconn;
  52. #define SAFE_SQL_NTS(n) ((SQLSMALLINT) ((n)?(SQL_NTS):0))
  53. /* {{{ arginfo */
  54. ZEND_BEGIN_ARG_INFO(arginfo_odbc_close_all, 0)
  55. ZEND_END_ARG_INFO()
  56. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_binmode, 0, 0, 2)
  57. ZEND_ARG_INFO(0, result_id)
  58. ZEND_ARG_INFO(0, mode)
  59. ZEND_END_ARG_INFO()
  60. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_longreadlen, 0, 0, 2)
  61. ZEND_ARG_INFO(0, result_id)
  62. ZEND_ARG_INFO(0, length)
  63. ZEND_END_ARG_INFO()
  64. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_prepare, 0, 0, 2)
  65. ZEND_ARG_INFO(0, connection_id)
  66. ZEND_ARG_INFO(0, query)
  67. ZEND_END_ARG_INFO()
  68. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_execute, 0, 0, 1)
  69. ZEND_ARG_INFO(0, result_id)
  70. ZEND_ARG_INFO(0, parameters_array)
  71. ZEND_END_ARG_INFO()
  72. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_cursor, 0, 0, 1)
  73. ZEND_ARG_INFO(0, result_id)
  74. ZEND_END_ARG_INFO()
  75. #ifdef HAVE_SQLDATASOURCES
  76. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_data_source, 0, 0, 2)
  77. ZEND_ARG_INFO(0, connection_id)
  78. ZEND_ARG_INFO(0, fetch_type)
  79. ZEND_END_ARG_INFO()
  80. #endif
  81. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_exec, 0, 0, 2)
  82. ZEND_ARG_INFO(0, connection_id)
  83. ZEND_ARG_INFO(0, query)
  84. ZEND_ARG_INFO(0, flags)
  85. ZEND_END_ARG_INFO()
  86. #ifdef PHP_ODBC_HAVE_FETCH_HASH
  87. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_object, 0, 0, 1)
  88. ZEND_ARG_INFO(0, result)
  89. ZEND_ARG_INFO(0, rownumber)
  90. ZEND_END_ARG_INFO()
  91. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_array, 0, 0, 1)
  92. ZEND_ARG_INFO(0, result)
  93. ZEND_ARG_INFO(0, rownumber)
  94. ZEND_END_ARG_INFO()
  95. #endif
  96. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_into, 0, 0, 2)
  97. ZEND_ARG_INFO(0, result_id)
  98. ZEND_ARG_INFO(1, result_array)
  99. ZEND_ARG_INFO(0, rownumber)
  100. ZEND_END_ARG_INFO()
  101. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_row, 0, 0, 1)
  102. ZEND_ARG_INFO(0, result_id)
  103. ZEND_ARG_INFO(0, row_number)
  104. ZEND_END_ARG_INFO()
  105. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_result, 0, 0, 2)
  106. ZEND_ARG_INFO(0, result_id)
  107. ZEND_ARG_INFO(0, field)
  108. ZEND_END_ARG_INFO()
  109. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_result_all, 0, 0, 1)
  110. ZEND_ARG_INFO(0, result_id)
  111. ZEND_ARG_INFO(0, format)
  112. ZEND_END_ARG_INFO()
  113. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_free_result, 0, 0, 1)
  114. ZEND_ARG_INFO(0, result_id)
  115. ZEND_END_ARG_INFO()
  116. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_connect, 0, 0, 3)
  117. ZEND_ARG_INFO(0, dsn)
  118. ZEND_ARG_INFO(0, user)
  119. ZEND_ARG_INFO(0, password)
  120. ZEND_ARG_INFO(0, cursor_option)
  121. ZEND_END_ARG_INFO()
  122. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_pconnect, 0, 0, 3)
  123. ZEND_ARG_INFO(0, dsn)
  124. ZEND_ARG_INFO(0, user)
  125. ZEND_ARG_INFO(0, password)
  126. ZEND_ARG_INFO(0, cursor_option)
  127. ZEND_END_ARG_INFO()
  128. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_close, 0, 0, 1)
  129. ZEND_ARG_INFO(0, connection_id)
  130. ZEND_END_ARG_INFO()
  131. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_num_rows, 0, 0, 1)
  132. ZEND_ARG_INFO(0, result_id)
  133. ZEND_END_ARG_INFO()
  134. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
  135. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_next_result, 0, 0, 1)
  136. ZEND_ARG_INFO(0, result_id)
  137. ZEND_END_ARG_INFO()
  138. #endif
  139. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_num_fields, 0, 0, 1)
  140. ZEND_ARG_INFO(0, result_id)
  141. ZEND_END_ARG_INFO()
  142. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_name, 0, 0, 2)
  143. ZEND_ARG_INFO(0, result_id)
  144. ZEND_ARG_INFO(0, field_number)
  145. ZEND_END_ARG_INFO()
  146. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_type, 0, 0, 2)
  147. ZEND_ARG_INFO(0, result_id)
  148. ZEND_ARG_INFO(0, field_number)
  149. ZEND_END_ARG_INFO()
  150. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_len, 0, 0, 2)
  151. ZEND_ARG_INFO(0, result_id)
  152. ZEND_ARG_INFO(0, field_number)
  153. ZEND_END_ARG_INFO()
  154. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_scale, 0, 0, 2)
  155. ZEND_ARG_INFO(0, result_id)
  156. ZEND_ARG_INFO(0, field_number)
  157. ZEND_END_ARG_INFO()
  158. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_num, 0, 0, 2)
  159. ZEND_ARG_INFO(0, result_id)
  160. ZEND_ARG_INFO(0, field_name)
  161. ZEND_END_ARG_INFO()
  162. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_autocommit, 0, 0, 1)
  163. ZEND_ARG_INFO(0, connection_id)
  164. ZEND_ARG_INFO(0, onoff)
  165. ZEND_END_ARG_INFO()
  166. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_commit, 0, 0, 1)
  167. ZEND_ARG_INFO(0, connection_id)
  168. ZEND_END_ARG_INFO()
  169. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_rollback, 0, 0, 1)
  170. ZEND_ARG_INFO(0, connection_id)
  171. ZEND_END_ARG_INFO()
  172. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_error, 0, 0, 0)
  173. ZEND_ARG_INFO(0, connection_id)
  174. ZEND_END_ARG_INFO()
  175. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_errormsg, 0, 0, 0)
  176. ZEND_ARG_INFO(0, connection_id)
  177. ZEND_END_ARG_INFO()
  178. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_setoption, 0, 0, 4)
  179. ZEND_ARG_INFO(0, conn_id)
  180. ZEND_ARG_INFO(0, which)
  181. ZEND_ARG_INFO(0, option)
  182. ZEND_ARG_INFO(0, value)
  183. ZEND_END_ARG_INFO()
  184. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_tables, 0, 0, 1)
  185. ZEND_ARG_INFO(0, connection_id)
  186. ZEND_ARG_INFO(0, qualifier)
  187. ZEND_ARG_INFO(0, owner)
  188. ZEND_ARG_INFO(0, name)
  189. ZEND_ARG_INFO(0, table_types)
  190. ZEND_END_ARG_INFO()
  191. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_columns, 0, 0, 1)
  192. ZEND_ARG_INFO(0, connection_id)
  193. ZEND_ARG_INFO(0, qualifier)
  194. ZEND_ARG_INFO(0, owner)
  195. ZEND_ARG_INFO(0, table_name)
  196. ZEND_ARG_INFO(0, column_name)
  197. ZEND_END_ARG_INFO()
  198. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_gettypeinfo, 0, 0, 1)
  199. ZEND_ARG_INFO(0, connection_id)
  200. ZEND_ARG_INFO(0, data_type)
  201. ZEND_END_ARG_INFO()
  202. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_primarykeys, 0, 0, 4)
  203. ZEND_ARG_INFO(0, connection_id)
  204. ZEND_ARG_INFO(0, qualifier)
  205. ZEND_ARG_INFO(0, owner)
  206. ZEND_ARG_INFO(0, table)
  207. ZEND_END_ARG_INFO()
  208. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
  209. #if !defined(HAVE_BIRDSTEP)
  210. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_procedurecolumns, 0, 0, 1)
  211. ZEND_ARG_INFO(0, connection_id)
  212. ZEND_ARG_INFO(0, qualifier)
  213. ZEND_ARG_INFO(0, owner)
  214. ZEND_ARG_INFO(0, proc)
  215. ZEND_ARG_INFO(0, column)
  216. ZEND_END_ARG_INFO()
  217. #endif
  218. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_procedures, 0, 0, 1)
  219. ZEND_ARG_INFO(0, connection_id)
  220. ZEND_ARG_INFO(0, qualifier)
  221. ZEND_ARG_INFO(0, owner)
  222. ZEND_ARG_INFO(0, name)
  223. ZEND_END_ARG_INFO()
  224. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_foreignkeys, 0, 0, 7)
  225. ZEND_ARG_INFO(0, connection_id)
  226. ZEND_ARG_INFO(0, pk_qualifier)
  227. ZEND_ARG_INFO(0, pk_owner)
  228. ZEND_ARG_INFO(0, pk_table)
  229. ZEND_ARG_INFO(0, fk_qualifier)
  230. ZEND_ARG_INFO(0, fk_owner)
  231. ZEND_ARG_INFO(0, fk_table)
  232. ZEND_END_ARG_INFO()
  233. #endif
  234. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_specialcolumns, 0, 0, 7)
  235. ZEND_ARG_INFO(0, connection_id)
  236. ZEND_ARG_INFO(0, type)
  237. ZEND_ARG_INFO(0, qualifier)
  238. ZEND_ARG_INFO(0, owner)
  239. ZEND_ARG_INFO(0, table)
  240. ZEND_ARG_INFO(0, scope)
  241. ZEND_ARG_INFO(0, nullable)
  242. ZEND_END_ARG_INFO()
  243. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_statistics, 0, 0, 6)
  244. ZEND_ARG_INFO(0, connection_id)
  245. ZEND_ARG_INFO(0, qualifier)
  246. ZEND_ARG_INFO(0, owner)
  247. ZEND_ARG_INFO(0, name)
  248. ZEND_ARG_INFO(0, unique)
  249. ZEND_ARG_INFO(0, accuracy)
  250. ZEND_END_ARG_INFO()
  251. #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) &&!defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)
  252. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_tableprivileges, 0, 0, 4)
  253. ZEND_ARG_INFO(0, connection_id)
  254. ZEND_ARG_INFO(0, qualifier)
  255. ZEND_ARG_INFO(0, owner)
  256. ZEND_ARG_INFO(0, name)
  257. ZEND_END_ARG_INFO()
  258. ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_columnprivileges, 0, 0, 5)
  259. ZEND_ARG_INFO(0, connection_id)
  260. ZEND_ARG_INFO(0, catalog)
  261. ZEND_ARG_INFO(0, schema)
  262. ZEND_ARG_INFO(0, table)
  263. ZEND_ARG_INFO(0, column)
  264. ZEND_END_ARG_INFO()
  265. #endif
  266. /* }}} */
  267. /* {{{ odbc_functions[]
  268. */
  269. const zend_function_entry odbc_functions[] = {
  270. PHP_FE(odbc_autocommit, arginfo_odbc_autocommit)
  271. PHP_FE(odbc_binmode, arginfo_odbc_binmode)
  272. PHP_FE(odbc_close, arginfo_odbc_close)
  273. PHP_FE(odbc_close_all, arginfo_odbc_close_all)
  274. PHP_FE(odbc_columns, arginfo_odbc_columns)
  275. PHP_FE(odbc_commit, arginfo_odbc_commit)
  276. PHP_FE(odbc_connect, arginfo_odbc_connect)
  277. PHP_FE(odbc_cursor, arginfo_odbc_cursor)
  278. #ifdef HAVE_SQLDATASOURCES
  279. PHP_FE(odbc_data_source, arginfo_odbc_data_source)
  280. #endif
  281. PHP_FE(odbc_execute, arginfo_odbc_execute)
  282. PHP_FE(odbc_error, arginfo_odbc_error)
  283. PHP_FE(odbc_errormsg, arginfo_odbc_errormsg)
  284. PHP_FE(odbc_exec, arginfo_odbc_exec)
  285. #ifdef PHP_ODBC_HAVE_FETCH_HASH
  286. PHP_FE(odbc_fetch_array, arginfo_odbc_fetch_array)
  287. PHP_FE(odbc_fetch_object, arginfo_odbc_fetch_object)
  288. #endif
  289. PHP_FE(odbc_fetch_row, arginfo_odbc_fetch_row)
  290. PHP_FE(odbc_fetch_into, arginfo_odbc_fetch_into)
  291. PHP_FE(odbc_field_len, arginfo_odbc_field_len)
  292. PHP_FE(odbc_field_scale, arginfo_odbc_field_scale)
  293. PHP_FE(odbc_field_name, arginfo_odbc_field_name)
  294. PHP_FE(odbc_field_type, arginfo_odbc_field_type)
  295. PHP_FE(odbc_field_num, arginfo_odbc_field_num)
  296. PHP_FE(odbc_free_result, arginfo_odbc_free_result)
  297. PHP_FE(odbc_gettypeinfo, arginfo_odbc_gettypeinfo)
  298. PHP_FE(odbc_longreadlen, arginfo_odbc_longreadlen)
  299. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
  300. PHP_FE(odbc_next_result, arginfo_odbc_next_result)
  301. #endif
  302. PHP_FE(odbc_num_fields, arginfo_odbc_num_fields)
  303. PHP_FE(odbc_num_rows, arginfo_odbc_num_rows)
  304. PHP_FE(odbc_pconnect, arginfo_odbc_pconnect)
  305. PHP_FE(odbc_prepare, arginfo_odbc_prepare)
  306. PHP_FE(odbc_result, arginfo_odbc_result)
  307. PHP_FE(odbc_result_all, arginfo_odbc_result_all)
  308. PHP_FE(odbc_rollback, arginfo_odbc_rollback)
  309. PHP_FE(odbc_setoption, arginfo_odbc_setoption)
  310. PHP_FE(odbc_specialcolumns, arginfo_odbc_specialcolumns)
  311. PHP_FE(odbc_statistics, arginfo_odbc_statistics)
  312. PHP_FE(odbc_tables, arginfo_odbc_tables)
  313. PHP_FE(odbc_primarykeys, arginfo_odbc_primarykeys)
  314. #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) &&!defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP) /* not supported now */
  315. PHP_FE(odbc_columnprivileges, arginfo_odbc_columnprivileges)
  316. PHP_FE(odbc_tableprivileges, arginfo_odbc_tableprivileges)
  317. #endif
  318. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) /* not supported */
  319. PHP_FE(odbc_foreignkeys, arginfo_odbc_foreignkeys)
  320. PHP_FE(odbc_procedures, arginfo_odbc_procedures)
  321. #if !defined(HAVE_BIRDSTEP)
  322. PHP_FE(odbc_procedurecolumns, arginfo_odbc_procedurecolumns)
  323. #endif
  324. #endif
  325. PHP_FALIAS(odbc_do, odbc_exec, arginfo_odbc_exec)
  326. PHP_FALIAS(odbc_field_precision, odbc_field_len, arginfo_odbc_field_len)
  327. PHP_FE_END
  328. };
  329. /* }}} */
  330. ZEND_DECLARE_MODULE_GLOBALS(odbc)
  331. static PHP_GINIT_FUNCTION(odbc);
  332. /* {{{ odbc_module_entry
  333. */
  334. zend_module_entry odbc_module_entry = {
  335. STANDARD_MODULE_HEADER,
  336. "odbc",
  337. odbc_functions,
  338. PHP_MINIT(odbc),
  339. PHP_MSHUTDOWN(odbc),
  340. PHP_RINIT(odbc),
  341. PHP_RSHUTDOWN(odbc),
  342. PHP_MINFO(odbc),
  343. "1.0",
  344. PHP_MODULE_GLOBALS(odbc),
  345. PHP_GINIT(odbc),
  346. NULL,
  347. NULL,
  348. STANDARD_MODULE_PROPERTIES_EX
  349. };
  350. /* }}} */
  351. #ifdef COMPILE_DL_ODBC
  352. ZEND_GET_MODULE(odbc)
  353. #endif
  354. /* {{{ _free_odbc_result
  355. */
  356. static void _free_odbc_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  357. {
  358. odbc_result *res = (odbc_result *)rsrc->ptr;
  359. int i;
  360. RETCODE rc;
  361. if (res) {
  362. if (res->values) {
  363. for(i = 0; i < res->numcols; i++) {
  364. if (res->values[i].value)
  365. efree(res->values[i].value);
  366. }
  367. efree(res->values);
  368. res->values = NULL;
  369. }
  370. /* If aborted via timer expiration, don't try to call any unixODBC function */
  371. if (res->stmt && !(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
  372. #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
  373. SQLTransact(res->conn_ptr->henv, res->conn_ptr->hdbc,
  374. (SQLUSMALLINT) SQL_COMMIT);
  375. #endif
  376. rc = SQLFreeStmt(res->stmt,SQL_DROP);
  377. /* We don't want the connection to be closed after the last statment has been closed
  378. * Connections will be closed on shutdown
  379. * zend_list_delete(res->conn_ptr->id);
  380. */
  381. }
  382. if (res->param_info) {
  383. efree(res->param_info);
  384. }
  385. efree(res);
  386. }
  387. }
  388. /* }}} */
  389. /* {{{ safe_odbc_disconnect
  390. * disconnect, and if it fails, then issue a rollback for any pending transaction (lurcher)
  391. */
  392. static void safe_odbc_disconnect( void *handle )
  393. {
  394. int ret;
  395. ret = SQLDisconnect( handle );
  396. if ( ret == SQL_ERROR )
  397. {
  398. SQLTransact( NULL, handle, SQL_ROLLBACK );
  399. SQLDisconnect( handle );
  400. }
  401. }
  402. /* }}} */
  403. /* {{{ _close_odbc_conn
  404. */
  405. static void _close_odbc_conn(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  406. {
  407. int i, nument, type;
  408. void *ptr;
  409. odbc_result *res;
  410. odbc_connection *conn = (odbc_connection *)rsrc->ptr;
  411. nument = zend_hash_next_free_element(&EG(regular_list));
  412. for(i = 1; i < nument; i++) {
  413. ptr = zend_list_find(i, &type);
  414. if (ptr && (type == le_result)) {
  415. res = (odbc_result *)ptr;
  416. if (res->conn_ptr == conn) {
  417. zend_list_delete(i);
  418. }
  419. }
  420. }
  421. /* If aborted via timer expiration, don't try to call any unixODBC function */
  422. if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
  423. safe_odbc_disconnect(conn->hdbc);
  424. SQLFreeConnect(conn->hdbc);
  425. SQLFreeEnv(conn->henv);
  426. }
  427. efree(conn);
  428. ODBCG(num_links)--;
  429. }
  430. /* }}} */
  431. /* {{{ void _close_odbc_pconn
  432. */
  433. static void _close_odbc_pconn(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  434. {
  435. int i, nument, type;
  436. void *ptr;
  437. odbc_result *res;
  438. odbc_connection *conn = (odbc_connection *)rsrc->ptr;
  439. nument = zend_hash_next_free_element(&EG(persistent_list));
  440. for(i = 1; i < nument; i++) {
  441. ptr = zend_list_find(i, &type);
  442. if (ptr && (type == le_result)) {
  443. res = (odbc_result *)ptr;
  444. if (res->conn_ptr == conn) {
  445. zend_list_delete(i);
  446. }
  447. }
  448. }
  449. /* If aborted via timer expiration, don't try to call any unixODBC function */
  450. if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
  451. safe_odbc_disconnect(conn->hdbc);
  452. SQLFreeConnect(conn->hdbc);
  453. SQLFreeEnv(conn->henv);
  454. }
  455. free(conn);
  456. ODBCG(num_links)--;
  457. ODBCG(num_persistent)--;
  458. }
  459. /* }}} */
  460. /* {{{ PHP_INI_DISP(display_link_nums)
  461. */
  462. static PHP_INI_DISP(display_link_nums)
  463. {
  464. char *value;
  465. TSRMLS_FETCH();
  466. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  467. value = ini_entry->orig_value;
  468. } else if (ini_entry->value) {
  469. value = ini_entry->value;
  470. } else {
  471. value = NULL;
  472. }
  473. if (value) {
  474. if (atoi(value) == -1) {
  475. PUTS("Unlimited");
  476. } else {
  477. php_printf("%s", value);
  478. }
  479. }
  480. }
  481. /* }}} */
  482. /* {{{ PHP_INI_DISP(display_defPW)
  483. */
  484. static PHP_INI_DISP(display_defPW)
  485. {
  486. char *value;
  487. TSRMLS_FETCH();
  488. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  489. value = ini_entry->orig_value;
  490. } else if (ini_entry->value) {
  491. value = ini_entry->value;
  492. } else {
  493. value = NULL;
  494. }
  495. if (value) {
  496. #if PHP_DEBUG
  497. php_printf("%s", value);
  498. #else
  499. PUTS("********");
  500. #endif
  501. } else {
  502. if (PG(html_errors)) {
  503. PUTS("<i>no value</i>");
  504. } else {
  505. PUTS("no value");
  506. }
  507. }
  508. }
  509. /* }}} */
  510. /* {{{ PHP_INI_DISP(display_binmode)
  511. */
  512. static PHP_INI_DISP(display_binmode)
  513. {
  514. char *value;
  515. TSRMLS_FETCH();
  516. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  517. value = ini_entry->orig_value;
  518. } else if (ini_entry->value) {
  519. value = ini_entry->value;
  520. } else {
  521. value = NULL;
  522. }
  523. if (value) {
  524. switch(atoi(value)) {
  525. case 0:
  526. PUTS("passthru");
  527. break;
  528. case 1:
  529. PUTS("return as is");
  530. break;
  531. case 2:
  532. PUTS("return as char");
  533. break;
  534. }
  535. }
  536. }
  537. /* }}} */
  538. /* {{{ PHP_INI_DISP(display_lrl)
  539. */
  540. static PHP_INI_DISP(display_lrl)
  541. {
  542. char *value;
  543. TSRMLS_FETCH();
  544. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  545. value = ini_entry->orig_value;
  546. } else if (ini_entry->value) {
  547. value = ini_entry->value;
  548. } else {
  549. value = NULL;
  550. }
  551. if (value) {
  552. if (atoi(value) <= 0) {
  553. PUTS("Passthru");
  554. } else {
  555. php_printf("return up to %s bytes", value);
  556. }
  557. }
  558. }
  559. /* }}} */
  560. /* {{{ PHP_INI_DISP(display_cursortype)
  561. */
  562. static PHP_INI_DISP(display_cursortype)
  563. {
  564. char *value;
  565. TSRMLS_FETCH();
  566. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  567. value = ini_entry->orig_value;
  568. } else if (ini_entry->value) {
  569. value = ini_entry->value;
  570. } else {
  571. value = NULL;
  572. }
  573. if (value) {
  574. switch (atoi (value))
  575. {
  576. case SQL_CURSOR_FORWARD_ONLY:
  577. PUTS ("Forward Only cursor");
  578. break;
  579. case SQL_CURSOR_STATIC:
  580. PUTS ("Static cursor");
  581. break;
  582. case SQL_CURSOR_KEYSET_DRIVEN:
  583. PUTS ("Keyset driven cursor");
  584. break;
  585. case SQL_CURSOR_DYNAMIC:
  586. PUTS ("Dynamic cursor");
  587. break;
  588. default:
  589. php_printf("Unknown cursor model %s", value);
  590. break;
  591. }
  592. }
  593. }
  594. /* }}} */
  595. /* {{{ PHP_INI_BEGIN
  596. */
  597. PHP_INI_BEGIN()
  598. STD_PHP_INI_BOOLEAN("odbc.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong,
  599. allow_persistent, zend_odbc_globals, odbc_globals)
  600. STD_PHP_INI_ENTRY_EX("odbc.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong,
  601. max_persistent, zend_odbc_globals, odbc_globals, display_link_nums)
  602. STD_PHP_INI_ENTRY_EX("odbc.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong,
  603. max_links, zend_odbc_globals, odbc_globals, display_link_nums)
  604. STD_PHP_INI_ENTRY("odbc.default_db", NULL, PHP_INI_ALL, OnUpdateString,
  605. defDB, zend_odbc_globals, odbc_globals)
  606. STD_PHP_INI_ENTRY("odbc.default_user", NULL, PHP_INI_ALL, OnUpdateString,
  607. defUser, zend_odbc_globals, odbc_globals)
  608. STD_PHP_INI_ENTRY_EX("odbc.default_pw", NULL, PHP_INI_ALL, OnUpdateString,
  609. defPW, zend_odbc_globals, odbc_globals, display_defPW)
  610. STD_PHP_INI_ENTRY_EX("odbc.defaultlrl", "4096", PHP_INI_ALL, OnUpdateLong,
  611. defaultlrl, zend_odbc_globals, odbc_globals, display_lrl)
  612. STD_PHP_INI_ENTRY_EX("odbc.defaultbinmode", "1", PHP_INI_ALL, OnUpdateLong,
  613. defaultbinmode, zend_odbc_globals, odbc_globals, display_binmode)
  614. STD_PHP_INI_BOOLEAN("odbc.check_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong,
  615. check_persistent, zend_odbc_globals, odbc_globals)
  616. STD_PHP_INI_ENTRY_EX("odbc.default_cursortype", "3", PHP_INI_ALL, OnUpdateLong,
  617. default_cursortype, zend_odbc_globals, odbc_globals, display_cursortype)
  618. PHP_INI_END()
  619. /* }}} */
  620. static PHP_GINIT_FUNCTION(odbc)
  621. {
  622. odbc_globals->num_persistent = 0;
  623. }
  624. /* {{{ PHP_MINIT_FUNCTION */
  625. PHP_MINIT_FUNCTION(odbc)
  626. {
  627. #ifdef SQLANY_BUG
  628. ODBC_SQL_CONN_T foobar;
  629. RETCODE rc;
  630. #endif
  631. REGISTER_INI_ENTRIES();
  632. le_result = zend_register_list_destructors_ex(_free_odbc_result, NULL, "odbc result", module_number);
  633. le_conn = zend_register_list_destructors_ex(_close_odbc_conn, NULL, "odbc link", module_number);
  634. le_pconn = zend_register_list_destructors_ex(NULL, _close_odbc_pconn, "odbc link persistent", module_number);
  635. Z_TYPE(odbc_module_entry) = type;
  636. REGISTER_STRING_CONSTANT("ODBC_TYPE", PHP_ODBC_TYPE, CONST_CS | CONST_PERSISTENT);
  637. REGISTER_LONG_CONSTANT("ODBC_BINMODE_PASSTHRU", 0, CONST_CS | CONST_PERSISTENT);
  638. REGISTER_LONG_CONSTANT("ODBC_BINMODE_RETURN", 1, CONST_CS | CONST_PERSISTENT);
  639. REGISTER_LONG_CONSTANT("ODBC_BINMODE_CONVERT", 2, CONST_CS | CONST_PERSISTENT);
  640. /* Define Constants for options
  641. these Constants are defined in <sqlext.h>
  642. */
  643. REGISTER_LONG_CONSTANT("SQL_ODBC_CURSORS", SQL_ODBC_CURSORS, CONST_PERSISTENT | CONST_CS);
  644. REGISTER_LONG_CONSTANT("SQL_CUR_USE_DRIVER", SQL_CUR_USE_DRIVER, CONST_PERSISTENT | CONST_CS);
  645. REGISTER_LONG_CONSTANT("SQL_CUR_USE_IF_NEEDED", SQL_CUR_USE_IF_NEEDED, CONST_PERSISTENT | CONST_CS);
  646. REGISTER_LONG_CONSTANT("SQL_CUR_USE_ODBC", SQL_CUR_USE_ODBC, CONST_PERSISTENT | CONST_CS);
  647. REGISTER_LONG_CONSTANT("SQL_CONCURRENCY", SQL_CONCURRENCY, CONST_PERSISTENT | CONST_CS);
  648. REGISTER_LONG_CONSTANT("SQL_CONCUR_READ_ONLY", SQL_CONCUR_READ_ONLY, CONST_PERSISTENT | CONST_CS);
  649. REGISTER_LONG_CONSTANT("SQL_CONCUR_LOCK", SQL_CONCUR_LOCK, CONST_PERSISTENT | CONST_CS);
  650. REGISTER_LONG_CONSTANT("SQL_CONCUR_ROWVER", SQL_CONCUR_ROWVER, CONST_PERSISTENT | CONST_CS);
  651. REGISTER_LONG_CONSTANT("SQL_CONCUR_VALUES", SQL_CONCUR_VALUES, CONST_PERSISTENT | CONST_CS);
  652. REGISTER_LONG_CONSTANT("SQL_CURSOR_TYPE", SQL_CURSOR_TYPE, CONST_PERSISTENT | CONST_CS);
  653. REGISTER_LONG_CONSTANT("SQL_CURSOR_FORWARD_ONLY", SQL_CURSOR_FORWARD_ONLY, CONST_PERSISTENT | CONST_CS);
  654. REGISTER_LONG_CONSTANT("SQL_CURSOR_KEYSET_DRIVEN", SQL_CURSOR_KEYSET_DRIVEN, CONST_PERSISTENT | CONST_CS);
  655. REGISTER_LONG_CONSTANT("SQL_CURSOR_DYNAMIC", SQL_CURSOR_DYNAMIC, CONST_PERSISTENT | CONST_CS);
  656. REGISTER_LONG_CONSTANT("SQL_CURSOR_STATIC", SQL_CURSOR_STATIC, CONST_PERSISTENT | CONST_CS);
  657. REGISTER_LONG_CONSTANT("SQL_KEYSET_SIZE", SQL_KEYSET_SIZE, CONST_PERSISTENT | CONST_CS);
  658. /* these are for the Data Source type */
  659. REGISTER_LONG_CONSTANT("SQL_FETCH_FIRST", SQL_FETCH_FIRST, CONST_PERSISTENT | CONST_CS);
  660. REGISTER_LONG_CONSTANT("SQL_FETCH_NEXT", SQL_FETCH_NEXT, CONST_PERSISTENT | CONST_CS);
  661. /*
  662. * register the standard data types
  663. */
  664. REGISTER_LONG_CONSTANT("SQL_CHAR", SQL_CHAR, CONST_PERSISTENT | CONST_CS);
  665. REGISTER_LONG_CONSTANT("SQL_VARCHAR", SQL_VARCHAR, CONST_PERSISTENT | CONST_CS);
  666. REGISTER_LONG_CONSTANT("SQL_LONGVARCHAR", SQL_LONGVARCHAR, CONST_PERSISTENT | CONST_CS);
  667. REGISTER_LONG_CONSTANT("SQL_DECIMAL", SQL_DECIMAL, CONST_PERSISTENT | CONST_CS);
  668. REGISTER_LONG_CONSTANT("SQL_NUMERIC", SQL_NUMERIC, CONST_PERSISTENT | CONST_CS);
  669. REGISTER_LONG_CONSTANT("SQL_BIT", SQL_BIT, CONST_PERSISTENT | CONST_CS);
  670. REGISTER_LONG_CONSTANT("SQL_TINYINT", SQL_TINYINT, CONST_PERSISTENT | CONST_CS);
  671. REGISTER_LONG_CONSTANT("SQL_SMALLINT", SQL_SMALLINT, CONST_PERSISTENT | CONST_CS);
  672. REGISTER_LONG_CONSTANT("SQL_INTEGER", SQL_INTEGER, CONST_PERSISTENT | CONST_CS);
  673. REGISTER_LONG_CONSTANT("SQL_BIGINT", SQL_BIGINT, CONST_PERSISTENT | CONST_CS);
  674. REGISTER_LONG_CONSTANT("SQL_REAL", SQL_REAL, CONST_PERSISTENT | CONST_CS);
  675. REGISTER_LONG_CONSTANT("SQL_FLOAT", SQL_FLOAT, CONST_PERSISTENT | CONST_CS);
  676. REGISTER_LONG_CONSTANT("SQL_DOUBLE", SQL_DOUBLE, CONST_PERSISTENT | CONST_CS);
  677. REGISTER_LONG_CONSTANT("SQL_BINARY", SQL_BINARY, CONST_PERSISTENT | CONST_CS);
  678. REGISTER_LONG_CONSTANT("SQL_VARBINARY", SQL_VARBINARY, CONST_PERSISTENT | CONST_CS);
  679. REGISTER_LONG_CONSTANT("SQL_LONGVARBINARY", SQL_LONGVARBINARY, CONST_PERSISTENT | CONST_CS);
  680. REGISTER_LONG_CONSTANT("SQL_DATE", SQL_DATE, CONST_PERSISTENT | CONST_CS);
  681. REGISTER_LONG_CONSTANT("SQL_TIME", SQL_TIME, CONST_PERSISTENT | CONST_CS);
  682. REGISTER_LONG_CONSTANT("SQL_TIMESTAMP", SQL_TIMESTAMP, CONST_PERSISTENT | CONST_CS);
  683. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  684. REGISTER_LONG_CONSTANT("SQL_TYPE_DATE", SQL_TYPE_DATE, CONST_PERSISTENT | CONST_CS);
  685. REGISTER_LONG_CONSTANT("SQL_TYPE_TIME", SQL_TYPE_TIME, CONST_PERSISTENT | CONST_CS);
  686. REGISTER_LONG_CONSTANT("SQL_TYPE_TIMESTAMP", SQL_TYPE_TIMESTAMP, CONST_PERSISTENT | CONST_CS);
  687. REGISTER_LONG_CONSTANT("SQL_WCHAR", SQL_WCHAR, CONST_PERSISTENT | CONST_CS);
  688. REGISTER_LONG_CONSTANT("SQL_WVARCHAR", SQL_WVARCHAR, CONST_PERSISTENT | CONST_CS);
  689. REGISTER_LONG_CONSTANT("SQL_WLONGVARCHAR", SQL_WLONGVARCHAR, CONST_PERSISTENT | CONST_CS);
  690. /*
  691. * SQLSpecialColumns values
  692. */
  693. REGISTER_LONG_CONSTANT("SQL_BEST_ROWID", SQL_BEST_ROWID, CONST_PERSISTENT | CONST_CS);
  694. REGISTER_LONG_CONSTANT("SQL_ROWVER", SQL_ROWVER, CONST_PERSISTENT | CONST_CS);
  695. REGISTER_LONG_CONSTANT("SQL_SCOPE_CURROW", SQL_SCOPE_CURROW, CONST_PERSISTENT | CONST_CS);
  696. REGISTER_LONG_CONSTANT("SQL_SCOPE_TRANSACTION", SQL_SCOPE_TRANSACTION, CONST_PERSISTENT | CONST_CS);
  697. REGISTER_LONG_CONSTANT("SQL_SCOPE_SESSION", SQL_SCOPE_SESSION, CONST_PERSISTENT | CONST_CS);
  698. REGISTER_LONG_CONSTANT("SQL_NO_NULLS", SQL_NO_NULLS, CONST_PERSISTENT | CONST_CS);
  699. REGISTER_LONG_CONSTANT("SQL_NULLABLE", SQL_NULLABLE, CONST_PERSISTENT | CONST_CS);
  700. /*
  701. * SQLStatistics values
  702. */
  703. REGISTER_LONG_CONSTANT("SQL_INDEX_UNIQUE", SQL_INDEX_UNIQUE, CONST_PERSISTENT | CONST_CS);
  704. REGISTER_LONG_CONSTANT("SQL_INDEX_ALL", SQL_INDEX_ALL, CONST_PERSISTENT | CONST_CS);
  705. REGISTER_LONG_CONSTANT("SQL_ENSURE", SQL_ENSURE, CONST_PERSISTENT | CONST_CS);
  706. REGISTER_LONG_CONSTANT("SQL_QUICK", SQL_QUICK, CONST_PERSISTENT | CONST_CS);
  707. #endif
  708. #if defined(HAVE_IBMDB2) && defined(_AIX)
  709. /* atexit() handler in the DB2/AIX library segfaults in PHP CLI */
  710. /* DB2NOEXITLIST env variable prevents DB2 from invoking atexit() */
  711. putenv("DB2NOEXITLIST=TRUE");
  712. #endif
  713. return SUCCESS;
  714. }
  715. /* }}} */
  716. /* {{{ PHP_RINIT_FUNCTION */
  717. PHP_RINIT_FUNCTION(odbc)
  718. {
  719. ODBCG(defConn) = -1;
  720. ODBCG(num_links) = ODBCG(num_persistent);
  721. memset(ODBCG(laststate), '\0', 6);
  722. memset(ODBCG(lasterrormsg), '\0', SQL_MAX_MESSAGE_LENGTH);
  723. return SUCCESS;
  724. }
  725. /* }}} */
  726. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  727. PHP_RSHUTDOWN_FUNCTION(odbc)
  728. {
  729. return SUCCESS;
  730. }
  731. /* }}} */
  732. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  733. PHP_MSHUTDOWN_FUNCTION(odbc)
  734. {
  735. UNREGISTER_INI_ENTRIES();
  736. return SUCCESS;
  737. }
  738. /* }}} */
  739. /* {{{ PHP_MINFO_FUNCTION */
  740. PHP_MINFO_FUNCTION(odbc)
  741. {
  742. char buf[32];
  743. php_info_print_table_start();
  744. php_info_print_table_header(2, "ODBC Support", "enabled");
  745. snprintf(buf, sizeof(buf), "%ld", ODBCG(num_persistent));
  746. php_info_print_table_row(2, "Active Persistent Links", buf);
  747. snprintf(buf, sizeof(buf), "%ld", ODBCG(num_links));
  748. php_info_print_table_row(2, "Active Links", buf);
  749. php_info_print_table_row(2, "ODBC library", PHP_ODBC_TYPE);
  750. #ifndef PHP_WIN32
  751. php_info_print_table_row(2, "ODBC_INCLUDE", PHP_ODBC_INCLUDE);
  752. php_info_print_table_row(2, "ODBC_LFLAGS", PHP_ODBC_LFLAGS);
  753. php_info_print_table_row(2, "ODBC_LIBS", PHP_ODBC_LIBS);
  754. #endif
  755. php_info_print_table_end();
  756. DISPLAY_INI_ENTRIES();
  757. }
  758. /* }}} */
  759. /* {{{ odbc_sql_error */
  760. void odbc_sql_error(ODBC_SQL_ERROR_PARAMS)
  761. {
  762. char state[6];
  763. SQLINTEGER error; /* Not used */
  764. char errormsg[SQL_MAX_MESSAGE_LENGTH];
  765. SQLSMALLINT errormsgsize; /* Not used */
  766. RETCODE rc;
  767. ODBC_SQL_ENV_T henv;
  768. ODBC_SQL_CONN_T conn;
  769. TSRMLS_FETCH();
  770. if (conn_resource) {
  771. henv = conn_resource->henv;
  772. conn = conn_resource->hdbc;
  773. } else {
  774. henv = SQL_NULL_HENV;
  775. conn = SQL_NULL_HDBC;
  776. }
  777. /* This leads to an endless loop in many drivers!
  778. *
  779. while(henv != SQL_NULL_HENV){
  780. do {
  781. */
  782. rc = SQLError(henv, conn, stmt, state, &error, errormsg, sizeof(errormsg)-1, &errormsgsize);
  783. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  784. snprintf(state, sizeof(state), "HY000");
  785. snprintf(errormsg, sizeof(errormsg), "Failed to fetch error message");
  786. }
  787. if (conn_resource) {
  788. memcpy(conn_resource->laststate, state, sizeof(state));
  789. memcpy(conn_resource->lasterrormsg, errormsg, sizeof(errormsg));
  790. }
  791. memcpy(ODBCG(laststate), state, sizeof(state));
  792. memcpy(ODBCG(lasterrormsg), errormsg, sizeof(errormsg));
  793. if (func) {
  794. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQL error: %s, SQL state %s in %s", errormsg, state, func);
  795. } else {
  796. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQL error: %s, SQL state %s", errormsg, state);
  797. }
  798. /*
  799. } while (SQL_SUCCEEDED(rc));
  800. }
  801. */
  802. }
  803. /* }}} */
  804. /* {{{ php_odbc_fetch_attribs */
  805. void php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAMETERS, int mode)
  806. {
  807. odbc_result *result;
  808. zval *pv_res;
  809. long flag;
  810. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &pv_res, &flag) == FAILURE) {
  811. return;
  812. }
  813. if (Z_LVAL_P(pv_res)) {
  814. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  815. if (mode) {
  816. result->longreadlen = flag;
  817. } else {
  818. result->binmode = flag;
  819. }
  820. } else {
  821. if (mode) {
  822. ODBCG(defaultlrl) = flag;
  823. } else {
  824. ODBCG(defaultbinmode) = flag;
  825. }
  826. }
  827. RETURN_TRUE;
  828. }
  829. /* }}} */
  830. /* {{{ odbc_bindcols */
  831. int odbc_bindcols(odbc_result *result TSRMLS_DC)
  832. {
  833. RETCODE rc;
  834. int i;
  835. SQLSMALLINT colnamelen; /* Not used */
  836. SQLLEN displaysize;
  837. SQLUSMALLINT colfieldid;
  838. int charextraalloc;
  839. result->values = (odbc_result_value *) safe_emalloc(sizeof(odbc_result_value), result->numcols, 0);
  840. result->longreadlen = ODBCG(defaultlrl);
  841. result->binmode = ODBCG(defaultbinmode);
  842. for(i = 0; i < result->numcols; i++) {
  843. charextraalloc = 0;
  844. colfieldid = SQL_COLUMN_DISPLAY_SIZE;
  845. rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), PHP_ODBC_SQL_DESC_NAME,
  846. result->values[i].name, sizeof(result->values[i].name), &colnamelen, 0);
  847. rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_TYPE,
  848. NULL, 0, NULL, &result->values[i].coltype);
  849. /* Don't bind LONG / BINARY columns, so that fetch behaviour can
  850. * be controlled by odbc_binmode() / odbc_longreadlen()
  851. */
  852. switch(result->values[i].coltype) {
  853. case SQL_BINARY:
  854. case SQL_VARBINARY:
  855. case SQL_LONGVARBINARY:
  856. case SQL_LONGVARCHAR:
  857. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  858. case SQL_WLONGVARCHAR:
  859. #endif
  860. result->values[i].value = NULL;
  861. break;
  862. #ifdef HAVE_ADABAS
  863. case SQL_TIMESTAMP:
  864. result->values[i].value = (char *)emalloc(27);
  865. SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
  866. 27, &result->values[i].vallen);
  867. break;
  868. #endif /* HAVE_ADABAS */
  869. case SQL_CHAR:
  870. case SQL_VARCHAR:
  871. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  872. case SQL_WCHAR:
  873. case SQL_WVARCHAR:
  874. colfieldid = SQL_DESC_OCTET_LENGTH;
  875. #else
  876. charextraalloc = 1;
  877. #endif
  878. default:
  879. rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), colfieldid,
  880. NULL, 0, NULL, &displaysize);
  881. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  882. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && colfieldid == SQL_DESC_OCTET_LENGTH) {
  883. /* This is a quirk for ODBC 2.0 compatibility for broken driver implementations.
  884. */
  885. charextraalloc = 1;
  886. rc = SQLColAttributes(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_DISPLAY_SIZE,
  887. NULL, 0, NULL, &displaysize);
  888. }
  889. /* Workaround for drivers that report NVARCHAR(MAX) columns as SQL_WVARCHAR with size 0 (bug #69975) */
  890. if (result->values[i].coltype == SQL_WVARCHAR && displaysize == 0) {
  891. result->values[i].coltype = SQL_WLONGVARCHAR;
  892. result->values[i].value = NULL;
  893. break;
  894. }
  895. #endif
  896. /* Workaround for Oracle ODBC Driver bug (#50162) when fetching TIMESTAMP column */
  897. if (result->values[i].coltype == SQL_TIMESTAMP) {
  898. displaysize += 3;
  899. }
  900. if (charextraalloc) {
  901. /* Since we don't know the exact # of bytes, allocate extra */
  902. displaysize *= 4;
  903. }
  904. result->values[i].value = (char *)emalloc(displaysize + 1);
  905. rc = SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
  906. displaysize + 1, &result->values[i].vallen);
  907. break;
  908. }
  909. }
  910. return 1;
  911. }
  912. /* }}} */
  913. /* {{{ odbc_transact */
  914. void odbc_transact(INTERNAL_FUNCTION_PARAMETERS, int type)
  915. {
  916. odbc_connection *conn;
  917. RETCODE rc;
  918. zval *pv_conn;
  919. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_conn) == FAILURE) {
  920. return;
  921. }
  922. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  923. rc = SQLTransact(conn->henv, conn->hdbc, (SQLUSMALLINT)((type)?SQL_COMMIT:SQL_ROLLBACK));
  924. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  925. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTransact");
  926. RETURN_FALSE;
  927. }
  928. RETURN_TRUE;
  929. }
  930. /* }}} */
  931. /* {{{ _close_pconn_with_id */
  932. static int _close_pconn_with_id(zend_rsrc_list_entry *le, int *id TSRMLS_DC)
  933. {
  934. if(Z_TYPE_P(le) == le_pconn && (((odbc_connection *)(le->ptr))->id == *id)){
  935. return 1;
  936. }else{
  937. return 0;
  938. }
  939. }
  940. /* }}} */
  941. /* {{{ odbc_column_lengths */
  942. void odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type)
  943. {
  944. odbc_result *result;
  945. #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
  946. /* this seems to be necessary for Solid2.3 ( tested by
  947. * tammy@synchronis.com) and Solid 3.0 (tested by eric@terra.telemediair.nl)
  948. * Solid does not seem to declare a SQLINTEGER, but it does declare a
  949. * SQL_INTEGER which does not work (despite being the same type as a SDWORD.
  950. * Solid 3.5 does not have this issue.
  951. */
  952. SDWORD len;
  953. #else
  954. SQLLEN len;
  955. #endif
  956. zval *pv_res;
  957. long pv_num;
  958. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &pv_res, &pv_num) == FAILURE) {
  959. return;
  960. }
  961. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  962. if (result->numcols == 0) {
  963. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  964. RETURN_FALSE;
  965. }
  966. if (pv_num > result->numcols) {
  967. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field index larger than number of fields");
  968. RETURN_FALSE;
  969. }
  970. if (pv_num < 1) {
  971. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field numbering starts at 1");
  972. RETURN_FALSE;
  973. }
  974. PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, (SQLUSMALLINT) (type?SQL_COLUMN_SCALE:SQL_COLUMN_PRECISION), NULL, 0, NULL, &len);
  975. RETURN_LONG(len);
  976. }
  977. /* }}} */
  978. /* Main User Functions */
  979. /* {{{ proto void odbc_close_all(void)
  980. Close all ODBC connections */
  981. PHP_FUNCTION(odbc_close_all)
  982. {
  983. void *ptr;
  984. int type;
  985. int i;
  986. int nument;
  987. if (zend_parse_parameters_none() == FAILURE) {
  988. return;
  989. }
  990. nument = zend_hash_next_free_element(&EG(regular_list));
  991. /* Loop through list and close all statements */
  992. for(i = 1; i < nument; i++) {
  993. ptr = zend_list_find(i, &type);
  994. if (ptr && (type == le_result)){
  995. zend_list_delete(i);
  996. }
  997. }
  998. /* Second loop through list, now close all connections */
  999. nument = zend_hash_next_free_element(&EG(regular_list));
  1000. for(i = 1; i < nument; i++) {
  1001. ptr = zend_list_find(i, &type);
  1002. if (ptr){
  1003. if(type == le_conn){
  1004. zend_list_delete(i);
  1005. }else if(type == le_pconn){
  1006. zend_list_delete(i);
  1007. /* Delete the persistent connection */
  1008. zend_hash_apply_with_argument(&EG(persistent_list),
  1009. (apply_func_arg_t) _close_pconn_with_id, (void *) &i TSRMLS_CC);
  1010. }
  1011. }
  1012. }
  1013. }
  1014. /* }}} */
  1015. /* {{{ proto bool odbc_binmode(int result_id, int mode)
  1016. Handle binary column data */
  1017. PHP_FUNCTION(odbc_binmode)
  1018. {
  1019. php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1020. }
  1021. /* }}} */
  1022. /* {{{ proto bool odbc_longreadlen(int result_id, int length)
  1023. Handle LONG columns */
  1024. PHP_FUNCTION(odbc_longreadlen)
  1025. {
  1026. php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1027. }
  1028. /* }}} */
  1029. /* {{{ proto resource odbc_prepare(resource connection_id, string query)
  1030. Prepares a statement for execution */
  1031. PHP_FUNCTION(odbc_prepare)
  1032. {
  1033. zval *pv_conn;
  1034. char *query;
  1035. int query_len;
  1036. odbc_result *result = NULL;
  1037. odbc_connection *conn;
  1038. RETCODE rc;
  1039. int i;
  1040. #ifdef HAVE_SQL_EXTENDED_FETCH
  1041. SQLUINTEGER scrollopts;
  1042. #endif
  1043. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pv_conn, &query, &query_len) == FAILURE) {
  1044. return;
  1045. }
  1046. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  1047. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  1048. result->numparams = 0;
  1049. result->param_info = NULL;
  1050. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  1051. if (rc == SQL_INVALID_HANDLE) {
  1052. efree(result);
  1053. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  1054. RETURN_FALSE;
  1055. }
  1056. if (rc == SQL_ERROR) {
  1057. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  1058. efree(result);
  1059. RETURN_FALSE;
  1060. }
  1061. #ifdef HAVE_SQL_EXTENDED_FETCH
  1062. /* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
  1063. whether Driver supports ExtendedFetch */
  1064. rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
  1065. if (rc == SQL_SUCCESS) {
  1066. if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
  1067. /* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
  1068. type if not possible.
  1069. */
  1070. SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
  1071. }
  1072. } else {
  1073. result->fetch_abs = 0;
  1074. }
  1075. #endif
  1076. rc = SQLPrepare(result->stmt, query, SQL_NTS);
  1077. switch (rc) {
  1078. case SQL_SUCCESS:
  1079. break;
  1080. case SQL_SUCCESS_WITH_INFO:
  1081. odbc_sql_error(conn, result->stmt, "SQLPrepare");
  1082. break;
  1083. default:
  1084. odbc_sql_error(conn, result->stmt, "SQLPrepare");
  1085. RETURN_FALSE;
  1086. }
  1087. SQLNumParams(result->stmt, &(result->numparams));
  1088. SQLNumResultCols(result->stmt, &(result->numcols));
  1089. if (result->numcols > 0) {
  1090. if (!odbc_bindcols(result TSRMLS_CC)) {
  1091. efree(result);
  1092. RETURN_FALSE;
  1093. }
  1094. } else {
  1095. result->values = NULL;
  1096. }
  1097. zend_list_addref(conn->id);
  1098. result->conn_ptr = conn;
  1099. result->fetched = 0;
  1100. result->param_info = (odbc_param_info *) safe_emalloc(sizeof(odbc_param_info), result->numparams, 0);
  1101. for (i=0;i<result->numparams;i++) {
  1102. rc = SQLDescribeParam(result->stmt, (SQLUSMALLINT)(i+1), &result->param_info[i].sqltype, &result->param_info[i].precision,
  1103. &result->param_info[i].scale, &result->param_info[i].nullable);
  1104. if (rc == SQL_ERROR) {
  1105. odbc_sql_error(result->conn_ptr, result->stmt, "SQLDescribeParameter");
  1106. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  1107. efree(result->param_info);
  1108. efree(result);
  1109. RETURN_FALSE;
  1110. }
  1111. }
  1112. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  1113. }
  1114. /* }}} */
  1115. /*
  1116. * Execute prepared SQL statement. Supports only input parameters.
  1117. */
  1118. /* {{{ proto bool odbc_execute(resource result_id [, array parameters_array])
  1119. Execute a prepared statement */
  1120. PHP_FUNCTION(odbc_execute)
  1121. {
  1122. zval *pv_res, *pv_param_arr, **tmp;
  1123. typedef struct params_t {
  1124. SQLLEN vallen;
  1125. int fp;
  1126. } params_t;
  1127. params_t *params = NULL;
  1128. char *filename;
  1129. unsigned char otype;
  1130. SQLSMALLINT ctype;
  1131. odbc_result *result;
  1132. int numArgs, i, ne;
  1133. RETCODE rc;
  1134. numArgs = ZEND_NUM_ARGS();
  1135. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|a", &pv_res, &pv_param_arr) == FAILURE) {
  1136. return;
  1137. }
  1138. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  1139. /* XXX check for already bound parameters*/
  1140. if (result->numparams > 0 && numArgs == 1) {
  1141. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No parameters to SQL statement given");
  1142. RETURN_FALSE;
  1143. }
  1144. if (result->numparams > 0) {
  1145. if ((ne = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr))) < result->numparams) {
  1146. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Not enough parameters (%d should be %d) given", ne, result->numparams);
  1147. RETURN_FALSE;
  1148. }
  1149. zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
  1150. params = (params_t *)safe_emalloc(sizeof(params_t), result->numparams, 0);
  1151. for(i = 0; i < result->numparams; i++) {
  1152. params[i].fp = -1;
  1153. }
  1154. for(i = 1; i <= result->numparams; i++) {
  1155. if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
  1156. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
  1157. SQLFreeStmt(result->stmt,SQL_RESET_PARAMS);
  1158. for (i = 0; i < result->numparams; i++) {
  1159. if (params[i].fp != -1) {
  1160. close(params[i].fp);
  1161. }
  1162. }
  1163. efree(params);
  1164. RETURN_FALSE;
  1165. }
  1166. otype = (*tmp)->type;
  1167. convert_to_string_ex(tmp);
  1168. if (Z_TYPE_PP(tmp) != IS_STRING) {
  1169. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
  1170. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  1171. for (i = 0; i < result->numparams; i++) {
  1172. if (params[i].fp != -1) {
  1173. close(params[i].fp);
  1174. }
  1175. }
  1176. efree(params);
  1177. RETURN_FALSE;
  1178. }
  1179. params[i-1].vallen = Z_STRLEN_PP(tmp);
  1180. params[i-1].fp = -1;
  1181. if (IS_SQL_BINARY(result->param_info[i-1].sqltype)) {
  1182. ctype = SQL_C_BINARY;
  1183. } else {
  1184. ctype = SQL_C_CHAR;
  1185. }
  1186. if (Z_STRLEN_PP(tmp) > 2 &&
  1187. Z_STRVAL_PP(tmp)[0] == '\'' &&
  1188. Z_STRVAL_PP(tmp)[Z_STRLEN_PP(tmp) - 1] == '\'') {
  1189. if (CHECK_ZVAL_NULL_PATH(*tmp)) {
  1190. RETURN_FALSE;
  1191. }
  1192. filename = estrndup(&Z_STRVAL_PP(tmp)[1], Z_STRLEN_PP(tmp) - 2);
  1193. filename[strlen(filename)] = '\0';
  1194. /* Check the basedir */
  1195. if (php_check_open_basedir(filename TSRMLS_CC)) {
  1196. efree(filename);
  1197. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  1198. for (i = 0; i < result->numparams; i++) {
  1199. if (params[i].fp != -1) {
  1200. close(params[i].fp);
  1201. }
  1202. }
  1203. efree(params);
  1204. RETURN_FALSE;
  1205. }
  1206. if ((params[i-1].fp = open(filename,O_RDONLY)) == -1) {
  1207. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Can't open file %s", filename);
  1208. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  1209. for (i = 0; i < result->numparams; i++) {
  1210. if (params[i].fp != -1) {
  1211. close(params[i].fp);
  1212. }
  1213. }
  1214. efree(params);
  1215. efree(filename);
  1216. RETURN_FALSE;
  1217. }
  1218. efree(filename);
  1219. params[i-1].vallen = SQL_LEN_DATA_AT_EXEC(0);
  1220. rc = SQLBindParameter(result->stmt, (SQLUSMALLINT)i, SQL_PARAM_INPUT,
  1221. ctype, result->param_info[i-1].sqltype, result->param_info[i-1].precision, result->param_info[i-1].scale,
  1222. (void *)params[i-1].fp, 0,
  1223. &params[i-1].vallen);
  1224. } else {
  1225. #ifdef HAVE_DBMAKER
  1226. precision = params[i-1].vallen;
  1227. #endif
  1228. if (otype == IS_NULL) {
  1229. params[i-1].vallen = SQL_NULL_DATA;
  1230. }
  1231. rc = SQLBindParameter(result->stmt, (SQLUSMALLINT)i, SQL_PARAM_INPUT,
  1232. ctype, result->param_info[i-1].sqltype, result->param_info[i-1].precision, result->param_info[i-1].scale,
  1233. Z_STRVAL_PP(tmp), 0,
  1234. &params[i-1].vallen);
  1235. }
  1236. if (rc == SQL_ERROR) {
  1237. odbc_sql_error(result->conn_ptr, result->stmt, "SQLBindParameter");
  1238. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  1239. for (i = 0; i < result->numparams; i++) {
  1240. if (params[i].fp != -1) {
  1241. close(params[i].fp);
  1242. }
  1243. }
  1244. efree(params);
  1245. RETURN_FALSE;
  1246. }
  1247. zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
  1248. }
  1249. }
  1250. /* Close cursor, needed for doing multiple selects */
  1251. rc = SQLFreeStmt(result->stmt, SQL_CLOSE);
  1252. if (rc == SQL_ERROR) {
  1253. odbc_sql_error(result->conn_ptr, result->stmt, "SQLFreeStmt");
  1254. }
  1255. rc = SQLExecute(result->stmt);
  1256. result->fetched = 0;
  1257. if (rc == SQL_NEED_DATA) {
  1258. char buf[4096];
  1259. int fp, nbytes;
  1260. while (rc == SQL_NEED_DATA) {
  1261. rc = SQLParamData(result->stmt, (void*)&fp);
  1262. if (rc == SQL_NEED_DATA) {
  1263. while ((nbytes = read(fp, &buf, 4096)) > 0) {
  1264. SQLPutData(result->stmt, (void*)&buf, nbytes);
  1265. }
  1266. }
  1267. }
  1268. } else {
  1269. switch (rc) {
  1270. case SQL_SUCCESS:
  1271. break;
  1272. case SQL_NO_DATA_FOUND:
  1273. case SQL_SUCCESS_WITH_INFO:
  1274. odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
  1275. break;
  1276. default:
  1277. odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
  1278. RETVAL_FALSE;
  1279. }
  1280. }
  1281. if (result->numparams > 0) {
  1282. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  1283. for(i = 0; i < result->numparams; i++) {
  1284. if (params[i].fp != -1) {
  1285. close(params[i].fp);
  1286. }
  1287. }
  1288. efree(params);
  1289. }
  1290. if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO || rc == SQL_NO_DATA_FOUND) {
  1291. RETVAL_TRUE;
  1292. }
  1293. if (result->numcols == 0) {
  1294. SQLNumResultCols(result->stmt, &(result->numcols));
  1295. if (result->numcols > 0) {
  1296. if (!odbc_bindcols(result TSRMLS_CC)) {
  1297. efree(result);
  1298. RETVAL_FALSE;
  1299. }
  1300. } else {
  1301. result->values = NULL;
  1302. }
  1303. }
  1304. }
  1305. /* }}} */
  1306. /* {{{ proto string odbc_cursor(resource result_id)
  1307. Get cursor name */
  1308. PHP_FUNCTION(odbc_cursor)
  1309. {
  1310. zval *pv_res;
  1311. SQLUSMALLINT max_len;
  1312. SQLSMALLINT len;
  1313. char *cursorname;
  1314. odbc_result *result;
  1315. RETCODE rc;
  1316. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_res) == FAILURE) {
  1317. return;
  1318. }
  1319. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  1320. rc = SQLGetInfo(result->conn_ptr->hdbc,SQL_MAX_CURSOR_NAME_LEN, (void *)&max_len,sizeof(max_len),&len);
  1321. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1322. RETURN_FALSE;
  1323. }
  1324. if (max_len > 0) {
  1325. cursorname = emalloc(max_len + 1);
  1326. rc = SQLGetCursorName(result->stmt,cursorname,(SQLSMALLINT)max_len,&len);
  1327. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1328. char state[6]; /* Not used */
  1329. SQLINTEGER error; /* Not used */
  1330. char errormsg[SQL_MAX_MESSAGE_LENGTH];
  1331. SQLSMALLINT errormsgsize; /* Not used */
  1332. SQLError( result->conn_ptr->henv, result->conn_ptr->hdbc,
  1333. result->stmt, state, &error, errormsg,
  1334. sizeof(errormsg)-1, &errormsgsize);
  1335. if (!strncmp(state,"S1015",5)) {
  1336. snprintf(cursorname, max_len+1, "php_curs_%d", (int)result->stmt);
  1337. if (SQLSetCursorName(result->stmt,cursorname,SQL_NTS) != SQL_SUCCESS) {
  1338. odbc_sql_error(result->conn_ptr, result->stmt, "SQLSetCursorName");
  1339. RETVAL_FALSE;
  1340. } else {
  1341. RETVAL_STRING(cursorname,1);
  1342. }
  1343. } else {
  1344. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQL error: %s, SQL state %s", errormsg, state);
  1345. RETVAL_FALSE;
  1346. }
  1347. } else {
  1348. RETVAL_STRING(cursorname,1);
  1349. }
  1350. efree(cursorname);
  1351. } else {
  1352. RETVAL_FALSE;
  1353. }
  1354. }
  1355. /* }}} */
  1356. #ifdef HAVE_SQLDATASOURCES
  1357. /* {{{ proto array odbc_data_source(resource connection_id, int fetch_type)
  1358. Return information about the currently connected data source */
  1359. PHP_FUNCTION(odbc_data_source)
  1360. {
  1361. zval *zv_conn;
  1362. long zv_fetch_type;
  1363. RETCODE rc = 0; /* assume all is good */
  1364. odbc_connection *conn;
  1365. UCHAR server_name[100], desc[200];
  1366. SQLSMALLINT len1=0, len2=0, fetch_type;
  1367. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zv_conn, &zv_fetch_type) == FAILURE) {
  1368. return;
  1369. }
  1370. fetch_type = (SQLSMALLINT) zv_fetch_type;
  1371. if (!(fetch_type == SQL_FETCH_FIRST || fetch_type == SQL_FETCH_NEXT)) {
  1372. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid fetch type (%d)", fetch_type);
  1373. RETURN_FALSE;
  1374. }
  1375. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &zv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  1376. /* now we have the "connection" lets call the DataSource object */
  1377. rc = SQLDataSources(conn->henv,
  1378. fetch_type,
  1379. server_name,
  1380. (SQLSMALLINT)sizeof(server_name),
  1381. &len1,
  1382. desc,
  1383. (SQLSMALLINT)sizeof(desc),
  1384. &len2);
  1385. if (rc != SQL_SUCCESS) {
  1386. /* ummm.... he did it */
  1387. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLDataSources");
  1388. RETURN_FALSE;
  1389. }
  1390. if (len1 == 0 || len2 == 0) {
  1391. /* we have a non-valid entry... so stop the looping */
  1392. RETURN_FALSE;
  1393. }
  1394. array_init(return_value);
  1395. add_assoc_string_ex(return_value, "server", sizeof("server"), server_name, 1);
  1396. add_assoc_string_ex(return_value, "description", sizeof("description"), desc, 1);
  1397. }
  1398. /* }}} */
  1399. #endif /* HAVE_SQLDATASOURCES */
  1400. /* {{{ proto resource odbc_exec(resource connection_id, string query [, int flags])
  1401. Prepare and execute an SQL statement */
  1402. /* XXX Use flags */
  1403. PHP_FUNCTION(odbc_exec)
  1404. {
  1405. zval *pv_conn;
  1406. long pv_flags;
  1407. char *query;
  1408. int numArgs, query_len;
  1409. odbc_result *result = NULL;
  1410. odbc_connection *conn;
  1411. RETCODE rc;
  1412. #ifdef HAVE_SQL_EXTENDED_FETCH
  1413. SQLUINTEGER scrollopts;
  1414. #endif
  1415. numArgs = ZEND_NUM_ARGS();
  1416. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &pv_conn, &query, &query_len, &pv_flags) == FAILURE) {
  1417. return;
  1418. }
  1419. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  1420. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  1421. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  1422. if (rc == SQL_INVALID_HANDLE) {
  1423. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  1424. efree(result);
  1425. RETURN_FALSE;
  1426. }
  1427. if (rc == SQL_ERROR) {
  1428. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  1429. efree(result);
  1430. RETURN_FALSE;
  1431. }
  1432. #ifdef HAVE_SQL_EXTENDED_FETCH
  1433. /* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
  1434. whether Driver supports ExtendedFetch */
  1435. rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
  1436. if (rc == SQL_SUCCESS) {
  1437. if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
  1438. /* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
  1439. type if not possible.
  1440. */
  1441. SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
  1442. }
  1443. } else {
  1444. result->fetch_abs = 0;
  1445. }
  1446. #endif
  1447. rc = SQLExecDirect(result->stmt, query, SQL_NTS);
  1448. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && rc != SQL_NO_DATA_FOUND) {
  1449. /* XXX FIXME we should really check out SQLSTATE with SQLError
  1450. * in case rc is SQL_SUCCESS_WITH_INFO here.
  1451. */
  1452. odbc_sql_error(conn, result->stmt, "SQLExecDirect");
  1453. SQLFreeStmt(result->stmt, SQL_DROP);
  1454. efree(result);
  1455. RETURN_FALSE;
  1456. }
  1457. SQLNumResultCols(result->stmt, &(result->numcols));
  1458. /* For insert, update etc. cols == 0 */
  1459. if (result->numcols > 0) {
  1460. if (!odbc_bindcols(result TSRMLS_CC)) {
  1461. efree(result);
  1462. RETURN_FALSE;
  1463. }
  1464. } else {
  1465. result->values = NULL;
  1466. }
  1467. zend_list_addref(conn->id);
  1468. result->conn_ptr = conn;
  1469. result->fetched = 0;
  1470. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  1471. }
  1472. /* }}} */
  1473. #ifdef PHP_ODBC_HAVE_FETCH_HASH
  1474. #define ODBC_NUM 1
  1475. #define ODBC_OBJECT 2
  1476. /* {{{ php_odbc_fetch_hash */
  1477. static void php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)
  1478. {
  1479. int i;
  1480. odbc_result *result;
  1481. RETCODE rc;
  1482. SQLSMALLINT sql_c_type;
  1483. char *buf = NULL;
  1484. #ifdef HAVE_SQL_EXTENDED_FETCH
  1485. SQLULEN crow;
  1486. SQLUSMALLINT RowStatus[1];
  1487. SQLLEN rownum;
  1488. zval *pv_res, *tmp;
  1489. long pv_row = -1;
  1490. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &pv_res, &pv_row) == FAILURE) {
  1491. return;
  1492. }
  1493. rownum = pv_row;
  1494. #else
  1495. zval *pv_res, *tmp;
  1496. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_res) == FAILURE) {
  1497. return;
  1498. }
  1499. #endif
  1500. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  1501. if (result->numcols == 0) {
  1502. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  1503. RETURN_FALSE;
  1504. }
  1505. #ifdef HAVE_SQL_EXTENDED_FETCH
  1506. if (result->fetch_abs) {
  1507. if (rownum > 0) {
  1508. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
  1509. } else {
  1510. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1511. }
  1512. } else
  1513. #endif
  1514. rc = SQLFetch(result->stmt);
  1515. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1516. RETURN_FALSE;
  1517. }
  1518. array_init(return_value);
  1519. #ifdef HAVE_SQL_EXTENDED_FETCH
  1520. if (rownum > 0 && result->fetch_abs)
  1521. result->fetched = rownum;
  1522. else
  1523. #endif
  1524. result->fetched++;
  1525. for(i = 0; i < result->numcols; i++) {
  1526. ALLOC_INIT_ZVAL(tmp);
  1527. Z_TYPE_P(tmp) = IS_STRING;
  1528. Z_STRLEN_P(tmp) = 0;
  1529. sql_c_type = SQL_C_CHAR;
  1530. switch(result->values[i].coltype) {
  1531. case SQL_BINARY:
  1532. case SQL_VARBINARY:
  1533. case SQL_LONGVARBINARY:
  1534. if (result->binmode <= 0) {
  1535. Z_STRVAL_P(tmp) = STR_EMPTY_ALLOC();
  1536. break;
  1537. }
  1538. if (result->binmode == 1) {
  1539. sql_c_type = SQL_C_BINARY;
  1540. }
  1541. case SQL_LONGVARCHAR:
  1542. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1543. case SQL_WLONGVARCHAR:
  1544. #endif
  1545. if (IS_SQL_LONG(result->values[i].coltype) && result->longreadlen <= 0) {
  1546. Z_STRVAL_P(tmp) = STR_EMPTY_ALLOC();
  1547. break;
  1548. }
  1549. if (buf == NULL) {
  1550. buf = emalloc(result->longreadlen + 1);
  1551. }
  1552. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1), sql_c_type, buf, result->longreadlen + 1, &result->values[i].vallen);
  1553. if (rc == SQL_ERROR) {
  1554. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1555. efree(buf);
  1556. RETURN_FALSE;
  1557. }
  1558. if (rc == SQL_SUCCESS_WITH_INFO) {
  1559. Z_STRLEN_P(tmp) = result->longreadlen;
  1560. } else if (result->values[i].vallen == SQL_NULL_DATA) {
  1561. ZVAL_NULL(tmp);
  1562. break;
  1563. } else {
  1564. Z_STRLEN_P(tmp) = result->values[i].vallen;
  1565. }
  1566. Z_STRVAL_P(tmp) = estrndup(buf, Z_STRLEN_P(tmp));
  1567. break;
  1568. default:
  1569. if (result->values[i].vallen == SQL_NULL_DATA) {
  1570. ZVAL_NULL(tmp);
  1571. break;
  1572. }
  1573. Z_STRLEN_P(tmp) = result->values[i].vallen;
  1574. Z_STRVAL_P(tmp) = estrndup(result->values[i].value,Z_STRLEN_P(tmp));
  1575. break;
  1576. }
  1577. if (result_type & ODBC_NUM) {
  1578. zend_hash_index_update(Z_ARRVAL_P(return_value), i, &tmp, sizeof(zval *), NULL);
  1579. } else {
  1580. if (!*(result->values[i].name) && Z_TYPE_P(tmp) == IS_STRING) {
  1581. zend_hash_update(Z_ARRVAL_P(return_value), Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)+1, &tmp, sizeof(zval *), NULL);
  1582. } else {
  1583. zend_hash_update(Z_ARRVAL_P(return_value), result->values[i].name, strlen(result->values[i].name)+1, &tmp, sizeof(zval *), NULL);
  1584. }
  1585. }
  1586. }
  1587. if (buf) {
  1588. efree(buf);
  1589. }
  1590. }
  1591. /* }}} */
  1592. /* {{{ proto object odbc_fetch_object(int result [, int rownumber])
  1593. Fetch a result row as an object */
  1594. PHP_FUNCTION(odbc_fetch_object)
  1595. {
  1596. php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, ODBC_OBJECT);
  1597. if (Z_TYPE_P(return_value) == IS_ARRAY) {
  1598. object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
  1599. }
  1600. }
  1601. /* }}} */
  1602. /* {{{ proto array odbc_fetch_array(int result [, int rownumber])
  1603. Fetch a result row as an associative array */
  1604. PHP_FUNCTION(odbc_fetch_array)
  1605. {
  1606. php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, ODBC_OBJECT);
  1607. }
  1608. /* }}} */
  1609. #endif
  1610. /* {{{ proto int odbc_fetch_into(resource result_id, array &result_array, [, int rownumber])
  1611. Fetch one result row into an array */
  1612. PHP_FUNCTION(odbc_fetch_into)
  1613. {
  1614. int i;
  1615. odbc_result *result;
  1616. RETCODE rc;
  1617. SQLSMALLINT sql_c_type;
  1618. char *buf = NULL;
  1619. zval *pv_res, **pv_res_arr, *tmp;
  1620. #ifdef HAVE_SQL_EXTENDED_FETCH
  1621. long pv_row = 0;
  1622. SQLULEN crow;
  1623. SQLUSMALLINT RowStatus[1];
  1624. SQLLEN rownum = -1;
  1625. #endif /* HAVE_SQL_EXTENDED_FETCH */
  1626. #ifdef HAVE_SQL_EXTENDED_FETCH
  1627. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rZ|l", &pv_res, &pv_res_arr, &pv_row) == FAILURE) {
  1628. return;
  1629. }
  1630. rownum = pv_row;
  1631. #else
  1632. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rZ", &pv_res, &pv_res_arr) == FAILURE) {
  1633. return;
  1634. }
  1635. #endif /* HAVE_SQL_EXTENDED_FETCH */
  1636. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  1637. if (result->numcols == 0) {
  1638. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  1639. RETURN_FALSE;
  1640. }
  1641. if (Z_TYPE_PP(pv_res_arr) != IS_ARRAY) {
  1642. array_init(*pv_res_arr);
  1643. }
  1644. #ifdef HAVE_SQL_EXTENDED_FETCH
  1645. if (result->fetch_abs) {
  1646. if (rownum > 0) {
  1647. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
  1648. } else {
  1649. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1650. }
  1651. } else
  1652. #endif
  1653. rc = SQLFetch(result->stmt);
  1654. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1655. RETURN_FALSE;
  1656. }
  1657. #ifdef HAVE_SQL_EXTENDED_FETCH
  1658. if (rownum > 0 && result->fetch_abs)
  1659. result->fetched = rownum;
  1660. else
  1661. #endif
  1662. result->fetched++;
  1663. for(i = 0; i < result->numcols; i++) {
  1664. MAKE_STD_ZVAL(tmp);
  1665. Z_TYPE_P(tmp) = IS_STRING;
  1666. Z_STRLEN_P(tmp) = 0;
  1667. sql_c_type = SQL_C_CHAR;
  1668. switch(result->values[i].coltype) {
  1669. case SQL_BINARY:
  1670. case SQL_VARBINARY:
  1671. case SQL_LONGVARBINARY:
  1672. if (result->binmode <= 0) {
  1673. Z_STRVAL_P(tmp) = STR_EMPTY_ALLOC();
  1674. break;
  1675. }
  1676. if (result->binmode == 1) sql_c_type = SQL_C_BINARY;
  1677. case SQL_LONGVARCHAR:
  1678. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1679. case SQL_WLONGVARCHAR:
  1680. #endif
  1681. if (IS_SQL_LONG(result->values[i].coltype) && result->longreadlen <= 0) {
  1682. Z_STRVAL_P(tmp) = STR_EMPTY_ALLOC();
  1683. break;
  1684. }
  1685. if (buf == NULL) {
  1686. buf = emalloc(result->longreadlen + 1);
  1687. }
  1688. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1),sql_c_type, buf, result->longreadlen + 1, &result->values[i].vallen);
  1689. if (rc == SQL_ERROR) {
  1690. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1691. efree(buf);
  1692. RETURN_FALSE;
  1693. }
  1694. if (rc == SQL_SUCCESS_WITH_INFO) {
  1695. Z_STRLEN_P(tmp) = result->longreadlen;
  1696. } else if (result->values[i].vallen == SQL_NULL_DATA) {
  1697. ZVAL_NULL(tmp);
  1698. break;
  1699. } else {
  1700. Z_STRLEN_P(tmp) = result->values[i].vallen;
  1701. }
  1702. Z_STRVAL_P(tmp) = estrndup(buf, Z_STRLEN_P(tmp));
  1703. break;
  1704. default:
  1705. if (result->values[i].vallen == SQL_NULL_DATA) {
  1706. ZVAL_NULL(tmp);
  1707. break;
  1708. }
  1709. Z_STRLEN_P(tmp) = result->values[i].vallen;
  1710. Z_STRVAL_P(tmp) = estrndup(result->values[i].value,Z_STRLEN_P(tmp));
  1711. break;
  1712. }
  1713. zend_hash_index_update(Z_ARRVAL_PP(pv_res_arr), i, &tmp, sizeof(zval *), NULL);
  1714. }
  1715. if (buf) efree(buf);
  1716. RETURN_LONG(result->numcols);
  1717. }
  1718. /* }}} */
  1719. /* {{{ proto bool solid_fetch_prev(resource result_id)
  1720. */
  1721. #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
  1722. PHP_FUNCTION(solid_fetch_prev)
  1723. {
  1724. odbc_result *result;
  1725. RETCODE rc;
  1726. zval *pv_res;
  1727. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_res) == FAILURE) {
  1728. return;
  1729. }
  1730. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  1731. if (result->numcols == 0) {
  1732. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  1733. RETURN_FALSE;
  1734. }
  1735. rc = SQLFetchPrev(result->stmt);
  1736. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1737. RETURN_FALSE;
  1738. }
  1739. if (result->fetched > 1) {
  1740. result->fetched--;
  1741. }
  1742. RETURN_TRUE;
  1743. }
  1744. #endif
  1745. /* }}} */
  1746. /* {{{ proto bool odbc_fetch_row(resource result_id [, int row_number])
  1747. Fetch a row */
  1748. PHP_FUNCTION(odbc_fetch_row)
  1749. {
  1750. SQLLEN rownum;
  1751. odbc_result *result;
  1752. RETCODE rc;
  1753. zval *pv_res;
  1754. long pv_row = 1;
  1755. #ifdef HAVE_SQL_EXTENDED_FETCH
  1756. SQLULEN crow;
  1757. SQLUSMALLINT RowStatus[1];
  1758. #endif
  1759. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &pv_res, &pv_row) == FAILURE) {
  1760. return;
  1761. }
  1762. rownum = pv_row;
  1763. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  1764. if (result->numcols == 0) {
  1765. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  1766. RETURN_FALSE;
  1767. }
  1768. #ifdef HAVE_SQL_EXTENDED_FETCH
  1769. if (result->fetch_abs) {
  1770. if (ZEND_NUM_ARGS() > 1) {
  1771. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
  1772. } else {
  1773. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1774. }
  1775. } else
  1776. #endif
  1777. rc = SQLFetch(result->stmt);
  1778. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1779. RETURN_FALSE;
  1780. }
  1781. if (ZEND_NUM_ARGS() > 1) {
  1782. result->fetched = rownum;
  1783. } else {
  1784. result->fetched++;
  1785. }
  1786. RETURN_TRUE;
  1787. }
  1788. /* }}} */
  1789. /* {{{ proto mixed odbc_result(resource result_id, mixed field)
  1790. Get result data */
  1791. PHP_FUNCTION(odbc_result)
  1792. {
  1793. char *field;
  1794. int field_ind;
  1795. SQLSMALLINT sql_c_type = SQL_C_CHAR;
  1796. odbc_result *result;
  1797. int i = 0;
  1798. RETCODE rc;
  1799. SQLLEN fieldsize;
  1800. zval *pv_res, **pv_field;
  1801. #ifdef HAVE_SQL_EXTENDED_FETCH
  1802. SQLULEN crow;
  1803. SQLUSMALLINT RowStatus[1];
  1804. #endif
  1805. field_ind = -1;
  1806. field = NULL;
  1807. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rZ", &pv_res, &pv_field) == FAILURE) {
  1808. return;
  1809. }
  1810. if (Z_TYPE_PP(pv_field) == IS_STRING) {
  1811. field = Z_STRVAL_PP(pv_field);
  1812. } else {
  1813. convert_to_long_ex(pv_field);
  1814. field_ind = Z_LVAL_PP(pv_field) - 1;
  1815. }
  1816. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  1817. if ((result->numcols == 0)) {
  1818. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  1819. RETURN_FALSE;
  1820. }
  1821. /* get field index if the field parameter was a string */
  1822. if (field != NULL) {
  1823. if (result->values == NULL) {
  1824. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Result set contains no data");
  1825. RETURN_FALSE;
  1826. }
  1827. for(i = 0; i < result->numcols; i++) {
  1828. if (!strcasecmp(result->values[i].name, field)) {
  1829. field_ind = i;
  1830. break;
  1831. }
  1832. }
  1833. if (field_ind < 0) {
  1834. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field %s not found", field);
  1835. RETURN_FALSE;
  1836. }
  1837. } else {
  1838. /* check for limits of field_ind if the field parameter was an int */
  1839. if (field_ind >= result->numcols || field_ind < 0) {
  1840. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field index is larger than the number of fields");
  1841. RETURN_FALSE;
  1842. }
  1843. }
  1844. if (result->fetched == 0) {
  1845. /* User forgot to call odbc_fetch_row(), or wants to reload the results, do it now */
  1846. #ifdef HAVE_SQL_EXTENDED_FETCH
  1847. if (result->fetch_abs)
  1848. rc = SQLExtendedFetch(result->stmt, SQL_FETCH_NEXT, 1, &crow,RowStatus);
  1849. else
  1850. #endif
  1851. rc = SQLFetch(result->stmt);
  1852. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1853. RETURN_FALSE;
  1854. }
  1855. result->fetched++;
  1856. }
  1857. switch(result->values[field_ind].coltype) {
  1858. case SQL_BINARY:
  1859. case SQL_VARBINARY:
  1860. case SQL_LONGVARBINARY:
  1861. if (result->binmode <= 1) {
  1862. sql_c_type = SQL_C_BINARY;
  1863. }
  1864. if (result->binmode <= 0) {
  1865. break;
  1866. }
  1867. case SQL_LONGVARCHAR:
  1868. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1869. case SQL_WLONGVARCHAR:
  1870. #endif
  1871. if (IS_SQL_LONG(result->values[field_ind].coltype)) {
  1872. if (result->longreadlen <= 0) {
  1873. break;
  1874. } else {
  1875. fieldsize = result->longreadlen;
  1876. }
  1877. } else {
  1878. PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(field_ind + 1),
  1879. (SQLUSMALLINT)((sql_c_type == SQL_C_BINARY) ? SQL_COLUMN_LENGTH :
  1880. SQL_COLUMN_DISPLAY_SIZE),
  1881. NULL, 0, NULL, &fieldsize);
  1882. }
  1883. /* For char data, the length of the returned string will be longreadlen - 1 */
  1884. fieldsize = (result->longreadlen <= 0) ? 4096 : result->longreadlen;
  1885. field = emalloc(fieldsize);
  1886. /* SQLGetData will truncate CHAR data to fieldsize - 1 bytes and append \0.
  1887. * For binary data it is truncated to fieldsize bytes.
  1888. */
  1889. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1), sql_c_type,
  1890. field, fieldsize, &result->values[field_ind].vallen);
  1891. if (rc == SQL_ERROR) {
  1892. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1893. efree(field);
  1894. RETURN_FALSE;
  1895. }
  1896. if (result->values[field_ind].vallen == SQL_NULL_DATA) {
  1897. efree(field);
  1898. RETURN_NULL();
  1899. } else if (rc == SQL_NO_DATA_FOUND) {
  1900. efree(field);
  1901. RETURN_FALSE;
  1902. }
  1903. /* Reduce fieldlen by 1 if we have char data. One day we might
  1904. have binary strings... */
  1905. if ((result->values[field_ind].coltype == SQL_LONGVARCHAR)
  1906. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1907. || (result->values[field_ind].coltype == SQL_WLONGVARCHAR)
  1908. #endif
  1909. ) {
  1910. fieldsize -= 1;
  1911. }
  1912. /* Don't duplicate result, saves one emalloc.
  1913. For SQL_SUCCESS, the length is in vallen.
  1914. */
  1915. RETURN_STRINGL(field, (rc == SQL_SUCCESS_WITH_INFO) ? fieldsize : result->values[field_ind].vallen, 0);
  1916. break;
  1917. default:
  1918. if (result->values[field_ind].vallen == SQL_NULL_DATA) {
  1919. RETURN_NULL();
  1920. } else {
  1921. RETURN_STRINGL(result->values[field_ind].value, result->values[field_ind].vallen, 1);
  1922. }
  1923. break;
  1924. }
  1925. /* If we come here, output unbound LONG and/or BINARY column data to the client */
  1926. /* We emalloc 1 byte more for SQL_C_CHAR (trailing \0) */
  1927. fieldsize = (sql_c_type == SQL_C_CHAR) ? 4096 : 4095;
  1928. field = emalloc(fieldsize);
  1929. /* Call SQLGetData() until SQL_SUCCESS is returned */
  1930. while(1) {
  1931. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1),sql_c_type, field, fieldsize, &result->values[field_ind].vallen);
  1932. if (rc == SQL_ERROR) {
  1933. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1934. efree(field);
  1935. RETURN_FALSE;
  1936. }
  1937. if (result->values[field_ind].vallen == SQL_NULL_DATA) {
  1938. efree(field);
  1939. RETURN_NULL();
  1940. }
  1941. /* chop the trailing \0 by outputing only 4095 bytes */
  1942. PHPWRITE(field,(rc == SQL_SUCCESS_WITH_INFO) ? 4095 : result->values[field_ind].vallen);
  1943. if (rc == SQL_SUCCESS) { /* no more data avail */
  1944. efree(field);
  1945. RETURN_TRUE;
  1946. }
  1947. }
  1948. RETURN_TRUE;
  1949. }
  1950. /* }}} */
  1951. /* {{{ proto int odbc_result_all(resource result_id [, string format])
  1952. Print result as HTML table */
  1953. PHP_FUNCTION(odbc_result_all)
  1954. {
  1955. char *buf = NULL;
  1956. odbc_result *result;
  1957. RETCODE rc;
  1958. zval *pv_res;
  1959. char *pv_format = NULL;
  1960. int i, pv_format_len = 0;
  1961. SQLSMALLINT sql_c_type;
  1962. #ifdef HAVE_SQL_EXTENDED_FETCH
  1963. SQLULEN crow;
  1964. SQLUSMALLINT RowStatus[1];
  1965. #endif
  1966. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s", &pv_res, &pv_format, &pv_format_len) == FAILURE) {
  1967. return;
  1968. }
  1969. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  1970. if (result->numcols == 0) {
  1971. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  1972. RETURN_FALSE;
  1973. }
  1974. #ifdef HAVE_SQL_EXTENDED_FETCH
  1975. if (result->fetch_abs)
  1976. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1977. else
  1978. #endif
  1979. rc = SQLFetch(result->stmt);
  1980. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1981. php_printf("<h2>No rows found</h2>\n");
  1982. RETURN_LONG(0);
  1983. }
  1984. /* Start table tag */
  1985. if (ZEND_NUM_ARGS() == 1) {
  1986. php_printf("<table><tr>");
  1987. } else {
  1988. php_printf("<table %s ><tr>", pv_format);
  1989. }
  1990. for (i = 0; i < result->numcols; i++) {
  1991. php_printf("<th>%s</th>", result->values[i].name);
  1992. }
  1993. php_printf("</tr>\n");
  1994. while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
  1995. result->fetched++;
  1996. php_printf("<tr>");
  1997. for(i = 0; i < result->numcols; i++) {
  1998. sql_c_type = SQL_C_CHAR;
  1999. switch(result->values[i].coltype) {
  2000. case SQL_BINARY:
  2001. case SQL_VARBINARY:
  2002. case SQL_LONGVARBINARY:
  2003. if (result->binmode <= 0) {
  2004. php_printf("<td>Not printable</td>");
  2005. break;
  2006. }
  2007. if (result->binmode <= 1) sql_c_type = SQL_C_BINARY;
  2008. case SQL_LONGVARCHAR:
  2009. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  2010. case SQL_WLONGVARCHAR:
  2011. #endif
  2012. if (IS_SQL_LONG(result->values[i].coltype) &&
  2013. result->longreadlen <= 0) {
  2014. php_printf("<td>Not printable</td>");
  2015. break;
  2016. }
  2017. if (buf == NULL) {
  2018. buf = emalloc(result->longreadlen);
  2019. }
  2020. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1),sql_c_type, buf, result->longreadlen, &result->values[i].vallen);
  2021. php_printf("<td>");
  2022. if (rc == SQL_ERROR) {
  2023. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  2024. php_printf("</td></tr></table>");
  2025. efree(buf);
  2026. RETURN_FALSE;
  2027. }
  2028. if (rc == SQL_SUCCESS_WITH_INFO) {
  2029. PHPWRITE(buf, result->longreadlen);
  2030. } else if (result->values[i].vallen == SQL_NULL_DATA) {
  2031. php_printf("<td>NULL</td>");
  2032. break;
  2033. } else {
  2034. PHPWRITE(buf, result->values[i].vallen);
  2035. }
  2036. php_printf("</td>");
  2037. break;
  2038. default:
  2039. if (result->values[i].vallen == SQL_NULL_DATA) {
  2040. php_printf("<td>NULL</td>");
  2041. } else {
  2042. php_printf("<td>%s</td>", result->values[i].value);
  2043. }
  2044. break;
  2045. }
  2046. }
  2047. php_printf("</tr>\n");
  2048. #ifdef HAVE_SQL_EXTENDED_FETCH
  2049. if (result->fetch_abs)
  2050. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  2051. else
  2052. #endif
  2053. rc = SQLFetch(result->stmt);
  2054. }
  2055. php_printf("</table>\n");
  2056. if (buf) efree(buf);
  2057. RETURN_LONG(result->fetched);
  2058. }
  2059. /* }}} */
  2060. /* {{{ proto bool odbc_free_result(resource result_id)
  2061. Free resources associated with a result */
  2062. PHP_FUNCTION(odbc_free_result)
  2063. {
  2064. zval *pv_res;
  2065. odbc_result *result;
  2066. int i;
  2067. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_res) == FAILURE) {
  2068. return;
  2069. }
  2070. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  2071. if (result->values) {
  2072. for (i = 0; i < result->numcols; i++) {
  2073. if (result->values[i].value) {
  2074. efree(result->values[i].value);
  2075. }
  2076. }
  2077. efree(result->values);
  2078. result->values = NULL;
  2079. }
  2080. zend_list_delete(Z_LVAL_P(pv_res));
  2081. RETURN_TRUE;
  2082. }
  2083. /* }}} */
  2084. /* {{{ proto resource odbc_connect(string DSN, string user, string password [, int cursor_option])
  2085. Connect to a datasource */
  2086. PHP_FUNCTION(odbc_connect)
  2087. {
  2088. odbc_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2089. }
  2090. /* }}} */
  2091. /* {{{ proto resource odbc_pconnect(string DSN, string user, string password [, int cursor_option])
  2092. Establish a persistent connection to a datasource */
  2093. PHP_FUNCTION(odbc_pconnect)
  2094. {
  2095. odbc_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2096. }
  2097. /* }}} */
  2098. /* {{{ odbc_sqlconnect */
  2099. int odbc_sqlconnect(odbc_connection **conn, char *db, char *uid, char *pwd, int cur_opt, int persistent TSRMLS_DC)
  2100. {
  2101. RETCODE rc;
  2102. *conn = (odbc_connection *)pemalloc(sizeof(odbc_connection), persistent);
  2103. (*conn)->persistent = persistent;
  2104. SQLAllocEnv(&((*conn)->henv));
  2105. SQLAllocConnect((*conn)->henv, &((*conn)->hdbc));
  2106. #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
  2107. SQLSetConnectOption((*conn)->hdbc, SQL_TRANSLATE_OPTION,
  2108. SQL_SOLID_XLATOPT_NOCNV);
  2109. #endif
  2110. #ifdef HAVE_ODBC_ROUTER
  2111. {
  2112. #define CONNSTRSIZE 2048
  2113. char *lpszConnStr = emalloc(CONNSTRSIZE);
  2114. if (lpszConnStr && db) {
  2115. short cbszConnStr;
  2116. if (strstr(db, ";")) {
  2117. /* the caller has apparently passed a connection-string */
  2118. if (strstr(db, "uid") || strstr(db, "UID")) {
  2119. uid = NULL;
  2120. }
  2121. if (strstr(db, "pwd") || strstr(db, "PWD")) {
  2122. pwd = NULL;
  2123. }
  2124. strlcpy( lpszConnStr, db, CONNSTRSIZE);
  2125. }
  2126. else {
  2127. strcpy(lpszConnStr, "DSN=");
  2128. strlcat(lpszConnStr, db, CONNSTRSIZE);
  2129. }
  2130. if (uid) {
  2131. if (uid[0]) {
  2132. strlcat(lpszConnStr, ";UID=", CONNSTRSIZE);
  2133. strlcat(lpszConnStr, uid, CONNSTRSIZE);
  2134. strlcat(lpszConnStr, ";", CONNSTRSIZE);
  2135. }
  2136. if (pwd) {
  2137. if (pwd[0]) {
  2138. strlcat(lpszConnStr, "PWD=", CONNSTRSIZE);
  2139. strlcat(lpszConnStr, pwd, CONNSTRSIZE);
  2140. strlcat(lpszConnStr, ";", CONNSTRSIZE);
  2141. }
  2142. }
  2143. }
  2144. rc = SQLDriverConnect((*conn)->hdbc, NULL, lpszConnStr, SQL_NTS, lpszConnStr, CONNSTRSIZE, &cbszConnStr, SQL_DRIVER_NOPROMPT);
  2145. efree(lpszConnStr);
  2146. }
  2147. }
  2148. #else
  2149. #ifdef HAVE_OPENLINK
  2150. {
  2151. char dsnbuf[1024];
  2152. short dsnbuflen;
  2153. rc = SQLDriverConnect((*conn)->hdbc, NULL, db, SQL_NTS, dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
  2154. }
  2155. #else
  2156. if (cur_opt != SQL_CUR_DEFAULT) {
  2157. rc = SQLSetConnectOption((*conn)->hdbc, SQL_ODBC_CURSORS, cur_opt);
  2158. if (rc != SQL_SUCCESS) { /* && rc != SQL_SUCCESS_WITH_INFO ? */
  2159. odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLSetConnectOption");
  2160. SQLFreeConnect((*conn)->hdbc);
  2161. pefree(*conn, persistent);
  2162. return FALSE;
  2163. }
  2164. }
  2165. /* Possible fix for bug #10250
  2166. * Needs testing on UnixODBC < 2.0.5 though. */
  2167. #if defined(HAVE_EMPRESS) || defined(HAVE_UNIXODBC) || defined(PHP_WIN32) || defined (HAVE_IODBC)
  2168. /* * Uncomment the line above, and comment line below to fully test
  2169. * #ifdef HAVE_EMPRESS */
  2170. {
  2171. int direct = 0;
  2172. char dsnbuf[1024];
  2173. short dsnbuflen;
  2174. char *ldb = 0;
  2175. int ldb_len = 0;
  2176. if (strstr((char*)db, ";")) {
  2177. direct = 1;
  2178. if (uid && !strstr ((char*)db, "uid") && !strstr((char*)db, "UID")) {
  2179. spprintf(&ldb, 0, "%s;UID=%s;PWD=%s", db, uid, pwd);
  2180. } else {
  2181. ldb_len = strlen(db)+1;
  2182. ldb = (char*) emalloc(ldb_len);
  2183. memcpy(ldb, db, ldb_len);
  2184. }
  2185. }
  2186. if (direct) {
  2187. rc = SQLDriverConnect((*conn)->hdbc, NULL, ldb, strlen(ldb), dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
  2188. } else {
  2189. rc = SQLConnect((*conn)->hdbc, db, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
  2190. }
  2191. if (ldb) {
  2192. efree(ldb);
  2193. }
  2194. }
  2195. #else
  2196. rc = SQLConnect((*conn)->hdbc, db, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
  2197. #endif
  2198. #endif
  2199. #endif
  2200. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2201. odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLConnect");
  2202. SQLFreeConnect((*conn)->hdbc);
  2203. pefree((*conn), persistent);
  2204. return FALSE;
  2205. }
  2206. /* (*conn)->open = 1;*/
  2207. return TRUE;
  2208. }
  2209. /* }}} */
  2210. /* Persistent connections: two list-types le_pconn, le_conn and a plist
  2211. * where hashed connection info is stored together with index pointer to
  2212. * the actual link of type le_pconn in the list. Only persistent
  2213. * connections get hashed up. Normal connections use existing pconnections.
  2214. * Maybe this has to change with regard to transactions on pconnections?
  2215. * Possibly set autocommit to on on request shutdown.
  2216. *
  2217. * We do have to hash non-persistent connections, and reuse connections.
  2218. * In the case where two connects were being made, without closing the first
  2219. * connect, access violations were occuring. This is because some of the
  2220. * "globals" in this module should actualy be per-connection variables. I
  2221. * simply fixed things to get them working for now. Shane
  2222. */
  2223. /* {{{ odbc_do_connect */
  2224. void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  2225. {
  2226. char *db, *uid, *pwd;
  2227. int db_len, uid_len, pwd_len;
  2228. long pv_opt = SQL_CUR_DEFAULT;
  2229. odbc_connection *db_conn;
  2230. char *hashed_details;
  2231. int hashed_len, cur_opt;
  2232. /* Now an optional 4th parameter specifying the cursor type
  2233. * defaulting to the cursors default
  2234. */
  2235. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|l", &db, &db_len, &uid, &uid_len, &pwd, &pwd_len, &pv_opt) == FAILURE) {
  2236. return;
  2237. }
  2238. cur_opt = pv_opt;
  2239. if (ZEND_NUM_ARGS() > 3) {
  2240. /* Confirm the cur_opt range */
  2241. if (! (cur_opt == SQL_CUR_USE_IF_NEEDED ||
  2242. cur_opt == SQL_CUR_USE_ODBC ||
  2243. cur_opt == SQL_CUR_USE_DRIVER ||
  2244. cur_opt == SQL_CUR_DEFAULT) ) {
  2245. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Cursor type (%d)", cur_opt);
  2246. RETURN_FALSE;
  2247. }
  2248. }
  2249. if (ODBCG(allow_persistent) <= 0) {
  2250. persistent = 0;
  2251. }
  2252. hashed_len = spprintf(&hashed_details, 0, "%s_%s_%s_%s_%d", ODBC_TYPE, db, uid, pwd, cur_opt);
  2253. /* FIXME the idea of checking to see if our connection is already persistent
  2254. is good, but it adds a lot of overhead to non-persistent connections. We
  2255. should look and see if we can fix that somehow */
  2256. /* try to find if we already have this link in our persistent list,
  2257. * no matter if it is to be persistent or not
  2258. */
  2259. try_and_get_another_connection:
  2260. if (persistent) {
  2261. zend_rsrc_list_entry *le;
  2262. /* the link is not in the persistent list */
  2263. if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_len + 1, (void **) &le) == FAILURE) {
  2264. zend_rsrc_list_entry new_le;
  2265. if (ODBCG(max_links) != -1 && ODBCG(num_links) >= ODBCG(max_links)) {
  2266. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", ODBCG(num_links));
  2267. efree(hashed_details);
  2268. RETURN_FALSE;
  2269. }
  2270. if (ODBCG(max_persistent) != -1 && ODBCG(num_persistent) >= ODBCG(max_persistent)) {
  2271. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Too many open persistent links (%ld)", ODBCG(num_persistent));
  2272. efree(hashed_details);
  2273. RETURN_FALSE;
  2274. }
  2275. if (!odbc_sqlconnect(&db_conn, db, uid, pwd, cur_opt, 1 TSRMLS_CC)) {
  2276. efree(hashed_details);
  2277. RETURN_FALSE;
  2278. }
  2279. Z_TYPE(new_le) = le_pconn;
  2280. new_le.ptr = db_conn;
  2281. if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_len + 1, &new_le,
  2282. sizeof(zend_rsrc_list_entry), NULL) == FAILURE) {
  2283. free(db_conn);
  2284. efree(hashed_details);
  2285. RETURN_FALSE;
  2286. }
  2287. ODBCG(num_persistent)++;
  2288. ODBCG(num_links)++;
  2289. db_conn->id = ZEND_REGISTER_RESOURCE(return_value, db_conn, le_pconn);
  2290. } else { /* found connection */
  2291. if (Z_TYPE_P(le) != le_pconn) {
  2292. RETURN_FALSE;
  2293. }
  2294. /*
  2295. * check to see if the connection is still valid
  2296. */
  2297. db_conn = (odbc_connection *)le->ptr;
  2298. /*
  2299. * check to see if the connection is still in place (lurcher)
  2300. */
  2301. if(ODBCG(check_persistent)){
  2302. RETCODE ret;
  2303. UCHAR d_name[32];
  2304. SQLSMALLINT len;
  2305. ret = SQLGetInfo(db_conn->hdbc,
  2306. SQL_DATA_SOURCE_READ_ONLY,
  2307. d_name, sizeof(d_name), &len);
  2308. if(ret != SQL_SUCCESS || len == 0) {
  2309. zend_hash_del(&EG(persistent_list), hashed_details, hashed_len + 1);
  2310. /* Commented out to fix a possible double closure error
  2311. * when working with persistent connections as submitted by
  2312. * bug #15758
  2313. *
  2314. * safe_odbc_disconnect(db_conn->hdbc);
  2315. * SQLFreeConnect(db_conn->hdbc);
  2316. */
  2317. goto try_and_get_another_connection;
  2318. }
  2319. }
  2320. }
  2321. db_conn->id = ZEND_REGISTER_RESOURCE(return_value, db_conn, le_pconn);
  2322. } else { /* non persistent */
  2323. zend_rsrc_list_entry *index_ptr, new_index_ptr;
  2324. if (zend_hash_find(&EG(regular_list), hashed_details, hashed_len + 1, (void **) &index_ptr) == SUCCESS) {
  2325. int type, conn_id;
  2326. void *ptr;
  2327. if (Z_TYPE_P(index_ptr) != le_index_ptr) {
  2328. RETURN_FALSE;
  2329. }
  2330. conn_id = (int)index_ptr->ptr;
  2331. ptr = zend_list_find(conn_id, &type); /* check if the connection is still there */
  2332. if (ptr && (type == le_conn || type == le_pconn)) {
  2333. zend_list_addref(conn_id);
  2334. Z_LVAL_P(return_value) = conn_id;
  2335. Z_TYPE_P(return_value) = IS_RESOURCE;
  2336. efree(hashed_details);
  2337. return;
  2338. } else {
  2339. zend_hash_del(&EG(regular_list), hashed_details, hashed_len + 1);
  2340. }
  2341. }
  2342. if (ODBCG(max_links) != -1 && ODBCG(num_links) >= ODBCG(max_links)) {
  2343. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Too many open connections (%ld)",ODBCG(num_links));
  2344. efree(hashed_details);
  2345. RETURN_FALSE;
  2346. }
  2347. if (!odbc_sqlconnect(&db_conn, db, uid, pwd, cur_opt, 0 TSRMLS_CC)) {
  2348. efree(hashed_details);
  2349. RETURN_FALSE;
  2350. }
  2351. db_conn->id = ZEND_REGISTER_RESOURCE(return_value, db_conn, le_conn);
  2352. new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
  2353. Z_TYPE(new_index_ptr) = le_index_ptr;
  2354. if (zend_hash_update(&EG(regular_list), hashed_details, hashed_len + 1, (void *) &new_index_ptr,
  2355. sizeof(zend_rsrc_list_entry), NULL) == FAILURE) {
  2356. efree(hashed_details);
  2357. RETURN_FALSE;
  2358. /* XXX Free Connection */
  2359. }
  2360. ODBCG(num_links)++;
  2361. }
  2362. efree(hashed_details);
  2363. }
  2364. /* }}} */
  2365. /* {{{ proto void odbc_close(resource connection_id)
  2366. Close an ODBC connection */
  2367. PHP_FUNCTION(odbc_close)
  2368. {
  2369. zval *pv_conn;
  2370. void *ptr;
  2371. odbc_connection *conn;
  2372. odbc_result *res;
  2373. int nument;
  2374. int i;
  2375. int type;
  2376. int is_pconn = 0;
  2377. int found_resource_type = le_conn;
  2378. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_conn) == FAILURE) {
  2379. return;
  2380. }
  2381. conn = (odbc_connection *) zend_fetch_resource(&pv_conn TSRMLS_CC, -1, "ODBC-Link", &found_resource_type, 2, le_conn, le_pconn);
  2382. if (found_resource_type==le_pconn) {
  2383. is_pconn = 1;
  2384. }
  2385. nument = zend_hash_next_free_element(&EG(regular_list));
  2386. for(i = 1; i < nument; i++){
  2387. ptr = zend_list_find(i, &type);
  2388. if(ptr && (type == le_result)){
  2389. res = (odbc_result *)ptr;
  2390. if(res->conn_ptr == conn){
  2391. zend_list_delete(i);
  2392. }
  2393. }
  2394. }
  2395. zend_list_delete(Z_LVAL_P(pv_conn));
  2396. if(is_pconn){
  2397. zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) _close_pconn_with_id, (void *) &(Z_LVAL_P(pv_conn)) TSRMLS_CC);
  2398. }
  2399. }
  2400. /* }}} */
  2401. /* {{{ proto int odbc_num_rows(resource result_id)
  2402. Get number of rows in a result */
  2403. PHP_FUNCTION(odbc_num_rows)
  2404. {
  2405. odbc_result *result;
  2406. SQLLEN rows;
  2407. zval *pv_res;
  2408. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_res) == FAILURE) {
  2409. return;
  2410. }
  2411. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  2412. SQLRowCount(result->stmt, &rows);
  2413. RETURN_LONG(rows);
  2414. }
  2415. /* }}} */
  2416. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
  2417. /* {{{ proto bool odbc_next_result(resource result_id)
  2418. Checks if multiple results are available */
  2419. PHP_FUNCTION(odbc_next_result)
  2420. {
  2421. odbc_result *result;
  2422. zval *pv_res;
  2423. int rc, i;
  2424. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_res) == FAILURE) {
  2425. return;
  2426. }
  2427. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  2428. if (result->values) {
  2429. for(i = 0; i < result->numcols; i++) {
  2430. if (result->values[i].value) {
  2431. efree(result->values[i].value);
  2432. }
  2433. }
  2434. efree(result->values);
  2435. result->values = NULL;
  2436. }
  2437. result->fetched = 0;
  2438. rc = SQLMoreResults(result->stmt);
  2439. if (rc == SQL_SUCCESS_WITH_INFO || rc == SQL_SUCCESS) {
  2440. rc = SQLFreeStmt(result->stmt, SQL_UNBIND);
  2441. SQLNumParams(result->stmt, &(result->numparams));
  2442. SQLNumResultCols(result->stmt, &(result->numcols));
  2443. if (result->numcols > 0) {
  2444. if (!odbc_bindcols(result TSRMLS_CC)) {
  2445. efree(result);
  2446. RETVAL_FALSE;
  2447. }
  2448. } else {
  2449. result->values = NULL;
  2450. }
  2451. RETURN_TRUE;
  2452. } else if (rc == SQL_NO_DATA_FOUND) {
  2453. RETURN_FALSE;
  2454. } else {
  2455. odbc_sql_error(result->conn_ptr, result->stmt, "SQLMoreResults");
  2456. RETURN_FALSE;
  2457. }
  2458. }
  2459. /* }}} */
  2460. #endif
  2461. /* {{{ proto int odbc_num_fields(resource result_id)
  2462. Get number of columns in a result */
  2463. PHP_FUNCTION(odbc_num_fields)
  2464. {
  2465. odbc_result *result;
  2466. zval *pv_res;
  2467. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pv_res) == FAILURE) {
  2468. return;
  2469. }
  2470. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  2471. RETURN_LONG(result->numcols);
  2472. }
  2473. /* }}} */
  2474. /* {{{ proto string odbc_field_name(resource result_id, int field_number)
  2475. Get a column name */
  2476. PHP_FUNCTION(odbc_field_name)
  2477. {
  2478. odbc_result *result;
  2479. zval *pv_res;
  2480. long pv_num;
  2481. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &pv_res, &pv_num) == FAILURE) {
  2482. return;
  2483. }
  2484. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  2485. if (result->numcols == 0) {
  2486. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  2487. RETURN_FALSE;
  2488. }
  2489. if (pv_num > result->numcols) {
  2490. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field index larger than number of fields");
  2491. RETURN_FALSE;
  2492. }
  2493. if (pv_num < 1) {
  2494. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field numbering starts at 1");
  2495. RETURN_FALSE;
  2496. }
  2497. RETURN_STRING(result->values[pv_num - 1].name, 1);
  2498. }
  2499. /* }}} */
  2500. /* {{{ proto string odbc_field_type(resource result_id, int field_number)
  2501. Get the datatype of a column */
  2502. PHP_FUNCTION(odbc_field_type)
  2503. {
  2504. odbc_result *result;
  2505. char tmp[32];
  2506. SQLSMALLINT tmplen;
  2507. zval *pv_res;
  2508. long pv_num;
  2509. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &pv_res, &pv_num) == FAILURE) {
  2510. return;
  2511. }
  2512. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  2513. if (result->numcols == 0) {
  2514. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  2515. RETURN_FALSE;
  2516. }
  2517. if (pv_num > result->numcols) {
  2518. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field index larger than number of fields");
  2519. RETURN_FALSE;
  2520. }
  2521. if (pv_num < 1) {
  2522. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field numbering starts at 1");
  2523. RETURN_FALSE;
  2524. }
  2525. PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, SQL_COLUMN_TYPE_NAME, tmp, 31, &tmplen, NULL);
  2526. RETURN_STRING(tmp,1)
  2527. }
  2528. /* }}} */
  2529. /* {{{ proto int odbc_field_len(resource result_id, int field_number)
  2530. Get the length (precision) of a column */
  2531. PHP_FUNCTION(odbc_field_len)
  2532. {
  2533. odbc_column_lengths(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2534. }
  2535. /* }}} */
  2536. /* {{{ proto int odbc_field_scale(resource result_id, int field_number)
  2537. Get the scale of a column */
  2538. PHP_FUNCTION(odbc_field_scale)
  2539. {
  2540. odbc_column_lengths(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2541. }
  2542. /* }}} */
  2543. /* {{{ proto int odbc_field_num(resource result_id, string field_name)
  2544. Return column number */
  2545. PHP_FUNCTION(odbc_field_num)
  2546. {
  2547. char *fname;
  2548. int i, field_ind, fname_len;
  2549. odbc_result *result;
  2550. zval *pv_res;
  2551. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pv_res, &fname, &fname_len) == FAILURE) {
  2552. return;
  2553. }
  2554. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_res, -1, "ODBC result", le_result);
  2555. if (result->numcols == 0) {
  2556. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No tuples available at this result index");
  2557. RETURN_FALSE;
  2558. }
  2559. field_ind = -1;
  2560. for(i = 0; i < result->numcols; i++) {
  2561. if (strcasecmp(result->values[i].name, fname) == 0) {
  2562. field_ind = i + 1;
  2563. }
  2564. }
  2565. if (field_ind == -1) {
  2566. RETURN_FALSE;
  2567. }
  2568. RETURN_LONG(field_ind);
  2569. }
  2570. /* }}} */
  2571. /* {{{ proto mixed odbc_autocommit(resource connection_id [, int OnOff])
  2572. Toggle autocommit mode or get status */
  2573. /* There can be problems with pconnections!*/
  2574. PHP_FUNCTION(odbc_autocommit)
  2575. {
  2576. odbc_connection *conn;
  2577. RETCODE rc;
  2578. zval *pv_conn;
  2579. long pv_onoff = 0;
  2580. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &pv_conn, &pv_onoff) == FAILURE) {
  2581. return;
  2582. }
  2583. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  2584. if (ZEND_NUM_ARGS() > 1) {
  2585. rc = SQLSetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (pv_onoff) ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF);
  2586. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2587. odbc_sql_error(conn, SQL_NULL_HSTMT, "Set autocommit");
  2588. RETURN_FALSE;
  2589. }
  2590. RETVAL_TRUE;
  2591. } else {
  2592. SQLINTEGER status;
  2593. rc = SQLGetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (PTR)&status);
  2594. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2595. odbc_sql_error(conn, SQL_NULL_HSTMT, "Get commit status");
  2596. RETURN_FALSE;
  2597. }
  2598. RETVAL_LONG((long)status);
  2599. }
  2600. }
  2601. /* }}} */
  2602. /* {{{ proto bool odbc_commit(resource connection_id)
  2603. Commit an ODBC transaction */
  2604. PHP_FUNCTION(odbc_commit)
  2605. {
  2606. odbc_transact(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2607. }
  2608. /* }}} */
  2609. /* {{{ proto bool odbc_rollback(resource connection_id)
  2610. Rollback a transaction */
  2611. PHP_FUNCTION(odbc_rollback)
  2612. {
  2613. odbc_transact(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2614. }
  2615. /* }}} */
  2616. /* {{{ php_odbc_lasterror */
  2617. static void php_odbc_lasterror(INTERNAL_FUNCTION_PARAMETERS, int mode)
  2618. {
  2619. odbc_connection *conn;
  2620. zval *pv_handle;
  2621. char *ptr;
  2622. int len;
  2623. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &pv_handle) == FAILURE) {
  2624. return;
  2625. }
  2626. if (mode == 0) { /* last state */
  2627. len = 6;
  2628. } else { /* last error message */
  2629. len = SQL_MAX_MESSAGE_LENGTH;
  2630. }
  2631. if (ZEND_NUM_ARGS() == 1) {
  2632. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_handle, -1, "ODBC-Link", le_conn, le_pconn);
  2633. ptr = ecalloc(len + 1, 1);
  2634. if (mode == 0) {
  2635. strlcpy(ptr, conn->laststate, len+1);
  2636. } else {
  2637. strlcpy(ptr, conn->lasterrormsg, len+1);
  2638. }
  2639. } else {
  2640. ptr = ecalloc(len + 1, 1);
  2641. if (mode == 0) {
  2642. strlcpy(ptr, ODBCG(laststate), len+1);
  2643. } else {
  2644. strlcpy(ptr, ODBCG(lasterrormsg), len+1);
  2645. }
  2646. }
  2647. RETVAL_STRING(ptr, 0);
  2648. }
  2649. /* }}} */
  2650. /* {{{ proto string odbc_error([resource connection_id])
  2651. Get the last error code */
  2652. PHP_FUNCTION(odbc_error)
  2653. {
  2654. php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2655. }
  2656. /* }}} */
  2657. /* {{{ proto string odbc_errormsg([resource connection_id])
  2658. Get the last error message */
  2659. PHP_FUNCTION(odbc_errormsg)
  2660. {
  2661. php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2662. }
  2663. /* }}} */
  2664. /* {{{ proto bool odbc_setoption(resource conn_id|result_id, int which, int option, int value)
  2665. Sets connection or statement options */
  2666. /* This one has to be used carefully. We can't allow to set connection options for
  2667. persistent connections. I think that SetStmtOption is of little use, since most
  2668. of those can only be specified before preparing/executing statements.
  2669. On the other hand, they can be made connection wide default through SetConnectOption
  2670. - but will be overidden by calls to SetStmtOption() in odbc_prepare/odbc_do
  2671. */
  2672. PHP_FUNCTION(odbc_setoption)
  2673. {
  2674. odbc_connection *conn;
  2675. odbc_result *result;
  2676. RETCODE rc;
  2677. zval *pv_handle;
  2678. long pv_which, pv_opt, pv_val;
  2679. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &pv_handle, &pv_which, &pv_opt, &pv_val) == FAILURE) {
  2680. return;
  2681. }
  2682. switch (pv_which) {
  2683. case 1: /* SQLSetConnectOption */
  2684. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_handle, -1, "ODBC-Link", le_conn, le_pconn);
  2685. if (conn->persistent) {
  2686. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set option for persistent connection");
  2687. RETURN_FALSE;
  2688. }
  2689. rc = SQLSetConnectOption(conn->hdbc, (unsigned short) pv_opt, pv_val);
  2690. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2691. odbc_sql_error(conn, SQL_NULL_HSTMT, "SetConnectOption");
  2692. RETURN_FALSE;
  2693. }
  2694. break;
  2695. case 2: /* SQLSetStmtOption */
  2696. ZEND_FETCH_RESOURCE(result, odbc_result *, &pv_handle, -1, "ODBC result", le_result);
  2697. rc = SQLSetStmtOption(result->stmt, (unsigned short) pv_opt, pv_val);
  2698. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2699. odbc_sql_error(result->conn_ptr, result->stmt, "SetStmtOption");
  2700. RETURN_FALSE;
  2701. }
  2702. break;
  2703. default:
  2704. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown option type");
  2705. RETURN_FALSE;
  2706. break;
  2707. }
  2708. RETURN_TRUE;
  2709. }
  2710. /* }}} */
  2711. /*
  2712. * metadata functions
  2713. */
  2714. /* {{{ proto resource odbc_tables(resource connection_id [, string qualifier [, string owner [, string name [, string table_types]]]])
  2715. Call the SQLTables function */
  2716. PHP_FUNCTION(odbc_tables)
  2717. {
  2718. zval *pv_conn;
  2719. odbc_result *result = NULL;
  2720. odbc_connection *conn;
  2721. char *cat = NULL, *schema = NULL, *table = NULL, *type = NULL;
  2722. int cat_len = 0, schema_len = 0, table_len = 0, type_len = 0;
  2723. RETCODE rc;
  2724. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  2725. &table, &table_len, &type, &type_len) == FAILURE) {
  2726. return;
  2727. }
  2728. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  2729. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2730. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2731. if (rc == SQL_INVALID_HANDLE) {
  2732. efree(result);
  2733. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2734. RETURN_FALSE;
  2735. }
  2736. if (rc == SQL_ERROR) {
  2737. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2738. efree(result);
  2739. RETURN_FALSE;
  2740. }
  2741. /* This hack is needed to access table information in Access databases (fmk) */
  2742. if (table && table_len && schema && schema_len == 0) {
  2743. schema = NULL;
  2744. }
  2745. rc = SQLTables(result->stmt,
  2746. cat, SAFE_SQL_NTS(cat),
  2747. schema, SAFE_SQL_NTS(schema),
  2748. table, SAFE_SQL_NTS(table),
  2749. type, SAFE_SQL_NTS(type));
  2750. if (rc == SQL_ERROR) {
  2751. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTables");
  2752. efree(result);
  2753. RETURN_FALSE;
  2754. }
  2755. result->numparams = 0;
  2756. SQLNumResultCols(result->stmt, &(result->numcols));
  2757. if (result->numcols > 0) {
  2758. if (!odbc_bindcols(result TSRMLS_CC)) {
  2759. efree(result);
  2760. RETURN_FALSE;
  2761. }
  2762. } else {
  2763. result->values = NULL;
  2764. }
  2765. result->conn_ptr = conn;
  2766. result->fetched = 0;
  2767. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  2768. }
  2769. /* }}} */
  2770. /* {{{ proto resource odbc_columns(resource connection_id [, string qualifier [, string owner [, string table_name [, string column_name]]]])
  2771. Returns a result identifier that can be used to fetch a list of column names in specified tables */
  2772. PHP_FUNCTION(odbc_columns)
  2773. {
  2774. zval *pv_conn;
  2775. odbc_result *result = NULL;
  2776. odbc_connection *conn;
  2777. char *cat = NULL, *schema = NULL, *table = NULL, *column = NULL;
  2778. int cat_len = 0, schema_len = 0, table_len = 0, column_len = 0;
  2779. RETCODE rc;
  2780. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  2781. &table, &table_len, &column, &column_len) == FAILURE) {
  2782. return;
  2783. }
  2784. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  2785. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2786. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2787. if (rc == SQL_INVALID_HANDLE) {
  2788. efree(result);
  2789. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2790. RETURN_FALSE;
  2791. }
  2792. if (rc == SQL_ERROR) {
  2793. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2794. efree(result);
  2795. RETURN_FALSE;
  2796. }
  2797. /*
  2798. * Needed to make MS Access happy
  2799. */
  2800. if (table && table_len && schema && schema_len == 0) {
  2801. schema = NULL;
  2802. }
  2803. rc = SQLColumns(result->stmt,
  2804. cat, (SQLSMALLINT) cat_len,
  2805. schema, (SQLSMALLINT) schema_len,
  2806. table, (SQLSMALLINT) table_len,
  2807. column, (SQLSMALLINT) column_len);
  2808. if (rc == SQL_ERROR) {
  2809. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLColumns");
  2810. efree(result);
  2811. RETURN_FALSE;
  2812. }
  2813. result->numparams = 0;
  2814. SQLNumResultCols(result->stmt, &(result->numcols));
  2815. if (result->numcols > 0) {
  2816. if (!odbc_bindcols(result TSRMLS_CC)) {
  2817. efree(result);
  2818. RETURN_FALSE;
  2819. }
  2820. } else {
  2821. result->values = NULL;
  2822. }
  2823. result->conn_ptr = conn;
  2824. result->fetched = 0;
  2825. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  2826. }
  2827. /* }}} */
  2828. #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)
  2829. /* {{{ proto resource odbc_columnprivileges(resource connection_id, string catalog, string schema, string table, string column)
  2830. Returns a result identifier that can be used to fetch a list of columns and associated privileges for the specified table */
  2831. PHP_FUNCTION(odbc_columnprivileges)
  2832. {
  2833. zval *pv_conn;
  2834. odbc_result *result = NULL;
  2835. odbc_connection *conn;
  2836. char *cat = NULL, *schema, *table, *column;
  2837. int cat_len = 0, schema_len, table_len, column_len;
  2838. RETCODE rc;
  2839. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  2840. &table, &table_len, &column, &column_len) == FAILURE) {
  2841. return;
  2842. }
  2843. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  2844. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2845. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2846. if (rc == SQL_INVALID_HANDLE) {
  2847. efree(result);
  2848. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2849. RETURN_FALSE;
  2850. }
  2851. if (rc == SQL_ERROR) {
  2852. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2853. efree(result);
  2854. RETURN_FALSE;
  2855. }
  2856. rc = SQLColumnPrivileges(result->stmt,
  2857. cat, SAFE_SQL_NTS(cat),
  2858. schema, SAFE_SQL_NTS(schema),
  2859. table, SAFE_SQL_NTS(table),
  2860. column, SAFE_SQL_NTS(column));
  2861. if (rc == SQL_ERROR) {
  2862. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLColumnPrivileges");
  2863. efree(result);
  2864. RETURN_FALSE;
  2865. }
  2866. result->numparams = 0;
  2867. SQLNumResultCols(result->stmt, &(result->numcols));
  2868. if (result->numcols > 0) {
  2869. if (!odbc_bindcols(result TSRMLS_CC)) {
  2870. efree(result);
  2871. RETURN_FALSE;
  2872. }
  2873. } else {
  2874. result->values = NULL;
  2875. }
  2876. result->conn_ptr = conn;
  2877. result->fetched = 0;
  2878. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  2879. }
  2880. /* }}} */
  2881. #endif /* HAVE_DBMAKER || HAVE_SOLID*/
  2882. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
  2883. /* {{{ proto resource odbc_foreignkeys(resource connection_id, string pk_qualifier, string pk_owner, string pk_table, string fk_qualifier, string fk_owner, string fk_table)
  2884. Returns a result identifier to either a list of foreign keys in the specified table or a list of foreign keys in other tables that refer to the primary key in the specified table */
  2885. PHP_FUNCTION(odbc_foreignkeys)
  2886. {
  2887. zval *pv_conn;
  2888. odbc_result *result = NULL;
  2889. odbc_connection *conn;
  2890. char *pcat = NULL, *pschema, *ptable, *fcat, *fschema, *ftable;
  2891. int pcat_len = 0, pschema_len, ptable_len, fcat_len, fschema_len, ftable_len;
  2892. RETCODE rc;
  2893. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!sssss", &pv_conn, &pcat, &pcat_len, &pschema, &pschema_len,
  2894. &ptable, &ptable_len, &fcat, &fcat_len, &fschema, &fschema_len, &ftable, &ftable_len) == FAILURE) {
  2895. return;
  2896. }
  2897. #if defined(HAVE_DBMAKER) || defined(HAVE_IBMDB2)
  2898. #define EMPTY_TO_NULL(xstr) \
  2899. if ((int)strlen((xstr)) == 0) (xstr) = NULL
  2900. EMPTY_TO_NULL(pcat);
  2901. EMPTY_TO_NULL(pschema);
  2902. EMPTY_TO_NULL(ptable);
  2903. EMPTY_TO_NULL(fcat);
  2904. EMPTY_TO_NULL(fschema);
  2905. EMPTY_TO_NULL(ftable);
  2906. #endif
  2907. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  2908. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2909. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2910. if (rc == SQL_INVALID_HANDLE) {
  2911. efree(result);
  2912. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2913. RETURN_FALSE;
  2914. }
  2915. if (rc == SQL_ERROR) {
  2916. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2917. efree(result);
  2918. RETURN_FALSE;
  2919. }
  2920. rc = SQLForeignKeys(result->stmt,
  2921. pcat, SAFE_SQL_NTS(pcat),
  2922. pschema, SAFE_SQL_NTS(pschema),
  2923. ptable, SAFE_SQL_NTS(ptable),
  2924. fcat, SAFE_SQL_NTS(fcat),
  2925. fschema, SAFE_SQL_NTS(fschema),
  2926. ftable, SAFE_SQL_NTS(ftable) );
  2927. if (rc == SQL_ERROR) {
  2928. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLForeignKeys");
  2929. efree(result);
  2930. RETURN_FALSE;
  2931. }
  2932. result->numparams = 0;
  2933. SQLNumResultCols(result->stmt, &(result->numcols));
  2934. if (result->numcols > 0) {
  2935. if (!odbc_bindcols(result TSRMLS_CC)) {
  2936. efree(result);
  2937. RETURN_FALSE;
  2938. }
  2939. } else {
  2940. result->values = NULL;
  2941. }
  2942. result->conn_ptr = conn;
  2943. result->fetched = 0;
  2944. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  2945. }
  2946. /* }}} */
  2947. #endif /* HAVE_SOLID */
  2948. /* {{{ proto resource odbc_gettypeinfo(resource connection_id [, int data_type])
  2949. Returns a result identifier containing information about data types supported by the data source */
  2950. PHP_FUNCTION(odbc_gettypeinfo)
  2951. {
  2952. zval *pv_conn;
  2953. long pv_data_type = SQL_ALL_TYPES;
  2954. odbc_result *result = NULL;
  2955. odbc_connection *conn;
  2956. RETCODE rc;
  2957. SQLSMALLINT data_type;
  2958. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &pv_conn, &pv_data_type) == FAILURE) {
  2959. return;
  2960. }
  2961. data_type = (SQLSMALLINT) pv_data_type;
  2962. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  2963. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2964. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2965. if (rc == SQL_INVALID_HANDLE) {
  2966. efree(result);
  2967. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2968. RETURN_FALSE;
  2969. }
  2970. if (rc == SQL_ERROR) {
  2971. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2972. efree(result);
  2973. RETURN_FALSE;
  2974. }
  2975. rc = SQLGetTypeInfo(result->stmt, data_type );
  2976. if (rc == SQL_ERROR) {
  2977. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLGetTypeInfo");
  2978. efree(result);
  2979. RETURN_FALSE;
  2980. }
  2981. result->numparams = 0;
  2982. SQLNumResultCols(result->stmt, &(result->numcols));
  2983. if (result->numcols > 0) {
  2984. if (!odbc_bindcols(result TSRMLS_CC)) {
  2985. efree(result);
  2986. RETURN_FALSE;
  2987. }
  2988. } else {
  2989. result->values = NULL;
  2990. }
  2991. result->conn_ptr = conn;
  2992. result->fetched = 0;
  2993. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  2994. }
  2995. /* }}} */
  2996. /* {{{ proto resource odbc_primarykeys(resource connection_id, string qualifier, string owner, string table)
  2997. Returns a result identifier listing the column names that comprise the primary key for a table */
  2998. PHP_FUNCTION(odbc_primarykeys)
  2999. {
  3000. zval *pv_conn;
  3001. odbc_result *result = NULL;
  3002. odbc_connection *conn;
  3003. char *cat = NULL, *schema = NULL, *table = NULL;
  3004. int cat_len = 0, schema_len, table_len;
  3005. RETCODE rc;
  3006. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &table, &table_len) == FAILURE) {
  3007. return;
  3008. }
  3009. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  3010. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  3011. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  3012. if (rc == SQL_INVALID_HANDLE) {
  3013. efree(result);
  3014. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  3015. RETURN_FALSE;
  3016. }
  3017. if (rc == SQL_ERROR) {
  3018. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  3019. efree(result);
  3020. RETURN_FALSE;
  3021. }
  3022. rc = SQLPrimaryKeys(result->stmt,
  3023. cat, SAFE_SQL_NTS(cat),
  3024. schema, SAFE_SQL_NTS(schema),
  3025. table, SAFE_SQL_NTS(table) );
  3026. if (rc == SQL_ERROR) {
  3027. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLPrimaryKeys");
  3028. efree(result);
  3029. RETURN_FALSE;
  3030. }
  3031. result->numparams = 0;
  3032. SQLNumResultCols(result->stmt, &(result->numcols));
  3033. if (result->numcols > 0) {
  3034. if (!odbc_bindcols(result TSRMLS_CC)) {
  3035. efree(result);
  3036. RETURN_FALSE;
  3037. }
  3038. } else {
  3039. result->values = NULL;
  3040. }
  3041. result->conn_ptr = conn;
  3042. result->fetched = 0;
  3043. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  3044. }
  3045. /* }}} */
  3046. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)
  3047. /* {{{ proto resource odbc_procedurecolumns(resource connection_id [, string qualifier, string owner, string proc, string column])
  3048. Returns a result identifier containing the list of input and output parameters, as well as the columns that make up the result set for the specified procedures */
  3049. PHP_FUNCTION(odbc_procedurecolumns)
  3050. {
  3051. zval *pv_conn;
  3052. odbc_result *result = NULL;
  3053. odbc_connection *conn;
  3054. char *cat = NULL, *schema = NULL, *proc = NULL, *col = NULL;
  3055. int cat_len = 0, schema_len = 0, proc_len = 0, col_len = 0;
  3056. RETCODE rc;
  3057. if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 5) {
  3058. WRONG_PARAM_COUNT;
  3059. }
  3060. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  3061. &proc, &proc_len, &col, &col_len) == FAILURE) {
  3062. return;
  3063. }
  3064. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  3065. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  3066. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  3067. if (rc == SQL_INVALID_HANDLE) {
  3068. efree(result);
  3069. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  3070. RETURN_FALSE;
  3071. }
  3072. if (rc == SQL_ERROR) {
  3073. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  3074. efree(result);
  3075. RETURN_FALSE;
  3076. }
  3077. rc = SQLProcedureColumns(result->stmt,
  3078. cat, SAFE_SQL_NTS(cat),
  3079. schema, SAFE_SQL_NTS(schema),
  3080. proc, SAFE_SQL_NTS(proc),
  3081. col, SAFE_SQL_NTS(col) );
  3082. if (rc == SQL_ERROR) {
  3083. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLProcedureColumns");
  3084. efree(result);
  3085. RETURN_FALSE;
  3086. }
  3087. result->numparams = 0;
  3088. SQLNumResultCols(result->stmt, &(result->numcols));
  3089. if (result->numcols > 0) {
  3090. if (!odbc_bindcols(result TSRMLS_CC)) {
  3091. efree(result);
  3092. RETURN_FALSE;
  3093. }
  3094. } else {
  3095. result->values = NULL;
  3096. }
  3097. result->conn_ptr = conn;
  3098. result->fetched = 0;
  3099. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  3100. }
  3101. /* }}} */
  3102. #endif /* HAVE_SOLID */
  3103. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
  3104. /* {{{ proto resource odbc_procedures(resource connection_id [, string qualifier, string owner, string name])
  3105. Returns a result identifier containg the list of procedure names in a datasource */
  3106. PHP_FUNCTION(odbc_procedures)
  3107. {
  3108. zval *pv_conn;
  3109. odbc_result *result = NULL;
  3110. odbc_connection *conn;
  3111. char *cat = NULL, *schema = NULL, *proc = NULL;
  3112. int cat_len = 0, schema_len = 0, proc_len = 0;
  3113. RETCODE rc;
  3114. if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 4) {
  3115. WRONG_PARAM_COUNT;
  3116. }
  3117. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &proc, &proc_len) == FAILURE) {
  3118. return;
  3119. }
  3120. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  3121. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  3122. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  3123. if (rc == SQL_INVALID_HANDLE) {
  3124. efree(result);
  3125. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  3126. RETURN_FALSE;
  3127. }
  3128. if (rc == SQL_ERROR) {
  3129. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  3130. efree(result);
  3131. RETURN_FALSE;
  3132. }
  3133. rc = SQLProcedures(result->stmt,
  3134. cat, SAFE_SQL_NTS(cat),
  3135. schema, SAFE_SQL_NTS(schema),
  3136. proc, SAFE_SQL_NTS(proc) );
  3137. if (rc == SQL_ERROR) {
  3138. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLProcedures");
  3139. efree(result);
  3140. RETURN_FALSE;
  3141. }
  3142. result->numparams = 0;
  3143. SQLNumResultCols(result->stmt, &(result->numcols));
  3144. if (result->numcols > 0) {
  3145. if (!odbc_bindcols(result TSRMLS_CC)) {
  3146. efree(result);
  3147. RETURN_FALSE;
  3148. }
  3149. } else {
  3150. result->values = NULL;
  3151. }
  3152. result->conn_ptr = conn;
  3153. result->fetched = 0;
  3154. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  3155. }
  3156. /* }}} */
  3157. #endif /* HAVE_SOLID */
  3158. /* {{{ proto resource odbc_specialcolumns(resource connection_id, int type, string qualifier, string owner, string table, int scope, int nullable)
  3159. Returns a result identifier containing either the optimal set of columns that uniquely identifies a row in the table or columns that are automatically updated when any value in the row is updated by a transaction */
  3160. PHP_FUNCTION(odbc_specialcolumns)
  3161. {
  3162. zval *pv_conn;
  3163. long vtype, vscope, vnullable;
  3164. odbc_result *result = NULL;
  3165. odbc_connection *conn;
  3166. char *cat = NULL, *schema = NULL, *name = NULL;
  3167. int cat_len = 0, schema_len, name_len;
  3168. SQLUSMALLINT type, scope, nullable;
  3169. RETCODE rc;
  3170. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rls!ssll", &pv_conn, &vtype, &cat, &cat_len, &schema, &schema_len,
  3171. &name, &name_len, &vscope, &vnullable) == FAILURE) {
  3172. return;
  3173. }
  3174. type = (SQLUSMALLINT) vtype;
  3175. scope = (SQLUSMALLINT) vscope;
  3176. nullable = (SQLUSMALLINT) vnullable;
  3177. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  3178. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  3179. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  3180. if (rc == SQL_INVALID_HANDLE) {
  3181. efree(result);
  3182. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  3183. RETURN_FALSE;
  3184. }
  3185. if (rc == SQL_ERROR) {
  3186. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  3187. efree(result);
  3188. RETURN_FALSE;
  3189. }
  3190. rc = SQLSpecialColumns(result->stmt,
  3191. type,
  3192. cat, SAFE_SQL_NTS(cat),
  3193. schema, SAFE_SQL_NTS(schema),
  3194. name, SAFE_SQL_NTS(name),
  3195. scope,
  3196. nullable);
  3197. if (rc == SQL_ERROR) {
  3198. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLSpecialColumns");
  3199. efree(result);
  3200. RETURN_FALSE;
  3201. }
  3202. result->numparams = 0;
  3203. SQLNumResultCols(result->stmt, &(result->numcols));
  3204. if (result->numcols > 0) {
  3205. if (!odbc_bindcols(result TSRMLS_CC)) {
  3206. efree(result);
  3207. RETURN_FALSE;
  3208. }
  3209. } else {
  3210. result->values = NULL;
  3211. }
  3212. result->conn_ptr = conn;
  3213. result->fetched = 0;
  3214. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  3215. }
  3216. /* }}} */
  3217. /* {{{ proto resource odbc_statistics(resource connection_id, string qualifier, string owner, string name, int unique, int accuracy)
  3218. Returns a result identifier that contains statistics about a single table and the indexes associated with the table */
  3219. PHP_FUNCTION(odbc_statistics)
  3220. {
  3221. zval *pv_conn;
  3222. long vunique, vreserved;
  3223. odbc_result *result = NULL;
  3224. odbc_connection *conn;
  3225. char *cat = NULL, *schema, *name;
  3226. int cat_len = 0, schema_len, name_len;
  3227. SQLUSMALLINT unique, reserved;
  3228. RETCODE rc;
  3229. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!ssll", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  3230. &name, &name_len, &vunique, &vreserved) == FAILURE) {
  3231. return;
  3232. }
  3233. unique = (SQLUSMALLINT) vunique;
  3234. reserved = (SQLUSMALLINT) vreserved;
  3235. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  3236. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  3237. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  3238. if (rc == SQL_INVALID_HANDLE) {
  3239. efree(result);
  3240. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  3241. RETURN_FALSE;
  3242. }
  3243. if (rc == SQL_ERROR) {
  3244. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  3245. efree(result);
  3246. RETURN_FALSE;
  3247. }
  3248. rc = SQLStatistics(result->stmt,
  3249. cat, SAFE_SQL_NTS(cat),
  3250. schema, SAFE_SQL_NTS(schema),
  3251. name, SAFE_SQL_NTS(name),
  3252. unique,
  3253. reserved);
  3254. if (rc == SQL_ERROR) {
  3255. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLStatistics");
  3256. efree(result);
  3257. RETURN_FALSE;
  3258. }
  3259. result->numparams = 0;
  3260. SQLNumResultCols(result->stmt, &(result->numcols));
  3261. if (result->numcols > 0) {
  3262. if (!odbc_bindcols(result TSRMLS_CC)) {
  3263. efree(result);
  3264. RETURN_FALSE;
  3265. }
  3266. } else {
  3267. result->values = NULL;
  3268. }
  3269. result->conn_ptr = conn;
  3270. result->fetched = 0;
  3271. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  3272. }
  3273. /* }}} */
  3274. #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)
  3275. /* {{{ proto resource odbc_tableprivileges(resource connection_id, string qualifier, string owner, string name)
  3276. Returns a result identifier containing a list of tables and the privileges associated with each table */
  3277. PHP_FUNCTION(odbc_tableprivileges)
  3278. {
  3279. zval *pv_conn;
  3280. odbc_result *result = NULL;
  3281. odbc_connection *conn;
  3282. char *cat = NULL, *schema = NULL, *table = NULL;
  3283. int cat_len = 0, schema_len, table_len;
  3284. RETCODE rc;
  3285. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &table, &table_len) == FAILURE) {
  3286. return;
  3287. }
  3288. ZEND_FETCH_RESOURCE2(conn, odbc_connection *, &pv_conn, -1, "ODBC-Link", le_conn, le_pconn);
  3289. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  3290. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  3291. if (rc == SQL_INVALID_HANDLE) {
  3292. efree(result);
  3293. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  3294. RETURN_FALSE;
  3295. }
  3296. if (rc == SQL_ERROR) {
  3297. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  3298. efree(result);
  3299. RETURN_FALSE;
  3300. }
  3301. rc = SQLTablePrivileges(result->stmt,
  3302. cat, SAFE_SQL_NTS(cat),
  3303. schema, SAFE_SQL_NTS(schema),
  3304. table, SAFE_SQL_NTS(table));
  3305. if (rc == SQL_ERROR) {
  3306. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTablePrivileges");
  3307. efree(result);
  3308. RETURN_FALSE;
  3309. }
  3310. result->numparams = 0;
  3311. SQLNumResultCols(result->stmt, &(result->numcols));
  3312. if (result->numcols > 0) {
  3313. if (!odbc_bindcols(result TSRMLS_CC)) {
  3314. efree(result);
  3315. RETURN_FALSE;
  3316. }
  3317. } else {
  3318. result->values = NULL;
  3319. }
  3320. result->conn_ptr = conn;
  3321. result->fetched = 0;
  3322. ZEND_REGISTER_RESOURCE(return_value, result, le_result);
  3323. }
  3324. /* }}} */
  3325. #endif /* HAVE_DBMAKER */
  3326. #endif /* HAVE_UODBC */
  3327. /*
  3328. * Local variables:
  3329. * tab-width: 4
  3330. * c-basic-offset: 4
  3331. * End:
  3332. * vim600: sw=4 ts=4 fdm=marker
  3333. * vim<600: sw=4 ts=4
  3334. */