sqldb.pp 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187
  1. {
  2. Copyright (c) 2004-2014 by Joost van der Sluis, FPC contributors
  3. SQL database & dataset
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {$IFNDEF FPC_DOTTEDUNITS}
  11. unit SQLDB;
  12. {$ENDIF FPC_DOTTEDUNITS}
  13. {$mode objfpc}
  14. {$H+}
  15. {$M+} // ### remove this!!!
  16. interface
  17. {$IFDEF FPC_DOTTEDUNITS}
  18. uses System.SysUtils, System.Classes, Data.Db, Data.BufDataset, Data.Sql.Script, Data.Sql.Types;
  19. {$ELSE FPC_DOTTEDUNITS}
  20. uses SysUtils, Classes, DB, bufdataset, sqlscript, sqltypes;
  21. {$ENDIF FPC_DOTTEDUNITS}
  22. type
  23. TSchemaType = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.TSchemaType;
  24. TStatementType = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.TStatementType;
  25. TDBEventType = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.TDBEventType;
  26. TDBEventTypes = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.TDBEventTypes;
  27. TQuoteChars = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.TQuoteChars;
  28. const
  29. StatementTokens : Array[TStatementType] of string = ('(unknown)', 'select',
  30. 'insert', 'update', 'delete',
  31. 'create', 'get', 'put', 'execute',
  32. 'start','commit','rollback', '?'
  33. );
  34. TSchemaObjectNames: array[TSchemaType] of String = ('???', 'table_name',
  35. '???', 'procedure_name', 'column_name', 'param_name',
  36. 'index_name', 'package_name', 'schema_name','sequence');
  37. SingleQuotes : TQuoteChars = ('''','''');
  38. DoubleQuotes : TQuoteChars = ('"','"');
  39. LogAllEvents = [detCustom, detPrepare, detExecute, detFetch, detCommit, detRollBack];
  40. LogAllEventsExtra = [detCustom, detPrepare, detExecute, detFetch, detCommit, detRollBack, detParamValue,detActualSQL];
  41. // Backwards compatibility alias constants.
  42. stNoSchema = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stNoSchema;
  43. stTables = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stTables;
  44. stSysTables = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stSysTables;
  45. stProcedures = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stProcedures;
  46. stColumns = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stColumns;
  47. stProcedureParams = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stProcedureParams;
  48. stIndexes = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stIndexes;
  49. stPackages = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stPackages;
  50. stSchemata = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stSchemata;
  51. stSequences = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stSequences;
  52. stUnknown = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stUnknown;
  53. stSelect = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stSelect;
  54. stInsert = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stInsert;
  55. stUpdate = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stUpdate;
  56. stDelete = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stDelete;
  57. stDDL = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stDDL;
  58. stGetSegment = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stGetSegment;
  59. stPutSegment = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stPutSegment;
  60. stExecProcedure = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stExecProcedure;
  61. stStartTrans = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stStartTrans;
  62. stCommit = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stCommit;
  63. stRollback = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stRollback;
  64. stSelectForUpd = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.stSelectForUpd;
  65. detCustom = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.detCustom;
  66. detPrepare = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.detPrepare;
  67. detExecute = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.detExecute;
  68. detFetch = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.detFetch;
  69. detCommit = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.detCommit;
  70. detRollBack = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.detRollBack;
  71. detParamValue = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.detParamValue;
  72. detActualSQL = {$IFDEF FPC_DOTTEDUNITS}Data.Sql.Types{$ELSE}sqltypes{$ENDIF}.detActualSQL;
  73. DefaultMacroChar = '%';
  74. Type
  75. TRowsCount = LargeInt;
  76. TSQLStatementInfo = Record
  77. StatementType : TStatementType;
  78. TableName : String;
  79. Updateable : Boolean;
  80. WhereStartPos ,
  81. WhereStopPos : integer;
  82. end;
  83. TSQLConnection = class;
  84. TSQLTransaction = class;
  85. TCustomSQLQuery = class;
  86. TCustomSQLStatement = Class;
  87. TSQLQuery = class;
  88. TSQLScript = class;
  89. TSQLHandle = Class(TObject)
  90. end;
  91. { TSQLCursor }
  92. TSQLCursor = Class(TSQLHandle)
  93. public
  94. FDirect : Boolean;
  95. FPrepared : Boolean;
  96. FSelectable : Boolean;
  97. FInitFieldDef : Boolean;
  98. FStatementType : TStatementType;
  99. FSchemaType : TSchemaType;
  100. end;
  101. { ESQLDatabaseError}
  102. ESQLDatabaseError = class(EDatabaseError)
  103. public
  104. ErrorCode: integer;
  105. SQLState : string;
  106. constructor CreateFmt(const Fmt: string; const Args: array of const;
  107. Comp : TComponent; AErrorCode: integer; const ASQLState: string); overload;
  108. end;
  109. { TSQLDBFieldDef }
  110. TSQLDBFieldDef = Class(TFieldDef)
  111. private
  112. FData: Pointer;
  113. Public
  114. Property SQLDBData : Pointer Read FData Write FData;
  115. end;
  116. { TSQLDBFieldDefs }
  117. TSQLDBFieldDefs = Class(TFieldDefs)
  118. Protected
  119. Class Function FieldDefClass : TFieldDefClass; override;
  120. end;
  121. { TSQLDBParam }
  122. TSQLDBParam = Class(TParam)
  123. private
  124. FFieldDef: TFieldDef;
  125. FData : Pointer;
  126. Public
  127. Property FieldDef : TFieldDef Read FFieldDef Write FFieldDef;
  128. Property SQLDBData : Pointer Read FData Write FData;
  129. end;
  130. { TSQLDBParams }
  131. TSQLDBParams = Class(TParams)
  132. Protected
  133. Class Function ParamClass : TParamClass; override;
  134. end;
  135. type
  136. { TServerIndexDefs }
  137. TServerIndexDefs = class(TIndexDefs)
  138. Private
  139. public
  140. constructor Create(ADataSet: TDataSet); override;
  141. procedure Update; override;
  142. end;
  143. type
  144. { TSQLConnection }
  145. TDBLogNotifyEvent = Procedure (Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String) of object;
  146. TConnOption = (sqSupportParams, sqSupportEmptyDatabaseName, sqEscapeSlash, sqEscapeRepeat, sqImplicitTransaction, sqLastInsertID, sqSupportReturning,sqSequences, sqCommitEndsPrepared, sqRollbackEndsPrepared);
  147. TConnOptions= set of TConnOption;
  148. TSQLConnectionOption = (scoExplicitConnect, scoApplyUpdatesChecksRowsAffected);
  149. TSQLConnectionOptions = Set of TSQLConnectionOption;
  150. TConnInfoType=(citAll=-1, citServerType, citServerVersion, citServerVersionString, citClientName, citClientVersion);
  151. TSQLConnection = class (TDatabase)
  152. private
  153. FFieldNameQuoteChars : TQuoteChars;
  154. FOptions : TSQLConnectionOptions;
  155. FPassword : string;
  156. FTransaction : TSQLTransaction;
  157. FUserName : string;
  158. FHostName : string;
  159. FCharSet : string;
  160. FCodePage : TSystemCodePage;
  161. FRole : String;
  162. FStatements : TThreadList;
  163. FLogEvents: TDBEventTypes;
  164. FOnLog: TDBLogNotifyEvent;
  165. function GetPort: cardinal;
  166. procedure SetOptions(AValue: TSQLConnectionOptions);
  167. procedure SetPort(const AValue: cardinal);
  168. function AttemptCommit(trans : TSQLHandle) : boolean;
  169. function AttemptRollBack(trans : TSQLHandle) : boolean;
  170. protected
  171. FConnOptions : TConnOptions;
  172. FSQLFormatSettings : TFormatSettings;
  173. // Updating of DB records is moved out of TSQLQuery.
  174. // It is done here, so descendents can override it and implement DB-specific.
  175. // One day, this may be factored out to a TSQLResolver class.
  176. // The following allow construction of update queries. They can be adapted as needed by descendents to fit the DB engine.
  177. procedure AddFieldToUpdateWherePart(var sql_where: string; UpdateMode : TUpdateMode; F: TField); virtual;
  178. function ConstructInsertSQL(Query: TCustomSQLQuery; Var ReturningClause : Boolean): string; virtual;
  179. function ConstructUpdateSQL(Query: TCustomSQLQuery; Var ReturningClause : Boolean): string; virtual;
  180. function ConstructDeleteSQL(Query: TCustomSQLQuery): string; virtual;
  181. function ConstructRefreshSQL(Query: TCustomSQLQuery; UpdateKind : TUpdateKind): string; virtual;
  182. // factory function used to create custom statements
  183. function CreateCustomQuery(aOwner: TComponent): TCustomSQLQuery; virtual;
  184. function InitialiseUpdateStatement(Query: TCustomSQLQuery; var qry: TCustomSQLQuery): TCustomSQLQuery;
  185. procedure ApplyFieldUpdate(C : TSQLCursor; P: TSQLDBParam; F: TField; UseOldValue: Boolean); virtual;
  186. // This is the call that updates a record, it used to be in TSQLQuery.
  187. procedure ApplyRecUpdate(Query : TCustomSQLQuery; UpdateKind : TUpdateKind); virtual;
  188. function RefreshLastInsertID(Query : TCustomSQLQuery; Field : TField): Boolean; virtual;
  189. procedure GetDBInfo(const ASchemaType : TSchemaType; const ASchemaObjectName, AReturnField : string; AList: TStrings);
  190. function PortParamName: string; virtual;
  191. function GetConnectionCharSet: string; virtual;
  192. procedure SetTransaction(Value : TSQLTransaction); virtual;
  193. procedure DoConnect; override;
  194. procedure DoInternalConnect; override;
  195. procedure DoInternalDisconnect; override;
  196. function GetAsString(Param: TParam): RawByteString;
  197. function GetAsSQLText(Field : TField) : string; overload; virtual;
  198. function GetAsSQLText(Param : TParam) : string; overload; virtual;
  199. function GetHandle : pointer; virtual;
  200. Function LogEvent(EventType : TDBEventType) : Boolean;
  201. Procedure LogParams(Const AParams : TParams); virtual;
  202. Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
  203. Procedure RegisterStatement(S : TCustomSQLStatement);
  204. Procedure UnRegisterStatement(S : TCustomSQLStatement);
  205. Procedure UnPrepareStatements(aTransaction : TSQLTransaction);
  206. Function AllocateCursorHandle : TSQLCursor; virtual; abstract;
  207. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); virtual; abstract;
  208. function StrToStatementType(s : string) : TStatementType; virtual;
  209. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); virtual; abstract;
  210. procedure UnPrepareStatement(cursor : TSQLCursor); virtual; abstract;
  211. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); virtual; abstract;
  212. function RowsAffected(cursor: TSQLCursor): TRowsCount; virtual;
  213. function Fetch(cursor : TSQLCursor) : boolean; virtual; abstract;
  214. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TFieldDefs); virtual; abstract;
  215. function AddFieldDef(AFieldDefs: TFieldDefs; AFieldNo: Longint; const AName: string; ADataType: TFieldType; ASize, APrecision: Integer; AByteSize, ARequired, AReadOnly: Boolean): TFieldDef;
  216. function LoadField(cursor : TSQLCursor; FieldDef : TFieldDef; buffer : pointer; out CreateBlob : boolean) : boolean; virtual; abstract;
  217. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); virtual; abstract;
  218. procedure FreeFldBuffers(cursor : TSQLCursor); virtual;
  219. Function AllocateTransactionHandle : TSQLHandle; virtual; abstract;
  220. function GetTransactionHandle(trans : TSQLHandle): pointer; virtual; abstract;
  221. function Commit(trans : TSQLHandle) : boolean; virtual; abstract;
  222. function RollBack(trans : TSQLHandle) : boolean; virtual; abstract;
  223. function StartImplicitTransaction(trans : TSQLHandle; aParams : string) : boolean; virtual;
  224. function StartDBTransaction(trans : TSQLHandle; aParams : string) : boolean; virtual; abstract;
  225. procedure CommitRetaining(trans : TSQLHandle); virtual; abstract;
  226. procedure RollBackRetaining(trans : TSQLHandle); virtual; abstract;
  227. procedure UpdateIndexDefs(IndexDefs : TIndexDefs; TableName : string); virtual;
  228. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; virtual;
  229. function GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string; virtual;
  230. Procedure MaybeConnect;
  231. Property Statements : TThreadList Read FStatements;
  232. property Port: cardinal read GetPort write SetPort;
  233. property CodePage: TSystemCodePage read FCodePage;
  234. public
  235. constructor Create(AOwner: TComponent); override;
  236. destructor Destroy; override;
  237. procedure StartTransaction; override;
  238. procedure EndTransaction; override;
  239. procedure ExecuteDirect(SQL : String); overload; virtual;
  240. procedure ExecuteDirect(SQL : String; ATransaction : TSQLTransaction); overload; virtual;
  241. // Unified version
  242. function GetObjectNames(ASchemaType: TSchemaType; AList : TSqlObjectIdentifierList): Integer; virtual;
  243. // Older versions.
  244. Function HasTable(const aTable : String; SearchSystemTables : Boolean = false) : Boolean;
  245. procedure GetTableNames(List : TStrings; SystemTables : Boolean = false); virtual;
  246. procedure GetProcedureNames(List : TStrings); virtual;
  247. procedure GetFieldNames(const TableName : string; List : TStrings); virtual;
  248. procedure GetSchemaNames(List: TStrings); virtual;
  249. procedure GetSequenceNames(List: TStrings); virtual;
  250. function GetConnectionInfo(InfoType:TConnInfoType): string; virtual;
  251. function GetStatementInfo(const ASQL: string): TSQLStatementInfo; virtual;
  252. procedure CreateDB; virtual;
  253. procedure DropDB; virtual;
  254. function GetNextValue(const SequenceName: string; IncrementBy: integer=1): Int64; virtual;
  255. property ConnOptions: TConnOptions read FConnOptions;
  256. property Handle: Pointer read GetHandle;
  257. property FieldNameQuoteChars: TQuoteChars read FFieldNameQuoteChars write FFieldNameQuoteChars;
  258. published
  259. property Password : string read FPassword write FPassword;
  260. property Transaction : TSQLTransaction read FTransaction write SetTransaction;
  261. property UserName : string read FUserName write FUserName;
  262. property CharSet : string read FCharSet write FCharSet;
  263. property HostName : string Read FHostName Write FHostName;
  264. Property OnLog : TDBLogNotifyEvent Read FOnLog Write FOnLog;
  265. Property LogEvents : TDBEventTypes Read FLogEvents Write FLogEvents Default LogAllEvents;
  266. Property Options : TSQLConnectionOptions Read FOptions Write SetOptions default [];
  267. Property Role : String read FRole write FRole;
  268. property Connected;
  269. property DatabaseName;
  270. property KeepConnection;
  271. property LoginPrompt;
  272. property Params;
  273. property OnLogin;
  274. end;
  275. { TSQLTransaction }
  276. TCommitRollbackAction = (caNone, caCommit, caCommitRetaining, caRollback,
  277. caRollbackRetaining);
  278. TSQLTransactionOption = (stoUseImplicit, stoExplicitStart);
  279. TSQLTransactionOptions = Set of TSQLTransactionOption;
  280. TSQLTransaction = class (TDBTransaction)
  281. private
  282. FOptions : TSQLTransactionOptions;
  283. FTrans : TSQLHandle;
  284. FAction : TCommitRollbackAction;
  285. FParams : TStringList;
  286. function GetSQLConnection: TSQLConnection;
  287. procedure SetOptions(AValue: TSQLTransactionOptions);
  288. procedure SetParams(const AValue: TStringList);
  289. procedure SetSQLConnection(AValue: TSQLConnection);
  290. protected
  291. Procedure UnPrepareStatements; virtual;
  292. Procedure MaybeStartTransaction;
  293. Function AllowClose(DS: TDBDataset): Boolean; override;
  294. procedure CloseDataset(DS: TDBDataset; InCommit : Boolean); override;
  295. function GetHandle : Pointer; virtual;
  296. Procedure SetDatabase (Value : TDatabase); override;
  297. Function LogEvent(EventType : TDBEventType) : Boolean;
  298. Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
  299. public
  300. constructor Create(AOwner : TComponent); override;
  301. destructor Destroy; override;
  302. procedure Commit; override;
  303. procedure CommitRetaining; override;
  304. procedure Rollback; override;
  305. procedure RollbackRetaining; override;
  306. procedure StartTransaction; override;
  307. procedure EndTransaction; override;
  308. property Handle: Pointer read GetHandle;
  309. Property SQLConnection : TSQLConnection Read GetSQLConnection Write SetSQLConnection;
  310. published
  311. property Action : TCommitRollbackAction read FAction write FAction Default caRollBack;
  312. property Database;
  313. property Params : TStringList read FParams write SetParams;
  314. Property Options : TSQLTransactionOptions Read FOptions Write SetOptions default [];
  315. end;
  316. { TCustomSQLStatement }
  317. TCustomSQLStatement = Class(TComponent)
  318. Private
  319. FCursor : TSQLCursor;
  320. FDatabase: TSQLConnection;
  321. FOnSQLChanged: TNotifyEvent;
  322. FParamCheck: Boolean;
  323. FParams: TParams;
  324. FMacroCheck: Boolean;
  325. FMacroChar: AnsiChar;
  326. FMacros: TParams;
  327. FSQL: TStrings;
  328. FOrigSQL : String;
  329. FServerSQL : String;
  330. FTransaction: TSQLTransaction;
  331. FParseSQL: Boolean;
  332. FDoUnPrepare : Boolean;
  333. FDataLink : TDataLink;
  334. FRowsAffected : TRowsCount;
  335. FInfoquery : Boolean;
  336. function ExpandMacros(const OrigSQL: String): String;
  337. procedure SetDatabase(AValue: TSQLConnection);
  338. procedure SetMacroChar(AValue: AnsiChar);
  339. procedure SetMacroCheck(AValue: Boolean);
  340. procedure SetParams(AValue: TParams);
  341. procedure SetMacros(AValue: TParams);
  342. procedure SetSQL(AValue: TStrings);
  343. procedure SetTransaction(AValue: TSQLTransaction);
  344. procedure RecreateMacros;
  345. Function GetPrepared : Boolean;
  346. Procedure CheckUnprepare;
  347. Procedure CheckPrepare;
  348. Function HasParams : Boolean;
  349. Function HasMacros : Boolean;
  350. Protected
  351. Function CreateDataLink : TDataLink; virtual;
  352. procedure OnChangeSQL(Sender : TObject); virtual;
  353. function GetDataSource: TDataSource; Virtual;
  354. procedure SetDataSource(AValue: TDataSource); virtual;
  355. Procedure CopyParamsFromMaster(CopyBound : Boolean); virtual;
  356. procedure AllocateCursor;
  357. procedure DeAllocateCursor;
  358. Function GetSchemaType : TSchemaType; virtual;
  359. Function GetSchemaObjectName : String; virtual;
  360. Function GetSchemaPattern: String; virtual;
  361. Function IsSelectable : Boolean ; virtual;
  362. procedure GetStatementInfo(var ASQL: String; out Info: TSQLStatementInfo); virtual;
  363. Procedure DoExecute; virtual;
  364. procedure DoPrepare; virtual;
  365. procedure DoUnPrepare; virtual;
  366. Function CreateParams : TSQLDBParams; virtual;
  367. Function LogEvent(EventType : TDBEventType) : Boolean;
  368. Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
  369. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  370. Property Cursor : TSQLCursor read FCursor;
  371. Property Database : TSQLConnection Read FDatabase Write SetDatabase;
  372. Property Transaction : TSQLTransaction Read FTransaction Write SetTransaction;
  373. Property SQL : TStrings Read FSQL Write SetSQL;
  374. Property Params : TParams Read FParams Write SetParams stored HasParams;
  375. Property Macros : TParams Read FMacros Write SetMacros stored HasMacros;
  376. property MacroChar: AnsiChar read FMacroChar write SetMacroChar default DefaultMacroChar;
  377. Property DataSource : TDataSource Read GetDataSource Write SetDataSource;
  378. Property ParseSQL : Boolean Read FParseSQL Write FParseSQL;
  379. Property ParamCheck : Boolean Read FParamCheck Write FParamCheck default true;
  380. Property MacroCheck : Boolean Read FMacroCheck Write SetMacroCheck default false;
  381. Property InfoQuery : Boolean Read FInfoQuery Write FInfoQuery;
  382. Property OnSQLChanged : TNotifyEvent Read FOnSQLChanged Write FOnSQLChanged;
  383. Public
  384. constructor Create(AOwner : TComponent); override;
  385. destructor Destroy; override;
  386. Procedure Prepare;
  387. Procedure Execute;
  388. Procedure Unprepare;
  389. function ParamByName(Const AParamName : String) : TParam;
  390. function RowsAffected: TRowsCount; virtual;
  391. Property Prepared : boolean read GetPrepared;
  392. end;
  393. TSQLStatement = Class(TCustomSQLStatement)
  394. Published
  395. Property Database;
  396. Property DataSource;
  397. Property ParamCheck;
  398. Property Params;
  399. Property MacroCheck;
  400. Property Macros;
  401. Property ParseSQL;
  402. Property SQL;
  403. Property Transaction;
  404. end;
  405. { TSQLSequence }
  406. TSQLSequenceApplyEvent = (saeOnNewRecord, saeOnPost);
  407. TSQLSequence = class(TPersistent)
  408. private
  409. FQuery: TCustomSQLQuery;
  410. FFieldName: String;
  411. FSequenceName: String;
  412. FIncrementBy: Integer;
  413. FApplyEvent: TSQLSequenceApplyEvent;
  414. public
  415. constructor Create(AQuery: TCustomSQLQuery);
  416. procedure Assign(Source: TPersistent); override;
  417. procedure Apply;
  418. function GetNextValue: Int64;
  419. published
  420. property FieldName: String read FFieldName write FFieldName;
  421. property SequenceName: String read FSequenceName write FSequenceName;
  422. property IncrementBy: Integer read FIncrementBy write FIncrementBy default 1;
  423. property ApplyEvent: TSQLSequenceApplyEvent read FApplyEvent write FApplyEvent default saeOnNewRecord;
  424. end;
  425. { TCustomSQLQuery }
  426. TSQLQueryOption = (sqoKeepOpenOnCommit, sqoAutoApplyUpdates, sqoAutoCommit, sqoCancelUpdatesOnRefresh, sqoRefreshUsingSelect, sqoNoCloseOnSQLChange);
  427. TSQLQueryOptions = Set of TSQLQueryOption;
  428. TCustomSQLQuery = class (TCustomBufDataset)
  429. private
  430. FOptions : TSQLQueryOptions;
  431. FSchemaType : TSchemaType;
  432. FUpdateable : boolean;
  433. FTableName : string;
  434. FStatement : TCustomSQLStatement;
  435. FInsertSQL,
  436. FUpdateSQL,
  437. FDeleteSQL,
  438. FRefreshSQL : TStringList;
  439. FIsEOF : boolean;
  440. FLoadingFieldDefs : boolean;
  441. FUpdateMode : TUpdateMode;
  442. FDoUnprepare : Boolean;
  443. FusePrimaryKeyAsKey : Boolean;
  444. FWhereStartPos : integer;
  445. FWhereStopPos : integer;
  446. FServerFilterText : string;
  447. FServerFiltered : Boolean;
  448. FServerIndexDefs : TServerIndexDefs;
  449. // Used by SetSchemaType
  450. FSchemaObjectName : string;
  451. FSchemaPattern : string;
  452. FInsertQry,
  453. FUpdateQry,
  454. FDeleteQry : TCustomSQLQuery;
  455. FSequence : TSQLSequence;
  456. procedure CheckPrepare(InfoQuery : Boolean);
  457. procedure CheckUnPrepare;
  458. procedure FreeFldBuffers;
  459. function GetMacroChar: AnsiChar;
  460. function GetParamCheck: Boolean;
  461. function GetParams: TParams;
  462. function GetMacroCheck: Boolean;
  463. function GetMacros: TParams;
  464. function GetParseSQL: Boolean;
  465. function GetServerIndexDefs: TServerIndexDefs;
  466. function GetSQL: TStringList;
  467. function GetSQLConnection: TSQLConnection;
  468. function GetSQLTransaction: TSQLTransaction;
  469. function GetStatementType : TStatementType;
  470. function HasMacros: Boolean;
  471. Function HasParams : Boolean;
  472. Function NeedLastInsertID: TField;
  473. procedure OnChangeSelectSQL(Sender: TObject);
  474. procedure SetMacroChar(AValue: AnsiChar);
  475. procedure SetOptions(AValue: TSQLQueryOptions);
  476. procedure SetParamCheck(AValue: Boolean);
  477. procedure SetMacroCheck(AValue: Boolean);
  478. procedure SetSQLConnection(AValue: TSQLConnection);
  479. procedure SetSQLTransaction(AValue: TSQLTransaction);
  480. procedure SetInsertSQL(const AValue: TStringList);
  481. procedure SetUpdateSQL(const AValue: TStringList);
  482. procedure SetDeleteSQL(const AValue: TStringList);
  483. procedure SetRefreshSQL(const AValue: TStringList);
  484. procedure SetParams(AValue: TParams);
  485. procedure SetMacros(AValue: TParams);
  486. procedure SetParseSQL(AValue : Boolean);
  487. procedure SetSQL(const AValue: TStringList);
  488. procedure SetUsePrimaryKeyAsKey(AValue : Boolean);
  489. procedure SetUpdateMode(AValue : TUpdateMode);
  490. procedure OnChangeModifySQL(Sender : TObject);
  491. procedure Execute;
  492. procedure ApplyFilter;
  493. Function AddFilter(const SQLstr : string) : string;
  494. protected
  495. procedure OpenCursor(InfoQuery: Boolean); override;
  496. function CreateSQLStatement(aOwner: TComponent): TCustomSQLStatement; virtual;
  497. Function CreateParams: TSQLDBParams; virtual;
  498. Function RefreshLastInsertID(Field: TField): Boolean; virtual;
  499. Function NeedRefreshRecord (UpdateKind: TUpdateKind): Boolean; virtual;
  500. Function RefreshRecord (UpdateKind: TUpdateKind) : Boolean; virtual;
  501. Procedure ApplyReturningResult(Q : TCustomSQLQuery; UpdateKind : TUpdateKind);
  502. Function Cursor : TSQLCursor;
  503. Function LogEvent(EventType : TDBEventType) : Boolean;
  504. Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
  505. // abstract & virtual methods of TBufDataset
  506. function Fetch : boolean; override;
  507. function LoadField(FieldDef : TFieldDef; buffer : pointer; out CreateBlob : boolean) : boolean; override;
  508. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField); override;
  509. procedure ApplyRecUpdate(UpdateKind : TUpdateKind); override;
  510. procedure SetPacketRecords(aValue : integer); override;
  511. // abstract & virtual methods of TDataset
  512. procedure UpdateServerIndexDefs; virtual;
  513. procedure SetDatabase(Value : TDatabase); override;
  514. Procedure SetTransaction(Value : TDBTransaction); override;
  515. procedure InternalAddRecord(Buffer: Pointer; AAppend: Boolean); override;
  516. procedure InternalClose; override;
  517. procedure InternalInitFieldDefs; override;
  518. procedure InternalOpen; override;
  519. Procedure InternalRefresh; override;
  520. function GetCanModify: Boolean; override;
  521. Function IsPrepared : Boolean; virtual;
  522. procedure SetServerFiltered(Value: Boolean); virtual;
  523. procedure SetServerFilterText(const Value: string); virtual;
  524. Function GetDataSource : TDataSource; override;
  525. Procedure SetDataSource(AValue : TDataSource);
  526. procedure BeforeRefreshOpenCursor; override;
  527. procedure SetReadOnly(AValue : Boolean); override;
  528. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  529. procedure DoOnNewRecord; override;
  530. procedure DoBeforePost; override;
  531. class function FieldDefsClass : TFieldDefsClass; override;
  532. // IProviderSupport methods
  533. function PSGetUpdateException(E: Exception; Prev: EUpdateError): EUpdateError; override;
  534. function PSGetTableName: string; override;
  535. Property TableName : String Read FTableName Write FTableName; // alternative: procedure DoGetTableName
  536. public
  537. constructor Create(AOwner : TComponent); override;
  538. destructor Destroy; override;
  539. procedure Prepare; virtual;
  540. procedure UnPrepare; virtual;
  541. procedure ExecSQL; virtual;
  542. procedure SetSchemaInfo( ASchemaType : TSchemaType; ASchemaObjectName, ASchemaPattern : string); virtual;
  543. function RowsAffected: TRowsCount; virtual;
  544. function ParamByName(Const AParamName : String) : TParam;
  545. function MacroByName(Const AParamName : String) : TParam;
  546. Property Prepared : boolean read IsPrepared;
  547. Property SQLConnection : TSQLConnection Read GetSQLConnection Write SetSQLConnection;
  548. Property SQLTransaction: TSQLTransaction Read GetSQLTransaction Write SetSQLTransaction;
  549. // overriden TBufDataSet methods
  550. Procedure ApplyUpdates(MaxErrors: Integer); override; overload;
  551. // overriden TDataSet methods
  552. Procedure Post; override;
  553. Procedure Delete; override;
  554. protected
  555. // redeclared TDataSet properties
  556. property Active;
  557. property Filter;
  558. property Filtered;
  559. property BeforeOpen;
  560. property AfterOpen;
  561. property BeforeClose;
  562. property AfterClose;
  563. property BeforeInsert;
  564. property AfterInsert;
  565. property BeforeEdit;
  566. property AfterEdit;
  567. property BeforePost;
  568. property AfterPost;
  569. property BeforeCancel;
  570. property AfterCancel;
  571. property BeforeDelete;
  572. property AfterDelete;
  573. property BeforeRefresh;
  574. property AfterRefresh;
  575. property BeforeScroll;
  576. property AfterScroll;
  577. property OnCalcFields;
  578. property OnDeleteError;
  579. property OnEditError;
  580. property OnFilterRecord;
  581. property OnNewRecord;
  582. property OnPostError;
  583. property AutoCalcFields;
  584. // protected
  585. property Database;
  586. property Transaction;
  587. property SchemaType : TSchemaType read FSchemaType default stNoSchema;
  588. property SQL : TStringlist read GetSQL write SetSQL;
  589. property InsertSQL : TStringList read FInsertSQL write SetInsertSQL;
  590. property UpdateSQL : TStringList read FUpdateSQL write SetUpdateSQL;
  591. property DeleteSQL : TStringList read FDeleteSQL write SetDeleteSQL;
  592. property RefreshSQL : TStringList read FRefreshSQL write SetRefreshSQL;
  593. Property Options : TSQLQueryOptions Read FOptions Write SetOptions default [];
  594. property Params : TParams read GetParams Write SetParams stored HasParams;
  595. Property ParamCheck : Boolean Read GetParamCheck Write SetParamCheck default true;
  596. property Macros : TParams read GetMacros Write SetMacros stored HasMacros;
  597. Property MacroCheck : Boolean Read GetMacroCheck Write SetMacroCheck default false;
  598. Property MacroChar : AnsiChar Read GetMacroChar Write SetMacroChar default DefaultMacroChar;
  599. property ParseSQL : Boolean read GetParseSQL write SetParseSQL default true;
  600. property UpdateMode : TUpdateMode read FUpdateMode write SetUpdateMode default upWhereKeyOnly;
  601. property UsePrimaryKeyAsKey : boolean read FUsePrimaryKeyAsKey write SetUsePrimaryKeyAsKey default true;
  602. property StatementType : TStatementType read GetStatementType;
  603. Property DataSource : TDataSource Read GetDataSource Write SetDataSource;
  604. property Sequence: TSQLSequence read FSequence write FSequence;
  605. property ServerFilter: string read FServerFilterText write SetServerFilterText;
  606. property ServerFiltered: Boolean read FServerFiltered write SetServerFiltered default False;
  607. property ServerIndexDefs : TServerIndexDefs read GetServerIndexDefs;
  608. end;
  609. { TSQLQuery }
  610. TSQLQuery = Class(TCustomSQLQuery)
  611. public
  612. property SchemaType;
  613. Property StatementType;
  614. Published
  615. property MaxIndexesCount;
  616. // TDataset stuff
  617. property FieldDefs;
  618. Property Active;
  619. Property AutoCalcFields;
  620. Property Filter;
  621. Property Filtered;
  622. Property AfterCancel;
  623. Property AfterClose;
  624. Property AfterDelete;
  625. Property AfterEdit;
  626. Property AfterInsert;
  627. Property AfterOpen;
  628. Property AfterPost;
  629. Property AfterRefresh;
  630. Property AfterScroll;
  631. Property BeforeCancel;
  632. Property BeforeClose;
  633. Property BeforeDelete;
  634. Property BeforeEdit;
  635. Property BeforeInsert;
  636. Property BeforeOpen;
  637. Property BeforePost;
  638. Property BeforeRefresh;
  639. Property BeforeScroll;
  640. Property OnCalcFields;
  641. Property OnDeleteError;
  642. Property OnEditError;
  643. Property OnFilterRecord;
  644. Property OnNewRecord;
  645. Property OnPostError;
  646. // property SchemaInfo default stNoSchema;
  647. property Database;
  648. property Transaction;
  649. property ReadOnly;
  650. property SQL;
  651. property InsertSQL;
  652. property UpdateSQL;
  653. property DeleteSQL;
  654. property RefreshSQL;
  655. property IndexDefs;
  656. Property Options;
  657. property Params;
  658. Property ParamCheck;
  659. property Macros;
  660. Property MacroCheck;
  661. Property MacroChar;
  662. property ParseSQL;
  663. property UpdateMode;
  664. property UsePrimaryKeyAsKey;
  665. Property DataSource;
  666. property Sequence;
  667. property ServerFilter;
  668. property ServerFiltered;
  669. property ServerIndexDefs;
  670. end;
  671. { TSQLScript }
  672. TSQLScript = class (TCustomSQLscript)
  673. private
  674. FOnDirective: TSQLScriptDirectiveEvent;
  675. FQuery : TCustomSQLQuery;
  676. FDatabase : TDatabase;
  677. FTransaction : TDBTransaction;
  678. protected
  679. procedure ExecuteStatement (SQLStatement: TStrings; var StopExecution: Boolean); override;
  680. procedure ExecuteDirective (Directive, Argument: String; var StopExecution: Boolean); override;
  681. procedure ExecuteCommit(CommitRetaining: boolean=true); override;
  682. Procedure SetDatabase (Value : TDatabase); virtual;
  683. Procedure SetTransaction(Value : TDBTransaction); virtual;
  684. Procedure CheckDatabase;
  685. function CreateQuery: TCustomSQLQuery; virtual;
  686. public
  687. constructor Create(AOwner : TComponent); override;
  688. destructor Destroy; override;
  689. procedure Execute; override;
  690. procedure ExecuteScript;
  691. Property Aborted;
  692. Property Line;
  693. published
  694. Property DataBase : TDatabase Read FDatabase Write SetDatabase;
  695. Property Transaction : TDBTransaction Read FTransaction Write SetTransaction;
  696. property OnDirective: TSQLScriptDirectiveEvent read FOnDirective write FOnDirective;
  697. Property AutoCommit;
  698. Property UseDollarString;
  699. Property DollarStrings;
  700. property Directives;
  701. property Defines;
  702. property Script;
  703. property Terminator;
  704. property CommentsinSQL;
  705. property UseSetTerm;
  706. property UseCommit;
  707. property UseDefines;
  708. property OnException;
  709. end;
  710. { TSQLConnector }
  711. TSQLConnector = Class(TSQLConnection)
  712. private
  713. FProxy : TSQLConnection;
  714. FConnectorType: String;
  715. procedure SetConnectorType(const AValue: String);
  716. protected
  717. procedure SetForcedClose(AValue: Boolean); override;
  718. procedure SetTransaction(Value : TSQLTransaction);override;
  719. procedure DoInternalConnect; override;
  720. procedure DoInternalDisconnect; override;
  721. Procedure CheckProxy;
  722. Procedure CreateProxy; virtual;
  723. Procedure FreeProxy; virtual;
  724. function StrToStatementType(s : string) : TStatementType; override;
  725. function GetAsSQLText(Field : TField) : string; overload; override;
  726. function GetAsSQLText(Param : TParam) : string; overload; override;
  727. function GetHandle : pointer; override;
  728. Function AllocateCursorHandle : TSQLCursor; override;
  729. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  730. Function AllocateTransactionHandle : TSQLHandle; override;
  731. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  732. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
  733. function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
  734. function Fetch(cursor : TSQLCursor) : boolean; override;
  735. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
  736. procedure UnPrepareStatement(cursor : TSQLCursor); override;
  737. function LoadField(cursor : TSQLCursor; FieldDef : TFieldDef; buffer : pointer; out CreateBlob : boolean) : boolean; override;
  738. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); override;
  739. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  740. function GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string; override;
  741. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  742. function Commit(trans : TSQLHandle) : boolean; override;
  743. function RollBack(trans : TSQLHandle) : boolean; override;
  744. function StartDBTransaction(trans : TSQLHandle; aParams : string) : boolean; override;
  745. procedure CommitRetaining(trans : TSQLHandle); override;
  746. procedure RollBackRetaining(trans : TSQLHandle); override;
  747. procedure UpdateIndexDefs(IndexDefs : TIndexDefs; TableName : string); override;
  748. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  749. Property Proxy : TSQLConnection Read FProxy;
  750. Published
  751. Property ConnectorType : String Read FConnectorType Write SetConnectorType;
  752. Property Port;
  753. end;
  754. TSQLConnectionClass = Class of TSQLConnection;
  755. { TConnectionDef }
  756. TLibraryLoadFunction = Function (Const S : AnsiString) : Integer;
  757. TLibraryUnLoadFunction = Procedure;
  758. TConnectionDef = Class(TPersistent)
  759. Class Function TypeName : String; virtual;
  760. Class Function ConnectionClass : TSQLConnectionClass; virtual;
  761. Class Function Description : String; virtual;
  762. Class Function DefaultLibraryName : String; virtual;
  763. Class Function LoadFunction : TLibraryLoadFunction; virtual;
  764. Class Function UnLoadFunction : TLibraryUnLoadFunction; virtual;
  765. Class Function LoadedLibraryName : string; virtual;
  766. Procedure ApplyParams(Params : TStrings; AConnection : TSQLConnection); virtual;
  767. end;
  768. TConnectionDefClass = class of TConnectionDef;
  769. Var
  770. GlobalDBLogHook : TDBLogNotifyEvent;
  771. Procedure RegisterConnection(Def : TConnectionDefClass);
  772. Procedure UnRegisterConnection(Def : TConnectionDefClass);
  773. Procedure UnRegisterConnection(const ConnectionName : String);
  774. Function GetConnectionDef(const ConnectorName : String) : TConnectionDef;
  775. Procedure GetConnectionList(List : TSTrings);
  776. const DefaultSQLFormatSettings : TFormatSettings = (
  777. CurrencyFormat: 1;
  778. NegCurrFormat: 5;
  779. ThousandSeparator: #0;
  780. DecimalSeparator: '.';
  781. CurrencyDecimals: 2;
  782. DateSeparator: '-';
  783. TimeSeparator: ':';
  784. ListSeparator: ' ';
  785. CurrencyString: '$';
  786. ShortDateFormat: 'yyyy-mm-dd';
  787. LongDateFormat: '';
  788. TimeAMString: '';
  789. TimePMString: '';
  790. ShortTimeFormat: 'hh:nn:ss';
  791. LongTimeFormat: 'hh:nn:ss.zzz';
  792. ShortMonthNames: ('','','','','','','','','','','','');
  793. LongMonthNames: ('','','','','','','','','','','','');
  794. ShortDayNames: ('','','','','','','');
  795. LongDayNames: ('','','','','','','');
  796. TwoDigitYearCenturyWindow: 50;
  797. );
  798. implementation
  799. {$IFDEF FPC_DOTTEDUNITS}
  800. uses Data.Consts, System.StrUtils;
  801. {$ELSE FPC_DOTTEDUNITS}
  802. uses dbconst, strutils;
  803. {$ENDIF FPC_DOTTEDUNITS}
  804. Const
  805. // Flags to check which fields must be refreshed.
  806. RefreshFlags : Array [ukModify..ukInsert] of TProviderFlag = (pfRefreshOnUpdate,pfRefreshOnInsert);
  807. function TimeIntervalToString(Time: TDateTime): string;
  808. var
  809. millisecond: word;
  810. second : word;
  811. minute : word;
  812. hour : word;
  813. begin
  814. DecodeTime(Time,hour,minute,second,millisecond);
  815. hour := hour + trunc(Time)*24;
  816. Result := Format('%.2d:%.2d:%.2d.%.3d',[hour,minute,second,millisecond]);
  817. end;
  818. { TSQLDBFieldDefs }
  819. class function TSQLDBFieldDefs.FieldDefClass: TFieldDefClass;
  820. begin
  821. Result:=TSQLDBFieldDef;
  822. end;
  823. { TSQLDBParams }
  824. class function TSQLDBParams.ParamClass: TParamClass;
  825. begin
  826. Result:=TSQLDBParam;
  827. end;
  828. { ESQLDatabaseError }
  829. constructor ESQLDatabaseError.CreateFmt(const Fmt: string; const Args: array of const;
  830. Comp: TComponent; AErrorCode: integer; const ASQLState: string);
  831. const CompNameFmt='%s : %s';
  832. var Msg: string;
  833. begin
  834. if not assigned(Comp) then
  835. Msg := Fmt
  836. else if Comp.Name = '' then
  837. Msg := Format(CompNameFmt, [Comp.ClassName,Fmt])
  838. else
  839. Msg := Format(CompNameFmt, [Comp.Name,Fmt]);
  840. if Length(Args) = 0 then
  841. inherited Create(Msg)
  842. else
  843. inherited CreateFmt(Msg, Args);
  844. ErrorCode := AErrorCode;
  845. SQLState := ASQLState;
  846. end;
  847. { TCustomSQLStatement }
  848. procedure TCustomSQLStatement.OnChangeSQL(Sender: TObject);
  849. var
  850. ConnOptions : TConnOptions;
  851. NewParams: TSQLDBParams;
  852. begin
  853. if Assigned(FOnSQLChanged) then
  854. FOnSQLChanged(Self);
  855. UnPrepare;
  856. RecreateMacros;
  857. if not ParamCheck then
  858. exit;
  859. if assigned(DataBase) then
  860. ConnOptions:=DataBase.ConnOptions
  861. else
  862. ConnOptions := [sqEscapeRepeat,sqEscapeSlash];
  863. NewParams := CreateParams;
  864. try
  865. NewParams.ParseSQL(FSQL.Text, True, sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions, psInterbase);
  866. NewParams.AssignValues(FParams);
  867. FParams.Assign(NewParams);
  868. finally
  869. NewParams.Free;
  870. end;
  871. end;
  872. procedure TCustomSQLStatement.SetDatabase(AValue: TSQLConnection);
  873. begin
  874. if FDatabase=AValue then Exit;
  875. UnPrepare;
  876. If Assigned(FDatabase) then
  877. begin
  878. FDatabase.UnregisterStatement(Self);
  879. FDatabase.RemoveFreeNotification(Self);
  880. end;
  881. FDatabase:=AValue;
  882. If Assigned(FDatabase) then
  883. begin
  884. FDatabase.FreeNotification(Self);
  885. FDatabase.RegisterStatement(Self);
  886. if Assigned(Database.Transaction) and (not Assigned(Transaction) or (Transaction.DataBase <> Database)) then
  887. Transaction := Database.Transaction;
  888. OnChangeSQL(Self);
  889. end;
  890. end;
  891. procedure TCustomSQLStatement.SetMacroChar(AValue: AnsiChar);
  892. begin
  893. if FMacroChar=AValue then Exit;
  894. FMacroChar:=AValue;
  895. RecreateMacros;
  896. end;
  897. procedure TCustomSQLStatement.SetMacroCheck(AValue: Boolean);
  898. begin
  899. if FMacroCheck=AValue then Exit;
  900. FMacroCheck:=AValue;
  901. RecreateMacros;
  902. end;
  903. procedure TCustomSQLStatement.SetTransaction(AValue: TSQLTransaction);
  904. begin
  905. if FTransaction=AValue then Exit;
  906. UnPrepare;
  907. if Assigned(FTransaction) then
  908. FTransaction.RemoveFreeNotification(Self);
  909. FTransaction:=AValue;
  910. if Assigned(FTransaction) then
  911. begin
  912. FTransaction.FreeNotification(Self);
  913. if Assigned(Transaction.DataBase) and (Database <> Transaction.DataBase) then
  914. Database := Transaction.DataBase as TSQLConnection;
  915. end;
  916. end;
  917. procedure TCustomSQLStatement.RecreateMacros;
  918. var
  919. NewParams: TSQLDBParams;
  920. ConnOptions: TConnOptions;
  921. PO : TSQLParseOptions;
  922. PB : TParamBinding;
  923. RS : String;
  924. begin
  925. if MacroCheck then begin
  926. if assigned(DataBase) then
  927. ConnOptions:=DataBase.ConnOptions
  928. else
  929. ConnOptions := [sqEscapeRepeat,sqEscapeSlash];
  930. NewParams := CreateParams;
  931. try
  932. PO:=[spoCreate,spoUseMacro];
  933. if sqEscapeSlash in ConnOptions then
  934. Include(PO,spoEscapeSlash);
  935. if sqEscapeRepeat in ConnOptions then
  936. Include(PO,spoEscapeRepeat);
  937. NewParams.ParseSQL(FSQL.Text, PO, psInterbase, PB, MacroChar,RS);
  938. NewParams.AssignValues(FMacros);
  939. FMacros.Assign(NewParams);
  940. finally
  941. NewParams.Free;
  942. end;
  943. end;
  944. end;
  945. procedure TCustomSQLStatement.SetDataSource(AValue: TDataSource);
  946. begin
  947. if GetDataSource=AValue then Exit;
  948. if (FDataLink=Nil) then
  949. FDataLink:=CreateDataLink;
  950. FDataLink.DataSource:=AValue;
  951. end;
  952. procedure TCustomSQLStatement.CopyParamsFromMaster(CopyBound: Boolean);
  953. begin
  954. if Assigned(DataSource) and Assigned(DataSource.Dataset) then
  955. FParams.CopyParamValuesFromDataset(DataSource.Dataset,CopyBound);
  956. end;
  957. procedure TCustomSQLStatement.SetParams(AValue: TParams);
  958. begin
  959. if FParams=AValue then Exit;
  960. FParams.Assign(AValue);
  961. end;
  962. procedure TCustomSQLStatement.SetMacros(AValue: TParams);
  963. begin
  964. if FMacros=AValue then Exit;
  965. FMacros.Assign(AValue);
  966. end;
  967. procedure TCustomSQLStatement.SetSQL(AValue: TStrings);
  968. begin
  969. if FSQL=AValue then Exit;
  970. FSQL.Assign(AValue);
  971. RecreateMacros;
  972. end;
  973. procedure TCustomSQLStatement.DoExecute;
  974. begin
  975. FRowsAffected:=-1;
  976. If (FParams.Count>0) and Assigned(DataSource) then
  977. CopyParamsFromMaster(False);
  978. If LogEvent(detExecute) then
  979. Log(detExecute,FServerSQL);
  980. Database.Execute(FCursor,Transaction, FParams);
  981. end;
  982. function TCustomSQLStatement.GetPrepared: Boolean;
  983. begin
  984. Result := Assigned(FCursor) and (FCursor.FPrepared or FCursor.FDirect);
  985. end;
  986. procedure TCustomSQLStatement.CheckUnprepare;
  987. begin
  988. if FDoUnPrepare then
  989. begin
  990. UnPrepare;
  991. FDoUnPrepare:=False;
  992. end;
  993. end;
  994. procedure TCustomSQLStatement.CheckPrepare;
  995. begin
  996. if Not Prepared then
  997. begin
  998. FDoUnprepare:=True;
  999. Prepare;
  1000. end;
  1001. end;
  1002. function TCustomSQLStatement.CreateDataLink: TDataLink;
  1003. begin
  1004. Result:=TDataLink.Create;
  1005. end;
  1006. function TCustomSQLStatement.CreateParams: TSQLDBParams;
  1007. begin
  1008. Result:=TSQLDBParams.Create(Nil);
  1009. end;
  1010. Function TCustomSQLStatement.HasParams : Boolean;
  1011. begin
  1012. Result:=Params.Count>0;
  1013. end;
  1014. Function TCustomSQLStatement.HasMacros : Boolean;
  1015. begin
  1016. Result:=Macros.Count>0;
  1017. end;
  1018. function TCustomSQLStatement.LogEvent(EventType: TDBEventType): Boolean;
  1019. begin
  1020. Result:=Assigned(Database) and Database.LogEvent(EventType);
  1021. end;
  1022. procedure TCustomSQLStatement.Log(EventType: TDBEventType; const Msg: String);
  1023. Var
  1024. M : String;
  1025. begin
  1026. If LogEvent(EventType) then
  1027. begin
  1028. If (Name<>'') then
  1029. M:=Name
  1030. else
  1031. M:=ClassName;
  1032. Database.Log(EventType,M+' : '+Msg);
  1033. end;
  1034. end;
  1035. procedure TCustomSQLStatement.Notification(AComponent: TComponent;
  1036. Operation: TOperation);
  1037. begin
  1038. inherited Notification(AComponent, Operation);
  1039. if (operation=opRemove) then
  1040. If (AComponent=FTransaction) then
  1041. FTransaction:=Nil
  1042. else if (AComponent=FDatabase) then
  1043. begin
  1044. UnPrepare;
  1045. FDatabase:=Nil;
  1046. end;
  1047. end;
  1048. constructor TCustomSQLStatement.Create(AOwner: TComponent);
  1049. begin
  1050. inherited Create(AOwner);
  1051. FSQL:=TStringList.Create;
  1052. TStringList(FSQL).OnChange:=@OnChangeSQL;
  1053. FParams:=CreateParams;
  1054. FParamCheck:=True;
  1055. FMacros:=CreateParams;
  1056. FMacroChar:=DefaultMacroChar;
  1057. FMacroCheck:=False;
  1058. FParseSQL:=True;
  1059. FRowsAffected:=-1;
  1060. end;
  1061. destructor TCustomSQLStatement.Destroy;
  1062. begin
  1063. UnPrepare;
  1064. Transaction:=Nil;
  1065. Database:=Nil;
  1066. DataSource:=Nil;
  1067. FreeAndNil(FDataLink);
  1068. FreeAndNil(FParams);
  1069. FreeAndNil(FMacros);
  1070. FreeAndNil(FSQL);
  1071. inherited Destroy;
  1072. end;
  1073. function TCustomSQLStatement.GetSchemaType: TSchemaType;
  1074. begin
  1075. Result:=stNoSchema
  1076. end;
  1077. function TCustomSQLStatement.GetSchemaObjectName: String;
  1078. begin
  1079. Result:='';
  1080. end;
  1081. function TCustomSQLStatement.GetSchemaPattern: String;
  1082. begin
  1083. Result:='';
  1084. end;
  1085. function TCustomSQLStatement.IsSelectable: Boolean;
  1086. begin
  1087. Result:=False;
  1088. end;
  1089. procedure TCustomSQLStatement.GetStatementInfo(var ASQL: String; out Info: TSQLStatementInfo);
  1090. begin
  1091. Info:=Database.GetStatementInfo(ASQL);
  1092. end;
  1093. procedure TCustomSQLStatement.AllocateCursor;
  1094. begin
  1095. if not assigned(FCursor) then
  1096. // Do this as late as possible.
  1097. FCursor:=Database.AllocateCursorHandle;
  1098. end;
  1099. procedure TCustomSQLStatement.DeAllocateCursor;
  1100. begin
  1101. if Assigned(FCursor) then
  1102. begin
  1103. if Assigned(Database) then
  1104. DataBase.DeAllocateCursorHandle(FCursor);
  1105. FreeAndNil(FCursor);
  1106. end;
  1107. end;
  1108. function TCustomSQLStatement.ExpandMacros(const OrigSQL : String ) : String;
  1109. Const
  1110. Terminators = SQLDelimiterCharacters+
  1111. [ #0,'=','+','-','*','\','/','[',']','|' ];
  1112. var
  1113. I: Integer;
  1114. Ch : AnsiChar;
  1115. TermArr : Set of AnsiChar;
  1116. TempStr, TempMacroName : String;
  1117. MacroFlag : Boolean;
  1118. Procedure SubstituteMacro;
  1119. var
  1120. Param: TParam;
  1121. begin
  1122. Param := Macros.FindParam( TempMacroName );
  1123. if Assigned( Param ) then
  1124. Result := Result + Param.AsString
  1125. else
  1126. Result := Result + MacroChar + TempMacroName;
  1127. TempMacroName:='';
  1128. end;
  1129. begin
  1130. Result := OrigSQL;
  1131. if not MacroCheck then
  1132. Exit;
  1133. TermArr := Terminators +[MacroChar];
  1134. Result := '';
  1135. MacroFlag := False;
  1136. for Ch in OrigSQL do
  1137. begin
  1138. if not MacroFlag and (Ch=MacroChar) then
  1139. begin
  1140. MacroFlag := True;
  1141. TempMacroName := '';
  1142. end
  1143. else if MacroFlag then
  1144. begin
  1145. if not (Ch In TermArr) then
  1146. TempMacroName := TempMacroName + Ch
  1147. else
  1148. begin
  1149. SubstituteMacro;
  1150. if Ch <> MacroChar then
  1151. MacroFlag := False;
  1152. TempMacroName := '';
  1153. end
  1154. end;
  1155. if not MacroFlag then
  1156. Result := Result + Ch;
  1157. end;
  1158. if (TempMacroName<>'') then
  1159. SubstituteMacro;
  1160. end;
  1161. procedure TCustomSQLStatement.DoPrepare;
  1162. var
  1163. StmInfo: TSQLStatementInfo;
  1164. begin
  1165. if GetSchemaType=stNoSchema then
  1166. FOrigSQL := TrimRight(FSQL.Text)
  1167. else
  1168. FOrigSQL := Database.GetSchemaInfoSQL(GetSchemaType, GetSchemaObjectName, GetSchemaPattern);
  1169. if (FOrigSQL='') then
  1170. DatabaseError(SErrNoStatement);
  1171. FServerSQL:=ExpandMacros( FOrigSQL );
  1172. GetStatementInfo(FServerSQL,StmInfo);
  1173. AllocateCursor;
  1174. if FInfoquery then
  1175. // We signal that we want to have field definitions.
  1176. // This is needed for e.g. postgres, where the result of the prepare statement is not enough to get field defs.
  1177. FCursor.FInitFieldDef:=True;
  1178. FCursor.FSelectable:=True; // let PrepareStatement and/or Execute alter it
  1179. FCursor.FStatementType:=StmInfo.StatementType;
  1180. FCursor.FSchemaType:=GetSchemaType;
  1181. If LogEvent(detPrepare) then
  1182. Log(detPrepare,FServerSQL);
  1183. Database.PrepareStatement(FCursor,Transaction,FServerSQL,FParams);
  1184. // Update FInitFieldDef with whatever we got from the server.
  1185. FCursor.FInitFieldDef:=FCursor.FSelectable;
  1186. end;
  1187. procedure TCustomSQLStatement.Prepare;
  1188. begin
  1189. if Prepared then
  1190. exit;
  1191. if not assigned(Database) then
  1192. DatabaseError(SErrDatabasenAssigned);
  1193. if not assigned(Transaction) then
  1194. DatabaseError(SErrTransactionnSet);
  1195. Database.MaybeConnect;
  1196. if not Transaction.Active then
  1197. Transaction.MaybeStartTransaction;
  1198. try
  1199. DoPrepare;
  1200. except
  1201. DeAllocateCursor;
  1202. Raise;
  1203. end;
  1204. end;
  1205. procedure TCustomSQLStatement.Execute;
  1206. begin
  1207. CheckPrepare;
  1208. try
  1209. DoExecute;
  1210. finally
  1211. CheckUnPrepare;
  1212. end;
  1213. end;
  1214. procedure TCustomSQLStatement.DoUnPrepare;
  1215. begin
  1216. If Assigned(FCursor) then
  1217. If Assigned(Database) then
  1218. begin
  1219. DataBase.UnPrepareStatement(FCursor);
  1220. DeAllocateCursor;
  1221. end
  1222. else // this should never happen. It means a cursor handle leaks in the DB itself.
  1223. FreeAndNil(FCursor);
  1224. end;
  1225. function TCustomSQLStatement.GetDataSource: TDataSource;
  1226. begin
  1227. if Assigned(FDataLink) then
  1228. Result:=FDataLink.DataSource
  1229. else
  1230. Result:=Nil;
  1231. end;
  1232. procedure TCustomSQLStatement.Unprepare;
  1233. begin
  1234. // Some SQLConnections does not support statement [un]preparation, but they have allocated local cursor(s)
  1235. // so let them do cleanup f.e. cancel pending queries and/or free resultset
  1236. // and also do UnRegisterStatement!
  1237. if assigned(FCursor) then
  1238. DoUnprepare;
  1239. end;
  1240. function TCustomSQLStatement.ParamByName(const AParamName: String): TParam;
  1241. begin
  1242. Result:=FParams.ParamByName(AParamName);
  1243. end;
  1244. function TCustomSQLStatement.RowsAffected: TRowsCount;
  1245. begin
  1246. if FRowsAffected=-1 then
  1247. begin
  1248. if Assigned(Database) then
  1249. FRowsAffected:=Database.RowsAffected(FCursor);
  1250. end;
  1251. Result:=FRowsAffected;
  1252. end;
  1253. { TSQLConnection }
  1254. constructor TSQLConnection.Create(AOwner: TComponent);
  1255. begin
  1256. inherited Create(AOwner);
  1257. FSQLFormatSettings:=DefaultSQLFormatSettings;
  1258. FFieldNameQuoteChars:=DoubleQuotes;
  1259. FLogEvents:=LogAllEvents; //match Property LogEvents...Default LogAllEvents
  1260. FStatements:=TThreadList.Create;
  1261. FStatements.Duplicates:=dupIgnore;
  1262. FConnOptions:=[sqCommitEndsPrepared, sqRollbackEndsPrepared];
  1263. end;
  1264. destructor TSQLConnection.Destroy;
  1265. begin
  1266. try
  1267. CloseForDestroy; // needed because we want to de-allocate statements
  1268. Finally
  1269. FreeAndNil(FStatements);
  1270. inherited Destroy;
  1271. end;
  1272. end;
  1273. function TSQLConnection.StrToStatementType(s : string) : TStatementType;
  1274. var
  1275. T : TStatementType;
  1276. LS : String;
  1277. begin
  1278. LS:=Lowercase(s);
  1279. for T:=stSelect to stRollback do
  1280. if (LS=StatementTokens[T]) then
  1281. Exit(T);
  1282. Result:=stUnknown;
  1283. end;
  1284. procedure TSQLConnection.SetTransaction(Value : TSQLTransaction);
  1285. begin
  1286. if FTransaction<>value then
  1287. begin
  1288. if Assigned(FTransaction) and FTransaction.Active then
  1289. DatabaseError(SErrAssTransaction);
  1290. if Assigned(Value) then
  1291. Value.Database := Self;
  1292. FTransaction := Value;
  1293. If Assigned(FTransaction) and (FTransaction.Database=Nil) then
  1294. FTransaction.Database:=Self;
  1295. end;
  1296. end;
  1297. procedure TSQLConnection.UpdateIndexDefs(IndexDefs : TIndexDefs; TableName : string);
  1298. begin
  1299. // Empty abstract
  1300. end;
  1301. procedure TSQLConnection.DoConnect;
  1302. var ConnectionCharSet: string;
  1303. begin
  1304. inherited;
  1305. // map connection CharSet to corresponding local CodePage
  1306. // do not set FCodePage to CP_ACP if FCodePage = DefaultSystemCodePage
  1307. // aliases listed here are commonly used, but not recognized by CodePageNameToCodePage()
  1308. ConnectionCharSet := LowerCase(GetConnectionCharSet);
  1309. case ConnectionCharSet of
  1310. 'utf8','utf-8','utf8mb4':
  1311. FCodePage := CP_UTF8;
  1312. 'win1250','cp1250':
  1313. FCodePage := 1250;
  1314. 'win1251','cp1251':
  1315. FCodePage := 1251;
  1316. 'win1252','cp1252','latin1','iso8859_1':
  1317. FCodePage := 1252;
  1318. else
  1319. begin
  1320. FCodePage := CodePageNameToCodePage(ConnectionCharSet);
  1321. if FCodePage = CP_NONE then
  1322. FCodePage := CP_ACP;
  1323. end;
  1324. end;
  1325. end;
  1326. procedure TSQLConnection.DoInternalConnect;
  1327. begin
  1328. if (DatabaseName = '') and not(sqSupportEmptyDatabaseName in FConnOptions) then
  1329. DatabaseError(SErrNoDatabaseName,Self);
  1330. end;
  1331. procedure TSQLConnection.DoInternalDisconnect;
  1332. Var
  1333. I : integer;
  1334. L : TList;
  1335. begin
  1336. If Assigned(FStatements) then
  1337. begin
  1338. L:=FStatements.LockList;
  1339. try
  1340. For I:=0 to L.Count-1 do
  1341. TCustomSQLStatement(L[i]).Unprepare;
  1342. L.Clear;
  1343. finally
  1344. FStatements.UnlockList;
  1345. end;
  1346. end;
  1347. end;
  1348. procedure TSQLConnection.StartTransaction;
  1349. begin
  1350. if not assigned(Transaction) then
  1351. DatabaseError(SErrConnTransactionnSet)
  1352. else
  1353. Transaction.StartTransaction;
  1354. end;
  1355. procedure TSQLConnection.EndTransaction;
  1356. begin
  1357. if not assigned(Transaction) then
  1358. DatabaseError(SErrConnTransactionnSet)
  1359. else
  1360. Transaction.EndTransaction;
  1361. end;
  1362. procedure TSQLConnection.ExecuteDirect(SQL: String);
  1363. begin
  1364. ExecuteDirect(SQL,FTransaction);
  1365. end;
  1366. procedure TSQLConnection.ExecuteDirect(SQL: String;
  1367. ATransaction: TSQLTransaction);
  1368. var Cursor : TSQLCursor;
  1369. begin
  1370. if not assigned(ATransaction) then
  1371. DatabaseError(SErrTransactionnSet);
  1372. if not Connected then Open;
  1373. if not ATransaction.Active then
  1374. ATransaction.MaybeStartTransaction;
  1375. SQL := TrimRight(SQL);
  1376. if SQL = '' then
  1377. DatabaseError(SErrNoStatement);
  1378. try
  1379. Cursor := AllocateCursorHandle;
  1380. Cursor.FStatementType := stUnknown;
  1381. If LogEvent(detPrepare) then
  1382. Log(detPrepare,SQL);
  1383. PrepareStatement(Cursor,ATransaction,SQL,Nil);
  1384. try
  1385. If LogEvent(detExecute) then
  1386. Log(detExecute,SQL);
  1387. Execute(Cursor,ATransaction, Nil);
  1388. finally
  1389. UnPrepareStatement(Cursor);
  1390. end;
  1391. finally;
  1392. DeAllocateCursorHandle(Cursor);
  1393. FreeAndNil(Cursor);
  1394. end;
  1395. end;
  1396. function TSQLConnection.GetPort: cardinal;
  1397. begin
  1398. result := StrToIntDef(Params.Values[PortParamName],0);
  1399. end;
  1400. procedure TSQLConnection.SetOptions(AValue: TSQLConnectionOptions);
  1401. begin
  1402. if FOptions=AValue then Exit;
  1403. FOptions:=AValue;
  1404. end;
  1405. procedure TSQLConnection.SetPort(const AValue: cardinal);
  1406. begin
  1407. if AValue<>0 then
  1408. Params.Values[PortParamName]:=IntToStr(AValue)
  1409. else with params do if IndexOfName(PortParamName) > -1 then
  1410. Delete(IndexOfName(PortParamName));
  1411. end;
  1412. function TSQLConnection.AttemptCommit(trans: TSQLHandle): boolean;
  1413. begin
  1414. try
  1415. Result:=Commit(trans);
  1416. except
  1417. if ForcedClose then
  1418. Result:=True
  1419. else
  1420. Raise;
  1421. end;
  1422. end;
  1423. function TSQLConnection.AttemptRollBack(trans: TSQLHandle): boolean;
  1424. begin
  1425. try
  1426. Result:=Rollback(trans);
  1427. except
  1428. if ForcedClose then
  1429. Result:=True
  1430. else
  1431. Raise;
  1432. end;
  1433. end;
  1434. procedure TSQLConnection.GetDBInfo(const ASchemaType : TSchemaType; const ASchemaObjectName, AReturnField : string; AList: TStrings);
  1435. var qry : TCustomSQLQuery;
  1436. begin
  1437. if not assigned(Transaction) then
  1438. DatabaseError(SErrConnTransactionnSet);
  1439. qry := TCustomSQLQuery.Create(nil);
  1440. try
  1441. qry.transaction := Transaction;
  1442. qry.database := Self;
  1443. with qry do
  1444. begin
  1445. ParseSQL := False;
  1446. SetSchemaInfo(ASchemaType,ASchemaObjectName,'');
  1447. open;
  1448. AList.Clear;
  1449. while not eof do
  1450. begin
  1451. AList.Append(trim(fieldbyname(AReturnField).asstring));
  1452. Next;
  1453. end;
  1454. end;
  1455. finally
  1456. qry.free;
  1457. end;
  1458. end;
  1459. function TSQLConnection.GetConnectionCharSet: string;
  1460. begin
  1461. // default implementation returns user supplied FCharSet
  1462. // (can be overriden by descendants, which are able retrieve current connection charset using client API)
  1463. Result := LowerCase(FCharSet);
  1464. end;
  1465. function TSQLConnection.RowsAffected(cursor: TSQLCursor): TRowsCount;
  1466. begin
  1467. Result := -1;
  1468. end;
  1469. function TSQLConnection.AddFieldDef(AFieldDefs: TFieldDefs; AFieldNo: Longint;
  1470. const AName: string; ADataType: TFieldType; ASize, APrecision: Integer;
  1471. AByteSize, ARequired, AReadOnly: Boolean): TFieldDef;
  1472. var
  1473. ACodePage: TSystemCodePage;
  1474. begin
  1475. // helper function used by descendants
  1476. if ADataType in [ftString, ftFixedChar, ftMemo] then
  1477. begin
  1478. ACodePage := FCodePage;
  1479. // if ASize of character data is passed as "byte length",
  1480. // translate it to "character length" as expected by TFieldDef
  1481. if AByteSize and (ACodePage = CP_UTF8) then
  1482. ASize := ASize div 4;
  1483. end
  1484. else
  1485. ACodePage := 0;
  1486. Result := AFieldDefs.Add(AName, ADataType, ASize, APrecision, ARequired, AReadOnly, AFieldNo, ACodePage);
  1487. end;
  1488. procedure TSQLConnection.GetTableNames(List: TStrings; SystemTables: Boolean);
  1489. begin
  1490. if not SystemTables then
  1491. GetDBInfo(stTables,'','table_name',List)
  1492. else
  1493. GetDBInfo(stSysTables,'','table_name',List);
  1494. end;
  1495. procedure TSQLConnection.GetProcedureNames(List: TStrings);
  1496. begin
  1497. GetDBInfo(stProcedures,'','procedure_name',List);
  1498. end;
  1499. procedure TSQLConnection.GetFieldNames(const TableName: string; List: TStrings);
  1500. begin
  1501. GetDBInfo(stColumns,TableName,'column_name',List);
  1502. end;
  1503. procedure TSQLConnection.GetSchemaNames(List: TStrings);
  1504. begin
  1505. GetDBInfo(stSchemata,'','SCHEMA_NAME',List);
  1506. end;
  1507. procedure TSQLConnection.GetSequenceNames(List: TStrings);
  1508. begin
  1509. GetDBInfo(stSequences,'','SEQUENCE_NAME',List);
  1510. end;
  1511. {
  1512. See if we can integrate/merge this with GetDBInfo. They are virtually identical
  1513. }
  1514. Function TSQLConnection.GetObjectNames(ASchemaType: TSchemaType; AList : TSqlObjectIdentifierList) : Integer;
  1515. var
  1516. qry : TCustomSQLQuery;
  1517. vSchemaName, vObjectName: String;
  1518. f: TField;
  1519. begin
  1520. Result:=0;
  1521. if not assigned(Transaction) then
  1522. DatabaseError(SErrConnTransactionnSet);
  1523. qry := TCustomSQLQuery.Create(nil);
  1524. try
  1525. qry.transaction := Transaction;
  1526. qry.database := Self;
  1527. with qry do
  1528. begin
  1529. ParseSQL := False;
  1530. SetSchemaInfo(ASchemaType,TSchemaObjectNames[ASchemaType],'');
  1531. open;
  1532. f:=FindField(TSchemaObjectNames[stSchemata]);
  1533. while not eof do
  1534. begin
  1535. vSchemaName:='';
  1536. if Assigned(f) then
  1537. vSchemaName:=f.AsString;
  1538. vObjectName:=FieldByName(FSchemaObjectName).AsString;
  1539. AList.AddIdentifier(vObjectName, vSchemaName);
  1540. Next;
  1541. Inc(Result);
  1542. end;
  1543. end;
  1544. finally
  1545. qry.free;
  1546. end;
  1547. end;
  1548. function TSQLConnection.HasTable(const aTable: String; SearchSystemTables: Boolean) : Boolean;
  1549. var
  1550. L : TStrings;
  1551. begin
  1552. L:=TStringList.Create;
  1553. try
  1554. TStringList(L).Sorted:=True;
  1555. GetTableNames(L,SearchSystemTables);
  1556. Result:=L.IndexOf(aTable)<>-1;
  1557. Finally
  1558. L.Free;
  1559. end;
  1560. end;
  1561. function TSQLConnection.GetConnectionInfo(InfoType: TConnInfoType): string;
  1562. var i: TConnInfoType;
  1563. begin
  1564. Result:='';
  1565. if InfoType = citAll then
  1566. for i:=citServerType to citClientVersion do
  1567. begin
  1568. if Result<>'' then Result:=Result+',';
  1569. Result:=Result+'"'+GetConnectionInfo(i)+'"';
  1570. end;
  1571. end;
  1572. function TSQLConnection.GetStatementInfo(const ASQL: string): TSQLStatementInfo;
  1573. type
  1574. TParsePart = (ppStart,ppWith,ppSelect,ppTableName,ppFrom,ppWhere,ppGroup,ppOrder,ppBogus);
  1575. TPhraseSeparator = (sepNone, sepWhiteSpace, sepComma, sepComment, sepParentheses, sepDoubleQuote, sepEnd);
  1576. TKeyword = (kwWITH, kwSELECT, kwINSERT, kwUPDATE, kwDELETE, kwFROM, kwJOIN, kwWHERE, kwGROUP, kwORDER, kwUNION, kwROWS, kwLIMIT, kwUnknown);
  1577. const
  1578. KeywordNames: array[TKeyword] of string =
  1579. ('WITH', 'SELECT', 'INSERT', 'UPDATE', 'DELETE', 'FROM', 'JOIN', 'WHERE', 'GROUP', 'ORDER', 'UNION', 'ROWS', 'LIMIT', '');
  1580. var
  1581. PSQL, CurrentP, SavedP,
  1582. PhraseP, PStatementPart : PChar;
  1583. S : string;
  1584. ParsePart : TParsePart;
  1585. BracketCount : Integer;
  1586. Separator : TPhraseSeparator;
  1587. Keyword, K : TKeyword;
  1588. begin
  1589. PSQL:=PChar(ASQL);
  1590. ParsePart := ppStart;
  1591. CurrentP := PSQL-1;
  1592. PhraseP := PSQL;
  1593. Result.TableName := '';
  1594. Result.Updateable := False;
  1595. Result.WhereStartPos := 0;
  1596. Result.WhereStopPos := 0;
  1597. repeat
  1598. inc(CurrentP);
  1599. SavedP := CurrentP;
  1600. case CurrentP^ of
  1601. ' ', #9..#13:
  1602. Separator := sepWhiteSpace;
  1603. ',':
  1604. Separator := sepComma;
  1605. #0, ';':
  1606. Separator := sepEnd;
  1607. '(':
  1608. begin
  1609. Separator := sepParentheses;
  1610. // skip everything between brackets, since it could be a sub-select, and
  1611. // further nothing between brackets could be interesting for the parser.
  1612. BracketCount := 1;
  1613. repeat
  1614. inc(CurrentP);
  1615. if CurrentP^ = '(' then inc(BracketCount)
  1616. else if CurrentP^ = ')' then dec(BracketCount);
  1617. until (CurrentP^ = #0) or (BracketCount = 0);
  1618. if CurrentP^ <> #0 then inc(CurrentP);
  1619. end;
  1620. '"','`':
  1621. if SkipComments(CurrentP, sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions) then
  1622. Separator := sepDoubleQuote;
  1623. else
  1624. if SkipComments(CurrentP, sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions) then
  1625. Separator := sepComment
  1626. else
  1627. Separator := sepNone;
  1628. end;
  1629. if Separator <> sepNone then
  1630. begin
  1631. if (CurrentP > SavedP) and (SavedP > PhraseP) then
  1632. CurrentP := SavedP; // there is something before comment or left parenthesis or double quote
  1633. if (Separator in [sepWhitespace,sepComment]) and (SavedP = PhraseP) then
  1634. PhraseP := CurrentP; // skip comments (but not parentheses) and white spaces
  1635. if (CurrentP-PhraseP > 0) or (Separator = sepEnd) then
  1636. begin
  1637. SetString(s, PhraseP, CurrentP-PhraseP);
  1638. Keyword := kwUnknown;
  1639. for K in TKeyword do
  1640. if SameText(s, KeywordNames[K]) then
  1641. begin
  1642. Keyword := K;
  1643. break;
  1644. end;
  1645. case ParsePart of
  1646. ppStart : begin
  1647. Result.StatementType := StrToStatementType(s);
  1648. case Keyword of
  1649. kwWITH : ParsePart := ppWith;
  1650. kwSELECT: ParsePart := ppSelect;
  1651. else break;
  1652. end;
  1653. end;
  1654. ppWith : begin
  1655. // WITH [RECURSIVE] CTE_name [ ( column_names ) ] AS ( CTE_query_definition ) [, ...]
  1656. // { SELECT | INSERT | UPDATE | DELETE } ...
  1657. case Keyword of
  1658. kwSELECT: Result.StatementType := stSelect;
  1659. kwINSERT: Result.StatementType := stInsert;
  1660. kwUPDATE: Result.StatementType := stUpdate;
  1661. kwDELETE: Result.StatementType := stDelete;
  1662. end;
  1663. if Result.StatementType <> stUnknown then break;
  1664. end;
  1665. ppSelect : begin
  1666. if Keyword = kwFROM then
  1667. ParsePart := ppTableName;
  1668. end;
  1669. ppTableName:
  1670. begin
  1671. // Meta-data requests are never updateable
  1672. // and select statements from more than one table
  1673. // and/or derived tables are also not updateable
  1674. if Separator in [sepWhitespace, sepComment, sepDoubleQuote, sepEnd] then
  1675. begin
  1676. Result.TableName := Result.TableName + s;
  1677. Result.Updateable := True;
  1678. end;
  1679. // compound delimited classifier like: "schema name"."table name"
  1680. if not (CurrentP^ in ['.','"']) then
  1681. ParsePart := ppFrom;
  1682. end;
  1683. ppFrom : begin
  1684. if (Keyword in [kwWHERE, kwGROUP, kwORDER, kwLIMIT, kwROWS]) or
  1685. (Separator = sepEnd) then
  1686. begin
  1687. case Keyword of
  1688. kwWHERE: ParsePart := ppWhere;
  1689. kwGROUP: ParsePart := ppGroup;
  1690. kwORDER: ParsePart := ppOrder;
  1691. else ParsePart := ppBogus;
  1692. end;
  1693. Result.WhereStartPos := PhraseP-PSQL+1;
  1694. PStatementPart := CurrentP;
  1695. end
  1696. else
  1697. // joined table or user_defined_function (...)
  1698. if (Keyword = kwJOIN) or (Separator in [sepComma, sepParentheses]) then
  1699. begin
  1700. Result.TableName := '';
  1701. Result.Updateable := False;
  1702. end;
  1703. end;
  1704. ppWhere : begin
  1705. if (Keyword in [kwGROUP, kwORDER, kwLIMIT, kwROWS]) or
  1706. (Separator = sepEnd) then
  1707. begin
  1708. ParsePart := ppBogus;
  1709. Result.WhereStartPos := PStatementPart-PSQL;
  1710. if (Separator = sepEnd) then
  1711. Result.WhereStopPos := CurrentP-PSQL+1
  1712. else
  1713. Result.WhereStopPos := PhraseP-PSQL+1;
  1714. end
  1715. else if (Keyword = kwUNION) then
  1716. begin
  1717. ParsePart := ppBogus;
  1718. Result.Updateable := False;
  1719. end;
  1720. end;
  1721. end; {case}
  1722. end;
  1723. if Separator in [sepComment, sepParentheses, sepDoubleQuote] then
  1724. dec(CurrentP);
  1725. PhraseP := CurrentP+1;
  1726. end
  1727. until CurrentP^=#0;
  1728. end;
  1729. function TSQLConnection.GetAsString(Param: TParam): RawByteString;
  1730. begin
  1731. // converts parameter value to connection charset
  1732. if FCodePage = CP_UTF8 then
  1733. Result := Param.AsUTF8String
  1734. else if (FCodePage = DefaultSystemCodePage) or
  1735. (FCodePage = CP_ACP) or (FCodePage = CP_NONE) then
  1736. Result := Param.AsAnsiString
  1737. else
  1738. begin
  1739. Result := Param.AsAnsiString;
  1740. SetCodePage(Result, FCodePage, True);
  1741. end;
  1742. end;
  1743. function TSQLConnection.GetAsSQLText(Field : TField) : string;
  1744. begin
  1745. if (not assigned(Field)) or Field.IsNull then Result := 'Null'
  1746. else case Field.DataType of
  1747. ftString : Result := QuotedStr(Field.AsString);
  1748. ftDate : Result := '''' + FormatDateTime('yyyy-mm-dd',Field.AsDateTime,FSQLFormatSettings) + '''';
  1749. ftDateTime : Result := '''' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',Field.AsDateTime,FSQLFormatSettings) + '''';
  1750. ftTime : Result := '''' + TimeIntervalToString(Field.AsDateTime) + '''';
  1751. else
  1752. Result := Field.AsString;
  1753. end; {case}
  1754. end;
  1755. function TSQLConnection.GetAsSQLText(Param: TParam) : string;
  1756. begin
  1757. if (not assigned(Param)) or Param.IsNull then Result := 'Null'
  1758. else case Param.DataType of
  1759. ftGuid,
  1760. ftMemo,
  1761. ftFixedChar,
  1762. ftString : Result := QuotedStr(GetAsString(Param));
  1763. ftDate : Result := '''' + FormatDateTime('yyyy-mm-dd', Param.AsDateTime, FSQLFormatSettings) + '''';
  1764. ftTime : Result := '''' + TimeIntervalToString(Param.AsDateTime) + '''';
  1765. ftDateTime : Result := '''' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Param.AsDateTime, FSQLFormatSettings) + '''';
  1766. ftCurrency,
  1767. ftBcd : Result := CurrToStr(Param.AsCurrency, FSQLFormatSettings);
  1768. ftFloat : Result := FloatToStr(Param.AsFloat, FSQLFormatSettings);
  1769. ftFMTBcd : Result := StringReplace(Param.AsString, DefaultFormatSettings.DecimalSeparator, FSQLFormatSettings.DecimalSeparator, []);
  1770. ftSingle : Result := FloatToStr(Param.AsSingle, FSQLFormatSettings);
  1771. else
  1772. Result := Param.AsString;
  1773. end; {case}
  1774. end;
  1775. function TSQLConnection.GetHandle: pointer;
  1776. begin
  1777. Result := nil;
  1778. end;
  1779. function TSQLConnection.LogEvent(EventType: TDBEventType): Boolean;
  1780. begin
  1781. Result:=(Assigned(FOnLog) or Assigned(GlobalDBLogHook)) and (EventType in LogEvents);
  1782. end;
  1783. procedure TSQLConnection.LogParams(const AParams: TParams);
  1784. Var
  1785. S : String;
  1786. P : TParam;
  1787. begin
  1788. if not LogEvent(detParamValue) or not Assigned(AParams) then
  1789. Exit;
  1790. For P in AParams do
  1791. begin
  1792. if P.IsNull then
  1793. S:='<NULL>'
  1794. else if (P.DataType in ftBlobTypes) and not (P.DataType in [ftMemo, ftFmtMemo,ftWideMemo]) then
  1795. S:='<BLOB>'
  1796. else
  1797. S:=P.AsString;
  1798. Log(detParamValue,Format(SLogParamValue,[P.Name,S]));
  1799. end;
  1800. end;
  1801. procedure TSQLConnection.Log(EventType: TDBEventType; const Msg: String);
  1802. Var
  1803. M : String;
  1804. begin
  1805. If LogEvent(EventType) then
  1806. begin
  1807. If Assigned(FonLog) then
  1808. FOnLog(Self,EventType,Msg);
  1809. If Assigned(GlobalDBLogHook) then
  1810. begin
  1811. If (Name<>'') then
  1812. M:=Name+' : '+Msg
  1813. else
  1814. M:=ClassName+' : '+Msg;
  1815. GlobalDBLogHook(Self,EventType,M);
  1816. end;
  1817. end;
  1818. end;
  1819. procedure TSQLConnection.RegisterStatement(S: TCustomSQLStatement);
  1820. begin
  1821. FStatements.Add(S);
  1822. end;
  1823. procedure TSQLConnection.UnRegisterStatement(S: TCustomSQLStatement);
  1824. begin
  1825. if Assigned(FStatements) then // Can be nil, when we are destroying and datasets are uncoupled.
  1826. FStatements.Remove(S);
  1827. end;
  1828. procedure TSQLConnection.UnPrepareStatements(aTransaction: TSQLTransaction);
  1829. Var
  1830. I : integer;
  1831. L : TList;
  1832. S : TCustomSQLStatement;
  1833. begin
  1834. if not Assigned(FStatements) then // Can be nil, when we are destroying and datasets are uncoupled.
  1835. exit;
  1836. L:=FStatements.LockList;
  1837. try
  1838. For I:=0 to L.Count-1 do
  1839. begin
  1840. S:=TCustomSQLStatement(L[i]);
  1841. if (S.Transaction=aTransaction) then
  1842. S.Unprepare;
  1843. end;
  1844. L.Clear;
  1845. finally
  1846. FStatements.UnlockList;
  1847. end;
  1848. end;
  1849. function TSQLConnection.CreateCustomQuery(aOwner : TComponent) : TCustomSQLQuery;
  1850. begin
  1851. Result:=TCustomSQLQuery.Create(AOwner);
  1852. end;
  1853. function TSQLConnection.InitialiseUpdateStatement(Query : TCustomSQLQuery; var qry : TCustomSQLQuery): TCustomSQLQuery;
  1854. begin
  1855. if not assigned(qry) then
  1856. begin
  1857. qry := TCustomSQLQuery(TComponentClass(Query.ClassType).Create(Nil));
  1858. qry.ParseSQL := False;
  1859. qry.DataBase := Self;
  1860. qry.Transaction := Query.SQLTransaction;
  1861. qry.Unidirectional:=True;
  1862. qry.UsePrimaryKeyAsKey:=False;
  1863. qry.PacketRecords:=1;
  1864. end;
  1865. Result:=qry;
  1866. end;
  1867. procedure TSQLConnection.AddFieldToUpdateWherePart(var sql_where : string;UpdateMode : TUpdateMode; F : TField);
  1868. begin
  1869. if (pfInKey in F.ProviderFlags)
  1870. or ((UpdateMode = upWhereAll) and (pfInWhere in F.ProviderFlags))
  1871. or ((UpdateMode = UpWhereChanged) and (pfInWhere in F.ProviderFlags) and (F.Value <> F.OldValue)) then
  1872. begin
  1873. if (sql_where<>'') then
  1874. sql_where:=sql_where + ' and ';
  1875. sql_where:= sql_where + '(' + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1];
  1876. // primary key normally cannot be null
  1877. if Assigned(F.Dataset) and F.Dataset.Active and (F.OldValue = NULL) then
  1878. sql_where := sql_where + ' is null '
  1879. else
  1880. sql_where := sql_where +'= :"' + 'OLD_' + F.FieldName + '"';
  1881. sql_where:=sql_where+') ';
  1882. end;
  1883. end;
  1884. function TSQLConnection.ConstructInsertSQL(Query: TCustomSQLQuery;
  1885. var ReturningClause: Boolean): string;
  1886. var x : integer;
  1887. sql_fields : string;
  1888. sql_values : string;
  1889. returning_fields : String;
  1890. F : TField;
  1891. begin
  1892. sql_fields := '';
  1893. sql_values := '';
  1894. returning_fields := '';
  1895. for x := 0 to Query.Fields.Count -1 do
  1896. begin
  1897. F:=Query.Fields[x];
  1898. if (not F.IsNull) and (pfInUpdate in F.ProviderFlags) and (not F.ReadOnly) then
  1899. begin
  1900. sql_fields := sql_fields + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
  1901. sql_values := sql_values + ':"' + F.FieldName + '",';
  1902. end;
  1903. if ReturningClause and (pfRefreshOnInsert in F.ProviderFlags) then
  1904. returning_fields := returning_fields + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
  1905. end;
  1906. if length(sql_fields) = 0 then
  1907. DatabaseErrorFmt(sNoUpdateFields,['insert'],self);
  1908. setlength(sql_fields,length(sql_fields)-1);
  1909. setlength(sql_values,length(sql_values)-1);
  1910. result := 'insert into ' + Query.FTableName + ' (' + sql_fields + ') values (' + sql_values + ')';
  1911. if ReturningClause then
  1912. begin
  1913. ReturningClause:=length(returning_fields) <> 0 ;
  1914. if ReturningClause then
  1915. begin
  1916. setlength(returning_fields,length(returning_fields)-1);
  1917. Result := Result + ' returning ' + returning_fields;
  1918. end;
  1919. end;
  1920. end;
  1921. function TSQLConnection.ConstructUpdateSQL(Query: TCustomSQLQuery;
  1922. var ReturningClause: Boolean): string;
  1923. var x : integer;
  1924. F : TField;
  1925. sql_set : string;
  1926. sql_where : string;
  1927. returning_fields : String;
  1928. begin
  1929. sql_set := '';
  1930. sql_where := '';
  1931. returning_fields := '';
  1932. for x := 0 to Query.Fields.Count -1 do
  1933. begin
  1934. F:=Query.Fields[x];
  1935. AddFieldToUpdateWherePart(sql_where,Query.UpdateMode,F);
  1936. if (pfInUpdate in F.ProviderFlags) and (not F.ReadOnly) then
  1937. sql_set := sql_set +FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] +'=:"' + F.FieldName + '",';
  1938. if ReturningClause and (pfRefreshOnUpdate in F.ProviderFlags) then
  1939. returning_fields := returning_fields + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
  1940. end;
  1941. if length(sql_set) = 0 then DatabaseErrorFmt(sNoUpdateFields,['update'],self);
  1942. setlength(sql_set,length(sql_set)-1);
  1943. if length(sql_where) = 0 then DatabaseErrorFmt(sNoWhereFields,['update'],self);
  1944. result := 'update ' + Query.FTableName + ' set ' + sql_set + ' where ' + sql_where;
  1945. if ReturningClause then
  1946. begin
  1947. ReturningClause:=length(returning_fields) <> 0 ;
  1948. if ReturningClause then
  1949. begin
  1950. setlength(returning_fields,length(returning_fields)-1);
  1951. Result := Result + ' returning ' + returning_fields;
  1952. end;
  1953. end;
  1954. end;
  1955. function TSQLConnection.ConstructDeleteSQL(Query : TCustomSQLQuery) : string;
  1956. var
  1957. x : integer;
  1958. sql_where : string;
  1959. begin
  1960. sql_where := '';
  1961. for x := 0 to Query.Fields.Count -1 do
  1962. AddFieldToUpdateWherePart(sql_where,Query.UpdateMode, Query.Fields[x]);
  1963. if length(sql_where) = 0 then
  1964. DatabaseErrorFmt(sNoWhereFields,['delete'],self);
  1965. result := 'delete from ' + Query.FTableName + ' where ' + sql_where;
  1966. end;
  1967. function TSQLConnection.ConstructRefreshSQL(Query: TCustomSQLQuery; UpdateKind: TUpdateKind): string;
  1968. Var
  1969. F : TField;
  1970. PF : TProviderFlag;
  1971. Where : String;
  1972. begin
  1973. Result:=Trim(Query.RefreshSQL.Text);
  1974. if (Result='') then
  1975. begin
  1976. Where:='';
  1977. PF:=RefreshFlags[UpdateKind];
  1978. For F in Query.Fields do
  1979. begin
  1980. if PF in F.ProviderFlags then
  1981. begin
  1982. if (Result<>'') then
  1983. Result:=Result+', ';
  1984. if (F.Origin<>'') and (F.Origin<>F.FieldName) then
  1985. Result:=Result+F.Origin+' AS '+F.FieldName
  1986. else
  1987. Result:=Result+FieldNameQuoteChars[0]+F.FieldName+FieldNameQuoteChars[1]
  1988. end;
  1989. if pfInkey in F.ProviderFlags then
  1990. begin
  1991. if (Where<>'') then
  1992. Where:=Where+' AND ';
  1993. Where:=Where+'('+FieldNameQuoteChars[0]+F.FieldName+FieldNameQuoteChars[1]+' = :'+F.FieldName+')';
  1994. end;
  1995. end;
  1996. if (Where='') then
  1997. DatabaseError(SErrNoKeyFieldForRefreshClause,Query);
  1998. Result:='SELECT '+Result+' FROM '+Query.FTableName+' WHERE '+Where;
  1999. end;
  2000. end;
  2001. procedure TSQLConnection.ApplyFieldUpdate(C : TSQLCursor; P : TSQLDBParam; F : TField; UseOldValue : Boolean);
  2002. begin
  2003. if UseOldValue then
  2004. P.AssignFieldValue(F,F.OldValue)
  2005. else
  2006. P.AssignFieldValue(F,F.Value);
  2007. P.FFieldDef:=F.FieldDef;
  2008. end;
  2009. procedure TSQLConnection.ApplyRecUpdate(Query: TCustomSQLQuery; UpdateKind: TUpdateKind);
  2010. var
  2011. qry : TCustomSQLQuery;
  2012. s : string;
  2013. x : integer;
  2014. Fld : TField;
  2015. Par, P : TParam;
  2016. UseOldValue, HasReturningClause : Boolean;
  2017. begin
  2018. qry:=Nil;
  2019. HasReturningClause:=(sqSupportReturning in ConnOptions) and not (sqoRefreshUsingSelect in Query.Options) and (Trim(Query.RefreshSQL.Text)='');
  2020. case UpdateKind of
  2021. ukInsert : begin
  2022. s := Trim(Query.FInsertSQL.Text);
  2023. if s = '' then
  2024. s := ConstructInsertSQL(Query, HasReturningClause)
  2025. else
  2026. HasReturningClause := False;
  2027. qry := InitialiseUpdateStatement(Query, Query.FInsertQry);
  2028. end;
  2029. ukModify : begin
  2030. s := Trim(Query.FUpdateSQL.Text);
  2031. if s = '' then begin
  2032. //if not assigned(Query.FUpdateQry) or (Query.UpdateMode<>upWhereKeyOnly) then // first time or dynamic where part
  2033. s := ConstructUpdateSQL(Query, HasReturningClause);
  2034. end
  2035. else
  2036. HasReturningClause := False;
  2037. qry := InitialiseUpdateStatement(Query, Query.FUpdateQry);
  2038. end;
  2039. ukDelete : begin
  2040. s := Trim(Query.FDeleteSQL.Text);
  2041. if (s='') and (not assigned(Query.FDeleteQry) or (Query.UpdateMode<>upWhereKeyOnly)) then
  2042. s := ConstructDeleteSQL(Query);
  2043. HasReturningClause := False;
  2044. qry := InitialiseUpdateStatement(Query, Query.FDeleteQry);
  2045. end;
  2046. end;
  2047. if (s<>'') and (qry.SQL.Text<>s) then
  2048. qry.SQL.Text:=s; //assign only when changed, to avoid UnPrepare/Prepare
  2049. Assert(qry.SQL.Text<>'');
  2050. for x:=0 to Qry.Params.Count-1 do
  2051. begin
  2052. P:=Qry.Params[x];
  2053. S:=P.Name;
  2054. UseOldValue:=SameText(Copy(S,1,4),'OLD_');
  2055. if UseOldValue then
  2056. begin
  2057. Delete(S,1,4);
  2058. Fld:=Query.FieldByName(S);
  2059. end
  2060. else
  2061. Fld:=Query.FindField(S);
  2062. if Assigned(Fld) then
  2063. ApplyFieldUpdate(Query.Cursor, P as TSQLDBParam, Fld, UseOldValue)
  2064. else
  2065. begin
  2066. // if does not exists field with given name, try look for param
  2067. Par:=Query.Params.FindParam(S);
  2068. if Assigned(Par) then
  2069. P.Assign(Par)
  2070. else
  2071. DatabaseErrorFmt(SFieldNotFound,[S],Query); // same error as raised by FieldByName()
  2072. end;
  2073. end;
  2074. if HasReturningClause then
  2075. begin
  2076. Qry.Close;
  2077. Qry.Open
  2078. end
  2079. else
  2080. Qry.ExecSQL;
  2081. if (scoApplyUpdatesChecksRowsAffected in Options) and (Qry.RowsAffected<>1) then
  2082. begin
  2083. Qry.Close;
  2084. DatabaseErrorFmt(SErrFailedToUpdateRecord, [Qry.RowsAffected], Query);
  2085. end;
  2086. if HasReturningClause then
  2087. Query.ApplyReturningResult(Qry,UpdateKind);
  2088. end;
  2089. function TSQLConnection.RefreshLastInsertID(Query: TCustomSQLQuery; Field: TField): Boolean;
  2090. begin
  2091. Result:=False;
  2092. end;
  2093. procedure TSQLConnection.FreeFldBuffers(cursor: TSQLCursor);
  2094. begin
  2095. // empty
  2096. end;
  2097. function TSQLConnection.StartImplicitTransaction(trans: TSQLHandle; aParams: string): boolean;
  2098. begin
  2099. Result:=False;
  2100. end;
  2101. function TSQLConnection.GetSchemaInfoSQL( SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  2102. begin
  2103. case SchemaType of
  2104. stTables : Result := 'SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE=''BASE TABLE''';
  2105. stColumns : Result := 'SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='+QuotedStr(SchemaObjectName);
  2106. stProcedures: Result := 'SELECT *, ROUTINE_NAME AS PROCEDURE_NAME FROM INFORMATION_SCHEMA.ROUTINES';
  2107. stSchemata : Result := 'SELECT * FROM INFORMATION_SCHEMA.SCHEMATA';
  2108. stSequences : Result := 'SELECT * FROM INFORMATION_SCHEMA.SEQUENCES';
  2109. else DatabaseError(SMetadataUnavailable);
  2110. end;
  2111. end;
  2112. function TSQLConnection.GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string;
  2113. begin
  2114. Result := 'SELECT NEXT VALUE FOR ' + SequenceName;
  2115. end;
  2116. function TSQLConnection.GetNextValue(const SequenceName: string; IncrementBy: integer): Int64;
  2117. var
  2118. Q: TCustomSQLQuery;
  2119. begin
  2120. Result := 0;
  2121. Q := TCustomSQLQuery.Create(nil);
  2122. try
  2123. Q.DataBase := Self;
  2124. Q.Transaction := Transaction;
  2125. Q.SQL.Text := GetNextValueSQL(SequenceName, IncrementBy);
  2126. Q.Open;
  2127. if not Q.Eof then
  2128. Result := Q.Fields[0].AsLargeInt;
  2129. Q.Close;
  2130. finally
  2131. FreeAndNil(Q);
  2132. end;
  2133. end;
  2134. procedure TSQLConnection.MaybeConnect;
  2135. begin
  2136. If Not Connected then
  2137. begin
  2138. If (scoExplicitConnect in Options) then
  2139. DatabaseErrorFmt(SErrImplicitConnect,[Name]);
  2140. Connected:=True;
  2141. end;
  2142. end;
  2143. function TSQLConnection.PortParamName: string;
  2144. begin
  2145. Result := 'Port';
  2146. end;
  2147. procedure TSQLConnection.CreateDB;
  2148. begin
  2149. DatabaseError(SNotSupported);
  2150. end;
  2151. procedure TSQLConnection.DropDB;
  2152. begin
  2153. DatabaseError(SNotSupported);
  2154. end;
  2155. { TSQLTransaction }
  2156. constructor TSQLTransaction.Create(AOwner : TComponent);
  2157. begin
  2158. inherited Create(AOwner);
  2159. FParams := TStringList.Create;
  2160. Action := caRollBack;
  2161. end;
  2162. destructor TSQLTransaction.Destroy;
  2163. begin
  2164. EndTransaction;
  2165. FreeAndNil(FTrans);
  2166. FreeAndNil(FParams);
  2167. inherited Destroy;
  2168. end;
  2169. procedure TSQLTransaction.EndTransaction;
  2170. begin
  2171. Case Action of
  2172. caCommit, caCommitRetaining :
  2173. Commit;
  2174. caNone,
  2175. caRollback, caRollbackRetaining :
  2176. if not (stoUseImplicit in Options) then
  2177. RollBack
  2178. else
  2179. CloseTrans;
  2180. end;
  2181. end;
  2182. procedure TSQLTransaction.SetParams(const AValue: TStringList);
  2183. begin
  2184. FParams.Assign(AValue);
  2185. end;
  2186. function TSQLTransaction.GetSQLConnection: TSQLConnection;
  2187. begin
  2188. Result:=Database as TSQLConnection;
  2189. end;
  2190. procedure TSQLTransaction.SetOptions(AValue: TSQLTransactionOptions);
  2191. begin
  2192. if FOptions=AValue then Exit;
  2193. if (stoUseImplicit in Avalue) and Assigned(SQLConnection) And Not (sqImplicitTransaction in SQLConnection.ConnOptions) then
  2194. DatabaseErrorFmt(SErrNoImplicitTransaction, [SQLConnection.ClassName]);
  2195. FOptions:=AValue;
  2196. end;
  2197. procedure TSQLTransaction.SetSQLConnection(AValue: TSQLConnection);
  2198. begin
  2199. Database:=AValue;
  2200. end;
  2201. procedure TSQLTransaction.UnPrepareStatements;
  2202. begin
  2203. if Assigned(SQLConnection) then
  2204. SQLConnection.UnPrepareStatements(Self);
  2205. end;
  2206. Procedure TSQLTransaction.MaybeStartTransaction;
  2207. begin
  2208. if not Active then
  2209. begin
  2210. if (stoExplicitStart in Options) then
  2211. DatabaseErrorFmt(SErrImplictTransactionStart, [Database.Name,Name]);
  2212. StartTransaction;
  2213. end;
  2214. end;
  2215. function TSQLTransaction.GetHandle: Pointer;
  2216. begin
  2217. Result := SQLConnection.GetTransactionHandle(FTrans);
  2218. end;
  2219. Function TSQLTransaction.AllowClose(DS: TDBDataset): Boolean;
  2220. begin
  2221. Result:=(DS is TSQLQuery);
  2222. end;
  2223. procedure TSQLTransaction.CloseDataset(DS: TDBDataset; InCommit : Boolean);
  2224. Const
  2225. UnPrepOptions : Array[Boolean] of TConnOption
  2226. = (sqRollBackEndsPrepared, sqCommitEndsPrepared);
  2227. var
  2228. Q : TSQLQuery;
  2229. begin
  2230. Q:=DS as TSQLQuery;
  2231. if not (sqoKeepOpenOnCommit in Q.Options) then
  2232. inherited CloseDataset(Q,InCommit);
  2233. if UnPrepOptions[InCommit] in SQLConnection.ConnOptions then
  2234. Q.UnPrepare;
  2235. end;
  2236. procedure TSQLTransaction.Commit;
  2237. begin
  2238. if Active then
  2239. begin
  2240. CloseDataSets;
  2241. if sqCommitEndsPrepared in SQLConnection.ConnOptions then
  2242. UnPrepareStatements;
  2243. If LogEvent(detCommit) then
  2244. Log(detCommit,SCommitting);
  2245. // The inherited closetrans must always be called.
  2246. // So the last (FTrans=Nil) is for the case of forced close. (Bug IDs 35246 and 33737)
  2247. // Order is important:
  2248. // some connections do not have FTrans, but they must still go through AttemptCommit.
  2249. if (stoUseImplicit in Options) or SQLConnection.AttemptCommit(FTrans) or (FTrans=Nil) then
  2250. begin
  2251. CloseTrans;
  2252. FreeAndNil(FTrans);
  2253. end;
  2254. end;
  2255. end;
  2256. procedure TSQLTransaction.CommitRetaining;
  2257. begin
  2258. if Active then
  2259. begin
  2260. If LogEvent(detCommit) then
  2261. Log(detCommit,SCommitRetaining);
  2262. SQLConnection.CommitRetaining(FTrans);
  2263. end;
  2264. end;
  2265. procedure TSQLTransaction.Rollback;
  2266. begin
  2267. if Active then
  2268. begin
  2269. if (stoUseImplicit in Options) then
  2270. DatabaseError(SErrImplicitNoRollBack);
  2271. CloseDataSets;
  2272. if sqRollbackEndsPrepared in SQLConnection.ConnOptions then
  2273. UnPrepareStatements;
  2274. If LogEvent(detRollback) then
  2275. Log(detRollback,SRollingBack);
  2276. // The inherited closetrans must always be called.
  2277. // So the last (FTrans=Nil) is for the case of forced close. (Bug IDs 35246 and 33737)
  2278. // Order is important:
  2279. // some connections do not have FTrans, but they must still go through AttemptCommit.
  2280. // FTrans=Nil for the case of forced close.
  2281. if SQLConnection.AttemptRollBack(FTrans) or (FTrans=Nil) then
  2282. begin
  2283. CloseTrans;
  2284. FreeAndNil(FTrans);
  2285. end;
  2286. end;
  2287. end;
  2288. procedure TSQLTransaction.RollbackRetaining;
  2289. begin
  2290. if Active then
  2291. begin
  2292. if (stoUseImplicit in Options) then
  2293. DatabaseError(SErrImplicitNoRollBack);
  2294. If LogEvent(detRollback) then
  2295. Log(detRollback,SRollBackRetaining);
  2296. SQLConnection.RollBackRetaining(FTrans);
  2297. end;
  2298. end;
  2299. procedure TSQLTransaction.StartTransaction;
  2300. var db : TSQLConnection;
  2301. begin
  2302. if Active then
  2303. DatabaseError(SErrTransAlreadyActive);
  2304. db := SQLConnection;
  2305. if Db = nil then
  2306. DatabaseError(SErrDatabasenAssigned);
  2307. Db.MaybeConnect;
  2308. if not assigned(FTrans) then FTrans := Db.AllocateTransactionHandle;
  2309. if (stoUseImplicit in Options) then
  2310. begin
  2311. if Db.StartImplicitTransaction(FTrans,FParams.CommaText) then
  2312. OpenTrans
  2313. end
  2314. else
  2315. begin
  2316. if Db.StartDBTransaction(FTrans,FParams.CommaText) then
  2317. OpenTrans
  2318. end;
  2319. end;
  2320. Procedure TSQLTransaction.SetDatabase(Value: TDatabase);
  2321. begin
  2322. If Value<>Database then
  2323. begin
  2324. if Assigned(Value) and not (Value is TSQLConnection) then
  2325. DatabaseErrorFmt(SErrNotASQLConnection, [Value.Name], Self);
  2326. CheckInactive;
  2327. if (stoUseImplicit in Options) and Assigned(Value) and Not (sqImplicitTransaction in TSQLConnection(Value).ConnOptions) then
  2328. DatabaseErrorFmt(SErrNoImplicitTransaction, [Value.ClassName]);
  2329. If Assigned(Database) then
  2330. if SQLConnection.Transaction = Self then SQLConnection.Transaction := nil;
  2331. inherited;
  2332. If Assigned(Database) and not (csLoading in ComponentState) then
  2333. If SQLConnection.Transaction = Nil then SQLConnection.Transaction := Self;
  2334. end;
  2335. end;
  2336. Function TSQLTransaction.LogEvent(EventType: TDBEventType): Boolean;
  2337. begin
  2338. Result:=Assigned(Database) and SQLConnection.LogEvent(EventType);
  2339. end;
  2340. Procedure TSQLTransaction.Log(EventType: TDBEventType; Const Msg: String);
  2341. Var
  2342. M : String;
  2343. begin
  2344. If LogEvent(EventType) then
  2345. begin
  2346. If (Name<>'') then
  2347. M:=Name+' : '+Msg
  2348. else
  2349. M:=Msg;
  2350. SQLConnection.Log(EventType,M);
  2351. end;
  2352. end;
  2353. { TSQLSequence }
  2354. constructor TSQLSequence.Create(AQuery: TCustomSQLQuery);
  2355. begin
  2356. inherited Create;
  2357. FQuery := AQuery;
  2358. FApplyEvent := saeOnNewRecord;
  2359. FIncrementBy := 1;
  2360. end;
  2361. procedure TSQLSequence.Assign(Source: TPersistent);
  2362. var SourceSequence: TSQLSequence;
  2363. begin
  2364. if Source is TSQLSequence then
  2365. begin
  2366. SourceSequence := TSQLSequence(Source);
  2367. FFieldName := SourceSequence.FieldName;
  2368. FSequenceName := SourceSequence.SequenceName;
  2369. FIncrementBy := SourceSequence.IncrementBy;
  2370. FApplyEvent := SourceSequence.ApplyEvent;
  2371. end
  2372. else
  2373. inherited;
  2374. end;
  2375. procedure TSQLSequence.Apply;
  2376. var Field: TField;
  2377. begin
  2378. if Assigned(FQuery) and (FSequenceName<>'') and (FFieldName<>'') then
  2379. begin
  2380. Field := FQuery.FindField(FFieldName);
  2381. if Assigned(Field) and Field.IsNull then
  2382. Field.AsLargeInt := GetNextValue;
  2383. end;
  2384. end;
  2385. function TSQLSequence.GetNextValue: Int64;
  2386. begin
  2387. if (FQuery=Nil) or (FQuery.SQLConnection=Nil) then
  2388. DatabaseError(SErrDatabasenAssigned);
  2389. Result := FQuery.SQLConnection.GetNextValue(FSequenceName, FIncrementBy);
  2390. end;
  2391. Type
  2392. { TQuerySQLStatement }
  2393. TQuerySQLStatement = Class(TCustomSQLStatement)
  2394. protected
  2395. FQuery : TCustomSQLQuery;
  2396. function CreateParams: TSQLDBParams; override;
  2397. Function CreateDataLink : TDataLink; override;
  2398. Function GetSchemaType : TSchemaType; override;
  2399. Function GetSchemaObjectName : String; override;
  2400. Function GetSchemaPattern: String; override;
  2401. procedure GetStatementInfo(var ASQL: String; out Info: TSQLStatementInfo); override;
  2402. procedure OnChangeSQL(Sender : TObject); override;
  2403. public
  2404. constructor Create(AOwner: TComponent); override;
  2405. end;
  2406. { TQuerySQLStatement }
  2407. constructor TQuerySQLStatement.Create(AOwner: TComponent);
  2408. begin
  2409. FQuery:=TCustomSQLQuery(AOwner);
  2410. inherited Create(AOwner);
  2411. end;
  2412. function TQuerySQLStatement.CreateDataLink: TDataLink;
  2413. begin
  2414. Result:=TMasterParamsDataLink.Create(FQuery);
  2415. end;
  2416. function TQuerySQLStatement.CreateParams: TSQLDBParams;
  2417. begin
  2418. Result:=FQuery.CreateParams;
  2419. end;
  2420. function TQuerySQLStatement.GetSchemaType: TSchemaType;
  2421. begin
  2422. if Assigned(FQuery) then
  2423. Result:=FQuery.FSchemaType
  2424. else
  2425. Result:=stNoSchema;
  2426. end;
  2427. function TQuerySQLStatement.GetSchemaObjectName: String;
  2428. begin
  2429. if Assigned(FQuery) then
  2430. Result:=FQuery.FSchemaObjectname
  2431. else
  2432. Result:=inherited GetSchemaObjectName;
  2433. end;
  2434. function TQuerySQLStatement.GetSchemaPattern: String;
  2435. begin
  2436. if Assigned(FQuery) then
  2437. Result:=FQuery.FSchemaPattern
  2438. else
  2439. Result:=inherited GetSchemaPattern;
  2440. end;
  2441. procedure TQuerySQLStatement.GetStatementInfo(var ASQL: String; out Info: TSQLStatementInfo);
  2442. begin
  2443. inherited GetStatementInfo(ASQL, Info);
  2444. If Assigned(FQuery) then
  2445. // Note: practical side effect of switch off ParseSQL is that UpdateServerIndexDefs is bypassed
  2446. // which is used as performance tuning option
  2447. if (FQuery.FSchemaType = stNoSchema) and FParseSQL then
  2448. begin
  2449. FQuery.FUpdateable:=Info.Updateable;
  2450. FQuery.FTableName:=Info.TableName;
  2451. FQuery.FWhereStartPos:=Info.WhereStartPos;
  2452. FQuery.FWhereStopPos:=Info.WhereStopPos;
  2453. if FQuery.ServerFiltered then
  2454. ASQL:=FQuery.AddFilter(ASQL);
  2455. end
  2456. else
  2457. begin
  2458. FQuery.FUpdateable:=false;
  2459. FQuery.FTableName:='';
  2460. FQuery.FWhereStartPos:=0;
  2461. FQuery.FWhereStopPos:=0;
  2462. end;
  2463. end;
  2464. procedure TQuerySQLStatement.OnChangeSQL(Sender: TObject);
  2465. begin
  2466. UnPrepare;
  2467. inherited OnChangeSQL(Sender);
  2468. If ParamCheck and Assigned(FDataLink) then
  2469. (FDataLink as TMasterParamsDataLink).RefreshParamNames;
  2470. FQuery.ServerIndexDefs.Updated:=false;
  2471. end;
  2472. { TCustomSQLQuery }
  2473. function TCustomSQLQuery.CreateSQLStatement(aOwner: TComponent
  2474. ): TCustomSQLStatement;
  2475. begin
  2476. Result:=TQuerySQLStatement.Create(Self);
  2477. end;
  2478. constructor TCustomSQLQuery.Create(AOwner : TComponent);
  2479. begin
  2480. inherited Create(AOwner);
  2481. FStatement:=CreateSQLStatement(Self);
  2482. FStatement.OnSQLChanged:=@OnChangeSelectSQL;
  2483. FInsertSQL := TStringList.Create;
  2484. FInsertSQL.OnChange := @OnChangeModifySQL;
  2485. FUpdateSQL := TStringList.Create;
  2486. FUpdateSQL.OnChange := @OnChangeModifySQL;
  2487. FDeleteSQL := TStringList.Create;
  2488. FDeleteSQL.OnChange := @OnChangeModifySQL;
  2489. FRefreshSQL := TStringList.Create;
  2490. FRefreshSQL.OnChange := @OnChangeModifySQL;
  2491. FSequence := TSQLSequence.Create(Self);
  2492. FServerIndexDefs := TServerIndexDefs.Create(Self);
  2493. FServerFiltered := False;
  2494. FServerFilterText := '';
  2495. FSchemaType:=stNoSchema;
  2496. FSchemaObjectName:='';
  2497. FSchemaPattern:='';
  2498. // Delphi has upWhereAll as default, but since strings and oldvalue's don't work yet
  2499. // (variants) set it to upWhereKeyOnly
  2500. FUpdateMode := upWhereKeyOnly;
  2501. FUsePrimaryKeyAsKey := True;
  2502. end;
  2503. destructor TCustomSQLQuery.Destroy;
  2504. begin
  2505. if Active then Close;
  2506. UnPrepare;
  2507. FreeAndNil(FStatement);
  2508. FreeAndNil(FInsertSQL);
  2509. FreeAndNil(FUpdateSQL);
  2510. FreeAndNil(FDeleteSQL);
  2511. FreeAndNil(FRefreshSQL);
  2512. FreeAndNil(FSequence);
  2513. FreeAndNil(FServerIndexDefs);
  2514. inherited Destroy;
  2515. end;
  2516. function TCustomSQLQuery.ParamByName(const AParamName: String): TParam;
  2517. begin
  2518. Result:=Params.ParamByName(AParamName);
  2519. end;
  2520. function TCustomSQLQuery.MacroByName(const AParamName: String): TParam;
  2521. begin
  2522. Result:=Macros.ParamByName(AParamName);
  2523. end;
  2524. procedure TCustomSQLQuery.OnChangeModifySQL(Sender : TObject);
  2525. begin
  2526. CheckInactive;
  2527. end;
  2528. procedure TCustomSQLQuery.SetDatabase(Value : TDatabase);
  2529. var DB : TSQLConnection;
  2530. begin
  2531. if Database = Value then Exit;
  2532. if Assigned(Value) and not (Value is TSQLConnection) then
  2533. DatabaseErrorFmt(SErrNotASQLConnection, [Value.Name], Self);
  2534. UnPrepare;
  2535. DB := TSQLConnection(Value);
  2536. If Assigned(FStatement) then
  2537. FStatement.Database := DB;
  2538. inherited;
  2539. if Assigned(DB) and Assigned(DB.Transaction) and (not Assigned(Transaction) or (Transaction.DataBase<>Database)) then
  2540. Transaction := DB.Transaction;
  2541. end;
  2542. procedure TCustomSQLQuery.SetTransaction(Value: TDBTransaction);
  2543. begin
  2544. if Transaction = Value then Exit;
  2545. UnPrepare;
  2546. inherited;
  2547. If Assigned(FStatement) then
  2548. FStatement.Transaction := TSQLTransaction(Value);
  2549. If Assigned(Transaction) and Assigned(Transaction.DataBase) and (Database<>Transaction.DataBase) then
  2550. Database := Transaction.Database;
  2551. end;
  2552. function TCustomSQLQuery.IsPrepared: Boolean;
  2553. begin
  2554. if Assigned(Fstatement) then
  2555. Result := FStatement.Prepared
  2556. else
  2557. Result := False;
  2558. end;
  2559. function TCustomSQLQuery.AddFilter(const SQLstr: string): string;
  2560. Var
  2561. Res : String;
  2562. begin
  2563. Res:=SQLStr;
  2564. if (FWhereStartPos > 0) and (FWhereStopPos > 0) then
  2565. begin
  2566. system.insert('(',Res,FWhereStartPos+1);
  2567. system.insert(')',Res,FWhereStopPos+1);
  2568. end;
  2569. if FWhereStartPos = 0 then
  2570. Res := Res + ' where (' + ServerFilter + ')'
  2571. else if FWhereStopPos > 0 then
  2572. system.insert(' and ('+ServerFilter+') ',res,FWhereStopPos+2)
  2573. else
  2574. system.insert(' where ('+ServerFilter+') ',res,FWhereStartPos);
  2575. Result := res;
  2576. end;
  2577. procedure TCustomSQLQuery.OpenCursor(InfoQuery: Boolean);
  2578. begin
  2579. if InfoQuery then
  2580. CheckPrepare(InfoQuery);
  2581. try
  2582. inherited OpenCursor(InfoQuery);
  2583. finally
  2584. if InfoQuery then
  2585. CheckUnPrepare;
  2586. end;
  2587. end;
  2588. function TCustomSQLQuery.NeedRefreshRecord(UpdateKind: TUpdateKind): Boolean;
  2589. Var
  2590. PF : TProviderFlag;
  2591. I : Integer;
  2592. DoReturning : Boolean;
  2593. begin
  2594. Result:=(Trim(FRefreshSQL.Text)<>'');
  2595. DoReturning:=(sqSupportReturning in SQLConnection.ConnOptions) and not (sqoRefreshUsingSelect in Options);
  2596. if Not (Result or DoReturning) then
  2597. begin
  2598. PF:=RefreshFlags[UpdateKind];
  2599. I:=0;
  2600. While (Not Result) and (I<Fields.Count) do
  2601. begin
  2602. Result:=PF in Fields[i].ProviderFlags;
  2603. Inc(I);
  2604. end;
  2605. end;
  2606. end;
  2607. function TCustomSQLQuery.RefreshRecord(UpdateKind: TUpdateKind): Boolean;
  2608. Var
  2609. Q : TCustomSQLQuery;
  2610. P : TParam;
  2611. F,FD : TField;
  2612. N : String;
  2613. begin
  2614. Result:=False;
  2615. Q:=TCustomSQLQuery.Create(Nil);
  2616. try
  2617. Q.Database:=Self.Database;
  2618. Q.Transaction:=Self.Transaction;
  2619. Q.SQL.Text:=SQLConnection.ConstructRefreshSQL(Self,UpdateKind);
  2620. For P in Q.Params do
  2621. begin
  2622. N:=P.Name;
  2623. If CompareText(Copy(N,1,4),'OLD_')=0 then
  2624. system.Delete(N,1,4);
  2625. F:=Fields.FindField(N);
  2626. if Assigned(F) then
  2627. P.AssignField(F);
  2628. end;
  2629. Q.Open;
  2630. try
  2631. if (Q.EOF and Q.BOF) then
  2632. DatabaseError(SErrRefreshEmptyResult,Self)
  2633. else
  2634. begin
  2635. if Q.RecordCount<>1 then
  2636. DatabaseErrorFmt(SErrRefreshNotSingleton,[Q.RecordCount],Self);
  2637. For F in Q.Fields do
  2638. begin
  2639. FD:=Fields.FindField(F.FieldName);
  2640. if Assigned(FD) then
  2641. begin
  2642. FD.Assign(F);
  2643. Result:=True; // We could check if the new value differs from the old, but we won't.
  2644. end;
  2645. end;
  2646. end
  2647. finally
  2648. Q.Close;
  2649. end;
  2650. finally
  2651. Q.Free;
  2652. end;
  2653. end;
  2654. procedure TCustomSQLQuery.ApplyReturningResult(Q: TCustomSQLQuery; UpdateKind : TUpdateKind);
  2655. Var
  2656. S : TDataSetState;
  2657. refreshFlag : TProviderFlag;
  2658. F : TField;
  2659. begin
  2660. RefreshFlag:=RefreshFlags[UpdateKind];
  2661. S:=SetTempState(dsRefreshFields);
  2662. try
  2663. For F in Fields do
  2664. if RefreshFlag in F.ProviderFlags then
  2665. F.Assign(Q.FieldByName(F.FieldName));
  2666. finally
  2667. RestoreState(S);
  2668. end;
  2669. end;
  2670. procedure TCustomSQLQuery.ApplyFilter;
  2671. begin
  2672. if Prepared then
  2673. FStatement.Unprepare;
  2674. InternalRefresh;
  2675. First;
  2676. end;
  2677. procedure TCustomSQLQuery.SetServerFiltered(Value: Boolean);
  2678. begin
  2679. if Value and not ParseSQL then
  2680. DatabaseErrorFmt(SNoParseSQL,['Filtering ']);
  2681. if (ServerFiltered <> Value) then
  2682. begin
  2683. FServerFiltered := Value;
  2684. if Active then ApplyFilter;
  2685. end;
  2686. end;
  2687. procedure TCustomSQLQuery.SetServerFilterText(const Value: string);
  2688. begin
  2689. if Value <> ServerFilter then
  2690. begin
  2691. FServerFilterText := Value;
  2692. if Active then ApplyFilter;
  2693. end;
  2694. end;
  2695. procedure TCustomSQLQuery.Prepare;
  2696. begin
  2697. FStatement.Prepare;
  2698. end;
  2699. procedure TCustomSQLQuery.UnPrepare;
  2700. begin
  2701. if Not Refreshing then
  2702. CheckInactive;
  2703. If Assigned(FStatement) then
  2704. FStatement.Unprepare;
  2705. end;
  2706. procedure TCustomSQLQuery.FreeFldBuffers;
  2707. begin
  2708. if assigned(Cursor) then
  2709. SQLConnection.FreeFldBuffers(Cursor);
  2710. end;
  2711. function TCustomSQLQuery.GetMacroChar: AnsiChar;
  2712. begin
  2713. Result := FStatement.MacroChar;
  2714. end;
  2715. function TCustomSQLQuery.GetParamCheck: Boolean;
  2716. begin
  2717. Result:=FStatement.ParamCheck;
  2718. end;
  2719. function TCustomSQLQuery.GetParams: TParams;
  2720. begin
  2721. Result:=FStatement.Params;
  2722. end;
  2723. function TCustomSQLQuery.GetMacroCheck: Boolean;
  2724. begin
  2725. Result:=FStatement.MacroCheck;
  2726. end;
  2727. function TCustomSQLQuery.GetMacros: TParams;
  2728. begin
  2729. Result:=FStatement.Macros;
  2730. end;
  2731. function TCustomSQLQuery.GetParseSQL: Boolean;
  2732. begin
  2733. Result:=FStatement.ParseSQL;
  2734. end;
  2735. function TCustomSQLQuery.GetServerIndexDefs: TServerIndexDefs;
  2736. begin
  2737. Result := FServerIndexDefs;
  2738. end;
  2739. function TCustomSQLQuery.GetSQL: TStringList;
  2740. begin
  2741. Result:=TStringList(Fstatement.SQL);
  2742. end;
  2743. function TCustomSQLQuery.GetSQLConnection: TSQLConnection;
  2744. begin
  2745. Result:=Database as TSQLConnection;
  2746. end;
  2747. function TCustomSQLQuery.GetSQLTransaction: TSQLTransaction;
  2748. begin
  2749. Result:=Transaction as TSQLTransaction;
  2750. end;
  2751. function TCustomSQLQuery.Cursor: TSQLCursor;
  2752. begin
  2753. Result:=FStatement.Cursor;
  2754. end;
  2755. function TCustomSQLQuery.Fetch : boolean;
  2756. begin
  2757. if Not Assigned(Cursor) then
  2758. Exit;
  2759. if not Cursor.FSelectable then
  2760. Exit;
  2761. If LogEvent(detFetch) then
  2762. Log(detFetch,FStatement.FServerSQL);
  2763. if not FIsEof then FIsEOF := not SQLConnection.Fetch(Cursor);
  2764. Result := not FIsEOF;
  2765. end;
  2766. procedure TCustomSQLQuery.Execute;
  2767. begin
  2768. FStatement.DoExecute;
  2769. end;
  2770. function TCustomSQLQuery.RowsAffected: TRowsCount;
  2771. begin
  2772. Result:=FStatement.RowsAffected;
  2773. end;
  2774. function TCustomSQLQuery.LoadField(FieldDef : TFieldDef; buffer : pointer; out CreateBlob : boolean) : boolean;
  2775. begin
  2776. Result := SQLConnection.LoadField(Cursor, FieldDef, buffer, CreateBlob);
  2777. // disable deferred blob loading for "disconnected" datasets
  2778. if Result and (FieldDef.DataType in ftBlobTypes) and (sqoKeepOpenOnCommit in Options) then
  2779. CreateBlob:=True
  2780. end;
  2781. procedure TCustomSQLQuery.LoadBlobIntoBuffer(FieldDef: TFieldDef;
  2782. ABlobBuf: PBufBlobField);
  2783. begin
  2784. SQLConnection.LoadBlobIntoBuffer(FieldDef, ABlobBuf, Cursor,SQLTransaction);
  2785. end;
  2786. procedure TCustomSQLQuery.InternalAddRecord(Buffer: Pointer; AAppend: Boolean);
  2787. begin
  2788. // not implemented - sql dataset
  2789. end;
  2790. procedure TCustomSQLQuery.InternalClose;
  2791. begin
  2792. if assigned(Cursor) then
  2793. begin
  2794. if Cursor.FSelectable then
  2795. FreeFldBuffers;
  2796. CheckUnPrepare;
  2797. // Some SQLConnections does not support statement [un]preparation,
  2798. // so let them do cleanup f.e. cancel pending queries and/or free resultset
  2799. // if not Prepared then
  2800. // FStatement.DoUnprepare;
  2801. end;
  2802. if DefaultFields then
  2803. DestroyFields;
  2804. FIsEOF := False;
  2805. if assigned(FUpdateQry) then FreeAndNil(FUpdateQry);
  2806. if assigned(FInsertQry) then FreeAndNil(FInsertQry);
  2807. if assigned(FDeleteQry) then FreeAndNil(FDeleteQry);
  2808. // FRecordSize := 0;
  2809. inherited InternalClose;
  2810. end;
  2811. procedure TCustomSQLQuery.InternalInitFieldDefs;
  2812. begin
  2813. if FLoadingFieldDefs then
  2814. Exit;
  2815. FLoadingFieldDefs := True;
  2816. try
  2817. FieldDefs.Clear;
  2818. SQLConnection.AddFieldDefs(Cursor,FieldDefs);
  2819. finally
  2820. FLoadingFieldDefs := False;
  2821. if assigned(Cursor) then Cursor.FInitFieldDef := False;
  2822. end;
  2823. end;
  2824. procedure TCustomSQLQuery.InternalOpen;
  2825. var counter, fieldc : integer;
  2826. F : TField;
  2827. IndexFields : TStrings;
  2828. begin
  2829. if IsReadFromPacket then
  2830. begin
  2831. // When we read from file there is no need for Cursor, also note that Database may not be assigned
  2832. //FStatement.AllocateCursor;
  2833. //Cursor.FSelectable:=True;
  2834. //Cursor.FStatementType:=stSelect;
  2835. FUpdateable:=True;
  2836. end
  2837. else
  2838. begin
  2839. CheckPrepare(False);
  2840. if not Cursor.FSelectable then
  2841. DatabaseError(SErrNoSelectStatement,Self);
  2842. // Call UpdateServerIndexDefs before Execute, to avoid problems with connections
  2843. // which do not allow processing multiple recordsets at a time. (Microsoft
  2844. // calls this MARS, see bug 13241)
  2845. if DefaultFields and FUpdateable and FusePrimaryKeyAsKey and (not IsUniDirectional) then
  2846. UpdateServerIndexDefs;
  2847. FStatement.Execute;
  2848. if (Cursor=nil) or (not Cursor.FSelectable) then
  2849. DatabaseError(SErrNoSelectStatement,Self);
  2850. // InternalInitFieldDef is only called after a prepare. i.e. not twice if
  2851. // a dataset is opened - closed - opened.
  2852. if Cursor.FInitFieldDef then
  2853. InternalInitFieldDefs;
  2854. if DefaultFields then
  2855. begin
  2856. CreateFields;
  2857. if FUpdateable and FusePrimaryKeyAsKey and (not IsUniDirectional) then
  2858. for counter := 0 to ServerIndexDefs.Count-1 do
  2859. if ixPrimary in ServerIndexDefs[counter].Options then
  2860. begin
  2861. IndexFields := TStringList.Create;
  2862. ExtractStrings([';'],[' '],PAnsiChar(ServerIndexDefs[counter].Fields),IndexFields);
  2863. for fieldc := 0 to IndexFields.Count-1 do
  2864. begin
  2865. F := FindField(IndexFields[fieldc]);
  2866. if F <> nil then
  2867. F.ProviderFlags := F.ProviderFlags + [pfInKey];
  2868. end;
  2869. IndexFields.Free;
  2870. end;
  2871. end;
  2872. end;
  2873. BindFields(True);
  2874. if not ReadOnly and not FUpdateable and (FSchemaType=stNoSchema) then
  2875. begin
  2876. if (trim(FDeleteSQL.Text) <> '') or (trim(FUpdateSQL.Text) <> '') or
  2877. (trim(FInsertSQL.Text) <> '') then FUpdateable := True;
  2878. end;
  2879. inherited InternalOpen;
  2880. end;
  2881. procedure TCustomSQLQuery.InternalRefresh;
  2882. begin
  2883. if (ChangeCount>0) and (sqoCancelUpdatesOnRefresh in Options) then
  2884. CancelUpdates;
  2885. inherited InternalRefresh;
  2886. end;
  2887. // public part
  2888. procedure TCustomSQLQuery.CheckPrepare(InfoQuery : Boolean);
  2889. begin
  2890. if Not IsPrepared then
  2891. begin
  2892. FStatement.FInfoquery:=InfoQuery;
  2893. try
  2894. Prepare;
  2895. FDoUnPrepare:=True;
  2896. finally
  2897. FStatement.FInfoquery:=False;
  2898. end;
  2899. end;
  2900. end;
  2901. procedure TCustomSQLQuery.CheckUnPrepare;
  2902. begin
  2903. if FDoUnPrepare then
  2904. begin
  2905. FDoUnPrepare:=False;
  2906. UnPrepare;
  2907. end;
  2908. end;
  2909. procedure TCustomSQLQuery.ExecSQL;
  2910. begin
  2911. CheckPrepare(False);
  2912. try
  2913. Execute;
  2914. // Always retrieve rows affected
  2915. FStatement.RowsAffected;
  2916. If sqoAutoCommit in Options then
  2917. SQLTransaction.Commit;
  2918. finally
  2919. CheckUnPrepare;
  2920. // if not Prepared and (assigned(Database)) and (assigned(Cursor)) then SQLConnection.UnPrepareStatement(Cursor);
  2921. end;
  2922. end;
  2923. procedure TCustomSQLQuery.ApplyUpdates(MaxErrors: Integer);
  2924. begin
  2925. inherited ApplyUpdates(MaxErrors);
  2926. If sqoAutoCommit in Options then
  2927. begin
  2928. // Retrieve rows affected for last update.
  2929. FStatement.RowsAffected;
  2930. SQLTransaction.Commit;
  2931. end;
  2932. end;
  2933. procedure TCustomSQLQuery.Post;
  2934. begin
  2935. inherited Post;
  2936. If (sqoAutoApplyUpdates in Options) then
  2937. ApplyUpdates;
  2938. end;
  2939. procedure TCustomSQLQuery.Delete;
  2940. begin
  2941. inherited Delete;
  2942. If (sqoAutoApplyUpdates in Options) then
  2943. ApplyUpdates;
  2944. end;
  2945. procedure TCustomSQLQuery.SetReadOnly(AValue : Boolean);
  2946. begin
  2947. CheckInactive;
  2948. inherited SetReadOnly(AValue);
  2949. end;
  2950. procedure TCustomSQLQuery.SetParseSQL(AValue : Boolean);
  2951. begin
  2952. CheckInactive;
  2953. FStatement.ParseSQL:=AValue;
  2954. if not AValue then
  2955. FServerFiltered := False;
  2956. end;
  2957. procedure TCustomSQLQuery.SetSQL(const AValue: TStringList);
  2958. begin
  2959. FStatement.SQL.Assign(AValue);
  2960. end;
  2961. procedure TCustomSQLQuery.SetUsePrimaryKeyAsKey(AValue : Boolean);
  2962. begin
  2963. if not Active then FusePrimaryKeyAsKey := AValue
  2964. else
  2965. begin
  2966. // Just temporary, this should be possible in the future
  2967. DatabaseError(SActiveDataset);
  2968. end;
  2969. end;
  2970. procedure TCustomSQLQuery.UpdateServerIndexDefs;
  2971. begin
  2972. FServerIndexDefs.Clear;
  2973. if assigned(DataBase) and (FTableName<>'') then
  2974. SQLConnection.UpdateIndexDefs(ServerIndexDefs,FTableName);
  2975. end;
  2976. function TCustomSQLQuery.NeedLastInsertID: TField;
  2977. Var
  2978. I : Integer;
  2979. begin
  2980. Result:=Nil;
  2981. if sqLastInsertID in SQLConnection.ConnOptions then
  2982. begin
  2983. I:=0;
  2984. While (Result=Nil) and (I<Fields.Count) do
  2985. begin
  2986. Result:=Fields[i];
  2987. if (Result.DataType<>ftAutoInc) or not Result.IsNull then
  2988. Result:=Nil;
  2989. Inc(I);
  2990. end;
  2991. end
  2992. end;
  2993. procedure TCustomSQLQuery.OnChangeSelectSQL(Sender: TObject);
  2994. begin
  2995. if (sqoNoCloseOnSQLChange in Options) then
  2996. exit;
  2997. Close;
  2998. end;
  2999. procedure TCustomSQLQuery.SetMacroChar(AValue: AnsiChar);
  3000. begin
  3001. FStatement.MacroChar:=AValue;
  3002. end;
  3003. function TCustomSQLQuery.RefreshLastInsertID(Field: TField): Boolean;
  3004. begin
  3005. Result:=SQLConnection.RefreshLastInsertID(Self, Field);
  3006. end;
  3007. procedure TCustomSQLQuery.ApplyRecUpdate(UpdateKind: TUpdateKind);
  3008. Var
  3009. DoRefresh : Boolean;
  3010. LastIDField : TField;
  3011. S : TDataSetState;
  3012. begin
  3013. // Moved to connection: the SQLConnection always has more information about types etc.
  3014. // than SQLQuery itself.
  3015. SQLConnection.ApplyRecUpdate(Self,UpdateKind);
  3016. if UpdateKind=ukInsert then
  3017. LastIDField:=NeedLastInsertID
  3018. else
  3019. LastIDField:=nil;
  3020. DoRefresh:=(UpdateKind in [ukModify,ukInsert]) and NeedRefreshRecord(UpdateKind);
  3021. if assigned(LastIDField) or DoRefresh then
  3022. begin
  3023. // updates fields directly in record buffer of TBufDataSet
  3024. // TDataSet buffers are resynchronized at end of ApplyUpdates process
  3025. S:=SetTempState(dsRefreshFields);
  3026. try
  3027. if assigned(LastIDField) then
  3028. RefreshLastInsertID(LastIDField);
  3029. if DoRefresh then
  3030. RefreshRecord(UpdateKind);
  3031. finally
  3032. RestoreState(S);
  3033. end;
  3034. end;
  3035. end;
  3036. procedure TCustomSQLQuery.SetPacketRecords(aValue: integer);
  3037. begin
  3038. if (AValue=PacketRecords) then exit;
  3039. if (AValue<>-1) and (sqoKeepOpenOnCommit in Options) then
  3040. DatabaseError(SErrDisconnectedPacketRecords);
  3041. Inherited SetPacketRecords(aValue);
  3042. end;
  3043. function TCustomSQLQuery.GetCanModify: Boolean;
  3044. begin
  3045. // the test for assigned(Cursor) is needed for the case that the dataset isn't opened
  3046. if assigned(Cursor) and (Cursor.FStatementType = stSelect) then
  3047. Result:= FUpdateable and (not ReadOnly) and (not IsUniDirectional)
  3048. else
  3049. Result := False;
  3050. end;
  3051. procedure TCustomSQLQuery.SetUpdateMode(AValue : TUpdateMode);
  3052. begin
  3053. FUpdateMode := AValue;
  3054. end;
  3055. procedure TCustomSQLQuery.SetSchemaInfo( ASchemaType : TSchemaType; ASchemaObjectName, ASchemaPattern : string);
  3056. begin
  3057. FSchemaType:=ASchemaType;
  3058. FSchemaObjectName:=ASchemaObjectName;
  3059. FSchemaPattern:=ASchemaPattern;
  3060. end;
  3061. procedure TCustomSQLQuery.BeforeRefreshOpenCursor;
  3062. begin
  3063. // This is only necessary because TIBConnection can not re-open a
  3064. // prepared cursor. In fact this is wrong, but has never led to
  3065. // problems because in SetActive(false) queries are always
  3066. // unprepared. (which is also wrong, but has to be fixed later)
  3067. if IsPrepared then with SQLConnection do
  3068. UnPrepareStatement(Cursor);
  3069. end;
  3070. function TCustomSQLQuery.CreateParams: TSQLDBParams;
  3071. begin
  3072. Result:=TSQLDBParams.Create(Nil);
  3073. end;
  3074. function TCustomSQLQuery.LogEvent(EventType: TDBEventType): Boolean;
  3075. begin
  3076. Result:=Assigned(Database) and SQLConnection.LogEvent(EventType);
  3077. end;
  3078. procedure TCustomSQLQuery.Log(EventType: TDBEventType; const Msg: String);
  3079. Var
  3080. M : String;
  3081. begin
  3082. If LogEvent(EventType) then
  3083. begin
  3084. M:=Msg;
  3085. If (Name<>'') then
  3086. M:=Name+' : '+M;
  3087. SQLConnection.Log(EventType,M);
  3088. end;
  3089. end;
  3090. class function TCustomSQLQuery.FieldDefsClass: TFieldDefsClass;
  3091. begin
  3092. Result:=TSQLDBFieldDefs;
  3093. end;
  3094. function TCustomSQLQuery.GetStatementType : TStatementType;
  3095. begin
  3096. if Assigned(Cursor) then
  3097. Result:=Cursor.FStatementType
  3098. else
  3099. Result:=stUnknown;
  3100. end;
  3101. function TCustomSQLQuery.HasMacros: Boolean;
  3102. begin
  3103. Result := Macros.Count > 0;
  3104. end;
  3105. function TCustomSQLQuery.HasParams: Boolean;
  3106. begin
  3107. Result := Params.Count > 0;
  3108. end;
  3109. procedure TCustomSQLQuery.SetParamCheck(AValue: Boolean);
  3110. begin
  3111. FStatement.ParamCheck:=AValue;
  3112. end;
  3113. procedure TCustomSQLQuery.SetMacroCheck(AValue: Boolean);
  3114. begin
  3115. FStatement.MacroCheck:=AValue;
  3116. end;
  3117. procedure TCustomSQLQuery.SetOptions(AValue: TSQLQueryOptions);
  3118. begin
  3119. if FOptions=AValue then Exit;
  3120. CheckInactive;
  3121. FOptions:=AValue;
  3122. if sqoKeepOpenOnCommit in FOptions then
  3123. PacketRecords:=-1;
  3124. end;
  3125. procedure TCustomSQLQuery.SetSQLConnection(AValue: TSQLConnection);
  3126. begin
  3127. Database:=AValue;
  3128. end;
  3129. procedure TCustomSQLQuery.SetSQLTransaction(AValue: TSQLTransaction);
  3130. begin
  3131. Transaction:=AValue;
  3132. end;
  3133. procedure TCustomSQLQuery.SetInsertSQL(const AValue: TStringList);
  3134. begin
  3135. FInsertSQL.Assign(AValue);
  3136. end;
  3137. procedure TCustomSQLQuery.SetUpdateSQL(const AValue: TStringList);
  3138. begin
  3139. FUpdateSQL.Assign(AValue);
  3140. end;
  3141. procedure TCustomSQLQuery.SetDeleteSQL(const AValue: TStringList);
  3142. begin
  3143. FDeleteSQL.Assign(AValue);
  3144. end;
  3145. procedure TCustomSQLQuery.SetRefreshSQL(const AValue: TStringList);
  3146. begin
  3147. FRefreshSQL.Assign(AValue);
  3148. end;
  3149. procedure TCustomSQLQuery.SetParams(AValue: TParams);
  3150. begin
  3151. FStatement.Params.Assign(AValue);
  3152. end;
  3153. procedure TCustomSQLQuery.SetMacros(AValue: TParams);
  3154. begin
  3155. FStatement.Macros.Assign(AValue);
  3156. end;
  3157. procedure TCustomSQLQuery.SetDataSource(AValue: TDataSource);
  3158. Var
  3159. DS : TDataSource;
  3160. begin
  3161. DS:=DataSource;
  3162. If (AValue<>DS) then
  3163. begin
  3164. If Assigned(AValue) and (AValue.Dataset=Self) then
  3165. DatabaseError(SErrCircularDataSourceReferenceNotAllowed,Self);
  3166. If Assigned(DS) then
  3167. DS.RemoveFreeNotification(Self);
  3168. FStatement.DataSource:=AValue;
  3169. end;
  3170. end;
  3171. function TCustomSQLQuery.GetDataSource: TDataSource;
  3172. begin
  3173. If Assigned(FStatement) then
  3174. Result:=FStatement.DataSource
  3175. else
  3176. Result:=Nil;
  3177. end;
  3178. procedure TCustomSQLQuery.Notification(AComponent: TComponent; Operation: TOperation);
  3179. begin
  3180. Inherited;
  3181. If (Operation=opRemove) and (AComponent=DataSource) then
  3182. DataSource:=Nil;
  3183. end;
  3184. procedure TCustomSQLQuery.DoOnNewRecord;
  3185. begin
  3186. inherited;
  3187. if FSequence.ApplyEvent = saeOnNewRecord then
  3188. FSequence.Apply;
  3189. end;
  3190. procedure TCustomSQLQuery.DoBeforePost;
  3191. begin
  3192. if (State = dsInsert) and (FSequence.ApplyEvent = saeOnPost) then
  3193. FSequence.Apply;
  3194. inherited;
  3195. end;
  3196. function TCustomSQLQuery.PSGetUpdateException(E: Exception; Prev: EUpdateError): EUpdateError;
  3197. var
  3198. PrevErrorCode, ErrorCode: Integer;
  3199. begin
  3200. if Assigned(Prev) then
  3201. PrevErrorCode := Prev.ErrorCode
  3202. else
  3203. PrevErrorCode := 0;
  3204. if E is ESQLDatabaseError then
  3205. ErrorCode := ESQLDatabaseError(E).ErrorCode
  3206. else
  3207. ErrorCode := 0;
  3208. Result := EUpdateError.Create(SOnUpdateError, E.Message, ErrorCode, PrevErrorCode, E);
  3209. end;
  3210. function TCustomSQLQuery.PSGetTableName: string;
  3211. begin
  3212. Result := FTableName;
  3213. end;
  3214. { TSQLScript }
  3215. procedure TSQLScript.ExecuteStatement(SQLStatement: TStrings;
  3216. var StopExecution: Boolean);
  3217. begin
  3218. fquery.SQL.assign(SQLStatement);
  3219. fquery.ExecSQL;
  3220. end;
  3221. procedure TSQLScript.ExecuteDirective(Directive, Argument: String;
  3222. var StopExecution: Boolean);
  3223. begin
  3224. if assigned (FOnDirective) then
  3225. FOnDirective (Self, Directive, Argument, StopExecution);
  3226. end;
  3227. procedure TSQLScript.ExecuteCommit(CommitRetaining: boolean=true);
  3228. begin
  3229. if FTransaction is TSQLTransaction then
  3230. if CommitRetaining then
  3231. TSQLTransaction(FTransaction).CommitRetaining
  3232. else
  3233. begin
  3234. TSQLTransaction(FTransaction).Commit;
  3235. TSQLTransaction(FTransaction).StartTransaction;
  3236. end
  3237. else
  3238. begin
  3239. FTransaction.Active := false;
  3240. FTransaction.Active := true;
  3241. end;
  3242. end;
  3243. procedure TSQLScript.SetDatabase(Value: TDatabase);
  3244. begin
  3245. FDatabase := Value;
  3246. end;
  3247. procedure TSQLScript.SetTransaction(Value: TDBTransaction);
  3248. begin
  3249. FTransaction := Value;
  3250. end;
  3251. procedure TSQLScript.CheckDatabase;
  3252. begin
  3253. If (FDatabase=Nil) then
  3254. DatabaseError(SErrNoDatabaseAvailable,Self)
  3255. end;
  3256. function TSQLScript.CreateQuery: TCustomSQLQuery;
  3257. begin
  3258. Result := TCustomSQLQuery.Create(nil);
  3259. Result.ParamCheck := false; // Do not parse for parameters; breaks use of e.g. select bla into :bla in Firebird procedures
  3260. end;
  3261. constructor TSQLScript.Create(AOwner: TComponent);
  3262. begin
  3263. inherited Create(AOwner);
  3264. FQuery := CreateQuery;
  3265. end;
  3266. destructor TSQLScript.Destroy;
  3267. begin
  3268. FQuery.Free;
  3269. inherited Destroy;
  3270. end;
  3271. procedure TSQLScript.Execute;
  3272. begin
  3273. FQuery.DataBase := FDatabase;
  3274. FQuery.Transaction := FTransaction;
  3275. inherited Execute;
  3276. end;
  3277. procedure TSQLScript.ExecuteScript;
  3278. begin
  3279. Execute;
  3280. end;
  3281. { Connection definitions }
  3282. Var
  3283. ConnDefs : TStringList;
  3284. Procedure CheckDefs;
  3285. begin
  3286. If (ConnDefs=Nil) then
  3287. begin
  3288. ConnDefs:=TStringList.Create;
  3289. ConnDefs.Sorted:=True;
  3290. ConnDefs.Duplicates:=dupError;
  3291. end;
  3292. end;
  3293. Procedure DoneDefs;
  3294. Var
  3295. I : Integer;
  3296. begin
  3297. If Assigned(ConnDefs) then
  3298. begin
  3299. For I:=ConnDefs.Count-1 downto 0 do
  3300. begin
  3301. ConnDefs.Objects[i].Free;
  3302. ConnDefs.Delete(I);
  3303. end;
  3304. FreeAndNil(ConnDefs);
  3305. end;
  3306. end;
  3307. Function GetConnectionDef(const ConnectorName : String) : TConnectionDef;
  3308. Var
  3309. I : Integer;
  3310. begin
  3311. CheckDefs;
  3312. I:=ConnDefs.IndexOf(ConnectorName);
  3313. If (I<>-1) then
  3314. Result:=TConnectionDef(ConnDefs.Objects[i])
  3315. else
  3316. Result:=Nil;
  3317. end;
  3318. procedure RegisterConnection(Def: TConnectionDefClass);
  3319. Var
  3320. I : Integer;
  3321. begin
  3322. CheckDefs;
  3323. I:=ConnDefs.IndexOf(Def.TypeName);
  3324. If (I=-1) then
  3325. ConnDefs.AddObject(Def.TypeName,Def.Create)
  3326. else
  3327. begin
  3328. ConnDefs.Objects[I].Free;
  3329. ConnDefs.Objects[I]:=Def.Create;
  3330. end;
  3331. end;
  3332. procedure UnRegisterConnection(Def: TConnectionDefClass);
  3333. begin
  3334. UnRegisterConnection(Def.TypeName);
  3335. end;
  3336. procedure UnRegisterConnection(const ConnectionName: String);
  3337. Var
  3338. I : Integer;
  3339. begin
  3340. if (ConnDefs<>Nil) then
  3341. begin
  3342. I:=ConnDefs.IndexOf(ConnectionName);
  3343. If (I<>-1) then
  3344. begin
  3345. ConnDefs.Objects[I].Free;
  3346. ConnDefs.Delete(I);
  3347. end;
  3348. end;
  3349. end;
  3350. procedure GetConnectionList(List: TSTrings);
  3351. begin
  3352. CheckDefs;
  3353. List.Text:=ConnDefs.Text;
  3354. end;
  3355. { TSQLConnector }
  3356. procedure TSQLConnector.SetConnectorType(const AValue: String);
  3357. begin
  3358. if FConnectorType<>AValue then
  3359. begin
  3360. CheckDisconnected;
  3361. If Assigned(FProxy) then
  3362. FreeProxy;
  3363. FConnectorType:=AValue;
  3364. CreateProxy;
  3365. end;
  3366. end;
  3367. procedure TSQLConnector.SetForcedClose(AValue: Boolean);
  3368. begin
  3369. inherited SetForcedClose(AValue);
  3370. FProxy.ForcedClose:=aValue;
  3371. end;
  3372. procedure TSQLConnector.SetTransaction(Value: TSQLTransaction);
  3373. begin
  3374. inherited SetTransaction(Value);
  3375. If Assigned(FProxy) and (FProxy.Transaction<>Value) then
  3376. FProxy.FTransaction:=Value;
  3377. end;
  3378. procedure TSQLConnector.DoInternalConnect;
  3379. Var
  3380. D : TConnectionDef;
  3381. begin
  3382. inherited DoInternalConnect;
  3383. CheckProxy;
  3384. FProxy.CharSet:=Self.CharSet;
  3385. FProxy.DatabaseName:=Self.DatabaseName;
  3386. FProxy.HostName:=Self.HostName;
  3387. FProxy.LogEvents:=Self.LogEvents;
  3388. FProxy.Password:=Self.Password;
  3389. FProxy.Role:=Self.Role;
  3390. FProxy.UserName:=Self.UserName;
  3391. FProxy.FTransaction:=Self.Transaction;
  3392. FProxy.LogEvents:=Self.LogEvents;
  3393. FProxy.OnLog:=Self.OnLog;
  3394. FProxy.Options:=Self.Options;
  3395. D:=GetConnectionDef(ConnectorType);
  3396. D.ApplyParams(Params,FProxy);
  3397. FProxy.Connected:=True;
  3398. end;
  3399. procedure TSQLConnector.DoInternalDisconnect;
  3400. begin
  3401. FProxy.Connected:=False;
  3402. inherited DoInternalDisconnect;
  3403. end;
  3404. procedure TSQLConnector.CheckProxy;
  3405. begin
  3406. If (FProxy=Nil) then
  3407. CreateProxy;
  3408. end;
  3409. procedure TSQLConnector.CreateProxy;
  3410. Var
  3411. D : TConnectionDef;
  3412. begin
  3413. D:=GetConnectionDef(ConnectorType);
  3414. If (D=Nil) then
  3415. DatabaseErrorFmt(SErrUnknownConnectorType,[ConnectorType],Self);
  3416. FProxy:=D.ConnectionClass.Create(Self);
  3417. FFieldNameQuoteChars := FProxy.FieldNameQuoteChars;
  3418. FConnOptions := FProxy.ConnOptions;
  3419. end;
  3420. procedure TSQLConnector.FreeProxy;
  3421. begin
  3422. FProxy.Connected:=False;
  3423. FreeAndNil(FProxy);
  3424. end;
  3425. function TSQLConnector.StrToStatementType(s: string): TStatementType;
  3426. begin
  3427. CheckProxy;
  3428. Result:=FProxy.StrToStatementType(s);
  3429. end;
  3430. function TSQLConnector.GetAsSQLText(Field: TField): string;
  3431. begin
  3432. CheckProxy;
  3433. Result:=FProxy.GetAsSQLText(Field);
  3434. end;
  3435. function TSQLConnector.GetAsSQLText(Param: TParam): string;
  3436. begin
  3437. CheckProxy;
  3438. Result:=FProxy.GetAsSQLText(Param);
  3439. end;
  3440. function TSQLConnector.GetHandle: pointer;
  3441. begin
  3442. CheckProxy;
  3443. Result:=FProxy.GetHandle;
  3444. end;
  3445. function TSQLConnector.AllocateCursorHandle: TSQLCursor;
  3446. begin
  3447. CheckProxy;
  3448. Result:=FProxy.AllocateCursorHandle;
  3449. end;
  3450. procedure TSQLConnector.DeAllocateCursorHandle(var cursor: TSQLCursor);
  3451. begin
  3452. CheckProxy;
  3453. FProxy.DeAllocateCursorHandle(cursor);
  3454. end;
  3455. function TSQLConnector.AllocateTransactionHandle: TSQLHandle;
  3456. begin
  3457. CheckProxy;
  3458. Result:=FProxy.AllocateTransactionHandle;
  3459. end;
  3460. procedure TSQLConnector.PrepareStatement(cursor: TSQLCursor;
  3461. ATransaction: TSQLTransaction; buf: string; AParams: TParams);
  3462. begin
  3463. CheckProxy;
  3464. FProxy.PrepareStatement(cursor, ATransaction, buf, AParams);
  3465. end;
  3466. procedure TSQLConnector.Execute(cursor: TSQLCursor;
  3467. atransaction: tSQLtransaction; AParams: TParams);
  3468. begin
  3469. CheckProxy;
  3470. FProxy.Execute(cursor, atransaction, AParams);
  3471. end;
  3472. function TSQLConnector.Fetch(cursor: TSQLCursor): boolean;
  3473. begin
  3474. CheckProxy;
  3475. Result:=FProxy.Fetch(cursor);
  3476. end;
  3477. procedure TSQLConnector.AddFieldDefs(cursor: TSQLCursor; FieldDefs: TfieldDefs
  3478. );
  3479. begin
  3480. CheckProxy;
  3481. FProxy.AddFieldDefs(cursor, FieldDefs);
  3482. end;
  3483. procedure TSQLConnector.UnPrepareStatement(cursor: TSQLCursor);
  3484. begin
  3485. CheckProxy;
  3486. FProxy.UnPrepareStatement(cursor);
  3487. end;
  3488. procedure TSQLConnector.FreeFldBuffers(cursor: TSQLCursor);
  3489. begin
  3490. CheckProxy;
  3491. FProxy.FreeFldBuffers(cursor);
  3492. end;
  3493. function TSQLConnector.GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string;
  3494. begin
  3495. Result:=Proxy.GetNextValueSQL(SequenceName, IncrementBy);
  3496. end;
  3497. function TSQLConnector.LoadField(cursor: TSQLCursor; FieldDef: TFieldDef;
  3498. buffer: pointer; out CreateBlob: boolean): boolean;
  3499. begin
  3500. CheckProxy;
  3501. Result:=FProxy.LoadField(cursor, FieldDef, buffer, CreateBlob);
  3502. end;
  3503. procedure TSQLConnector.LoadBlobIntoBuffer(FieldDef: TFieldDef;
  3504. ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction: TSQLTransaction);
  3505. begin
  3506. CheckProxy;
  3507. FProxy.LoadBlobIntoBuffer(FieldDef, ABlobBuf, cursor, ATransaction);
  3508. end;
  3509. function TSQLConnector.RowsAffected(cursor: TSQLCursor): TRowsCount;
  3510. begin
  3511. CheckProxy;
  3512. Result := FProxy.RowsAffected(cursor);
  3513. end;
  3514. function TSQLConnector.GetTransactionHandle(trans: TSQLHandle): pointer;
  3515. begin
  3516. CheckProxy;
  3517. Result:=FProxy.GetTransactionHandle(trans);
  3518. end;
  3519. function TSQLConnector.Commit(trans: TSQLHandle): boolean;
  3520. begin
  3521. CheckProxy;
  3522. Result:=FProxy.Commit(trans);
  3523. end;
  3524. function TSQLConnector.RollBack(trans: TSQLHandle): boolean;
  3525. begin
  3526. CheckProxy;
  3527. Result:=FProxy.RollBack(trans);
  3528. end;
  3529. function TSQLConnector.StartDBTransaction(trans: TSQLHandle; aParams: string): boolean;
  3530. begin
  3531. CheckProxy;
  3532. Result:=FProxy.StartDBTransaction(trans, aParams);
  3533. end;
  3534. procedure TSQLConnector.CommitRetaining(trans: TSQLHandle);
  3535. begin
  3536. CheckProxy;
  3537. FProxy.CommitRetaining(trans);
  3538. end;
  3539. procedure TSQLConnector.RollBackRetaining(trans: TSQLHandle);
  3540. begin
  3541. CheckProxy;
  3542. FProxy.RollBackRetaining(trans);
  3543. end;
  3544. procedure TSQLConnector.UpdateIndexDefs(IndexDefs: TIndexDefs;
  3545. TableName: string);
  3546. begin
  3547. CheckProxy;
  3548. FProxy.UpdateIndexDefs(IndexDefs, TableName);
  3549. end;
  3550. function TSQLConnector.GetSchemaInfoSQL(SchemaType: TSchemaType;
  3551. SchemaObjectName, SchemaPattern: string): string;
  3552. begin
  3553. CheckProxy;
  3554. Result:=FProxy.GetSchemaInfoSQL(SchemaType, SchemaObjectName, SchemaPattern);
  3555. end;
  3556. { TConnectionDef }
  3557. class function TConnectionDef.TypeName: String;
  3558. begin
  3559. Result:='';
  3560. end;
  3561. class function TConnectionDef.ConnectionClass: TSQLConnectionClass;
  3562. begin
  3563. Result:=Nil;
  3564. end;
  3565. class function TConnectionDef.Description: String;
  3566. begin
  3567. Result:='';
  3568. end;
  3569. class function TConnectionDef.DefaultLibraryName: String;
  3570. begin
  3571. Result:='';
  3572. end;
  3573. class function TConnectionDef.LoadFunction: TLibraryLoadFunction;
  3574. begin
  3575. Result:=Nil;
  3576. end;
  3577. class function TConnectionDef.UnLoadFunction: TLibraryUnLoadFunction;
  3578. begin
  3579. Result:=Nil;
  3580. end;
  3581. class function TConnectionDef.LoadedLibraryName: string;
  3582. begin
  3583. Result:='';
  3584. end;
  3585. procedure TConnectionDef.ApplyParams(Params: TStrings;
  3586. AConnection: TSQLConnection);
  3587. begin
  3588. AConnection.Params.Assign(Params);
  3589. end;
  3590. { TServerIndexDefs }
  3591. constructor TServerIndexDefs.create(ADataset: TDataset);
  3592. begin
  3593. if not (ADataset is TCustomSQLQuery) then
  3594. DatabaseErrorFmt(SErrNotASQLQuery,[ADataset.Name]);
  3595. inherited create(ADataset);
  3596. end;
  3597. procedure TServerIndexDefs.Update;
  3598. begin
  3599. if (not updated) and assigned(Dataset) then
  3600. begin
  3601. TCustomSQLQuery(Dataset).UpdateServerIndexDefs;
  3602. updated := True;
  3603. end;
  3604. end;
  3605. Initialization
  3606. Finalization
  3607. DoneDefs;
  3608. end.