pgsql.c 161 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Zeev Suraski <zeev@php.net> |
  14. | Jouni Ahto <jouni.ahto@exdec.fi> |
  15. | Yasuo Ohgaki <yohgaki@php.net> |
  16. | Youichi Iwakiri <yiwakiri@st.rim.or.jp> (pg_copy_*) |
  17. | Chris Kings-Lynne <chriskl@php.net> (v3 protocol) |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include <stdlib.h>
  21. #define PHP_PGSQL_PRIVATE 1
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #define SMART_STR_PREALLOC 512
  26. #include "php.h"
  27. #include "php_ini.h"
  28. #include "ext/standard/php_standard.h"
  29. #include "zend_smart_str.h"
  30. #include "ext/pcre/php_pcre.h"
  31. #ifdef PHP_WIN32
  32. # include "win32/time.h"
  33. #endif
  34. #include "php_pgsql.h"
  35. #include "php_globals.h"
  36. #include "zend_exceptions.h"
  37. #include "pgsql_arginfo.h"
  38. #ifdef HAVE_PGSQL
  39. #ifndef InvalidOid
  40. #define InvalidOid ((Oid) 0)
  41. #endif
  42. #define PGSQL_ASSOC 1<<0
  43. #define PGSQL_NUM 1<<1
  44. #define PGSQL_BOTH (PGSQL_ASSOC|PGSQL_NUM)
  45. #define PGSQL_NOTICE_LAST 1 /* Get the last notice */
  46. #define PGSQL_NOTICE_ALL 2 /* Get all notices */
  47. #define PGSQL_NOTICE_CLEAR 3 /* Remove notices */
  48. #define PGSQL_STATUS_LONG 1
  49. #define PGSQL_STATUS_STRING 2
  50. #define PGSQL_MAX_LENGTH_OF_LONG 30
  51. #define PGSQL_MAX_LENGTH_OF_DOUBLE 60
  52. #if ZEND_LONG_MAX < UINT_MAX
  53. #define PGSQL_RETURN_OID(oid) do { \
  54. if (oid > ZEND_LONG_MAX) { \
  55. RETURN_STR(zend_ulong_to_str(oid)); \
  56. } \
  57. RETURN_LONG((zend_long)oid); \
  58. } while(0)
  59. #else
  60. #define PGSQL_RETURN_OID(oid) RETURN_LONG((zend_long)oid)
  61. #endif
  62. #define CHECK_DEFAULT_LINK(x) \
  63. if ((x) == NULL) { \
  64. zend_throw_error(NULL, "No PostgreSQL connection opened yet"); \
  65. RETURN_THROWS(); \
  66. }
  67. /* This is a bit hacky as the macro usage is "link = FETCH_DEFAULT_LINK();" */
  68. #define FETCH_DEFAULT_LINK() \
  69. (PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL); \
  70. php_error_docref(NULL, E_DEPRECATED, "Automatic fetching of PostgreSQL connection is deprecated")
  71. /* Used only when creating a connection */
  72. #define FETCH_DEFAULT_LINK_NO_WARNING() \
  73. (PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL)
  74. #define CHECK_PGSQL_LINK(link_handle) \
  75. if (link_handle->conn == NULL) { \
  76. zend_throw_error(NULL, "PostgreSQL connection has already been closed"); \
  77. RETURN_THROWS(); \
  78. }
  79. #define CHECK_PGSQL_RESULT(result_handle) \
  80. if (result_handle->result == NULL) { \
  81. zend_throw_error(NULL, "PostgreSQL result has already been closed"); \
  82. RETURN_THROWS(); \
  83. }
  84. #define CHECK_PGSQL_LOB(lob) \
  85. if (lob->conn == NULL) { \
  86. zend_throw_error(NULL, "PostgreSQL large object has already been closed"); \
  87. RETURN_THROWS(); \
  88. }
  89. #ifndef HAVE_PQFREEMEM
  90. #define PQfreemem free
  91. #endif
  92. ZEND_DECLARE_MODULE_GLOBALS(pgsql)
  93. static PHP_GINIT_FUNCTION(pgsql);
  94. /* {{{ pgsql_module_entry */
  95. zend_module_entry pgsql_module_entry = {
  96. STANDARD_MODULE_HEADER,
  97. "pgsql",
  98. ext_functions,
  99. PHP_MINIT(pgsql),
  100. PHP_MSHUTDOWN(pgsql),
  101. PHP_RINIT(pgsql),
  102. PHP_RSHUTDOWN(pgsql),
  103. PHP_MINFO(pgsql),
  104. PHP_PGSQL_VERSION,
  105. PHP_MODULE_GLOBALS(pgsql),
  106. PHP_GINIT(pgsql),
  107. NULL,
  108. NULL,
  109. STANDARD_MODULE_PROPERTIES_EX
  110. };
  111. /* }}} */
  112. #ifdef COMPILE_DL_PGSQL
  113. #ifdef ZTS
  114. ZEND_TSRMLS_CACHE_DEFINE()
  115. #endif
  116. ZEND_GET_MODULE(pgsql)
  117. #endif
  118. static int le_plink;
  119. static zend_class_entry *pgsql_link_ce, *pgsql_result_ce, *pgsql_lob_ce;
  120. static zend_object_handlers pgsql_link_object_handlers, pgsql_result_object_handlers, pgsql_lob_object_handlers;
  121. static inline pgsql_link_handle *pgsql_link_from_obj(zend_object *obj) {
  122. return (pgsql_link_handle *)((char *)(obj) - XtOffsetOf(pgsql_link_handle, std));
  123. }
  124. #define Z_PGSQL_LINK_P(zv) pgsql_link_from_obj(Z_OBJ_P(zv))
  125. static zend_object *pgsql_link_create_object(zend_class_entry *class_type) {
  126. pgsql_link_handle *intern = zend_object_alloc(sizeof(pgsql_link_handle), class_type);
  127. zend_object_std_init(&intern->std, class_type);
  128. object_properties_init(&intern->std, class_type);
  129. intern->std.handlers = &pgsql_link_object_handlers;
  130. return &intern->std;
  131. }
  132. static zend_function *pgsql_link_get_constructor(zend_object *object) {
  133. zend_throw_error(NULL, "Cannot directly construct PgSql\\Connection, use pg_connect() or pg_pconnect() instead");
  134. return NULL;
  135. }
  136. static void pgsql_link_free(pgsql_link_handle *link)
  137. {
  138. PGresult *res;
  139. while ((res = PQgetResult(link->conn))) {
  140. PQclear(res);
  141. }
  142. if (!link->persistent) {
  143. PQfinish(link->conn);
  144. }
  145. PGG(num_links)--;
  146. zend_hash_del(&PGG(connections), link->hash);
  147. link->conn = NULL;
  148. zend_string_release(link->hash);
  149. if (link->notices) {
  150. zend_hash_destroy(link->notices);
  151. FREE_HASHTABLE(link->notices);
  152. link->notices = NULL;
  153. }
  154. }
  155. static void pgsql_link_free_obj(zend_object *obj)
  156. {
  157. pgsql_link_handle *link = pgsql_link_from_obj(obj);
  158. if (link->conn) {
  159. pgsql_link_free(link);
  160. }
  161. zend_object_std_dtor(&link->std);
  162. }
  163. static inline pgsql_result_handle *pgsql_result_from_obj(zend_object *obj) {
  164. return (pgsql_result_handle *)((char *)(obj) - XtOffsetOf(pgsql_result_handle, std));
  165. }
  166. #define Z_PGSQL_RESULT_P(zv) pgsql_result_from_obj(Z_OBJ_P(zv))
  167. static zend_object *pgsql_result_create_object(zend_class_entry *class_type) {
  168. pgsql_result_handle *intern = zend_object_alloc(sizeof(pgsql_result_handle), class_type);
  169. zend_object_std_init(&intern->std, class_type);
  170. object_properties_init(&intern->std, class_type);
  171. intern->std.handlers = &pgsql_result_object_handlers;
  172. return &intern->std;
  173. }
  174. static zend_function *pgsql_result_get_constructor(zend_object *object) {
  175. zend_throw_error(NULL, "Cannot directly construct PgSql\\Result, use a dedicated function instead");
  176. return NULL;
  177. }
  178. static void pgsql_result_free(pgsql_result_handle *pg_result)
  179. {
  180. PQclear(pg_result->result);
  181. pg_result->result = NULL;
  182. }
  183. static void pgsql_result_free_obj(zend_object *obj)
  184. {
  185. pgsql_result_handle *pg_result = pgsql_result_from_obj(obj);
  186. if (pg_result->result) {
  187. pgsql_result_free(pg_result);
  188. }
  189. zend_object_std_dtor(&pg_result->std);
  190. }
  191. static inline pgLofp *pgsql_lob_from_obj(zend_object *obj) {
  192. return (pgLofp *)((char *)(obj) - XtOffsetOf(pgLofp, std));
  193. }
  194. #define Z_PGSQL_LOB_P(zv) pgsql_lob_from_obj(Z_OBJ_P(zv))
  195. static zend_object *pgsql_lob_create_object(zend_class_entry *class_type) {
  196. pgLofp *intern = zend_object_alloc(sizeof(pgLofp), class_type);
  197. zend_object_std_init(&intern->std, class_type);
  198. object_properties_init(&intern->std, class_type);
  199. intern->std.handlers = &pgsql_lob_object_handlers;
  200. return &intern->std;
  201. }
  202. static zend_function *pgsql_lob_get_constructor(zend_object *object) {
  203. zend_throw_error(NULL, "Cannot directly construct PgSql\\Lob, use pg_lo_open() instead");
  204. return NULL;
  205. }
  206. static void pgsql_lob_free_obj(zend_object *obj)
  207. {
  208. pgLofp *lofp = pgsql_lob_from_obj(obj);
  209. zend_object_std_dtor(&lofp->std);
  210. }
  211. /* Compatibility definitions */
  212. #ifndef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
  213. #define pg_encoding_to_char(x) "SQL_ASCII"
  214. #endif
  215. static zend_string *_php_pgsql_trim_message(const char *message)
  216. {
  217. size_t i = strlen(message);
  218. if (i>2 && (message[i-2] == '\r' || message[i-2] == '\n') && message[i-1] == '.') {
  219. --i;
  220. }
  221. while (i>1 && (message[i-1] == '\r' || message[i-1] == '\n')) {
  222. --i;
  223. }
  224. return zend_string_init(message, i, 0);
  225. }
  226. #define PHP_PQ_ERROR(text, pgsql) { \
  227. zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql)); \
  228. php_error_docref(NULL, E_WARNING, text, ZSTR_VAL(msgbuf)); \
  229. zend_string_release(msgbuf); \
  230. } \
  231. static void php_pgsql_set_default_link(zend_object *obj)
  232. {
  233. GC_ADDREF(obj);
  234. if (PGG(default_link) != NULL) {
  235. zend_object_release(PGG(default_link));
  236. }
  237. PGG(default_link) = obj;
  238. }
  239. static void _close_pgsql_plink(zend_resource *rsrc)
  240. {
  241. PGconn *link = (PGconn *)rsrc->ptr;
  242. PGresult *res;
  243. while ((res = PQgetResult(link))) {
  244. PQclear(res);
  245. }
  246. PQfinish(link);
  247. PGG(num_persistent)--;
  248. PGG(num_links)--;
  249. rsrc->ptr = NULL;
  250. }
  251. static void _php_pgsql_notice_handler(void *l, const char *message)
  252. {
  253. if (PGG(ignore_notices)) {
  254. return;
  255. }
  256. zval tmp;
  257. pgsql_link_handle *link = (pgsql_link_handle *) l;
  258. if (!link->notices) {
  259. link->notices = zend_new_array(1);
  260. }
  261. zend_string *trimmed_message = _php_pgsql_trim_message(message);
  262. if (PGG(log_notices)) {
  263. php_error_docref(NULL, E_NOTICE, "%s", ZSTR_VAL(trimmed_message));
  264. }
  265. ZVAL_STR(&tmp, trimmed_message);
  266. zend_hash_next_index_insert(link->notices, &tmp);
  267. }
  268. static int _rollback_transactions(zval *el)
  269. {
  270. PGconn *link;
  271. PGresult *res;
  272. zend_resource *rsrc = Z_RES_P(el);
  273. if (rsrc->type != le_plink) {
  274. return ZEND_HASH_APPLY_KEEP;
  275. }
  276. link = (PGconn *) rsrc->ptr;
  277. if (PQsetnonblocking(link, 0)) {
  278. php_error_docref("ref.pgsql", E_NOTICE, "Cannot set connection to blocking mode");
  279. return -1;
  280. }
  281. while ((res = PQgetResult(link))) {
  282. PQclear(res);
  283. }
  284. if ((PQprotocolVersion(link) >= 3 && PQtransactionStatus(link) != PQTRANS_IDLE) || PQprotocolVersion(link) < 3) {
  285. int orig = PGG(ignore_notices);
  286. PGG(ignore_notices) = 1;
  287. res = PQexec(link,"ROLLBACK;");
  288. PQclear(res);
  289. PGG(ignore_notices) = orig;
  290. }
  291. return ZEND_HASH_APPLY_KEEP;
  292. }
  293. static void release_string(zval *zv)
  294. {
  295. zend_string_release((zend_string *) Z_PTR_P(zv));
  296. }
  297. static bool _php_pgsql_identifier_is_escaped(const char *identifier, size_t len) /* {{{ */
  298. {
  299. /* Handle edge case. Cannot be a escaped string */
  300. if (len <= 2) {
  301. return false;
  302. }
  303. /* Detect double quotes */
  304. if (identifier[0] == '"' && identifier[len-1] == '"') {
  305. size_t i;
  306. /* Detect wrong format of " inside of escaped string */
  307. for (i = 1; i < len-1; i++) {
  308. if (identifier[i] == '"' && (identifier[++i] != '"' || i == len-1)) {
  309. return false;
  310. }
  311. }
  312. } else {
  313. return false;
  314. }
  315. /* Escaped properly */
  316. return true;
  317. }
  318. /* {{{ PHP_INI */
  319. PHP_INI_BEGIN()
  320. STD_PHP_INI_BOOLEAN( "pgsql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateBool, allow_persistent, zend_pgsql_globals, pgsql_globals)
  321. STD_PHP_INI_ENTRY_EX("pgsql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_pgsql_globals, pgsql_globals, display_link_numbers)
  322. STD_PHP_INI_ENTRY_EX("pgsql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_pgsql_globals, pgsql_globals, display_link_numbers)
  323. STD_PHP_INI_BOOLEAN( "pgsql.auto_reset_persistent", "0", PHP_INI_SYSTEM, OnUpdateBool, auto_reset_persistent, zend_pgsql_globals, pgsql_globals)
  324. STD_PHP_INI_BOOLEAN( "pgsql.ignore_notice", "0", PHP_INI_ALL, OnUpdateBool, ignore_notices, zend_pgsql_globals, pgsql_globals)
  325. STD_PHP_INI_BOOLEAN( "pgsql.log_notice", "0", PHP_INI_ALL, OnUpdateBool, log_notices, zend_pgsql_globals, pgsql_globals)
  326. PHP_INI_END()
  327. static PHP_GINIT_FUNCTION(pgsql)
  328. {
  329. #if defined(COMPILE_DL_PGSQL) && defined(ZTS)
  330. ZEND_TSRMLS_CACHE_UPDATE();
  331. #endif
  332. memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
  333. zend_hash_init(&pgsql_globals->connections, 0, NULL, NULL, 1);
  334. }
  335. static void php_libpq_version(char *buf, size_t len)
  336. {
  337. int version = PQlibVersion();
  338. int major = version / 10000;
  339. if (major >= 10) {
  340. int minor = version % 10000;
  341. snprintf(buf, len, "%d.%d", major, minor);
  342. } else {
  343. int minor = version / 100 % 100;
  344. int revision = version % 100;
  345. snprintf(buf, len, "%d.%d.%d", major, minor, revision);
  346. }
  347. }
  348. PHP_MINIT_FUNCTION(pgsql)
  349. {
  350. char buf[16];
  351. REGISTER_INI_ENTRIES();
  352. le_plink = zend_register_list_destructors_ex(NULL, _close_pgsql_plink, "pgsql link persistent", module_number);
  353. pgsql_link_ce = register_class_PgSql_Connection();
  354. pgsql_link_ce->create_object = pgsql_link_create_object;
  355. memcpy(&pgsql_link_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  356. pgsql_link_object_handlers.offset = XtOffsetOf(pgsql_link_handle, std);
  357. pgsql_link_object_handlers.free_obj = pgsql_link_free_obj;
  358. pgsql_link_object_handlers.get_constructor = pgsql_link_get_constructor;
  359. pgsql_link_object_handlers.clone_obj = NULL;
  360. pgsql_link_object_handlers.compare = zend_objects_not_comparable;
  361. pgsql_result_ce = register_class_PgSql_Result();
  362. pgsql_result_ce->create_object = pgsql_result_create_object;
  363. memcpy(&pgsql_result_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  364. pgsql_result_object_handlers.offset = XtOffsetOf(pgsql_result_handle, std);
  365. pgsql_result_object_handlers.free_obj = pgsql_result_free_obj;
  366. pgsql_result_object_handlers.get_constructor = pgsql_result_get_constructor;
  367. pgsql_result_object_handlers.clone_obj = NULL;
  368. pgsql_result_object_handlers.compare = zend_objects_not_comparable;
  369. pgsql_lob_ce = register_class_PgSql_Lob();
  370. pgsql_lob_ce->create_object = pgsql_lob_create_object;
  371. memcpy(&pgsql_lob_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  372. pgsql_lob_object_handlers.offset = XtOffsetOf(pgLofp, std);
  373. pgsql_lob_object_handlers.free_obj = pgsql_lob_free_obj;
  374. pgsql_lob_object_handlers.get_constructor = pgsql_lob_get_constructor;
  375. pgsql_lob_object_handlers.clone_obj = NULL;
  376. pgsql_lob_object_handlers.compare = zend_objects_not_comparable;
  377. /* libpq version */
  378. php_libpq_version(buf, sizeof(buf));
  379. REGISTER_STRING_CONSTANT("PGSQL_LIBPQ_VERSION", buf, CONST_CS | CONST_PERSISTENT);
  380. REGISTER_STRING_CONSTANT("PGSQL_LIBPQ_VERSION_STR", buf, CONST_CS | CONST_PERSISTENT | CONST_DEPRECATED);
  381. /* For connection option */
  382. REGISTER_LONG_CONSTANT("PGSQL_CONNECT_FORCE_NEW", PGSQL_CONNECT_FORCE_NEW, CONST_CS | CONST_PERSISTENT);
  383. REGISTER_LONG_CONSTANT("PGSQL_CONNECT_ASYNC", PGSQL_CONNECT_ASYNC, CONST_CS | CONST_PERSISTENT);
  384. /* For pg_fetch_array() */
  385. REGISTER_LONG_CONSTANT("PGSQL_ASSOC", PGSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
  386. REGISTER_LONG_CONSTANT("PGSQL_NUM", PGSQL_NUM, CONST_CS | CONST_PERSISTENT);
  387. REGISTER_LONG_CONSTANT("PGSQL_BOTH", PGSQL_BOTH, CONST_CS | CONST_PERSISTENT);
  388. /* For pg_last_notice() */
  389. REGISTER_LONG_CONSTANT("PGSQL_NOTICE_LAST", PGSQL_NOTICE_LAST, CONST_CS | CONST_PERSISTENT);
  390. REGISTER_LONG_CONSTANT("PGSQL_NOTICE_ALL", PGSQL_NOTICE_ALL, CONST_CS | CONST_PERSISTENT);
  391. REGISTER_LONG_CONSTANT("PGSQL_NOTICE_CLEAR", PGSQL_NOTICE_CLEAR, CONST_CS | CONST_PERSISTENT);
  392. /* For pg_connection_status() */
  393. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_BAD", CONNECTION_BAD, CONST_CS | CONST_PERSISTENT);
  394. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_OK", CONNECTION_OK, CONST_CS | CONST_PERSISTENT);
  395. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_STARTED", CONNECTION_STARTED, CONST_CS | CONST_PERSISTENT);
  396. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_MADE", CONNECTION_MADE, CONST_CS | CONST_PERSISTENT);
  397. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_AWAITING_RESPONSE", CONNECTION_AWAITING_RESPONSE, CONST_CS | CONST_PERSISTENT);
  398. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_AUTH_OK", CONNECTION_AUTH_OK, CONST_CS | CONST_PERSISTENT);
  399. #ifdef CONNECTION_SSL_STARTUP
  400. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_SSL_STARTUP", CONNECTION_SSL_STARTUP, CONST_CS | CONST_PERSISTENT);
  401. #endif
  402. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_SETENV", CONNECTION_SETENV, CONST_CS | CONST_PERSISTENT);
  403. /* For pg_connect_poll() */
  404. REGISTER_LONG_CONSTANT("PGSQL_POLLING_FAILED", PGRES_POLLING_FAILED, CONST_CS | CONST_PERSISTENT);
  405. REGISTER_LONG_CONSTANT("PGSQL_POLLING_READING", PGRES_POLLING_READING, CONST_CS | CONST_PERSISTENT);
  406. REGISTER_LONG_CONSTANT("PGSQL_POLLING_WRITING", PGRES_POLLING_WRITING, CONST_CS | CONST_PERSISTENT);
  407. REGISTER_LONG_CONSTANT("PGSQL_POLLING_OK", PGRES_POLLING_OK, CONST_CS | CONST_PERSISTENT);
  408. REGISTER_LONG_CONSTANT("PGSQL_POLLING_ACTIVE", PGRES_POLLING_ACTIVE, CONST_CS | CONST_PERSISTENT);
  409. /* For pg_transaction_status() */
  410. REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_IDLE", PQTRANS_IDLE, CONST_CS | CONST_PERSISTENT);
  411. REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_ACTIVE", PQTRANS_ACTIVE, CONST_CS | CONST_PERSISTENT);
  412. REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_INTRANS", PQTRANS_INTRANS, CONST_CS | CONST_PERSISTENT);
  413. REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_INERROR", PQTRANS_INERROR, CONST_CS | CONST_PERSISTENT);
  414. REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_UNKNOWN", PQTRANS_UNKNOWN, CONST_CS | CONST_PERSISTENT);
  415. /* For pg_set_error_verbosity() */
  416. REGISTER_LONG_CONSTANT("PGSQL_ERRORS_TERSE", PQERRORS_TERSE, CONST_CS | CONST_PERSISTENT);
  417. REGISTER_LONG_CONSTANT("PGSQL_ERRORS_DEFAULT", PQERRORS_DEFAULT, CONST_CS | CONST_PERSISTENT);
  418. REGISTER_LONG_CONSTANT("PGSQL_ERRORS_VERBOSE", PQERRORS_VERBOSE, CONST_CS | CONST_PERSISTENT);
  419. /* For lo_seek() */
  420. REGISTER_LONG_CONSTANT("PGSQL_SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
  421. REGISTER_LONG_CONSTANT("PGSQL_SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
  422. REGISTER_LONG_CONSTANT("PGSQL_SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
  423. /* For pg_result_status() return value type */
  424. REGISTER_LONG_CONSTANT("PGSQL_STATUS_LONG", PGSQL_STATUS_LONG, CONST_CS | CONST_PERSISTENT);
  425. REGISTER_LONG_CONSTANT("PGSQL_STATUS_STRING", PGSQL_STATUS_STRING, CONST_CS | CONST_PERSISTENT);
  426. /* For pg_result_status() return value */
  427. REGISTER_LONG_CONSTANT("PGSQL_EMPTY_QUERY", PGRES_EMPTY_QUERY, CONST_CS | CONST_PERSISTENT);
  428. REGISTER_LONG_CONSTANT("PGSQL_COMMAND_OK", PGRES_COMMAND_OK, CONST_CS | CONST_PERSISTENT);
  429. REGISTER_LONG_CONSTANT("PGSQL_TUPLES_OK", PGRES_TUPLES_OK, CONST_CS | CONST_PERSISTENT);
  430. REGISTER_LONG_CONSTANT("PGSQL_COPY_OUT", PGRES_COPY_OUT, CONST_CS | CONST_PERSISTENT);
  431. REGISTER_LONG_CONSTANT("PGSQL_COPY_IN", PGRES_COPY_IN, CONST_CS | CONST_PERSISTENT);
  432. REGISTER_LONG_CONSTANT("PGSQL_BAD_RESPONSE", PGRES_BAD_RESPONSE, CONST_CS | CONST_PERSISTENT);
  433. REGISTER_LONG_CONSTANT("PGSQL_NONFATAL_ERROR", PGRES_NONFATAL_ERROR, CONST_CS | CONST_PERSISTENT);
  434. REGISTER_LONG_CONSTANT("PGSQL_FATAL_ERROR", PGRES_FATAL_ERROR, CONST_CS | CONST_PERSISTENT);
  435. /* For pg_result_error_field() field codes */
  436. REGISTER_LONG_CONSTANT("PGSQL_DIAG_SEVERITY", PG_DIAG_SEVERITY, CONST_CS | CONST_PERSISTENT);
  437. REGISTER_LONG_CONSTANT("PGSQL_DIAG_SQLSTATE", PG_DIAG_SQLSTATE, CONST_CS | CONST_PERSISTENT);
  438. REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_PRIMARY", PG_DIAG_MESSAGE_PRIMARY, CONST_CS | CONST_PERSISTENT);
  439. REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_DETAIL", PG_DIAG_MESSAGE_DETAIL, CONST_CS | CONST_PERSISTENT);
  440. REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_HINT", PG_DIAG_MESSAGE_HINT, CONST_CS | CONST_PERSISTENT);
  441. REGISTER_LONG_CONSTANT("PGSQL_DIAG_STATEMENT_POSITION", PG_DIAG_STATEMENT_POSITION, CONST_CS | CONST_PERSISTENT);
  442. #ifdef PG_DIAG_INTERNAL_POSITION
  443. REGISTER_LONG_CONSTANT("PGSQL_DIAG_INTERNAL_POSITION", PG_DIAG_INTERNAL_POSITION, CONST_CS | CONST_PERSISTENT);
  444. #endif
  445. #ifdef PG_DIAG_INTERNAL_QUERY
  446. REGISTER_LONG_CONSTANT("PGSQL_DIAG_INTERNAL_QUERY", PG_DIAG_INTERNAL_QUERY, CONST_CS | CONST_PERSISTENT);
  447. #endif
  448. REGISTER_LONG_CONSTANT("PGSQL_DIAG_CONTEXT", PG_DIAG_CONTEXT, CONST_CS | CONST_PERSISTENT);
  449. REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FILE", PG_DIAG_SOURCE_FILE, CONST_CS | CONST_PERSISTENT);
  450. REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_LINE", PG_DIAG_SOURCE_LINE, CONST_CS | CONST_PERSISTENT);
  451. REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FUNCTION", PG_DIAG_SOURCE_FUNCTION, CONST_CS | CONST_PERSISTENT);
  452. #ifdef PG_DIAG_SCHEMA_NAME
  453. REGISTER_LONG_CONSTANT("PGSQL_DIAG_SCHEMA_NAME", PG_DIAG_SCHEMA_NAME, CONST_CS | CONST_PERSISTENT);
  454. #endif
  455. #ifdef PG_DIAG_TABLE_NAME
  456. REGISTER_LONG_CONSTANT("PGSQL_DIAG_TABLE_NAME", PG_DIAG_TABLE_NAME, CONST_CS | CONST_PERSISTENT);
  457. #endif
  458. #ifdef PG_DIAG_COLUMN_NAME
  459. REGISTER_LONG_CONSTANT("PGSQL_DIAG_COLUMN_NAME", PG_DIAG_COLUMN_NAME, CONST_CS | CONST_PERSISTENT);
  460. #endif
  461. #ifdef PG_DIAG_DATATYPE_NAME
  462. REGISTER_LONG_CONSTANT("PGSQL_DIAG_DATATYPE_NAME", PG_DIAG_DATATYPE_NAME, CONST_CS | CONST_PERSISTENT);
  463. #endif
  464. #ifdef PG_DIAG_CONSTRAINT_NAME
  465. REGISTER_LONG_CONSTANT("PGSQL_DIAG_CONSTRAINT_NAME", PG_DIAG_CONSTRAINT_NAME, CONST_CS | CONST_PERSISTENT);
  466. #endif
  467. #ifdef PG_DIAG_SEVERITY_NONLOCALIZED
  468. REGISTER_LONG_CONSTANT("PGSQL_DIAG_SEVERITY_NONLOCALIZED", PG_DIAG_SEVERITY_NONLOCALIZED, CONST_CS | CONST_PERSISTENT);
  469. #endif
  470. /* pg_convert options */
  471. REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_DEFAULT", PGSQL_CONV_IGNORE_DEFAULT, CONST_CS | CONST_PERSISTENT);
  472. REGISTER_LONG_CONSTANT("PGSQL_CONV_FORCE_NULL", PGSQL_CONV_FORCE_NULL, CONST_CS | CONST_PERSISTENT);
  473. REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_NOT_NULL", PGSQL_CONV_IGNORE_NOT_NULL, CONST_CS | CONST_PERSISTENT);
  474. /* pg_insert/update/delete/select options */
  475. REGISTER_LONG_CONSTANT("PGSQL_DML_ESCAPE", PGSQL_DML_ESCAPE, CONST_CS | CONST_PERSISTENT);
  476. REGISTER_LONG_CONSTANT("PGSQL_DML_NO_CONV", PGSQL_DML_NO_CONV, CONST_CS | CONST_PERSISTENT);
  477. REGISTER_LONG_CONSTANT("PGSQL_DML_EXEC", PGSQL_DML_EXEC, CONST_CS | CONST_PERSISTENT);
  478. REGISTER_LONG_CONSTANT("PGSQL_DML_ASYNC", PGSQL_DML_ASYNC, CONST_CS | CONST_PERSISTENT);
  479. REGISTER_LONG_CONSTANT("PGSQL_DML_STRING", PGSQL_DML_STRING, CONST_CS | CONST_PERSISTENT);
  480. return SUCCESS;
  481. }
  482. PHP_MSHUTDOWN_FUNCTION(pgsql)
  483. {
  484. UNREGISTER_INI_ENTRIES();
  485. zend_hash_destroy(&PGG(connections));
  486. return SUCCESS;
  487. }
  488. PHP_RINIT_FUNCTION(pgsql)
  489. {
  490. PGG(default_link) = NULL;
  491. PGG(num_links) = PGG(num_persistent);
  492. zend_hash_init(&PGG(field_oids), 0, NULL, release_string, 0);
  493. zend_hash_init(&PGG(table_oids), 0, NULL, release_string, 0);
  494. return SUCCESS;
  495. }
  496. PHP_RSHUTDOWN_FUNCTION(pgsql)
  497. {
  498. if (PGG(default_link)) {
  499. zend_object_release(PGG(default_link));
  500. PGG(default_link) = NULL;
  501. }
  502. zend_hash_destroy(&PGG(field_oids));
  503. zend_hash_destroy(&PGG(table_oids));
  504. /* clean up persistent connection */
  505. zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions);
  506. return SUCCESS;
  507. }
  508. PHP_MINFO_FUNCTION(pgsql)
  509. {
  510. char buf[256];
  511. php_info_print_table_start();
  512. php_info_print_table_header(2, "PostgreSQL Support", "enabled");
  513. php_libpq_version(buf, sizeof(buf));
  514. php_info_print_table_row(2, "PostgreSQL (libpq) Version", buf);
  515. #ifdef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
  516. php_info_print_table_row(2, "Multibyte character support", "enabled");
  517. #else
  518. php_info_print_table_row(2, "Multibyte character support", "disabled");
  519. #endif
  520. snprintf(buf, sizeof(buf), ZEND_LONG_FMT, PGG(num_persistent));
  521. php_info_print_table_row(2, "Active Persistent Links", buf);
  522. snprintf(buf, sizeof(buf), ZEND_LONG_FMT, PGG(num_links));
  523. php_info_print_table_row(2, "Active Links", buf);
  524. php_info_print_table_end();
  525. DISPLAY_INI_ENTRIES();
  526. }
  527. static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  528. {
  529. char *connstring;
  530. size_t connstring_len;
  531. pgsql_link_handle *link;
  532. PGconn *pgsql;
  533. smart_str str = {0};
  534. zend_long connect_type = 0;
  535. PGresult *pg_result;
  536. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &connstring, &connstring_len, &connect_type) == FAILURE) {
  537. RETURN_THROWS();
  538. }
  539. smart_str_appends(&str, "pgsql");
  540. smart_str_appendl(&str, connstring, connstring_len);
  541. smart_str_appendc(&str, '_');
  542. /* make sure that the PGSQL_CONNECT_FORCE_NEW bit is not part of the hash so that subsequent
  543. * connections can re-use this connection. See bug #39979. */
  544. smart_str_append_long(&str, connect_type & ~PGSQL_CONNECT_FORCE_NEW);
  545. smart_str_0(&str);
  546. if (persistent && PGG(allow_persistent)) {
  547. zend_resource *le;
  548. /* try to find if we already have this link in our persistent list */
  549. if ((le = zend_hash_find_ptr(&EG(persistent_list), str.s)) == NULL) { /* we don't */
  550. if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
  551. php_error_docref(NULL, E_WARNING,
  552. "Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links));
  553. goto err;
  554. }
  555. if (PGG(max_persistent) != -1 && PGG(num_persistent) >= PGG(max_persistent)) {
  556. php_error_docref(NULL, E_WARNING,
  557. "Cannot create new link. Too many open persistent links (" ZEND_LONG_FMT ")", PGG(num_persistent));
  558. goto err;
  559. }
  560. /* create the link */
  561. pgsql = PQconnectdb(connstring);
  562. if (pgsql == NULL || PQstatus(pgsql) == CONNECTION_BAD) {
  563. PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql)
  564. if (pgsql) {
  565. PQfinish(pgsql);
  566. }
  567. goto err;
  568. }
  569. /* hash it up */
  570. if (zend_register_persistent_resource(ZSTR_VAL(str.s), ZSTR_LEN(str.s), pgsql, le_plink) == NULL) {
  571. goto err;
  572. }
  573. PGG(num_links)++;
  574. PGG(num_persistent)++;
  575. } else { /* we do */
  576. if (le->type != le_plink) {
  577. goto err;
  578. }
  579. /* ensure that the link did not die */
  580. if (PGG(auto_reset_persistent) & 1) {
  581. /* need to send & get something from backend to
  582. make sure we catch CONNECTION_BAD every time */
  583. PGresult *pg_result;
  584. pg_result = PQexec(le->ptr, "select 1");
  585. PQclear(pg_result);
  586. }
  587. if (PQstatus(le->ptr) == CONNECTION_BAD) { /* the link died */
  588. if (le->ptr == NULL) {
  589. le->ptr = PQconnectdb(connstring);
  590. }
  591. else {
  592. PQreset(le->ptr);
  593. }
  594. if (le->ptr == NULL || PQstatus(le->ptr) == CONNECTION_BAD) {
  595. php_error_docref(NULL, E_WARNING,"PostgreSQL connection lost, unable to reconnect");
  596. zend_hash_del(&EG(persistent_list), str.s);
  597. goto err;
  598. }
  599. }
  600. pgsql = (PGconn *) le->ptr;
  601. /* consider to use php_version_compare() here */
  602. if (PQprotocolVersion(pgsql) >= 3 && zend_strtod(PQparameterStatus(pgsql, "server_version"), NULL) >= 7.2) {
  603. pg_result = PQexec(pgsql, "RESET ALL;");
  604. PQclear(pg_result);
  605. }
  606. }
  607. object_init_ex(return_value, pgsql_link_ce);
  608. link = Z_PGSQL_LINK_P(return_value);
  609. link->conn = pgsql;
  610. link->hash = zend_string_copy(str.s);
  611. link->notices = NULL;
  612. link->persistent = 1;
  613. } else { /* Non persistent connection */
  614. zval *index_ptr;
  615. /* first we check the hash for the hashed_details key. If it exists,
  616. * it should point us to the right offset where the actual pgsql link sits.
  617. * if it doesn't, open a new pgsql link, add it to the resource list,
  618. * and add a pointer to it with hashed_details as the key.
  619. */
  620. if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
  621. && (index_ptr = zend_hash_find(&PGG(connections), str.s)) != NULL) {
  622. php_pgsql_set_default_link(Z_OBJ_P(index_ptr));
  623. ZVAL_COPY(return_value, index_ptr);
  624. goto cleanup;
  625. }
  626. if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
  627. php_error_docref(NULL, E_WARNING, "Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links));
  628. goto err;
  629. }
  630. /* Non-blocking connect */
  631. if (connect_type & PGSQL_CONNECT_ASYNC) {
  632. pgsql = PQconnectStart(connstring);
  633. if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
  634. PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
  635. if (pgsql) {
  636. PQfinish(pgsql);
  637. }
  638. goto err;
  639. }
  640. } else {
  641. pgsql = PQconnectdb(connstring);
  642. if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
  643. PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
  644. if (pgsql) {
  645. PQfinish(pgsql);
  646. }
  647. goto err;
  648. }
  649. }
  650. object_init_ex(return_value, pgsql_link_ce);
  651. link = Z_PGSQL_LINK_P(return_value);
  652. link->conn = pgsql;
  653. link->hash = zend_string_copy(str.s);
  654. link->notices = NULL;
  655. link->persistent = 0;
  656. /* add it to the hash */
  657. zend_hash_update(&PGG(connections), str.s, return_value);
  658. /* Keep track of link => hash mapping, so we can remove the hash entry from connections
  659. * when the connection is closed. This uses the address of the connection rather than the
  660. * zend_resource, because the resource destructor is passed a stack copy of the resource
  661. * structure. */
  662. PGG(num_links)++;
  663. }
  664. /* set notice processor */
  665. if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_OBJECT) {
  666. PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, link);
  667. }
  668. php_pgsql_set_default_link(Z_OBJ_P(return_value));
  669. cleanup:
  670. smart_str_free(&str);
  671. return;
  672. err:
  673. smart_str_free(&str);
  674. RETURN_FALSE;
  675. }
  676. /* }}} */
  677. /* {{{ Open a PostgreSQL connection */
  678. PHP_FUNCTION(pg_connect)
  679. {
  680. php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
  681. }
  682. /* }}} */
  683. /* {{{ Poll the status of an in-progress async PostgreSQL connection attempt*/
  684. PHP_FUNCTION(pg_connect_poll)
  685. {
  686. zval *pgsql_link;
  687. pgsql_link_handle *pgsql_handle;
  688. PGconn *pgsql;
  689. int ret;
  690. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  691. RETURN_THROWS();
  692. }
  693. pgsql_handle = Z_PGSQL_LINK_P(pgsql_link);
  694. CHECK_PGSQL_LINK(pgsql_handle);
  695. pgsql = pgsql_handle->conn;
  696. ret = PQconnectPoll(pgsql);
  697. RETURN_LONG(ret);
  698. }
  699. /* }}} */
  700. /* {{{ Open a persistent PostgreSQL connection */
  701. PHP_FUNCTION(pg_pconnect)
  702. {
  703. php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
  704. }
  705. /* }}} */
  706. /* {{{ Close a PostgreSQL connection */
  707. PHP_FUNCTION(pg_close)
  708. {
  709. zval *pgsql_link = NULL;
  710. pgsql_link_handle *link;
  711. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &pgsql_link, pgsql_link_ce) == FAILURE) {
  712. RETURN_THROWS();
  713. }
  714. if (!pgsql_link) {
  715. link = FETCH_DEFAULT_LINK();
  716. CHECK_DEFAULT_LINK(link);
  717. zend_object_release(PGG(default_link));
  718. PGG(default_link) = NULL;
  719. RETURN_TRUE;
  720. }
  721. link = Z_PGSQL_LINK_P(pgsql_link);
  722. CHECK_PGSQL_LINK(link);
  723. if (link == FETCH_DEFAULT_LINK_NO_WARNING()) {
  724. GC_DELREF(PGG(default_link));
  725. PGG(default_link) = NULL;
  726. }
  727. pgsql_link_free(link);
  728. RETURN_TRUE;
  729. }
  730. /* }}} */
  731. #define PHP_PG_DBNAME 1
  732. #define PHP_PG_ERROR_MESSAGE 2
  733. #define PHP_PG_OPTIONS 3
  734. #define PHP_PG_PORT 4
  735. #define PHP_PG_TTY 5
  736. #define PHP_PG_HOST 6
  737. #define PHP_PG_VERSION 7
  738. /* php_pgsql_get_link_info */
  739. static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  740. {
  741. pgsql_link_handle *link;
  742. zval *pgsql_link = NULL;
  743. PGconn *pgsql;
  744. char *result;
  745. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &pgsql_link, pgsql_link_ce) == FAILURE) {
  746. RETURN_THROWS();
  747. }
  748. if (!pgsql_link) {
  749. link = FETCH_DEFAULT_LINK();
  750. CHECK_DEFAULT_LINK(link);
  751. } else {
  752. link = Z_PGSQL_LINK_P(pgsql_link);
  753. CHECK_PGSQL_LINK(link);
  754. }
  755. pgsql = link->conn;
  756. switch(entry_type) {
  757. case PHP_PG_DBNAME:
  758. result = PQdb(pgsql);
  759. break;
  760. case PHP_PG_ERROR_MESSAGE:
  761. RETURN_STR(_php_pgsql_trim_message(PQerrorMessage(pgsql)));
  762. case PHP_PG_OPTIONS:
  763. result = PQoptions(pgsql);
  764. break;
  765. case PHP_PG_PORT:
  766. result = PQport(pgsql);
  767. break;
  768. case PHP_PG_TTY:
  769. result = PQtty(pgsql);
  770. break;
  771. case PHP_PG_HOST:
  772. result = PQhost(pgsql);
  773. break;
  774. case PHP_PG_VERSION:
  775. array_init(return_value);
  776. char buf[16];
  777. php_libpq_version(buf, sizeof(buf));
  778. add_assoc_string(return_value, "client", buf);
  779. add_assoc_long(return_value, "protocol", PQprotocolVersion(pgsql));
  780. if (PQprotocolVersion(pgsql) >= 3) {
  781. /* 8.0 or grater supports protorol version 3 */
  782. char *tmp;
  783. add_assoc_string(return_value, "server", (char*)PQparameterStatus(pgsql, "server_version"));
  784. #define PHP_PQ_COPY_PARAM(_x) tmp = (char*)PQparameterStatus(pgsql, _x); \
  785. if(tmp) add_assoc_string(return_value, _x, tmp); \
  786. else add_assoc_null(return_value, _x);
  787. PHP_PQ_COPY_PARAM("server_encoding");
  788. PHP_PQ_COPY_PARAM("client_encoding");
  789. PHP_PQ_COPY_PARAM("is_superuser");
  790. PHP_PQ_COPY_PARAM("session_authorization");
  791. PHP_PQ_COPY_PARAM("DateStyle");
  792. PHP_PQ_COPY_PARAM("IntervalStyle");
  793. PHP_PQ_COPY_PARAM("TimeZone");
  794. PHP_PQ_COPY_PARAM("integer_datetimes");
  795. PHP_PQ_COPY_PARAM("standard_conforming_strings");
  796. PHP_PQ_COPY_PARAM("application_name");
  797. }
  798. return;
  799. EMPTY_SWITCH_DEFAULT_CASE()
  800. }
  801. if (result) {
  802. RETURN_STRING(result);
  803. } else {
  804. RETURN_EMPTY_STRING();
  805. }
  806. }
  807. /* Get the database name */
  808. PHP_FUNCTION(pg_dbname)
  809. {
  810. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_DBNAME);
  811. }
  812. /* Get the error message string */
  813. PHP_FUNCTION(pg_last_error)
  814. {
  815. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_ERROR_MESSAGE);
  816. }
  817. /* Get the options associated with the connection */
  818. PHP_FUNCTION(pg_options)
  819. {
  820. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_OPTIONS);
  821. }
  822. /* Return the port number associated with the connection */
  823. PHP_FUNCTION(pg_port)
  824. {
  825. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_PORT);
  826. }
  827. /* Return the tty name associated with the connection */
  828. PHP_FUNCTION(pg_tty)
  829. {
  830. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_TTY);
  831. }
  832. /* Returns the host name associated with the connection */
  833. PHP_FUNCTION(pg_host)
  834. {
  835. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_HOST);
  836. }
  837. /* Returns an array with client, protocol and server version (when available) */
  838. PHP_FUNCTION(pg_version)
  839. {
  840. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_VERSION);
  841. }
  842. /* Returns the value of a server parameter */
  843. PHP_FUNCTION(pg_parameter_status)
  844. {
  845. zval *pgsql_link = NULL;
  846. pgsql_link_handle *link;
  847. PGconn *pgsql;
  848. char *param;
  849. size_t len;
  850. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &param, &len) == FAILURE) {
  851. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &param, &len) == FAILURE) {
  852. RETURN_THROWS();
  853. }
  854. link = FETCH_DEFAULT_LINK();
  855. CHECK_DEFAULT_LINK(link);
  856. } else {
  857. link = Z_PGSQL_LINK_P(pgsql_link);
  858. CHECK_PGSQL_LINK(link);
  859. }
  860. pgsql = link->conn;
  861. param = (char*)PQparameterStatus(pgsql, param);
  862. if (param) {
  863. RETURN_STRING(param);
  864. } else {
  865. RETURN_FALSE;
  866. }
  867. }
  868. /* Ping database. If connection is bad, try to reconnect. */
  869. PHP_FUNCTION(pg_ping)
  870. {
  871. zval *pgsql_link = NULL;
  872. PGconn *pgsql;
  873. PGresult *res;
  874. pgsql_link_handle *link;
  875. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &pgsql_link, pgsql_link_ce) == FAILURE) {
  876. RETURN_THROWS();
  877. }
  878. if (pgsql_link == NULL) {
  879. link = FETCH_DEFAULT_LINK();
  880. CHECK_DEFAULT_LINK(link);
  881. } else {
  882. link = Z_PGSQL_LINK_P(pgsql_link);
  883. CHECK_PGSQL_LINK(link);
  884. }
  885. pgsql = link->conn;
  886. /* ping connection */
  887. res = PQexec(pgsql, "SELECT 1;");
  888. PQclear(res);
  889. /* check status. */
  890. if (PQstatus(pgsql) == CONNECTION_OK)
  891. RETURN_TRUE;
  892. /* reset connection if it's broken */
  893. PQreset(pgsql);
  894. if (PQstatus(pgsql) == CONNECTION_OK) {
  895. RETURN_TRUE;
  896. }
  897. RETURN_FALSE;
  898. }
  899. /* Execute a query */
  900. PHP_FUNCTION(pg_query)
  901. {
  902. zval *pgsql_link = NULL;
  903. char *query;
  904. size_t query_len;
  905. int leftover = 0;
  906. pgsql_link_handle *link;
  907. PGconn *pgsql;
  908. PGresult *pgsql_result;
  909. ExecStatusType status;
  910. if (ZEND_NUM_ARGS() == 1) {
  911. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query, &query_len) == FAILURE) {
  912. RETURN_THROWS();
  913. }
  914. link = FETCH_DEFAULT_LINK();
  915. CHECK_DEFAULT_LINK(link);
  916. } else {
  917. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &query, &query_len) == FAILURE) {
  918. RETURN_THROWS();
  919. }
  920. link = Z_PGSQL_LINK_P(pgsql_link);
  921. CHECK_PGSQL_LINK(link);
  922. }
  923. pgsql = link->conn;
  924. if (PQsetnonblocking(pgsql, 0)) {
  925. php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
  926. RETURN_FALSE;
  927. }
  928. while ((pgsql_result = PQgetResult(pgsql))) {
  929. PQclear(pgsql_result);
  930. leftover = 1;
  931. }
  932. if (leftover) {
  933. php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
  934. }
  935. pgsql_result = PQexec(pgsql, query);
  936. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
  937. PQclear(pgsql_result);
  938. PQreset(pgsql);
  939. pgsql_result = PQexec(pgsql, query);
  940. }
  941. if (pgsql_result) {
  942. status = PQresultStatus(pgsql_result);
  943. } else {
  944. status = (ExecStatusType) PQstatus(pgsql);
  945. }
  946. switch (status) {
  947. case PGRES_EMPTY_QUERY:
  948. case PGRES_BAD_RESPONSE:
  949. case PGRES_NONFATAL_ERROR:
  950. case PGRES_FATAL_ERROR:
  951. PHP_PQ_ERROR("Query failed: %s", pgsql);
  952. PQclear(pgsql_result);
  953. RETURN_FALSE;
  954. break;
  955. case PGRES_COMMAND_OK: /* successful command that did not return rows */
  956. default:
  957. if (pgsql_result) {
  958. object_init_ex(return_value, pgsql_result_ce);
  959. pgsql_result_handle *pg_result = Z_PGSQL_RESULT_P(return_value);
  960. pg_result->conn = pgsql;
  961. pg_result->result = pgsql_result;
  962. pg_result->row = 0;
  963. } else {
  964. PQclear(pgsql_result);
  965. RETURN_FALSE;
  966. }
  967. break;
  968. }
  969. }
  970. static void _php_pgsql_free_params(char **params, int num_params)
  971. {
  972. if (num_params > 0) {
  973. int i;
  974. for (i = 0; i < num_params; i++) {
  975. if (params[i]) {
  976. efree(params[i]);
  977. }
  978. }
  979. efree(params);
  980. }
  981. }
  982. /* Execute a query */
  983. PHP_FUNCTION(pg_query_params)
  984. {
  985. zval *pgsql_link = NULL;
  986. zval *pv_param_arr, *tmp;
  987. char *query;
  988. size_t query_len;
  989. int leftover = 0;
  990. int num_params = 0;
  991. char **params = NULL;
  992. pgsql_link_handle *link;
  993. PGconn *pgsql;
  994. PGresult *pgsql_result;
  995. ExecStatusType status;
  996. pgsql_result_handle *pg_result;
  997. if (ZEND_NUM_ARGS() == 2) {
  998. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa", &query, &query_len, &pv_param_arr) == FAILURE) {
  999. RETURN_THROWS();
  1000. }
  1001. link = FETCH_DEFAULT_LINK();
  1002. CHECK_DEFAULT_LINK(link);
  1003. } else {
  1004. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osa", &pgsql_link, pgsql_link_ce, &query, &query_len, &pv_param_arr) == FAILURE) {
  1005. RETURN_THROWS();
  1006. }
  1007. link = Z_PGSQL_LINK_P(pgsql_link);
  1008. CHECK_PGSQL_LINK(link);
  1009. }
  1010. pgsql = link->conn;
  1011. if (PQsetnonblocking(pgsql, 0)) {
  1012. php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
  1013. RETURN_FALSE;
  1014. }
  1015. while ((pgsql_result = PQgetResult(pgsql))) {
  1016. PQclear(pgsql_result);
  1017. leftover = 1;
  1018. }
  1019. if (leftover) {
  1020. php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
  1021. }
  1022. num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
  1023. if (num_params > 0) {
  1024. int i = 0;
  1025. params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
  1026. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) {
  1027. ZVAL_DEREF(tmp);
  1028. if (Z_TYPE_P(tmp) == IS_NULL) {
  1029. params[i] = NULL;
  1030. } else {
  1031. zend_string *param_str = zval_try_get_string(tmp);
  1032. if (!param_str) {
  1033. _php_pgsql_free_params(params, i);
  1034. RETURN_THROWS();
  1035. }
  1036. params[i] = estrndup(ZSTR_VAL(param_str), ZSTR_LEN(param_str));
  1037. zend_string_release(param_str);
  1038. }
  1039. i++;
  1040. } ZEND_HASH_FOREACH_END();
  1041. }
  1042. pgsql_result = PQexecParams(pgsql, query, num_params,
  1043. NULL, (const char * const *)params, NULL, NULL, 0);
  1044. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
  1045. PQclear(pgsql_result);
  1046. PQreset(pgsql);
  1047. pgsql_result = PQexecParams(pgsql, query, num_params,
  1048. NULL, (const char * const *)params, NULL, NULL, 0);
  1049. }
  1050. if (pgsql_result) {
  1051. status = PQresultStatus(pgsql_result);
  1052. } else {
  1053. status = (ExecStatusType) PQstatus(pgsql);
  1054. }
  1055. _php_pgsql_free_params(params, num_params);
  1056. switch (status) {
  1057. case PGRES_EMPTY_QUERY:
  1058. case PGRES_BAD_RESPONSE:
  1059. case PGRES_NONFATAL_ERROR:
  1060. case PGRES_FATAL_ERROR:
  1061. PHP_PQ_ERROR("Query failed: %s", pgsql);
  1062. PQclear(pgsql_result);
  1063. RETURN_FALSE;
  1064. break;
  1065. case PGRES_COMMAND_OK: /* successful command that did not return rows */
  1066. default:
  1067. if (pgsql_result) {
  1068. object_init_ex(return_value, pgsql_result_ce);
  1069. pg_result = Z_PGSQL_RESULT_P(return_value);
  1070. pg_result->conn = pgsql;
  1071. pg_result->result = pgsql_result;
  1072. pg_result->row = 0;
  1073. } else {
  1074. PQclear(pgsql_result);
  1075. RETURN_FALSE;
  1076. }
  1077. break;
  1078. }
  1079. }
  1080. /* Prepare a query for future execution */
  1081. PHP_FUNCTION(pg_prepare)
  1082. {
  1083. zval *pgsql_link = NULL;
  1084. char *query, *stmtname;
  1085. size_t query_len, stmtname_len;
  1086. int leftover = 0;
  1087. PGconn *pgsql;
  1088. pgsql_link_handle *link;
  1089. PGresult *pgsql_result;
  1090. ExecStatusType status;
  1091. pgsql_result_handle *pg_result;
  1092. if (ZEND_NUM_ARGS() == 2) {
  1093. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
  1094. RETURN_THROWS();
  1095. }
  1096. link = FETCH_DEFAULT_LINK();
  1097. CHECK_DEFAULT_LINK(link);
  1098. } else {
  1099. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss", &pgsql_link, pgsql_link_ce, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
  1100. RETURN_THROWS();
  1101. }
  1102. link = Z_PGSQL_LINK_P(pgsql_link);
  1103. CHECK_PGSQL_LINK(link);
  1104. }
  1105. pgsql = link->conn;
  1106. if (PQsetnonblocking(pgsql, 0)) {
  1107. php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
  1108. RETURN_FALSE;
  1109. }
  1110. while ((pgsql_result = PQgetResult(pgsql))) {
  1111. PQclear(pgsql_result);
  1112. leftover = 1;
  1113. }
  1114. if (leftover) {
  1115. php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
  1116. }
  1117. pgsql_result = PQprepare(pgsql, stmtname, query, 0, NULL);
  1118. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
  1119. PQclear(pgsql_result);
  1120. PQreset(pgsql);
  1121. pgsql_result = PQprepare(pgsql, stmtname, query, 0, NULL);
  1122. }
  1123. if (pgsql_result) {
  1124. status = PQresultStatus(pgsql_result);
  1125. } else {
  1126. status = (ExecStatusType) PQstatus(pgsql);
  1127. }
  1128. switch (status) {
  1129. case PGRES_EMPTY_QUERY:
  1130. case PGRES_BAD_RESPONSE:
  1131. case PGRES_NONFATAL_ERROR:
  1132. case PGRES_FATAL_ERROR:
  1133. PHP_PQ_ERROR("Query failed: %s", pgsql);
  1134. PQclear(pgsql_result);
  1135. RETURN_FALSE;
  1136. break;
  1137. case PGRES_COMMAND_OK: /* successful command that did not return rows */
  1138. default:
  1139. if (pgsql_result) {
  1140. object_init_ex(return_value, pgsql_result_ce);
  1141. pg_result = Z_PGSQL_RESULT_P(return_value);
  1142. pg_result->conn = pgsql;
  1143. pg_result->result = pgsql_result;
  1144. pg_result->row = 0;
  1145. } else {
  1146. PQclear(pgsql_result);
  1147. RETURN_FALSE;
  1148. }
  1149. break;
  1150. }
  1151. }
  1152. /* Execute a prepared query */
  1153. PHP_FUNCTION(pg_execute)
  1154. {
  1155. zval *pgsql_link = NULL;
  1156. zval *pv_param_arr, *tmp;
  1157. char *stmtname;
  1158. size_t stmtname_len;
  1159. int leftover = 0;
  1160. int num_params = 0;
  1161. char **params = NULL;
  1162. PGconn *pgsql;
  1163. pgsql_link_handle *link;
  1164. PGresult *pgsql_result;
  1165. ExecStatusType status;
  1166. pgsql_result_handle *pg_result;
  1167. if (ZEND_NUM_ARGS() == 2) {
  1168. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa", &stmtname, &stmtname_len, &pv_param_arr)==FAILURE) {
  1169. RETURN_THROWS();
  1170. }
  1171. link = FETCH_DEFAULT_LINK();
  1172. CHECK_DEFAULT_LINK(link);
  1173. } else {
  1174. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osa", &pgsql_link, pgsql_link_ce, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
  1175. RETURN_THROWS();
  1176. }
  1177. link = Z_PGSQL_LINK_P(pgsql_link);
  1178. CHECK_PGSQL_LINK(link);
  1179. }
  1180. pgsql = link->conn;
  1181. if (PQsetnonblocking(pgsql, 0)) {
  1182. php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
  1183. RETURN_FALSE;
  1184. }
  1185. while ((pgsql_result = PQgetResult(pgsql))) {
  1186. PQclear(pgsql_result);
  1187. leftover = 1;
  1188. }
  1189. if (leftover) {
  1190. php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
  1191. }
  1192. num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
  1193. if (num_params > 0) {
  1194. int i = 0;
  1195. params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
  1196. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) {
  1197. if (Z_TYPE_P(tmp) == IS_NULL) {
  1198. params[i] = NULL;
  1199. } else {
  1200. zend_string *tmp_str;
  1201. zend_string *str = zval_get_tmp_string(tmp, &tmp_str);
  1202. params[i] = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
  1203. zend_tmp_string_release(tmp_str);
  1204. }
  1205. i++;
  1206. } ZEND_HASH_FOREACH_END();
  1207. }
  1208. pgsql_result = PQexecPrepared(pgsql, stmtname, num_params,
  1209. (const char * const *)params, NULL, NULL, 0);
  1210. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
  1211. PQclear(pgsql_result);
  1212. PQreset(pgsql);
  1213. pgsql_result = PQexecPrepared(pgsql, stmtname, num_params,
  1214. (const char * const *)params, NULL, NULL, 0);
  1215. }
  1216. if (pgsql_result) {
  1217. status = PQresultStatus(pgsql_result);
  1218. } else {
  1219. status = (ExecStatusType) PQstatus(pgsql);
  1220. }
  1221. _php_pgsql_free_params(params, num_params);
  1222. switch (status) {
  1223. case PGRES_EMPTY_QUERY:
  1224. case PGRES_BAD_RESPONSE:
  1225. case PGRES_NONFATAL_ERROR:
  1226. case PGRES_FATAL_ERROR:
  1227. PHP_PQ_ERROR("Query failed: %s", pgsql);
  1228. PQclear(pgsql_result);
  1229. RETURN_FALSE;
  1230. break;
  1231. case PGRES_COMMAND_OK: /* successful command that did not return rows */
  1232. default:
  1233. if (pgsql_result) {
  1234. object_init_ex(return_value, pgsql_result_ce);
  1235. pg_result = Z_PGSQL_RESULT_P(return_value);
  1236. pg_result->conn = pgsql;
  1237. pg_result->result = pgsql_result;
  1238. pg_result->row = 0;
  1239. } else {
  1240. PQclear(pgsql_result);
  1241. RETURN_FALSE;
  1242. }
  1243. break;
  1244. }
  1245. }
  1246. #define PHP_PG_NUM_ROWS 1
  1247. #define PHP_PG_NUM_FIELDS 2
  1248. #define PHP_PG_CMD_TUPLES 3
  1249. /* php_pgsql_get_result_info */
  1250. static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  1251. {
  1252. zval *result;
  1253. PGresult *pgsql_result;
  1254. pgsql_result_handle *pg_result;
  1255. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &result, pgsql_result_ce) == FAILURE) {
  1256. RETURN_THROWS();
  1257. }
  1258. pg_result = Z_PGSQL_RESULT_P(result);
  1259. CHECK_PGSQL_RESULT(pg_result);
  1260. pgsql_result = pg_result->result;
  1261. switch (entry_type) {
  1262. case PHP_PG_NUM_ROWS:
  1263. RETVAL_LONG(PQntuples(pgsql_result));
  1264. break;
  1265. case PHP_PG_NUM_FIELDS:
  1266. RETVAL_LONG(PQnfields(pgsql_result));
  1267. break;
  1268. case PHP_PG_CMD_TUPLES:
  1269. RETVAL_LONG(atoi(PQcmdTuples(pgsql_result)));
  1270. break;
  1271. EMPTY_SWITCH_DEFAULT_CASE()
  1272. }
  1273. }
  1274. /* Return the number of rows in the result */
  1275. PHP_FUNCTION(pg_num_rows)
  1276. {
  1277. php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_ROWS);
  1278. }
  1279. /* Return the number of fields in the result */
  1280. PHP_FUNCTION(pg_num_fields)
  1281. {
  1282. php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_FIELDS);
  1283. }
  1284. /* Returns the number of affected tuples */
  1285. PHP_FUNCTION(pg_affected_rows)
  1286. {
  1287. php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_CMD_TUPLES);
  1288. }
  1289. /* Returns the last notice set by the backend */
  1290. PHP_FUNCTION(pg_last_notice)
  1291. {
  1292. zval *pgsql_link = NULL;
  1293. zval *notice;
  1294. HashTable *notices;
  1295. pgsql_link_handle *link;
  1296. zend_long option = PGSQL_NOTICE_LAST;
  1297. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &pgsql_link, pgsql_link_ce, &option) == FAILURE) {
  1298. RETURN_THROWS();
  1299. }
  1300. link = Z_PGSQL_LINK_P(pgsql_link);
  1301. CHECK_PGSQL_LINK(link);
  1302. notices = link->notices;
  1303. switch (option) {
  1304. case PGSQL_NOTICE_LAST:
  1305. if (notices) {
  1306. zend_hash_internal_pointer_end(notices);
  1307. if ((notice = zend_hash_get_current_data(notices)) == NULL) {
  1308. RETURN_EMPTY_STRING();
  1309. }
  1310. RETURN_COPY(notice);
  1311. } else {
  1312. RETURN_EMPTY_STRING();
  1313. }
  1314. break;
  1315. case PGSQL_NOTICE_ALL:
  1316. if (notices) {
  1317. RETURN_ARR(zend_array_dup(notices));
  1318. } else {
  1319. array_init(return_value);
  1320. return;
  1321. }
  1322. break;
  1323. case PGSQL_NOTICE_CLEAR:
  1324. if (notices) {
  1325. zend_hash_clean(notices);
  1326. }
  1327. RETURN_TRUE;
  1328. break;
  1329. default:
  1330. zend_argument_value_error(2, "must be one of PGSQL_NOTICE_LAST, PGSQL_NOTICE_ALL, or PGSQL_NOTICE_CLEAR");
  1331. RETURN_THROWS();
  1332. }
  1333. RETURN_FALSE;
  1334. }
  1335. static inline bool is_valid_oid_string(zend_string *oid, Oid *return_oid)
  1336. {
  1337. char *end_ptr;
  1338. *return_oid = (Oid) strtoul(ZSTR_VAL(oid), &end_ptr, 10);
  1339. return ZSTR_VAL(oid) + ZSTR_LEN(oid) == end_ptr;
  1340. }
  1341. static zend_string *get_field_name(PGconn *pgsql, Oid oid)
  1342. {
  1343. zend_string *ret = zend_hash_index_find_ptr(&PGG(field_oids), oid);
  1344. if (ret) {
  1345. zend_string_addref(ret);
  1346. return ret;
  1347. }
  1348. PGresult *result = PQexec(pgsql, "select oid,typname from pg_type");
  1349. if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
  1350. if (result) {
  1351. PQclear(result);
  1352. }
  1353. return ZSTR_EMPTY_ALLOC();
  1354. }
  1355. int num_rows = PQntuples(result);
  1356. int oid_offset = PQfnumber(result,"oid");
  1357. int name_offset = PQfnumber(result,"typname");
  1358. for (int i = 0; i < num_rows; i++) {
  1359. char *tmp_oid_str = PQgetvalue(result, i, oid_offset);
  1360. if (!tmp_oid_str) {
  1361. continue;
  1362. }
  1363. char *tmp_name = PQgetvalue(result, i, name_offset);
  1364. if (!tmp_name) {
  1365. continue;
  1366. }
  1367. char *end_ptr;
  1368. Oid tmp_oid = strtoul(tmp_oid_str, &end_ptr, 10);
  1369. zend_string *name = zend_string_init(tmp_name, strlen(tmp_name), 0);
  1370. zend_hash_index_update_ptr(&PGG(field_oids), tmp_oid, name);
  1371. if (!ret && tmp_oid == oid) {
  1372. ret = zend_string_copy(name);
  1373. }
  1374. }
  1375. PQclear(result);
  1376. return ret;
  1377. }
  1378. /* Returns the name of the table field belongs to, or table's oid if oid_only is true */
  1379. PHP_FUNCTION(pg_field_table)
  1380. {
  1381. zval *result;
  1382. pgsql_result_handle *pg_result;
  1383. zend_long fnum = -1;
  1384. bool return_oid = 0;
  1385. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|b", &result, pgsql_result_ce, &fnum, &return_oid) == FAILURE) {
  1386. RETURN_THROWS();
  1387. }
  1388. pg_result = Z_PGSQL_RESULT_P(result);
  1389. CHECK_PGSQL_RESULT(pg_result);
  1390. if (fnum < 0) {
  1391. zend_argument_value_error(2, "must be greater than or equal to 0");
  1392. RETURN_THROWS();
  1393. }
  1394. if (fnum >= PQnfields(pg_result->result)) {
  1395. zend_argument_value_error(2, "must be less than the number of fields for this result set");
  1396. RETURN_THROWS();
  1397. }
  1398. Oid oid = PQftable(pg_result->result, (int)fnum);
  1399. if (InvalidOid == oid) {
  1400. RETURN_FALSE;
  1401. }
  1402. if (return_oid) {
  1403. PGSQL_RETURN_OID(oid);
  1404. }
  1405. zend_string *field_table = zend_hash_index_find_ptr(&PGG(table_oids), oid);
  1406. if (field_table) {
  1407. RETURN_STR_COPY(field_table);
  1408. }
  1409. /* Not found, lookup by querying PostgreSQL system tables */
  1410. smart_str querystr = {0};
  1411. smart_str_appends(&querystr, "select relname from pg_class where oid=");
  1412. smart_str_append_unsigned(&querystr, oid);
  1413. smart_str_0(&querystr);
  1414. PGresult *tmp_res = PQexec(pg_result->conn, ZSTR_VAL(querystr.s));
  1415. smart_str_free(&querystr);
  1416. if (!tmp_res || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
  1417. if (tmp_res) {
  1418. PQclear(tmp_res);
  1419. }
  1420. RETURN_FALSE;
  1421. }
  1422. char *table_name = PQgetvalue(tmp_res, 0, 0);
  1423. if (!table_name) {
  1424. PQclear(tmp_res);
  1425. RETURN_FALSE;
  1426. }
  1427. field_table = zend_string_init(table_name, strlen(table_name), 0);
  1428. zend_hash_index_update_ptr(&PGG(table_oids), oid, field_table);
  1429. PQclear(tmp_res);
  1430. RETURN_STR_COPY(field_table);
  1431. }
  1432. /* }}} */
  1433. #define PHP_PG_FIELD_NAME 1
  1434. #define PHP_PG_FIELD_SIZE 2
  1435. #define PHP_PG_FIELD_TYPE 3
  1436. #define PHP_PG_FIELD_TYPE_OID 4
  1437. /* {{{ php_pgsql_get_field_info */
  1438. static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  1439. {
  1440. zval *result;
  1441. zend_long field;
  1442. PGresult *pgsql_result;
  1443. pgsql_result_handle *pg_result;
  1444. Oid oid;
  1445. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &result, pgsql_result_ce, &field) == FAILURE) {
  1446. RETURN_THROWS();
  1447. }
  1448. pg_result = Z_PGSQL_RESULT_P(result);
  1449. CHECK_PGSQL_RESULT(pg_result);
  1450. if (field < 0) {
  1451. zend_argument_value_error(2, "must be greater than or equal to 0");
  1452. RETURN_THROWS();
  1453. }
  1454. pgsql_result = pg_result->result;
  1455. if (field >= PQnfields(pgsql_result)) {
  1456. zend_argument_value_error(2, "must be less than the number of fields for this result set");
  1457. RETURN_THROWS();
  1458. }
  1459. switch (entry_type) {
  1460. case PHP_PG_FIELD_NAME:
  1461. RETURN_STRING(PQfname(pgsql_result, (int)field));
  1462. break;
  1463. case PHP_PG_FIELD_SIZE:
  1464. RETURN_LONG(PQfsize(pgsql_result, (int)field));
  1465. break;
  1466. case PHP_PG_FIELD_TYPE:
  1467. RETURN_STR(get_field_name(pg_result->conn, PQftype(pgsql_result, (int)field)));
  1468. break;
  1469. case PHP_PG_FIELD_TYPE_OID:
  1470. oid = PQftype(pgsql_result, (int)field);
  1471. PGSQL_RETURN_OID(oid);
  1472. break;
  1473. EMPTY_SWITCH_DEFAULT_CASE()
  1474. }
  1475. }
  1476. /* }}} */
  1477. /* {{{ Returns the name of the field */
  1478. PHP_FUNCTION(pg_field_name)
  1479. {
  1480. php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_NAME);
  1481. }
  1482. /* }}} */
  1483. /* {{{ Returns the internal size of the field */
  1484. PHP_FUNCTION(pg_field_size)
  1485. {
  1486. php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_SIZE);
  1487. }
  1488. /* }}} */
  1489. /* {{{ Returns the type name for the given field */
  1490. PHP_FUNCTION(pg_field_type)
  1491. {
  1492. php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE);
  1493. }
  1494. /* }}} */
  1495. /* {{{ Returns the type oid for the given field */
  1496. PHP_FUNCTION(pg_field_type_oid)
  1497. {
  1498. php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE_OID);
  1499. }
  1500. /* }}} */
  1501. /* {{{ Returns the field number of the named field */
  1502. PHP_FUNCTION(pg_field_num)
  1503. {
  1504. zval *result;
  1505. char *field;
  1506. size_t field_len;
  1507. PGresult *pgsql_result;
  1508. pgsql_result_handle *pg_result;
  1509. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &result, pgsql_result_ce, &field, &field_len) == FAILURE) {
  1510. RETURN_THROWS();
  1511. }
  1512. pg_result = Z_PGSQL_RESULT_P(result);
  1513. CHECK_PGSQL_RESULT(pg_result);
  1514. pgsql_result = pg_result->result;
  1515. RETURN_LONG(PQfnumber(pgsql_result, field));
  1516. }
  1517. /* }}} */
  1518. static zend_long field_arg_to_offset(
  1519. PGresult *result, zend_string *field_name, zend_long field_offset, int arg_num) {
  1520. if (field_name) {
  1521. field_offset = PQfnumber(result, ZSTR_VAL(field_name));
  1522. if (field_offset < 0) {
  1523. /* Avoid displaying the argument name, as the signature is overloaded and the name
  1524. * might not line up. */
  1525. zend_value_error("Argument #%d must be a field name from this result set", arg_num);
  1526. return -1;
  1527. }
  1528. } else {
  1529. if (field_offset < 0) {
  1530. zend_value_error("Argument #%d must be greater than or equal to 0", arg_num);
  1531. return -1;
  1532. }
  1533. if (field_offset >= PQnfields(result)) {
  1534. zend_value_error("Argument #%d must be less than the number of fields for this result set", arg_num);
  1535. return -1;
  1536. }
  1537. }
  1538. return field_offset;
  1539. }
  1540. /* {{{ Returns values from a result identifier */
  1541. PHP_FUNCTION(pg_fetch_result)
  1542. {
  1543. zval *result;
  1544. zend_string *field_name;
  1545. zend_long row, field_offset;
  1546. PGresult *pgsql_result;
  1547. pgsql_result_handle *pg_result;
  1548. int pgsql_row;
  1549. if (ZEND_NUM_ARGS() == 2) {
  1550. ZEND_PARSE_PARAMETERS_START(2, 2)
  1551. Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
  1552. Z_PARAM_STR_OR_LONG(field_name, field_offset)
  1553. ZEND_PARSE_PARAMETERS_END();
  1554. } else {
  1555. ZEND_PARSE_PARAMETERS_START(3, 3)
  1556. Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
  1557. Z_PARAM_LONG(row)
  1558. Z_PARAM_STR_OR_LONG(field_name, field_offset)
  1559. ZEND_PARSE_PARAMETERS_END();
  1560. }
  1561. pg_result = Z_PGSQL_RESULT_P(result);
  1562. CHECK_PGSQL_RESULT(pg_result);
  1563. pgsql_result = pg_result->result;
  1564. if (ZEND_NUM_ARGS() == 2) {
  1565. if (pg_result->row < 0) {
  1566. pg_result->row = 0;
  1567. }
  1568. pgsql_row = pg_result->row;
  1569. if (pgsql_row >= PQntuples(pgsql_result)) {
  1570. RETURN_FALSE;
  1571. }
  1572. pg_result->row++;
  1573. } else {
  1574. if (row < 0) {
  1575. zend_argument_value_error(2, "must be greater than or equal to 0");
  1576. RETURN_THROWS();
  1577. }
  1578. if (row >= PQntuples(pgsql_result)) {
  1579. php_error_docref(NULL, E_WARNING, "Unable to jump to row " ZEND_LONG_FMT " on PostgreSQL result index " ZEND_LONG_FMT,
  1580. row, Z_LVAL_P(result));
  1581. RETURN_FALSE;
  1582. }
  1583. pgsql_row = (int)row;
  1584. }
  1585. field_offset = field_arg_to_offset(pgsql_result, field_name, field_offset, ZEND_NUM_ARGS());
  1586. if (field_offset < 0) {
  1587. RETURN_THROWS();
  1588. }
  1589. if (PQgetisnull(pgsql_result, pgsql_row, field_offset)) {
  1590. RETVAL_NULL();
  1591. } else {
  1592. RETVAL_STRINGL(PQgetvalue(pgsql_result, pgsql_row, field_offset),
  1593. PQgetlength(pgsql_result, pgsql_row, field_offset));
  1594. }
  1595. }
  1596. /* }}} */
  1597. /* {{{ void php_pgsql_fetch_hash */
  1598. static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_type, int into_object)
  1599. {
  1600. zval *result;
  1601. PGresult *pgsql_result;
  1602. pgsql_result_handle *pg_result;
  1603. int i, num_fields, pgsql_row;
  1604. zend_long row;
  1605. bool row_is_null = 1;
  1606. char *field_name;
  1607. zval *ctor_params = NULL;
  1608. zend_class_entry *ce = NULL;
  1609. if (into_object) {
  1610. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!Ca", &result, pgsql_result_ce, &row, &row_is_null, &ce, &ctor_params) == FAILURE) {
  1611. RETURN_THROWS();
  1612. }
  1613. if (!ce) {
  1614. ce = zend_standard_class_def;
  1615. }
  1616. result_type = PGSQL_ASSOC;
  1617. } else {
  1618. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!l", &result, pgsql_result_ce, &row, &row_is_null, &result_type) == FAILURE) {
  1619. RETURN_THROWS();
  1620. }
  1621. }
  1622. if (!row_is_null && row < 0) {
  1623. zend_argument_value_error(2, "must be greater than or equal to 0");
  1624. RETURN_THROWS();
  1625. }
  1626. if (!(result_type & PGSQL_BOTH)) {
  1627. zend_argument_value_error(3, "must be one of PGSQL_ASSOC, PGSQL_NUM, or PGSQL_BOTH");
  1628. RETURN_THROWS();
  1629. }
  1630. pg_result = Z_PGSQL_RESULT_P(result);
  1631. CHECK_PGSQL_RESULT(pg_result);
  1632. pgsql_result = pg_result->result;
  1633. if (!row_is_null) {
  1634. if (row >= PQntuples(pgsql_result)) {
  1635. php_error_docref(NULL, E_WARNING, "Unable to jump to row " ZEND_LONG_FMT " on PostgreSQL result index " ZEND_LONG_FMT,
  1636. row, Z_LVAL_P(result));
  1637. RETURN_FALSE;
  1638. }
  1639. pgsql_row = (int)row;
  1640. pg_result->row = pgsql_row;
  1641. } else {
  1642. /* If 2nd param is NULL, use internal row counter to access next row */
  1643. pgsql_row = pg_result->row;
  1644. if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
  1645. RETURN_FALSE;
  1646. }
  1647. pg_result->row++;
  1648. }
  1649. array_init(return_value);
  1650. for (i = 0, num_fields = PQnfields(pgsql_result); i < num_fields; i++) {
  1651. if (PQgetisnull(pgsql_result, pgsql_row, i)) {
  1652. if (result_type & PGSQL_NUM) {
  1653. add_index_null(return_value, i);
  1654. }
  1655. if (result_type & PGSQL_ASSOC) {
  1656. field_name = PQfname(pgsql_result, i);
  1657. add_assoc_null(return_value, field_name);
  1658. }
  1659. } else {
  1660. char *element = PQgetvalue(pgsql_result, pgsql_row, i);
  1661. if (element) {
  1662. const size_t element_len = strlen(element);
  1663. if (result_type & PGSQL_NUM) {
  1664. add_index_stringl(return_value, i, element, element_len);
  1665. }
  1666. if (result_type & PGSQL_ASSOC) {
  1667. field_name = PQfname(pgsql_result, i);
  1668. add_assoc_stringl(return_value, field_name, element, element_len);
  1669. }
  1670. }
  1671. }
  1672. }
  1673. if (into_object) {
  1674. zval dataset;
  1675. zend_fcall_info fci;
  1676. zend_fcall_info_cache fcc;
  1677. zval retval;
  1678. ZVAL_COPY_VALUE(&dataset, return_value);
  1679. object_init_ex(return_value, ce);
  1680. if (!ce->default_properties_count && !ce->__set) {
  1681. Z_OBJ_P(return_value)->properties = Z_ARR(dataset);
  1682. } else {
  1683. zend_merge_properties(return_value, Z_ARRVAL(dataset));
  1684. zval_ptr_dtor(&dataset);
  1685. }
  1686. if (ce->constructor) {
  1687. fci.size = sizeof(fci);
  1688. ZVAL_UNDEF(&fci.function_name);
  1689. fci.object = Z_OBJ_P(return_value);
  1690. fci.retval = &retval;
  1691. fci.params = NULL;
  1692. fci.param_count = 0;
  1693. fci.named_params = NULL;
  1694. if (ctor_params) {
  1695. if (zend_fcall_info_args(&fci, ctor_params) == FAILURE) {
  1696. ZEND_UNREACHABLE();
  1697. }
  1698. }
  1699. fcc.function_handler = ce->constructor;
  1700. fcc.called_scope = Z_OBJCE_P(return_value);
  1701. fcc.object = Z_OBJ_P(return_value);
  1702. if (zend_call_function(&fci, &fcc) == FAILURE) {
  1703. zend_throw_exception_ex(zend_ce_exception, 0, "Could not execute %s::%s()", ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
  1704. } else {
  1705. zval_ptr_dtor(&retval);
  1706. }
  1707. if (fci.params) {
  1708. efree(fci.params);
  1709. }
  1710. } else if (ctor_params && zend_hash_num_elements(Z_ARRVAL_P(ctor_params)) > 0) {
  1711. zend_argument_error(zend_ce_exception, 3,
  1712. "must be empty when the specified class (%s) does not have a constructor",
  1713. ZSTR_VAL(ce->name)
  1714. );
  1715. }
  1716. }
  1717. }
  1718. /* }}} */
  1719. /* {{{ Get a row as an enumerated array */
  1720. PHP_FUNCTION(pg_fetch_row)
  1721. {
  1722. php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_NUM, 0);
  1723. }
  1724. /* }}} */
  1725. /* {{{ Fetch a row as an assoc array */
  1726. PHP_FUNCTION(pg_fetch_assoc)
  1727. {
  1728. /* pg_fetch_assoc() is added from PHP 4.3.0. It should raise error, when
  1729. there is 3rd parameter */
  1730. if (ZEND_NUM_ARGS() > 2)
  1731. WRONG_PARAM_COUNT;
  1732. php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 0);
  1733. }
  1734. /* }}} */
  1735. /* {{{ Fetch a row as an array */
  1736. PHP_FUNCTION(pg_fetch_array)
  1737. {
  1738. php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_BOTH, 0);
  1739. }
  1740. /* }}} */
  1741. /* {{{ Fetch a row as an object */
  1742. PHP_FUNCTION(pg_fetch_object)
  1743. {
  1744. /* pg_fetch_object() allowed result_type used to be. 3rd parameter
  1745. must be allowed for compatibility */
  1746. php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 1);
  1747. }
  1748. /* }}} */
  1749. /* {{{ Fetch all rows into array */
  1750. PHP_FUNCTION(pg_fetch_all)
  1751. {
  1752. zval *result;
  1753. zend_long result_type = PGSQL_ASSOC;
  1754. PGresult *pgsql_result;
  1755. pgsql_result_handle *pg_result;
  1756. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &result, pgsql_result_ce, &result_type) == FAILURE) {
  1757. RETURN_THROWS();
  1758. }
  1759. if (!(result_type & PGSQL_BOTH)) {
  1760. zend_argument_value_error(2, "must be one of PGSQL_ASSOC, PGSQL_NUM, or PGSQL_BOTH");
  1761. RETURN_THROWS();
  1762. }
  1763. pg_result = Z_PGSQL_RESULT_P(result);
  1764. CHECK_PGSQL_RESULT(pg_result);
  1765. pgsql_result = pg_result->result;
  1766. array_init(return_value);
  1767. php_pgsql_result2array(pgsql_result, return_value, result_type);
  1768. }
  1769. /* }}} */
  1770. /* {{{ Fetch all rows into array */
  1771. PHP_FUNCTION(pg_fetch_all_columns)
  1772. {
  1773. zval *result;
  1774. PGresult *pgsql_result;
  1775. pgsql_result_handle *pg_result;
  1776. zend_long colno=0;
  1777. int pg_numrows, pg_row;
  1778. size_t num_fields;
  1779. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &result, pgsql_result_ce, &colno) == FAILURE) {
  1780. RETURN_THROWS();
  1781. }
  1782. pg_result = Z_PGSQL_RESULT_P(result);
  1783. CHECK_PGSQL_RESULT(pg_result);
  1784. if (colno < 0) {
  1785. zend_argument_value_error(2, "must be greater than or equal to 0");
  1786. RETURN_THROWS();
  1787. }
  1788. pgsql_result = pg_result->result;
  1789. num_fields = PQnfields(pgsql_result);
  1790. if (colno >= (zend_long)num_fields) {
  1791. zend_argument_value_error(2, "must be less than the number of fields for this result set");
  1792. RETURN_THROWS();
  1793. }
  1794. array_init(return_value);
  1795. if ((pg_numrows = PQntuples(pgsql_result)) <= 0) {
  1796. return;
  1797. }
  1798. for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
  1799. if (PQgetisnull(pgsql_result, pg_row, (int)colno)) {
  1800. add_next_index_null(return_value);
  1801. } else {
  1802. add_next_index_string(return_value, PQgetvalue(pgsql_result, pg_row, (int)colno));
  1803. }
  1804. }
  1805. }
  1806. /* }}} */
  1807. /* {{{ Set internal row offset */
  1808. PHP_FUNCTION(pg_result_seek)
  1809. {
  1810. zval *result;
  1811. zend_long row;
  1812. pgsql_result_handle *pg_result;
  1813. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &result, pgsql_result_ce, &row) == FAILURE) {
  1814. RETURN_THROWS();
  1815. }
  1816. pg_result = Z_PGSQL_RESULT_P(result);
  1817. CHECK_PGSQL_RESULT(pg_result);
  1818. if (row < 0 || row >= PQntuples(pg_result->result)) {
  1819. RETURN_FALSE;
  1820. }
  1821. /* seek to offset */
  1822. pg_result->row = (int)row;
  1823. RETURN_TRUE;
  1824. }
  1825. /* }}} */
  1826. #define PHP_PG_DATA_LENGTH 1
  1827. #define PHP_PG_DATA_ISNULL 2
  1828. /* {{{ php_pgsql_data_info */
  1829. static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  1830. {
  1831. zval *result;
  1832. zend_string *field_name;
  1833. zend_long row, field_offset;
  1834. PGresult *pgsql_result;
  1835. pgsql_result_handle *pg_result;
  1836. int pgsql_row;
  1837. if (ZEND_NUM_ARGS() == 2) {
  1838. ZEND_PARSE_PARAMETERS_START(2, 2)
  1839. Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
  1840. Z_PARAM_STR_OR_LONG(field_name, field_offset)
  1841. ZEND_PARSE_PARAMETERS_END();
  1842. } else {
  1843. ZEND_PARSE_PARAMETERS_START(3, 3)
  1844. Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
  1845. Z_PARAM_LONG(row)
  1846. Z_PARAM_STR_OR_LONG(field_name, field_offset)
  1847. ZEND_PARSE_PARAMETERS_END();
  1848. }
  1849. pg_result = Z_PGSQL_RESULT_P(result);
  1850. CHECK_PGSQL_RESULT(pg_result);
  1851. pgsql_result = pg_result->result;
  1852. if (ZEND_NUM_ARGS() == 2) {
  1853. if (pg_result->row < 0) {
  1854. pg_result->row = 0;
  1855. }
  1856. pgsql_row = pg_result->row;
  1857. if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
  1858. RETURN_FALSE;
  1859. }
  1860. } else {
  1861. if (row < 0) {
  1862. zend_argument_value_error(2, "must be greater than or equal to 0");
  1863. RETURN_THROWS();
  1864. }
  1865. if (row >= PQntuples(pgsql_result)) {
  1866. php_error_docref(NULL, E_WARNING, "Unable to jump to row " ZEND_LONG_FMT " on PostgreSQL result index " ZEND_LONG_FMT,
  1867. row, Z_LVAL_P(result));
  1868. RETURN_FALSE;
  1869. }
  1870. pgsql_row = (int)row;
  1871. }
  1872. field_offset = field_arg_to_offset(pgsql_result, field_name, field_offset, ZEND_NUM_ARGS());
  1873. if (field_offset < 0) {
  1874. RETURN_THROWS();
  1875. }
  1876. switch (entry_type) {
  1877. case PHP_PG_DATA_LENGTH:
  1878. RETVAL_LONG(PQgetlength(pgsql_result, pgsql_row, field_offset));
  1879. break;
  1880. case PHP_PG_DATA_ISNULL:
  1881. RETVAL_LONG(PQgetisnull(pgsql_result, pgsql_row, field_offset));
  1882. break;
  1883. EMPTY_SWITCH_DEFAULT_CASE()
  1884. }
  1885. }
  1886. /* }}} */
  1887. /* {{{ Returns the printed length */
  1888. PHP_FUNCTION(pg_field_prtlen)
  1889. {
  1890. php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH);
  1891. }
  1892. /* }}} */
  1893. /* {{{ Test if a field is NULL */
  1894. PHP_FUNCTION(pg_field_is_null)
  1895. {
  1896. php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL);
  1897. }
  1898. /* }}} */
  1899. /* {{{ Free result memory */
  1900. PHP_FUNCTION(pg_free_result)
  1901. {
  1902. zval *result;
  1903. pgsql_result_handle *pg_result;
  1904. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &result, pgsql_result_ce) == FAILURE) {
  1905. RETURN_THROWS();
  1906. }
  1907. pg_result = Z_PGSQL_RESULT_P(result);
  1908. CHECK_PGSQL_RESULT(pg_result);
  1909. pgsql_result_free(pg_result);
  1910. RETURN_TRUE;
  1911. }
  1912. /* }}} */
  1913. /* {{{ Returns the last object identifier */
  1914. PHP_FUNCTION(pg_last_oid)
  1915. {
  1916. zval *result;
  1917. PGresult *pgsql_result;
  1918. pgsql_result_handle *pg_result;
  1919. Oid oid;
  1920. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &result, pgsql_result_ce) == FAILURE) {
  1921. RETURN_THROWS();
  1922. }
  1923. pg_result = Z_PGSQL_RESULT_P(result);
  1924. CHECK_PGSQL_RESULT(pg_result);
  1925. pgsql_result = pg_result->result;
  1926. oid = PQoidValue(pgsql_result);
  1927. if (oid == InvalidOid) {
  1928. RETURN_FALSE;
  1929. }
  1930. PGSQL_RETURN_OID(oid);
  1931. }
  1932. /* }}} */
  1933. /* {{{ Enable tracing a PostgreSQL connection */
  1934. PHP_FUNCTION(pg_trace)
  1935. {
  1936. char *z_filename, *mode = "w";
  1937. size_t z_filename_len, mode_len;
  1938. zval *pgsql_link = NULL;
  1939. PGconn *pgsql;
  1940. FILE *fp = NULL;
  1941. php_stream *stream;
  1942. pgsql_link_handle *link;
  1943. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|sO!", &z_filename, &z_filename_len, &mode, &mode_len, &pgsql_link, pgsql_link_ce) == FAILURE) {
  1944. RETURN_THROWS();
  1945. }
  1946. if (!pgsql_link) {
  1947. link = FETCH_DEFAULT_LINK();
  1948. CHECK_DEFAULT_LINK(link);
  1949. } else {
  1950. link = Z_PGSQL_LINK_P(pgsql_link);
  1951. CHECK_PGSQL_LINK(link);
  1952. }
  1953. pgsql = link->conn;
  1954. stream = php_stream_open_wrapper(z_filename, mode, REPORT_ERRORS, NULL);
  1955. if (!stream) {
  1956. RETURN_FALSE;
  1957. }
  1958. if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
  1959. php_stream_close(stream);
  1960. RETURN_FALSE;
  1961. }
  1962. php_stream_auto_cleanup(stream);
  1963. PQtrace(pgsql, fp);
  1964. RETURN_TRUE;
  1965. }
  1966. /* }}} */
  1967. /* {{{ Disable tracing of a PostgreSQL connection */
  1968. PHP_FUNCTION(pg_untrace)
  1969. {
  1970. zval *pgsql_link = NULL;
  1971. PGconn *pgsql;
  1972. pgsql_link_handle *link;
  1973. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &pgsql_link) == FAILURE) {
  1974. RETURN_THROWS();
  1975. }
  1976. if (pgsql_link == NULL) {
  1977. link = FETCH_DEFAULT_LINK();
  1978. CHECK_DEFAULT_LINK(link);
  1979. } else {
  1980. link = Z_PGSQL_LINK_P(pgsql_link);
  1981. CHECK_PGSQL_LINK(link);
  1982. }
  1983. pgsql = link->conn;
  1984. PQuntrace(pgsql);
  1985. RETURN_TRUE;
  1986. }
  1987. /* }}} */
  1988. /* {{{ Create a large object */
  1989. PHP_FUNCTION(pg_lo_create)
  1990. {
  1991. zval *pgsql_link = NULL, *oid = NULL;
  1992. PGconn *pgsql;
  1993. Oid pgsql_oid, wanted_oid = InvalidOid;
  1994. pgsql_link_handle *link;
  1995. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|zz", &pgsql_link, &oid) == FAILURE) {
  1996. RETURN_THROWS();
  1997. }
  1998. /* Overloaded method uses default link if arg 1 is not an object, set oid pointer */
  1999. if ((ZEND_NUM_ARGS() == 1) && (Z_TYPE_P(pgsql_link) != IS_OBJECT)) {
  2000. oid = pgsql_link;
  2001. pgsql_link = NULL;
  2002. }
  2003. if (pgsql_link == NULL) {
  2004. link = FETCH_DEFAULT_LINK();
  2005. CHECK_DEFAULT_LINK(link);
  2006. } else if ((Z_TYPE_P(pgsql_link) == IS_OBJECT && instanceof_function(Z_OBJCE_P(pgsql_link), pgsql_link_ce))) {
  2007. link = Z_PGSQL_LINK_P(pgsql_link);
  2008. CHECK_PGSQL_LINK(link);
  2009. } else {
  2010. zend_argument_type_error(1, "must be of type PgSql\\Connection when the connection is provided");
  2011. RETURN_THROWS();
  2012. }
  2013. pgsql = link->conn;
  2014. if (oid) {
  2015. switch (Z_TYPE_P(oid)) {
  2016. case IS_STRING:
  2017. {
  2018. if (!is_valid_oid_string(Z_STR_P(oid), &wanted_oid)) {
  2019. /* wrong integer format */
  2020. zend_value_error("Invalid OID value passed");
  2021. RETURN_THROWS();
  2022. }
  2023. }
  2024. break;
  2025. case IS_LONG:
  2026. if (Z_LVAL_P(oid) < (zend_long)InvalidOid) {
  2027. zend_value_error("Invalid OID value passed");
  2028. RETURN_THROWS();
  2029. }
  2030. wanted_oid = (Oid)Z_LVAL_P(oid);
  2031. break;
  2032. default:
  2033. zend_type_error("OID value must be of type string|int, %s given", zend_zval_type_name(oid));
  2034. RETURN_THROWS();
  2035. }
  2036. if ((pgsql_oid = lo_create(pgsql, wanted_oid)) == InvalidOid) {
  2037. php_error_docref(NULL, E_WARNING, "Unable to create PostgreSQL large object");
  2038. RETURN_FALSE;
  2039. }
  2040. PGSQL_RETURN_OID(pgsql_oid);
  2041. }
  2042. if ((pgsql_oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == InvalidOid) {
  2043. php_error_docref(NULL, E_WARNING, "Unable to create PostgreSQL large object");
  2044. RETURN_FALSE;
  2045. }
  2046. PGSQL_RETURN_OID(pgsql_oid);
  2047. }
  2048. /* }}} */
  2049. /* {{{ Delete a large object */
  2050. PHP_FUNCTION(pg_lo_unlink)
  2051. {
  2052. zval *pgsql_link = NULL;
  2053. zend_long oid_long;
  2054. zend_string *oid_string;
  2055. PGconn *pgsql;
  2056. Oid oid;
  2057. pgsql_link_handle *link;
  2058. /* accept string type since Oid type is unsigned int */
  2059. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OS", &pgsql_link, pgsql_link_ce, &oid_string) == SUCCESS) {
  2060. if (!is_valid_oid_string(oid_string, &oid)) {
  2061. /* wrong integer format */
  2062. zend_value_error("Invalid OID value passed");
  2063. RETURN_THROWS();
  2064. }
  2065. link = Z_PGSQL_LINK_P(pgsql_link);
  2066. CHECK_PGSQL_LINK(link);
  2067. }
  2068. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2069. "Ol", &pgsql_link, pgsql_link_ce, &oid_long) == SUCCESS) {
  2070. if (oid_long <= (zend_long)InvalidOid) {
  2071. zend_value_error("Invalid OID value passed");
  2072. RETURN_THROWS();
  2073. }
  2074. oid = (Oid)oid_long;
  2075. link = Z_PGSQL_LINK_P(pgsql_link);
  2076. CHECK_PGSQL_LINK(link);
  2077. }
  2078. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "S", &oid_string) == SUCCESS) {
  2079. if (!is_valid_oid_string(oid_string, &oid)) {
  2080. /* wrong integer format */
  2081. zend_value_error("Invalid OID value passed");
  2082. RETURN_THROWS();
  2083. }
  2084. link = FETCH_DEFAULT_LINK();
  2085. CHECK_DEFAULT_LINK(link);
  2086. }
  2087. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2088. "l", &oid_long) == SUCCESS) {
  2089. if (oid_long <= (zend_long)InvalidOid) {
  2090. zend_value_error("Invalid OID value passed");
  2091. RETURN_THROWS();
  2092. }
  2093. oid = (Oid)oid_long;
  2094. link = FETCH_DEFAULT_LINK();
  2095. CHECK_DEFAULT_LINK(link);
  2096. }
  2097. else {
  2098. zend_argument_count_error("Requires 1 or 2 arguments, %d given", ZEND_NUM_ARGS());
  2099. RETURN_THROWS();
  2100. }
  2101. pgsql = link->conn;
  2102. if (lo_unlink(pgsql, oid) == -1) {
  2103. php_error_docref(NULL, E_WARNING, "Unable to delete PostgreSQL large object %u", oid);
  2104. RETURN_FALSE;
  2105. }
  2106. RETURN_TRUE;
  2107. }
  2108. /* }}} */
  2109. /* {{{ Open a large object and return fd */
  2110. PHP_FUNCTION(pg_lo_open)
  2111. {
  2112. zval *pgsql_link = NULL;
  2113. zend_long oid_long;
  2114. zend_string *oid_string;
  2115. zend_string *mode;
  2116. PGconn *pgsql;
  2117. Oid oid;
  2118. int pgsql_mode=0, pgsql_lofd;
  2119. bool create = false;
  2120. pgLofp *pgsql_lofp;
  2121. pgsql_link_handle *link;
  2122. /* accept string type since Oid is unsigned int */
  2123. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2124. "OSS", &pgsql_link, pgsql_link_ce, &oid_string, &mode) == SUCCESS) {
  2125. if (!is_valid_oid_string(oid_string, &oid)) {
  2126. /* wrong integer format */
  2127. zend_value_error("Invalid OID value passed");
  2128. RETURN_THROWS();
  2129. }
  2130. link = Z_PGSQL_LINK_P(pgsql_link);
  2131. CHECK_PGSQL_LINK(link);
  2132. }
  2133. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2134. "Ols", &pgsql_link, pgsql_link_ce, &oid_long, &mode) == SUCCESS) {
  2135. if (oid_long <= (zend_long)InvalidOid) {
  2136. zend_value_error("Invalid OID value passed");
  2137. RETURN_THROWS();
  2138. }
  2139. oid = (Oid)oid_long;
  2140. link = Z_PGSQL_LINK_P(pgsql_link);
  2141. CHECK_PGSQL_LINK(link);
  2142. }
  2143. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2144. "SS", &oid_string, &mode) == SUCCESS) {
  2145. if (!is_valid_oid_string(oid_string, &oid)) {
  2146. /* wrong integer format */
  2147. zend_value_error("Invalid OID value passed");
  2148. RETURN_THROWS();
  2149. }
  2150. link = FETCH_DEFAULT_LINK();
  2151. CHECK_DEFAULT_LINK(link);
  2152. }
  2153. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2154. "lS", &oid_long, &mode) == SUCCESS) {
  2155. if (oid_long <= (zend_long)InvalidOid) {
  2156. zend_value_error("Invalid OID value passed");
  2157. RETURN_THROWS();
  2158. }
  2159. oid = (Oid)oid_long;
  2160. link = FETCH_DEFAULT_LINK();
  2161. CHECK_DEFAULT_LINK(link);
  2162. }
  2163. else {
  2164. zend_argument_count_error("Requires 1 or 2 arguments, %d given", ZEND_NUM_ARGS());
  2165. RETURN_THROWS();
  2166. }
  2167. pgsql = link->conn;
  2168. /* r/w/+ is little bit more PHP-like than INV_READ/INV_WRITE and a lot of
  2169. faster to type. Unfortunately, doesn't behave the same way as fopen()...
  2170. (Jouni)
  2171. */
  2172. if (zend_string_equals_literal(mode, "r")) {
  2173. pgsql_mode |= INV_READ;
  2174. } else if (zend_string_equals_literal(mode, "w")) {
  2175. pgsql_mode |= INV_WRITE;
  2176. create = true;
  2177. } else if (zend_string_equals_literal(mode, "r+")) {
  2178. pgsql_mode |= INV_READ;
  2179. pgsql_mode |= INV_WRITE;
  2180. } else if (zend_string_equals_literal(mode, "w+")) {
  2181. pgsql_mode |= INV_READ;
  2182. pgsql_mode |= INV_WRITE;
  2183. create = true;
  2184. } else {
  2185. zend_value_error("Mode must be one of 'r', 'r+', 'w', or 'w+'");
  2186. RETURN_THROWS();
  2187. }
  2188. if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
  2189. if (create) {
  2190. if ((oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == 0) {
  2191. php_error_docref(NULL, E_WARNING, "Unable to create PostgreSQL large object");
  2192. RETURN_FALSE;
  2193. } else {
  2194. if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
  2195. if (lo_unlink(pgsql, oid) == -1) {
  2196. php_error_docref(NULL, E_WARNING, "Something is really messed up! Your database is badly corrupted in a way NOT related to PHP");
  2197. } else {
  2198. php_error_docref(NULL, E_WARNING, "Unable to open PostgreSQL large object");
  2199. }
  2200. RETURN_FALSE;
  2201. }
  2202. }
  2203. } else {
  2204. php_error_docref(NULL, E_WARNING, "Unable to open PostgreSQL large object");
  2205. RETURN_FALSE;
  2206. }
  2207. }
  2208. object_init_ex(return_value, pgsql_lob_ce);
  2209. pgsql_lofp = Z_PGSQL_LOB_P(return_value);
  2210. pgsql_lofp->conn = pgsql;
  2211. pgsql_lofp->lofd = pgsql_lofd;
  2212. }
  2213. /* }}} */
  2214. /* {{{ Close a large object */
  2215. PHP_FUNCTION(pg_lo_close)
  2216. {
  2217. zval *pgsql_lofp;
  2218. pgLofp *pgsql;
  2219. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_lofp, pgsql_lob_ce) == FAILURE) {
  2220. RETURN_THROWS();
  2221. }
  2222. pgsql = Z_PGSQL_LOB_P(pgsql_lofp);
  2223. CHECK_PGSQL_LOB(pgsql);
  2224. if (lo_close((PGconn *)pgsql->conn, pgsql->lofd) < 0) {
  2225. php_error_docref(NULL, E_WARNING, "Unable to close PostgreSQL large object descriptor %d", pgsql->lofd);
  2226. RETVAL_FALSE;
  2227. } else {
  2228. RETVAL_TRUE;
  2229. }
  2230. return;
  2231. }
  2232. /* }}} */
  2233. #define PGSQL_LO_READ_BUF_SIZE 8192
  2234. /* {{{ Read a large object */
  2235. PHP_FUNCTION(pg_lo_read)
  2236. {
  2237. zval *pgsql_id;
  2238. zend_long buffer_length = PGSQL_LO_READ_BUF_SIZE;
  2239. int nbytes;
  2240. zend_string *buf;
  2241. pgLofp *pgsql;
  2242. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &pgsql_id, pgsql_lob_ce, &buffer_length) == FAILURE) {
  2243. RETURN_THROWS();
  2244. }
  2245. pgsql = Z_PGSQL_LOB_P(pgsql_id);
  2246. CHECK_PGSQL_LOB(pgsql);
  2247. if (buffer_length < 0) {
  2248. zend_argument_value_error(2, "must be greater or equal than 0");
  2249. RETURN_THROWS();
  2250. }
  2251. buf = zend_string_alloc(buffer_length, 0);
  2252. if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, ZSTR_VAL(buf), ZSTR_LEN(buf)))<0) {
  2253. zend_string_efree(buf);
  2254. RETURN_FALSE;
  2255. }
  2256. /* TODO Use truncate API? */
  2257. ZSTR_LEN(buf) = nbytes;
  2258. ZSTR_VAL(buf)[ZSTR_LEN(buf)] = '\0';
  2259. RETURN_NEW_STR(buf);
  2260. }
  2261. /* }}} */
  2262. /* {{{ Write a large object */
  2263. PHP_FUNCTION(pg_lo_write)
  2264. {
  2265. zval *pgsql_id;
  2266. zend_string *str;
  2267. zend_long z_len;
  2268. bool z_len_is_null = 1;
  2269. size_t nbytes;
  2270. size_t len;
  2271. pgLofp *pgsql;
  2272. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS|l!", &pgsql_id, pgsql_lob_ce, &str, &z_len, &z_len_is_null) == FAILURE) {
  2273. RETURN_THROWS();
  2274. }
  2275. if (!z_len_is_null) {
  2276. if (z_len < 0) {
  2277. zend_argument_value_error(3, "must be greater than or equal to 0");
  2278. RETURN_THROWS();
  2279. }
  2280. if (z_len > (zend_long)ZSTR_LEN(str)) {
  2281. zend_argument_value_error(3, "must be less than or equal to the length of argument #2 ($buf)");
  2282. RETURN_THROWS();
  2283. }
  2284. len = z_len;
  2285. }
  2286. else {
  2287. len = ZSTR_LEN(str);
  2288. }
  2289. pgsql = Z_PGSQL_LOB_P(pgsql_id);
  2290. CHECK_PGSQL_LOB(pgsql);
  2291. if ((nbytes = lo_write((PGconn *)pgsql->conn, pgsql->lofd, ZSTR_VAL(str), len)) == (size_t)-1) {
  2292. RETURN_FALSE;
  2293. }
  2294. RETURN_LONG(nbytes);
  2295. }
  2296. /* }}} */
  2297. /* {{{ Read a large object and send straight to browser */
  2298. PHP_FUNCTION(pg_lo_read_all)
  2299. {
  2300. zval *pgsql_id;
  2301. int tbytes;
  2302. volatile int nbytes;
  2303. char buf[PGSQL_LO_READ_BUF_SIZE];
  2304. pgLofp *pgsql;
  2305. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_id, pgsql_lob_ce) == FAILURE) {
  2306. RETURN_THROWS();
  2307. }
  2308. pgsql = Z_PGSQL_LOB_P(pgsql_id);
  2309. CHECK_PGSQL_LOB(pgsql);
  2310. tbytes = 0;
  2311. while ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, PGSQL_LO_READ_BUF_SIZE))>0) {
  2312. PHPWRITE(buf, nbytes);
  2313. tbytes += nbytes;
  2314. }
  2315. RETURN_LONG(tbytes);
  2316. }
  2317. /* }}} */
  2318. /* {{{ Import large object direct from filesystem */
  2319. PHP_FUNCTION(pg_lo_import)
  2320. {
  2321. zval *pgsql_link = NULL, *oid = NULL;
  2322. zend_string *file_in;
  2323. PGconn *pgsql;
  2324. Oid returned_oid;
  2325. pgsql_link_handle *link;
  2326. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2327. "OP|z", &pgsql_link, pgsql_link_ce, &file_in, &oid) == SUCCESS) {
  2328. link = Z_PGSQL_LINK_P(pgsql_link);
  2329. CHECK_PGSQL_LINK(link);
  2330. }
  2331. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2332. "P|z", &file_in, &oid) == SUCCESS) {
  2333. link = FETCH_DEFAULT_LINK();
  2334. CHECK_DEFAULT_LINK(link);
  2335. }
  2336. else {
  2337. WRONG_PARAM_COUNT;
  2338. }
  2339. if (php_check_open_basedir(ZSTR_VAL(file_in))) {
  2340. RETURN_FALSE;
  2341. }
  2342. pgsql = link->conn;
  2343. if (oid) {
  2344. Oid wanted_oid;
  2345. switch (Z_TYPE_P(oid)) {
  2346. case IS_STRING:
  2347. {
  2348. if (!is_valid_oid_string(Z_STR_P(oid), &wanted_oid)) {
  2349. /* wrong integer format */
  2350. zend_value_error("Invalid OID value passed");
  2351. RETURN_THROWS();
  2352. }
  2353. }
  2354. break;
  2355. case IS_LONG:
  2356. if (Z_LVAL_P(oid) < (zend_long)InvalidOid) {
  2357. zend_value_error("Invalid OID value passed");
  2358. RETURN_THROWS();
  2359. }
  2360. wanted_oid = (Oid)Z_LVAL_P(oid);
  2361. break;
  2362. default:
  2363. zend_type_error("OID value must be of type string|int, %s given", zend_zval_type_name(oid));
  2364. RETURN_THROWS();
  2365. }
  2366. returned_oid = lo_import_with_oid(pgsql, ZSTR_VAL(file_in), wanted_oid);
  2367. if (returned_oid == InvalidOid) {
  2368. RETURN_FALSE;
  2369. }
  2370. PGSQL_RETURN_OID(returned_oid);
  2371. }
  2372. returned_oid = lo_import(pgsql, ZSTR_VAL(file_in));
  2373. if (returned_oid == InvalidOid) {
  2374. RETURN_FALSE;
  2375. }
  2376. PGSQL_RETURN_OID(returned_oid);
  2377. }
  2378. /* }}} */
  2379. /* {{{ Export large object direct to filesystem */
  2380. PHP_FUNCTION(pg_lo_export)
  2381. {
  2382. zval *pgsql_link = NULL;
  2383. zend_string *oid_string;
  2384. zend_string *file_out;
  2385. zend_long oid_long;
  2386. Oid oid;
  2387. PGconn *pgsql;
  2388. pgsql_link_handle *link;
  2389. /* allow string to handle large OID value correctly */
  2390. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2391. "rlP", &pgsql_link, pgsql_link_ce, &oid_long, &file_out) == SUCCESS) {
  2392. if (oid_long <= (zend_long)InvalidOid) {
  2393. zend_value_error("Invalid OID value passed");
  2394. RETURN_THROWS();
  2395. }
  2396. oid = (Oid)oid_long;
  2397. link = Z_PGSQL_LINK_P(pgsql_link);
  2398. CHECK_PGSQL_LINK(link);
  2399. }
  2400. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2401. "OSP", &pgsql_link, pgsql_link_ce, &oid_string, &file_out) == SUCCESS) {
  2402. if (!is_valid_oid_string(oid_string, &oid)) {
  2403. /* wrong integer format */
  2404. zend_value_error("Invalid OID value passed");
  2405. RETURN_THROWS();
  2406. }
  2407. link = Z_PGSQL_LINK_P(pgsql_link);
  2408. CHECK_PGSQL_LINK(link);
  2409. }
  2410. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2411. "lP", &oid_long, &file_out) == SUCCESS) {
  2412. if (oid_long <= (zend_long)InvalidOid) {
  2413. zend_value_error("Invalid OID value passed");
  2414. RETURN_THROWS();
  2415. }
  2416. oid = (Oid)oid_long;
  2417. link = FETCH_DEFAULT_LINK();
  2418. CHECK_DEFAULT_LINK(link);
  2419. }
  2420. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
  2421. "SP", &oid_string, &file_out) == SUCCESS) {
  2422. if (!is_valid_oid_string(oid_string, &oid)) {
  2423. /* wrong integer format */
  2424. zend_value_error("Invalid OID value passed");
  2425. RETURN_THROWS();
  2426. }
  2427. link = FETCH_DEFAULT_LINK();
  2428. CHECK_DEFAULT_LINK(link);
  2429. }
  2430. else {
  2431. zend_argument_count_error("Requires 2 or 3 arguments, %d given", ZEND_NUM_ARGS());
  2432. RETURN_THROWS();
  2433. }
  2434. if (php_check_open_basedir(ZSTR_VAL(file_out))) {
  2435. RETURN_FALSE;
  2436. }
  2437. pgsql = link->conn;
  2438. if (lo_export(pgsql, oid, ZSTR_VAL(file_out)) == -1) {
  2439. RETURN_FALSE;
  2440. }
  2441. RETURN_TRUE;
  2442. }
  2443. /* }}} */
  2444. /* {{{ Seeks position of large object */
  2445. PHP_FUNCTION(pg_lo_seek)
  2446. {
  2447. zval *pgsql_id = NULL;
  2448. zend_long result, offset = 0, whence = SEEK_CUR;
  2449. pgLofp *pgsql;
  2450. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|l", &pgsql_id, pgsql_lob_ce, &offset, &whence) == FAILURE) {
  2451. RETURN_THROWS();
  2452. }
  2453. if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) {
  2454. zend_argument_value_error(3, "must be one of PGSQL_SEEK_SET, PGSQL_SEEK_CUR, or PGSQL_SEEK_END");
  2455. RETURN_THROWS();
  2456. }
  2457. pgsql = Z_PGSQL_LOB_P(pgsql_id);
  2458. CHECK_PGSQL_LOB(pgsql);
  2459. #ifdef HAVE_PG_LO64
  2460. if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
  2461. result = lo_lseek64((PGconn *)pgsql->conn, pgsql->lofd, offset, (int)whence);
  2462. } else {
  2463. result = lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, (int)offset, (int)whence);
  2464. }
  2465. #else
  2466. result = lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence);
  2467. #endif
  2468. if (result > -1) {
  2469. RETURN_TRUE;
  2470. } else {
  2471. RETURN_FALSE;
  2472. }
  2473. }
  2474. /* }}} */
  2475. /* {{{ Returns current position of large object */
  2476. PHP_FUNCTION(pg_lo_tell)
  2477. {
  2478. zval *pgsql_id = NULL;
  2479. zend_long offset = 0;
  2480. pgLofp *pgsql;
  2481. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_id, pgsql_lob_ce) == FAILURE) {
  2482. RETURN_THROWS();
  2483. }
  2484. pgsql = Z_PGSQL_LOB_P(pgsql_id);
  2485. CHECK_PGSQL_LOB(pgsql);
  2486. #ifdef VE_PG_LO64
  2487. if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
  2488. offset = lo_tell64((PGconn *)pgsql->conn, pgsql->lofd);
  2489. } else {
  2490. offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd);
  2491. }
  2492. #else
  2493. offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd);
  2494. #endif
  2495. RETURN_LONG(offset);
  2496. }
  2497. /* }}} */
  2498. /* {{{ Truncate large object to size */
  2499. PHP_FUNCTION(pg_lo_truncate)
  2500. {
  2501. zval *pgsql_id = NULL;
  2502. size_t size;
  2503. pgLofp *pgsql;
  2504. int result;
  2505. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pgsql_id, pgsql_lob_ce, &size) == FAILURE) {
  2506. RETURN_THROWS();
  2507. }
  2508. pgsql = Z_PGSQL_LOB_P(pgsql_id);
  2509. CHECK_PGSQL_LOB(pgsql);
  2510. #ifdef VE_PG_LO64
  2511. if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
  2512. result = lo_truncate64((PGconn *)pgsql->conn, pgsql->lofd, size);
  2513. } else {
  2514. result = lo_truncate((PGconn *)pgsql->conn, pgsql->lofd, size);
  2515. }
  2516. #else
  2517. result = lo_truncate((PGconn *)pgsql->conn, pgsql->lofd, size);
  2518. #endif
  2519. if (!result) {
  2520. RETURN_TRUE;
  2521. } else {
  2522. RETURN_FALSE;
  2523. }
  2524. }
  2525. /* }}} */
  2526. /* {{{ Set error verbosity */
  2527. PHP_FUNCTION(pg_set_error_verbosity)
  2528. {
  2529. zval *pgsql_link = NULL;
  2530. zend_long verbosity;
  2531. PGconn *pgsql;
  2532. pgsql_link_handle *link;
  2533. if (ZEND_NUM_ARGS() == 1) {
  2534. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &verbosity) == FAILURE) {
  2535. RETURN_THROWS();
  2536. }
  2537. link = FETCH_DEFAULT_LINK();
  2538. CHECK_DEFAULT_LINK(link);
  2539. } else {
  2540. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pgsql_link, pgsql_link_ce, &verbosity) == FAILURE) {
  2541. RETURN_THROWS();
  2542. }
  2543. link = Z_PGSQL_LINK_P(pgsql_link);
  2544. CHECK_PGSQL_LINK(link);
  2545. }
  2546. pgsql = link->conn;
  2547. if (verbosity & (PQERRORS_TERSE|PQERRORS_DEFAULT|PQERRORS_VERBOSE)) {
  2548. RETURN_LONG(PQsetErrorVerbosity(pgsql, verbosity));
  2549. } else {
  2550. RETURN_FALSE;
  2551. }
  2552. }
  2553. /* }}} */
  2554. /* {{{ Set client encoding */
  2555. PHP_FUNCTION(pg_set_client_encoding)
  2556. {
  2557. char *encoding;
  2558. size_t encoding_len;
  2559. zval *pgsql_link = NULL;
  2560. PGconn *pgsql;
  2561. pgsql_link_handle *link;
  2562. if (ZEND_NUM_ARGS() == 1) {
  2563. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &encoding, &encoding_len) == FAILURE) {
  2564. RETURN_THROWS();
  2565. }
  2566. link = FETCH_DEFAULT_LINK();
  2567. CHECK_DEFAULT_LINK(link);
  2568. } else {
  2569. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &encoding, &encoding_len) == FAILURE) {
  2570. RETURN_THROWS();
  2571. }
  2572. link = Z_PGSQL_LINK_P(pgsql_link);
  2573. CHECK_PGSQL_LINK(link);
  2574. }
  2575. pgsql = link->conn;
  2576. RETURN_LONG(PQsetClientEncoding(pgsql, encoding));
  2577. }
  2578. /* }}} */
  2579. /* {{{ Get the current client encoding */
  2580. PHP_FUNCTION(pg_client_encoding)
  2581. {
  2582. zval *pgsql_link = NULL;
  2583. PGconn *pgsql;
  2584. pgsql_link_handle *link;
  2585. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &pgsql_link, pgsql_link_ce) == FAILURE) {
  2586. RETURN_THROWS();
  2587. }
  2588. if (pgsql_link == NULL) {
  2589. link = FETCH_DEFAULT_LINK();
  2590. CHECK_DEFAULT_LINK(link);
  2591. } else {
  2592. link = Z_PGSQL_LINK_P(pgsql_link);
  2593. CHECK_PGSQL_LINK(link);
  2594. }
  2595. pgsql = link->conn;
  2596. /* Just do the same as found in PostgreSQL sources... */
  2597. RETURN_STRING((char *) pg_encoding_to_char(PQclientEncoding(pgsql)));
  2598. }
  2599. /* }}} */
  2600. /* {{{ Sync with backend. Completes the Copy command */
  2601. PHP_FUNCTION(pg_end_copy)
  2602. {
  2603. zval *pgsql_link = NULL;
  2604. PGconn *pgsql;
  2605. int result = 0;
  2606. pgsql_link_handle *link;
  2607. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|O!", &pgsql_link, pgsql_link_ce) == FAILURE) {
  2608. RETURN_THROWS();
  2609. }
  2610. if (pgsql_link == NULL) {
  2611. link = FETCH_DEFAULT_LINK();
  2612. CHECK_DEFAULT_LINK(link);
  2613. } else {
  2614. link = Z_PGSQL_LINK_P(pgsql_link);
  2615. CHECK_PGSQL_LINK(link);
  2616. }
  2617. pgsql = link->conn;
  2618. result = PQendcopy(pgsql);
  2619. if (result!=0) {
  2620. PHP_PQ_ERROR("Query failed: %s", pgsql);
  2621. RETURN_FALSE;
  2622. }
  2623. RETURN_TRUE;
  2624. }
  2625. /* }}} */
  2626. /* {{{ Send null-terminated string to backend server*/
  2627. PHP_FUNCTION(pg_put_line)
  2628. {
  2629. char *query;
  2630. size_t query_len;
  2631. zval *pgsql_link = NULL;
  2632. PGconn *pgsql;
  2633. pgsql_link_handle *link;
  2634. int result = 0;
  2635. if (ZEND_NUM_ARGS() == 1) {
  2636. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query, &query_len) == FAILURE) {
  2637. RETURN_THROWS();
  2638. }
  2639. link = FETCH_DEFAULT_LINK();
  2640. CHECK_DEFAULT_LINK(link);
  2641. } else {
  2642. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &query, &query_len) == FAILURE) {
  2643. RETURN_THROWS();
  2644. }
  2645. link = Z_PGSQL_LINK_P(pgsql_link);
  2646. CHECK_PGSQL_LINK(link);
  2647. }
  2648. pgsql = link->conn;
  2649. result = PQputline(pgsql, query);
  2650. if (result==EOF) {
  2651. PHP_PQ_ERROR("Query failed: %s", pgsql);
  2652. RETURN_FALSE;
  2653. }
  2654. RETURN_TRUE;
  2655. }
  2656. /* }}} */
  2657. /* {{{ Copy table to array */
  2658. PHP_FUNCTION(pg_copy_to)
  2659. {
  2660. zval *pgsql_link;
  2661. pgsql_link_handle *link;
  2662. zend_string *table_name;
  2663. zend_string *pg_delimiter = NULL;
  2664. char *pg_null_as = NULL;
  2665. size_t pg_null_as_len = 0;
  2666. bool free_pg_null = false;
  2667. char *query;
  2668. PGconn *pgsql;
  2669. PGresult *pgsql_result;
  2670. ExecStatusType status;
  2671. char *csv = (char *)NULL;
  2672. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|Ss", &pgsql_link, pgsql_link_ce,
  2673. &table_name, &pg_delimiter, &pg_null_as, &pg_null_as_len) == FAILURE
  2674. ) {
  2675. RETURN_THROWS();
  2676. }
  2677. link = Z_PGSQL_LINK_P(pgsql_link);
  2678. CHECK_PGSQL_LINK(link);
  2679. pgsql = link->conn;
  2680. if (!pg_delimiter) {
  2681. pg_delimiter = ZSTR_CHAR('\t');
  2682. } else if (ZSTR_LEN(pg_delimiter) != 1) {
  2683. zend_argument_value_error(3, "must be one character");
  2684. RETURN_THROWS();
  2685. }
  2686. if (!pg_null_as) {
  2687. pg_null_as = estrdup("\\\\N");
  2688. free_pg_null = true;
  2689. }
  2690. spprintf(&query, 0, "COPY %s TO STDOUT DELIMITER E'%c' NULL AS E'%s'", ZSTR_VAL(table_name), *ZSTR_VAL(pg_delimiter), pg_null_as);
  2691. while ((pgsql_result = PQgetResult(pgsql))) {
  2692. PQclear(pgsql_result);
  2693. }
  2694. pgsql_result = PQexec(pgsql, query);
  2695. if (free_pg_null) {
  2696. efree(pg_null_as);
  2697. }
  2698. efree(query);
  2699. if (pgsql_result) {
  2700. status = PQresultStatus(pgsql_result);
  2701. } else {
  2702. status = (ExecStatusType) PQstatus(pgsql);
  2703. }
  2704. switch (status) {
  2705. case PGRES_COPY_OUT:
  2706. if (pgsql_result) {
  2707. int copydone = 0;
  2708. PQclear(pgsql_result);
  2709. array_init(return_value);
  2710. while (!copydone)
  2711. {
  2712. int ret = PQgetCopyData(pgsql, &csv, 0);
  2713. switch (ret) {
  2714. case -1:
  2715. copydone = 1;
  2716. break;
  2717. case 0:
  2718. case -2:
  2719. PHP_PQ_ERROR("getline failed: %s", pgsql);
  2720. RETURN_FALSE;
  2721. break;
  2722. default:
  2723. add_next_index_string(return_value, csv);
  2724. PQfreemem(csv);
  2725. break;
  2726. }
  2727. }
  2728. while ((pgsql_result = PQgetResult(pgsql))) {
  2729. PQclear(pgsql_result);
  2730. }
  2731. } else {
  2732. PQclear(pgsql_result);
  2733. RETURN_FALSE;
  2734. }
  2735. break;
  2736. default:
  2737. PQclear(pgsql_result);
  2738. PHP_PQ_ERROR("Copy command failed: %s", pgsql);
  2739. RETURN_FALSE;
  2740. break;
  2741. }
  2742. }
  2743. /* }}} */
  2744. /* {{{ Copy table from array */
  2745. PHP_FUNCTION(pg_copy_from)
  2746. {
  2747. zval *pgsql_link = NULL, *pg_rows;
  2748. pgsql_link_handle *link;
  2749. zval *value;
  2750. zend_string *table_name;
  2751. zend_string *pg_delimiter = NULL;
  2752. char *pg_null_as = NULL;
  2753. size_t pg_null_as_len;
  2754. bool pg_null_as_free = false;
  2755. char *query;
  2756. PGconn *pgsql;
  2757. PGresult *pgsql_result;
  2758. ExecStatusType status;
  2759. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPa|Ss", &pgsql_link, pgsql_link_ce,
  2760. &table_name, &pg_rows, &pg_delimiter, &pg_null_as, &pg_null_as_len) == FAILURE
  2761. ) {
  2762. RETURN_THROWS();
  2763. }
  2764. link = Z_PGSQL_LINK_P(pgsql_link);
  2765. CHECK_PGSQL_LINK(link);
  2766. pgsql = link->conn;
  2767. if (!pg_delimiter) {
  2768. pg_delimiter = ZSTR_CHAR('\t');
  2769. } else if (ZSTR_LEN(pg_delimiter) != 1) {
  2770. zend_argument_value_error(4, "must be one character");
  2771. RETURN_THROWS();
  2772. }
  2773. if (!pg_null_as) {
  2774. pg_null_as = estrdup("\\\\N");
  2775. pg_null_as_free = true;
  2776. }
  2777. spprintf(&query, 0, "COPY %s FROM STDIN DELIMITER E'%c' NULL AS E'%s'", ZSTR_VAL(table_name), *ZSTR_VAL(pg_delimiter), pg_null_as);
  2778. while ((pgsql_result = PQgetResult(pgsql))) {
  2779. PQclear(pgsql_result);
  2780. }
  2781. pgsql_result = PQexec(pgsql, query);
  2782. if (pg_null_as_free) {
  2783. efree(pg_null_as);
  2784. }
  2785. efree(query);
  2786. if (pgsql_result) {
  2787. status = PQresultStatus(pgsql_result);
  2788. } else {
  2789. status = (ExecStatusType) PQstatus(pgsql);
  2790. }
  2791. switch (status) {
  2792. case PGRES_COPY_IN:
  2793. if (pgsql_result) {
  2794. int command_failed = 0;
  2795. PQclear(pgsql_result);
  2796. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), value) {
  2797. zend_string *tmp = zval_try_get_string(value);
  2798. if (UNEXPECTED(!tmp)) {
  2799. return;
  2800. }
  2801. query = (char *)emalloc(ZSTR_LEN(tmp) + 2);
  2802. strlcpy(query, ZSTR_VAL(tmp), ZSTR_LEN(tmp) + 2);
  2803. if (ZSTR_LEN(tmp) > 0 && *(query + ZSTR_LEN(tmp) - 1) != '\n') {
  2804. strlcat(query, "\n", ZSTR_LEN(tmp) + 2);
  2805. }
  2806. if (PQputCopyData(pgsql, query, (int)strlen(query)) != 1) {
  2807. efree(query);
  2808. zend_string_release(tmp);
  2809. PHP_PQ_ERROR("copy failed: %s", pgsql);
  2810. RETURN_FALSE;
  2811. }
  2812. efree(query);
  2813. zend_string_release(tmp);
  2814. } ZEND_HASH_FOREACH_END();
  2815. if (PQputCopyEnd(pgsql, NULL) != 1) {
  2816. PHP_PQ_ERROR("putcopyend failed: %s", pgsql);
  2817. RETURN_FALSE;
  2818. }
  2819. while ((pgsql_result = PQgetResult(pgsql))) {
  2820. if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
  2821. PHP_PQ_ERROR("Copy command failed: %s", pgsql);
  2822. command_failed = 1;
  2823. }
  2824. PQclear(pgsql_result);
  2825. }
  2826. if (command_failed) {
  2827. RETURN_FALSE;
  2828. }
  2829. } else {
  2830. PQclear(pgsql_result);
  2831. RETURN_FALSE;
  2832. }
  2833. RETURN_TRUE;
  2834. break;
  2835. default:
  2836. PQclear(pgsql_result);
  2837. PHP_PQ_ERROR("Copy command failed: %s", pgsql);
  2838. RETURN_FALSE;
  2839. break;
  2840. }
  2841. }
  2842. /* }}} */
  2843. /* {{{ Escape string for text/char type */
  2844. PHP_FUNCTION(pg_escape_string)
  2845. {
  2846. zend_string *from = NULL, *to = NULL;
  2847. zval *pgsql_link;
  2848. pgsql_link_handle *link;
  2849. PGconn *pgsql;
  2850. switch (ZEND_NUM_ARGS()) {
  2851. case 1:
  2852. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &from) == FAILURE) {
  2853. RETURN_THROWS();
  2854. }
  2855. link = FETCH_DEFAULT_LINK();
  2856. break;
  2857. default:
  2858. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &pgsql_link, pgsql_link_ce, &from) == FAILURE) {
  2859. RETURN_THROWS();
  2860. }
  2861. link = Z_PGSQL_LINK_P(pgsql_link);
  2862. CHECK_PGSQL_LINK(link);
  2863. break;
  2864. }
  2865. to = zend_string_safe_alloc(ZSTR_LEN(from), 2, 0, 0);
  2866. if (link) {
  2867. pgsql = link->conn;
  2868. ZSTR_LEN(to) = PQescapeStringConn(pgsql, ZSTR_VAL(to), ZSTR_VAL(from), ZSTR_LEN(from), NULL);
  2869. } else
  2870. {
  2871. ZSTR_LEN(to) = PQescapeString(ZSTR_VAL(to), ZSTR_VAL(from), ZSTR_LEN(from));
  2872. }
  2873. to = zend_string_truncate(to, ZSTR_LEN(to), 0);
  2874. RETURN_NEW_STR(to);
  2875. }
  2876. /* }}} */
  2877. /* {{{ Escape binary for bytea type */
  2878. PHP_FUNCTION(pg_escape_bytea)
  2879. {
  2880. zend_string *from;
  2881. char *to = NULL;
  2882. size_t to_len;
  2883. PGconn *pgsql;
  2884. zval *pgsql_link;
  2885. pgsql_link_handle *link;
  2886. switch (ZEND_NUM_ARGS()) {
  2887. case 1:
  2888. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &from) == FAILURE) {
  2889. RETURN_THROWS();
  2890. }
  2891. link = FETCH_DEFAULT_LINK();
  2892. break;
  2893. default:
  2894. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &pgsql_link, pgsql_link_ce, &from) == FAILURE) {
  2895. RETURN_THROWS();
  2896. }
  2897. link = Z_PGSQL_LINK_P(pgsql_link);
  2898. CHECK_PGSQL_LINK(link);
  2899. break;
  2900. }
  2901. if (link) {
  2902. pgsql = link->conn;
  2903. to = (char *)PQescapeByteaConn(pgsql, (unsigned char *)ZSTR_VAL(from), ZSTR_LEN(from), &to_len);
  2904. } else {
  2905. to = (char *)PQescapeBytea((unsigned char *)ZSTR_VAL(from), ZSTR_LEN(from), &to_len);
  2906. }
  2907. RETVAL_STRINGL(to, to_len-1); /* to_len includes additional '\0' */
  2908. PQfreemem(to);
  2909. }
  2910. /* }}} */
  2911. /* {{{ Unescape binary for bytea type */
  2912. PHP_FUNCTION(pg_unescape_bytea)
  2913. {
  2914. char *from, *tmp;
  2915. size_t to_len;
  2916. size_t from_len;
  2917. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &from, &from_len) == FAILURE) {
  2918. RETURN_THROWS();
  2919. }
  2920. tmp = (char *)PQunescapeBytea((unsigned char*)from, &to_len);
  2921. if (!tmp) {
  2922. zend_error(E_ERROR, "Out of memory");
  2923. return;
  2924. }
  2925. RETVAL_STRINGL(tmp, to_len);
  2926. PQfreemem(tmp);
  2927. }
  2928. /* }}} */
  2929. static void php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAMETERS, int escape_literal) /* {{{ */ {
  2930. zend_string *from = NULL;
  2931. zval *pgsql_link = NULL;
  2932. PGconn *pgsql;
  2933. char *tmp;
  2934. pgsql_link_handle *link;
  2935. switch (ZEND_NUM_ARGS()) {
  2936. case 1:
  2937. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &from) == FAILURE) {
  2938. RETURN_THROWS();
  2939. }
  2940. link = FETCH_DEFAULT_LINK();
  2941. CHECK_DEFAULT_LINK(link);
  2942. break;
  2943. default:
  2944. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &pgsql_link, pgsql_link_ce, &from) == FAILURE) {
  2945. RETURN_THROWS();
  2946. }
  2947. link = Z_PGSQL_LINK_P(pgsql_link);
  2948. CHECK_PGSQL_LINK(link);
  2949. break;
  2950. }
  2951. pgsql = link->conn;
  2952. if (escape_literal) {
  2953. tmp = PQescapeLiteral(pgsql, ZSTR_VAL(from), ZSTR_LEN(from));
  2954. } else {
  2955. tmp = PQescapeIdentifier(pgsql, ZSTR_VAL(from), ZSTR_LEN(from));
  2956. }
  2957. if (!tmp) {
  2958. php_error_docref(NULL, E_WARNING,"Failed to escape");
  2959. RETURN_FALSE;
  2960. }
  2961. RETVAL_STRING(tmp);
  2962. PQfreemem(tmp);
  2963. }
  2964. /* }}} */
  2965. /* {{{ Escape parameter as string literal (i.e. parameter) */
  2966. PHP_FUNCTION(pg_escape_literal)
  2967. {
  2968. php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2969. }
  2970. /* }}} */
  2971. /* {{{ Escape identifier (i.e. table name, field name) */
  2972. PHP_FUNCTION(pg_escape_identifier)
  2973. {
  2974. php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2975. }
  2976. /* }}} */
  2977. /* {{{ Get error message associated with result */
  2978. PHP_FUNCTION(pg_result_error)
  2979. {
  2980. zval *result;
  2981. PGresult *pgsql_result;
  2982. pgsql_result_handle *pg_result;
  2983. char *err = NULL;
  2984. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &result, pgsql_result_ce) == FAILURE) {
  2985. RETURN_THROWS();
  2986. }
  2987. pg_result = Z_PGSQL_RESULT_P(result);
  2988. pgsql_result = pg_result->result;
  2989. if (!pgsql_result) {
  2990. RETURN_FALSE;
  2991. }
  2992. err = (char *)PQresultErrorMessage(pgsql_result);
  2993. RETURN_STRING(err);
  2994. }
  2995. /* }}} */
  2996. /* {{{ Get error message field associated with result */
  2997. PHP_FUNCTION(pg_result_error_field)
  2998. {
  2999. zval *result;
  3000. zend_long fieldcode;
  3001. PGresult *pgsql_result;
  3002. pgsql_result_handle *pg_result;
  3003. char *field = NULL;
  3004. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &result, pgsql_result_ce, &fieldcode) == FAILURE) {
  3005. RETURN_THROWS();
  3006. }
  3007. pg_result = Z_PGSQL_RESULT_P(result);
  3008. pgsql_result = pg_result->result;
  3009. if (!pgsql_result) {
  3010. RETURN_FALSE;
  3011. }
  3012. if (fieldcode & (PG_DIAG_SEVERITY|PG_DIAG_SQLSTATE|PG_DIAG_MESSAGE_PRIMARY|PG_DIAG_MESSAGE_DETAIL
  3013. |PG_DIAG_MESSAGE_HINT|PG_DIAG_STATEMENT_POSITION
  3014. #ifdef PG_DIAG_INTERNAL_POSITION
  3015. |PG_DIAG_INTERNAL_POSITION
  3016. #endif
  3017. #ifdef PG_DIAG_INTERNAL_QUERY
  3018. |PG_DIAG_INTERNAL_QUERY
  3019. #endif
  3020. |PG_DIAG_CONTEXT|PG_DIAG_SOURCE_FILE|PG_DIAG_SOURCE_LINE
  3021. |PG_DIAG_SOURCE_FUNCTION)) {
  3022. field = (char *)PQresultErrorField(pgsql_result, (int)fieldcode);
  3023. if (field == NULL) {
  3024. RETURN_NULL();
  3025. } else {
  3026. RETURN_STRING(field);
  3027. }
  3028. } else {
  3029. RETURN_FALSE;
  3030. }
  3031. }
  3032. /* }}} */
  3033. /* {{{ Get connection status */
  3034. PHP_FUNCTION(pg_connection_status)
  3035. {
  3036. zval *pgsql_link = NULL;
  3037. pgsql_link_handle *link;
  3038. PGconn *pgsql;
  3039. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3040. RETURN_THROWS();
  3041. }
  3042. link = Z_PGSQL_LINK_P(pgsql_link);
  3043. CHECK_PGSQL_LINK(link);
  3044. pgsql = link->conn;
  3045. RETURN_LONG(PQstatus(pgsql));
  3046. }
  3047. /* }}} */
  3048. /* {{{ Get transaction status */
  3049. PHP_FUNCTION(pg_transaction_status)
  3050. {
  3051. zval *pgsql_link = NULL;
  3052. pgsql_link_handle *link;
  3053. PGconn *pgsql;
  3054. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3055. RETURN_THROWS();
  3056. }
  3057. link = Z_PGSQL_LINK_P(pgsql_link);
  3058. CHECK_PGSQL_LINK(link);
  3059. pgsql = link->conn;
  3060. RETURN_LONG(PQtransactionStatus(pgsql));
  3061. }
  3062. /* }}} */
  3063. /* {{{ Reset connection (reconnect) */
  3064. PHP_FUNCTION(pg_connection_reset)
  3065. {
  3066. zval *pgsql_link;
  3067. pgsql_link_handle *link;
  3068. PGconn *pgsql;
  3069. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3070. RETURN_THROWS();
  3071. }
  3072. link = Z_PGSQL_LINK_P(pgsql_link);
  3073. CHECK_PGSQL_LINK(link);
  3074. pgsql = link->conn;
  3075. PQreset(pgsql);
  3076. if (PQstatus(pgsql) == CONNECTION_BAD) {
  3077. RETURN_FALSE;
  3078. }
  3079. RETURN_TRUE;
  3080. }
  3081. /* }}} */
  3082. #define PHP_PG_ASYNC_IS_BUSY 1
  3083. #define PHP_PG_ASYNC_REQUEST_CANCEL 2
  3084. /* {{{ php_pgsql_flush_query */
  3085. static int php_pgsql_flush_query(PGconn *pgsql)
  3086. {
  3087. PGresult *res;
  3088. int leftover = 0;
  3089. if (PQsetnonblocking(pgsql, 1)) {
  3090. php_error_docref(NULL, E_NOTICE,"Cannot set connection to nonblocking mode");
  3091. return -1;
  3092. }
  3093. while ((res = PQgetResult(pgsql))) {
  3094. PQclear(res);
  3095. leftover++;
  3096. }
  3097. PQsetnonblocking(pgsql, 0);
  3098. return leftover;
  3099. }
  3100. /* }}} */
  3101. /* {{{ php_pgsql_do_async */
  3102. static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  3103. {
  3104. zval *pgsql_link;
  3105. pgsql_link_handle *link;
  3106. PGconn *pgsql;
  3107. PGresult *pgsql_result;
  3108. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3109. RETURN_THROWS();
  3110. }
  3111. link = Z_PGSQL_LINK_P(pgsql_link);
  3112. CHECK_PGSQL_LINK(link);
  3113. pgsql = link->conn;
  3114. if (PQsetnonblocking(pgsql, 1)) {
  3115. php_error_docref(NULL, E_NOTICE, "Cannot set connection to nonblocking mode");
  3116. RETURN_FALSE;
  3117. }
  3118. switch(entry_type) {
  3119. case PHP_PG_ASYNC_IS_BUSY:
  3120. PQconsumeInput(pgsql);
  3121. RETVAL_LONG(PQisBusy(pgsql));
  3122. break;
  3123. case PHP_PG_ASYNC_REQUEST_CANCEL:
  3124. RETVAL_LONG(PQrequestCancel(pgsql));
  3125. while ((pgsql_result = PQgetResult(pgsql))) {
  3126. PQclear(pgsql_result);
  3127. }
  3128. break;
  3129. EMPTY_SWITCH_DEFAULT_CASE()
  3130. }
  3131. if (PQsetnonblocking(pgsql, 0)) {
  3132. php_error_docref(NULL, E_NOTICE, "Cannot set connection to blocking mode");
  3133. }
  3134. convert_to_boolean(return_value);
  3135. }
  3136. /* }}} */
  3137. /* {{{ Cancel request */
  3138. PHP_FUNCTION(pg_cancel_query)
  3139. {
  3140. php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_REQUEST_CANCEL);
  3141. }
  3142. /* }}} */
  3143. /* {{{ Get connection is busy or not */
  3144. PHP_FUNCTION(pg_connection_busy)
  3145. {
  3146. php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_IS_BUSY);
  3147. }
  3148. /* }}} */
  3149. static bool _php_pgsql_link_has_results(PGconn *pgsql) /* {{{ */
  3150. {
  3151. PGresult *result;
  3152. while ((result = PQgetResult(pgsql))) {
  3153. PQclear(result);
  3154. return true;
  3155. }
  3156. return false;
  3157. }
  3158. /* }}} */
  3159. /* {{{ Send asynchronous query */
  3160. PHP_FUNCTION(pg_send_query)
  3161. {
  3162. zval *pgsql_link;
  3163. pgsql_link_handle *link;
  3164. char *query;
  3165. size_t len;
  3166. PGconn *pgsql;
  3167. int is_non_blocking;
  3168. int ret;
  3169. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pgsql_link, pgsql_link_ce, &query, &len) == FAILURE) {
  3170. RETURN_THROWS();
  3171. }
  3172. link = Z_PGSQL_LINK_P(pgsql_link);
  3173. CHECK_PGSQL_LINK(link);
  3174. pgsql = link->conn;
  3175. is_non_blocking = PQisnonblocking(pgsql);
  3176. if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
  3177. php_error_docref(NULL, E_NOTICE, "Cannot set connection to nonblocking mode");
  3178. RETURN_FALSE;
  3179. }
  3180. if (_php_pgsql_link_has_results(pgsql)) {
  3181. php_error_docref(NULL, E_NOTICE,
  3182. "There are results on this connection. Call pg_get_result() until it returns FALSE");
  3183. }
  3184. if (is_non_blocking) {
  3185. if (!PQsendQuery(pgsql, query)) {
  3186. RETURN_FALSE;
  3187. }
  3188. ret = PQflush(pgsql);
  3189. } else {
  3190. if (!PQsendQuery(pgsql, query)) {
  3191. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
  3192. PQreset(pgsql);
  3193. }
  3194. if (!PQsendQuery(pgsql, query)) {
  3195. RETURN_FALSE;
  3196. }
  3197. }
  3198. /* Wait to finish sending buffer */
  3199. while ((ret = PQflush(pgsql))) {
  3200. if (ret == -1) {
  3201. php_error_docref(NULL, E_NOTICE, "Could not empty PostgreSQL send buffer");
  3202. break;
  3203. }
  3204. usleep(10000);
  3205. }
  3206. if (PQsetnonblocking(pgsql, 0)) {
  3207. php_error_docref(NULL, E_NOTICE, "Cannot set connection to blocking mode");
  3208. }
  3209. }
  3210. if (ret == 0) {
  3211. RETURN_TRUE;
  3212. } else if (ret == -1) {
  3213. RETURN_FALSE;
  3214. } else {
  3215. RETURN_LONG(0);
  3216. }
  3217. }
  3218. /* }}} */
  3219. /* {{{ Send asynchronous parameterized query */
  3220. PHP_FUNCTION(pg_send_query_params)
  3221. {
  3222. zval *pgsql_link, *pv_param_arr, *tmp;
  3223. pgsql_link_handle *link;
  3224. int num_params = 0;
  3225. char **params = NULL;
  3226. char *query;
  3227. size_t query_len;
  3228. PGconn *pgsql;
  3229. int is_non_blocking;
  3230. int ret;
  3231. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osa", &pgsql_link, pgsql_link_ce, &query, &query_len, &pv_param_arr) == FAILURE) {
  3232. RETURN_THROWS();
  3233. }
  3234. link = Z_PGSQL_LINK_P(pgsql_link);
  3235. CHECK_PGSQL_LINK(link);
  3236. pgsql = link->conn;
  3237. is_non_blocking = PQisnonblocking(pgsql);
  3238. if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
  3239. php_error_docref(NULL, E_NOTICE, "Cannot set connection to nonblocking mode");
  3240. RETURN_FALSE;
  3241. }
  3242. if (_php_pgsql_link_has_results(pgsql)) {
  3243. php_error_docref(NULL, E_NOTICE,
  3244. "There are results on this connection. Call pg_get_result() until it returns FALSE");
  3245. }
  3246. num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
  3247. if (num_params > 0) {
  3248. int i = 0;
  3249. params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
  3250. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) {
  3251. if (Z_TYPE_P(tmp) == IS_NULL) {
  3252. params[i] = NULL;
  3253. } else {
  3254. zend_string *tmp_str;
  3255. zend_string *str = zval_get_tmp_string(tmp, &tmp_str);
  3256. params[i] = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
  3257. zend_tmp_string_release(tmp_str);
  3258. }
  3259. i++;
  3260. } ZEND_HASH_FOREACH_END();
  3261. }
  3262. if (PQsendQueryParams(pgsql, query, num_params, NULL, (const char * const *)params, NULL, NULL, 0)) {
  3263. _php_pgsql_free_params(params, num_params);
  3264. } else if (is_non_blocking) {
  3265. _php_pgsql_free_params(params, num_params);
  3266. RETURN_FALSE;
  3267. } else {
  3268. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
  3269. PQreset(pgsql);
  3270. }
  3271. if (!PQsendQueryParams(pgsql, query, num_params, NULL, (const char * const *)params, NULL, NULL, 0)) {
  3272. _php_pgsql_free_params(params, num_params);
  3273. RETURN_FALSE;
  3274. }
  3275. }
  3276. if (is_non_blocking) {
  3277. ret = PQflush(pgsql);
  3278. } else {
  3279. /* Wait to finish sending buffer */
  3280. while ((ret = PQflush(pgsql))) {
  3281. if (ret == -1) {
  3282. php_error_docref(NULL, E_NOTICE, "Could not empty PostgreSQL send buffer");
  3283. break;
  3284. }
  3285. usleep(10000);
  3286. }
  3287. if (PQsetnonblocking(pgsql, 0) != 0) {
  3288. php_error_docref(NULL, E_NOTICE, "Cannot set connection to blocking mode");
  3289. }
  3290. }
  3291. if (ret == 0) {
  3292. RETURN_TRUE;
  3293. } else if (ret == -1) {
  3294. RETURN_FALSE;
  3295. } else {
  3296. RETURN_LONG(0);
  3297. }
  3298. }
  3299. /* }}} */
  3300. /* {{{ Asynchronously prepare a query for future execution */
  3301. PHP_FUNCTION(pg_send_prepare)
  3302. {
  3303. zval *pgsql_link;
  3304. pgsql_link_handle *link;
  3305. char *query, *stmtname;
  3306. size_t stmtname_len, query_len;
  3307. PGconn *pgsql;
  3308. int is_non_blocking;
  3309. int ret;
  3310. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss", &pgsql_link, pgsql_link_ce, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
  3311. RETURN_THROWS();
  3312. }
  3313. link = Z_PGSQL_LINK_P(pgsql_link);
  3314. CHECK_PGSQL_LINK(link);
  3315. pgsql = link->conn;
  3316. is_non_blocking = PQisnonblocking(pgsql);
  3317. if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
  3318. php_error_docref(NULL, E_NOTICE, "Cannot set connection to nonblocking mode");
  3319. RETURN_FALSE;
  3320. }
  3321. if (_php_pgsql_link_has_results(pgsql)) {
  3322. php_error_docref(NULL, E_NOTICE,
  3323. "There are results on this connection. Call pg_get_result() until it returns FALSE");
  3324. }
  3325. if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) {
  3326. if (is_non_blocking) {
  3327. RETURN_FALSE;
  3328. } else {
  3329. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
  3330. PQreset(pgsql);
  3331. }
  3332. if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) {
  3333. RETURN_FALSE;
  3334. }
  3335. }
  3336. }
  3337. if (is_non_blocking) {
  3338. ret = PQflush(pgsql);
  3339. } else {
  3340. /* Wait to finish sending buffer */
  3341. while ((ret = PQflush(pgsql))) {
  3342. if (ret == -1) {
  3343. php_error_docref(NULL, E_NOTICE, "Could not empty PostgreSQL send buffer");
  3344. break;
  3345. }
  3346. usleep(10000);
  3347. }
  3348. if (PQsetnonblocking(pgsql, 0) != 0) {
  3349. php_error_docref(NULL, E_NOTICE, "Cannot set connection to blocking mode");
  3350. }
  3351. }
  3352. if (ret == 0) {
  3353. RETURN_TRUE;
  3354. } else if (ret == -1) {
  3355. RETURN_FALSE;
  3356. } else {
  3357. RETURN_LONG(0);
  3358. }
  3359. }
  3360. /* }}} */
  3361. /* {{{ Executes prevriously prepared stmtname asynchronously */
  3362. PHP_FUNCTION(pg_send_execute)
  3363. {
  3364. zval *pgsql_link;
  3365. pgsql_link_handle *link;
  3366. zval *pv_param_arr, *tmp;
  3367. int num_params = 0;
  3368. char **params = NULL;
  3369. char *stmtname;
  3370. size_t stmtname_len;
  3371. PGconn *pgsql;
  3372. int is_non_blocking;
  3373. int ret;
  3374. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osa", &pgsql_link, pgsql_link_ce, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
  3375. RETURN_THROWS();
  3376. }
  3377. link = Z_PGSQL_LINK_P(pgsql_link);
  3378. CHECK_PGSQL_LINK(link);
  3379. pgsql = link->conn;
  3380. is_non_blocking = PQisnonblocking(pgsql);
  3381. if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
  3382. php_error_docref(NULL, E_NOTICE, "Cannot set connection to nonblocking mode");
  3383. RETURN_FALSE;
  3384. }
  3385. if (_php_pgsql_link_has_results(pgsql)) {
  3386. php_error_docref(NULL, E_NOTICE,
  3387. "There are results on this connection. Call pg_get_result() until it returns FALSE");
  3388. }
  3389. num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
  3390. if (num_params > 0) {
  3391. int i = 0;
  3392. params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
  3393. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) {
  3394. if (Z_TYPE_P(tmp) == IS_NULL) {
  3395. params[i] = NULL;
  3396. } else {
  3397. zend_string *tmp_str = zval_try_get_string(tmp);
  3398. if (UNEXPECTED(!tmp_str)) {
  3399. _php_pgsql_free_params(params, i);
  3400. return;
  3401. }
  3402. params[i] = estrndup(ZSTR_VAL(tmp_str), ZSTR_LEN(tmp_str));
  3403. zend_string_release(tmp_str);
  3404. }
  3405. i++;
  3406. } ZEND_HASH_FOREACH_END();
  3407. }
  3408. if (PQsendQueryPrepared(pgsql, stmtname, num_params, (const char * const *)params, NULL, NULL, 0)) {
  3409. _php_pgsql_free_params(params, num_params);
  3410. } else if (is_non_blocking) {
  3411. _php_pgsql_free_params(params, num_params);
  3412. RETURN_FALSE;
  3413. } else {
  3414. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
  3415. PQreset(pgsql);
  3416. }
  3417. if (!PQsendQueryPrepared(pgsql, stmtname, num_params, (const char * const *)params, NULL, NULL, 0)) {
  3418. _php_pgsql_free_params(params, num_params);
  3419. RETURN_FALSE;
  3420. }
  3421. }
  3422. if (is_non_blocking) {
  3423. ret = PQflush(pgsql);
  3424. } else {
  3425. /* Wait to finish sending buffer */
  3426. while ((ret = PQflush(pgsql))) {
  3427. if (ret == -1) {
  3428. php_error_docref(NULL, E_NOTICE, "Could not empty PostgreSQL send buffer");
  3429. break;
  3430. }
  3431. usleep(10000);
  3432. }
  3433. if (PQsetnonblocking(pgsql, 0) != 0) {
  3434. php_error_docref(NULL, E_NOTICE, "Cannot set connection to blocking mode");
  3435. }
  3436. }
  3437. if (ret == 0) {
  3438. RETURN_TRUE;
  3439. } else if (ret == -1) {
  3440. RETURN_FALSE;
  3441. } else {
  3442. RETURN_LONG(0);
  3443. }
  3444. }
  3445. /* }}} */
  3446. /* {{{ Get asynchronous query result */
  3447. PHP_FUNCTION(pg_get_result)
  3448. {
  3449. zval *pgsql_link;
  3450. pgsql_link_handle *link;
  3451. PGconn *pgsql;
  3452. PGresult *pgsql_result;
  3453. pgsql_result_handle *pg_result;
  3454. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3455. RETURN_THROWS();
  3456. }
  3457. link = Z_PGSQL_LINK_P(pgsql_link);
  3458. CHECK_PGSQL_LINK(link);
  3459. pgsql = link->conn;
  3460. pgsql_result = PQgetResult(pgsql);
  3461. if (!pgsql_result) {
  3462. /* no result */
  3463. RETURN_FALSE;
  3464. }
  3465. object_init_ex(return_value, pgsql_result_ce);
  3466. pg_result = Z_PGSQL_RESULT_P(return_value);
  3467. pg_result->conn = pgsql;
  3468. pg_result->result = pgsql_result;
  3469. pg_result->row = 0;
  3470. }
  3471. /* }}} */
  3472. /* {{{ Get status of query result */
  3473. PHP_FUNCTION(pg_result_status)
  3474. {
  3475. zval *result;
  3476. zend_long result_type = PGSQL_STATUS_LONG;
  3477. ExecStatusType status;
  3478. PGresult *pgsql_result;
  3479. pgsql_result_handle *pg_result;
  3480. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &result, pgsql_result_ce, &result_type) == FAILURE) {
  3481. RETURN_THROWS();
  3482. }
  3483. pg_result = Z_PGSQL_RESULT_P(result);
  3484. CHECK_PGSQL_RESULT(pg_result);
  3485. pgsql_result = pg_result->result;
  3486. if (result_type == PGSQL_STATUS_LONG) {
  3487. status = PQresultStatus(pgsql_result);
  3488. RETURN_LONG((int)status);
  3489. }
  3490. else if (result_type == PGSQL_STATUS_STRING) {
  3491. RETURN_STRING(PQcmdStatus(pgsql_result));
  3492. } else {
  3493. zend_argument_value_error(2, "must be either PGSQL_STATUS_LONG or PGSQL_STATUS_STRING");
  3494. RETURN_THROWS();
  3495. }
  3496. }
  3497. /* }}} */
  3498. /* {{{ Get asynchronous notification */
  3499. PHP_FUNCTION(pg_get_notify)
  3500. {
  3501. zval *pgsql_link;
  3502. pgsql_link_handle *link;
  3503. zend_long result_type = PGSQL_ASSOC;
  3504. PGconn *pgsql;
  3505. PGnotify *pgsql_notify;
  3506. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &pgsql_link, pgsql_link_ce, &result_type) == FAILURE) {
  3507. RETURN_THROWS();
  3508. }
  3509. link = Z_PGSQL_LINK_P(pgsql_link);
  3510. CHECK_PGSQL_LINK(link);
  3511. pgsql = link->conn;
  3512. if (!(result_type & PGSQL_BOTH)) {
  3513. zend_argument_value_error(2, "must be one of PGSQL_ASSOC, PGSQL_NUM, or PGSQL_BOTH");
  3514. RETURN_THROWS();
  3515. }
  3516. PQconsumeInput(pgsql);
  3517. pgsql_notify = PQnotifies(pgsql);
  3518. if (!pgsql_notify) {
  3519. /* no notify message */
  3520. RETURN_FALSE;
  3521. }
  3522. array_init(return_value);
  3523. if (result_type & PGSQL_NUM) {
  3524. add_index_string(return_value, 0, pgsql_notify->relname);
  3525. add_index_long(return_value, 1, pgsql_notify->be_pid);
  3526. /* consider to use php_version_compare() here */
  3527. if (PQprotocolVersion(pgsql) >= 3 && zend_strtod(PQparameterStatus(pgsql, "server_version"), NULL) >= 9.0) {
  3528. add_index_string(return_value, 2, pgsql_notify->extra);
  3529. }
  3530. }
  3531. if (result_type & PGSQL_ASSOC) {
  3532. add_assoc_string(return_value, "message", pgsql_notify->relname);
  3533. add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
  3534. /* consider to use php_version_compare() here */
  3535. if (PQprotocolVersion(pgsql) >= 3 && zend_strtod(PQparameterStatus(pgsql, "server_version"), NULL) >= 9.0) {
  3536. add_assoc_string(return_value, "payload", pgsql_notify->extra);
  3537. }
  3538. }
  3539. PQfreemem(pgsql_notify);
  3540. }
  3541. /* }}} */
  3542. /* {{{ Get backend(server) pid */
  3543. PHP_FUNCTION(pg_get_pid)
  3544. {
  3545. zval *pgsql_link;
  3546. pgsql_link_handle *link;
  3547. PGconn *pgsql;
  3548. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3549. RETURN_THROWS();
  3550. }
  3551. link = Z_PGSQL_LINK_P(pgsql_link);
  3552. CHECK_PGSQL_LINK(link);
  3553. pgsql = link->conn;
  3554. RETURN_LONG(PQbackendPID(pgsql));
  3555. }
  3556. /* }}} */
  3557. static ssize_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count) /* {{{ */
  3558. {
  3559. return -1;
  3560. }
  3561. /* }}} */
  3562. static ssize_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count) /* {{{ */
  3563. {
  3564. return -1;
  3565. }
  3566. /* }}} */
  3567. static int php_pgsql_fd_close(php_stream *stream, int close_handle) /* {{{ */
  3568. {
  3569. return EOF;
  3570. }
  3571. /* }}} */
  3572. static int php_pgsql_fd_flush(php_stream *stream) /* {{{ */
  3573. {
  3574. return FAILURE;
  3575. }
  3576. /* }}} */
  3577. static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam) /* {{{ */
  3578. {
  3579. PGconn *pgsql = (PGconn *) stream->abstract;
  3580. switch (option) {
  3581. case PHP_STREAM_OPTION_BLOCKING:
  3582. return PQsetnonblocking(pgsql, value);
  3583. default:
  3584. return FAILURE;
  3585. }
  3586. }
  3587. /* }}} */
  3588. static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret) /* {{{ */
  3589. {
  3590. PGconn *pgsql = (PGconn *) stream->abstract;
  3591. switch (cast_as) {
  3592. case PHP_STREAM_AS_FD_FOR_SELECT:
  3593. case PHP_STREAM_AS_FD:
  3594. case PHP_STREAM_AS_SOCKETD: {
  3595. int fd_number = PQsocket(pgsql);
  3596. if (fd_number == -1) {
  3597. return FAILURE;
  3598. }
  3599. if (ret) {
  3600. *(php_socket_t *)ret = fd_number;
  3601. }
  3602. }
  3603. return SUCCESS;
  3604. ZEND_FALLTHROUGH;
  3605. default:
  3606. return FAILURE;
  3607. }
  3608. }
  3609. /* }}} */
  3610. /* {{{ Get a read-only handle to the socket underlying the pgsql connection */
  3611. PHP_FUNCTION(pg_socket)
  3612. {
  3613. zval *pgsql_link;
  3614. pgsql_link_handle *link;
  3615. php_stream *stream;
  3616. PGconn *pgsql;
  3617. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3618. RETURN_THROWS();
  3619. }
  3620. link = Z_PGSQL_LINK_P(pgsql_link);
  3621. CHECK_PGSQL_LINK(link);
  3622. pgsql = link->conn;
  3623. stream = php_stream_alloc(&php_stream_pgsql_fd_ops, pgsql, NULL, "r");
  3624. if (stream) {
  3625. php_stream_to_zval(stream, return_value);
  3626. return;
  3627. }
  3628. RETURN_FALSE;
  3629. }
  3630. /* }}} */
  3631. /* {{{ Reads input on the connection */
  3632. PHP_FUNCTION(pg_consume_input)
  3633. {
  3634. zval *pgsql_link;
  3635. pgsql_link_handle *link;
  3636. PGconn *pgsql;
  3637. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3638. RETURN_THROWS();
  3639. }
  3640. link = Z_PGSQL_LINK_P(pgsql_link);
  3641. CHECK_PGSQL_LINK(link);
  3642. pgsql = link->conn;
  3643. RETURN_BOOL(PQconsumeInput(pgsql));
  3644. }
  3645. /* }}} */
  3646. /* {{{ Flush outbound query data on the connection */
  3647. PHP_FUNCTION(pg_flush)
  3648. {
  3649. zval *pgsql_link;
  3650. pgsql_link_handle *link;
  3651. PGconn *pgsql;
  3652. int ret;
  3653. int is_non_blocking;
  3654. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pgsql_link, pgsql_link_ce) == FAILURE) {
  3655. RETURN_THROWS();
  3656. }
  3657. link = Z_PGSQL_LINK_P(pgsql_link);
  3658. CHECK_PGSQL_LINK(link);
  3659. pgsql = link->conn;
  3660. is_non_blocking = PQisnonblocking(pgsql);
  3661. if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
  3662. php_error_docref(NULL, E_NOTICE, "Cannot set connection to nonblocking mode");
  3663. RETURN_FALSE;
  3664. }
  3665. ret = PQflush(pgsql);
  3666. if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 0) == -1) {
  3667. php_error_docref(NULL, E_NOTICE, "Failed resetting connection to blocking mode");
  3668. }
  3669. switch (ret) {
  3670. case 0: RETURN_TRUE; break;
  3671. case 1: RETURN_LONG(0); break;
  3672. default: RETURN_FALSE;
  3673. }
  3674. }
  3675. /* }}} */
  3676. /* {{{ php_pgsql_meta_data
  3677. * table_name must not be empty
  3678. * TODO: Add meta_data cache for better performance
  3679. */
  3680. PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended)
  3681. {
  3682. PGresult *pg_result;
  3683. char *src, *tmp_name, *tmp_name2 = NULL;
  3684. char *escaped;
  3685. smart_str querystr = {0};
  3686. size_t new_len;
  3687. int i, num_rows;
  3688. zval elem;
  3689. ZEND_ASSERT(ZSTR_LEN(table_name) != 0);
  3690. src = estrdup(ZSTR_VAL(table_name));
  3691. tmp_name = php_strtok_r(src, ".", &tmp_name2);
  3692. if (!tmp_name) {
  3693. // TODO ValueError (empty table name)?
  3694. efree(src);
  3695. php_error_docref(NULL, E_WARNING, "The table name must be specified");
  3696. return FAILURE;
  3697. }
  3698. if (!tmp_name2 || !*tmp_name2) {
  3699. /* Default schema */
  3700. tmp_name2 = tmp_name;
  3701. tmp_name = "public";
  3702. }
  3703. if (extended) {
  3704. smart_str_appends(&querystr,
  3705. "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotNULL, a.atthasdef, a.attndims, t.typtype, "
  3706. "d.description "
  3707. "FROM pg_class as c "
  3708. " JOIN pg_attribute a ON (a.attrelid = c.oid) "
  3709. " JOIN pg_type t ON (a.atttypid = t.oid) "
  3710. " JOIN pg_namespace n ON (c.relnamespace = n.oid) "
  3711. " LEFT JOIN pg_description d ON (d.objoid=a.attrelid AND d.objsubid=a.attnum AND c.oid=d.objoid) "
  3712. "WHERE a.attnum > 0 AND c.relname = '");
  3713. } else {
  3714. smart_str_appends(&querystr,
  3715. "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotnull, a.atthasdef, a.attndims, t.typtype "
  3716. "FROM pg_class as c "
  3717. " JOIN pg_attribute a ON (a.attrelid = c.oid) "
  3718. " JOIN pg_type t ON (a.atttypid = t.oid) "
  3719. " JOIN pg_namespace n ON (c.relnamespace = n.oid) "
  3720. "WHERE a.attnum > 0 AND c.relname = '");
  3721. }
  3722. escaped = (char *)safe_emalloc(strlen(tmp_name2), 2, 1);
  3723. new_len = PQescapeStringConn(pg_link, escaped, tmp_name2, strlen(tmp_name2), NULL);
  3724. if (new_len) {
  3725. smart_str_appendl(&querystr, escaped, new_len);
  3726. }
  3727. efree(escaped);
  3728. smart_str_appends(&querystr, "' AND n.nspname = '");
  3729. escaped = (char *)safe_emalloc(strlen(tmp_name), 2, 1);
  3730. new_len = PQescapeStringConn(pg_link, escaped, tmp_name, strlen(tmp_name), NULL);
  3731. if (new_len) {
  3732. smart_str_appendl(&querystr, escaped, new_len);
  3733. }
  3734. efree(escaped);
  3735. smart_str_appends(&querystr, "' ORDER BY a.attnum;");
  3736. smart_str_0(&querystr);
  3737. efree(src);
  3738. pg_result = PQexec(pg_link, ZSTR_VAL(querystr.s));
  3739. if (PQresultStatus(pg_result) != PGRES_TUPLES_OK || (num_rows = PQntuples(pg_result)) == 0) {
  3740. php_error_docref(NULL, E_WARNING, "Table '%s' doesn't exists", ZSTR_VAL(table_name));
  3741. smart_str_free(&querystr);
  3742. PQclear(pg_result);
  3743. return FAILURE;
  3744. }
  3745. smart_str_free(&querystr);
  3746. for (i = 0; i < num_rows; i++) {
  3747. char *name;
  3748. array_init(&elem);
  3749. /* pg_attribute.attnum */
  3750. add_assoc_long_ex(&elem, "num", sizeof("num") - 1, atoi(PQgetvalue(pg_result, i, 1)));
  3751. /* pg_type.typname */
  3752. add_assoc_string_ex(&elem, "type", sizeof("type") - 1, PQgetvalue(pg_result, i, 2));
  3753. /* pg_attribute.attlen */
  3754. add_assoc_long_ex(&elem, "len", sizeof("len") - 1, atoi(PQgetvalue(pg_result,i,3)));
  3755. /* pg_attribute.attnonull */
  3756. add_assoc_bool_ex(&elem, "not null", sizeof("not null") - 1, !strcmp(PQgetvalue(pg_result, i, 4), "t"));
  3757. /* pg_attribute.atthasdef */
  3758. add_assoc_bool_ex(&elem, "has default", sizeof("has default") - 1, !strcmp(PQgetvalue(pg_result,i,5), "t"));
  3759. /* pg_attribute.attndims */
  3760. add_assoc_long_ex(&elem, "array dims", sizeof("array dims") - 1, atoi(PQgetvalue(pg_result, i, 6)));
  3761. /* pg_type.typtype */
  3762. add_assoc_bool_ex(&elem, "is enum", sizeof("is enum") - 1, !strcmp(PQgetvalue(pg_result, i, 7), "e"));
  3763. if (extended) {
  3764. /* pg_type.typtype */
  3765. add_assoc_bool_ex(&elem, "is base", sizeof("is base") - 1, !strcmp(PQgetvalue(pg_result, i, 7), "b"));
  3766. add_assoc_bool_ex(&elem, "is composite", sizeof("is composite") - 1, !strcmp(PQgetvalue(pg_result, i, 7), "c"));
  3767. add_assoc_bool_ex(&elem, "is pesudo", sizeof("is pesudo") - 1, !strcmp(PQgetvalue(pg_result, i, 7), "p"));
  3768. /* pg_description.description */
  3769. add_assoc_string_ex(&elem, "description", sizeof("description") - 1, PQgetvalue(pg_result, i, 8));
  3770. }
  3771. /* pg_attribute.attname */
  3772. name = PQgetvalue(pg_result,i,0);
  3773. add_assoc_zval(meta, name, &elem);
  3774. }
  3775. PQclear(pg_result);
  3776. return SUCCESS;
  3777. }
  3778. /* }}} */
  3779. /* {{{ Get meta_data */
  3780. PHP_FUNCTION(pg_meta_data)
  3781. {
  3782. zval *pgsql_link;
  3783. pgsql_link_handle *link;
  3784. zend_string *table_name;
  3785. bool extended=0;
  3786. PGconn *pgsql;
  3787. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|b",
  3788. &pgsql_link, pgsql_link_ce, &table_name, &extended) == FAILURE
  3789. ) {
  3790. RETURN_THROWS();
  3791. }
  3792. link = Z_PGSQL_LINK_P(pgsql_link);
  3793. CHECK_PGSQL_LINK(link);
  3794. pgsql = link->conn;
  3795. /* php_pgsql_meta_data() asserts that table_name is not empty */
  3796. if (ZSTR_LEN(table_name) == 0) {
  3797. zend_argument_value_error(2, "cannot be empty");
  3798. RETURN_THROWS();
  3799. }
  3800. array_init(return_value);
  3801. if (php_pgsql_meta_data(pgsql, table_name, return_value, extended) == FAILURE) {
  3802. zend_array_destroy(Z_ARR_P(return_value)); /* destroy array */
  3803. RETURN_FALSE;
  3804. }
  3805. }
  3806. /* }}} */
  3807. /* {{{ php_pgsql_get_data_type */
  3808. static php_pgsql_data_type php_pgsql_get_data_type(const zend_string *type_name)
  3809. {
  3810. /* This is stupid way to do. I'll fix it when I decide how to support
  3811. user defined types. (Yasuo) */
  3812. /* boolean */
  3813. if (zend_string_equals_literal(type_name, "bool")|| zend_string_equals_literal(type_name, "boolean"))
  3814. return PG_BOOL;
  3815. /* object id */
  3816. if (zend_string_equals_literal(type_name, "oid"))
  3817. return PG_OID;
  3818. /* integer */
  3819. if (zend_string_equals_literal(type_name, "int2") || zend_string_equals_literal(type_name, "smallint"))
  3820. return PG_INT2;
  3821. if (zend_string_equals_literal(type_name, "int4") || zend_string_equals_literal(type_name, "integer"))
  3822. return PG_INT4;
  3823. if (zend_string_equals_literal(type_name, "int8") || zend_string_equals_literal(type_name, "bigint"))
  3824. return PG_INT8;
  3825. /* real and other */
  3826. if (zend_string_equals_literal(type_name, "float4") || zend_string_equals_literal(type_name, "real"))
  3827. return PG_FLOAT4;
  3828. if (zend_string_equals_literal(type_name, "float8") || zend_string_equals_literal(type_name, "double precision"))
  3829. return PG_FLOAT8;
  3830. if (zend_string_equals_literal(type_name, "numeric"))
  3831. return PG_NUMERIC;
  3832. if (zend_string_equals_literal(type_name, "money"))
  3833. return PG_MONEY;
  3834. /* character */
  3835. if (zend_string_equals_literal(type_name, "text"))
  3836. return PG_TEXT;
  3837. if (zend_string_equals_literal(type_name, "bpchar") || zend_string_equals_literal(type_name, "character"))
  3838. return PG_CHAR;
  3839. if (zend_string_equals_literal(type_name, "varchar") || zend_string_equals_literal(type_name, "character varying"))
  3840. return PG_VARCHAR;
  3841. /* time and interval */
  3842. if (zend_string_equals_literal(type_name, "abstime"))
  3843. return PG_UNIX_TIME;
  3844. if (zend_string_equals_literal(type_name, "reltime"))
  3845. return PG_UNIX_TIME_INTERVAL;
  3846. if (zend_string_equals_literal(type_name, "tinterval"))
  3847. return PG_UNIX_TIME_INTERVAL;
  3848. if (zend_string_equals_literal(type_name, "date"))
  3849. return PG_DATE;
  3850. if (zend_string_equals_literal(type_name, "time"))
  3851. return PG_TIME;
  3852. if (zend_string_equals_literal(type_name, "time with time zone") || zend_string_equals_literal(type_name, "timetz"))
  3853. return PG_TIME_WITH_TIMEZONE;
  3854. if (zend_string_equals_literal(type_name, "timestamp without time zone") || zend_string_equals_literal(type_name, "timestamp"))
  3855. return PG_TIMESTAMP;
  3856. if (zend_string_equals_literal(type_name, "timestamp with time zone") || zend_string_equals_literal(type_name, "timestamptz"))
  3857. return PG_TIMESTAMP_WITH_TIMEZONE;
  3858. if (zend_string_equals_literal(type_name, "interval"))
  3859. return PG_INTERVAL;
  3860. /* binary */
  3861. if (zend_string_equals_literal(type_name, "bytea"))
  3862. return PG_BYTEA;
  3863. /* network */
  3864. if (zend_string_equals_literal(type_name, "cidr"))
  3865. return PG_CIDR;
  3866. if (zend_string_equals_literal(type_name, "inet"))
  3867. return PG_INET;
  3868. if (zend_string_equals_literal(type_name, "macaddr"))
  3869. return PG_MACADDR;
  3870. /* bit */
  3871. if (zend_string_equals_literal(type_name, "bit"))
  3872. return PG_BIT;
  3873. if (zend_string_equals_literal(type_name, "bit varying"))
  3874. return PG_VARBIT;
  3875. /* geometric */
  3876. if (zend_string_equals_literal(type_name, "line"))
  3877. return PG_LINE;
  3878. if (zend_string_equals_literal(type_name, "lseg"))
  3879. return PG_LSEG;
  3880. if (zend_string_equals_literal(type_name, "box"))
  3881. return PG_BOX;
  3882. if (zend_string_equals_literal(type_name, "path"))
  3883. return PG_PATH;
  3884. if (zend_string_equals_literal(type_name, "point"))
  3885. return PG_POINT;
  3886. if (zend_string_equals_literal(type_name, "polygon"))
  3887. return PG_POLYGON;
  3888. if (zend_string_equals_literal(type_name, "circle"))
  3889. return PG_CIRCLE;
  3890. return PG_UNKNOWN;
  3891. }
  3892. /* }}} */
  3893. /* {{{ php_pgsql_convert_match
  3894. * test field value with regular expression specified.
  3895. */
  3896. static int php_pgsql_convert_match(const zend_string *str, const char *regex , size_t regex_len, int icase)
  3897. {
  3898. pcre2_code *re;
  3899. PCRE2_SIZE err_offset;
  3900. int res, errnumber;
  3901. uint32_t options = PCRE2_NO_AUTO_CAPTURE;
  3902. size_t i;
  3903. pcre2_match_data *match_data;
  3904. /* Check invalid chars for POSIX regex */
  3905. for (i = 0; i < ZSTR_LEN(str); i++) {
  3906. if (ZSTR_VAL(str)[i] == '\n' ||
  3907. ZSTR_VAL(str)[i] == '\r' ||
  3908. ZSTR_VAL(str)[i] == '\0' ) {
  3909. return FAILURE;
  3910. }
  3911. }
  3912. if (icase) {
  3913. options |= PCRE2_CASELESS;
  3914. }
  3915. re = pcre2_compile((PCRE2_SPTR)regex, regex_len, options, &errnumber, &err_offset, php_pcre_cctx());
  3916. if (NULL == re) {
  3917. PCRE2_UCHAR err_msg[128];
  3918. pcre2_get_error_message(errnumber, err_msg, sizeof(err_msg));
  3919. php_error_docref(NULL, E_WARNING, "Cannot compile regex: '%s'", err_msg);
  3920. return FAILURE;
  3921. }
  3922. match_data = php_pcre_create_match_data(0, re);
  3923. if (NULL == match_data) {
  3924. pcre2_code_free(re);
  3925. php_error_docref(NULL, E_WARNING, "Cannot allocate match data");
  3926. return FAILURE;
  3927. }
  3928. res = pcre2_match(re, (PCRE2_SPTR)ZSTR_VAL(str), ZSTR_LEN(str), 0, 0, match_data, php_pcre_mctx());
  3929. php_pcre_free_match_data(match_data);
  3930. pcre2_code_free(re);
  3931. if (res == PCRE2_ERROR_NOMATCH) {
  3932. return FAILURE;
  3933. } else if (res < 0) {
  3934. php_error_docref(NULL, E_WARNING, "Cannot exec regex");
  3935. return FAILURE;
  3936. }
  3937. return SUCCESS;
  3938. }
  3939. /* }}} */
  3940. /* {{{ php_pgsql_add_quote
  3941. * add quotes around string.
  3942. */
  3943. static zend_string *php_pgsql_add_quotes(zend_string *src)
  3944. {
  3945. return zend_string_concat3("E'", strlen("E'"), ZSTR_VAL(src), ZSTR_LEN(src), "'", strlen("'"));
  3946. }
  3947. /* }}} */
  3948. /* Raise E_NOTICE to E_WARNING or Error? */
  3949. #define PGSQL_CONV_CHECK_IGNORE() \
  3950. if (!err && Z_TYPE(new_val) == IS_STRING && zend_string_equals_literal(Z_STR(new_val), "NULL")) { \
  3951. /* if new_value is string "NULL" and field has default value, remove element to use default value */ \
  3952. if (!(opt & PGSQL_CONV_IGNORE_DEFAULT) && Z_TYPE_P(has_default) == IS_TRUE) { \
  3953. zval_ptr_dtor(&new_val); \
  3954. skip_field = 1; \
  3955. } \
  3956. /* raise error if it's not null and cannot be ignored */ \
  3957. else if (!(opt & PGSQL_CONV_IGNORE_NOT_NULL) && Z_TYPE_P(not_null) == IS_TRUE) { \
  3958. php_error_docref(NULL, E_NOTICE, "Detected NULL for 'NOT NULL' field '%s'", ZSTR_VAL(field)); \
  3959. err = 1; \
  3960. } \
  3961. }
  3962. /* {{{ php_pgsql_convert
  3963. * check and convert array values (fieldname=>value pair) for sql
  3964. */
  3965. PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *table_name, const zval *values, zval *result, zend_ulong opt)
  3966. {
  3967. zend_string *field = NULL;
  3968. zval meta, *def, *type, *not_null, *has_default, *is_enum, *val, new_val;
  3969. int err = 0, skip_field;
  3970. php_pgsql_data_type data_type;
  3971. ZEND_ASSERT(pg_link != NULL);
  3972. ZEND_ASSERT(Z_TYPE_P(values) == IS_ARRAY);
  3973. ZEND_ASSERT(Z_TYPE_P(result) == IS_ARRAY);
  3974. ZEND_ASSERT(!(opt & ~PGSQL_CONV_OPTS));
  3975. ZEND_ASSERT(table_name);
  3976. /* Table name cannot be empty for php_pgsql_meta_data() */
  3977. ZEND_ASSERT(ZSTR_LEN(table_name) != 0);
  3978. array_init(&meta);
  3979. /* table_name is escaped by php_pgsql_meta_data */
  3980. if (php_pgsql_meta_data(pg_link, table_name, &meta, 0) == FAILURE) {
  3981. zval_ptr_dtor(&meta);
  3982. return FAILURE;
  3983. }
  3984. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(values), field, val) {
  3985. skip_field = 0;
  3986. ZVAL_DEREF(val);
  3987. ZVAL_NULL(&new_val);
  3988. /* TODO: Check when meta data can be broken and see if can use assertions instead */
  3989. if (!err && field == NULL) {
  3990. zend_value_error("Array of values must be an associative array with string keys");
  3991. err = 1;
  3992. }
  3993. if (!err && (def = zend_hash_find(Z_ARRVAL(meta), field)) == NULL) {
  3994. php_error_docref(NULL, E_NOTICE, "Invalid field name (%s) in values", ZSTR_VAL(field));
  3995. err = 1;
  3996. }
  3997. if (!err && (type = zend_hash_str_find(Z_ARRVAL_P(def), "type", sizeof("type") - 1)) == NULL) {
  3998. php_error_docref(NULL, E_NOTICE, "Detected broken meta data. Missing 'type'");
  3999. err = 1;
  4000. }
  4001. if (!err && (not_null = zend_hash_str_find(Z_ARRVAL_P(def), "not null", sizeof("not null") - 1)) == NULL) {
  4002. php_error_docref(NULL, E_NOTICE, "Detected broken meta data. Missing 'not null'");
  4003. err = 1;
  4004. }
  4005. if (!err && (has_default = zend_hash_str_find(Z_ARRVAL_P(def), "has default", sizeof("has default") - 1)) == NULL) {
  4006. php_error_docref(NULL, E_NOTICE, "Detected broken meta data. Missing 'has default'");
  4007. err = 1;
  4008. }
  4009. if (!err && (is_enum = zend_hash_str_find(Z_ARRVAL_P(def), "is enum", sizeof("is enum") - 1)) == NULL) {
  4010. php_error_docref(NULL, E_NOTICE, "Detected broken meta data. Missing 'is enum'");
  4011. err = 1;
  4012. }
  4013. if (!err && (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT || Z_TYPE_P(val) == IS_RESOURCE)) {
  4014. zend_type_error("Values must be of type string|int|float|bool|null, %s given", zend_zval_type_name(val));
  4015. err = 1;
  4016. }
  4017. if (err) {
  4018. break; /* break out for() */
  4019. }
  4020. convert_to_boolean(is_enum);
  4021. if (Z_TYPE_P(is_enum) == IS_TRUE) {
  4022. /* enums need to be treated like strings */
  4023. data_type = PG_TEXT;
  4024. } else {
  4025. data_type = php_pgsql_get_data_type(Z_STR_P(type));
  4026. }
  4027. /* TODO: Should E_NOTICE be converted to type error if PHP type cannot be converted to field type? */
  4028. switch(data_type)
  4029. {
  4030. case PG_BOOL:
  4031. switch (Z_TYPE_P(val)) {
  4032. case IS_STRING:
  4033. if (Z_STRLEN_P(val) == 0) {
  4034. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4035. }
  4036. else {
  4037. if (zend_string_equals_literal(Z_STR_P(val), "t") || zend_string_equals_literal(Z_STR_P(val), "T") ||
  4038. zend_string_equals_literal(Z_STR_P(val), "y") || zend_string_equals_literal(Z_STR_P(val), "Y") ||
  4039. zend_string_equals_literal(Z_STR_P(val), "true") || zend_string_equals_literal(Z_STR_P(val), "True") ||
  4040. zend_string_equals_literal(Z_STR_P(val), "yes") || zend_string_equals_literal(Z_STR_P(val), "Yes") ||
  4041. zend_string_equals_literal(Z_STR_P(val), "1")) {
  4042. ZVAL_STRINGL(&new_val, "'t'", sizeof("'t'")-1);
  4043. }
  4044. else if (zend_string_equals_literal(Z_STR_P(val), "f") || zend_string_equals_literal(Z_STR_P(val), "F") ||
  4045. zend_string_equals_literal(Z_STR_P(val), "n") || zend_string_equals_literal(Z_STR_P(val), "N") ||
  4046. zend_string_equals_literal(Z_STR_P(val), "false") || zend_string_equals_literal(Z_STR_P(val), "False") ||
  4047. zend_string_equals_literal(Z_STR_P(val), "no") || zend_string_equals_literal(Z_STR_P(val), "No") ||
  4048. zend_string_equals_literal(Z_STR_P(val), "0")) {
  4049. ZVAL_STRINGL(&new_val, "'f'", sizeof("'f'")-1);
  4050. }
  4051. else {
  4052. php_error_docref(NULL, E_NOTICE, "Detected invalid value (%s) for PostgreSQL %s field (%s)", Z_STRVAL_P(val), Z_STRVAL_P(type), ZSTR_VAL(field));
  4053. err = 1;
  4054. }
  4055. }
  4056. break;
  4057. case IS_LONG:
  4058. if (Z_LVAL_P(val)) {
  4059. ZVAL_STRINGL(&new_val, "'t'", sizeof("'t'")-1);
  4060. }
  4061. else {
  4062. ZVAL_STRINGL(&new_val, "'f'", sizeof("'f'")-1);
  4063. }
  4064. break;
  4065. case IS_TRUE:
  4066. ZVAL_STRINGL(&new_val, "'t'", sizeof("'t'")-1);
  4067. break;
  4068. case IS_FALSE:
  4069. ZVAL_STRINGL(&new_val, "'f'", sizeof("'f'")-1);
  4070. break;
  4071. case IS_NULL:
  4072. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4073. break;
  4074. default:
  4075. err = 1;
  4076. }
  4077. PGSQL_CONV_CHECK_IGNORE();
  4078. if (err) {
  4079. php_error_docref(NULL, E_NOTICE, "Expects string, null, long or boolelan value for PostgreSQL '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4080. }
  4081. break;
  4082. case PG_OID:
  4083. case PG_INT2:
  4084. case PG_INT4:
  4085. case PG_INT8:
  4086. switch (Z_TYPE_P(val)) {
  4087. case IS_STRING:
  4088. if (Z_STRLEN_P(val) == 0) {
  4089. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4090. }
  4091. else {
  4092. /* FIXME: better regex must be used */
  4093. #define REGEX0 "^([+-]{0,1}[0-9]+)$"
  4094. if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE) {
  4095. err = 1;
  4096. }
  4097. else {
  4098. ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
  4099. }
  4100. #undef REGEX0
  4101. }
  4102. break;
  4103. case IS_DOUBLE:
  4104. ZVAL_DOUBLE(&new_val, Z_DVAL_P(val));
  4105. convert_to_long(&new_val);
  4106. break;
  4107. case IS_LONG:
  4108. ZVAL_LONG(&new_val, Z_LVAL_P(val));
  4109. break;
  4110. case IS_NULL:
  4111. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4112. break;
  4113. default:
  4114. err = 1;
  4115. }
  4116. PGSQL_CONV_CHECK_IGNORE();
  4117. if (err) {
  4118. php_error_docref(NULL, E_NOTICE, "Expects NULL, string, long or double value for pgsql '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4119. }
  4120. break;
  4121. case PG_NUMERIC:
  4122. case PG_MONEY:
  4123. case PG_FLOAT4:
  4124. case PG_FLOAT8:
  4125. switch (Z_TYPE_P(val)) {
  4126. case IS_STRING:
  4127. if (Z_STRLEN_P(val) == 0) {
  4128. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4129. }
  4130. else {
  4131. #define REGEX0 "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$"
  4132. #define REGEX1 "^[+-]{0,1}(inf)(inity){0,1}$"
  4133. /* better regex? */
  4134. if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE) {
  4135. if (php_pgsql_convert_match(Z_STR_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
  4136. err = 1;
  4137. } else {
  4138. ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
  4139. }
  4140. }
  4141. else {
  4142. ZVAL_STRING(&new_val, Z_STRVAL_P(val));
  4143. }
  4144. #undef REGEX0
  4145. #undef REGEX1
  4146. }
  4147. break;
  4148. case IS_LONG:
  4149. ZVAL_LONG(&new_val, Z_LVAL_P(val));
  4150. break;
  4151. case IS_DOUBLE:
  4152. ZVAL_DOUBLE(&new_val, Z_DVAL_P(val));
  4153. break;
  4154. case IS_NULL:
  4155. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4156. break;
  4157. default:
  4158. err = 1;
  4159. }
  4160. PGSQL_CONV_CHECK_IGNORE();
  4161. if (err) {
  4162. php_error_docref(NULL, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4163. }
  4164. break;
  4165. /* Exotic types are handled as string also.
  4166. Please feel free to add more valitions. Invalid query fails
  4167. at execution anyway. */
  4168. case PG_TEXT:
  4169. case PG_CHAR:
  4170. case PG_VARCHAR:
  4171. /* bit */
  4172. case PG_BIT:
  4173. case PG_VARBIT:
  4174. /* geometric */
  4175. case PG_LINE:
  4176. case PG_LSEG:
  4177. case PG_POINT:
  4178. case PG_BOX:
  4179. case PG_PATH:
  4180. case PG_POLYGON:
  4181. case PG_CIRCLE:
  4182. /* unknown. JSON, Array etc */
  4183. case PG_UNKNOWN:
  4184. switch (Z_TYPE_P(val)) {
  4185. case IS_STRING:
  4186. if (Z_STRLEN_P(val) == 0) {
  4187. if (opt & PGSQL_CONV_FORCE_NULL) {
  4188. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4189. } else {
  4190. ZVAL_STRINGL(&new_val, "''", sizeof("''")-1);
  4191. }
  4192. }
  4193. else {
  4194. zend_string *str;
  4195. /* PostgreSQL ignores \0 */
  4196. str = zend_string_alloc(Z_STRLEN_P(val) * 2, 0);
  4197. /* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */
  4198. ZSTR_LEN(str) = PQescapeStringConn(pg_link, ZSTR_VAL(str), Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
  4199. ZVAL_STR(&new_val, php_pgsql_add_quotes(str));
  4200. zend_string_release_ex(str, false);
  4201. }
  4202. break;
  4203. case IS_LONG:
  4204. ZVAL_STR(&new_val, zend_long_to_str(Z_LVAL_P(val)));
  4205. break;
  4206. case IS_DOUBLE:
  4207. ZVAL_DOUBLE(&new_val, Z_DVAL_P(val));
  4208. convert_to_string(&new_val);
  4209. break;
  4210. case IS_NULL:
  4211. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4212. break;
  4213. default:
  4214. err = 1;
  4215. }
  4216. PGSQL_CONV_CHECK_IGNORE();
  4217. if (err) {
  4218. php_error_docref(NULL, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4219. }
  4220. break;
  4221. case PG_UNIX_TIME:
  4222. case PG_UNIX_TIME_INTERVAL:
  4223. /* these are the actallay a integer */
  4224. switch (Z_TYPE_P(val)) {
  4225. case IS_STRING:
  4226. if (Z_STRLEN_P(val) == 0) {
  4227. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4228. }
  4229. else {
  4230. /* better regex? */
  4231. if (php_pgsql_convert_match(Z_STR_P(val), "^[0-9]+$", sizeof("^[0-9]+$")-1, 0) == FAILURE) {
  4232. err = 1;
  4233. }
  4234. else {
  4235. ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
  4236. convert_to_long(&new_val);
  4237. }
  4238. }
  4239. break;
  4240. case IS_DOUBLE:
  4241. ZVAL_DOUBLE(&new_val, Z_DVAL_P(val));
  4242. convert_to_long(&new_val);
  4243. break;
  4244. case IS_LONG:
  4245. ZVAL_LONG(&new_val, Z_LVAL_P(val));
  4246. break;
  4247. case IS_NULL:
  4248. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4249. break;
  4250. default:
  4251. err = 1;
  4252. }
  4253. PGSQL_CONV_CHECK_IGNORE();
  4254. if (err) {
  4255. php_error_docref(NULL, E_NOTICE, "Expects NULL, string, long or double value for '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4256. }
  4257. break;
  4258. case PG_CIDR:
  4259. case PG_INET:
  4260. switch (Z_TYPE_P(val)) {
  4261. case IS_STRING:
  4262. if (Z_STRLEN_P(val) == 0) {
  4263. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4264. }
  4265. else {
  4266. #define REGEX0 "^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\\/[0-9]{1,3})?$"
  4267. #define REGEX1 "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/[0-9]{1,3})?$"
  4268. /* The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. See more in the doc.
  4269. The regex might still be not perfect, but catches the most of IP variants. We might decide to remove the regex
  4270. at all though and let the server side to handle it.*/
  4271. if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE
  4272. && php_pgsql_convert_match(Z_STR_P(val), REGEX1, sizeof(REGEX1)-1, 0) == FAILURE) {
  4273. err = 1;
  4274. }
  4275. else {
  4276. ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
  4277. }
  4278. #undef REGEX0
  4279. #undef REGEX1
  4280. }
  4281. break;
  4282. case IS_NULL:
  4283. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4284. break;
  4285. default:
  4286. err = 1;
  4287. }
  4288. PGSQL_CONV_CHECK_IGNORE();
  4289. if (err) {
  4290. php_error_docref(NULL, E_NOTICE, "Expects NULL or IPv4 or IPv6 address string for '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4291. }
  4292. break;
  4293. case PG_TIME_WITH_TIMEZONE:
  4294. case PG_TIMESTAMP:
  4295. case PG_TIMESTAMP_WITH_TIMEZONE:
  4296. switch(Z_TYPE_P(val)) {
  4297. case IS_STRING:
  4298. if (Z_STRLEN_P(val) == 0) {
  4299. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4300. } else if (zend_string_equals_literal_ci(Z_STR_P(val), "now()")) {
  4301. ZVAL_STRINGL(&new_val, "NOW()", sizeof("NOW()")-1);
  4302. } else {
  4303. #define REGEX0 "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})(([ \\t]+|T)(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$"
  4304. /* better regex? */
  4305. if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
  4306. err = 1;
  4307. } else {
  4308. ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
  4309. }
  4310. #undef REGEX0
  4311. }
  4312. break;
  4313. case IS_NULL:
  4314. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4315. break;
  4316. default:
  4317. err = 1;
  4318. }
  4319. PGSQL_CONV_CHECK_IGNORE();
  4320. if (err) {
  4321. php_error_docref(NULL, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4322. }
  4323. break;
  4324. case PG_DATE:
  4325. switch(Z_TYPE_P(val)) {
  4326. case IS_STRING:
  4327. if (Z_STRLEN_P(val) == 0) {
  4328. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4329. }
  4330. else {
  4331. #define REGEX0 "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$"
  4332. /* FIXME: better regex must be used */
  4333. if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
  4334. err = 1;
  4335. }
  4336. else {
  4337. ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
  4338. }
  4339. #undef REGEX0
  4340. }
  4341. break;
  4342. case IS_NULL:
  4343. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4344. break;
  4345. default:
  4346. err = 1;
  4347. }
  4348. PGSQL_CONV_CHECK_IGNORE();
  4349. if (err) {
  4350. php_error_docref(NULL, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4351. }
  4352. break;
  4353. case PG_TIME:
  4354. switch(Z_TYPE_P(val)) {
  4355. case IS_STRING:
  4356. if (Z_STRLEN_P(val) == 0) {
  4357. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4358. }
  4359. else {
  4360. #define REGEX0 "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}){0,1}$"
  4361. /* FIXME: better regex must be used */
  4362. if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
  4363. err = 1;
  4364. }
  4365. else {
  4366. ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
  4367. }
  4368. #undef REGEX0
  4369. }
  4370. break;
  4371. case IS_NULL:
  4372. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4373. break;
  4374. default:
  4375. err = 1;
  4376. }
  4377. PGSQL_CONV_CHECK_IGNORE();
  4378. if (err) {
  4379. php_error_docref(NULL, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4380. }
  4381. break;
  4382. case PG_INTERVAL:
  4383. switch(Z_TYPE_P(val)) {
  4384. case IS_STRING:
  4385. if (Z_STRLEN_P(val) == 0) {
  4386. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4387. }
  4388. else {
  4389. /* From the Postgres docs:
  4390. interval values can be written with the following syntax:
  4391. [@] quantity unit [quantity unit...] [direction]
  4392. Where: quantity is a number (possibly signed); unit is second, minute, hour,
  4393. day, week, month, year, decade, century, millennium, or abbreviations or
  4394. plurals of these units [note not *all* abbreviations] ; direction can be
  4395. ago or empty. The at sign (@) is optional noise.
  4396. ...
  4397. Quantities of days, hours, minutes, and seconds can be specified without explicit
  4398. unit markings. For example, '1 12:59:10' is read the same as '1 day 12 hours 59 min 10
  4399. sec'.
  4400. */
  4401. #define REGEX0 \
  4402. "^(@?[ \\t]+)?(" \
  4403. /* Textual time units and their abbreviations: */ \
  4404. "(([-+]?[ \\t]+)?" \
  4405. "[0-9]+(\\.[0-9]*)?[ \\t]*" \
  4406. "(millenniums|millennia|millennium|mil|mils|" \
  4407. "centuries|century|cent|c|" \
  4408. "decades|decade|dec|decs|" \
  4409. "years|year|y|" \
  4410. "months|month|mon|" \
  4411. "weeks|week|w|" \
  4412. "days|day|d|" \
  4413. "hours|hour|hr|hrs|h|" \
  4414. "minutes|minute|mins|min|m|" \
  4415. "seconds|second|secs|sec|s))+|" \
  4416. /* Textual time units plus (dd)* hh[:mm[:ss]] */ \
  4417. "((([-+]?[ \\t]+)?" \
  4418. "[0-9]+(\\.[0-9]*)?[ \\t]*" \
  4419. "(millenniums|millennia|millennium|mil|mils|" \
  4420. "centuries|century|cent|c|" \
  4421. "decades|decade|dec|decs|" \
  4422. "years|year|y|" \
  4423. "months|month|mon|" \
  4424. "weeks|week|w|" \
  4425. "days|day|d))+" \
  4426. "([-+]?[ \\t]+" \
  4427. "([0-9]+[ \\t]+)+" /* dd */ \
  4428. "(([0-9]{1,2}:){0,2}[0-9]{0,2})" /* hh:[mm:[ss]] */ \
  4429. ")?))" \
  4430. "([ \\t]+ago)?$"
  4431. if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
  4432. err = 1;
  4433. }
  4434. else {
  4435. ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
  4436. }
  4437. #undef REGEX0
  4438. }
  4439. break;
  4440. case IS_NULL:
  4441. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4442. break;
  4443. default:
  4444. err = 1;
  4445. }
  4446. PGSQL_CONV_CHECK_IGNORE();
  4447. if (err) {
  4448. php_error_docref(NULL, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4449. }
  4450. break;
  4451. case PG_BYTEA:
  4452. switch (Z_TYPE_P(val)) {
  4453. case IS_STRING:
  4454. if (Z_STRLEN_P(val) == 0) {
  4455. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4456. }
  4457. else {
  4458. unsigned char *tmp;
  4459. size_t to_len;
  4460. zend_string *tmp_zstr;
  4461. tmp = PQescapeByteaConn(pg_link, (unsigned char *)Z_STRVAL_P(val), Z_STRLEN_P(val), &to_len);
  4462. tmp_zstr = zend_string_init((char *)tmp, to_len - 1, false); /* PQescapeBytea's to_len includes additional '\0' */
  4463. PQfreemem(tmp);
  4464. ZVAL_STR(&new_val, php_pgsql_add_quotes(tmp_zstr));
  4465. zend_string_release_ex(tmp_zstr, false);
  4466. }
  4467. break;
  4468. case IS_LONG:
  4469. ZVAL_STR(&new_val, zend_long_to_str(Z_LVAL_P(val)));
  4470. break;
  4471. case IS_DOUBLE:
  4472. ZVAL_DOUBLE(&new_val, Z_DVAL_P(val));
  4473. convert_to_string(&new_val);
  4474. break;
  4475. case IS_NULL:
  4476. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4477. break;
  4478. default:
  4479. err = 1;
  4480. }
  4481. PGSQL_CONV_CHECK_IGNORE();
  4482. if (err) {
  4483. php_error_docref(NULL, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4484. }
  4485. break;
  4486. case PG_MACADDR:
  4487. switch(Z_TYPE_P(val)) {
  4488. case IS_STRING:
  4489. if (Z_STRLEN_P(val) == 0) {
  4490. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4491. }
  4492. else {
  4493. #define REGEX0 "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$"
  4494. if (php_pgsql_convert_match(Z_STR_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
  4495. err = 1;
  4496. }
  4497. else {
  4498. ZVAL_STR(&new_val, php_pgsql_add_quotes(Z_STR_P(val)));
  4499. }
  4500. #undef REGEX0
  4501. }
  4502. break;
  4503. case IS_NULL:
  4504. ZVAL_STR(&new_val, ZSTR_KNOWN(ZEND_STR_NULL));
  4505. break;
  4506. default:
  4507. err = 1;
  4508. }
  4509. PGSQL_CONV_CHECK_IGNORE();
  4510. if (err) {
  4511. php_error_docref(NULL, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), ZSTR_VAL(field));
  4512. }
  4513. break;
  4514. default:
  4515. /* should not happen */
  4516. php_error_docref(NULL, E_NOTICE, "Unknown or system data type '%s' for '%s'. Report error", Z_STRVAL_P(type), ZSTR_VAL(field));
  4517. err = 1;
  4518. break;
  4519. } /* switch */
  4520. if (err) {
  4521. zval_ptr_dtor(&new_val);
  4522. break; /* break out for() */
  4523. }
  4524. /* If field is NULL and HAS DEFAULT, should be skipped */
  4525. if (!skip_field) {
  4526. if (_php_pgsql_identifier_is_escaped(ZSTR_VAL(field), ZSTR_LEN(field))) {
  4527. zend_hash_update(Z_ARRVAL_P(result), field, &new_val);
  4528. } else {
  4529. char *escaped = PQescapeIdentifier(pg_link, ZSTR_VAL(field), ZSTR_LEN(field));
  4530. add_assoc_zval(result, escaped, &new_val);
  4531. PQfreemem(escaped);
  4532. }
  4533. }
  4534. } ZEND_HASH_FOREACH_END(); /* for */
  4535. zval_ptr_dtor(&meta);
  4536. if (err) {
  4537. /* shouldn't destroy & free zval here */
  4538. return FAILURE;
  4539. }
  4540. return SUCCESS;
  4541. }
  4542. /* }}} */
  4543. /* {{{ Check and convert values for PostgreSQL SQL statement */
  4544. PHP_FUNCTION(pg_convert)
  4545. {
  4546. zval *pgsql_link, *values;
  4547. pgsql_link_handle *link;
  4548. zend_string *table_name;
  4549. zend_ulong option = 0;
  4550. PGconn *pg_link;
  4551. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPa|l", &pgsql_link, pgsql_link_ce, &table_name, &values, &option) == FAILURE) {
  4552. RETURN_THROWS();
  4553. }
  4554. if (ZSTR_LEN(table_name) == 0) {
  4555. zend_argument_value_error(2, "cannot be empty");
  4556. RETURN_THROWS();
  4557. }
  4558. if (option & ~PGSQL_CONV_OPTS) {
  4559. zend_argument_value_error(4, "must be a valid bit mask of PGSQL_CONV_IGNORE_DEFAULT, "
  4560. "PGSQL_CONV_FORCE_NULL, and PGSQL_CONV_IGNORE_NOT_NULL");
  4561. RETURN_THROWS();
  4562. }
  4563. link = Z_PGSQL_LINK_P(pgsql_link);
  4564. CHECK_PGSQL_LINK(link);
  4565. pg_link = link->conn;
  4566. if (php_pgsql_flush_query(pg_link)) {
  4567. php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
  4568. }
  4569. array_init(return_value);
  4570. if (php_pgsql_convert(pg_link, table_name, values, return_value, option) == FAILURE) {
  4571. zend_array_destroy(Z_ARR_P(return_value));
  4572. RETURN_FALSE;
  4573. }
  4574. }
  4575. /* }}} */
  4576. static bool do_exec(smart_str *querystr, ExecStatusType expect, PGconn *pg_link, zend_ulong opt) /* {{{ */
  4577. {
  4578. if (opt & PGSQL_DML_ASYNC) {
  4579. if (PQsendQuery(pg_link, ZSTR_VAL(querystr->s))) {
  4580. return true;
  4581. }
  4582. } else {
  4583. PGresult *pg_result;
  4584. pg_result = PQexec(pg_link, ZSTR_VAL(querystr->s));
  4585. if (PQresultStatus(pg_result) == expect) {
  4586. PQclear(pg_result);
  4587. return true;
  4588. } else {
  4589. php_error_docref(NULL, E_WARNING, "%s", PQresultErrorMessage(pg_result));
  4590. PQclear(pg_result);
  4591. }
  4592. }
  4593. return false;
  4594. }
  4595. /* }}} */
  4596. static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const zend_string *table) /* {{{ */
  4597. {
  4598. /* schema.table should be "schema"."table" */
  4599. const char *dot = memchr(ZSTR_VAL(table), '.', ZSTR_LEN(table));
  4600. size_t len = dot ? dot - ZSTR_VAL(table) : ZSTR_LEN(table);
  4601. if (_php_pgsql_identifier_is_escaped(ZSTR_VAL(table), len)) {
  4602. smart_str_appendl(querystr, ZSTR_VAL(table), len);
  4603. } else {
  4604. char *escaped = PQescapeIdentifier(pg_link, ZSTR_VAL(table), len);
  4605. smart_str_appends(querystr, escaped);
  4606. PQfreemem(escaped);
  4607. }
  4608. if (dot) {
  4609. const char *after_dot = dot + 1;
  4610. len = ZSTR_LEN(table) - len - 1;
  4611. /* "schema"."table" format */
  4612. if (_php_pgsql_identifier_is_escaped(after_dot, len)) {
  4613. smart_str_appendc(querystr, '.');
  4614. smart_str_appendl(querystr, after_dot, len);
  4615. } else {
  4616. char *escaped = PQescapeIdentifier(pg_link, after_dot, len);
  4617. smart_str_appendc(querystr, '.');
  4618. smart_str_appends(querystr, escaped);
  4619. PQfreemem(escaped);
  4620. }
  4621. }
  4622. }
  4623. /* }}} */
  4624. /* {{{ php_pgsql_insert */
  4625. PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const zend_string *table, zval *var_array, zend_ulong opt, zend_string **sql)
  4626. {
  4627. zval *val, converted;
  4628. char buf[256];
  4629. char *tmp;
  4630. smart_str querystr = {0};
  4631. zend_result ret = FAILURE;
  4632. zend_string *fld;
  4633. ZEND_ASSERT(pg_link != NULL);
  4634. ZEND_ASSERT(table != NULL);
  4635. ZEND_ASSERT(Z_TYPE_P(var_array) == IS_ARRAY);
  4636. ZVAL_UNDEF(&converted);
  4637. if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0) {
  4638. smart_str_appends(&querystr, "INSERT INTO ");
  4639. build_tablename(&querystr, pg_link, table);
  4640. smart_str_appends(&querystr, " DEFAULT VALUES");
  4641. goto no_values;
  4642. }
  4643. /* convert input array if needed */
  4644. if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
  4645. array_init(&converted);
  4646. if (php_pgsql_convert(pg_link, table, var_array, &converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
  4647. goto cleanup;
  4648. }
  4649. var_array = &converted;
  4650. }
  4651. smart_str_appends(&querystr, "INSERT INTO ");
  4652. build_tablename(&querystr, pg_link, table);
  4653. smart_str_appends(&querystr, " (");
  4654. ZEND_HASH_FOREACH_STR_KEY(Z_ARRVAL_P(var_array), fld) {
  4655. if (fld == NULL) {
  4656. zend_value_error("Array of values must be an associative array with string keys");
  4657. goto cleanup;
  4658. }
  4659. if (opt & PGSQL_DML_ESCAPE) {
  4660. tmp = PQescapeIdentifier(pg_link, ZSTR_VAL(fld), ZSTR_LEN(fld) + 1);
  4661. smart_str_appends(&querystr, tmp);
  4662. PQfreemem(tmp);
  4663. } else {
  4664. smart_str_append(&querystr, fld);
  4665. }
  4666. smart_str_appendc(&querystr, ',');
  4667. } ZEND_HASH_FOREACH_END();
  4668. ZSTR_LEN(querystr.s)--;
  4669. smart_str_appends(&querystr, ") VALUES (");
  4670. /* make values string */
  4671. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(var_array), val) {
  4672. /* we can avoid the key_type check here, because we tested it in the other loop */
  4673. switch (Z_TYPE_P(val)) {
  4674. case IS_STRING:
  4675. if (opt & PGSQL_DML_ESCAPE) {
  4676. size_t new_len;
  4677. char *tmp;
  4678. tmp = (char *)safe_emalloc(Z_STRLEN_P(val), 2, 1);
  4679. new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
  4680. smart_str_appendc(&querystr, '\'');
  4681. smart_str_appendl(&querystr, tmp, new_len);
  4682. smart_str_appendc(&querystr, '\'');
  4683. efree(tmp);
  4684. } else {
  4685. smart_str_append(&querystr, Z_STR_P(val));
  4686. }
  4687. break;
  4688. case IS_LONG:
  4689. smart_str_append_long(&querystr, Z_LVAL_P(val));
  4690. break;
  4691. case IS_DOUBLE:
  4692. smart_str_appendl(&querystr, buf, snprintf(buf, sizeof(buf), "%F", Z_DVAL_P(val)));
  4693. break;
  4694. case IS_NULL:
  4695. smart_str_appendl(&querystr, "NULL", sizeof("NULL")-1);
  4696. break;
  4697. default:
  4698. zend_type_error("Value must be of type string|int|float|null, %s given", zend_zval_type_name(val));
  4699. goto cleanup;
  4700. }
  4701. smart_str_appendc(&querystr, ',');
  4702. } ZEND_HASH_FOREACH_END();
  4703. /* Remove the trailing "," */
  4704. ZSTR_LEN(querystr.s)--;
  4705. smart_str_appends(&querystr, ");");
  4706. no_values:
  4707. smart_str_0(&querystr);
  4708. if ((opt & (PGSQL_DML_EXEC|PGSQL_DML_ASYNC)) &&
  4709. do_exec(&querystr, PGRES_COMMAND_OK, pg_link, (opt & PGSQL_CONV_OPTS))) {
  4710. ret = SUCCESS;
  4711. }
  4712. else if (opt & PGSQL_DML_STRING) {
  4713. ret = SUCCESS;
  4714. }
  4715. cleanup:
  4716. zval_ptr_dtor(&converted);
  4717. if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
  4718. *sql = querystr.s;
  4719. }
  4720. else {
  4721. smart_str_free(&querystr);
  4722. }
  4723. return ret;
  4724. }
  4725. /* }}} */
  4726. /* {{{ Insert values (filed=>value) to table */
  4727. PHP_FUNCTION(pg_insert)
  4728. {
  4729. zval *pgsql_link, *values;
  4730. pgsql_link_handle *link;
  4731. zend_string *table;
  4732. zend_ulong option = PGSQL_DML_EXEC, return_sql;
  4733. PGconn *pg_link;
  4734. PGresult *pg_result;
  4735. ExecStatusType status;
  4736. zend_string *sql = NULL;
  4737. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPa|l",
  4738. &pgsql_link, pgsql_link_ce, &table, &values, &option) == FAILURE
  4739. ) {
  4740. RETURN_THROWS();
  4741. }
  4742. if (ZSTR_LEN(table) == 0) {
  4743. zend_argument_value_error(2, "cannot be empty");
  4744. RETURN_THROWS();
  4745. }
  4746. if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
  4747. zend_argument_value_error(4, "must be a valid bit mask of PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, "
  4748. "PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC, and PGSQL_DML_STRING");
  4749. RETURN_THROWS();
  4750. }
  4751. link = Z_PGSQL_LINK_P(pgsql_link);
  4752. CHECK_PGSQL_LINK(link);
  4753. pg_link = link->conn;
  4754. if (php_pgsql_flush_query(pg_link)) {
  4755. php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
  4756. }
  4757. return_sql = option & PGSQL_DML_STRING;
  4758. if (option & PGSQL_DML_EXEC) {
  4759. /* return object when executed */
  4760. option = option & ~PGSQL_DML_EXEC;
  4761. if (php_pgsql_insert(pg_link, table, values, option|PGSQL_DML_STRING, &sql) == FAILURE) {
  4762. RETURN_FALSE;
  4763. }
  4764. pg_result = PQexec(pg_link, ZSTR_VAL(sql));
  4765. if ((PGG(auto_reset_persistent) & 2) && PQstatus(pg_link) != CONNECTION_OK) {
  4766. PQclear(pg_result);
  4767. PQreset(pg_link);
  4768. pg_result = PQexec(pg_link, ZSTR_VAL(sql));
  4769. }
  4770. efree(sql);
  4771. if (pg_result) {
  4772. status = PQresultStatus(pg_result);
  4773. } else {
  4774. status = (ExecStatusType) PQstatus(pg_link);
  4775. }
  4776. switch (status) {
  4777. case PGRES_EMPTY_QUERY:
  4778. case PGRES_BAD_RESPONSE:
  4779. case PGRES_NONFATAL_ERROR:
  4780. case PGRES_FATAL_ERROR:
  4781. PHP_PQ_ERROR("Query failed: %s", pg_link);
  4782. PQclear(pg_result);
  4783. RETURN_FALSE;
  4784. break;
  4785. case PGRES_COMMAND_OK: /* successful command that did not return rows */
  4786. default:
  4787. if (pg_result) {
  4788. object_init_ex(return_value, pgsql_result_ce);
  4789. pgsql_result_handle *pg_res = Z_PGSQL_RESULT_P(return_value);
  4790. pg_res->conn = pg_link;
  4791. pg_res->result = pg_result;
  4792. pg_res->row = 0;
  4793. return;
  4794. } else {
  4795. PQclear(pg_result);
  4796. RETURN_FALSE;
  4797. }
  4798. break;
  4799. }
  4800. } else if (php_pgsql_insert(pg_link, table, values, option, &sql) == FAILURE) {
  4801. RETURN_FALSE;
  4802. }
  4803. if (return_sql) {
  4804. RETURN_STR(sql);
  4805. return;
  4806. }
  4807. RETURN_TRUE;
  4808. }
  4809. /* }}} */
  4810. static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr, HashTable *ht, int where_cond, const char *pad, int pad_len, zend_ulong opt) /* {{{ */
  4811. {
  4812. zend_string *fld;
  4813. zval *val;
  4814. ZEND_HASH_FOREACH_STR_KEY_VAL(ht, fld, val) {
  4815. if (fld == NULL) {
  4816. zend_value_error("Array of values must be an associative array with string keys");
  4817. return -1;
  4818. }
  4819. if (opt & PGSQL_DML_ESCAPE) {
  4820. char *tmp = PQescapeIdentifier(pg_link, ZSTR_VAL(fld), ZSTR_LEN(fld) + 1);
  4821. smart_str_appends(querystr, tmp);
  4822. PQfreemem(tmp);
  4823. } else {
  4824. smart_str_append(querystr, fld);
  4825. }
  4826. if (where_cond && (Z_TYPE_P(val) == IS_TRUE || Z_TYPE_P(val) == IS_FALSE ||
  4827. (Z_TYPE_P(val) == IS_STRING && zend_string_equals_literal(Z_STR_P(val), "NULL")))) {
  4828. smart_str_appends(querystr, " IS ");
  4829. } else {
  4830. smart_str_appendc(querystr, '=');
  4831. }
  4832. switch (Z_TYPE_P(val)) {
  4833. case IS_STRING:
  4834. if (opt & PGSQL_DML_ESCAPE) {
  4835. char *tmp = (char *)safe_emalloc(Z_STRLEN_P(val), 2, 1);
  4836. size_t new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
  4837. smart_str_appendc(querystr, '\'');
  4838. smart_str_appendl(querystr, tmp, new_len);
  4839. smart_str_appendc(querystr, '\'');
  4840. efree(tmp);
  4841. } else {
  4842. smart_str_append(querystr, Z_STR_P(val));
  4843. }
  4844. break;
  4845. case IS_LONG:
  4846. smart_str_append_long(querystr, Z_LVAL_P(val));
  4847. break;
  4848. case IS_DOUBLE: {
  4849. char buf[256];
  4850. smart_str_appendl(querystr, buf, MIN(snprintf(buf, sizeof(buf), "%F", Z_DVAL_P(val)), sizeof(buf) - 1));
  4851. }
  4852. break;
  4853. case IS_NULL:
  4854. smart_str_appendl(querystr, "NULL", sizeof("NULL")-1);
  4855. break;
  4856. default:
  4857. zend_type_error("Value must be of type string|int|float|null, %s given", zend_zval_type_name(val));
  4858. return -1;
  4859. }
  4860. smart_str_appendl(querystr, pad, pad_len);
  4861. } ZEND_HASH_FOREACH_END();
  4862. if (querystr->s) {
  4863. ZSTR_LEN(querystr->s) -= pad_len;
  4864. }
  4865. return 0;
  4866. }
  4867. /* }}} */
  4868. /* {{{ php_pgsql_update */
  4869. PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, const zend_string *table, zval *var_array, zval *ids_array, zend_ulong opt, zend_string **sql)
  4870. {
  4871. zval var_converted, ids_converted;
  4872. smart_str querystr = {0};
  4873. zend_result ret = FAILURE;
  4874. ZEND_ASSERT(pg_link != NULL);
  4875. ZEND_ASSERT(table != NULL);
  4876. ZEND_ASSERT(Z_TYPE_P(var_array) == IS_ARRAY);
  4877. ZEND_ASSERT(Z_TYPE_P(ids_array) == IS_ARRAY);
  4878. ZEND_ASSERT(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)));
  4879. if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0
  4880. || zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
  4881. return FAILURE;
  4882. }
  4883. ZVAL_UNDEF(&var_converted);
  4884. ZVAL_UNDEF(&ids_converted);
  4885. if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
  4886. array_init(&var_converted);
  4887. if (php_pgsql_convert(pg_link, table, var_array, &var_converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
  4888. goto cleanup;
  4889. }
  4890. var_array = &var_converted;
  4891. array_init(&ids_converted);
  4892. if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
  4893. goto cleanup;
  4894. }
  4895. ids_array = &ids_converted;
  4896. }
  4897. smart_str_appends(&querystr, "UPDATE ");
  4898. build_tablename(&querystr, pg_link, table);
  4899. smart_str_appends(&querystr, " SET ");
  4900. if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(var_array), 0, ",", 1, opt))
  4901. goto cleanup;
  4902. smart_str_appends(&querystr, " WHERE ");
  4903. if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt))
  4904. goto cleanup;
  4905. smart_str_appendc(&querystr, ';');
  4906. smart_str_0(&querystr);
  4907. if ((opt & PGSQL_DML_EXEC) && do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt)) {
  4908. ret = SUCCESS;
  4909. } else if (opt & PGSQL_DML_STRING) {
  4910. ret = SUCCESS;
  4911. }
  4912. cleanup:
  4913. zval_ptr_dtor(&var_converted);
  4914. zval_ptr_dtor(&ids_converted);
  4915. if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
  4916. *sql = querystr.s;
  4917. }
  4918. else {
  4919. smart_str_free(&querystr);
  4920. }
  4921. return ret;
  4922. }
  4923. /* }}} */
  4924. /* {{{ Update table using values (field=>value) and ids (id=>value) */
  4925. PHP_FUNCTION(pg_update)
  4926. {
  4927. zval *pgsql_link, *values, *ids;
  4928. pgsql_link_handle *link;
  4929. zend_string *table;
  4930. zend_ulong option = PGSQL_DML_EXEC;
  4931. PGconn *pg_link;
  4932. zend_string *sql = NULL;
  4933. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPaa|l",
  4934. &pgsql_link, pgsql_link_ce, &table, &values, &ids, &option) == FAILURE
  4935. ) {
  4936. RETURN_THROWS();
  4937. }
  4938. if (ZSTR_LEN(table) == 0) {
  4939. zend_argument_value_error(2, "cannot be empty");
  4940. RETURN_THROWS();
  4941. }
  4942. if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
  4943. zend_argument_value_error(5, "must be a valid bit mask of PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, "
  4944. "PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC, and PGSQL_DML_STRING");
  4945. RETURN_THROWS();
  4946. }
  4947. link = Z_PGSQL_LINK_P(pgsql_link);
  4948. CHECK_PGSQL_LINK(link);
  4949. pg_link = link->conn;
  4950. if (php_pgsql_flush_query(pg_link)) {
  4951. php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
  4952. }
  4953. if (php_pgsql_update(pg_link, table, values, ids, option, &sql) == FAILURE) {
  4954. RETURN_FALSE;
  4955. }
  4956. if (option & PGSQL_DML_STRING) {
  4957. RETURN_STR(sql);
  4958. }
  4959. RETURN_TRUE;
  4960. }
  4961. /* }}} */
  4962. /* {{{ php_pgsql_delete */
  4963. PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const zend_string *table, zval *ids_array, zend_ulong opt, zend_string **sql)
  4964. {
  4965. zval ids_converted;
  4966. smart_str querystr = {0};
  4967. zend_result ret = FAILURE;
  4968. ZEND_ASSERT(pg_link != NULL);
  4969. ZEND_ASSERT(table != NULL);
  4970. ZEND_ASSERT(Z_TYPE_P(ids_array) == IS_ARRAY);
  4971. ZEND_ASSERT(!(opt & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)));
  4972. if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
  4973. return FAILURE;
  4974. }
  4975. ZVAL_UNDEF(&ids_converted);
  4976. if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
  4977. array_init(&ids_converted);
  4978. if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
  4979. goto cleanup;
  4980. }
  4981. ids_array = &ids_converted;
  4982. }
  4983. smart_str_appends(&querystr, "DELETE FROM ");
  4984. build_tablename(&querystr, pg_link, table);
  4985. smart_str_appends(&querystr, " WHERE ");
  4986. if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt))
  4987. goto cleanup;
  4988. smart_str_appendc(&querystr, ';');
  4989. smart_str_0(&querystr);
  4990. if ((opt & PGSQL_DML_EXEC) && do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt)) {
  4991. ret = SUCCESS;
  4992. } else if (opt & PGSQL_DML_STRING) {
  4993. ret = SUCCESS;
  4994. }
  4995. cleanup:
  4996. zval_ptr_dtor(&ids_converted);
  4997. if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
  4998. *sql = querystr.s;
  4999. }
  5000. else {
  5001. smart_str_free(&querystr);
  5002. }
  5003. return ret;
  5004. }
  5005. /* }}} */
  5006. /* {{{ Delete records has ids (id=>value) */
  5007. PHP_FUNCTION(pg_delete)
  5008. {
  5009. zval *pgsql_link, *ids;
  5010. pgsql_link_handle *link;
  5011. zend_string *table;
  5012. zend_ulong option = PGSQL_DML_EXEC;
  5013. PGconn *pg_link;
  5014. zend_string *sql;
  5015. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPa|l",
  5016. &pgsql_link, pgsql_link_ce, &table, &ids, &option
  5017. ) == FAILURE) {
  5018. RETURN_THROWS();
  5019. }
  5020. if (ZSTR_LEN(table) == 0) {
  5021. zend_argument_value_error(2, "cannot be empty");
  5022. RETURN_THROWS();
  5023. }
  5024. if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
  5025. zend_argument_value_error(4, "must be a valid bit mask of PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, "
  5026. "PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC, and PGSQL_DML_STRING");
  5027. RETURN_THROWS();
  5028. }
  5029. link = Z_PGSQL_LINK_P(pgsql_link);
  5030. CHECK_PGSQL_LINK(link);
  5031. pg_link = link->conn;
  5032. if (php_pgsql_flush_query(pg_link)) {
  5033. php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
  5034. }
  5035. if (php_pgsql_delete(pg_link, table, ids, option, &sql) == FAILURE) {
  5036. RETURN_FALSE;
  5037. }
  5038. if (option & PGSQL_DML_STRING) {
  5039. RETURN_STR(sql);
  5040. }
  5041. RETURN_TRUE;
  5042. }
  5043. /* }}} */
  5044. /* {{{ php_pgsql_result2array */
  5045. PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_type)
  5046. {
  5047. zval row;
  5048. char *field_name;
  5049. size_t num_fields;
  5050. int pg_numrows, pg_row;
  5051. uint32_t i;
  5052. ZEND_ASSERT(Z_TYPE_P(ret_array) == IS_ARRAY);
  5053. pg_numrows = PQntuples(pg_result);
  5054. for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
  5055. array_init(&row);
  5056. for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++) {
  5057. field_name = PQfname(pg_result, i);
  5058. if (PQgetisnull(pg_result, pg_row, i)) {
  5059. if (result_type & PGSQL_ASSOC) {
  5060. add_assoc_null(&row, field_name);
  5061. }
  5062. if (result_type & PGSQL_NUM) {
  5063. add_next_index_null(&row);
  5064. }
  5065. } else {
  5066. char *element = PQgetvalue(pg_result, pg_row, i);
  5067. if (element) {
  5068. const size_t element_len = strlen(element);
  5069. if (result_type & PGSQL_ASSOC) {
  5070. add_assoc_stringl(&row, field_name, element, element_len);
  5071. }
  5072. if (result_type & PGSQL_NUM) {
  5073. add_next_index_stringl(&row, element, element_len);
  5074. }
  5075. }
  5076. }
  5077. }
  5078. add_index_zval(ret_array, pg_row, &row);
  5079. }
  5080. }
  5081. /* }}} */
  5082. /* {{{ php_pgsql_select */
  5083. PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *table, zval *ids_array, zval *ret_array, zend_ulong opt, long result_type, zend_string **sql)
  5084. {
  5085. zval ids_converted;
  5086. smart_str querystr = {0};
  5087. zend_result ret = FAILURE;
  5088. PGresult *pg_result;
  5089. ZEND_ASSERT(pg_link != NULL);
  5090. ZEND_ASSERT(table != NULL);
  5091. ZEND_ASSERT(Z_TYPE_P(ids_array) == IS_ARRAY);
  5092. ZEND_ASSERT(Z_TYPE_P(ret_array) == IS_ARRAY);
  5093. ZEND_ASSERT(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)));
  5094. if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
  5095. return FAILURE;
  5096. }
  5097. ZVAL_UNDEF(&ids_converted);
  5098. if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
  5099. array_init(&ids_converted);
  5100. if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
  5101. goto cleanup;
  5102. }
  5103. ids_array = &ids_converted;
  5104. }
  5105. smart_str_appends(&querystr, "SELECT * FROM ");
  5106. build_tablename(&querystr, pg_link, table);
  5107. smart_str_appends(&querystr, " WHERE ");
  5108. if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt))
  5109. goto cleanup;
  5110. smart_str_appendc(&querystr, ';');
  5111. smart_str_0(&querystr);
  5112. pg_result = PQexec(pg_link, ZSTR_VAL(querystr.s));
  5113. if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) {
  5114. php_pgsql_result2array(pg_result, ret_array, result_type);
  5115. ret = SUCCESS;
  5116. } else {
  5117. php_error_docref(NULL, E_NOTICE, "Failed to execute '%s'", ZSTR_VAL(querystr.s));
  5118. }
  5119. PQclear(pg_result);
  5120. cleanup:
  5121. zval_ptr_dtor(&ids_converted);
  5122. if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
  5123. *sql = querystr.s;
  5124. }
  5125. else {
  5126. smart_str_free(&querystr);
  5127. }
  5128. return ret;
  5129. }
  5130. /* }}} */
  5131. /* {{{ Select records that has ids (id=>value) */
  5132. PHP_FUNCTION(pg_select)
  5133. {
  5134. zval *pgsql_link, *ids;
  5135. pgsql_link_handle *link;
  5136. zend_string *table;
  5137. zend_ulong option = PGSQL_DML_EXEC;
  5138. zend_long result_type = PGSQL_ASSOC;
  5139. PGconn *pg_link;
  5140. zend_string *sql = NULL;
  5141. /* TODO Document result_type param on php.net (apparently it was added in PHP 7.1) */
  5142. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPa|ll",
  5143. &pgsql_link, pgsql_link_ce, &table, &ids, &option, &result_type
  5144. ) == FAILURE) {
  5145. RETURN_THROWS();
  5146. }
  5147. if (ZSTR_LEN(table) == 0) {
  5148. zend_argument_value_error(2, "cannot be empty");
  5149. RETURN_THROWS();
  5150. }
  5151. if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
  5152. zend_argument_value_error(4, "must be a valid bit mask of PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, "
  5153. "PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC, and PGSQL_DML_STRING");
  5154. RETURN_THROWS();
  5155. }
  5156. if (!(result_type & PGSQL_BOTH)) {
  5157. zend_argument_value_error(5, "must be one of PGSQL_ASSOC, PGSQL_NUM, or PGSQL_BOTH");
  5158. RETURN_THROWS();
  5159. }
  5160. link = Z_PGSQL_LINK_P(pgsql_link);
  5161. CHECK_PGSQL_LINK(link);
  5162. pg_link = link->conn;
  5163. if (php_pgsql_flush_query(pg_link)) {
  5164. php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
  5165. }
  5166. array_init(return_value);
  5167. if (php_pgsql_select(pg_link, table, ids, return_value, option, result_type, &sql) == FAILURE) {
  5168. zval_ptr_dtor(return_value);
  5169. RETURN_FALSE;
  5170. }
  5171. if (option & PGSQL_DML_STRING) {
  5172. zval_ptr_dtor(return_value);
  5173. RETURN_STR(sql);
  5174. }
  5175. return;
  5176. }
  5177. /* }}} */
  5178. #endif