19 Star 33 Fork 26

openLooKeng/hetu-odbc-driver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ma_statement.c 167.44 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869
/************************************************************************************
Copyright (C) 2013,2019 MariaDB Corporation AB
Copyright (C) 2018-2020. Huawei Technologies Co., Ltd. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
#include <ma_odbc.h>
#define MADB_MIN_QUERY_LEN 5
#define CATALOGS_MAXLEN 1024*100
struct st_ma_stmt_methods MADB_StmtMethods; /* declared at the end of file */
extern SQLRETURN MADB_StoreQueryResult(MADB_Stmt *Stmt);
void MADB_StmtReset(MADB_Stmt *Stmt);
/* {{{ MADB_StmtInit */
SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt)
{
MADB_Stmt *Stmt= NULL;
if (!(Stmt = (MADB_Stmt *)MADB_CALLOC(sizeof(MADB_Stmt))))
goto error;
MADB_PutErrorPrefix(Connection, &Stmt->Error);
*pHStmt= Stmt;
Stmt->Connection= Connection;
LOCK_MARIADB(Connection);
if (!(Stmt->stmt= MADB_NewStmtHandle(Stmt)) ||
!(Stmt->IApd= MADB_DescInit(Connection, MADB_DESC_APD, FALSE)) ||
!(Stmt->IArd= MADB_DescInit(Connection, MADB_DESC_ARD, FALSE)) ||
!(Stmt->IIpd= MADB_DescInit(Connection, MADB_DESC_IPD, FALSE)) ||
!(Stmt->IIrd= MADB_DescInit(Connection, MADB_DESC_IRD, FALSE)))
{
UNLOCK_MARIADB(Stmt->Connection);
goto error;
}
MDBUG_C_PRINT(Stmt->Connection, "-->inited %0x", Stmt->stmt);
UNLOCK_MARIADB(Connection);
Stmt->PutParam= -1;
Stmt->Methods= &MADB_StmtMethods;
/* default behaviour is SQL_CURSOR_STATIC */
Stmt->Options.CursorType= SQL_CURSOR_STATIC;
Stmt->Options.UseBookmarks= SQL_UB_OFF;
Stmt->Options.MetadataId= Connection->MetadataId;
Stmt->Apd= Stmt->IApd;
Stmt->Ard= Stmt->IArd;
Stmt->Ipd= Stmt->IIpd;
Stmt->Ird= Stmt->IIrd;
Stmt->ListItem.data= (void *)Stmt;
EnterCriticalSection(&Stmt->Connection->ListsCs);
Stmt->Connection->Stmts= MADB_ListAdd(Stmt->Connection->Stmts, &Stmt->ListItem);
LeaveCriticalSection(&Stmt->Connection->ListsCs);
Stmt->Ard->Header.ArraySize= 1;
return SQL_SUCCESS;
error:
if (Stmt && Stmt->stmt)
{
MADB_STMT_CLOSE_STMT(Stmt);
}
MADB_DescFree(Stmt->IApd, TRUE);
MADB_DescFree(Stmt->IArd, TRUE);
MADB_DescFree(Stmt->IIpd, TRUE);
MADB_DescFree(Stmt->IIrd, TRUE);
MADB_FREE(Stmt);
return SQL_ERROR;
}
/* }}} */
void ClearOldQueryData(MADB_Stmt * Stmt) {
/* clear data */
if (Stmt->stmt->result.data) {
ma_free_root(&Stmt->stmt->result.alloc, 0);
Stmt->stmt->result_cursor = Stmt->stmt->result.data = NULL;
}
Stmt->stmt->result.rows = 0;
return;
}
/* {{{ MADB_ExecuteQuery */
SQLRETURN MADB_ExecuteQuery(MADB_Stmt * Stmt, char *StatementText, SQLINTEGER TextLength)
{
SQLRETURN ret= SQL_ERROR;
LOCK_MARIADB(Stmt->Connection);
if (StatementText)
{
MDBUG_C_PRINT(Stmt->Connection, "mysql_real_query(%0x,%s,%lu)", Stmt->Connection->mariadb, StatementText, TextLength);
ClearOldQueryData(Stmt);
if(!mysql_real_query(Stmt->Connection->mariadb, StatementText, TextLength))
{
ret= SQL_SUCCESS;
MADB_CLEAR_ERROR(&Stmt->Error);
if (Stmt->Connection->mariadb->field_count > 0) {
ret = MADB_StoreQueryResult(Stmt);
}
Stmt->AffectedRows= mysql_affected_rows(Stmt->Connection->mariadb);
}
else
{
MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_DBC, Stmt->Connection->mariadb);
}
}
else
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, mysql_error(Stmt->Connection->mariadb),
mysql_errno(Stmt->Connection->mariadb));
UNLOCK_MARIADB(Stmt->Connection);
return ret;
}
/* }}} */
/* {{{ MADB_StmtBulkOperations */
SQLRETURN MADB_StmtBulkOperations(MADB_Stmt *Stmt, SQLSMALLINT Operation)
{
MADB_CLEAR_ERROR(&Stmt->Error);
switch(Operation)
{
case SQL_ADD:
return Stmt->Methods->SetPos(Stmt, 0, SQL_ADD, SQL_LOCK_NO_CHANGE, 0);
default:
return SQL_ERROR;
}
}
/* }}} */
/* {{{ RemoveStmtRefFromDesc
Helper function removing references to the stmt in the descriptor when explisitly allocated descriptor is substituted
by some other descriptor */
void RemoveStmtRefFromDesc(MADB_Desc *desc, MADB_Stmt *Stmt, BOOL all)
{
if (desc->AppType)
{
unsigned int i;
for (i=0; i < desc->Stmts.elements; ++i)
{
MADB_Stmt **refStmt= ((MADB_Stmt **)desc->Stmts.buffer) + i;
if (Stmt == *refStmt)
{
MADB_DeleteDynamicElement(&desc->Stmts, i);
if (!all)
{
return;
}
}
}
}
}
/* }}} */
/* {{{ ResetMetadata */
void ResetMetadata(MYSQL_RES** metadata, MYSQL_RES* new_metadata)
{
if (*metadata != NULL)
{
mysql_free_result(*metadata);
}
*metadata= new_metadata;
}
/* }}} */
/* {{{ MADB_StmtFree */
SQLRETURN MADB_StmtFree(MADB_Stmt *Stmt, SQLUSMALLINT Option)
{
if (!Stmt)
return SQL_INVALID_HANDLE;
switch (Option) {
case SQL_CLOSE:
if (Stmt->stmt)
{
if (Stmt->Ird)
MADB_DescFree(Stmt->Ird, TRUE);
if (Stmt->State > MADB_SS_PREPARED && !QUERY_IS_MULTISTMT(Stmt->Query))
{
MDBUG_C_PRINT(Stmt->Connection, "mysql_stmt_free_result(%0x)", Stmt->stmt);
mysql_stmt_free_result(Stmt->stmt);
LOCK_MARIADB(Stmt->Connection);
MDBUG_C_PRINT(Stmt->Connection, "-->resetting %0x", Stmt->stmt);
mysql_stmt_reset(Stmt->stmt);
UNLOCK_MARIADB(Stmt->Connection);
}
if (QUERY_IS_MULTISTMT(Stmt->Query) && Stmt->MultiStmts)
{
unsigned int i;
LOCK_MARIADB(Stmt->Connection);
for (i=0; i < STMT_COUNT(Stmt->Query); ++i)
{
if (Stmt->MultiStmts[i] != NULL)
{
MDBUG_C_PRINT(Stmt->Connection, "-->resetting %0x(%u)", Stmt->MultiStmts[i], i);
mysql_stmt_reset(Stmt->MultiStmts[i]);
}
}
UNLOCK_MARIADB(Stmt->Connection);
}
ResetMetadata(&Stmt->metadata, NULL);
MADB_FREE(Stmt->result);
MADB_FREE(Stmt->CharOffset);
MADB_FREE(Stmt->Lengths);
RESET_STMT_STATE(Stmt);
RESET_DAE_STATUS(Stmt);
}
break;
case SQL_UNBIND:
MADB_FREE(Stmt->result);
MADB_DescFree(Stmt->Ard, TRUE);
break;
case SQL_RESET_PARAMS:
MADB_FREE(Stmt->params);
MADB_DescFree(Stmt->Apd, TRUE);
RESET_DAE_STATUS(Stmt);
break;
case SQL_DROP:
MADB_FREE(Stmt->params);
MADB_FREE(Stmt->result);
MADB_FREE(Stmt->Cursor.Name);
MADB_FREE(Stmt->CatalogName);
MADB_FREE(Stmt->TableName);
ResetMetadata(&Stmt->metadata, NULL);
/* For explicit descriptors we only remove reference to the stmt*/
if (Stmt->Apd->AppType)
{
EnterCriticalSection(&Stmt->Connection->ListsCs);
RemoveStmtRefFromDesc(Stmt->Apd, Stmt, TRUE);
LeaveCriticalSection(&Stmt->Connection->ListsCs);
MADB_DescFree(Stmt->IApd, FALSE);
}
else
{
MADB_DescFree( Stmt->Apd, FALSE);
}
if (Stmt->Ard->AppType)
{
EnterCriticalSection(&Stmt->Connection->ListsCs);
RemoveStmtRefFromDesc(Stmt->Ard, Stmt, TRUE);
LeaveCriticalSection(&Stmt->Connection->ListsCs);
MADB_DescFree(Stmt->IArd, FALSE);
}
else
{
MADB_DescFree(Stmt->Ard, FALSE);
}
MADB_DescFree(Stmt->Ipd, FALSE);
MADB_DescFree(Stmt->Ird, FALSE);
MADB_FREE(Stmt->CharOffset);
MADB_FREE(Stmt->Lengths);
ResetMetadata(&Stmt->DefaultsResult, NULL);
if (Stmt->DaeStmt != NULL)
{
Stmt->DaeStmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
Stmt->DaeStmt= NULL;
}
EnterCriticalSection(&Stmt->Connection->cs);
/* TODO: if multistatement was prepared, but not executed, we would get here Stmt->stmt leaked. Unlikely that is very probable scenario,
thus leaving this for new version */
if (QUERY_IS_MULTISTMT(Stmt->Query) && Stmt->MultiStmts)
{
unsigned int i;
for (i= 0; i < STMT_COUNT(Stmt->Query); ++i)
{
/* This dirty hack allows to avoid crash in case stmt object was not allocated
TODO: The better place for this check would be where MultiStmts was not allocated
to avoid inconsistency(MultiStmtCount > 0 and MultiStmts is NULL */
if (Stmt->MultiStmts!= NULL && Stmt->MultiStmts[i] != NULL)
{
MDBUG_C_PRINT(Stmt->Connection, "-->closing %0x(%u)", Stmt->MultiStmts[i], i);
mysql_stmt_close(Stmt->MultiStmts[i]);
}
}
MADB_FREE(Stmt->MultiStmts);
Stmt->MultiStmtNr= 0;
}
else if (Stmt->stmt != NULL)
{
MDBUG_C_PRINT(Stmt->Connection, "-->closing %0x", Stmt->stmt);
MADB_STMT_CLOSE_STMT(Stmt);
}
/* Query has to be deleted after multistmt handles are closed, since the depends on info in the Query */
MADB_DeleteQuery(&Stmt->Query);
LeaveCriticalSection(&Stmt->Connection->cs);
EnterCriticalSection(&Stmt->Connection->ListsCs);
Stmt->Connection->Stmts= MADB_ListDelete(Stmt->Connection->Stmts, &Stmt->ListItem);
LeaveCriticalSection(&Stmt->Connection->ListsCs);
MADB_FREE(Stmt);
} /* End of switch (Option) */
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_CheckIfExecDirectPossible
Checking if we can deploy mariadb_stmt_execute_direct */
BOOL MADB_CheckIfExecDirectPossible(MADB_Stmt *Stmt)
{
return MADB_ServerSupports(Stmt->Connection, MADB_CAPABLE_EXEC_DIRECT)
&& !(Stmt->Apd->Header.ArraySize > 1) /* With array of parameters exec_direct will be not optimal */
&& MADB_FindNextDaeParam(Stmt->Apd, -1, 1) == MADB_NOPARAM;
}
/* }}} */
/* {{{ MADB_BulkInsertPossible
Checking if we can deploy mariadb_stmt_execute_direct */
BOOL MADB_BulkInsertPossible(MADB_Stmt *Stmt)
{
return MADB_ServerSupports(Stmt->Connection, MADB_CAPABLE_PARAM_ARRAYS)
&& (Stmt->Apd->Header.ArraySize > 1)
&& (Stmt->Apd->Header.BindType == SQL_PARAM_BIND_BY_COLUMN) /* First we support column-wise binding */
&& (Stmt->Query.QueryType == MADB_QUERY_INSERT || Stmt->Query.QueryType == MADB_QUERY_UPDATE)
&& MADB_FindNextDaeParam(Stmt->Apd, -1, 1) == MADB_NOPARAM; /* TODO: should be not very hard ot optimize to use bulk in this
case for chunks of the array, delimitered by param rows with DAE
In particular, MADB_FindNextDaeParam should consider Stmt->ArrayOffset */
}
/* }}} */
/* {{{ MADB_StmtExecDirect */
SQLRETURN MADB_StmtExecDirect(MADB_Stmt *Stmt, char *StatementText, SQLINTEGER TextLength)
{
SQLRETURN ret;
BOOL ExecDirect= TRUE;
BOOL NeedPrepare = FALSE;
NeedPrepare = Stmt->Connection->Dsn->BinaryExec;
if (!NeedPrepare) {
LOCK_MARIADB(Stmt->Connection);
MADB_StmtReset(Stmt);
/* After this point we can't have SQL_NTS*/
ADJUST_LENGTH(StatementText, TextLength);
/* There is no need to send anything to the server to find out there is syntax error here */
if (TextLength < MADB_MIN_QUERY_LEN) {
UNLOCK_MARIADB(Stmt->Connection);
return MADB_SetError(&Stmt->Error, MADB_ERR_42000, NULL, 0);
}
MADB_ResetParser(Stmt, StatementText, TextLength);
MADB_ParseQuery(&Stmt->Query);
NeedPrepare = Stmt->Query.HasParameters; //need to execute with prepare process in case of having input parameter
UNLOCK_MARIADB(Stmt->Connection);
}
if (NeedPrepare) {
ret= Stmt->Methods->Prepare(Stmt, StatementText, TextLength, ExecDirect);
} else {
Stmt->State = MADB_SS_EMULATED;
ret = SQL_SUCCESS;
}
/* In case statement is not supported, we use mysql_query instead */
if (!SQL_SUCCEEDED(ret))
{
/* This is not quite good - 1064 may simply mean that syntax is wrong. we are screwed then */
if ((Stmt->Error.NativeError == 1295/*ER_UNSUPPORTED_PS*/ ||
Stmt->Error.NativeError == 1064/*ER_PARSE_ERROR*/))
{
Stmt->State= MADB_SS_EMULATED;
}
else
{
return ret;
}
}
/* For multistmt we don't use mariadb_stmt_execute_direct so far */
if (QUERY_IS_MULTISTMT(Stmt->Query))
{
ExecDirect= FALSE;
}
return Stmt->Methods->Execute(Stmt, ExecDirect);
}
/* }}} */
/* {{{ MADB_FindCursor */
MADB_Stmt *MADB_FindCursor(MADB_Stmt *Stmt, const char *CursorName)
{
MADB_Dbc *Dbc= Stmt->Connection;
MADB_List *LStmt, *LStmtNext;
for (LStmt= Dbc->Stmts; LStmt; LStmt= LStmtNext)
{
MADB_Cursor *Cursor= &((MADB_Stmt *)LStmt->data)->Cursor;
LStmtNext= LStmt->next;
if (Stmt != (MADB_Stmt *)LStmt->data &&
Cursor->Name && _stricmp(Cursor->Name, CursorName) == 0)
{
return (MADB_Stmt *)LStmt->data;
}
}
MADB_SetError(&Stmt->Error, MADB_ERR_34000, NULL, 0);
return NULL;
}
/* }}} */
/* {{{ FetchMetadata */
MYSQL_RES* FetchMetadata(MADB_Stmt *Stmt)
{
ResetMetadata(&Stmt->metadata, mysql_stmt_result_metadata(Stmt->stmt));
return Stmt->metadata;
}
/* }}} */
/* {{{ MADB_StmtReset - reseting Stmt handler for new use. Has to be called inside a lock */
void MADB_StmtReset(MADB_Stmt *Stmt)
{
if (!QUERY_IS_MULTISTMT(Stmt->Query) || Stmt->MultiStmts == NULL)
{
if (Stmt->State > MADB_SS_PREPARED)
{
MDBUG_C_PRINT(Stmt->Connection, "mysql_stmt_free_result(%0x)", Stmt->stmt);
mysql_stmt_free_result(Stmt->stmt);
}
if (Stmt->State >= MADB_SS_PREPARED)
{
MDBUG_C_PRINT(Stmt->Connection, "-->closing %0x", Stmt->stmt);
MADB_STMT_CLOSE_STMT(Stmt);
Stmt->stmt= MADB_NewStmtHandle(Stmt);
MDBUG_C_PRINT(Stmt->Connection, "-->inited %0x", Stmt->stmt);
}
}
else
{
CloseMultiStatements(Stmt);
Stmt->stmt= MADB_NewStmtHandle(Stmt);
MDBUG_C_PRINT(Stmt->Connection, "-->inited %0x", Stmt->stmt);
}
switch (Stmt->State)
{
case MADB_SS_EXECUTED:
case MADB_SS_OUTPARAMSFETCHED:
MADB_FREE(Stmt->result);
MADB_FREE(Stmt->CharOffset);
MADB_FREE(Stmt->Lengths);
RESET_DAE_STATUS(Stmt);
case MADB_SS_PREPARED:
ResetMetadata(&Stmt->metadata, NULL);
Stmt->PositionedCursor= NULL;
Stmt->Ird->Header.Count= 0;
case MADB_SS_EMULATED:
/* We can have the case, then query did not succeed, and in case of direct execution we wouldn't
have ane state set, but some of stuff still needs to be cleaned. Perhaps we could introduce a state
for such case, smth like DIREXEC_PREPARED. Would be more proper, but yet overkill */
if (QUERY_IS_MULTISTMT(Stmt->Query))
{
while (mysql_more_results(Stmt->Connection->mariadb))
{
mysql_next_result(Stmt->Connection->mariadb);
}
}
default:
Stmt->PositionedCommand= 0;
Stmt->State= MADB_SS_INITED;
MADB_CLEAR_ERROR(&Stmt->Error);
}
}
/* }}} */
/* {{{ MADB_EDPrepare - Method called from SQLPrepare in case it is SQLExecDirect and if server >= 10.2
(i.e. we gonna do mariadb_stmt_exec_direct) */
SQLRETURN MADB_EDPrepare(MADB_Stmt *Stmt)
{
/* TODO: In case of positioned command it shouldn't be always*/
if ((Stmt->ParamCount= Stmt->Apd->Header.Count + (MADB_POSITIONED_COMMAND(Stmt) ? MADB_POS_COMM_IDX_FIELD_COUNT(Stmt) : 0)) != 0)
{
if (Stmt->params)
{
MADB_FREE(Stmt->params);
}
/* If we have "WHERE CURRENT OF", we will need bind additionaly parameters for each field in the index */
Stmt->params= (MYSQL_BIND *)MADB_CALLOC(sizeof(MYSQL_BIND) * Stmt->ParamCount);
}
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_RegularPrepare - Method called from SQLPrepare in case it is SQLExecDirect and if !(server > 10.2)
(i.e. we aren't going to do mariadb_stmt_exec_direct) */
SQLRETURN MADB_RegularPrepare(MADB_Stmt *Stmt)
{
LOCK_MARIADB(Stmt->Connection);
MDBUG_C_PRINT(Stmt->Connection, "mysql_stmt_prepare(%0x,%s)", Stmt->stmt, STMT_STRING(Stmt));
if (mysql_stmt_prepare(Stmt->stmt, STMT_STRING(Stmt), (unsigned long)strlen(STMT_STRING(Stmt))))
{
/* Need to save error first */
MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
/* We need to close the stmt here, or it becomes unusable like in ODBC-21 */
MDBUG_C_PRINT(Stmt->Connection, "mysql_stmt_close(%0x)", Stmt->stmt);
MADB_STMT_CLOSE_STMT(Stmt);
Stmt->stmt= MADB_NewStmtHandle(Stmt);
UNLOCK_MARIADB(Stmt->Connection);
MDBUG_C_PRINT(Stmt->Connection, "mysql_stmt_init(%0x)->%0x", Stmt->Connection->mariadb, Stmt->stmt);
return Stmt->Error.ReturnValue;
}
UNLOCK_MARIADB(Stmt->Connection);
Stmt->State= MADB_SS_PREPARED;
/* If we have result returning query - fill descriptor records with metadata */
if (mysql_stmt_field_count(Stmt->stmt) > 0)
{
MADB_DescSetIrdMetadata(Stmt, mysql_fetch_fields(FetchMetadata(Stmt)), mysql_stmt_field_count(Stmt->stmt));
}
if ((Stmt->ParamCount= (SQLSMALLINT)mysql_stmt_param_count(Stmt->stmt)))
{
if (Stmt->params)
{
MADB_FREE(Stmt->params);
}
Stmt->params= (MYSQL_BIND *)MADB_CALLOC(sizeof(MYSQL_BIND) * Stmt->ParamCount);
}
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_StmtPrepare */
SQLRETURN MADB_StmtPrepare(MADB_Stmt *Stmt, char *StatementText, SQLINTEGER TextLength, BOOL ExecDirect)
{
char *CursorName= NULL;
unsigned int WhereOffset;
BOOL HasParameters= 0;
MDBUG_C_PRINT(Stmt->Connection, "%sMADB_StmtPrepare", "\t->");
LOCK_MARIADB(Stmt->Connection);
MADB_StmtReset(Stmt);
/* After this point we can't have SQL_NTS*/
ADJUST_LENGTH(StatementText, TextLength);
/* There is no need to send anything to the server to find out there is syntax error here */
if (TextLength < MADB_MIN_QUERY_LEN)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_42000, NULL, 0);
}
MADB_ResetParser(Stmt, StatementText, TextLength);
MADB_ParseQuery(&Stmt->Query);
if ((Stmt->Query.QueryType == MADB_QUERY_INSERT || Stmt->Query.QueryType == MADB_QUERY_UPDATE || Stmt->Query.QueryType == MADB_QUERY_DELETE)
&& MADB_FindToken(&Stmt->Query, "RETURNING"))
{
Stmt->Query.ReturnsResult= '\1';
}
/* if we have multiple statements we save single statements in Stmt->StrMultiStmt
and store the number in Stmt->MultiStmts */
if (QueryIsPossiblyMultistmt(&Stmt->Query) && QUERY_IS_MULTISTMT(Stmt->Query) &&
(Stmt->Query.ReturnsResult || Stmt->Query.HasParameters) && Stmt->Query.BatchAllowed) /* If neither of statements returns result,
and does not have parameters, we will run them
using text protocol */
{
if (ExecDirect != FALSE)
{
return MADB_EDPrepare(Stmt);
}
/* We had error preparing any of statements */
else if (GetMultiStatements(Stmt, ExecDirect))
{
return Stmt->Error.ReturnValue;
}
/* all statemtens successfully prepared */
UNLOCK_MARIADB(Stmt->Connection);
return SQL_SUCCESS;
}
UNLOCK_MARIADB(Stmt->Connection);
if (!MADB_ValidateStmt(&Stmt->Query))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY000, "SQL command SET NAMES is not allowed", 0);
return Stmt->Error.ReturnValue;
}
/* Transform WHERE CURRENT OF [cursorname]:
Append WHERE with Parameter Markers
In StmtExecute we will call SQLSetPos with update or delete:
*/
if ((CursorName = MADB_ParseCursorName(&Stmt->Query, &WhereOffset)))
{
MADB_DynString StmtStr;
char *TableName;
/* Make sure we have a delete or update statement
MADB_QUERY_DELETE and MADB_QUERY_UPDATE defined in the enum to have the same value
as SQL_UPDATE and SQL_DELETE, respectively */
if (Stmt->Query.QueryType == MADB_QUERY_DELETE || Stmt->Query.QueryType == MADB_QUERY_UPDATE)
{
Stmt->PositionedCommand= 1;
}
else
{
MADB_SetError(&Stmt->Error, MADB_ERR_42000, "Invalid SQL Syntax: DELETE or UPDATE expected for positioned update", 0);
return Stmt->Error.ReturnValue;
}
if (!(Stmt->PositionedCursor= MADB_FindCursor(Stmt, CursorName)))
return Stmt->Error.ReturnValue;
TableName= MADB_GetTableName(Stmt->PositionedCursor);
MADB_InitDynamicString(&StmtStr, "", 8192, 1024);
MADB_DynstrAppendMem(&StmtStr, Stmt->Query.RefinedText, WhereOffset);
MADB_DynStrGetWhere(Stmt->PositionedCursor, &StmtStr, TableName, TRUE);
MADB_RESET(STMT_STRING(Stmt), StmtStr.str);
/* Constructed query we've copied for execution has parameters */
Stmt->Query.HasParameters= 1;
MADB_DynstrFree(&StmtStr);
}
if (Stmt->Options.MaxRows)
{
/* TODO: LIMIT is not always the last clause. And not applicable to each query type.
Thus we need to check query type and last tokens, and possibly put limit before them */
char *p;
STMT_STRING(Stmt)= realloc((char *)STMT_STRING(Stmt), strlen(STMT_STRING(Stmt)) + 40);
p= STMT_STRING(Stmt) + strlen(STMT_STRING(Stmt));
_snprintf(p, 40, " LIMIT %zd", Stmt->Options.MaxRows);
}
if (!Stmt->Query.ReturnsResult && !Stmt->Query.HasParameters &&
/* If have multistatement query, and this is not allowed, we want to do normal prepare.
To give it last chance. And to return correct error otherwise */
! (QUERY_IS_MULTISTMT(Stmt->Query) && !Stmt->Query.BatchAllowed))
{
Stmt->State= MADB_SS_EMULATED;
return SQL_SUCCESS;
}
if (ExecDirect && MADB_CheckIfExecDirectPossible(Stmt))
{
return MADB_EDPrepare(Stmt);
}
else
{
return MADB_RegularPrepare(Stmt);
}
}
/* }}} */
/* {{{ MADB_StmtParamData */
SQLRETURN MADB_StmtParamData(MADB_Stmt *Stmt, SQLPOINTER *ValuePtrPtr)
{
MADB_Desc *Desc;
MADB_DescRecord *Record;
int ParamCount;
int i;
SQLRETURN ret;
if (Stmt->DataExecutionType == MADB_DAE_NORMAL)
{
if (!Stmt->Apd || !(ParamCount= Stmt->ParamCount))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY010, NULL, 0);
return Stmt->Error.ReturnValue;
}
Desc= Stmt->Apd;
}
else
{
if (!Stmt->Ard || !(ParamCount= Stmt->DaeStmt->ParamCount))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY010, NULL, 0);
return Stmt->Error.ReturnValue;
}
Desc= Stmt->DaeStmt->Apd;
}
/* If we have last DAE param(Stmt->PutParam), we are starting from the next one. Otherwise from first */
for (i= Stmt->PutParam > -1 ? Stmt->PutParam + 1 : 0; i < ParamCount; i++)
{
if ((Record= MADB_DescGetInternalRecord(Desc, i, MADB_DESC_READ)))
{
if (Record->OctetLengthPtr)
{
/* Stmt->DaeRowNumber is 1 based */
SQLLEN *OctetLength = (SQLLEN *)GetBindOffset(Desc, Record, Record->OctetLengthPtr, Stmt->DaeRowNumber > 1 ? Stmt->DaeRowNumber - 1 : 0, sizeof(SQLLEN));
if (PARAM_IS_DAE(OctetLength))
{
Stmt->PutDataRec= Record;
*ValuePtrPtr = GetBindOffset(Desc, Record, Record->DataPtr, Stmt->DaeRowNumber > 1 ? Stmt->DaeRowNumber - 1 : 0, Record->OctetLength);
Stmt->PutParam= i;
Stmt->Status= SQL_NEED_DATA;
return SQL_NEED_DATA;
}
}
}
}
/* reset status, otherwise SQLSetPos and SQLExecute will fail */
MARK_DAE_DONE(Stmt);
if (Stmt->DataExecutionType == MADB_DAE_ADD || Stmt->DataExecutionType == MADB_DAE_UPDATE)
{
MARK_DAE_DONE(Stmt->DaeStmt);
}
switch (Stmt->DataExecutionType) {
case MADB_DAE_NORMAL:
ret= Stmt->Methods->Execute(Stmt, FALSE);
RESET_DAE_STATUS(Stmt);
break;
case MADB_DAE_UPDATE:
ret= Stmt->Methods->SetPos(Stmt, Stmt->DaeRowNumber, SQL_UPDATE, SQL_LOCK_NO_CHANGE, 1);
RESET_DAE_STATUS(Stmt);
break;
case MADB_DAE_ADD:
ret= Stmt->DaeStmt->Methods->Execute(Stmt->DaeStmt, FALSE);
MADB_CopyError(&Stmt->Error, &Stmt->DaeStmt->Error);
RESET_DAE_STATUS(Stmt->DaeStmt);
break;
default:
ret= SQL_ERROR;
}
/* Interesting should we reset if execution failed? */
return ret;
}
/* }}} */
/* {{{ MADB_StmtPutData */
SQLRETURN MADB_StmtPutData(MADB_Stmt *Stmt, SQLPOINTER DataPtr, SQLLEN StrLen_or_Ind)
{
MADB_DescRecord *Record;
MADB_Stmt *MyStmt= Stmt;
SQLPOINTER ConvertedDataPtr= NULL;
SQLULEN Length= 0;
MADB_CLEAR_ERROR(&Stmt->Error);
if (DataPtr != NULL && StrLen_or_Ind < 0 && StrLen_or_Ind != SQL_NTS && StrLen_or_Ind != SQL_NULL_DATA)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY090, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (Stmt->DataExecutionType != MADB_DAE_NORMAL)
{
MyStmt= Stmt->DaeStmt;
}
Record= MADB_DescGetInternalRecord(MyStmt->Apd, Stmt->PutParam, MADB_DESC_READ);
assert(Record);
if (StrLen_or_Ind == SQL_NULL_DATA)
{
/* Check if we've already sent any data */
if (MyStmt->stmt->params[Stmt->PutParam].long_data_used)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY011, NULL, 0);
return Stmt->Error.ReturnValue;
}
Record->Type= SQL_TYPE_NULL;
return SQL_SUCCESS;
}
/* This normally should be enforced by DM */
if (DataPtr == NULL && StrLen_or_Ind != 0)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY009, NULL, 0);
return Stmt->Error.ReturnValue;
}
/*
if (StrLen_or_Ind == SQL_NTS)
{
if (Record->ConciseType == SQL_C_WCHAR)
StrLen_or_Ind= wcslen((SQLWCHAR *)DataPtr);
else
StrLen_or_Ind= strlen((char *)DataPtr);
}
*/
if (Record->ConciseType == SQL_C_WCHAR)
{
/* Conn cs */
ConvertedDataPtr= MADB_ConvertFromWChar((SQLWCHAR *)DataPtr, (SQLINTEGER)(StrLen_or_Ind/sizeof(SQLWCHAR)), &Length, &Stmt->Connection->Charset, NULL);
if ((ConvertedDataPtr == NULL || Length == 0) && StrLen_or_Ind > 0)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
return Stmt->Error.ReturnValue;
}
}
else
{
if (StrLen_or_Ind == SQL_NTS)
{
Length= strlen((char *)DataPtr);
}
else
{
Length= StrLen_or_Ind;
}
}
/* To make sure that we will not consume the doble amount of memory, we need to send
data via mysql_send_long_data directly to the server instead of allocating a separate
buffer. This means we need to process Update and Insert statements row by row. */
if (mysql_stmt_send_long_data(MyStmt->stmt, Stmt->PutParam, (ConvertedDataPtr ? (char *)ConvertedDataPtr : DataPtr), (unsigned long)Length))
{
MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, MyStmt->stmt);
}
else
{
Record->InternalLength+= (unsigned long)Length;
}
MADB_FREE(ConvertedDataPtr);
return Stmt->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_ExecutePositionedUpdate */
SQLRETURN MADB_ExecutePositionedUpdate(MADB_Stmt *Stmt, BOOL ExecDirect)
{
SQLSMALLINT j;
SQLRETURN ret;
MADB_DynArray DynData;
MADB_Stmt *SaveCursor;
char *p;
MADB_CLEAR_ERROR(&Stmt->Error);
if (!Stmt->PositionedCursor->result)
{
MADB_SetError(&Stmt->Error, MADB_ERR_34000, "Cursor has no result set or is not open", 0);
return Stmt->Error.ReturnValue;
}
MADB_StmtDataSeek(Stmt->PositionedCursor, Stmt->PositionedCursor->Cursor.Position);
Stmt->Methods->RefreshRowPtrs(Stmt->PositionedCursor);
memcpy(&Stmt->Apd->Header, &Stmt->Ard->Header, sizeof(MADB_Header));
Stmt->AffectedRows= 0;
MADB_InitDynamicArray(&DynData, sizeof(char *), 8, 8);
for (j= 1; j < MADB_POS_COMM_IDX_FIELD_COUNT(Stmt) + 1; ++j)
{
SQLLEN Length;
MADB_DescRecord *Rec= MADB_DescGetInternalRecord(Stmt->PositionedCursor->Ard, j, MADB_DESC_READ);
Length= Rec->OctetLength;
/* if (Rec->inUse)
MA_SQLBindParameter(Stmt, j+1, SQL_PARAM_INPUT, Rec->ConciseType, Rec->Type, Rec->DisplaySize, Rec->Scale, Rec->DataPtr, Length, Rec->OctetLengthPtr);
else */
{
Stmt->Methods->GetData(Stmt->PositionedCursor, j, SQL_CHAR, NULL, 0, &Length, TRUE);
p= (char *)MADB_CALLOC(Length + 2);
MADB_InsertDynamic(&DynData, (char *)&p);
Stmt->Methods->GetData(Stmt->PositionedCursor, j, SQL_CHAR, p, Length + 1, NULL, TRUE);
Stmt->Methods->BindParam(Stmt, j + (Stmt->ParamCount - MADB_POS_COMM_IDX_FIELD_COUNT(Stmt)), SQL_PARAM_INPUT, SQL_CHAR, SQL_CHAR, 0, 0, p, Length, NULL);
}
}
SaveCursor= Stmt->PositionedCursor;
Stmt->PositionedCursor= NULL;
ret= Stmt->Methods->Execute(Stmt, ExecDirect);
Stmt->PositionedCursor= SaveCursor;
/* For the case of direct execution we need to restore number of parameters bound by application, for the case when application
re-uses handle with same parameters for another query. Otherwise we won't know that number (of application's parameters) */
if (ExecDirect)
{
Stmt->Apd->Header.Count-= MADB_POS_COMM_IDX_FIELD_COUNT(Stmt);
}
for (j=0; j < (int)DynData.elements; j++)
{
MADB_GetDynamic(&DynData, (char *)&p, j);
MADB_FREE(p);
}
MADB_DeleteDynamic(&DynData);
if (Stmt->PositionedCursor->Options.CursorType == SQL_CURSOR_DYNAMIC &&
(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
{
SQLRETURN rc;
rc= Stmt->Methods->RefreshDynamicCursor(Stmt->PositionedCursor);
if (!SQL_SUCCEEDED(rc))
{
MADB_CopyError(&Stmt->Error, &Stmt->PositionedCursor->Error);
return Stmt->Error.ReturnValue;
}
if (Stmt->Query.QueryType == SQL_DELETE)
MADB_STMT_RESET_CURSOR(Stmt->PositionedCursor);
}
//MADB_FREE(DataPtr);
return ret;
}
/* }}} */
/* {{{ MADB_GetOutParams */
SQLRETURN MADB_GetOutParams(MADB_Stmt *Stmt, int CurrentOffset)
{
MYSQL_BIND *Bind;
unsigned int i=0, ParameterNr= 0;
/* Since Outparams are only one row, we use store result */
if (mysql_stmt_store_result(Stmt->stmt))
{
return MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt);
}
Bind= (MYSQL_BIND *)MADB_CALLOC(sizeof(MYSQL_BIND) * mysql_stmt_field_count(Stmt->stmt));
for (i=0; i < (unsigned int)Stmt->ParamCount && ParameterNr < mysql_stmt_field_count(Stmt->stmt); i++)
{
MADB_DescRecord *IpdRecord, *ApdRecord;
if ((IpdRecord= MADB_DescGetInternalRecord(Stmt->Ipd, i, MADB_DESC_READ))!= NULL)
{
if (IpdRecord->ParameterType == SQL_PARAM_INPUT_OUTPUT ||
IpdRecord->ParameterType == SQL_PARAM_OUTPUT)
{
ApdRecord= MADB_DescGetInternalRecord(Stmt->Apd, i, MADB_DESC_READ);
Bind[ParameterNr].buffer= GetBindOffset(Stmt->Apd, ApdRecord, ApdRecord->DataPtr, CurrentOffset, ApdRecord->OctetLength);
if (ApdRecord->OctetLengthPtr)
{
Bind[ParameterNr].length= (unsigned long *)GetBindOffset(Stmt->Apd, ApdRecord, ApdRecord->OctetLengthPtr,
CurrentOffset, sizeof(SQLLEN));
}
Bind[ParameterNr].buffer_length= (unsigned long)ApdRecord->OctetLength;
Bind[ParameterNr].buffer_type= Stmt->stmt->params[i].buffer_type;
ParameterNr++;
}
}
}
mysql_stmt_bind_result(Stmt->stmt, Bind);
mysql_stmt_fetch(Stmt->stmt);
mysql_stmt_data_seek(Stmt->stmt, 0);
MADB_FREE(Bind);
return SQL_SUCCESS;
}
/* }}} */
/* {{{ ResetInternalLength */
static void ResetInternalLength(MADB_Stmt *Stmt, unsigned int ParamOffset)
{
unsigned int i;
MADB_DescRecord *ApdRecord;
for (i= ParamOffset; i < ParamOffset + Stmt->ParamCount; ++i)
{
if ((ApdRecord= MADB_DescGetInternalRecord(Stmt->Apd, i, MADB_DESC_READ)))
{
ApdRecord->InternalLength= 0;
}
}
}
/* }}} */
/* {{{ MADB_DoExecute */
/* Actually executing on the server, doing required actions with C API, and processing execution result */
SQLRETURN MADB_DoExecute(MADB_Stmt *Stmt, BOOL ExecDirect)
{
SQLRETURN ret= SQL_SUCCESS;
/**************************** mysql_stmt_bind_param **********************************/
if (ExecDirect)
{
mysql_stmt_attr_set(Stmt->stmt, STMT_ATTR_PREBIND_PARAMS, &Stmt->ParamCount);
}
mysql_stmt_attr_set(Stmt->stmt, STMT_ATTR_ARRAY_SIZE, (void*)&Stmt->Bulk.ArraySize);
if (Stmt->ParamCount)
{
mysql_stmt_bind_param(Stmt->stmt, Stmt->params);
}
ret= SQL_SUCCESS;
/**************************** mysql_stmt_execute *************************************/
MDBUG_C_PRINT(Stmt->Connection, ExecDirect ? "mariadb_stmt_execute_direct(%0x,%s)"
: "mariadb_stmt_execute(%0x)(%s)", Stmt->stmt, STMT_STRING(Stmt));
if ((ExecDirect && mariadb_stmt_execute_direct(Stmt->stmt, STMT_STRING(Stmt), strlen(STMT_STRING(Stmt))))
|| (!ExecDirect && mysql_stmt_execute(Stmt->stmt)))
{
ret= MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
MDBUG_C_PRINT(Stmt->Connection, "mysql_stmt_execute:ERROR%s", "");
}
else
{
unsigned int ServerStatus;
Stmt->State= MADB_SS_EXECUTED;
mariadb_get_infov(Stmt->Connection->mariadb, MARIADB_CONNECTION_SERVER_STATUS, (void*)&ServerStatus);
if (ServerStatus & SERVER_PS_OUT_PARAMS)
{
Stmt->State= MADB_SS_OUTPARAMSFETCHED;
ret= Stmt->Methods->GetOutParams(Stmt, 0);
}
}
return ret;
}
/* }}} */
void MADB_SetStatusArray(MADB_Stmt *Stmt, SQLUSMALLINT Status)
{
if (Stmt->Ipd->Header.ArrayStatusPtr != NULL)
{
memset(Stmt->Ipd->Header.ArrayStatusPtr, 0x00ff & Status, Stmt->Apd->Header.ArraySize*sizeof(SQLUSMALLINT));
if (Stmt->Apd->Header.ArrayStatusPtr != NULL)
{
unsigned int i;
for (i= 0; i < Stmt->Apd->Header.ArraySize; ++i)
{
if (Stmt->Apd->Header.ArrayStatusPtr[i] == SQL_PARAM_IGNORE)
{
Stmt->Ipd->Header.ArrayStatusPtr[i]= SQL_PARAM_UNUSED;
}
}
}
}
}
/* {{{ MADB_StmtExecute */
SQLRETURN MADB_StmtExecute(MADB_Stmt *Stmt, BOOL ExecDirect)
{
unsigned int i;
MYSQL_RES *DefaultResult= NULL;
SQLRETURN ret= SQL_SUCCESS;
unsigned int ErrorCount= 0;
unsigned int StatementNr;
unsigned int ParamOffset= 0; /* for multi statements */
/* Will use it for STMT_ATTR_ARRAY_SIZE and as indicator if we are deploying MariaDB bulk insert feature */
unsigned int MariadbArrSize= MADB_BulkInsertPossible(Stmt) != FALSE ? (unsigned int)Stmt->Apd->Header.ArraySize : 0;
SQLULEN j, Start= 0;
/* For multistatement direct execution */
char *CurQuery= Stmt->Query.RefinedText, *QueriesEnd= Stmt->Query.RefinedText + Stmt->Query.RefinedLength;
MDBUG_C_PRINT(Stmt->Connection, "%sMADB_StmtExecute", "\t->");
MADB_CLEAR_ERROR(&Stmt->Error);
if (Stmt->State == MADB_SS_EMULATED)
{
return MADB_ExecuteQuery(Stmt, STMT_STRING(Stmt), (SQLINTEGER)strlen(STMT_STRING(Stmt)));
}
if (MADB_POSITIONED_COMMAND(Stmt))
{
return MADB_ExecutePositionedUpdate(Stmt, ExecDirect);
}
/* Stmt->params was allocated during prepare, but could be cleared
by SQLResetStmt. In latter case we need to allocate it again */
if (!Stmt->params &&
!(Stmt->params = (MYSQL_BIND *)MADB_CALLOC(sizeof(MYSQL_BIND) * MADB_STMT_PARAM_COUNT(Stmt))))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
return Stmt->Error.ReturnValue;
}
/* Normally this check is done by a DM. We are doing that too, keeping in mind direct linking.
If exectution routine called from the SQLParamData, DataExecutionType has been reset */
if (Stmt->Status == SQL_NEED_DATA && !DAE_DONE(Stmt))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY010, NULL, 0);
}
LOCK_MARIADB(Stmt->Connection);
Stmt->AffectedRows= 0;
Start+= Stmt->ArrayOffset;
if (Stmt->Ipd->Header.RowsProcessedPtr)
{
*Stmt->Ipd->Header.RowsProcessedPtr= 0;
}
if (MariadbArrSize > 1)
{
if (MADB_DOING_BULK_OPER(Stmt))
{
//MADB_CleanBulkOperationData(Stmt);
}
Stmt->Bulk.ArraySize= MariadbArrSize;
Stmt->Bulk.HasRowsToSkip= 0;
}
for (StatementNr= 0; StatementNr < STMT_COUNT(Stmt->Query); ++StatementNr)
{
if (QUERY_IS_MULTISTMT(Stmt->Query))
{
if (Stmt->MultiStmts && Stmt->MultiStmts[StatementNr] != NULL)
{
Stmt->stmt= Stmt->MultiStmts[StatementNr];
}
else
{
/* We have direct execution, since otherwise it'd already prepared, and thus Stmt->MultiStmts would be set */
if (CurQuery >= QueriesEnd)
{
/* Something went wrong(with parsing). But we've got here, and everything worked. Giving it chance to fail later.
This shouldn't really happen */
MDBUG_C_PRINT(Stmt->Connection, "Got past end of query direct-executing %s on stmt #%u", Stmt->Query.RefinedText, StatementNr);
continue;
}
if (StatementNr > 0)
{
Stmt->stmt= MADB_NewStmtHandle(Stmt);
}
else
{
Stmt->MultiStmts= (MYSQL_STMT **)MADB_CALLOC(sizeof(MYSQL_STMT) * STMT_COUNT(Stmt->Query));
}
Stmt->MultiStmts[StatementNr]= Stmt->stmt;
if (mysql_stmt_prepare(Stmt->stmt, CurQuery, (unsigned long)strlen(CurQuery)))
{
return MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
}
CurQuery+= strlen(CurQuery) + 1;
}
Stmt->RebindParams= TRUE;
if (Stmt->ParamCount != mysql_stmt_param_count(Stmt->stmt))
{
Stmt->ParamCount= (SQLSMALLINT)mysql_stmt_param_count(Stmt->stmt);
Stmt->params= (MYSQL_BIND*)MADB_REALLOC(Stmt->params, sizeof(MYSQL_BIND) * MADB_STMT_PARAM_COUNT(Stmt));
}
memset(Stmt->params, 0, sizeof(MYSQL_BIND) * MADB_STMT_PARAM_COUNT(Stmt));
}
if (MADB_DOING_BULK_OPER(Stmt))
{
if (!SQL_SUCCEEDED(MADB_ExecuteBulk(Stmt, ParamOffset)))
{
/* Doing just the same thing as we would do in general case */
MADB_CleanBulkOperData(Stmt, ParamOffset);
ErrorCount= (unsigned int)Stmt->Apd->Header.ArraySize;
MADB_SetStatusArray(Stmt, SQL_PARAM_DIAG_UNAVAILABLE);
goto end;
}
else if (!mysql_stmt_field_count(Stmt->stmt) && !Stmt->MultiStmts)
{
Stmt->AffectedRows+= mysql_stmt_affected_rows(Stmt->stmt);
}
/* Suboptimal, but more reliable and simple */
MADB_CleanBulkOperData(Stmt, ParamOffset);
Stmt->ArrayOffset+= (int)Stmt->Apd->Header.ArraySize;
if (Stmt->Ipd->Header.RowsProcessedPtr)
{
*Stmt->Ipd->Header.RowsProcessedPtr= *Stmt->Ipd->Header.RowsProcessedPtr + Stmt->Apd->Header.ArraySize;
}
MADB_SetStatusArray(Stmt, SQL_PARAM_SUCCESS);
}
else
{
/* Convert and bind parameters */
for (j= Start; j < Start + Stmt->Apd->Header.ArraySize; ++j)
{
/* "... In an IPD, this SQLUINTEGER * header field points to a buffer containing the number
of sets of parameters that have been processed, including error sets. ..." */
if (Stmt->Ipd->Header.RowsProcessedPtr)
{
*Stmt->Ipd->Header.RowsProcessedPtr= *Stmt->Ipd->Header.RowsProcessedPtr + 1;
}
if (Stmt->Apd->Header.ArrayStatusPtr &&
Stmt->Apd->Header.ArrayStatusPtr[j-Start] == SQL_PARAM_IGNORE)
{
if (Stmt->Ipd->Header.ArrayStatusPtr)
{
Stmt->Ipd->Header.ArrayStatusPtr[j-Start]= SQL_PARAM_UNUSED;
}
continue;
}
for (i= ParamOffset; i < ParamOffset + MADB_STMT_PARAM_COUNT(Stmt); ++i)
{
MADB_DescRecord *ApdRecord, *IpdRecord;
if ((ApdRecord= MADB_DescGetInternalRecord(Stmt->Apd, i, MADB_DESC_READ)) &&
(IpdRecord= MADB_DescGetInternalRecord(Stmt->Ipd, i, MADB_DESC_READ)))
{
/* check if parameter was bound */
if (!ApdRecord->inUse)
{
ret= MADB_SetError(&Stmt->Error, MADB_ERR_07002, NULL, 0);
goto end;
}
if (MADB_ConversionSupported(ApdRecord, IpdRecord) == FALSE)
{
ret= MADB_SetError(&Stmt->Error, MADB_ERR_07006, NULL, 0);
goto end;
}
Stmt->params[i-ParamOffset].length= NULL;
ret= MADB_C2SQL(Stmt, ApdRecord, IpdRecord, j - Start, &Stmt->params[i-ParamOffset]);
if (!SQL_SUCCEEDED(ret))
{
goto end;
}
}
} /* End of for() on parameters */
if (Stmt->RebindParams && MADB_STMT_PARAM_COUNT(Stmt))
{
Stmt->stmt->bind_param_done= 1;
Stmt->RebindParams= FALSE;
}
ret= MADB_DoExecute(Stmt, ExecDirect && MADB_CheckIfExecDirectPossible(Stmt));
if (!SQL_SUCCEEDED(ret))
{
++ErrorCount;
}
/* We need to unset InternalLength, i.e. reset dae length counters for next stmt.
However that length is not used anywhere, and is not clear what is it needed for */
ResetInternalLength(Stmt, ParamOffset);
if (Stmt->Ipd->Header.ArrayStatusPtr)
{
Stmt->Ipd->Header.ArrayStatusPtr[j-Start]= SQL_SUCCEEDED(ret) ? SQL_PARAM_SUCCESS :
(j == Stmt->Apd->Header.ArraySize - 1) ? SQL_PARAM_ERROR : SQL_PARAM_DIAG_UNAVAILABLE;
}
if (!mysql_stmt_field_count(Stmt->stmt) && SQL_SUCCEEDED(ret) && !Stmt->MultiStmts)
{
Stmt->AffectedRows+= mysql_stmt_affected_rows(Stmt->stmt);
}
++Stmt->ArrayOffset;
if (!SQL_SUCCEEDED(ret) && j == Start + Stmt->Apd->Header.ArraySize)
{
goto end;
}
} /* End of for() thru paramsets(parameters array) */
} /* End of if (bulk/not bulk) execution */
if (QUERY_IS_MULTISTMT(Stmt->Query))
{
/* If we optimize memory allocation, then we will need to free bulk operation data here(among other places) */
/* MADB_CleanBulkOperData(Stmt, ParamOffset); */
ParamOffset+= MADB_STMT_PARAM_COUNT(Stmt);
if (mysql_stmt_field_count(Stmt->stmt))
{
mysql_stmt_store_result(Stmt->stmt);
}
}
} /* End of for() on statements(Multistatmt) */
/* All rows processed, so we can unset ArrayOffset */
Stmt->ArrayOffset= 0;
if (Stmt->MultiStmts)
{
Stmt->MultiStmtNr= 0;
MADB_InstallStmt(Stmt, Stmt->MultiStmts[Stmt->MultiStmtNr]);
}
else if (mysql_stmt_field_count(Stmt->stmt) > 0)
{
MADB_StmtResetResultStructures(Stmt);
/* Todo: for SQL_CURSOR_FORWARD_ONLY we should use cursor and prefetch rows */
/*************************** mysql_stmt_store_result ******************************/
/*If we did OUT params already, we should not store */
if (Stmt->State == MADB_SS_EXECUTED && mysql_stmt_store_result(Stmt->stmt) != 0)
{
UNLOCK_MARIADB(Stmt->Connection);
if (DefaultResult)
{
mysql_free_result(DefaultResult);
}
return MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
}
/* I don't think we can reliably establish the fact that we do not need to re-fetch the metadata, thus we are re-fetching always
The fact that we have resultset has been established above in "if" condition(fields count is > 0) */
MADB_DescSetIrdMetadata(Stmt, mysql_fetch_fields(FetchMetadata(Stmt)), mysql_stmt_field_count(Stmt->stmt));
Stmt->AffectedRows= -1;
}
end:
UNLOCK_MARIADB(Stmt->Connection);
Stmt->LastRowFetched= 0;
if (DefaultResult)
mysql_free_result(DefaultResult);
if (ErrorCount)
{
if (ErrorCount < Stmt->Apd->Header.ArraySize)
ret= SQL_SUCCESS_WITH_INFO;
else
ret= SQL_ERROR;
}
return ret;
}
/* }}} */
/* {{{ MADB_StmtBindCol */
SQLRETURN MADB_StmtBindCol(MADB_Stmt *Stmt, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType,
SQLPOINTER TargetValuePtr, SQLLEN BufferLength, SQLLEN *StrLen_or_Ind)
{
MADB_Desc *Ard= Stmt->Ard;
MADB_DescRecord *Record;
if ((ColumnNumber < 1 && Stmt->Options.UseBookmarks == SQL_UB_OFF) ||
(mysql_stmt_field_count(Stmt->stmt) &&
Stmt->stmt->state > MYSQL_STMT_PREPARED &&
ColumnNumber > mysql_stmt_field_count(Stmt->stmt)))
{
MADB_SetError(&Stmt->Error, MADB_ERR_07009, NULL, 0);
return SQL_ERROR;
}
/* Bookmark */
if (ColumnNumber == 0)
{
if (TargetType == SQL_C_BOOKMARK || TargetType == SQL_C_VARBOOKMARK)
{
Stmt->Options.BookmarkPtr= TargetValuePtr;
Stmt->Options.BookmarkLength = BufferLength;
Stmt->Options.BookmarkType= TargetType;
return SQL_SUCCESS;
}
MADB_SetError(&Stmt->Error, MADB_ERR_07006, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (!(Record= MADB_DescGetInternalRecord(Ard, ColumnNumber - 1, MADB_DESC_WRITE)))
{
MADB_CopyError(&Stmt->Error, &Ard->Error);
return Stmt->Error.ReturnValue;
}
/* check if we need to unbind and delete a record */
if (!TargetValuePtr && !StrLen_or_Ind)
{
int i;
Record->inUse= 0;
/* Update counter */
for (i= Ard->Records.elements; i > 0; i--)
{
MADB_DescRecord *Rec= MADB_DescGetInternalRecord(Ard, i-1, MADB_DESC_READ);
if (Rec && Rec->inUse)
{
Ard->Header.Count= i;
return SQL_SUCCESS;
}
}
Ard->Header.Count= 0;
return SQL_SUCCESS;
}
if (!SQL_SUCCEEDED(MADB_DescSetField(Ard, ColumnNumber, SQL_DESC_TYPE, (SQLPOINTER)(SQLLEN)TargetType, SQL_IS_SMALLINT, 0)) ||
!SQL_SUCCEEDED(MADB_DescSetField(Ard, ColumnNumber, SQL_DESC_OCTET_LENGTH_PTR, (SQLPOINTER)StrLen_or_Ind, SQL_IS_POINTER, 0)) ||
!SQL_SUCCEEDED(MADB_DescSetField(Ard, ColumnNumber, SQL_DESC_INDICATOR_PTR, (SQLPOINTER)StrLen_or_Ind, SQL_IS_POINTER, 0)) ||
!SQL_SUCCEEDED(MADB_DescSetField(Ard, ColumnNumber, SQL_DESC_OCTET_LENGTH, (SQLPOINTER)MADB_GetTypeLength(TargetType, BufferLength), SQL_IS_INTEGER, 0)) ||
!SQL_SUCCEEDED(MADB_DescSetField(Ard, ColumnNumber, SQL_DESC_DATA_PTR, TargetValuePtr, SQL_IS_POINTER, 0)))
{
MADB_CopyError(&Stmt->Error, &Ard->Error);
return Stmt->Error.ReturnValue;
}
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_StmtBindParam */
SQLRETURN MADB_StmtBindParam(MADB_Stmt *Stmt, SQLUSMALLINT ParameterNumber,
SQLSMALLINT InputOutputType, SQLSMALLINT ValueType,
SQLSMALLINT ParameterType, SQLULEN ColumnSize,
SQLSMALLINT DecimalDigits, SQLPOINTER ParameterValuePtr,
SQLLEN BufferLength, SQLLEN *StrLen_or_IndPtr)
{
MADB_Desc *Apd= Stmt->Apd,
*Ipd= Stmt->Ipd;
MADB_DescRecord *ApdRecord, *IpdRecord;
SQLRETURN ret= SQL_SUCCESS;
MADB_CLEAR_ERROR(&Stmt->Error);
if (!(ApdRecord= MADB_DescGetInternalRecord(Apd, ParameterNumber - 1, MADB_DESC_WRITE)))
{
MADB_CopyError(&Stmt->Error, &Apd->Error);
return Stmt->Error.ReturnValue;
}
if (!(IpdRecord= MADB_DescGetInternalRecord(Ipd, ParameterNumber - 1, MADB_DESC_WRITE)))
{
MADB_CopyError(&Stmt->Error, &Ipd->Error);
return Stmt->Error.ReturnValue;
}
/* Map to the correspoinding type */
if (ValueType == SQL_C_DEFAULT)
{
ValueType= MADB_GetDefaultType(ParameterType);
}
if (!(SQL_SUCCEEDED(MADB_DescSetField(Apd, ParameterNumber, SQL_DESC_CONCISE_TYPE, (SQLPOINTER)(SQLLEN)ValueType, SQL_IS_SMALLINT, 0))) ||
!(SQL_SUCCEEDED(MADB_DescSetField(Apd, ParameterNumber, SQL_DESC_OCTET_LENGTH_PTR, (SQLPOINTER)StrLen_or_IndPtr, SQL_IS_POINTER, 0))) ||
!(SQL_SUCCEEDED(MADB_DescSetField(Apd, ParameterNumber, SQL_DESC_OCTET_LENGTH, (SQLPOINTER)MADB_GetTypeLength(ValueType, BufferLength), SQL_IS_INTEGER, 0))) ||
!(SQL_SUCCEEDED(MADB_DescSetField(Apd, ParameterNumber, SQL_DESC_INDICATOR_PTR, (SQLPOINTER)StrLen_or_IndPtr, SQL_IS_POINTER, 0))) ||
!(SQL_SUCCEEDED(MADB_DescSetField(Apd, ParameterNumber, SQL_DESC_DATA_PTR, ParameterValuePtr, SQL_IS_POINTER, 0))))
{
MADB_CopyError(&Stmt->Error, &Apd->Error);
return Stmt->Error.ReturnValue;
}
if (!(SQL_SUCCEEDED(MADB_DescSetField(Ipd, ParameterNumber, SQL_DESC_CONCISE_TYPE, (SQLPOINTER)(SQLLEN)ParameterType, SQL_IS_SMALLINT, 0))) ||
!(SQL_SUCCEEDED(MADB_DescSetField(Ipd, ParameterNumber, SQL_DESC_PARAMETER_TYPE, (SQLPOINTER)(SQLLEN)InputOutputType, SQL_IS_SMALLINT, 0))))
{
MADB_CopyError(&Stmt->Error, &Ipd->Error);
return Stmt->Error.ReturnValue;
}
switch(ParameterType) {
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
case SQL_WCHAR:
case SQL_WLONGVARCHAR:
case SQL_WVARCHAR:
ret= MADB_DescSetField(Ipd, ParameterNumber, SQL_DESC_LENGTH, (SQLPOINTER)ColumnSize, SQL_IS_INTEGER, 0);
break;
case SQL_FLOAT:
case SQL_REAL:
case SQL_DOUBLE:
ret= MADB_DescSetField(Ipd, ParameterNumber, SQL_DESC_PRECISION, (SQLPOINTER)ColumnSize, SQL_IS_INTEGER, 0);
break;
case SQL_DECIMAL:
case SQL_NUMERIC:
ret= MADB_DescSetField(Ipd, ParameterNumber, SQL_DESC_PRECISION, (SQLPOINTER)ColumnSize, SQL_IS_SMALLINT, 0);
if (SQL_SUCCEEDED(ret))
ret= MADB_DescSetField(Ipd, ParameterNumber, SQL_DESC_SCALE, (SQLPOINTER)(SQLLEN)DecimalDigits, SQL_IS_SMALLINT, 0);
break;
case SQL_INTERVAL_MINUTE_TO_SECOND:
case SQL_INTERVAL_HOUR_TO_SECOND:
case SQL_INTERVAL_DAY_TO_SECOND:
case SQL_INTERVAL_SECOND:
case SQL_TYPE_TIMESTAMP:
case SQL_TYPE_TIME:
ret= MADB_DescSetField(Ipd, ParameterNumber, SQL_DESC_PRECISION, (SQLPOINTER)(SQLLEN)DecimalDigits, SQL_IS_SMALLINT, 0);
break;
}
if(!SQL_SUCCEEDED(ret))
MADB_CopyError(&Stmt->Error, &Ipd->Error);
Stmt->RebindParams= TRUE;
return ret;
}
/* }}} */
void MADB_InitStatusPtr(SQLUSMALLINT *Ptr, SQLULEN Size, SQLSMALLINT InitialValue)
{
SQLULEN i;
for (i=0; i < Size; i++)
Ptr[i]= InitialValue;
}
/* Not used for now, but leaving it so far here - it may be useful */
/* BOOL MADB_NumericBufferType(SQLSMALLINT BufferType)
{
switch (BufferType)
{
case SQL_C_TINYINT:
case SQL_C_UTINYINT:
case SQL_C_STINYINT:
case SQL_C_SHORT:
case SQL_C_SSHORT:
case SQL_C_USHORT:
case SQL_C_FLOAT:
case SQL_C_LONG:
case SQL_C_ULONG:
case SQL_C_SLONG:
case SQL_C_DOUBLE:
return TRUE;
default:
return FALSE;
}
}*/
/* {{{ MADB_BinaryFieldType */
BOOL MADB_BinaryFieldType(SQLSMALLINT FieldType)
{
return FieldType == SQL_BINARY || FieldType == SQL_BIT;
}
/* }}} */
/* {{{ MADB_PrepareBind
Filling bind structures in */
SQLRETURN MADB_PrepareBind(MADB_Stmt *Stmt, int RowNumber)
{
MADB_DescRecord *IrdRec, *ArdRec;
int i;
void *DataPtr= NULL;
for (i= 0; i < MADB_STMT_COLUMN_COUNT(Stmt); ++i)
{
ArdRec= MADB_DescGetInternalRecord(Stmt->Ard, i, MADB_DESC_READ);
if (ArdRec == NULL || !ArdRec->inUse)
{
Stmt->result[i].flags|= MADB_BIND_DUMMY;
continue;
}
DataPtr= (SQLLEN *)GetBindOffset(Stmt->Ard, ArdRec, ArdRec->DataPtr, RowNumber, ArdRec->OctetLength);
MADB_FREE(ArdRec->InternalBuffer);
if (!DataPtr)
{
Stmt->result[i].flags|= MADB_BIND_DUMMY;
continue;
}
else
{
Stmt->result[i].flags&= ~MADB_BIND_DUMMY;
}
IrdRec= MADB_DescGetInternalRecord(Stmt->Ird, i, MADB_DESC_READ);
/* assert(IrdRec != NULL) */
/* We can't use application's buffer directly, as it has/can have different size, than C/C needs */
Stmt->result[i].length= &Stmt->result[i].length_value;
switch(ArdRec->ConciseType) {
case SQL_C_WCHAR:
/* In worst case for 2 bytes of UTF16 in result, we need 3 bytes of utf8.
For ASCII we need 2 times less(for 2 bytes of UTF16 - 1 byte UTF8,
in other cases we need same 2 of 4 bytes. */
ArdRec->InternalBuffer= (char *)MADB_CALLOC((size_t)((ArdRec->OctetLength)*1.5));
Stmt->result[i].buffer= ArdRec->InternalBuffer;
Stmt->result[i].buffer_length= (unsigned long)(ArdRec->OctetLength*1.5);
Stmt->result[i].buffer_type= MYSQL_TYPE_STRING;
break;
case SQL_C_CHAR:
Stmt->result[i].buffer= DataPtr;
Stmt->result[i].buffer_length= (unsigned long)ArdRec->OctetLength;
Stmt->result[i].buffer_type= MYSQL_TYPE_STRING;
break;
case SQL_C_NUMERIC:
MADB_FREE(ArdRec->InternalBuffer);
Stmt->result[i].buffer_length= MADB_DEFAULT_PRECISION + 1/*-*/ + 1/*.*/;
ArdRec->InternalBuffer= (char *)MADB_CALLOC(Stmt->result[i].buffer_length);
Stmt->result[i].buffer= ArdRec->InternalBuffer;
Stmt->result[i].buffer_type= MYSQL_TYPE_STRING;
break;
case SQL_TYPE_TIMESTAMP:
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
case SQL_C_TIMESTAMP:
case SQL_C_TIME:
case SQL_C_DATE:
MADB_FREE(ArdRec->InternalBuffer);
if (IrdRec->ConciseType == SQL_CHAR || IrdRec->ConciseType == SQL_VARCHAR)
{
ArdRec->InternalBuffer= (char *)MADB_CALLOC(Stmt->stmt->fields[i].max_length + 1);
if (ArdRec->InternalBuffer == NULL)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
Stmt->result[i].buffer= ArdRec->InternalBuffer;
Stmt->result[i].buffer_type= MYSQL_TYPE_STRING;
Stmt->result[i].buffer_length= Stmt->stmt->fields[i].max_length + 1;
}
else
{
ArdRec->InternalBuffer= (char *)MADB_CALLOC(sizeof(MYSQL_TIME));
Stmt->result[i].buffer= ArdRec->InternalBuffer;
Stmt->result[i].buffer_length= sizeof(MYSQL_TIME);
Stmt->result[i].buffer_type= MYSQL_TYPE_TIMESTAMP;
}
break;
case SQL_C_INTERVAL_HOUR_TO_MINUTE:
case SQL_C_INTERVAL_HOUR_TO_SECOND:
{
MYSQL_FIELD *Field= mysql_fetch_field_direct(Stmt->metadata, i);
MADB_FREE(ArdRec->InternalBuffer);
if (IrdRec->ConciseType == SQL_CHAR || IrdRec->ConciseType == SQL_VARCHAR)
{
ArdRec->InternalBuffer= (char *)MADB_CALLOC(Stmt->stmt->fields[i].max_length + 1);
if (ArdRec->InternalBuffer == NULL)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
Stmt->result[i].buffer= ArdRec->InternalBuffer;
Stmt->result[i].buffer_type= MYSQL_TYPE_STRING;
Stmt->result[i].buffer_length= Stmt->stmt->fields[i].max_length + 1;
}
else
{
ArdRec->InternalBuffer= (char *)MADB_CALLOC(sizeof(MYSQL_TIME));
Stmt->result[i].buffer= ArdRec->InternalBuffer;
Stmt->result[i].buffer_length= sizeof(MYSQL_TIME);
Stmt->result[i].buffer_type= Field && Field->type == MYSQL_TYPE_TIME ? MYSQL_TYPE_TIME : MYSQL_TYPE_TIMESTAMP;
}
}
break;
case SQL_C_TINYINT:
case SQL_C_UTINYINT:
case SQL_C_STINYINT:
case SQL_C_SHORT:
case SQL_C_SSHORT:
case SQL_C_USHORT:
case SQL_C_FLOAT:
case SQL_C_LONG:
case SQL_C_ULONG:
case SQL_C_SLONG:
case SQL_C_DOUBLE:
if (MADB_BinaryFieldType(IrdRec->ConciseType))
{
/* To keep things simple - we will use internal buffer of the column size, and later(in the MADB_FixFetchedValues) will copy (correct part of)
it to the application's buffer taking care of endianness. Perhaps it'd be better just not to support this type of conversion */
MADB_FREE(ArdRec->InternalBuffer);
ArdRec->InternalBuffer= (char *)MADB_CALLOC(IrdRec->OctetLength);
Stmt->result[i].buffer= ArdRec->InternalBuffer;
Stmt->result[i].buffer_length= (unsigned long)IrdRec->OctetLength;
Stmt->result[i].buffer_type= MYSQL_TYPE_BLOB;
break;
}
/* else {we are falling through below} */
default:
if (!MADB_CheckODBCType(ArdRec->ConciseType))
{
return MADB_SetError(&Stmt->Error, MADB_ERR_07006, NULL, 0);
}
Stmt->result[i].buffer_length= (unsigned long)ArdRec->OctetLength;
Stmt->result[i].buffer= DataPtr;
Stmt->result[i].buffer_type= MADB_GetMaDBTypeAndLength(ArdRec->ConciseType,
&Stmt->result[i].is_unsigned,
&Stmt->result[i].buffer_length);
break;
}
}
return SQL_SUCCESS;
}
/* }}} */
/* {{{ LittleEndian */
char LittleEndian()
{
int x= 1;
char *c= (char*)&x;
return *c;
}
/* }}} */
/* {{{ SwitchEndianness */
void SwitchEndianness(char *Src, SQLLEN SrcBytes, char *Dst, SQLLEN DstBytes)
{
/* SrcBytes can only be less or equal DstBytes */
while (SrcBytes--)
{
*Dst++= *(Src + SrcBytes);
}
}
/* }}} */
#define CALC_ALL_FLDS_RC(_agg_rc, _field_rc) if (_field_rc != SQL_SUCCESS && _agg_rc != SQL_ERROR) _agg_rc= _field_rc
/* {{{ MADB_FixFetchedValues
Converting and/or fixing fetched values if needed */
SQLRETURN MADB_FixFetchedValues(MADB_Stmt *Stmt, int RowNumber, MYSQL_ROW_OFFSET SaveCursor)
{
MADB_DescRecord *IrdRec, *ArdRec;
int i;
SQLLEN *IndicatorPtr= NULL, *LengthPtr= NULL, Dummy= 0;
void *DataPtr= NULL;
SQLRETURN rc= SQL_SUCCESS, FieldRc;
for (i= 0; i < MADB_STMT_COLUMN_COUNT(Stmt); ++i)
{
if ((ArdRec= MADB_DescGetInternalRecord(Stmt->Ard, i, MADB_DESC_READ)) && ArdRec->inUse)
{
/* set indicator and dataptr */
LengthPtr= (SQLLEN *)GetBindOffset(Stmt->Ard, ArdRec, ArdRec->OctetLengthPtr, RowNumber, sizeof(SQLLEN));
IndicatorPtr= (SQLLEN *)GetBindOffset(Stmt->Ard, ArdRec, ArdRec->IndicatorPtr, RowNumber, sizeof(SQLLEN));
DataPtr= (SQLLEN *)GetBindOffset(Stmt->Ard, ArdRec, ArdRec->DataPtr, RowNumber, ArdRec->OctetLength);
if (LengthPtr == NULL)
{
LengthPtr= &Dummy;
}
/* clear IndicatorPtr */
if (IndicatorPtr != NULL && IndicatorPtr != LengthPtr && *IndicatorPtr < 0)
{
*IndicatorPtr= 0;
}
IrdRec= MADB_DescGetInternalRecord(Stmt->Ird, i, MADB_DESC_READ);
/* assert(IrdRec != NULL) */
if (*Stmt->stmt->bind[i].is_null)
{
if (IndicatorPtr)
{
*IndicatorPtr= SQL_NULL_DATA;
}
else
{
if (SaveCursor)
{
mysql_stmt_row_seek(Stmt->stmt, SaveCursor);
}
rc= MADB_SetError(&Stmt->Error, MADB_ERR_22002, NULL, 0);
continue;
}
}
else
{
switch (ArdRec->ConciseType)
{
case SQL_C_BIT:
{
char *p= (char *)Stmt->result[i].buffer;
if (p)
{
*p= test(*p != '\0');
}
}
break;
case SQL_C_TYPE_TIMESTAMP:
case SQL_C_TYPE_DATE:
case SQL_C_TYPE_TIME:
case SQL_C_TIMESTAMP:
case SQL_C_TIME:
case SQL_C_DATE:
{
MYSQL_TIME tm, *Intermidiate;
if (IrdRec->ConciseType == SQL_CHAR || IrdRec->ConciseType == SQL_VARCHAR)
{
BOOL isTime;
FieldRc= MADB_Str2Ts(ArdRec->InternalBuffer, *Stmt->stmt->bind[i].length, &tm, FALSE, &Stmt->Error, &isTime);
if (SQL_SUCCEEDED(FieldRc))
{
Intermidiate= &tm;
}
else
{
CALC_ALL_FLDS_RC(rc, FieldRc);
break;
}
}
else
{
Intermidiate= (MYSQL_TIME *)ArdRec->InternalBuffer;
}
FieldRc= MADB_CopyMadbTimestamp(Stmt, Intermidiate, DataPtr, LengthPtr, IndicatorPtr, ArdRec->Type, IrdRec->ConciseType);
CALC_ALL_FLDS_RC(rc, FieldRc);
}
break;
case SQL_C_INTERVAL_HOUR_TO_MINUTE:
case SQL_C_INTERVAL_HOUR_TO_SECOND:
{
MYSQL_TIME *tm= (MYSQL_TIME*)ArdRec->InternalBuffer, ForConversion;
SQL_INTERVAL_STRUCT *ts= (SQL_INTERVAL_STRUCT *)DataPtr;
if (IrdRec->ConciseType == SQL_CHAR || IrdRec->ConciseType == SQL_VARCHAR)
{
BOOL isTime;
FieldRc= MADB_Str2Ts(ArdRec->InternalBuffer, *Stmt->stmt->bind[i].length, &ForConversion, FALSE, &Stmt->Error, &isTime);
if (SQL_SUCCEEDED(FieldRc))
{
tm= &ForConversion;
}
else
{
CALC_ALL_FLDS_RC(rc, FieldRc);
break;
}
}
/* If we have ts == NULL we (may) have tm also NULL, since we didn't really bind this column */
if (ts != NULL)
{
if (tm->hour > 99999)
{
FieldRc= MADB_SetError(&Stmt->Error, MADB_ERR_22015, NULL, 0);
CALC_ALL_FLDS_RC(rc, FieldRc);
break;
}
ts->intval.day_second.hour= tm->hour;
ts->intval.day_second.minute= tm->minute;
ts->interval_sign= tm->neg ? SQL_TRUE : SQL_FALSE;
if (ArdRec->Type == SQL_C_INTERVAL_HOUR_TO_MINUTE)
{
ts->intval.day_second.second= 0;
ts->interval_type= SQL_INTERVAL_HOUR_TO_MINUTE;
if (tm->second)
{
FieldRc= MADB_SetError(&Stmt->Error, MADB_ERR_01S07, NULL, 0);
CALC_ALL_FLDS_RC(rc, FieldRc);
break;
}
}
else
{
ts->interval_type= SQL_INTERVAL_HOUR_TO_SECOND;
ts->intval.day_second.second= tm->second;
}
}
*LengthPtr= sizeof(SQL_INTERVAL_STRUCT);
}
break;
case SQL_C_NUMERIC:
{
int rc= 0;
MADB_CLEAR_ERROR(&Stmt->Error);
if (DataPtr != NULL && Stmt->result[i].buffer_length < Stmt->stmt->fields[i].max_length)
{
MADB_SetError(&Stmt->Error, MADB_ERR_22003, NULL, 0);
ArdRec->InternalBuffer[Stmt->result[i].buffer_length - 1]= 0;
return Stmt->Error.ReturnValue;
}
if ((rc= MADB_CharToSQLNumeric(ArdRec->InternalBuffer, Stmt->Ard, ArdRec, NULL, RowNumber)))
{
MADB_SetError(&Stmt->Error, rc, NULL, 0);
}
/* TODO: why is it here individually for Numeric type?! */
if (Stmt->Ard->Header.ArrayStatusPtr)
{
Stmt->Ard->Header.ArrayStatusPtr[RowNumber]= Stmt->Error.ReturnValue;
}
*LengthPtr= sizeof(SQL_NUMERIC_STRUCT);
}
break;
case SQL_C_WCHAR:
{
SQLLEN CharLen= MADB_SetString(&Stmt->Connection->Charset, DataPtr, ArdRec->OctetLength, (char *)Stmt->result[i].buffer,
*Stmt->stmt->bind[i].length, &Stmt->Error);
/* Not quite right */
*LengthPtr= CharLen * sizeof(SQLWCHAR);
}
break;
case SQL_C_TINYINT:
case SQL_C_UTINYINT:
case SQL_C_STINYINT:
case SQL_C_SHORT:
case SQL_C_SSHORT:
case SQL_C_USHORT:
case SQL_C_FLOAT:
case SQL_C_LONG:
case SQL_C_ULONG:
case SQL_C_SLONG:
case SQL_C_DOUBLE:
if (MADB_BinaryFieldType(IrdRec->ConciseType))
{
if (DataPtr != NULL)
{
if (Stmt->result[i].buffer_length >= (unsigned long)ArdRec->OctetLength)
{
if (LittleEndian())
{
/* We currently got the bigendian number. If we or littleendian machine, we need to switch bytes */
SwitchEndianness((char*)Stmt->result[i].buffer + Stmt->result[i].buffer_length - ArdRec->OctetLength,
ArdRec->OctetLength,
(char*)DataPtr,
ArdRec->OctetLength);
}
else
{
memcpy(DataPtr, (void*)((char*)Stmt->result[i].buffer + Stmt->result[i].buffer_length - ArdRec->OctetLength), ArdRec->OctetLength);
}
}
else
{
/* We won't write to the whole memory pointed by DataPtr, thus to need to zerofill prior to that */
memset(DataPtr, 0, ArdRec->OctetLength);
if (LittleEndian())
{
SwitchEndianness((char*)Stmt->result[i].buffer,
Stmt->result[i].buffer_length,
(char*)DataPtr,
ArdRec->OctetLength);
}
else
{
memcpy((void*)((char*)DataPtr + ArdRec->OctetLength - Stmt->result[i].buffer_length),
Stmt->result[i].buffer, Stmt->result[i].buffer_length);
}
}
*LengthPtr= *Stmt->stmt->bind[i].length;
}
break;
}
/* else {we are falling through below} */
default:
if (DataPtr != NULL)
{
if (Stmt->Ard->Header.ArraySize > 1)
{
if (Stmt->Ard->Header.BindType)
{
Stmt->result[i].buffer= (char *)Stmt->result[i].buffer + Stmt->Ard->Header.BindType;
}
else
{
Stmt->result[i].buffer = (char *)ArdRec->DataPtr + (RowNumber + 1) * ArdRec->OctetLength;
}
}
*LengthPtr= *Stmt->stmt->bind[i].length;
}
break;
}
}
}
}
return rc;
}
/* }}} */
#undef CALC_ALL_FLDS_RC
SQLUSMALLINT MADB_MapToRowStatus(SQLRETURN rc)
{
switch (rc)
{
case SQL_SUCCESS_WITH_INFO: return SQL_ROW_SUCCESS_WITH_INFO;
case SQL_ERROR: return SQL_ROW_ERROR;
/* Assuming is that status array pre-filled with SQL_ROW_NOROW,
and it never needs to be mapped to */
}
return SQL_ROW_SUCCESS;
}
void ResetDescIntBuffers(MADB_Desc *Desc)
{
MADB_DescRecord *Rec;
SQLSMALLINT i;
for (i= 0; i < Desc->Header.Count; ++i)
{
Rec= MADB_DescGetInternalRecord(Desc, i, MADB_DESC_READ);
if (Rec)
{
MADB_FREE(Rec->InternalBuffer);
}
}
}
/* For first row we just take its result as initial.
For the rest, if all rows SQL_SUCCESS or SQL_ERROR - aggregated result is SQL_SUCCESS or SQL_ERROR, respectively
Otherwise - SQL_SUCCESS_WITH_INFO */
#define CALC_ALL_ROWS_RC(_accumulated_rc, _cur_row_rc, _row_num)\
if (_row_num == 0) _accumulated_rc= _cur_row_rc;\
else if (_cur_row_rc != _accumulated_rc) _accumulated_rc= SQL_SUCCESS_WITH_INFO
/* {{{ MADB_StmtFetch */
SQLRETURN MADB_StmtFetch(MADB_Stmt *Stmt)
{
unsigned int RowNum, j, rc;
SQLULEN Rows2Fetch= Stmt->Ard->Header.ArraySize, Processed, *ProcessedPtr= &Processed;
MYSQL_ROW_OFFSET SaveCursor= NULL;
SQLRETURN Result= SQL_SUCCESS, RowResult;
MADB_CLEAR_ERROR(&Stmt->Error);
if (!(MADB_STMT_COLUMN_COUNT(Stmt) > 0))
{
return MADB_SetError(&Stmt->Error, MADB_ERR_24000, NULL, 0);
}
if ((Stmt->Options.UseBookmarks == SQL_UB_VARIABLE && Stmt->Options.BookmarkType == SQL_C_BOOKMARK) ||
(Stmt->Options.UseBookmarks != SQL_UB_VARIABLE && Stmt->Options.BookmarkType == SQL_C_VARBOOKMARK))
{
MADB_SetError(&Stmt->Error, MADB_ERR_07006, NULL, 0);
return Stmt->Error.ReturnValue;
}
/* We don't have much to do if ArraySize == 0 */
if (Stmt->Ard->Header.ArraySize == 0)
{
return SQL_SUCCESS;
}
Stmt->LastRowFetched= 0;
if (Stmt->result == NULL && !(Stmt->result= (MYSQL_BIND *)MADB_CALLOC(sizeof(MYSQL_BIND) * mysql_stmt_field_count(Stmt->stmt))))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
return Stmt->Error.ReturnValue;
}
Rows2Fetch= MADB_RowsToFetch(&Stmt->Cursor, Stmt->Ard->Header.ArraySize, mysql_stmt_num_rows(Stmt->stmt));
if (Rows2Fetch == 0)
{
return SQL_NO_DATA;
}
if (Stmt->Ard->Header.ArrayStatusPtr)
{
MADB_InitStatusPtr(Stmt->Ard->Header.ArrayStatusPtr, Stmt->Ard->Header.ArraySize, SQL_NO_DATA);
}
if (Stmt->Ird->Header.RowsProcessedPtr)
{
ProcessedPtr= Stmt->Ird->Header.RowsProcessedPtr;
}
if (Stmt->Ird->Header.ArrayStatusPtr)
{
MADB_InitStatusPtr(Stmt->Ird->Header.ArrayStatusPtr, Stmt->Ard->Header.ArraySize, SQL_ROW_NOROW);
}
*ProcessedPtr= 0;
/* We need to return to 1st row in the rowset only if there are >1 rows in it. Otherwise we stay on it anyway */
if (Rows2Fetch > 1 && Stmt->Options.CursorType != SQL_CURSOR_FORWARD_ONLY)
{
SaveCursor= mysql_stmt_row_tell(Stmt->stmt);
/* Skipping current row for for reading now, it will be read when the Cursor is returned to it */
MoveNext(Stmt, 1LL);
}
for (j= 0; j < Rows2Fetch; ++j)
{
RowResult= SQL_SUCCESS;
/* If we need to return the cursor to 1st row in the rowset, we start to read it from 2nd, and 1st row we read the last */
if (SaveCursor != NULL)
{
RowNum= j + 1;
if (RowNum == Rows2Fetch)
{
RowNum= 0;
Stmt->Cursor.Next= mysql_stmt_row_tell(Stmt->stmt);
mysql_stmt_row_seek(Stmt->stmt, SaveCursor);
}
}
else
{
RowNum= j;
}
/*************** Setting up BIND structures ********************/
/* Basically, nothing should happen here, but if happens, then it will happen on each row.
Thus it's ok to stop */
RETURN_ERROR_OR_CONTINUE(MADB_PrepareBind(Stmt, RowNum));
/************************ Bind! ********************************/
mysql_stmt_bind_result(Stmt->stmt, Stmt->result);
if (Stmt->Options.UseBookmarks && Stmt->Options.BookmarkPtr != NULL)
{
/* TODO: Bookmark can be not only "unsigned long*", but also "unsigned char*". Can be determined by examining Stmt->Options.BookmarkType */
long *p= (long *)Stmt->Options.BookmarkPtr;
p+= RowNum * Stmt->Options.BookmarkLength;
*p= (long)Stmt->Cursor.Position;
}
/************************ Fetch! ********************************/
rc= mysql_stmt_fetch(Stmt->stmt);
*ProcessedPtr += 1;
if (Stmt->Cursor.Position < 0)
{
Stmt->Cursor.Position= 0;
}
switch(rc) {
case 1:
RowResult= MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
/* If mysql_stmt_fetch returned error, there is no sense to continue */
if (Stmt->Ird->Header.ArrayStatusPtr)
{
Stmt->Ird->Header.ArrayStatusPtr[RowNum]= MADB_MapToRowStatus(RowResult);
}
CALC_ALL_ROWS_RC(Result, RowResult, RowNum);
return Result;
case MYSQL_DATA_TRUNCATED:
{
/* We will not report truncation if a dummy buffer was bound */
int col;
for (col= 0; col < MADB_STMT_COLUMN_COUNT(Stmt); ++col)
{
if (Stmt->stmt->bind[col].error && *Stmt->stmt->bind[col].error > 0 &&
!(Stmt->stmt->bind[col].flags & MADB_BIND_DUMMY))
{
MADB_DescRecord *ArdRec= MADB_DescGetInternalRecord(Stmt->Ard, col, MADB_DESC_READ),
*IrdRec= MADB_DescGetInternalRecord(Stmt->Ird, col, MADB_DESC_READ);
/* If (numeric) field value and buffer are of the same size - ignoring truncation.
In some cases specs are not clear enough if certain column signed or not(think of catalog functions for example), and
some apps bind signed buffer where we return unsigdned value. And in general - if application want to fetch unsigned as
signed, or vice versa, why we should prevent that. */
if (ArdRec->OctetLength == IrdRec->OctetLength
&& MADB_IsIntType(IrdRec->ConciseType) && MADB_IsIntType(ArdRec->ConciseType))
{
continue;
}
/* For numeric types we return either 22003 or 01S07, 01004 for the rest.
if ird type is not fractional - we return 22003. But as a matter of fact, it's possible that we have 22003 if converting
from fractional types */
RowResult= MADB_SetError(&Stmt->Error, ArdRec != NULL && MADB_IsNumericType(ArdRec->ConciseType) ?
(MADB_IsIntType(IrdRec->ConciseType) ? MADB_ERR_22003 : MADB_ERR_01S07) : MADB_ERR_01004, NULL, 0);
/* One found such column is enough */
break;
}
}
break;
}
case MYSQL_NO_DATA:
/* We have already incremented this counter, since there was no more rows, need to decrement */
--*ProcessedPtr;
/* SQL_NO_DATA should be only returned if first fetched row is already beyond end of the resultset */
if (RowNum > 0)
{
continue;
}
return SQL_NO_DATA;
} /* End of switch on fetch result */
++Stmt->LastRowFetched;
++Stmt->PositionedCursor;
/*Conversion etc. At this point, after fetch we can have RowResult either SQL_SUCCESS or SQL_SUCCESS_WITH_INFO */
switch (MADB_FixFetchedValues(Stmt, RowNum, SaveCursor))
{
case SQL_ERROR:
RowResult= SQL_ERROR;
break;
case SQL_SUCCESS_WITH_INFO:
RowResult= SQL_SUCCESS_WITH_INFO;
/* And if result of conversions - success, just leaving that we had before */
}
CALC_ALL_ROWS_RC(Result, RowResult, RowNum);
if (Stmt->Ird->Header.ArrayStatusPtr)
{
Stmt->Ird->Header.ArrayStatusPtr[RowNum]= MADB_MapToRowStatus(RowResult);
}
}
memset(Stmt->CharOffset, 0, sizeof(long) * mysql_stmt_field_count(Stmt->stmt));
memset(Stmt->Lengths, 0, sizeof(long) * mysql_stmt_field_count(Stmt->stmt));
ResetDescIntBuffers(Stmt->Ird);
return Result;
}
/* }}} */
#undef CALC_ALL_ROWS_RC
/* {{{ MADB_StmtGetAttr */
SQLRETURN MADB_StmtGetAttr(MADB_Stmt *Stmt, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER BufferLength,
SQLINTEGER *StringLengthPtr)
{
SQLINTEGER StringLength;
SQLRETURN ret= SQL_SUCCESS;
if (!StringLengthPtr)
StringLengthPtr= &StringLength;
if (!Stmt)
return SQL_INVALID_HANDLE;
switch(Attribute) {
case SQL_ATTR_APP_PARAM_DESC:
*(SQLPOINTER *)ValuePtr= Stmt->Apd;
*StringLengthPtr= sizeof(SQLPOINTER *);
break;
case SQL_ATTR_APP_ROW_DESC:
*(SQLPOINTER *)ValuePtr= Stmt->Ard;
*StringLengthPtr= sizeof(SQLPOINTER *);
break;
case SQL_ATTR_IMP_PARAM_DESC:
*(SQLPOINTER *)ValuePtr= Stmt->Ipd;
*StringLengthPtr= sizeof(SQLPOINTER *);
break;
case SQL_ATTR_IMP_ROW_DESC:
*(SQLPOINTER *)ValuePtr= Stmt->Ird;
*StringLengthPtr= sizeof(SQLPOINTER *);
break;
case SQL_ATTR_PARAM_BIND_OFFSET_PTR:
*(SQLPOINTER *)ValuePtr= Stmt->Apd->Header.BindOffsetPtr;
break;
case SQL_ATTR_PARAM_BIND_TYPE:
*(SQLULEN *)ValuePtr= Stmt->Apd->Header.BindType;
break;
case SQL_ATTR_PARAM_OPERATION_PTR:
*(SQLPOINTER *)ValuePtr= (SQLPOINTER)Stmt->Apd->Header.ArrayStatusPtr;
break;
case SQL_ATTR_PARAM_STATUS_PTR:
*(SQLPOINTER *)ValuePtr= (SQLPOINTER)Stmt->Ipd->Header.ArrayStatusPtr;
break;
case SQL_ATTR_PARAMS_PROCESSED_PTR:
*(SQLPOINTER *)ValuePtr= (SQLPOINTER)(SQLULEN)Stmt->Ipd->Header.BindType;
break;
case SQL_ATTR_PARAMSET_SIZE:
*(SQLULEN *)ValuePtr= Stmt->Apd->Header.ArraySize;
break;
case SQL_ATTR_ASYNC_ENABLE:
*(SQLPOINTER *)ValuePtr= SQL_ASYNC_ENABLE_OFF;
break;
case SQL_ATTR_ROW_ARRAY_SIZE:
case SQL_ROWSET_SIZE:
*(SQLULEN *)ValuePtr= Stmt->Ard->Header.ArraySize;
break;
case SQL_ATTR_ROW_BIND_OFFSET_PTR:
*(SQLPOINTER *)ValuePtr= (SQLPOINTER)Stmt->Ard->Header.BindOffsetPtr;
break;
case SQL_ATTR_ROW_BIND_TYPE:
*(SQLULEN *)ValuePtr= Stmt->Ard->Header.BindType;
break;
case SQL_ATTR_ROW_OPERATION_PTR:
*(SQLPOINTER *)ValuePtr= (SQLPOINTER)Stmt->Ard->Header.ArrayStatusPtr;
break;
case SQL_ATTR_ROW_STATUS_PTR:
*(SQLPOINTER *)ValuePtr= (SQLPOINTER)Stmt->Ird->Header.ArrayStatusPtr;
break;
case SQL_ATTR_ROWS_FETCHED_PTR:
*(SQLULEN **)ValuePtr= Stmt->Ird->Header.RowsProcessedPtr;
break;
case SQL_ATTR_USE_BOOKMARKS:
*(SQLUINTEGER *)ValuePtr= Stmt->Options.UseBookmarks;
break;
case SQL_ATTR_SIMULATE_CURSOR:
*(SQLULEN *)ValuePtr= Stmt->Options.SimulateCursor;
break;
case SQL_ATTR_CURSOR_SCROLLABLE:
*(SQLULEN *)ValuePtr= Stmt->Options.CursorType;
break;
case SQL_ATTR_CURSOR_SENSITIVITY:
*(SQLULEN *)ValuePtr= SQL_UNSPECIFIED;
break;
case SQL_ATTR_CURSOR_TYPE:
*(SQLULEN *)ValuePtr= Stmt->Options.CursorType;
break;
case SQL_ATTR_CONCURRENCY:
*(SQLULEN *)ValuePtr= SQL_CONCUR_READ_ONLY;
break;
case SQL_ATTR_ENABLE_AUTO_IPD:
*(SQLULEN *)ValuePtr= SQL_FALSE;
break;
case SQL_ATTR_MAX_LENGTH:
*(SQLULEN *)ValuePtr= Stmt->Options.MaxLength;
break;
case SQL_ATTR_MAX_ROWS:
*(SQLULEN *)ValuePtr= Stmt->Options.MaxRows;
break;
case SQL_ATTR_METADATA_ID:
*(SQLULEN *)ValuePtr= Stmt->Options.MetadataId;
break;
case SQL_ATTR_NOSCAN:
*(SQLULEN *)ValuePtr= SQL_NOSCAN_ON;
break;
case SQL_ATTR_QUERY_TIMEOUT:
*(SQLULEN *)ValuePtr= 0;
break;
case SQL_ATTR_RETRIEVE_DATA:
*(SQLULEN *)ValuePtr= SQL_RD_ON;
break;
}
return ret;
}
/* }}} */
/* {{{ MADB_StmtSetAttr */
SQLRETURN MADB_StmtSetAttr(MADB_Stmt *Stmt, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength)
{
SQLRETURN ret= SQL_SUCCESS;
if (!Stmt)
return SQL_INVALID_HANDLE;
switch(Attribute) {
case SQL_ATTR_APP_PARAM_DESC:
if (ValuePtr)
{
MADB_Desc *Desc= (MADB_Desc *)ValuePtr;
if (!Desc->AppType && Desc != Stmt->IApd)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY017, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (Desc->DescType != MADB_DESC_APD && Desc->DescType != MADB_DESC_UNKNOWN)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY024, NULL, 0);
return Stmt->Error.ReturnValue;
}
RemoveStmtRefFromDesc(Stmt->Apd, Stmt, FALSE);
Stmt->Apd= (MADB_Desc *)ValuePtr;
Stmt->Apd->DescType= MADB_DESC_APD;
if (Stmt->Apd != Stmt->IApd)
{
MADB_Stmt **IntStmt;
IntStmt = (MADB_Stmt **)MADB_AllocDynamic(&Stmt->Apd->Stmts);
*IntStmt= Stmt;
}
}
else
{
RemoveStmtRefFromDesc(Stmt->Apd, Stmt, FALSE);
Stmt->Apd= Stmt->IApd;
}
break;
case SQL_ATTR_APP_ROW_DESC:
if (ValuePtr)
{
MADB_Desc *Desc= (MADB_Desc *)ValuePtr;
if (!Desc->AppType && Desc != Stmt->IArd)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY017, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (Desc->DescType != MADB_DESC_ARD && Desc->DescType != MADB_DESC_UNKNOWN)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY024, NULL, 0);
return Stmt->Error.ReturnValue;
}
RemoveStmtRefFromDesc(Stmt->Ard, Stmt, FALSE);
Stmt->Ard= Desc;
Stmt->Ard->DescType= MADB_DESC_ARD;
if (Stmt->Ard != Stmt->IArd)
{
MADB_Stmt **IntStmt;
IntStmt = (MADB_Stmt **)MADB_AllocDynamic(&Stmt->Ard->Stmts);
*IntStmt= Stmt;
}
}
else
{
RemoveStmtRefFromDesc(Stmt->Ard, Stmt, FALSE);
Stmt->Ard= Stmt->IArd;
}
break;
case SQL_ATTR_PARAM_BIND_OFFSET_PTR:
Stmt->Apd->Header.BindOffsetPtr= (SQLULEN*)ValuePtr;
break;
case SQL_ATTR_PARAM_BIND_TYPE:
Stmt->Apd->Header.BindType= (SQLINTEGER)(SQLLEN)ValuePtr;
break;
case SQL_ATTR_PARAM_OPERATION_PTR:
Stmt->Apd->Header.ArrayStatusPtr= (SQLUSMALLINT *)ValuePtr;
break;
case SQL_ATTR_PARAM_STATUS_PTR:
Stmt->Ipd->Header.ArrayStatusPtr= (SQLUSMALLINT *)ValuePtr;
break;
case SQL_ATTR_PARAMS_PROCESSED_PTR:
Stmt->Ipd->Header.RowsProcessedPtr = (SQLULEN *)ValuePtr;
break;
case SQL_ATTR_PARAMSET_SIZE:
Stmt->Apd->Header.ArraySize= (SQLULEN)ValuePtr;
break;
case SQL_ATTR_ROW_ARRAY_SIZE:
case SQL_ROWSET_SIZE:
Stmt->Ard->Header.ArraySize= (SQLULEN)ValuePtr;
break;
case SQL_ATTR_ROW_BIND_OFFSET_PTR:
Stmt->Ard->Header.BindOffsetPtr= (SQLULEN*)ValuePtr;
break;
case SQL_ATTR_ROW_BIND_TYPE:
Stmt->Ard->Header.BindType= (SQLINTEGER)(SQLLEN)ValuePtr;
break;
case SQL_ATTR_ROW_OPERATION_PTR:
Stmt->Ard->Header.ArrayStatusPtr= (SQLUSMALLINT *)ValuePtr;
break;
case SQL_ATTR_ROW_STATUS_PTR:
Stmt->Ird->Header.ArrayStatusPtr= (SQLUSMALLINT *)ValuePtr;
break;
case SQL_ATTR_ROWS_FETCHED_PTR:
Stmt->Ird->Header.RowsProcessedPtr= (SQLULEN*)ValuePtr;
break;
case SQL_ATTR_ASYNC_ENABLE:
if ((SQLULEN)ValuePtr != SQL_ASYNC_ENABLE_OFF)
{
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default (SQL_ATTR_ASYNC_ENABLE)", 0);
ret= SQL_SUCCESS_WITH_INFO;
}
break;
case SQL_ATTR_SIMULATE_CURSOR:
Stmt->Options.SimulateCursor= (SQLULEN) ValuePtr;
break;
case SQL_ATTR_CURSOR_SCROLLABLE:
Stmt->Options.CursorType= ((SQLULEN)ValuePtr == SQL_NONSCROLLABLE) ?
SQL_CURSOR_FORWARD_ONLY : SQL_CURSOR_STATIC;
break;
case SQL_ATTR_CURSOR_SENSITIVITY:
/* we only support default value = SQL_UNSPECIFIED */
if ((SQLULEN)ValuePtr != SQL_UNSPECIFIED)
{
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default cursor sensitivity", 0);
ret= SQL_SUCCESS_WITH_INFO;
}
break;
case SQL_ATTR_CURSOR_TYPE:
/* We need to check global DSN/Connection settings */
if (MA_ODBC_CURSOR_FORWARD_ONLY(Stmt->Connection) && (SQLULEN)ValuePtr != SQL_CURSOR_FORWARD_ONLY)
{
Stmt->Options.CursorType= SQL_CURSOR_FORWARD_ONLY;
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default (SQL_CURSOR_FORWARD_ONLY)", 0);
return Stmt->Error.ReturnValue;
}
else if (MA_ODBC_CURSOR_DYNAMIC(Stmt->Connection))
{
if ((SQLULEN)ValuePtr == SQL_CURSOR_KEYSET_DRIVEN)
{
Stmt->Options.CursorType= SQL_CURSOR_STATIC;
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default (SQL_CURSOR_STATIC)", 0);
return Stmt->Error.ReturnValue;
}
Stmt->Options.CursorType= (SQLUINTEGER)(SQLULEN)ValuePtr;
}
/* only FORWARD or Static is allowed */
else
{
if ((SQLULEN)ValuePtr != SQL_CURSOR_FORWARD_ONLY &&
(SQLULEN)ValuePtr != SQL_CURSOR_STATIC)
{
Stmt->Options.CursorType= SQL_CURSOR_STATIC;
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default (SQL_CURSOR_STATIC)", 0);
return Stmt->Error.ReturnValue;
}
Stmt->Options.CursorType= (SQLUINTEGER)(SQLULEN)ValuePtr;
}
break;
case SQL_ATTR_CONCURRENCY:
if ((SQLULEN)ValuePtr != SQL_CONCUR_READ_ONLY)
{
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default (SQL_CONCUR_READ_ONLY). ", 0);
ret= SQL_SUCCESS_WITH_INFO;
}
break;
case SQL_ATTR_ENABLE_AUTO_IPD:
/* MariaDB doesn't deliver param metadata after prepare, so we can't autopopulate ird */
MADB_SetError(&Stmt->Error, MADB_ERR_HYC00, NULL, 0);
return Stmt->Error.ReturnValue;
break;
case SQL_ATTR_MAX_LENGTH:
Stmt->Options.MaxLength= (SQLULEN)ValuePtr;
break;
case SQL_ATTR_MAX_ROWS:
Stmt->Options.MaxRows= (SQLULEN)ValuePtr;
break;
case SQL_ATTR_METADATA_ID:
Stmt->Options.MetadataId= (SQLULEN)ValuePtr;
break;
case SQL_ATTR_NOSCAN:
if ((SQLULEN)ValuePtr != SQL_NOSCAN_ON)
{
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default (SQL_NOSCAN_ON)", 0);
ret= SQL_SUCCESS_WITH_INFO;
}
break;
case SQL_ATTR_QUERY_TIMEOUT:
if ((SQLULEN)ValuePtr != 0)
{
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default (no timeout)", 0);
ret= SQL_SUCCESS_WITH_INFO;
}
break;
case SQL_ATTR_RETRIEVE_DATA:
if ((SQLULEN)ValuePtr != SQL_RD_ON)
{
MADB_SetError(&Stmt->Error, MADB_ERR_01S02, "Option value changed to default (SQL_RD_ON)", 0);
ret= SQL_SUCCESS_WITH_INFO;
}
break;
case SQL_ATTR_USE_BOOKMARKS:
Stmt->Options.UseBookmarks= (SQLUINTEGER)(SQLULEN)ValuePtr;
break;
case SQL_ATTR_FETCH_BOOKMARK_PTR:
MADB_SetError(&Stmt->Error, MADB_ERR_HYC00, NULL, 0);
return Stmt->Error.ReturnValue;
break;
default:
MADB_SetError(&Stmt->Error, MADB_ERR_HY024, NULL, 0);
return Stmt->Error.ReturnValue;
break;
}
return ret;
}
/* }}} */
SQLRETURN MADB_GetBookmark(MADB_Stmt *Stmt,
SQLSMALLINT TargetType,
SQLPOINTER TargetValuePtr,
SQLLEN BufferLength,
SQLLEN *StrLen_or_IndPtr)
{
if (Stmt->Options.UseBookmarks == SQL_UB_OFF)
{
MADB_SetError(&Stmt->Error, MADB_ERR_07009, NULL, 0);
return Stmt->Error.ReturnValue;
}
if ((Stmt->Options.UseBookmarks == SQL_UB_VARIABLE && TargetType != SQL_C_VARBOOKMARK) ||
(Stmt->Options.UseBookmarks != SQL_UB_VARIABLE && TargetType == SQL_C_VARBOOKMARK))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY003, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (TargetValuePtr && TargetType == SQL_C_BOOKMARK && BufferLength <= sizeof(SQLULEN))
{
*(SQLULEN *)TargetValuePtr= Stmt->Cursor.Position;
if (StrLen_or_IndPtr)
{
*StrLen_or_IndPtr= sizeof(SQLULEN);
}
return SQL_SUCCESS;
}
/* Keeping compiler happy */
return SQL_SUCCESS;
}
/* {{{ MADB_StmtGetData */
SQLRETURN MADB_StmtGetData(SQLHSTMT StatementHandle,
SQLUSMALLINT Col_or_Param_Num,
SQLSMALLINT TargetType,
SQLPOINTER TargetValuePtr,
SQLLEN BufferLength,
SQLLEN * StrLen_or_IndPtr,
BOOL InternalUse /* Currently this is respected for SQL_CHAR type only,
since all "internal" calls of the function need string representation of datat */)
{
MADB_Stmt *Stmt= (MADB_Stmt *)StatementHandle;
SQLUSMALLINT Offset= Col_or_Param_Num - 1;
SQLSMALLINT OdbcType= 0, MadbType= 0;
MYSQL_BIND Bind;
my_bool IsNull= FALSE;
my_bool ZeroTerminated= 0;
unsigned long CurrentOffset= InternalUse == TRUE ? 0 : Stmt->CharOffset[Offset]; /* We are supposed not get bookmark column here */
my_bool Error;
MADB_DescRecord *IrdRec= NULL;
MYSQL_FIELD *Field= mysql_fetch_field_direct(Stmt->metadata, Offset);
MADB_CLEAR_ERROR(&Stmt->Error);
/* Should not really happen, and is evidence of that something wrong happened in some previous call(SQLFetch?) */
if (Stmt->stmt->bind == NULL)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY109, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (Stmt->stmt->bind[Offset].is_null != NULL && *Stmt->stmt->bind[Offset].is_null != '\0')
{
if (!StrLen_or_IndPtr)
{
MADB_SetError(&Stmt->Error, MADB_ERR_22002, NULL, 0);
return Stmt->Error.ReturnValue;
}
*StrLen_or_IndPtr= SQL_NULL_DATA;
return SQL_SUCCESS;
}
memset(&Bind, 0, sizeof(MYSQL_BIND));
/* We might need it for SQL_C_DEFAULT type, or to obtain length of fixed length types(Access likes to have it) */
IrdRec= MADB_DescGetInternalRecord(Stmt->Ird, Offset, MADB_DESC_READ);
if (!IrdRec)
{
MADB_SetError(&Stmt->Error, MADB_ERR_07009, NULL, 0);
return Stmt->Error.ReturnValue;
}
switch (TargetType) {
case SQL_ARD_TYPE:
{
MADB_DescRecord *Ard= MADB_DescGetInternalRecord(Stmt->Ard, Offset, MADB_DESC_READ);
if (!Ard)
{
MADB_SetError(&Stmt->Error, MADB_ERR_07009, NULL, 0);
return Stmt->Error.ReturnValue;
}
OdbcType= Ard->ConciseType;
}
break;
case SQL_C_DEFAULT:
{
/* Taking type from IRD record. This way, if mysql type was fixed(currently that is mainly for catalog functions, we don't lose it.
(Access uses default types on getting catalog functions results, and not quite happy when it gets something unexpected. Seemingly it cares about returned data lenghts even for types,
for which standard says application should not care about */
OdbcType= IrdRec->ConciseType;
}
break;
default:
OdbcType= TargetType;
break;
}
/* Restoring mariadb/mysql type from odbc type */
MadbType= MADB_GetMaDBTypeAndLength(OdbcType, &Bind.is_unsigned, &Bind.buffer_length);
/* set global values for Bind */
Bind.error= &Error;
Bind.length= &Bind.length_value;
Bind.is_null= &IsNull;
switch(OdbcType)
{
case SQL_DATE:
case SQL_C_TYPE_DATE:
case SQL_TIMESTAMP:
case SQL_C_TYPE_TIMESTAMP:
case SQL_TIME:
case SQL_C_TYPE_TIME:
{
MYSQL_TIME tm;
if (IrdRec->ConciseType == SQL_CHAR || IrdRec->ConciseType == SQL_VARCHAR)
{
char *ClientValue= NULL;
BOOL isTime;
if (!(ClientValue = (char *)MADB_CALLOC(Stmt->stmt->fields[Offset].max_length + 1)))
{
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
Bind.buffer= ClientValue;
Bind.buffer_type= MYSQL_TYPE_STRING;
Bind.buffer_length= Stmt->stmt->fields[Offset].max_length + 1;
mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, 0);
RETURN_ERROR_OR_CONTINUE(MADB_Str2Ts(ClientValue, Bind.length_value, &tm, FALSE, &Stmt->Error, &isTime));
}
else
{
Bind.buffer_length= sizeof(MYSQL_TIME);
Bind.buffer= (void *)&tm;
/* c/c is too smart to convert hours to days and days to hours, we don't need that */
if ((OdbcType == SQL_C_TIME || OdbcType == SQL_C_TYPE_TIME)
&& (IrdRec->ConciseType == SQL_TIME || IrdRec->ConciseType == SQL_TYPE_TIME))
{
Bind.buffer_type= MYSQL_TYPE_TIME;
}
else
{
Bind.buffer_type= MYSQL_TYPE_TIMESTAMP;
}
mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, 0);
}
RETURN_ERROR_OR_CONTINUE(MADB_CopyMadbTimestamp(Stmt, &tm, TargetValuePtr, StrLen_or_IndPtr, StrLen_or_IndPtr, OdbcType, IrdRec->ConciseType));
break;
}
case SQL_C_INTERVAL_HOUR_TO_MINUTE:
case SQL_C_INTERVAL_HOUR_TO_SECOND:
{
MYSQL_TIME tm;
SQL_INTERVAL_STRUCT *ts= (SQL_INTERVAL_STRUCT *)TargetValuePtr;
if (IrdRec->ConciseType == SQL_CHAR || IrdRec->ConciseType == SQL_VARCHAR)
{
char *ClientValue= NULL;
BOOL isTime;
if (!(ClientValue = (char *)MADB_CALLOC(Stmt->stmt->fields[Offset].max_length + 1)))
{
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
Bind.buffer= ClientValue;
Bind.buffer_type= MYSQL_TYPE_STRING;
Bind.buffer_length= Stmt->stmt->fields[Offset].max_length + 1;
mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, 0);
RETURN_ERROR_OR_CONTINUE(MADB_Str2Ts(ClientValue, Bind.length_value, &tm, TRUE, &Stmt->Error, &isTime));
}
else
{
Bind.buffer_length= sizeof(MYSQL_TIME);
Bind.buffer= (void *)&tm;
/* c/c is too smart to convert hours to days and days to hours, we don't need that */
Bind.buffer_type= Field && Field->type == MYSQL_TYPE_TIME ? MYSQL_TYPE_TIME : MYSQL_TYPE_TIMESTAMP;
mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, 0);
}
if (tm.hour > 99999)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_22015, NULL, 0);
}
ts->intval.day_second.hour= tm.hour;
ts->intval.day_second.minute= tm.minute;
ts->interval_sign= tm.neg ? SQL_TRUE : SQL_FALSE;
if (TargetType == SQL_C_INTERVAL_HOUR_TO_MINUTE)
{
ts->intval.day_second.second= 0;
ts->interval_type= SQL_INTERVAL_HOUR_TO_MINUTE;
if (tm.second)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_01S07, NULL, 0);
}
}
else
{
ts->interval_type= SQL_INTERVAL_HOUR_TO_SECOND;
ts->intval.day_second.second= tm.second;
}
if (StrLen_or_IndPtr)
{
*StrLen_or_IndPtr= sizeof(SQL_INTERVAL_STRUCT);
}
}
break;
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
{
char *ClientValue= NULL;
size_t CharLength= 0;
/* Kinda this it not 1st call for this value, and we have it nice and recoded */
if (IrdRec->InternalBuffer == NULL/* && Stmt->Lengths[Offset] == 0*/)
{
if (!(ClientValue = (char *)MADB_CALLOC(Stmt->stmt->fields[Offset].max_length + 1)))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
return Stmt->Error.ReturnValue;
}
Bind.buffer= ClientValue;
Bind.buffer_type= MYSQL_TYPE_STRING;
Bind.buffer_length= Stmt->stmt->fields[Offset].max_length + 1;
if (mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, Stmt->CharOffset[Offset]))
{
MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
return Stmt->Error.ReturnValue;
}
/* check total length: if not enough space, we need to calculate new CharOffset for next fetch */
if (Stmt->stmt->fields[Offset].max_length)
{
size_t ReqBuffOctetLen;
/* Size in chars */
CharLength= MbstrCharLen(ClientValue, Stmt->stmt->fields[Offset].max_length - Stmt->CharOffset[Offset],
Stmt->Connection->Charset.cs_info);
/* MbstrCharLen gave us length in characters. For encoding of each character we might need
2 SQLWCHARs in case of UTF16, or 1 SQLWCHAR in case of UTF32. Probably we need calcualate better
number of required SQLWCHARs */
ReqBuffOctetLen= (CharLength + 1)*(4/sizeof(SQLWCHAR))*sizeof(SQLWCHAR);
if (BufferLength)
{
/* Buffer is not big enough. Alocating InternalBuffer.
MADB_SetString would do that anyway if - allocate buffer fitting the whole wide string,
and then copied its part to the application's buffer */
if (ReqBuffOctetLen > (size_t)BufferLength)
{
IrdRec->InternalBuffer= (char*)MADB_CALLOC(ReqBuffOctetLen);
if (IrdRec->InternalBuffer == 0)
{
MADB_FREE(ClientValue);
return MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
}
CharLength= MADB_SetString(&Stmt->Connection->Charset, IrdRec->InternalBuffer, (SQLINTEGER)ReqBuffOctetLen / sizeof(SQLWCHAR),
ClientValue, Stmt->stmt->fields[Offset].max_length - Stmt->CharOffset[Offset], &Stmt->Error);
}
else
{
/* Application's buffer is big enough - writing directly there */
CharLength= MADB_SetString(&Stmt->Connection->Charset, TargetValuePtr, (SQLINTEGER)(BufferLength / sizeof(SQLWCHAR)),
ClientValue, Stmt->stmt->fields[Offset].max_length - Stmt->CharOffset[Offset], &Stmt->Error);
}
if (!SQL_SUCCEEDED(Stmt->Error.ReturnValue))
{
MADB_FREE(ClientValue);
MADB_FREE(IrdRec->InternalBuffer);
return Stmt->Error.ReturnValue;
}
}
if (!Stmt->CharOffset[Offset])
{
Stmt->Lengths[Offset]= (unsigned long)(CharLength*sizeof(SQLWCHAR));
}
}
else if (BufferLength >= sizeof(SQLWCHAR))
{
*(SQLWCHAR*)TargetValuePtr= 0;
}
}
else /* IrdRec->InternalBuffer == NULL && Stmt->Lengths[Offset] == 0 */
{
CharLength= SqlwcsLen((SQLWCHAR*)((char*)IrdRec->InternalBuffer + Stmt->CharOffset[Offset]), -1);
}
if (StrLen_or_IndPtr)
{
*StrLen_or_IndPtr= CharLength * sizeof(SQLWCHAR);
}
if (!BufferLength)
{
MADB_FREE(ClientValue);
return MADB_SetError(&Stmt->Error, MADB_ERR_01004, NULL, 0);
}
if (IrdRec->InternalBuffer)
{
/* If we have more place than only for the TN */
if (BufferLength > sizeof(SQLWCHAR))
{
memcpy(TargetValuePtr, (char*)IrdRec->InternalBuffer + Stmt->CharOffset[Offset],
MIN(BufferLength - sizeof(SQLWCHAR), CharLength*sizeof(SQLWCHAR)));
}
/* Terminating Null */
*(SQLWCHAR*)((char*)TargetValuePtr + MIN(BufferLength - sizeof(SQLWCHAR), CharLength*sizeof(SQLWCHAR)))= 0;
}
if (CharLength >= BufferLength / sizeof(SQLWCHAR))
{
/* Calculate new offset and substract 1 byte for null termination */
Stmt->CharOffset[Offset]+= (unsigned long)BufferLength - sizeof(SQLWCHAR);
MADB_FREE(ClientValue);
return MADB_SetError(&Stmt->Error, MADB_ERR_01004, NULL, 0);
}
else
{
Stmt->CharOffset[Offset]= Stmt->Lengths[Offset];
MADB_FREE(IrdRec->InternalBuffer);
}
MADB_FREE(ClientValue);
}
break;
case SQL_CHAR:
case SQL_VARCHAR:
if (Stmt->stmt->fields[Offset].type == MYSQL_TYPE_BLOB &&
Stmt->stmt->fields[Offset].charsetnr == 63)
{
if (!BufferLength && StrLen_or_IndPtr)
{
*StrLen_or_IndPtr= Stmt->stmt->fields[Offset].max_length * 2;
return SQL_SUCCESS_WITH_INFO;
}
#ifdef CONVERSION_TO_HEX_IMPLEMENTED
{
/*TODO: */
char *TmpBuffer;
if (!(TmpBuffer= (char *)MADB_CALLOC(BufferLength)))
{
}
}
#endif
}
ZeroTerminated= 1;
case SQL_LONGVARCHAR:
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
{
Bind.buffer= TargetValuePtr;
Bind.buffer_length= (unsigned long)BufferLength;
Bind.buffer_type= MadbType;
if (!(BufferLength) && StrLen_or_IndPtr)
{
/* Paranoid - before StrLen_or_IndPtr was used as length directly. so leaving same value in Bind.length. Unlikely needed */
Bind.length_value= (unsigned long)*StrLen_or_IndPtr;
Bind.length= &Bind.length_value;
mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, Stmt->CharOffset[Offset]);
if (InternalUse)
{
*StrLen_or_IndPtr= MIN(*Bind.length, Stmt->stmt->fields[Offset].max_length);
}
else
{
if (!Stmt->CharOffset[Offset])
{
Stmt->Lengths[Offset]= MIN(*Bind.length, Stmt->stmt->fields[Offset].max_length);
}
*StrLen_or_IndPtr= Stmt->Lengths[Offset] - Stmt->CharOffset[Offset];
}
MADB_SetError(&Stmt->Error, MADB_ERR_01004, NULL, 0);
return SQL_SUCCESS_WITH_INFO;
}
if (mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, CurrentOffset))
{
MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
return Stmt->Error.ReturnValue;
}
/* Dirty temporary hack before we know what is going on. Yes, there is nothing more eternal, than temporary
It's not that bad, after all */
if ((long)*Bind.length == -1)
{
*Bind.length= 0;
}
/* end of dirty hack */
if (!InternalUse && !Stmt->CharOffset[Offset])
{
Stmt->Lengths[Offset]= MIN(*Bind.length, Stmt->stmt->fields[Offset].max_length);
}
if (ZeroTerminated)
{
char *p= (char *)Bind.buffer;
if (BufferLength > (SQLLEN)*Bind.length)
{
p[*Bind.length]= 0;
}
else
{
p[BufferLength-1]= 0;
}
}
if (StrLen_or_IndPtr)
{
*StrLen_or_IndPtr= *Bind.length - CurrentOffset;
}
if (InternalUse == FALSE)
{
/* Recording new offset only if that is API call, and not getting data for internal use */
Stmt->CharOffset[Offset]+= MIN((unsigned long)BufferLength - ZeroTerminated, *Bind.length);
if ((BufferLength - ZeroTerminated) && Stmt->Lengths[Offset] > Stmt->CharOffset[Offset])
{
MADB_SetError(&Stmt->Error, MADB_ERR_01004, NULL, 0);
return Stmt->Error.ReturnValue;
}
}
if (StrLen_or_IndPtr && BufferLength - ZeroTerminated < *StrLen_or_IndPtr)
{
MADB_SetError(&Stmt->Error, MADB_ERR_01004, NULL, 0);
return SQL_SUCCESS_WITH_INFO;
}
}
break;
case SQL_NUMERIC:
{
SQLRETURN rc;
char *tmp;
MADB_DescRecord *Ard= MADB_DescGetInternalRecord(Stmt->Ard, Offset, MADB_DESC_READ);
Bind.buffer_length= MADB_DEFAULT_PRECISION + 1/*-*/ + 1/*.*/;
tmp= (char *)MADB_CALLOC(Bind.buffer_length);
Bind.buffer= tmp;
Bind.buffer_type= MadbType;
mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, 0);
MADB_CLEAR_ERROR(&Stmt->Error);
if (Bind.buffer_length < Stmt->stmt->fields[Offset].max_length)
{
MADB_SetError(&Stmt->Error, MADB_ERR_22003, NULL, 0);
MADB_FREE(tmp);
return Stmt->Error.ReturnValue;
}
rc= MADB_CharToSQLNumeric(tmp, Stmt->Ard, Ard, TargetValuePtr, 0);
/* Ugly */
if (rc != SQL_SUCCESS)
{
MADB_SetError(&Stmt->Error, rc, NULL, 0);
if (rc == SQL_ERROR)
{
return SQL_ERROR;
}
}
if (StrLen_or_IndPtr != NULL)
{
*StrLen_or_IndPtr= sizeof(SQL_NUMERIC_STRUCT);
}
break;
}
default:
{
/* Set the conversion function */
Bind.buffer_type= MadbType;
Bind.buffer= TargetValuePtr;
if (Bind.buffer_length == 0 && BufferLength > 0)
{
Bind.buffer_length= (unsigned long)BufferLength;
}
mysql_stmt_fetch_column(Stmt->stmt, &Bind, Offset, 0);
if (StrLen_or_IndPtr != NULL)
{
/* We get here only for fixed data types. Thus, according to the specs
"this is the length of the data after conversion; that is, it is the size of the type to which the data was converted".
For us that is the size of the buffer in bind structure. Not the size of the field */
*StrLen_or_IndPtr= Bind.buffer_length;
/* Paranoid - it was here, so leaving it in place */
if ((long)Bind.length_value == -1)
{
Bind.length_value= 0;
}
/* We do this for catalog functions and MS Access in first turn. The thing is that for some columns in catalog functions result,
we fix column type manually, since we can't make field of desired type in the query to I_S. Mostly that is for SQLSMALLINT
fields, and we can cast only to int, not to short. MSAccess in its turn like to to get length for fixed length types, and
throws error if the length is not what it expected (ODBC-131)
Probably it makes sense to do this only for SQL_C_DEFAULT type, which MS Access uses. But atm it looks like this should
not hurt if done for other types, too */
if (*StrLen_or_IndPtr == 0 || (Bind.length_value > (unsigned long)IrdRec->OctetLength && *StrLen_or_IndPtr > IrdRec->OctetLength))
{
*StrLen_or_IndPtr= IrdRec->OctetLength;
}
}
}
} /* End of switch(OdbcType) */
/* Marking fixed length fields to be able to return SQL_NO_DATA on subsequent calls, as standard prescribes
"SQLGetData cannot be used to return fixed-length data in parts. If SQLGetData is called more than one time
in a row for a column containing fixed-length data, it returns SQL_NO_DATA for all calls after the first."
Stmt->Lengths[Offset] would be set for variable length types */
if (!InternalUse && Stmt->Lengths[Offset] == 0)
{
Stmt->CharOffset[Offset]= MAX((unsigned long)Bind.buffer_length, Bind.length_value);
}
if (IsNull)
{
if (!StrLen_or_IndPtr)
{
return MADB_SetError(&Stmt->Error, MADB_ERR_22002, NULL, 0);
}
*StrLen_or_IndPtr= SQL_NULL_DATA;
}
return Stmt->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_StmtRowCount */
SQLRETURN MADB_StmtRowCount(MADB_Stmt *Stmt, SQLLEN *RowCountPtr)
{
if (Stmt->AffectedRows != -1)
*RowCountPtr= (SQLLEN)Stmt->AffectedRows;
else if (Stmt->stmt && Stmt->stmt->result.rows && mysql_stmt_field_count(Stmt->stmt))
*RowCountPtr= (SQLLEN)mysql_stmt_num_rows(Stmt->stmt);
else
*RowCountPtr= 0;
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MapColAttributesDescType */
SQLUSMALLINT MapColAttributeDescType(SQLUSMALLINT FieldIdentifier)
{
/* we need to map the old field identifiers, see bug ODBC-8 */
switch (FieldIdentifier)
{
case SQL_COLUMN_SCALE:
return SQL_DESC_SCALE;
case SQL_COLUMN_PRECISION:
return SQL_DESC_PRECISION;
case SQL_COLUMN_NULLABLE:
return SQL_DESC_NULLABLE;
case SQL_COLUMN_LENGTH:
return SQL_DESC_OCTET_LENGTH;
case SQL_COLUMN_NAME:
return SQL_DESC_NAME;
default:
return FieldIdentifier;
}
}
/* }}} */
/* {{{ MADB_StmtRowCount */
SQLRETURN MADB_StmtParamCount(MADB_Stmt *Stmt, SQLSMALLINT *ParamCountPtr)
{
*ParamCountPtr= (SQLSMALLINT)mysql_stmt_param_count(Stmt->stmt);
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_StmtColumnCount */
SQLRETURN MADB_StmtColumnCount(MADB_Stmt *Stmt, SQLSMALLINT *ColumnCountPtr)
{
/* We supposed to have that data in the descriptor by now. No sense to ask C/C API one more time for that */
*ColumnCountPtr= (SQLSMALLINT)MADB_STMT_COLUMN_COUNT(Stmt);
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_StmtColAttr */
SQLRETURN MADB_StmtColAttr(MADB_Stmt *Stmt, SQLUSMALLINT ColumnNumber, SQLUSMALLINT FieldIdentifier, SQLPOINTER CharacterAttributePtr,
SQLSMALLINT BufferLength, SQLSMALLINT *StringLengthPtr, SQLLEN *NumericAttributePtr, my_bool IsWchar)
{
MADB_DescRecord *Record;
SQLSMALLINT StringLength= 0;
SQLLEN NumericAttribute;
BOOL IsNumericAttr= TRUE;
if (!Stmt)
return SQL_INVALID_HANDLE;
MADB_CLEAR_ERROR(&Stmt->Error);
if (StringLengthPtr)
*StringLengthPtr= 0;
if (!Stmt->stmt || !mysql_stmt_field_count(Stmt->stmt))
{
MADB_SetError(&Stmt->Error, MADB_ERR_07005, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (ColumnNumber < 1 || ColumnNumber > mysql_stmt_field_count(Stmt->stmt))
{
MADB_SetError(&Stmt->Error, MADB_ERR_07009, NULL, 0);
return Stmt->Error.ReturnValue;
}
/* We start at offset zero */
--ColumnNumber;
if (!(Record= MADB_DescGetInternalRecord(Stmt->Ird, ColumnNumber, MADB_DESC_READ)))
{
MADB_SetError(&Stmt->Error, MADB_ERR_07009, NULL, 0);
return Stmt->Error.ReturnValue;
}
/* Mapping ODBC2 attributes to ODBC3
TODO: it looks like it takes more than that
"In ODBC 3.x driver must support SQL_COLUMN_PRECISION and SQL_DESC_PRECISION, SQL_COLUMN_SCALE and SQL_DESC_SCALE,
and SQL_COLUMN_LENGTH and SQL_DESC_LENGTH. These values are different because precision, scale, and length are defined
differently in ODBC 3.x than they were in ODBC 2.x."
*/
FieldIdentifier= MapColAttributeDescType(FieldIdentifier);
switch(FieldIdentifier) {
case SQL_DESC_AUTO_UNIQUE_VALUE:
NumericAttribute= (SQLLEN)Record->AutoUniqueValue;
break;
case SQL_DESC_BASE_COLUMN_NAME:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : NULL,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
Record->BaseColumnName, strlen(Record->BaseColumnName), &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_BASE_TABLE_NAME:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : NULL,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
Record->BaseTableName, strlen(Record->BaseTableName), &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_CASE_SENSITIVE:
NumericAttribute= (SQLLEN)Record->CaseSensitive;
break;
case SQL_DESC_CATALOG_NAME:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : 0,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
Record->CatalogName, strlen(Record->CatalogName), &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_SCHEMA_NAME:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : 0,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
"", 0, &Stmt->Error);
IsNumericAttr= FALSE;
case SQL_DESC_CONCISE_TYPE:
NumericAttribute= (SQLLEN)Record->ConciseType;
break;
case SQL_DESC_SEARCHABLE:
NumericAttribute= (SQLLEN)Record->Searchable;
break;
case SQL_DESC_COUNT:
NumericAttribute= (SQLLEN)Stmt->Ird->Header.Count;
break;
case SQL_DESC_DISPLAY_SIZE:
NumericAttribute= (SQLLEN)Record->DisplaySize;
break;
case SQL_DESC_FIXED_PREC_SCALE:
NumericAttribute= (SQLLEN)Record->FixedPrecScale;
break;
case SQL_DESC_PRECISION:
NumericAttribute= (SQLLEN)Record->Precision;
break;
case SQL_DESC_LENGTH:
NumericAttribute= (SQLLEN)Record->Length;
break;
case SQL_DESC_LITERAL_PREFIX:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : 0,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
Record->LiteralPrefix, strlen(Record->LiteralPrefix), &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_LITERAL_SUFFIX:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : 0,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
Record->LiteralSuffix, strlen(Record->LiteralSuffix), &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_LOCAL_TYPE_NAME:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : 0,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
"", 0, &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_LABEL:
case SQL_DESC_NAME:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : 0,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
Record->ColumnName, strlen(Record->ColumnName), &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_TYPE_NAME:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : 0,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
Record->TypeName, strlen(Record->TypeName), &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_NULLABLE:
NumericAttribute= Record->Nullable;
break;
case SQL_DESC_UNNAMED:
NumericAttribute= Record->Unnamed;
break;
case SQL_DESC_UNSIGNED:
NumericAttribute= Record->Unsigned;
break;
case SQL_DESC_UPDATABLE:
NumericAttribute= Record->Updateable;
break;
case SQL_DESC_OCTET_LENGTH:
NumericAttribute= Record->OctetLength;
break;
case SQL_DESC_SCALE:
NumericAttribute= Record->Scale;
break;
case SQL_DESC_TABLE_NAME:
StringLength= (SQLSMALLINT)MADB_SetString(IsWchar ? &Stmt->Connection->Charset : 0,
CharacterAttributePtr, (IsWchar) ? BufferLength / sizeof(SQLWCHAR) : BufferLength,
Record->TableName, strlen(Record->TableName), &Stmt->Error);
IsNumericAttr= FALSE;
break;
case SQL_DESC_TYPE:
NumericAttribute= Record->Type;
break;
case SQL_COLUMN_COUNT:
NumericAttribute= mysql_stmt_field_count(Stmt->stmt);
break;
default:
MADB_SetError(&Stmt->Error, MADB_ERR_HYC00, NULL, 0);
return Stmt->Error.ReturnValue;
}
/* We need to return the number of bytes, not characters! */
if (StringLength)
{
if (StringLengthPtr)
*StringLengthPtr= (SQLSMALLINT)StringLength;
if (!BufferLength && CharacterAttributePtr)
MADB_SetError(&Stmt->Error, MADB_ERR_01004, NULL, 0);
}
/* We shouldn't touch application memory without purpose, writing garbage there. Thus IsNumericAttr.
Besides .Net was quite disappointed about that */
if (NumericAttributePtr && IsNumericAttr == TRUE)
*NumericAttributePtr= NumericAttribute;
if (StringLengthPtr && IsWchar)
*StringLengthPtr*= sizeof(SQLWCHAR);
return Stmt->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_StmtColumnPrivileges */
SQLRETURN MADB_StmtColumnPrivileges(MADB_Stmt *Stmt, char *CatalogName, SQLSMALLINT NameLength1,
char *SchemaName, SQLSMALLINT NameLength2, char *TableName,
SQLSMALLINT NameLength3, char *ColumnName, SQLSMALLINT NameLength4)
{
char StmtStr[1024];
char *p;
int length;
MADB_CLEAR_ERROR(&Stmt->Error);
/* TableName is mandatory */
if (!TableName || !NameLength3)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY009, "Tablename is required", 0);
return Stmt->Error.ReturnValue;
}
p= StmtStr;
length = _snprintf_s(StmtStr, sizeof(StmtStr), sizeof(StmtStr) - 1, "SELECT * FROM (SELECT CAST('NULL' AS VARCHAR(2048)) AS TABLE_CAT, "
"CAST('NULL' AS VARCHAR(2048)) AS TABLE_SCHEM, CAST('NOT NULL' AS VARCHAR(2048)) AS TABLE_NAME, "
"CAST('NOT NULL' AS VARCHAR(2048)) AS COLUMN_NAME, CAST('NOT NULL' AS VARCHAR(2048)) AS GRANTOR, "
"CAST('NOT NULL' AS VARCHAR(2048)) AS GRANTEE, CAST('NOT NULL' AS VARCHAR(2048)) AS PRIVILEGE, "
"CAST('NULL' AS VARCHAR(2048)) AS IS_GRANTABLE) WHERE 1=0 ");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
length = _snprintf_s(p, sizeof(StmtStr) - strlen(StmtStr), sizeof(StmtStr) - strlen(StmtStr) - 1, "ORDER BY TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
return Stmt->Methods->ExecDirect(Stmt, StmtStr, (SQLINTEGER)strlen(StmtStr));
}
/* }}} */
/* {{{ MADB_StmtTablePrivileges */
SQLRETURN MADB_StmtTablePrivileges(MADB_Stmt *Stmt, char *CatalogName, SQLSMALLINT NameLength1,
char *SchemaName, SQLSMALLINT NameLength2,
char *TableName, SQLSMALLINT NameLength3)
{
char StmtStr[1024],
*p;
int length;
MADB_CLEAR_ERROR(&Stmt->Error);
p= StmtStr;
length = _snprintf_s(StmtStr, sizeof(StmtStr), sizeof(StmtStr) - 1, "SELECT * FROM (SELECT CAST('NULL' AS VARCHAR(2048)) AS TABLE_CAT, "
"CAST('NULL' AS VARCHAR(2048)) AS TABLE_SCHEM, CAST('NOT NULL' AS VARCHAR(2048)) AS TABLE_NAME, "
"CAST('NULL' AS VARCHAR(2048)) AS GRANTOR, CAST('NOT NULL' AS VARCHAR(2048)) AS GRANTEE, "
"CAST('NOT NULL' AS VARCHAR(2048)) AS PRIVILEGE, CAST('NULL' AS VARCHAR(2048)) AS IS_GRANTABLE) WHERE 1=0 ");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
length = _snprintf_s(p, sizeof(StmtStr) - strlen(StmtStr), sizeof(StmtStr) - strlen(StmtStr) - 1, "ORDER BY TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
return Stmt->Methods->ExecDirect(Stmt, StmtStr, (SQLINTEGER)strlen(StmtStr));
}
/* }}} */
/* {{{ ONEQ_AllCatalogs */
SQLRETURN ONEQ_AllCatalogs(MADB_Stmt *Stmt, char *CatalogNames, int CatNamesLen)
{
SQLHSTMT hstmt = SQL_NULL_HSTMT; // Statement handle
SQLRETURN retcode;
SQLCHAR Tabcatalog_buff[256];
SQLLEN lenCatalogName = 0;
retcode = MA_SQLAllocHandle(SQL_HANDLE_STMT, Stmt->Connection, (SQLHANDLE *)&hstmt);
if ( SQL_SUCCESS != retcode )
return retcode;
//First ExecStmt
retcode = SQLExecDirect(hstmt,
(SQLCHAR*) "SELECT CATALOG_NAME FROM SYSTEM.METADATA.CATALOGS"
, SQL_NTS);
if ( SQL_SUCCESS != retcode )
return retcode;
retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, &Tabcatalog_buff, 256,
(SQLPOINTER)&lenCatalogName);
if ( SQL_SUCCESS != retcode )
return retcode;
//CatalogNames Init First, Or strcat_s will crash!!!
// Fetch the resultset till NO_DATA is returned
for (int i = 0; ; i++) {
retcode = SQLFetch(hstmt);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
if (strcat_s(CatalogNames, CatNamesLen, Tabcatalog_buff) != 0) {
return SQL_ERROR;
}
if (strcat_s(CatalogNames, CatNamesLen, ";") != 0) {
return SQL_ERROR;
}
}
else {
if (retcode != SQL_NO_DATA) {
return SQL_ERROR;
}
else {
break;
}
}
}
//Free the allocated Statement Handler
if (hstmt != SQL_NULL_HSTMT)
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_StmtTables */
SQLRETURN MADB_StmtTables(MADB_Stmt *Stmt, char *CatalogName, SQLSMALLINT CatalogNameLength,
char *SchemaName, SQLSMALLINT SchemaNameLength, char *TableName,
SQLSMALLINT TableNameLength, char *TableType, SQLSMALLINT TableTypeLength)
{
MADB_DynString StmtStr;
char Quote[2];
char Escapechar[2];
SQLRETURN ret;
/*
METADATA_ID CatalogName SchemaName TableName TableType
---------------------------------------------------------------------------------
ODBC_V3:
SQL_FALSE Pattern Pattern Pattern ValueList
SQL_TRUE Identifier Identifier Identifier ValueList
ODBC_V2:
Identifier Identifier Identifier ValueList
--------------------------------------------------------------------------------
*/
MDBUG_C_ENTER(Stmt->Connection, "MADB_StmtTables");
ADJUST_LENGTH(CatalogName, CatalogNameLength);
ADJUST_LENGTH(SchemaName, SchemaNameLength);
ADJUST_LENGTH(TableName, TableNameLength);
ADJUST_LENGTH(TableType, TableTypeLength);
if (CatalogNameLength > 64 || TableNameLength > 64)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY090, "Table and catalog names are limited to 64 chars", 0);
return Stmt->Error.ReturnValue;
}
//DB server does not support either of these quote, only ' is valid
if ((strlen("'") >= sizeof(Quote)) || (strcpy_s(Quote, sizeof(Quote), "'") != 0)) {
return SQL_ERROR;
}
//Escape Character, should be backslash in ODBC
if ((strlen("\\") >= sizeof(Escapechar)) || (strcpy_s(Escapechar, sizeof(Escapechar), "\\") != 0)) {
return SQL_ERROR;
}
/* SQL_ALL_CATALOGS
If CatalogName is SQL_ALL_CATALOGS and SchemaName and TableName are empty strings,
the result set contains a list of valid catalogs for the data source.
(All columns except the TABLE_CAT column contain NULLs
*/
if (CatalogName && CatalogNameLength && TableName != NULL && !TableNameLength &&
SchemaName != NULL && SchemaNameLength == 0 && !strcmp(CatalogName, SQL_ALL_CATALOGS))
{
MADB_InitDynamicString(&StmtStr, "SELECT CATALOG_NAME AS TABLE_CAT, CAST(NULL AS CHAR(64)) AS TABLE_SCHEM, "
"CAST(NULL AS CHAR(64)) AS TABLE_NAME, NULL AS TABLE_TYPE, NULL AS REMARKS "
"FROM SYSTEM.METADATA.CATALOGS",
8192, 512);
goto RETEXEC;
}
/* SQL_ALL_SCHEMAS
If SchemaName is SQL_ALL_SCHEMAS and CatalogName and TableName are empty strings,
the result set contains a list of valid schemas for the data source.
(All columns except the TABLE_SCHEM column contain NULLs
*/
else if (CatalogName != NULL && CatalogNameLength == 0 && TableName != NULL && !TableNameLength &&
SchemaName && SchemaNameLength && !strcmp(SchemaName, SQL_ALL_SCHEMAS))
{
SQLCHAR* Tabtemp = NULL;
char *Tabptr = NULL;
SQLCHAR* TabCatalogNames;
TabCatalogNames = (SQLCHAR*)calloc(CATALOGS_MAXLEN, sizeof(SQLCHAR));
if (NULL == TabCatalogNames)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, "Can't alloc enough mem for catalongnames", 0);
return Stmt->Error.ReturnValue;
}
ret = ONEQ_AllCatalogs(Stmt, TabCatalogNames, CATALOGS_MAXLEN);
if (ret != SQL_SUCCESS)
{
free(TabCatalogNames);
return ret;
}
Tabptr = strtok_s(TabCatalogNames, ";", &Tabtemp);
MADB_InitDynamicString(&StmtStr, "SELECT NULL AS TABLE_CAT, schema_name AS TABLE_SCHEM, "
"CAST(NULL AS CHAR(64)) AS TABLE_NAME, CAST(NULL AS CHAR(64)) AS TABLE_TYPE, "
"NULL AS REMARKS FROM ("
"SELECT * FROM \"",
524288, 512);
MADB_DynstrAppend(&StmtStr, Tabptr);
MADB_DynstrAppend(&StmtStr, "\".INFORMATION_SCHEMA.SCHEMATA ");
while ((Tabptr = strtok_s(NULL, ";", &Tabtemp)) != NULL)
{
MADB_DynstrAppend(&StmtStr, "UNION ALL ");
MADB_DynstrAppend(&StmtStr, "SELECT * FROM \"");
MADB_DynstrAppend(&StmtStr, Tabptr);
MADB_DynstrAppend(&StmtStr, "\".INFORMATION_SCHEMA.SCHEMATA ");
}
MADB_DynstrAppend(&StmtStr, ")");
free(TabCatalogNames);
goto RETEXEC;
}
/* SQL_ALL_TABLE_TYPES
If TableType is SQL_ALL_TABLE_TYPES and CatalogName, SchemaName, and TableName are empty strings,
the result set contains a list of valid table types for the data source.
(All columns except the TABLE_TYPE column contain NULLs.)
*/
else if (CatalogName != NULL && !CatalogNameLength && TableName != NULL && !TableNameLength &&
SchemaName != NULL && SchemaNameLength == 0 && TableType && TableTypeLength &&
!strcmp(TableType, SQL_ALL_TABLE_TYPES))
{
MADB_InitDynamicString(&StmtStr, "SELECT NULL AS TABLE_CAT, CAST(NULL AS CHAR(64)) AS TABLE_SCHEM, "
"CAST(NULL AS CHAR(64)) AS TABLE_NAME, 'TABLE' AS TABLE_TYPE, NULL AS REMARKS "
"UNION "
"SELECT NULL, NULL, NULL, 'VIEW', NULL "
"UNION "
"SELECT NULL, NULL, NULL, 'SYSTEM TABLE', NULL",
8192, 512);
goto RETEXEC;
}
//no DSN CatalogName and no input return an empty resultset. REAL EMPTYSET NOT RESULTSET WITH NULL!
else if (!CatalogName && !(Stmt->Connection->Dsn->Catalog))
{
//Using this create a null result, or it will return an error.
MADB_InitDynamicString(&StmtStr, "SELECT * FROM (SELECT NULL AS TABLE_CAT, CAST(NULL AS CHAR(64)) AS TABLE_SCHEM, "
"CAST(NULL AS CHAR(64)) AS TABLE_NAME, 'TABLE' AS TABLE_TYPE, NULL AS REMARKS) WHERE 1=0"
, 8192, 512);
goto RETEXEC;
}
else
{
if (CatalogName)
{
int CatalogNameIsEmptyStr = !strcmp(CatalogName, "");
if (CatalogNameIsEmptyStr)
{
//inside else, using this to generate a null resultset, or ORDER BY will return error.
MADB_InitDynamicString(&StmtStr, "SELECT table_catalog AS TABLE_CAT, table_schema AS TABLE_SCHEM, "
"table_name AS TABLE_NAME, table_type AS TABLE_TYPE, NULL AS REMARKS FROM SYSTEM.INFORMATION_SCHEMA.TABLES WHERE 1=0"
, 8192, 512);
goto RETEXEC;
}
else
{
SQLCHAR* Tabtemp = NULL;
char *Tabptr = NULL;
SQLCHAR* TabCatalogNames;
TabCatalogNames = (SQLCHAR*)calloc(CATALOGS_MAXLEN, sizeof(SQLCHAR));
if (NULL == TabCatalogNames)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, "Can't alloc enough mem for catalongnames", 0);
return Stmt->Error.ReturnValue;
}
ret = ONEQ_AllCatalogs(Stmt, TabCatalogNames, CATALOGS_MAXLEN);
if (ret != SQL_SUCCESS)
{
free(TabCatalogNames);
return ret;
}
Tabptr = strtok_s(TabCatalogNames, ";", &Tabtemp);
//Only In this case the max size of the InitDynaStr needs to be this large, in other case 8192 is just fine.
MADB_InitDynamicString(&StmtStr, "SELECT * FROM (SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME, "
"CASE TABLE_SCHEMA WHEN 'information_schema' then 'SYSTEM TABLE' ELSE "
"if(TABLE_TYPE='BASE TABLE','TABLE',TABLE_TYPE) END AS TABLE_TYPE, NULL AS REMARKS FROM ("
"SELECT * FROM \"",
524288, 512);
MADB_DynstrAppend(&StmtStr, Tabptr);
MADB_DynstrAppend(&StmtStr, "\".INFORMATION_SCHEMA.TABLES ");
while ((Tabptr = strtok_s(NULL, ";", &Tabtemp)) != NULL)
{
MADB_DynstrAppend(&StmtStr, "UNION ALL ");
MADB_DynstrAppend(&StmtStr, "SELECT * FROM \"");
MADB_DynstrAppend(&StmtStr, Tabptr);
MADB_DynstrAppend(&StmtStr, "\".INFORMATION_SCHEMA.TABLES ");
}
MADB_DynstrAppend(&StmtStr, ")) WHERE 1=1 ");
MADB_DynstrAppend(&StmtStr, " AND TABLE_CAT LIKE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, CatalogName);
MADB_DynstrAppend(&StmtStr, Quote);
//Set ESCAPE Character
MADB_DynstrAppend(&StmtStr, " ESCAPE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, Escapechar);
MADB_DynstrAppend(&StmtStr, Quote);
free(TabCatalogNames);
}
}
else if (Stmt->Connection->CatalogName)
{
MADB_InitDynamicString(&StmtStr, "SELECT * FROM (SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME, "
"CASE TABLE_SCHEMA WHEN 'information_schema' then 'SYSTEM TABLE' ELSE "
"if(TABLE_TYPE='BASE TABLE','TABLE',TABLE_TYPE) END AS TABLE_TYPE ,"
"NULL AS REMARKS FROM \"",
8192, 512);
MADB_DynstrAppend(&StmtStr, Stmt->Connection->CatalogName);
MADB_DynstrAppend(&StmtStr, "\".INFORMATION_SCHEMA.TABLES) WHERE 1=1 ");
}
else if (Stmt->Connection->Dsn->Catalog)
{
MADB_InitDynamicString(&StmtStr, "SELECT * FROM (SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME, "
"CASE TABLE_SCHEMA WHEN 'information_schema' then 'SYSTEM TABLE' ELSE "
"if(TABLE_TYPE='BASE TABLE','TABLE',TABLE_TYPE) END AS TABLE_TYPE ,"
"NULL AS REMARKS FROM \"",
8192, 512);
MADB_DynstrAppend(&StmtStr, Stmt->Connection->Dsn->Catalog);
MADB_DynstrAppend(&StmtStr, "\".INFORMATION_SCHEMA.TABLES) WHERE 1=1 ");
}
// TABLE TYPE "SYSTEM TABLE" should be dealt by SchemaName
if (TableType && TableTypeLength && strcmp(TableType, SQL_ALL_TABLE_TYPES) != 0)
{
char* TableTypetoUpper = strupr(TableType);
int TableTypeisSYS = !stricmp(TableTypetoUpper, "SYSTEM TABLE");
if (!TableTypeisSYS)
{
if (SchemaName && !stricmp(SchemaName, "information_schema"))
{
MADB_DynstrAppend(&StmtStr, " AND 1=0");
goto RETEXEC;
}
unsigned int i;
char *myTypes[2] = { "TABLE", "VIEW" };
MADB_DynstrAppend(&StmtStr, " AND TABLE_TYPE IN (''");
for (i = 0; i < 2; i++)
{
if (strstr(TableTypetoUpper, myTypes[i]))
{
MADB_DynstrAppend(&StmtStr, ", '");
MADB_DynstrAppend(&StmtStr, myTypes[i]);
MADB_DynstrAppend(&StmtStr, "'");
}
}
MADB_DynstrAppend(&StmtStr, ") ");
}
else
{
if (!SchemaName)
{
MADB_DynstrAppend(&StmtStr, " AND TABLE_SCHEM ");
MADB_DynstrAppend(&StmtStr, "LIKE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, "information_schema");
MADB_DynstrAppend(&StmtStr, Quote);
//Set ESCAPE Character
MADB_DynstrAppend(&StmtStr, " ESCAPE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, Escapechar);
MADB_DynstrAppend(&StmtStr, Quote);
}
else
{
//Fix the case when TableType is SYS and SchemaName is %, this is OK now
int SchemaNameisISorPercent = ( !stricmp(SchemaName, "information_schema")
|| !stricmp(SchemaName, "%") );
if (!SchemaNameisISorPercent)
{
MADB_DynstrAppend(&StmtStr, " AND 1=0");
goto RETEXEC;
}
else {
if (!stricmp(SchemaName, "%"))
{
MADB_DynstrAppend(&StmtStr, " AND TABLE_SCHEM ");
MADB_DynstrAppend(&StmtStr, "LIKE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, "information_schema");
MADB_DynstrAppend(&StmtStr, Quote);
//Set ESCAPE Character
MADB_DynstrAppend(&StmtStr, " ESCAPE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, Escapechar);
MADB_DynstrAppend(&StmtStr, Quote);
}
}
}
}
}
if (SchemaName && SchemaNameLength)
{
if (stricmp(SchemaName, "%"))
{
MADB_DynstrAppend(&StmtStr, " AND TABLE_SCHEM ");
MADB_DynstrAppend(&StmtStr, "LIKE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, SchemaName);
MADB_DynstrAppend(&StmtStr, Quote);
//Set ESCAPE Character
MADB_DynstrAppend(&StmtStr, " ESCAPE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, Escapechar);
MADB_DynstrAppend(&StmtStr, Quote);
}
}
if (TableName && TableNameLength)
{
MADB_DynstrAppend(&StmtStr, " AND TABLE_NAME LIKE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, TableName);
MADB_DynstrAppend(&StmtStr, Quote);
//Set ESCAPE Character
MADB_DynstrAppend(&StmtStr, " ESCAPE ");
MADB_DynstrAppend(&StmtStr, Quote);
MADB_DynstrAppend(&StmtStr, Escapechar);
MADB_DynstrAppend(&StmtStr, Quote);
}
MADB_DynstrAppend(&StmtStr, " ORDER BY TABLE_CAT, TABLE_SCHEM, TABLE_NAME, TABLE_TYPE");
}
RETEXEC: MDBUG_C_PRINT(Stmt->Connection, "SQL Statement: %s", StmtStr.str);
ret = Stmt->Methods->ExecDirect(Stmt, StmtStr.str, SQL_NTS);
MADB_DynstrFree(&StmtStr);
MDBUG_C_RETURN(Stmt->Connection, ret, &Stmt->Error);
}
/* }}} */
static MADB_ShortTypeInfo SqlStatsColType[13]=
/*1*/ {{SQL_VARCHAR, 0, SQL_NULLABLE, 0}, {SQL_VARCHAR, 0, SQL_NULLABLE, 0}, {SQL_VARCHAR, 0, SQL_NO_NULLS, 0}, {SQL_SMALLINT, 0, SQL_NULLABLE, 0},
/*5*/ {SQL_VARCHAR, 0, SQL_NULLABLE, 0}, {SQL_VARCHAR, 0, SQL_NULLABLE, 0}, {SQL_SMALLINT, 0, SQL_NO_NULLS, 0}, {SQL_SMALLINT, 0, SQL_NULLABLE, 0},
/*9*/ {SQL_VARCHAR, 0, SQL_NULLABLE, 0}, {SQL_CHAR, 0, SQL_NULLABLE, 2}, {SQL_INTEGER, 0, SQL_NULLABLE, 0}, {SQL_INTEGER, 0, SQL_NULLABLE, 0},
/*13*/ {SQL_VARCHAR, 0, SQL_NULLABLE, 0}};
/* {{{ MADB_StmtStatistics */
SQLRETURN MADB_StmtStatistics(MADB_Stmt *Stmt, char *CatalogName, SQLSMALLINT NameLength1,
char *SchemaName, SQLSMALLINT NameLength2,
char *TableName, SQLSMALLINT NameLength3,
SQLUSMALLINT Unique, SQLUSMALLINT Reserved)
{
char StmtStr[1024];
char *p= StmtStr;
int length;
SQLRETURN ret;
MADB_CLEAR_ERROR(&Stmt->Error);
/* TableName is mandatory */
if (!TableName || !NameLength3)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY009, "Tablename is required", 0);
return Stmt->Error.ReturnValue;
}
length = _snprintf_s(StmtStr, sizeof(StmtStr), sizeof(StmtStr) - 1, "SELECT * FROM (SELECT CAST('NULL' AS VARCHAR(2048)) AS TABLE_CAT, "
"CAST('NULL' AS VARCHAR(2048)) AS TABLE_SCHEM, CAST('NOT NULL' AS VARCHAR(2048)) AS TABLE_NAME, 0 AS NON_UNIQUE, "
"CAST('NULL' AS VARCHAR(2048)) AS INDEX_QUALIFIER, CAST('NULL' AS VARCHAR(2048)) AS INDEX_NAME, 0 AS TYPE, "
"0 AS ORDINAL_POSITION, CAST('NULL' AS VARCHAR(2048)) AS COLUMN_NAME, 'A' AS ASC_OR_DESC, 0 AS CARDINALITY, 0 AS PAGES, "
"CAST('NULL' AS VARCHAR(2048)) AS FILTER_CONDITION) WHERE 1=0 ",
SQL_INDEX_OTHER);
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
length = _snprintf_s(p, sizeof(StmtStr) - strlen(StmtStr), sizeof(StmtStr) - strlen(StmtStr) - 1, "ORDER BY NON_UNIQUE, TYPE, INDEX_NAME, ORDINAL_POSITION");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
ret= Stmt->Methods->ExecDirect(Stmt, StmtStr, SQL_NTS);
if (SQL_SUCCEEDED(ret))
{
MADB_FixColumnDataTypes(Stmt, SqlStatsColType);
}
return ret;
}
/* }}} */
static MADB_ShortTypeInfo SqlColumnsColType[18]=
/*1*/ {{SQL_VARCHAR, 0, SQL_NO_NULLS, 0}, {SQL_VARCHAR, 0, SQL_NO_NULLS, 0}, {SQL_VARCHAR, 0, SQL_NULLABLE, 0}, {SQL_VARCHAR, 0, SQL_NULLABLE, 0},
/*5*/ {SQL_SMALLINT, 0, SQL_NO_NULLS, 0}, {SQL_VARCHAR, 0, SQL_NO_NULLS, 0}, {SQL_INTEGER, 0, SQL_NULLABLE, 0}, {SQL_INTEGER, 0, SQL_NULLABLE, 0},
/*9*/ {SQL_SMALLINT, 0, SQL_NULLABLE, 0}, {SQL_SMALLINT, 0, SQL_NULLABLE, 0}, {SQL_SMALLINT, 0, SQL_NO_NULLS, 0}, {SQL_VARCHAR, 0, SQL_NULLABLE, 0},
/*13*/ {SQL_VARCHAR, 0, SQL_NULLABLE, 0}, {SQL_SMALLINT, 0, SQL_NO_NULLS, 0}, {SQL_SMALLINT, 0, SQL_NULLABLE, 0},
/*16*/ {SQL_INTEGER, 0, SQL_NULLABLE, 0}, {SQL_INTEGER, 0, SQL_NO_NULLS, 0}, {SQL_VARCHAR, 0, SQL_NULLABLE, 0}};
/* {{{ MADB_StmtColumns */
SQLRETURN MADB_StmtColumns(MADB_Stmt *Stmt,
char *CatalogName, SQLSMALLINT NameLength1,
char *SchemaName, SQLSMALLINT NameLength2,
char *TableName, SQLSMALLINT NameLength3,
char *ColumnName, SQLSMALLINT NameLength4)
{
MADB_DynString StmtStr;
SQLRETURN ret;
MDBUG_C_ENTER(Stmt->Connection, "StmtColumns");
// no DSN CatalogName and no input return an empty resultset.
if (!CatalogName && !(Stmt->Connection->Dsn->Catalog))
{
MADB_InitDynamicString(&StmtStr, ONEQODBC_COLUMNS_EMPTY, 8192, 1024);
goto RETEXEC;
} else if (CatalogName)
{
int CatalogNameIsEmptyStr = !strcmp(CatalogName, "");
my_bool CatalogNotExist = 0;
SQLCHAR catalog_buff[64] = {'\0'};
SQLCHAR* AllCatalogNames;
AllCatalogNames = (SQLCHAR*)calloc(CATALOGS_MAXLEN, sizeof(SQLCHAR));
if (NULL == AllCatalogNames)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, "Can't alloc enough mem for catalongnames", 0);
return Stmt->Error.ReturnValue;
}
int cataloglen = sizeof(catalog_buff) / sizeof(catalog_buff[0]);
if (strcat_s(catalog_buff, cataloglen, CatalogName) != 0) {
free(AllCatalogNames);
return SQL_ERROR;
}
if (strcat_s(catalog_buff, cataloglen, ";") != 0) {
free(AllCatalogNames);
return SQL_ERROR;
}
ret = ONEQ_AllCatalogs(Stmt, AllCatalogNames, CATALOGS_MAXLEN);
if (ret != SQL_SUCCESS)
{
free(AllCatalogNames);
return ret;
}
if (!strstr(AllCatalogNames, catalog_buff))
{
CatalogNotExist = 1;
}
free(AllCatalogNames);
if (CatalogNameIsEmptyStr || CatalogNotExist)
{
//using this to generate a null resultset
MADB_InitDynamicString(&StmtStr, ONEQODBC_COLUMNS_EMPTY, 8192, 1024);
goto RETEXEC;
}
}
MADB_InitDynamicString(&StmtStr, ONEQODBC_COLUMNS_OUTSIDESORT, 8192, 1024);
if (MADB_DynstrAppend(&StmtStr, ONEQODBC_COLUMNS_CHAROCTLEN))
goto dynerror;
if (MADB_DynstrAppend(&StmtStr, ONEQODBC_COLUMNS_COLUMNSIZE))
goto dynerror;
if (MADB_DynstrAppend(&StmtStr, ONEQODBC_COLUMNS_BUFFERLEN))
goto dynerror;
if (MADB_DynstrAppend(&StmtStr, ONEQODBC_COLUMNS_MAINBUILD))
goto dynerror;
if (MADB_DynstrAppend(&StmtStr, ONEQODBC_COLUMNS_DATATYPE))
goto dynerror;
if (MADB_DynstrAppend(&StmtStr, ONEQODBC_COLUMNS_COLUMNTYPE))
goto dynerror;
if (MADB_DynstrAppend(&StmtStr, ONEQODBC_COLUMNS_BRACKETLEN))
goto dynerror;
ADJUST_LENGTH(CatalogName, NameLength1);
ADJUST_LENGTH(SchemaName, NameLength2);
ADJUST_LENGTH(TableName, NameLength3);
ADJUST_LENGTH(ColumnName, NameLength4);
if (!CatalogName && Stmt->Connection->Dsn->Catalog)
{
if (MADB_DynstrAppend(&StmtStr, " from \"") ||
MADB_DynstrAppend(&StmtStr, Stmt->Connection->Dsn->Catalog) ||
MADB_DynstrAppend(&StmtStr, "\".INFORMATION_SCHEMA.COLUMNS WHERE 1=1 "))
goto dynerror;
} else if (CatalogName)
{
if (MADB_DynstrAppend(&StmtStr, " from \"") ||
MADB_DynstrAppendMem(&StmtStr, CatalogName, NameLength1) ||
MADB_DynstrAppend(&StmtStr, "\".INFORMATION_SCHEMA.COLUMNS WHERE 1=1 "))
goto dynerror;
}
if (SchemaName && NameLength2)
if (MADB_DynstrAppend(&StmtStr, " AND TABLE_SCHEMA LIKE '") ||
MADB_DynstrAppendMem(&StmtStr, SchemaName, NameLength2) ||
MADB_DynstrAppend(&StmtStr, "' ") ||
MADB_DynstrAppend(&StmtStr, " ESCAPE '\\' "))
goto dynerror;
if (TableName && NameLength3)
if (MADB_DynstrAppend(&StmtStr, " AND TABLE_NAME LIKE '") ||
MADB_DynstrAppendMem(&StmtStr, TableName, NameLength3) ||
MADB_DynstrAppend(&StmtStr, "' ") ||
MADB_DynstrAppend(&StmtStr, " ESCAPE '\\' "))
goto dynerror;
if (ColumnName && NameLength4)
if (MADB_DynstrAppend(&StmtStr, " AND COLUMN_NAME LIKE '") ||
MADB_DynstrAppendMem(&StmtStr, ColumnName, NameLength4) ||
MADB_DynstrAppend(&StmtStr, "' ") ||
MADB_DynstrAppend(&StmtStr, " ESCAPE '\\' "))
goto dynerror;
if (MADB_DynstrAppend(&StmtStr, ")))) ORDER BY TABLE_CAT, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION"))
goto dynerror;
RETEXEC:
MDBUG_C_DUMP(Stmt->Connection, StmtStr.str, s);
ret= Stmt->Methods->ExecDirect(Stmt, StmtStr.str, SQL_NTS);
if (SQL_SUCCEEDED(ret))
{
MADB_FixColumnDataTypes(Stmt, SqlColumnsColType);
}
MADB_DynstrFree(&StmtStr);
MDBUG_C_DUMP(Stmt->Connection, ret, d);
return ret;
dynerror:
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
return Stmt->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_StmtProcedureColumns */
SQLRETURN MADB_StmtProcedureColumns(MADB_Stmt *Stmt, char *CatalogName, SQLSMALLINT NameLength1,
char *SchemaName, SQLSMALLINT NameLength2, char *ProcName,
SQLSMALLINT NameLength3, char *ColumnName, SQLSMALLINT NameLength4)
{
char StmtStr[2048],
*p;
int length;
SQLRETURN ret;
MADB_CLEAR_ERROR(&Stmt->Error);
p= StmtStr;
length = _snprintf_s(p, sizeof(StmtStr), sizeof(StmtStr) - 1, ONEQODBC_PROCEDURE_COLUMNS);
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
length = _snprintf_s(p, sizeof(StmtStr) - strlen(StmtStr), sizeof(StmtStr) - strlen(StmtStr) - 1, " ORDER BY PROCEDURE_CAT, PROCEDURE_NAME, ORDINAL_POSITION");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
ret= Stmt->Methods->ExecDirect(Stmt, StmtStr, SQL_NTS);
return ret;
}
/* }}} */
/* {{{ MADB_StmtPrimaryKeys */
SQLRETURN MADB_StmtPrimaryKeys(MADB_Stmt *Stmt, char *CatalogName, SQLSMALLINT NameLength1,
char *SchemaName, SQLSMALLINT NameLength2, char *TableName,
SQLSMALLINT NameLength3)
{
char StmtStr[2048],
*p;
int length;
MADB_CLEAR_ERROR(&Stmt->Error);
/* TableName is mandatory */
if (!TableName || !NameLength3)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY009, "Tablename is required", 0);
return Stmt->Error.ReturnValue;
}
p= StmtStr;
length = _snprintf_s(p, sizeof(StmtStr), sizeof(StmtStr) - 1, "SELECT * FROM (SELECT CAST('NULL' AS VARCHAR(2048)) AS TABLE_CAT, "
"CAST('NULL' AS VARCHAR(2048)) AS TABLE_SCHEM, CAST('NULL' AS VARCHAR(2048)) AS TABLE_NAME, "
"CAST('NOT NULL' AS VARCHAR(2048)) AS COLUMN_NAME, CAST(0 AS SMALLINT) AS KEY_SEQ, "
"CAST('NULL' AS VARCHAR(2048)) AS PK_NAME) WHERE 1=0");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
length = _snprintf_s(p, sizeof(StmtStr) - strlen(StmtStr), sizeof(StmtStr) - strlen(StmtStr) - 1, " ORDER BY TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
return Stmt->Methods->ExecDirect(Stmt, StmtStr, SQL_NTS);
}
/* }}} */
/* {{{ MADB_StmtSpecialColumns */
SQLRETURN MADB_StmtSpecialColumns(MADB_Stmt *Stmt, SQLUSMALLINT IdentifierType,
char *CatalogName, SQLSMALLINT NameLength1,
char *SchemaName, SQLSMALLINT NameLength2,
char *TableName, SQLSMALLINT NameLength3,
SQLUSMALLINT Scope, SQLUSMALLINT Nullable)
{
char StmtStr[2048],
*p= StmtStr;
int length;
MADB_CLEAR_ERROR(&Stmt->Error);
/* TableName is mandatory */
if (!TableName || !NameLength3)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY009, "Tablename is required", 0);
return Stmt->Error.ReturnValue;
}
length = _snprintf_s(p, sizeof(StmtStr), sizeof(StmtStr) - 1, "SELECT * FROM (SELECT CAST(0 AS SMALLINT) AS SCOPE, "
"CAST('NOT NULL' AS VARCHAR(2048)) AS COLUMN_NAME, CAST(0 AS SMALLINT) AS DATA_TYPE, "
"CAST('NOT NULL' AS VARCHAR(2048)) as TYPE_NAME, 0 as COLUMN_SIZE, "
"0 AS BUFFER_LENGTH, CAST(0 AS SMALLINT) as DECIMAL_DIGITS, "
"CAST(0 AS SMALLINT) as PSEUDO_COLUMN) where 1=0 ");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
length = _snprintf_s(p, sizeof(StmtStr) - strlen(StmtStr), sizeof(StmtStr) - strlen(StmtStr) - 1, "ORDER BY SCOPE");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
return Stmt->Methods->ExecDirect(Stmt, StmtStr, SQL_NTS);
}
/* }}} */
/* {{{ MADB_StmtProcedures */
SQLRETURN MADB_StmtProcedures(MADB_Stmt *Stmt, char *CatalogName, SQLSMALLINT NameLength1,
char *SchemaName, SQLSMALLINT NameLength2, char *ProcName,
SQLSMALLINT NameLength3)
{
char StmtStr[2048],
*p;
int length;
MADB_CLEAR_ERROR(&Stmt->Error);
p= StmtStr;
length = _snprintf_s(p, sizeof(StmtStr), sizeof(StmtStr) - 1, "select * from (select CAST('NULL' AS VARCHAR(2048)) as PROCEDURE_CAT, "
"CAST('NULL' AS VARCHAR(2048)) as PROCEDURE_SCHEM, CAST('NOT NULL' AS VARCHAR(2048)) as PROCEDURE_NAME, "
"NULL as NUM_INPUT_PARAMS, NULL as NUM_OUTPUT_PARAMS, NULL as NUM_RESULT_SETS,"
"CAST('NULL' AS VARCHAR(2048)) as REMARKS, CAST(0 AS SMALLINT) as PROCEDURE_TYPE) where 1=0");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
length = _snprintf_s(p, sizeof(StmtStr) - strlen(StmtStr), sizeof(StmtStr) - strlen(StmtStr) - 1, " ORDER BY PROCEDURE_CAT, PROCEDURE_SCHEM, PROCEDURE_NAME");
p = (length >= 0)? (p + length) : (&StmtStr[sizeof(StmtStr) - 1]);
return Stmt->Methods->ExecDirect(Stmt, StmtStr, SQL_NTS);
}
/* }}} */
/* {{{ SQLForeignKeys */
SQLRETURN MADB_StmtForeignKeys(MADB_Stmt *Stmt, char *PKCatalogName, SQLSMALLINT NameLength1,
char *PKSchemaName, SQLSMALLINT NameLength2, char *PKTableName,
SQLSMALLINT NameLength3, char *FKCatalogName, SQLSMALLINT NameLength4,
char *FKSchemaName, SQLSMALLINT NameLength5, char *FKTableName,
SQLSMALLINT NameLength6)
{
SQLRETURN ret= SQL_ERROR;
MADB_DynString StmtStr;
MADB_CLEAR_ERROR(&Stmt->Error);
ADJUST_LENGTH(PKCatalogName, NameLength1);
ADJUST_LENGTH(PKSchemaName, NameLength2);
ADJUST_LENGTH(PKTableName, NameLength3);
ADJUST_LENGTH(FKCatalogName, NameLength4);
ADJUST_LENGTH(FKSchemaName, NameLength5);
ADJUST_LENGTH(FKTableName, NameLength6);
/* PKTableName and FKTableName are mandatory */
if ((!PKTableName || !NameLength3) && (!FKTableName || !NameLength6))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY009, "PKTableName or FKTableName are required", 0);
return Stmt->Error.ReturnValue;
}
MADB_InitDynamicString(&StmtStr,
"SELECT * FROM (SELECT CAST('NULL' AS VARCHAR(2048)) AS PKTABLE_CAT, CAST('NULL' AS VARCHAR(2048)) AS PKTABLE_SCHEM, "
"CAST('NOT NULL' AS VARCHAR(2048)) AS PKTABLE_NAME, CAST('NOT NULL' AS VARCHAR(2048)) AS PKCOLUMN_NAME, "
"CAST('NULL' AS VARCHAR(2048)) AS FKTABLE_CAT, CAST('NULL' AS VARCHAR(2048)) AS FKTABLE_SCHEM, "
"CAST('NOT NULL' AS VARCHAR(2048)) AS FKTABLE_NAME, CAST('NOT NULL' AS VARCHAR(2048)) AS FKCOLUMN_NAME, "
"CAST(0 AS SMALLINT) AS KEY_SEQ, CAST(0 AS SMALLINT) AS UPDATE_RULE, "
"CAST(0 AS SMALLINT) AS DELETE_RULE, CAST('NULL' AS VARCHAR(2048)) AS FK_NAME, "
"CAST('NULL' AS VARCHAR(2048)) AS PK_NAME, CAST(0 AS SMALLINT) AS DEFERRABILITY) WHERE 1=0 ",
8192, 128);
MADB_DynstrAppend(&StmtStr, "ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ, PKTABLE_NAME");
ret= Stmt->Methods->ExecDirect(Stmt, StmtStr.str, SQL_NTS);
MADB_DynstrFree(&StmtStr);
return ret;
}
/* }}} */
/* {{{ MADB_StmtDescribeCol */
SQLRETURN MADB_StmtDescribeCol(MADB_Stmt *Stmt, SQLUSMALLINT ColumnNumber, void *ColumnName,
SQLSMALLINT BufferLength, SQLSMALLINT *NameLengthPtr,
SQLSMALLINT *DataTypePtr, SQLULEN *ColumnSizePtr, SQLSMALLINT *DecimalDigitsPtr,
SQLSMALLINT *NullablePtr, my_bool isWChar)
{
MADB_DescRecord *Record;
MADB_CLEAR_ERROR(&Stmt->Error);
if (!mysql_stmt_field_count(Stmt->stmt))
{
MADB_SetError(&Stmt->Error, MADB_ERR_07005, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (ColumnNumber < 1 || ColumnNumber > mysql_stmt_field_count(Stmt->stmt))
{
MADB_SetError(&Stmt->Error, MADB_ERR_07009, NULL, 0);
return SQL_ERROR;
}
if (!(Record= MADB_DescGetInternalRecord(Stmt->Ird, ColumnNumber - 1, MADB_DESC_WRITE)))
{
MADB_CopyError(&Stmt->Error, &Stmt->Ird->Error);
return Stmt->Error.ReturnValue;
}
if (NameLengthPtr)
*NameLengthPtr= 0;
/* Don't map types if ansi mode was set */
if (DataTypePtr)
*DataTypePtr= (isWChar && !Stmt->Connection->IsAnsi) ? MADB_GetWCharType(Record->ConciseType) : Record->ConciseType;
/* Columnsize in characters, not bytes! */
if (ColumnSizePtr)
*ColumnSizePtr= Record->Length;
//Record->Precision ? MIN(Record->DisplaySize, Record->Precision) : Record->DisplaySize;
if (DecimalDigitsPtr)
*DecimalDigitsPtr= Record->Scale;
if (NullablePtr)
*NullablePtr= Record->Nullable;
if ((ColumnName || BufferLength) && Record->ColumnName)
{
size_t Length= MADB_SetString(isWChar ? &Stmt->Connection->Charset : 0, ColumnName, ColumnName ? BufferLength : 0, Record->ColumnName, SQL_NTS, &Stmt->Error);
if (NameLengthPtr)
*NameLengthPtr= (SQLSMALLINT)Length;
if (!BufferLength)
MADB_SetError(&Stmt->Error, MADB_ERR_01004, NULL, 0);
}
return Stmt->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_SetCursorName */
SQLRETURN MADB_SetCursorName(MADB_Stmt *Stmt, char *Buffer, SQLINTEGER BufferLength)
{
MADB_List *LStmt, *LStmtNext;
if (!Buffer)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY009, NULL, 0);
return SQL_ERROR;
}
if (BufferLength== SQL_NTS)
BufferLength= (SQLINTEGER)strlen(Buffer);
if (BufferLength < 0)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY090, NULL, 0);
return SQL_ERROR;
}
if ((BufferLength > 5 && strncmp(Buffer, "SQLCUR", 6) == 0) ||
(BufferLength > 6 && strncmp(Buffer, "SQL_CUR", 7) == 0))
{
MADB_SetError(&Stmt->Error, MADB_ERR_34000, NULL, 0);
return SQL_ERROR;
}
/* check if cursor name is unique */
for (LStmt= Stmt->Connection->Stmts; LStmt; LStmt= LStmtNext)
{
MADB_Cursor *Cursor= &((MADB_Stmt *)LStmt->data)->Cursor;
LStmtNext= LStmt->next;
if (Stmt != (MADB_Stmt *)LStmt->data &&
Cursor->Name && strncmp(Cursor->Name, Buffer, BufferLength) == 0)
{
MADB_SetError(&Stmt->Error, MADB_ERR_3C000, NULL, 0);
return SQL_ERROR;
}
}
MADB_FREE(Stmt->Cursor.Name);
Stmt->Cursor.Name= MADB_CALLOC(BufferLength + 1);
MADB_SetString(0, Stmt->Cursor.Name, BufferLength + 1, Buffer, BufferLength, NULL);
return SQL_SUCCESS;
}
/* }}} */
/* {{{ MADB_GetCursorName */
SQLRETURN MADB_GetCursorName(MADB_Stmt *Stmt, void *CursorName, SQLSMALLINT BufferLength,
SQLSMALLINT *NameLengthPtr, my_bool isWChar)
{
SQLSMALLINT Length;
MADB_CLEAR_ERROR(&Stmt->Error);
if (BufferLength < 0)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY090, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (!Stmt->Cursor.Name)
{
Stmt->Cursor.Name= (char *)MADB_CALLOC(MADB_MAX_CURSOR_NAME);
_snprintf(Stmt->Cursor.Name, MADB_MAX_CURSOR_NAME, "SQL_CUR%d",
Stmt->Connection->CursorCount++);
}
Length= (SQLSMALLINT)MADB_SetString(isWChar ? &Stmt->Connection->Charset : 0, CursorName,
BufferLength, Stmt->Cursor.Name, SQL_NTS, &Stmt->Error);
if (NameLengthPtr)
*NameLengthPtr= (SQLSMALLINT)Length;
if (!BufferLength)
MADB_SetError(&Stmt->Error, MADB_ERR_01004, NULL, 0);
return Stmt->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_RefreshRowPtrs */
SQLRETURN MADB_RefreshRowPtrs(MADB_Stmt *Stmt)
{
return MoveNext(Stmt, 1LL);
}
/* {{{ MADB_RefreshDynamicCursor */
SQLRETURN MADB_RefreshDynamicCursor(MADB_Stmt *Stmt)
{
SQLRETURN ret;
SQLLEN CurrentRow= Stmt->Cursor.Position;
long long AffectedRows= Stmt->AffectedRows;
SQLLEN LastRowFetched= Stmt->LastRowFetched;
ret= Stmt->Methods->Execute(Stmt, FALSE);
Stmt->Cursor.Position= CurrentRow;
if (Stmt->Cursor.Position > 0 && (my_ulonglong)Stmt->Cursor.Position >= mysql_stmt_num_rows(Stmt->stmt))
{
Stmt->Cursor.Position= (long)mysql_stmt_num_rows(Stmt->stmt) - 1;
}
Stmt->LastRowFetched= LastRowFetched;
Stmt->AffectedRows= AffectedRows;
if (Stmt->Cursor.Position < 0)
{
Stmt->Cursor.Position= 0;
}
return ret;
}
/* }}} */
/* Couple of macsros for this function specifically */
#define MADB_SETPOS_FIRSTROW(agg_result) (agg_result == SQL_INVALID_HANDLE)
#define MADB_SETPOS_AGG_RESULT(agg_result, row_result) if (MADB_SETPOS_FIRSTROW(agg_result)) agg_result= row_result; \
else if (row_result != agg_result) agg_result= SQL_SUCCESS_WITH_INFO
/* {{{ MADB_SetPos */
SQLRETURN MADB_StmtSetPos(MADB_Stmt *Stmt, SQLSETPOSIROW RowNumber, SQLUSMALLINT Operation,
SQLUSMALLINT LockType, int ArrayOffset)
{
if (!Stmt->result && !Stmt->stmt->fields)
{
MADB_SetError(&Stmt->Error, MADB_ERR_24000, NULL, 0);
return Stmt->Error.ReturnValue;
}
/* skip this for now, since we don't use unbuffered result sets
if (Stmt->Options.CursorType == SQL_CURSOR_FORWARD_ONLY)
{
MADB_SetError(&Stmt->Error, MADB_ERR_24000, NULL, 0);
return Stmt->Error.ReturnValue;
}
*/
if (LockType != SQL_LOCK_NO_CHANGE)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HYC00, NULL, 0);
return Stmt->Error.ReturnValue;
}
switch(Operation) {
case SQL_POSITION:
{
if (RowNumber < 1 || RowNumber > mysql_stmt_num_rows(Stmt->stmt))
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY109, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (Stmt->Options.CursorType == SQL_CURSOR_DYNAMIC)
if (!SQL_SUCCEEDED(Stmt->Methods->RefreshDynamicCursor(Stmt)))
return Stmt->Error.ReturnValue;
EnterCriticalSection(&Stmt->Connection->cs);
Stmt->Cursor.Position=(RowNumber - 1);
MADB_StmtDataSeek(Stmt, Stmt->Cursor.Position);
LeaveCriticalSection(&Stmt->Connection->cs);
}
break;
case SQL_ADD:
{
MADB_DynString DynStmt;
SQLRETURN ret;
char *TableName= MADB_GetTableName(Stmt);
char *CatalogName= MADB_GetCatalogName(Stmt);
int column, param= 0;
if (Stmt->Options.CursorType == SQL_CURSOR_DYNAMIC)
if (!SQL_SUCCEEDED(Stmt->Methods->RefreshDynamicCursor(Stmt)))
return Stmt->Error.ReturnValue;
Stmt->DaeRowNumber= RowNumber;
if (Stmt->DataExecutionType != MADB_DAE_ADD)
{
Stmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
MA_SQLAllocHandle(SQL_HANDLE_STMT, Stmt->Connection, (SQLHANDLE *)&Stmt->DaeStmt);
if (MADB_InitDynamicString(&DynStmt, "INSERT INTO ", 8192, 1024) ||
MADB_DynStrAppendQuoted(&DynStmt, CatalogName) ||
MADB_DynstrAppend(&DynStmt, ".") ||
MADB_DynStrAppendQuoted(&DynStmt, TableName)||
MADB_DynStrInsertSet(Stmt, &DynStmt))
{
MADB_DynstrFree(&DynStmt);
return Stmt->Error.ReturnValue;
}
ResetMetadata(&Stmt->DaeStmt->DefaultsResult, MADB_GetDefaultColumnValues(Stmt, Stmt->stmt->fields));
Stmt->DataExecutionType= MADB_DAE_ADD;
ret= Stmt->Methods->Prepare(Stmt->DaeStmt, DynStmt.str, SQL_NTS, FALSE);
MADB_DynstrFree(&DynStmt);
if (!SQL_SUCCEEDED(ret))
{
MADB_CopyError(&Stmt->Error, &Stmt->DaeStmt->Error);
Stmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
return Stmt->Error.ReturnValue;
}
}
/* Bind parameters - DaeStmt will process whole array of values, thus we don't need to iterate through the array*/
for (column= 0; column < MADB_STMT_COLUMN_COUNT(Stmt); ++column)
{
MADB_DescRecord *Rec= MADB_DescGetInternalRecord(Stmt->Ard, column, MADB_DESC_READ),
*ApdRec= NULL;
if (Rec->inUse && MADB_ColumnIgnoredInAllRows(Stmt->Ard, Rec) == FALSE)
{
Stmt->DaeStmt->Methods->BindParam(Stmt->DaeStmt, param + 1, SQL_PARAM_INPUT, Rec->ConciseType, Rec->Type,
Rec->DisplaySize, Rec->Scale, Rec->DataPtr, Rec->OctetLength, Rec->OctetLengthPtr);
}
else
{
/*Stmt->DaeStmt->Methods->BindParam(Stmt->DaeStmt, param + 1, SQL_PARAM_INPUT, SQL_CHAR, SQL_C_CHAR, 0, 0,
ApdRec->DefaultValue, strlen(ApdRec->DefaultValue), NULL);*/
continue;
}
ApdRec= MADB_DescGetInternalRecord(Stmt->DaeStmt->Apd, param, MADB_DESC_READ);
ApdRec->DefaultValue= MADB_GetDefaultColumnValue(Stmt->DaeStmt->DefaultsResult,
Stmt->stmt->fields[column].org_name);
++param;
}
memcpy(&Stmt->DaeStmt->Apd->Header, &Stmt->Ard->Header, sizeof(MADB_Header));
ret= Stmt->Methods->Execute(Stmt->DaeStmt, FALSE);
if (!SQL_SUCCEEDED(ret))
{
/* We can have SQL_NEED_DATA here, which would not set error (and its ReturnValue) */
MADB_CopyError(&Stmt->Error, &Stmt->DaeStmt->Error);
return ret;
}
if (Stmt->AffectedRows == -1)
{
Stmt->AffectedRows= 0;
}
Stmt->AffectedRows+= Stmt->DaeStmt->AffectedRows;
Stmt->DataExecutionType= MADB_DAE_NORMAL;
Stmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
Stmt->DaeStmt= NULL;
}
break;
case SQL_UPDATE:
{
char *TableName= MADB_GetTableName(Stmt);
my_ulonglong Start= 0,
End= mysql_stmt_num_rows(Stmt->stmt);
SQLRETURN result= SQL_INVALID_HANDLE; /* Just smth we cannot normally get */
if (!TableName)
{
MADB_SetError(&Stmt->Error, MADB_ERR_IM001, "Updatable Cursors with multiple tables are not supported", 0);
return Stmt->Error.ReturnValue;
}
Stmt->AffectedRows= 0;
if ((SQLLEN)RowNumber > Stmt->LastRowFetched)
{
MADB_SetError(&Stmt->Error, MADB_ERR_S1107, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (RowNumber < 0 || RowNumber > End)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY109, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (Stmt->Options.CursorType == SQL_CURSOR_DYNAMIC)
if (!SQL_SUCCEEDED(Stmt->Methods->RefreshDynamicCursor(Stmt)))
return Stmt->Error.ReturnValue;
Stmt->DaeRowNumber= MAX(1,RowNumber);
/* Cursor is open, but no row was fetched, so we simulate
that first row was fetched */
if (Stmt->Cursor.Position < 0)
Stmt->Cursor.Position= 1;
if (RowNumber)
Start= End= Stmt->Cursor.Position + RowNumber -1;
else
{
Start= Stmt->Cursor.Position;
/* TODO: if num_rows returns 1, End is 0? Start would be 1, no */
End= MIN(mysql_stmt_num_rows(Stmt->stmt)-1, Start + Stmt->Ard->Header.ArraySize - 1);
}
/* Stmt->ArrayOffset will be incremented in StmtExecute() */
Start+= Stmt->ArrayOffset;
/* TODO: SQL_ATTR_ROW_STATUS_PTR should be filled */
while (Start <= End)
{
SQLSMALLINT param= 0, column;
MADB_StmtDataSeek(Stmt, Start);
Stmt->Methods->RefreshRowPtrs(Stmt);
/* We don't need to prepare the statement, if SetPos was called
from SQLParamData() function */
if (!ArrayOffset)
{
if (!SQL_SUCCEEDED(MADB_DaeStmt(Stmt, SQL_UPDATE)))
{
MADB_SETPOS_AGG_RESULT(result, Stmt->Error.ReturnValue);
/* Moving to the next row */
Stmt->DaeRowNumber++;
Start++;
continue;
}
for(column= 0; column < MADB_STMT_COLUMN_COUNT(Stmt); ++column)
{
SQLLEN *LengthPtr= NULL;
my_bool GetDefault= FALSE;
MADB_DescRecord *Rec= MADB_DescGetInternalRecord(Stmt->Ard, column, MADB_DESC_READ);
/* TODO: shouldn't here be IndicatorPtr? */
if (Rec->OctetLengthPtr)
LengthPtr= GetBindOffset(Stmt->Ard, Rec, Rec->OctetLengthPtr, Stmt->DaeRowNumber > 1 ? Stmt->DaeRowNumber - 1 : 0, sizeof(SQLLEN)/*Rec->OctetLength*/);
if (!Rec->inUse ||
(LengthPtr && *LengthPtr == SQL_COLUMN_IGNORE))
{
GetDefault= TRUE;
continue;
}
/* TODO: Looks like this whole thing is not really needed. Not quite clear if !InUse should result in going this way */
if (GetDefault)
{
SQLLEN Length= 0;
/* set a default value */
if (Stmt->Methods->GetData(Stmt, column + 1, SQL_C_CHAR, NULL, 0, &Length, TRUE) != SQL_ERROR && Length)
{
MADB_FREE(Rec->DefaultValue);
if (Length > 0)
{
Rec->DefaultValue= (char *)MADB_CALLOC(Length + 1);
Stmt->Methods->GetData(Stmt, column + 1, SQL_C_CHAR, Rec->DefaultValue, Length+1, 0, TRUE);
}
Stmt->DaeStmt->Methods->BindParam(Stmt->DaeStmt, param + 1, SQL_PARAM_INPUT, SQL_CHAR, SQL_C_CHAR, 0, 0,
Rec->DefaultValue, Length, NULL);
++param;
continue;
}
}
if (!GetDefault)
{
Stmt->DaeStmt->Methods->BindParam(Stmt->DaeStmt, param + 1, SQL_PARAM_INPUT, Rec->ConciseType, Rec->Type,
Rec->DisplaySize, Rec->Scale,
GetBindOffset(Stmt->Ard, Rec, Rec->DataPtr, Stmt->DaeRowNumber > 1 ? Stmt->DaeRowNumber -1 : 0, Rec->OctetLength),
Rec->OctetLength, LengthPtr);
}
if (PARAM_IS_DAE(LengthPtr) && !DAE_DONE(Stmt->DaeStmt))
{
Stmt->Status= SQL_NEED_DATA;
++param;
continue;
}
++param;
} /* End of for(column=0;...) */
if (Stmt->Status == SQL_NEED_DATA)
return SQL_NEED_DATA;
} /* End of if (!ArrayOffset) */
if (Stmt->DaeStmt->Methods->Execute(Stmt->DaeStmt, FALSE) != SQL_ERROR)
{
Stmt->AffectedRows+= Stmt->DaeStmt->AffectedRows;
}
else
{
MADB_CopyError(&Stmt->Error, &Stmt->DaeStmt->Error);
}
MADB_SETPOS_AGG_RESULT(result, Stmt->DaeStmt->Error.ReturnValue);
Stmt->DaeRowNumber++;
Start++;
} /* End of while (Start <= End) */
Stmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
Stmt->DaeStmt= NULL;
Stmt->DataExecutionType= MADB_DAE_NORMAL;
/* Making sure we do not return initial value */
return result == SQL_INVALID_HANDLE ? SQL_SUCCESS :result;
}
case SQL_DELETE:
{
MADB_DynString DynamicStmt;
SQLULEN SaveArraySize= Stmt->Ard->Header.ArraySize;
my_ulonglong Start= 0,
End= mysql_stmt_num_rows(Stmt->stmt);
char *TableName= MADB_GetTableName(Stmt);
if (!TableName)
{
MADB_SetError(&Stmt->Error, MADB_ERR_IM001, "Updatable Cursors with multiple tables are not supported", 0);
return Stmt->Error.ReturnValue;
}
Stmt->Ard->Header.ArraySize= 1;
if (Stmt->Options.CursorType == SQL_CURSOR_DYNAMIC)
if (!SQL_SUCCEEDED(Stmt->Methods->RefreshDynamicCursor(Stmt)))
return Stmt->Error.ReturnValue;
Stmt->AffectedRows= 0;
if (RowNumber < 0 || RowNumber > End)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY109, NULL, 0);
return Stmt->Error.ReturnValue;
}
Start= (RowNumber) ? Stmt->Cursor.Position + RowNumber - 1 : Stmt->Cursor.Position;
if (SaveArraySize && !RowNumber)
End= MIN(End, Start + SaveArraySize - 1);
else
End= Start;
while (Start <= End)
{
MADB_StmtDataSeek(Stmt, Start);
Stmt->Methods->RefreshRowPtrs(Stmt);
MADB_InitDynamicString(&DynamicStmt, "DELETE FROM ", 8192, 1024);
if (MADB_DynStrAppendQuoted(&DynamicStmt, TableName) ||
MADB_DynStrGetWhere(Stmt, &DynamicStmt, TableName, FALSE))
{
MADB_DynstrFree(&DynamicStmt);
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, NULL, 0);
return Stmt->Error.ReturnValue;
}
LOCK_MARIADB(Stmt->Connection);
if (mysql_real_query(Stmt->Connection->mariadb, DynamicStmt.str, (unsigned long)DynamicStmt.length))
{
MADB_DynstrFree(&DynamicStmt);
MADB_SetError(&Stmt->Error, MADB_ERR_HY001, mysql_error(Stmt->Connection->mariadb),
mysql_errno(Stmt->Connection->mariadb));
UNLOCK_MARIADB(Stmt->Connection);
return Stmt->Error.ReturnValue;
}
MADB_DynstrFree(&DynamicStmt);
Stmt->AffectedRows+= mysql_affected_rows(Stmt->Connection->mariadb);
Start++;
}
UNLOCK_MARIADB(Stmt->Connection);
Stmt->Ard->Header.ArraySize= SaveArraySize;
/* if we have a dynamic cursor we need to adjust the rowset size */
if (Stmt->Options.CursorType == SQL_CURSOR_DYNAMIC)
{
Stmt->LastRowFetched-= (unsigned long)Stmt->AffectedRows;
}
}
break;
case SQL_REFRESH:
/* todo*/
break;
default:
MADB_SetError(&Stmt->Error, MADB_ERR_HYC00, "Only SQL_POSITION and SQL_REFRESH Operations are supported", 0);
return Stmt->Error.ReturnValue;
}
return SQL_SUCCESS;
}
/* }}} */
#undef MADB_SETPOS_FIRSTROW
#undef MADB_SETPOS_AGG_RESULT
/* {{{ MADB_StmtFetchScroll */
SQLRETURN MADB_StmtFetchScroll(MADB_Stmt *Stmt, SQLSMALLINT FetchOrientation,
SQLLEN FetchOffset)
{
SQLRETURN ret= SQL_SUCCESS;
SQLLEN Position;
SQLLEN RowsProcessed;
RowsProcessed= Stmt->LastRowFetched;
if (Stmt->Options.CursorType == SQL_CURSOR_FORWARD_ONLY &&
FetchOrientation != SQL_FETCH_NEXT)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY106, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (Stmt->Options.CursorType == SQL_CURSOR_DYNAMIC)
{
SQLRETURN rc;
rc= Stmt->Methods->RefreshDynamicCursor(Stmt);
if (!SQL_SUCCEEDED(rc))
{
return Stmt->Error.ReturnValue;
}
}
if (FetchOrientation != SQL_FETCH_NEXT)
{
MADB_STMT_FORGET_NEXT_POS(Stmt);
}
switch(FetchOrientation) {
case SQL_FETCH_NEXT:
Position= Stmt->Cursor.Position < 0 ? 0 : Stmt->Cursor.Position + RowsProcessed;
break;
case SQL_FETCH_PRIOR:
Position= Stmt->Cursor.Position < 0 ? - 1: Stmt->Cursor.Position - MAX(1, Stmt->Ard->Header.ArraySize);
break;
case SQL_FETCH_RELATIVE:
Position= Stmt->Cursor.Position + FetchOffset;
if (Position < 0 && Stmt->Cursor.Position > 0 &&
-FetchOffset <= (SQLINTEGER)Stmt->Ard->Header.ArraySize)
Position= 0;
break;
case SQL_FETCH_ABSOLUTE:
if (FetchOffset < 0)
{
if ((long long)mysql_stmt_num_rows(Stmt->stmt) - 1 + FetchOffset < 0 &&
((SQLULEN)-FetchOffset <= Stmt->Ard->Header.ArraySize))
Position= 0;
else
Position= (SQLLEN)mysql_stmt_num_rows(Stmt->stmt) + FetchOffset;
}
else
Position= FetchOffset - 1;
break;
case SQL_FETCH_FIRST:
Position= 0;
break;
case SQL_FETCH_LAST:
Position= (SQLLEN)mysql_stmt_num_rows(Stmt->stmt) - MAX(1, Stmt->Ard->Header.ArraySize);
/* if (Stmt->Ard->Header.ArraySize > 1)
Position= MAX(0, Position - Stmt->Ard->Header.ArraySize + 1); */
break;
case SQL_FETCH_BOOKMARK:
if (Stmt->Options.UseBookmarks == SQL_UB_OFF)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY106, NULL, 0);
return Stmt->Error.ReturnValue;
}
if (!Stmt->Options.BookmarkPtr)
{
MADB_SetError(&Stmt->Error, MADB_ERR_HY111, NULL, 0);
return Stmt->Error.ReturnValue;
}
Position= *((long *)Stmt->Options.BookmarkPtr);
if (Stmt->Connection->Environment->OdbcVersion >= SQL_OV_ODBC3)
Position+= FetchOffset;
break;
default:
MADB_SetError(&Stmt->Error, MADB_ERR_HY106, NULL, 0);
return Stmt->Error.ReturnValue;
break;
}
if (Position < 0)
{
MADB_STMT_RESET_CURSOR(Stmt);
}
else
{
Stmt->Cursor.Position= (SQLLEN)MIN((my_ulonglong)Position, mysql_stmt_num_rows(Stmt->stmt));
}
if (Position < 0 || (my_ulonglong)Position > mysql_stmt_num_rows(Stmt->stmt) - 1)
{
/* We need to put cursor before RS start, not only return error */
if (Position < 0)
{
MADB_StmtDataSeek(Stmt, 0);
}
return SQL_NO_DATA;
}
/* For dynamic cursor we "refresh" resultset eachtime(basically re-executing), and thus the (c/c)cursor is before 1st row at this point,
and thux we need to restore the last position. For array fetch with not forward_only cursor, the (c/c)cursor is at 1st row of the last
fetched rowset */
if (FetchOrientation != SQL_FETCH_NEXT || (RowsProcessed > 1 && Stmt->Options.CursorType != SQL_CURSOR_FORWARD_ONLY) ||
Stmt->Options.CursorType == SQL_CURSOR_DYNAMIC)
{
if (Stmt->Cursor.Next != NULL)
{
mysql_stmt_row_seek(Stmt->stmt, Stmt->Cursor.Next);
ret= SQL_SUCCESS;
}
else
{
ret= MADB_StmtDataSeek(Stmt, Stmt->Cursor.Position);
}
}
/* Assuming, that ret before previous "if" was SQL_SUCCESS */
if (ret == SQL_SUCCESS)
{
ret= Stmt->Methods->Fetch(Stmt);
}
if (ret == SQL_NO_DATA_FOUND && Stmt->LastRowFetched > 0)
{
ret= SQL_SUCCESS;
}
return ret;
}
struct st_ma_stmt_methods MADB_StmtMethods=
{
MADB_StmtPrepare,
MADB_StmtExecute,
MADB_StmtFetch,
MADB_StmtBindCol,
MADB_StmtBindParam,
MADB_StmtExecDirect,
MADB_StmtGetData,
MADB_StmtRowCount,
MADB_StmtParamCount,
MADB_StmtColumnCount,
MADB_StmtGetAttr,
MADB_StmtSetAttr,
MADB_StmtFree,
MADB_StmtColAttr,
MADB_StmtColumnPrivileges,
MADB_StmtTablePrivileges,
MADB_StmtTables,
MADB_StmtStatistics,
MADB_StmtColumns,
MADB_StmtProcedureColumns,
MADB_StmtPrimaryKeys,
MADB_StmtSpecialColumns,
MADB_StmtProcedures,
MADB_StmtForeignKeys,
MADB_StmtDescribeCol,
MADB_SetCursorName,
MADB_GetCursorName,
MADB_StmtSetPos,
MADB_StmtFetchScroll,
MADB_StmtParamData,
MADB_StmtPutData,
MADB_StmtBulkOperations,
MADB_RefreshDynamicCursor,
MADB_RefreshRowPtrs,
MADB_GetOutParams
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/openlookeng/hetu-odbc-driver.git
git@gitee.com:openlookeng/hetu-odbc-driver.git
openlookeng
hetu-odbc-driver
hetu-odbc-driver
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385