sqldb.pp 105 KB

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