sqldb.pp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. {
  2. Copyright (c) 2004 by Joost van der Sluis
  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;
  16. type TSchemaType = (stNoSchema, stTables, stSysTables, stProcedures, stColumns, stProcedureParams, stIndexes, stPackages);
  17. TConnOption = (sqSupportParams,sqEscapeSlash,sqEscapeRepeat,sqQuoteFieldnames);
  18. TConnOptions= set of TConnOption;
  19. TRowsCount = LargeInt;
  20. type
  21. TSQLConnection = class;
  22. TSQLTransaction = class;
  23. TCustomSQLQuery = class;
  24. TSQLQuery = class;
  25. TSQLScript = class;
  26. TStatementType = (stNone, stSelect, stInsert, stUpdate, stDelete,
  27. stDDL, stGetSegment, stPutSegment, stExecProcedure,
  28. stStartTrans, stCommit, stRollback, stSelectForUpd);
  29. TSQLHandle = Class(TObject)
  30. end;
  31. { TSQLCursor }
  32. TSQLCursor = Class(TSQLHandle)
  33. public
  34. FPrepared : Boolean;
  35. FInitFieldDef : Boolean;
  36. FStatementType : TStatementType;
  37. end;
  38. const
  39. StatementTokens : Array[TStatementType] of string = ('(none)', 'select',
  40. 'insert', 'update', 'delete',
  41. 'create', 'get', 'put', 'execute',
  42. 'start','commit','rollback', '?'
  43. );
  44. type
  45. { TServerIndexDefs }
  46. TServerIndexDefs = class(TIndexDefs)
  47. Private
  48. public
  49. constructor Create(ADataSet: TDataSet); override;
  50. procedure Update; override;
  51. end;
  52. { TSQLConnection }
  53. type
  54. { TSQLConnection }
  55. TSQLConnection = class (TDatabase)
  56. private
  57. FPassword : string;
  58. FTransaction : TSQLTransaction;
  59. FUserName : string;
  60. FHostName : string;
  61. FCharSet : string;
  62. FRole : String;
  63. function GetPort: cardinal;
  64. procedure Setport(const AValue: cardinal);
  65. protected
  66. FConnOptions : TConnOptions;
  67. procedure GetDBInfo(const SchemaType : TSchemaType; const SchemaObjectName, ReturnField : string; List: TStrings);
  68. procedure SetTransaction(Value : TSQLTransaction);virtual;
  69. function StrToStatementType(s : string) : TStatementType; virtual;
  70. procedure DoInternalConnect; override;
  71. procedure DoInternalDisconnect; override;
  72. function GetAsSQLText(Field : TField) : string; overload; virtual;
  73. function GetAsSQLText(Param : TParam) : string; overload; virtual;
  74. function GetHandle : pointer; virtual; virtual;
  75. Function AllocateCursorHandle : TSQLCursor; virtual; abstract;
  76. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); virtual; abstract;
  77. Function AllocateTransactionHandle : TSQLHandle; virtual; abstract;
  78. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); virtual; abstract;
  79. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); virtual; abstract;
  80. function Fetch(cursor : TSQLCursor) : boolean; virtual; abstract;
  81. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); virtual; abstract;
  82. procedure UnPrepareStatement(cursor : TSQLCursor); virtual; abstract;
  83. procedure FreeFldBuffers(cursor : TSQLCursor); virtual;
  84. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; virtual; abstract;
  85. function GetTransactionHandle(trans : TSQLHandle): pointer; virtual; abstract;
  86. function Commit(trans : TSQLHandle) : boolean; virtual; abstract;
  87. function RollBack(trans : TSQLHandle) : boolean; virtual; abstract;
  88. function StartdbTransaction(trans : TSQLHandle; aParams : string) : boolean; virtual; abstract;
  89. procedure CommitRetaining(trans : TSQLHandle); virtual; abstract;
  90. procedure RollBackRetaining(trans : TSQLHandle); virtual; abstract;
  91. procedure UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string); virtual;
  92. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; virtual;
  93. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); virtual; abstract;
  94. function RowsAffected(cursor: TSQLCursor): TRowsCount; virtual;
  95. property port: cardinal read GetPort write Setport;
  96. public
  97. property Handle: Pointer read GetHandle;
  98. destructor Destroy; override;
  99. procedure StartTransaction; override;
  100. procedure EndTransaction; override;
  101. property ConnOptions: TConnOptions read FConnOptions;
  102. procedure ExecuteDirect(SQL : String); overload; virtual;
  103. procedure ExecuteDirect(SQL : String; ATransaction : TSQLTransaction); overload; virtual;
  104. procedure GetTableNames(List : TStrings; SystemTables : Boolean = false); virtual;
  105. procedure GetProcedureNames(List : TStrings); virtual;
  106. procedure GetFieldNames(const TableName : string; List : TStrings); virtual;
  107. procedure CreateDB; virtual;
  108. procedure DropDB; virtual;
  109. published
  110. property Password : string read FPassword write FPassword;
  111. property Transaction : TSQLTransaction read FTransaction write SetTransaction;
  112. property UserName : string read FUserName write FUserName;
  113. property CharSet : string read FCharSet write FCharSet;
  114. property HostName : string Read FHostName Write FHostName;
  115. property Connected;
  116. Property Role : String read FRole write FRole;
  117. property DatabaseName;
  118. property KeepConnection;
  119. property LoginPrompt;
  120. property Params;
  121. property OnLogin;
  122. end;
  123. { TSQLTransaction }
  124. TCommitRollbackAction = (caNone, caCommit, caCommitRetaining, caRollback,
  125. caRollbackRetaining);
  126. TSQLTransaction = class (TDBTransaction)
  127. private
  128. FTrans : TSQLHandle;
  129. FAction : TCommitRollbackAction;
  130. FParams : TStringList;
  131. protected
  132. function GetHandle : Pointer; virtual;
  133. Procedure SetDatabase (Value : TDatabase); override;
  134. public
  135. procedure Commit; virtual;
  136. procedure CommitRetaining; virtual;
  137. procedure Rollback; virtual;
  138. procedure RollbackRetaining; virtual;
  139. procedure StartTransaction; override;
  140. constructor Create(AOwner : TComponent); override;
  141. destructor Destroy; override;
  142. property Handle: Pointer read GetHandle;
  143. procedure EndTransaction; override;
  144. published
  145. property Action : TCommitRollbackAction read FAction write FAction;
  146. property Database;
  147. property Params : TStringList read FParams write FParams;
  148. end;
  149. { TCustomSQLQuery }
  150. TCustomSQLQuery = class (Tbufdataset)
  151. private
  152. FCursor : TSQLCursor;
  153. FUpdateable : boolean;
  154. FTableName : string;
  155. FSQL : TStringList;
  156. FUpdateSQL,
  157. FInsertSQL,
  158. FDeleteSQL : TStringList;
  159. FIsEOF : boolean;
  160. FLoadingFieldDefs : boolean;
  161. FReadOnly : boolean;
  162. FUpdateMode : TUpdateMode;
  163. FParams : TParams;
  164. FusePrimaryKeyAsKey : Boolean;
  165. FSQLBuf : String;
  166. FWhereStartPos : integer;
  167. FWhereStopPos : integer;
  168. FParseSQL : boolean;
  169. FMasterLink : TMasterParamsDatalink;
  170. // FSchemaInfo : TSchemaInfo;
  171. FServerFilterText : string;
  172. FServerFiltered : Boolean;
  173. FServerIndexDefs : TServerIndexDefs;
  174. FUpdateQry,
  175. FDeleteQry,
  176. FInsertQry : TCustomSQLQuery;
  177. procedure FreeFldBuffers;
  178. function GetServerIndexDefs: TServerIndexDefs;
  179. function GetStatementType : TStatementType;
  180. procedure SetReadOnly(AValue : Boolean);
  181. procedure SetParseSQL(AValue : Boolean);
  182. procedure SetUsePrimaryKeyAsKey(AValue : Boolean);
  183. procedure SetUpdateMode(AValue : TUpdateMode);
  184. procedure OnChangeSQL(Sender : TObject);
  185. procedure OnChangeModifySQL(Sender : TObject);
  186. procedure Execute;
  187. Procedure SQLParser(var ASQL : string);
  188. procedure ApplyFilter;
  189. Function AddFilter(SQLstr : string) : string;
  190. protected
  191. // abstract & virtual methods of TBufDataset
  192. function Fetch : boolean; override;
  193. function LoadField(FieldDef : TFieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; override;
  194. // abstract & virtual methods of TDataset
  195. procedure UpdateServerIndexDefs; virtual;
  196. procedure SetDatabase(Value : TDatabase); override;
  197. Procedure SetTransaction(Value : TDBTransaction); override;
  198. procedure InternalAddRecord(Buffer: Pointer; AAppend: Boolean); override;
  199. procedure InternalClose; override;
  200. procedure InternalInitFieldDefs; override;
  201. procedure InternalOpen; override;
  202. function GetCanModify: Boolean; override;
  203. procedure ApplyRecUpdate(UpdateKind : TUpdateKind); override;
  204. Function IsPrepared : Boolean; virtual;
  205. Procedure SetActive (Value : Boolean); override;
  206. procedure SetServerFiltered(Value: Boolean); virtual;
  207. procedure SetServerFilterText(const Value: string); virtual;
  208. Function GetDataSource : TDatasource; override;
  209. Procedure SetDataSource(AValue : TDatasource);
  210. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField); override;
  211. public
  212. procedure Prepare; virtual;
  213. procedure UnPrepare; virtual;
  214. procedure ExecSQL; virtual;
  215. constructor Create(AOwner : TComponent); override;
  216. destructor Destroy; override;
  217. procedure SetSchemaInfo( SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string); virtual;
  218. property Prepared : boolean read IsPrepared;
  219. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  220. function RowsAffected: TRowsCount; virtual;
  221. protected
  222. // redeclared data set properties
  223. property Active;
  224. property Filter;
  225. property Filtered;
  226. // property FilterOptions;
  227. property BeforeOpen;
  228. property AfterOpen;
  229. property BeforeClose;
  230. property AfterClose;
  231. property BeforeInsert;
  232. property AfterInsert;
  233. property BeforeEdit;
  234. property AfterEdit;
  235. property BeforePost;
  236. property AfterPost;
  237. property BeforeCancel;
  238. property AfterCancel;
  239. property BeforeDelete;
  240. property AfterDelete;
  241. property BeforeScroll;
  242. property AfterScroll;
  243. property OnCalcFields;
  244. property OnDeleteError;
  245. property OnEditError;
  246. property OnFilterRecord;
  247. property OnNewRecord;
  248. property OnPostError;
  249. property AutoCalcFields;
  250. property Database;
  251. // protected
  252. // property SchemaInfo : TSchemaInfo read FSchemaInfo default stNoSchema;
  253. property Transaction;
  254. property ReadOnly : Boolean read FReadOnly write SetReadOnly;
  255. property SQL : TStringlist read FSQL write FSQL;
  256. property UpdateSQL : TStringlist read FUpdateSQL;
  257. property InsertSQL : TStringlist read FInsertSQL;
  258. property DeleteSQL : TStringlist read FDeleteSQL;
  259. property Params : TParams read FParams write FParams;
  260. property UpdateMode : TUpdateMode read FUpdateMode write SetUpdateMode;
  261. property UsePrimaryKeyAsKey : boolean read FUsePrimaryKeyAsKey write SetUsePrimaryKeyAsKey;
  262. property StatementType : TStatementType read GetStatementType;
  263. property ParseSQL : Boolean read FParseSQL write SetParseSQL default true;
  264. Property DataSource : TDatasource Read GetDataSource Write SetDatasource;
  265. property ServerFilter: string read FServerFilterText write SetServerFilterText;
  266. property ServerFiltered: Boolean read FServerFiltered write SetServerFiltered default False;
  267. property ServerIndexDefs : TServerIndexDefs read GetServerIndexDefs;
  268. end;
  269. { TSQLQuery }
  270. TSQLQuery = Class(TCustomSQLQuery)
  271. Published
  272. // TDataset stuff
  273. Property Active;
  274. Property AutoCalcFields;
  275. Property Filter;
  276. Property Filtered;
  277. Property AfterCancel;
  278. Property AfterClose;
  279. Property AfterDelete;
  280. Property AfterEdit;
  281. Property AfterInsert;
  282. Property AfterOpen;
  283. Property AfterPost;
  284. Property AfterScroll;
  285. Property BeforeCancel;
  286. Property BeforeClose;
  287. Property BeforeDelete;
  288. Property BeforeEdit;
  289. Property BeforeInsert;
  290. Property BeforeOpen;
  291. Property BeforePost;
  292. Property BeforeScroll;
  293. Property OnCalcFields;
  294. Property OnDeleteError;
  295. Property OnEditError;
  296. Property OnFilterRecord;
  297. Property OnNewRecord;
  298. Property OnPostError;
  299. // property SchemaInfo default stNoSchema;
  300. property Database;
  301. property Transaction;
  302. property ReadOnly;
  303. property SQL;
  304. property UpdateSQL;
  305. property InsertSQL;
  306. property DeleteSQL;
  307. property IndexDefs;
  308. property Params;
  309. property UpdateMode;
  310. property UsePrimaryKeyAsKey;
  311. property ParseSQL;
  312. Property DataSource;
  313. property ServerFilter;
  314. property ServerFiltered;
  315. property ServerIndexDefs;
  316. end;
  317. { TSQLScript }
  318. TSQLScript = class (Tcomponent)
  319. private
  320. FScript : TStrings;
  321. FQuery : TCustomSQLQuery;
  322. FDatabase : TDatabase;
  323. FTransaction : TDBTransaction;
  324. protected
  325. procedure SetScript(const AValue: TStrings);
  326. Procedure SetDatabase (Value : TDatabase); virtual;
  327. Procedure SetTransaction(Value : TDBTransaction); virtual;
  328. Procedure CheckDatabase;
  329. public
  330. constructor Create(AOwner : TComponent); override;
  331. destructor Destroy; override;
  332. procedure ExecuteScript;
  333. Property Script : TStrings Read FScript Write SetScript;
  334. Property DataBase : TDatabase Read FDatabase Write SetDatabase;
  335. Property Transaction : TDBTransaction Read FTransaction Write SetTransaction;
  336. end;
  337. { TSQLConnector }
  338. TSQLConnector = Class(TSQLConnection)
  339. private
  340. FProxy : TSQLConnection;
  341. FConnectorType: String;
  342. procedure SetConnectorType(const AValue: String);
  343. protected
  344. procedure SetTransaction(Value : TSQLTransaction);override;
  345. procedure DoInternalConnect; override;
  346. procedure DoInternalDisconnect; override;
  347. Procedure CheckProxy;
  348. Procedure CreateProxy; virtual;
  349. Procedure FreeProxy; virtual;
  350. function StrToStatementType(s : string) : TStatementType; override;
  351. function GetAsSQLText(Field : TField) : string; overload; override;
  352. function GetAsSQLText(Param : TParam) : string; overload; override;
  353. function GetHandle : pointer; override;
  354. Function AllocateCursorHandle : TSQLCursor; override;
  355. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  356. Function AllocateTransactionHandle : TSQLHandle; override;
  357. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  358. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
  359. function Fetch(cursor : TSQLCursor) : boolean; override;
  360. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
  361. procedure UnPrepareStatement(cursor : TSQLCursor); override;
  362. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  363. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; override;
  364. function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
  365. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  366. function Commit(trans : TSQLHandle) : boolean; override;
  367. function RollBack(trans : TSQLHandle) : boolean; override;
  368. function StartdbTransaction(trans : TSQLHandle; aParams : string) : boolean; override;
  369. procedure CommitRetaining(trans : TSQLHandle); override;
  370. procedure RollBackRetaining(trans : TSQLHandle); override;
  371. procedure UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string); override;
  372. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  373. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); override;
  374. Property Proxy : TSQLConnection Read FProxy;
  375. Published
  376. Property ConnectorType : String Read FConnectorType Write SetConnectorType;
  377. end;
  378. TSQLConnectionClass = Class of TSQLConnection;
  379. { TConnectionDef }
  380. TConnectionDef = Class(TPersistent)
  381. Class Function TypeName : String; virtual;
  382. Class Function ConnectionClass : TSQLConnectionClass; virtual;
  383. Class Function Description : String; virtual;
  384. Procedure ApplyParams(Params : TStrings; AConnection : TSQLConnection); virtual;
  385. end;
  386. TConnectionDefClass = class of TConnectionDef;
  387. Procedure RegisterConnection(Def : TConnectionDefClass);
  388. Procedure UnRegisterConnection(Def : TConnectionDefClass);
  389. Procedure UnRegisterConnection(ConnectionName : String);
  390. Function GetConnectionDef(ConnectorName : String) : TConnectionDef;
  391. Procedure GetConnectionList(List : TSTrings);
  392. implementation
  393. uses dbconst, strutils;
  394. { TSQLConnection }
  395. function TSQLConnection.StrToStatementType(s : string) : TStatementType;
  396. var T : TStatementType;
  397. begin
  398. S:=Lowercase(s);
  399. For t:=stselect to strollback do
  400. if (S=StatementTokens[t]) then
  401. Exit(t);
  402. end;
  403. procedure TSQLConnection.SetTransaction(Value : TSQLTransaction);
  404. begin
  405. if FTransaction<>value then
  406. begin
  407. if Assigned(FTransaction) and FTransaction.Active then
  408. DatabaseError(SErrAssTransaction);
  409. if Assigned(Value) then
  410. Value.Database := Self;
  411. FTransaction := Value;
  412. If Assigned(FTransaction) and (FTransaction.Database=Nil) then
  413. FTransaction.Database:=Self;
  414. end;
  415. end;
  416. procedure TSQLConnection.UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string);
  417. begin
  418. // Empty abstract
  419. end;
  420. procedure TSQLConnection.DoInternalConnect;
  421. begin
  422. if (DatabaseName = '') then
  423. DatabaseError(SErrNoDatabaseName,self);
  424. end;
  425. procedure TSQLConnection.DoInternalDisconnect;
  426. begin
  427. end;
  428. destructor TSQLConnection.Destroy;
  429. begin
  430. inherited Destroy;
  431. end;
  432. procedure TSQLConnection.StartTransaction;
  433. begin
  434. if not assigned(Transaction) then
  435. DatabaseError(SErrConnTransactionnSet)
  436. else
  437. Transaction.StartTransaction;
  438. end;
  439. procedure TSQLConnection.EndTransaction;
  440. begin
  441. if not assigned(Transaction) then
  442. DatabaseError(SErrConnTransactionnSet)
  443. else
  444. Transaction.EndTransaction;
  445. end;
  446. Procedure TSQLConnection.ExecuteDirect(SQL: String);
  447. begin
  448. ExecuteDirect(SQL,FTransaction);
  449. end;
  450. Procedure TSQLConnection.ExecuteDirect(SQL: String; ATransaction : TSQLTransaction);
  451. var Cursor : TSQLCursor;
  452. begin
  453. if not assigned(ATransaction) then
  454. DatabaseError(SErrTransactionnSet);
  455. if not Connected then Open;
  456. if not ATransaction.Active then ATransaction.StartTransaction;
  457. try
  458. Cursor := AllocateCursorHandle;
  459. SQL := TrimRight(SQL);
  460. if SQL = '' then
  461. DatabaseError(SErrNoStatement);
  462. Cursor.FStatementType := stNone;
  463. PrepareStatement(cursor,ATransaction,SQL,Nil);
  464. execute(cursor,ATransaction, Nil);
  465. UnPrepareStatement(Cursor);
  466. finally;
  467. DeAllocateCursorHandle(Cursor);
  468. end;
  469. end;
  470. function TSQLConnection.GetPort: cardinal;
  471. begin
  472. result := StrToIntDef(Params.Values['Port'],0);
  473. end;
  474. procedure TSQLConnection.Setport(const AValue: cardinal);
  475. begin
  476. if AValue<>0 then
  477. params.Values['Port']:=IntToStr(AValue)
  478. else with params do if IndexOfName('Port') > -1 then
  479. Delete(IndexOfName('Port'));
  480. end;
  481. procedure TSQLConnection.GetDBInfo(const SchemaType : TSchemaType; const SchemaObjectName, ReturnField : string; List: TStrings);
  482. var qry : TCustomSQLQuery;
  483. begin
  484. if not assigned(Transaction) then
  485. DatabaseError(SErrConnTransactionnSet);
  486. qry := TCustomSQLQuery.Create(nil);
  487. qry.transaction := Transaction;
  488. qry.database := Self;
  489. with qry do
  490. begin
  491. ParseSQL := False;
  492. SetSchemaInfo(SchemaType,SchemaObjectName,'');
  493. open;
  494. List.Clear;
  495. while not eof do
  496. begin
  497. List.Append(trim(fieldbyname(ReturnField).asstring));
  498. Next;
  499. end;
  500. end;
  501. qry.free;
  502. end;
  503. function TSQLConnection.RowsAffected(cursor: TSQLCursor): TRowsCount;
  504. begin
  505. Result := -1;
  506. end;
  507. procedure TSQLConnection.GetTableNames(List: TStrings; SystemTables: Boolean);
  508. begin
  509. if not systemtables then GetDBInfo(stTables,'','table_name',List)
  510. else GetDBInfo(stSysTables,'','table_name',List);
  511. end;
  512. procedure TSQLConnection.GetProcedureNames(List: TStrings);
  513. begin
  514. GetDBInfo(stProcedures,'','proc_name',List);
  515. end;
  516. procedure TSQLConnection.GetFieldNames(const TableName: string; List: TStrings);
  517. begin
  518. GetDBInfo(stColumns,TableName,'column_name',List);
  519. end;
  520. function TSQLConnection.GetAsSQLText(Field : TField) : string;
  521. begin
  522. if (not assigned(field)) or field.IsNull then Result := 'Null'
  523. else case field.DataType of
  524. ftString : Result := '''' + field.asstring + '''';
  525. ftDate : Result := '''' + FormatDateTime('yyyy-mm-dd',Field.AsDateTime) + '''';
  526. ftDateTime : Result := '''' + FormatDateTime('yyyy-mm-dd hh:mm:ss',Field.AsDateTime) + ''''
  527. else
  528. Result := field.asstring;
  529. end; {case}
  530. end;
  531. function TSQLConnection.GetAsSQLText(Param: TParam) : string;
  532. begin
  533. if (not assigned(param)) or param.IsNull then Result := 'Null'
  534. else case param.DataType of
  535. ftString : Result := '''' + param.asstring + '''';
  536. ftDate : Result := '''' + FormatDateTime('yyyy-mm-dd',Param.AsDateTime) + '''';
  537. ftDateTime : Result := '''' + FormatDateTime('yyyy-mm-dd hh:mm:ss',Param.AsDateTime) + ''''
  538. else
  539. Result := Param.asstring;
  540. end; {case}
  541. end;
  542. function TSQLConnection.GetHandle: pointer;
  543. begin
  544. Result := nil;
  545. end;
  546. procedure TSQLConnection.FreeFldBuffers(cursor: TSQLCursor);
  547. begin
  548. // empty
  549. end;
  550. function TSQLConnection.GetSchemaInfoSQL( SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  551. begin
  552. DatabaseError(SMetadataUnavailable);
  553. end;
  554. procedure TSQLConnection.CreateDB;
  555. begin
  556. DatabaseError(SNotSupported);
  557. end;
  558. procedure TSQLConnection.DropDB;
  559. begin
  560. DatabaseError(SNotSupported);
  561. end;
  562. { TSQLTransaction }
  563. procedure TSQLTransaction.EndTransaction;
  564. begin
  565. rollback;
  566. end;
  567. function TSQLTransaction.GetHandle: pointer;
  568. begin
  569. Result := TSQLConnection(Database).GetTransactionHandle(FTrans);
  570. end;
  571. procedure TSQLTransaction.Commit;
  572. begin
  573. if active then
  574. begin
  575. closedatasets;
  576. if TSQLConnection(Database).commit(FTrans) then
  577. begin
  578. closeTrans;
  579. FreeAndNil(FTrans);
  580. end;
  581. end;
  582. end;
  583. procedure TSQLTransaction.CommitRetaining;
  584. begin
  585. if active then
  586. TSQLConnection(Database).commitRetaining(FTrans);
  587. end;
  588. procedure TSQLTransaction.Rollback;
  589. begin
  590. if active then
  591. begin
  592. closedatasets;
  593. if TSQLConnection(Database).RollBack(FTrans) then
  594. begin
  595. CloseTrans;
  596. FreeAndNil(FTrans);
  597. end;
  598. end;
  599. end;
  600. procedure TSQLTransaction.RollbackRetaining;
  601. begin
  602. if active then
  603. TSQLConnection(Database).RollBackRetaining(FTrans);
  604. end;
  605. procedure TSQLTransaction.StartTransaction;
  606. var db : TSQLConnection;
  607. begin
  608. if Active then
  609. DatabaseError(SErrTransAlreadyActive);
  610. db := TSQLConnection(Database);
  611. if Db = nil then
  612. DatabaseError(SErrDatabasenAssigned);
  613. if not Db.Connected then
  614. Db.Open;
  615. if not assigned(FTrans) then FTrans := Db.AllocateTransactionHandle;
  616. if Db.StartdbTransaction(FTrans,FParams.CommaText) then OpenTrans;
  617. end;
  618. constructor TSQLTransaction.Create(AOwner : TComponent);
  619. begin
  620. inherited Create(AOwner);
  621. FParams := TStringList.Create;
  622. end;
  623. destructor TSQLTransaction.Destroy;
  624. begin
  625. Rollback;
  626. FreeAndNil(FParams);
  627. inherited Destroy;
  628. end;
  629. Procedure TSQLTransaction.SetDatabase(Value : TDatabase);
  630. begin
  631. If Value<>Database then
  632. begin
  633. if assigned(value) and not (Value is TSQLConnection) then
  634. DatabaseErrorFmt(SErrNotASQLConnection,[value.Name],self);
  635. CheckInactive;
  636. If Assigned(Database) then
  637. with TSQLConnection(DataBase) do
  638. if Transaction = self then Transaction := nil;
  639. inherited SetDatabase(Value);
  640. If Assigned(Database) then
  641. If (TSQLConnection(DataBase).Transaction=Nil) then
  642. TSQLConnection(DataBase).Transaction:=Self;
  643. end;
  644. end;
  645. { TCustomSQLQuery }
  646. procedure TCustomSQLQuery.OnChangeSQL(Sender : TObject);
  647. var ConnOptions : TConnOptions;
  648. begin
  649. UnPrepare;
  650. if (FSQL <> nil) then
  651. begin
  652. if assigned(DataBase) then
  653. ConnOptions := TSQLConnection(DataBase).ConnOptions
  654. else
  655. ConnOptions := [sqEscapeRepeat,sqEscapeSlash];
  656. Fparams.ParseSQL(FSQL.Text,True, sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions,psInterbase);
  657. If Assigned(FMasterLink) then
  658. FMasterLink.RefreshParamNames;
  659. end;
  660. end;
  661. procedure TCustomSQLQuery.OnChangeModifySQL(Sender : TObject);
  662. begin
  663. CheckInactive;
  664. end;
  665. Procedure TCustomSQLQuery.SetTransaction(Value : TDBTransaction);
  666. begin
  667. UnPrepare;
  668. inherited;
  669. If (Transaction<>Nil) and (Database=Nil) then
  670. Database:=TSQLTransaction(Transaction).Database;
  671. end;
  672. procedure TCustomSQLQuery.SetDatabase(Value : TDatabase);
  673. var db : tsqlconnection;
  674. begin
  675. if (Database <> Value) then
  676. begin
  677. if assigned(value) and not (Value is TSQLConnection) then
  678. DatabaseErrorFmt(SErrNotASQLConnection,[value.Name],self);
  679. UnPrepare;
  680. if assigned(FCursor) then TSQLConnection(DataBase).DeAllocateCursorHandle(FCursor);
  681. db := TSQLConnection(Value);
  682. inherited setdatabase(value);
  683. if assigned(value) and (Transaction = nil) and (Assigned(db.Transaction)) then
  684. transaction := Db.Transaction;
  685. OnChangeSQL(Self);
  686. end;
  687. end;
  688. Function TCustomSQLQuery.IsPrepared : Boolean;
  689. begin
  690. Result := Assigned(FCursor) and FCursor.FPrepared;
  691. end;
  692. Function TCustomSQLQuery.AddFilter(SQLstr : string) : string;
  693. begin
  694. if FWhereStartPos = 0 then
  695. SQLstr := SQLstr + ' where (' + Filter + ')'
  696. else if FWhereStopPos > 0 then
  697. system.insert(' and ('+ServerFilter+') ',SQLstr,FWhereStopPos+1)
  698. else
  699. system.insert(' where ('+ServerFilter+') ',SQLstr,FWhereStartPos);
  700. Result := SQLstr;
  701. end;
  702. procedure TCustomSQLQuery.ApplyFilter;
  703. var S : String;
  704. begin
  705. FreeFldBuffers;
  706. TSQLConnection(Database).UnPrepareStatement(FCursor);
  707. FIsEOF := False;
  708. inherited internalclose;
  709. s := FSQLBuf;
  710. if ServerFiltered then s := AddFilter(s);
  711. TSQLConnection(Database).PrepareStatement(Fcursor,(transaction as tsqltransaction),S,FParams);
  712. Execute;
  713. inherited InternalOpen;
  714. First;
  715. end;
  716. Procedure TCustomSQLQuery.SetActive (Value : Boolean);
  717. begin
  718. inherited SetActive(Value);
  719. // The query is UnPrepared, so that if a transaction closes all datasets
  720. // they also get unprepared
  721. if not Value and IsPrepared then UnPrepare;
  722. end;
  723. procedure TCustomSQLQuery.SetServerFiltered(Value: Boolean);
  724. begin
  725. if Value and not FParseSQL then DatabaseErrorFmt(SNoParseSQL,['Filtering ']);
  726. if (ServerFiltered <> Value) then
  727. begin
  728. FServerFiltered := Value;
  729. if active then ApplyFilter;
  730. end;
  731. end;
  732. procedure TCustomSQLQuery.SetServerFilterText(const Value: string);
  733. begin
  734. if Value <> ServerFilter then
  735. begin
  736. FServerFilterText := Value;
  737. if active then ApplyFilter;
  738. end;
  739. end;
  740. procedure TCustomSQLQuery.Prepare;
  741. var
  742. db : tsqlconnection;
  743. sqltr : tsqltransaction;
  744. begin
  745. if not IsPrepared then
  746. begin
  747. db := TSQLConnection(Database);
  748. sqltr := (transaction as tsqltransaction);
  749. if not assigned(Db) then
  750. DatabaseError(SErrDatabasenAssigned);
  751. if not assigned(sqltr) then
  752. DatabaseError(SErrTransactionnSet);
  753. if not Db.Connected then db.Open;
  754. if not sqltr.Active then sqltr.StartTransaction;
  755. // if assigned(fcursor) then FreeAndNil(fcursor);
  756. if not assigned(fcursor) then
  757. FCursor := Db.AllocateCursorHandle;
  758. FSQLBuf := TrimRight(FSQL.Text);
  759. if FSQLBuf = '' then
  760. DatabaseError(SErrNoStatement);
  761. SQLParser(FSQLBuf);
  762. if ServerFiltered then
  763. Db.PrepareStatement(Fcursor,sqltr,AddFilter(FSQLBuf),FParams)
  764. else
  765. Db.PrepareStatement(Fcursor,sqltr,FSQLBuf,FParams);
  766. if (FCursor.FStatementType = stSelect) then
  767. FCursor.FInitFieldDef := True;
  768. end;
  769. end;
  770. procedure TCustomSQLQuery.UnPrepare;
  771. begin
  772. CheckInactive;
  773. if IsPrepared then with TSQLConnection(DataBase) do
  774. UnPrepareStatement(FCursor);
  775. end;
  776. procedure TCustomSQLQuery.FreeFldBuffers;
  777. begin
  778. if assigned(FCursor) then TSQLConnection(Database).FreeFldBuffers(FCursor);
  779. end;
  780. function TCustomSQLQuery.GetServerIndexDefs: TServerIndexDefs;
  781. begin
  782. Result := FServerIndexDefs;
  783. end;
  784. function TCustomSQLQuery.Fetch : boolean;
  785. begin
  786. if not (Fcursor.FStatementType in [stSelect]) then
  787. Exit;
  788. if not FIsEof then FIsEOF := not TSQLConnection(Database).Fetch(Fcursor);
  789. Result := not FIsEOF;
  790. end;
  791. procedure TCustomSQLQuery.Execute;
  792. begin
  793. If (FParams.Count>0) and Assigned(FMasterLink) then
  794. FMasterLink.CopyParamsFromMaster(False);
  795. TSQLConnection(Database).execute(Fcursor,Transaction as tsqltransaction, FParams);
  796. end;
  797. function TCustomSQLQuery.LoadField(FieldDef : TFieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
  798. begin
  799. result := TSQLConnection(Database).LoadField(FCursor,FieldDef,buffer, Createblob)
  800. end;
  801. function TCustomSQLQuery.RowsAffected: TRowsCount;
  802. begin
  803. Result := -1;
  804. if not Assigned(Database) then Exit;
  805. //assert(Database is TSQLConnection);
  806. Result := TSQLConnection(Database).RowsAffected(FCursor);
  807. end;
  808. procedure TCustomSQLQuery.InternalAddRecord(Buffer: Pointer; AAppend: Boolean);
  809. begin
  810. // not implemented - sql dataset
  811. end;
  812. procedure TCustomSQLQuery.InternalClose;
  813. begin
  814. if StatementType = stSelect then FreeFldBuffers;
  815. // Database and FCursor could be nil, for example if the database is not assigned, and .open is called
  816. if (not IsPrepared) and (assigned(database)) and (assigned(FCursor)) then TSQLConnection(database).UnPrepareStatement(FCursor);
  817. if DefaultFields then
  818. DestroyFields;
  819. FIsEOF := False;
  820. if assigned(FUpdateQry) then FreeAndNil(FUpdateQry);
  821. if assigned(FInsertQry) then FreeAndNil(FInsertQry);
  822. if assigned(FDeleteQry) then FreeAndNil(FDeleteQry);
  823. // FRecordSize := 0;
  824. inherited internalclose;
  825. end;
  826. procedure TCustomSQLQuery.InternalInitFieldDefs;
  827. begin
  828. if FLoadingFieldDefs then
  829. Exit;
  830. FLoadingFieldDefs := True;
  831. try
  832. FieldDefs.Clear;
  833. TSQLConnection(Database).AddFieldDefs(fcursor,FieldDefs);
  834. finally
  835. FLoadingFieldDefs := False;
  836. FCursor.FInitFieldDef := false;
  837. end;
  838. end;
  839. procedure TCustomSQLQuery.SQLParser(var ASQL : string);
  840. type TParsePart = (ppStart,ppSelect,ppWhere,ppFrom,ppOrder,ppComment,ppGroup,ppBogus);
  841. Var
  842. PSQL,CurrentP,
  843. PhraseP, PStatementPart : pchar;
  844. S : string;
  845. ParsePart : TParsePart;
  846. StrLength : Integer;
  847. EndOfComment : Boolean;
  848. BracketCount : Integer;
  849. ConnOptions : TConnOptions;
  850. FFromPart : String;
  851. begin
  852. PSQL:=Pchar(ASQL);
  853. ParsePart := ppStart;
  854. CurrentP := PSQL-1;
  855. PhraseP := PSQL;
  856. FWhereStartPos := 0;
  857. FWhereStopPos := 0;
  858. ConnOptions := TSQLConnection(DataBase).ConnOptions;
  859. FUpdateable := False;
  860. repeat
  861. begin
  862. inc(CurrentP);
  863. EndOfComment := SkipComments(CurrentP,sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions);
  864. if EndOfcomment then dec(currentp);
  865. if EndOfComment and (ParsePart = ppStart) then PhraseP := CurrentP;
  866. // skip everything between bracket, since it could be a sub-select, and
  867. // further nothing between brackets could be interesting for the parser.
  868. if currentp^='(' then
  869. begin
  870. inc(currentp);
  871. BracketCount := 0;
  872. while (currentp^ <> #0) and ((currentp^ <> ')') or (BracketCount > 0 )) do
  873. begin
  874. if currentp^ = '(' then inc(bracketcount)
  875. else if currentp^ = ')' then dec(bracketcount);
  876. inc(currentp);
  877. end;
  878. EndOfComment := True;
  879. end;
  880. if EndOfComment or (CurrentP^ in [' ',#13,#10,#9,#0,';']) then
  881. begin
  882. if (CurrentP-PhraseP > 0) or (CurrentP^ in [';',#0]) then
  883. begin
  884. strLength := CurrentP-PhraseP;
  885. Setlength(S,strLength);
  886. if strLength > 0 then Move(PhraseP^,S[1],(strLength));
  887. s := uppercase(s);
  888. case ParsePart of
  889. ppStart : begin
  890. FCursor.FStatementType := TSQLConnection(Database).StrToStatementType(s);
  891. if FCursor.FStatementType = stSelect then ParsePart := ppSelect
  892. else break;
  893. if not FParseSQL then break;
  894. PStatementPart := CurrentP;
  895. end;
  896. ppSelect : begin
  897. if s = 'FROM' then
  898. begin
  899. ParsePart := ppFrom;
  900. PhraseP := CurrentP;
  901. PStatementPart := CurrentP;
  902. end;
  903. end;
  904. ppFrom : begin
  905. if (s = 'WHERE') or (s = 'ORDER') or (s = 'GROUP') or (CurrentP^=#0) or (CurrentP^=';') then
  906. begin
  907. if (s = 'WHERE') then
  908. begin
  909. ParsePart := ppWhere;
  910. StrLength := PhraseP-PStatementPart;
  911. end
  912. else if (s = 'GROUP') then
  913. begin
  914. ParsePart := ppGroup;
  915. StrLength := PhraseP-PStatementPart;
  916. end
  917. else if (s = 'ORDER') then
  918. begin
  919. ParsePart := ppOrder;
  920. StrLength := PhraseP-PStatementPart
  921. end
  922. else
  923. begin
  924. ParsePart := ppBogus;
  925. StrLength := CurrentP-PStatementPart;
  926. end;
  927. if FCursor.FStatementType = stSelect then
  928. begin
  929. Setlength(FFromPart,StrLength);
  930. Move(PStatementPart^,FFromPart[1],(StrLength));
  931. FFrompart := trim(FFrompart);
  932. // select-statements from more then one table are not updateable
  933. if ExtractStrings([',',' '],[],pchar(FFromPart),nil) = 1 then
  934. begin
  935. FUpdateable := True;
  936. FTableName := FFromPart;
  937. end;
  938. end;
  939. FWhereStartPos := PStatementPart-PSQL+StrLength+1;
  940. PStatementPart := CurrentP;
  941. end;
  942. end;
  943. ppWhere : begin
  944. if (s = 'ORDER') or (s = 'GROUP') or (CurrentP^=#0) or (CurrentP^=';') then
  945. begin
  946. ParsePart := ppBogus;
  947. FWhereStartPos := PStatementPart-PSQL;
  948. if (s = 'ORDER') or (s = 'GROUP') then
  949. FWhereStopPos := PhraseP-PSQL+1
  950. else
  951. FWhereStopPos := CurrentP-PSQL+1;
  952. end
  953. else if (s = 'UNION') then
  954. begin
  955. ParsePart := ppBogus;
  956. FUpdateable := False;
  957. end;
  958. end;
  959. end; {case}
  960. end;
  961. PhraseP := CurrentP+1;
  962. end
  963. end;
  964. until CurrentP^=#0;
  965. if (FWhereStartPos > 0) and (FWhereStopPos > 0) then
  966. begin
  967. system.insert('(',ASQL,FWhereStartPos+1);
  968. inc(FWhereStopPos);
  969. system.insert(')',ASQL,FWhereStopPos);
  970. end
  971. end;
  972. procedure TCustomSQLQuery.InternalOpen;
  973. var tel, fieldc : integer;
  974. f : TField;
  975. s : string;
  976. IndexFields : TStrings;
  977. begin
  978. try
  979. Prepare;
  980. if FCursor.FStatementType in [stSelect] then
  981. begin
  982. Execute;
  983. // InternalInitFieldDef is only called after a prepare. i.e. not twice if
  984. // a dataset is opened - closed - opened.
  985. if FCursor.FInitFieldDef then InternalInitFieldDefs;
  986. if DefaultFields then
  987. begin
  988. CreateFields;
  989. if FUpdateable then
  990. begin
  991. if FusePrimaryKeyAsKey then
  992. begin
  993. UpdateServerIndexDefs;
  994. for tel := 0 to ServerIndexDefs.count-1 do
  995. begin
  996. if ixPrimary in ServerIndexDefs[tel].options then
  997. begin
  998. IndexFields := TStringList.Create;
  999. ExtractStrings([';'],[' '],pchar(ServerIndexDefs[tel].fields),IndexFields);
  1000. for fieldc := 0 to IndexFields.Count-1 do
  1001. begin
  1002. F := Findfield(IndexFields[fieldc]);
  1003. if F <> nil then
  1004. F.ProviderFlags := F.ProviderFlags + [pfInKey];
  1005. end;
  1006. IndexFields.Free;
  1007. end;
  1008. end;
  1009. end;
  1010. end;
  1011. end
  1012. else
  1013. BindFields(True);
  1014. if not ReadOnly and not FUpdateable then
  1015. begin
  1016. if (trim(FDeleteSQL.Text) <> '') or (trim(FUpdateSQL.Text) <> '') or
  1017. (trim(FInsertSQL.Text) <> '') then FUpdateable := True;
  1018. end
  1019. end
  1020. else
  1021. DatabaseError(SErrNoSelectStatement,Self);
  1022. except
  1023. on E:Exception do
  1024. raise;
  1025. end;
  1026. inherited InternalOpen;
  1027. end;
  1028. // public part
  1029. procedure TCustomSQLQuery.ExecSQL;
  1030. begin
  1031. try
  1032. Prepare;
  1033. Execute;
  1034. finally
  1035. // FCursor has to be assigned, or else the prepare went wrong before PrepareStatment was
  1036. // called, so UnPrepareStatement shoudn't be called either
  1037. if (not IsPrepared) and (assigned(database)) and (assigned(FCursor)) then TSQLConnection(database).UnPrepareStatement(Fcursor);
  1038. end;
  1039. end;
  1040. constructor TCustomSQLQuery.Create(AOwner : TComponent);
  1041. begin
  1042. inherited Create(AOwner);
  1043. FParams := TParams.create(self);
  1044. FSQL := TStringList.Create;
  1045. FSQL.OnChange := @OnChangeSQL;
  1046. FUpdateSQL := TStringList.Create;
  1047. FUpdateSQL.OnChange := @OnChangeModifySQL;
  1048. FInsertSQL := TStringList.Create;
  1049. FInsertSQL.OnChange := @OnChangeModifySQL;
  1050. FDeleteSQL := TStringList.Create;
  1051. FDeleteSQL.OnChange := @OnChangeModifySQL;
  1052. FServerIndexDefs := TServerIndexDefs.Create(Self);
  1053. FReadOnly := false;
  1054. FParseSQL := True;
  1055. FServerFiltered := False;
  1056. FServerFilterText := '';
  1057. // Delphi has upWhereAll as default, but since strings and oldvalue's don't work yet
  1058. // (variants) set it to upWhereKeyOnly
  1059. FUpdateMode := upWhereKeyOnly;
  1060. FUsePrimaryKeyAsKey := True;
  1061. end;
  1062. destructor TCustomSQLQuery.Destroy;
  1063. begin
  1064. if Active then Close;
  1065. UnPrepare;
  1066. if assigned(FCursor) then TSQLConnection(Database).DeAllocateCursorHandle(FCursor);
  1067. FreeAndNil(FMasterLink);
  1068. FreeAndNil(FParams);
  1069. FreeAndNil(FSQL);
  1070. FreeAndNil(FInsertSQL);
  1071. FreeAndNil(FDeleteSQL);
  1072. FreeAndNil(FUpdateSQL);
  1073. FServerIndexDefs.Free;
  1074. inherited Destroy;
  1075. end;
  1076. procedure TCustomSQLQuery.SetReadOnly(AValue : Boolean);
  1077. begin
  1078. CheckInactive;
  1079. FReadOnly:=AValue;
  1080. end;
  1081. procedure TCustomSQLQuery.SetParseSQL(AValue : Boolean);
  1082. begin
  1083. CheckInactive;
  1084. if not AValue then
  1085. begin
  1086. FServerFiltered := False;
  1087. FParseSQL := False;
  1088. end
  1089. else
  1090. FParseSQL := True;
  1091. end;
  1092. procedure TCustomSQLQuery.SetUsePrimaryKeyAsKey(AValue : Boolean);
  1093. begin
  1094. if not Active then FusePrimaryKeyAsKey := AValue
  1095. else
  1096. begin
  1097. // Just temporary, this should be possible in the future
  1098. DatabaseError(SActiveDataset);
  1099. end;
  1100. end;
  1101. Procedure TCustomSQLQuery.UpdateServerIndexDefs;
  1102. begin
  1103. FServerIndexDefs.Clear;
  1104. if assigned(DataBase) and (FTableName<>'') then
  1105. TSQLConnection(DataBase).UpdateIndexDefs(ServerIndexDefs,FTableName);
  1106. end;
  1107. Procedure TCustomSQLQuery.ApplyRecUpdate(UpdateKind : TUpdateKind);
  1108. var FieldNamesQuoteChar : char;
  1109. procedure InitialiseModifyQuery(var qry : TCustomSQLQuery; aSQL: String);
  1110. begin
  1111. qry := TCustomSQLQuery.Create(nil);
  1112. with qry do
  1113. begin
  1114. ParseSQL := False;
  1115. DataBase := Self.DataBase;
  1116. Transaction := Self.Transaction;
  1117. SQL.text := aSQL;
  1118. end;
  1119. end;
  1120. procedure UpdateWherePart(var sql_where : string;x : integer);
  1121. begin
  1122. if (pfInKey in Fields[x].ProviderFlags) or
  1123. ((FUpdateMode = upWhereAll) and (pfInWhere in Fields[x].ProviderFlags)) or
  1124. ((FUpdateMode = UpWhereChanged) and (pfInWhere in Fields[x].ProviderFlags) and (fields[x].value <> fields[x].oldvalue)) then
  1125. sql_where := sql_where + '(' + FieldNamesQuoteChar + fields[x].FieldName + FieldNamesQuoteChar + '= :OLD_' + fields[x].FieldName + ') and ';
  1126. end;
  1127. function ModifyRecQuery : string;
  1128. var x : integer;
  1129. sql_set : string;
  1130. sql_where : string;
  1131. begin
  1132. sql_set := '';
  1133. sql_where := '';
  1134. for x := 0 to Fields.Count -1 do
  1135. begin
  1136. UpdateWherePart(sql_where,x);
  1137. if (pfInUpdate in Fields[x].ProviderFlags) then
  1138. sql_set := sql_set +FieldNamesQuoteChar + fields[x].FieldName + FieldNamesQuoteChar +'=:' + fields[x].FieldName + ',';
  1139. end;
  1140. if length(sql_set) = 0 then DatabaseErrorFmt(sNoUpdateFields,['update'],self);
  1141. setlength(sql_set,length(sql_set)-1);
  1142. if length(sql_where) = 0 then DatabaseErrorFmt(sNoWhereFields,['update'],self);
  1143. setlength(sql_where,length(sql_where)-5);
  1144. result := 'update ' + FTableName + ' set ' + sql_set + ' where ' + sql_where;
  1145. end;
  1146. function InsertRecQuery : string;
  1147. var x : integer;
  1148. sql_fields : string;
  1149. sql_values : string;
  1150. begin
  1151. sql_fields := '';
  1152. sql_values := '';
  1153. for x := 0 to Fields.Count -1 do
  1154. begin
  1155. if (not fields[x].IsNull) and (pfInUpdate in Fields[x].ProviderFlags) then
  1156. begin
  1157. sql_fields := sql_fields + FieldNamesQuoteChar + fields[x].FieldName + FieldNamesQuoteChar + ',';
  1158. sql_values := sql_values + ':' + fields[x].FieldName + ',';
  1159. end;
  1160. end;
  1161. if length(sql_fields) = 0 then DatabaseErrorFmt(sNoUpdateFields,['insert'],self);
  1162. setlength(sql_fields,length(sql_fields)-1);
  1163. setlength(sql_values,length(sql_values)-1);
  1164. result := 'insert into ' + FTableName + ' (' + sql_fields + ') values (' + sql_values + ')';
  1165. end;
  1166. function DeleteRecQuery : string;
  1167. var x : integer;
  1168. sql_where : string;
  1169. begin
  1170. sql_where := '';
  1171. for x := 0 to Fields.Count -1 do
  1172. UpdateWherePart(sql_where,x);
  1173. if length(sql_where) = 0 then DatabaseErrorFmt(sNoWhereFields,['delete'],self);
  1174. setlength(sql_where,length(sql_where)-5);
  1175. result := 'delete from ' + FTableName + ' where ' + sql_where;
  1176. end;
  1177. var qry : TCustomSQLQuery;
  1178. x : integer;
  1179. Fld : TField;
  1180. begin
  1181. if sqQuoteFieldnames in TSQLConnection(DataBase).ConnOptions then
  1182. FieldNamesQuoteChar := '"'
  1183. else
  1184. FieldNamesQuoteChar := ' ';
  1185. case UpdateKind of
  1186. ukModify : begin
  1187. if not assigned(FUpdateQry) then
  1188. begin
  1189. if (trim(FUpdateSQL.Text)<> '') then
  1190. InitialiseModifyQuery(FUpdateQry,FUpdateSQL.Text)
  1191. else
  1192. InitialiseModifyQuery(FUpdateQry,ModifyRecQuery);
  1193. end;
  1194. qry := FUpdateQry;
  1195. end;
  1196. ukInsert : begin
  1197. if not assigned(FInsertQry) and (trim(FInsertSQL.Text)<> '') then
  1198. InitialiseModifyQuery(FInsertQry,FInsertSQL.Text)
  1199. else
  1200. InitialiseModifyQuery(FInsertQry,InsertRecQuery);
  1201. qry := FInsertQry;
  1202. end;
  1203. ukDelete : begin
  1204. if not assigned(FDeleteQry) and (trim(FDeleteSQL.Text)<> '') then
  1205. InitialiseModifyQuery(FDeleteQry,FDeleteSQL.Text)
  1206. else
  1207. InitialiseModifyQuery(FDeleteQry,DeleteRecQuery);
  1208. qry := FDeleteQry;
  1209. end;
  1210. end;
  1211. assert(qry.sql.Text<>'');
  1212. with qry do
  1213. begin
  1214. for x := 0 to Params.Count-1 do with params[x] do if leftstr(name,4)='OLD_' then
  1215. begin
  1216. Fld := self.FieldByName(copy(name,5,length(name)-4));
  1217. AssignFieldValue(Fld,Fld.OldValue);
  1218. end
  1219. else
  1220. begin
  1221. Fld := self.FieldByName(name);
  1222. AssignFieldValue(Fld,Fld.Value);
  1223. end;
  1224. execsql;
  1225. end;
  1226. end;
  1227. Function TCustomSQLQuery.GetCanModify: Boolean;
  1228. begin
  1229. // the test for assigned(FCursor) is needed for the case that the dataset isn't opened
  1230. if assigned(FCursor) and (FCursor.FStatementType = stSelect) then
  1231. Result:= FUpdateable and (not FReadOnly)
  1232. else
  1233. Result := False;
  1234. end;
  1235. procedure TCustomSQLQuery.SetUpdateMode(AValue : TUpdateMode);
  1236. begin
  1237. FUpdateMode := AValue;
  1238. end;
  1239. procedure TCustomSQLQuery.SetSchemaInfo( SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string);
  1240. begin
  1241. ReadOnly := True;
  1242. SQL.Clear;
  1243. SQL.Add(TSQLConnection(DataBase).GetSchemaInfoSQL(SchemaType, SchemaObjectName, SchemaPattern));
  1244. end;
  1245. procedure TCustomSQLQuery.LoadBlobIntoBuffer(FieldDef: TFieldDef;
  1246. ABlobBuf: PBufBlobField);
  1247. begin
  1248. TSQLConnection(DataBase).LoadBlobIntoBuffer(FieldDef, ABlobBuf, FCursor,(Transaction as tsqltransaction));
  1249. end;
  1250. function TCustomSQLQuery.GetStatementType : TStatementType;
  1251. begin
  1252. if assigned(FCursor) then Result := FCursor.FStatementType
  1253. else Result := stNone;
  1254. end;
  1255. Procedure TCustomSQLQuery.SetDataSource(AVAlue : TDatasource);
  1256. Var
  1257. DS : TDatasource;
  1258. begin
  1259. DS:=DataSource;
  1260. If (AValue<>DS) then
  1261. begin
  1262. If Assigned(AValue) and (AValue.Dataset=Self) then
  1263. DatabaseError(SErrCircularDataSourceReferenceNotAllowed,Self);
  1264. If Assigned(DS) then
  1265. DS.RemoveFreeNotification(Self);
  1266. If Assigned(AValue) then
  1267. begin
  1268. AValue.FreeNotification(Self);
  1269. FMasterLink:=TMasterParamsDataLink.Create(Self);
  1270. FMasterLink.Datasource:=AValue;
  1271. end
  1272. else
  1273. FreeAndNil(FMasterLink);
  1274. end;
  1275. end;
  1276. Function TCustomSQLQuery.GetDataSource : TDatasource;
  1277. begin
  1278. If Assigned(FMasterLink) then
  1279. Result:=FMasterLink.DataSource
  1280. else
  1281. Result:=Nil;
  1282. end;
  1283. procedure TCustomSQLQuery.Notification(AComponent: TComponent; Operation: TOperation);
  1284. begin
  1285. Inherited;
  1286. If (Operation=opRemove) and (AComponent=DataSource) then
  1287. DataSource:=Nil;
  1288. end;
  1289. { TSQLScript }
  1290. procedure TSQLScript.SetScript(const AValue: TStrings);
  1291. begin
  1292. FScript.assign(AValue);
  1293. end;
  1294. procedure TSQLScript.SetDatabase(Value: TDatabase);
  1295. begin
  1296. FDatabase := Value;
  1297. end;
  1298. procedure TSQLScript.SetTransaction(Value: TDBTransaction);
  1299. begin
  1300. FTransaction := Value;
  1301. end;
  1302. procedure TSQLScript.CheckDatabase;
  1303. begin
  1304. If (FDatabase=Nil) then
  1305. DatabaseError(SErrNoDatabaseAvailable,Self)
  1306. end;
  1307. constructor TSQLScript.Create(AOwner: TComponent);
  1308. begin
  1309. inherited Create(AOwner);
  1310. FScript := TStringList.Create;
  1311. FQuery := TCustomSQLQuery.Create(nil);
  1312. end;
  1313. destructor TSQLScript.Destroy;
  1314. begin
  1315. FScript.Free;
  1316. FQuery.Free;
  1317. inherited Destroy;
  1318. end;
  1319. procedure TSQLScript.ExecuteScript;
  1320. var BufStr : String;
  1321. pBufStatStart,
  1322. pBufPos : PChar;
  1323. Statement : String;
  1324. begin
  1325. FQuery.DataBase := FDatabase;
  1326. FQuery.Transaction := FTransaction;
  1327. BufStr := FScript.Text;
  1328. pBufPos := @BufStr[1];
  1329. repeat
  1330. pBufStatStart := pBufPos;
  1331. repeat
  1332. inc(pBufPos);
  1333. until (pBufPos^ = ';') or (pBufPos^ = #0);
  1334. SetLength(statement,pbufpos-pBufStatStart);
  1335. move(pBufStatStart^,Statement[1],pbufpos-pBufStatStart);
  1336. if trim(statement) <> '' then
  1337. begin
  1338. fquery.SQL.Text := Statement;
  1339. fquery.ExecSQL;
  1340. inc(pBufPos);
  1341. end;
  1342. until pBufPos^ = #0;
  1343. end;
  1344. { Connection definitions }
  1345. Var
  1346. ConnDefs : TStringList;
  1347. Procedure CheckDefs;
  1348. begin
  1349. If (ConnDefs=Nil) then
  1350. begin
  1351. ConnDefs:=TStringList.Create;
  1352. ConnDefs.Sorted:=True;
  1353. ConnDefs.Duplicates:=dupError;
  1354. end;
  1355. end;
  1356. Procedure DoneDefs;
  1357. Var
  1358. I : Integer;
  1359. begin
  1360. If Assigned(ConnDefs) then
  1361. begin
  1362. For I:=ConnDefs.Count-1 downto 0 do
  1363. begin
  1364. ConnDefs.Objects[i].Free;
  1365. ConnDefs.Delete(I);
  1366. end;
  1367. FreeAndNil(ConnDefs);
  1368. end;
  1369. end;
  1370. Function GetConnectionDef(ConnectorName : String) : TConnectionDef;
  1371. Var
  1372. I : Integer;
  1373. begin
  1374. CheckDefs;
  1375. I:=ConnDefs.IndexOf(ConnectorName);
  1376. If (I<>-1) then
  1377. Result:=TConnectionDef(ConnDefs.Objects[i])
  1378. else
  1379. Result:=Nil;
  1380. end;
  1381. procedure RegisterConnection(Def: TConnectionDefClass);
  1382. Var
  1383. I : Integer;
  1384. begin
  1385. CheckDefs;
  1386. I:=ConnDefs.IndexOf(Def.TypeName);
  1387. If (I=-1) then
  1388. ConnDefs.AddObject(Def.TypeName,Def.Create)
  1389. else
  1390. begin
  1391. ConnDefs.Objects[I].Free;
  1392. ConnDefs.Objects[I]:=Def.Create;
  1393. end;
  1394. end;
  1395. procedure UnRegisterConnection(Def: TConnectionDefClass);
  1396. begin
  1397. UnRegisterConnection(Def.TypeName);
  1398. end;
  1399. procedure UnRegisterConnection(ConnectionName: String);
  1400. Var
  1401. I : Integer;
  1402. begin
  1403. if (ConnDefs<>Nil) then
  1404. begin
  1405. I:=ConnDefs.IndexOf(ConnectionName);
  1406. If (I<>-1) then
  1407. begin
  1408. ConnDefs.Objects[I].Free;
  1409. ConnDefs.Delete(I);
  1410. end;
  1411. end;
  1412. end;
  1413. procedure GetConnectionList(List: TSTrings);
  1414. begin
  1415. CheckDefs;
  1416. List.Text:=ConnDefs.Text;
  1417. end;
  1418. { TSQLConnector }
  1419. procedure TSQLConnector.SetConnectorType(const AValue: String);
  1420. begin
  1421. if FConnectorType<>AValue then
  1422. begin
  1423. CheckDisconnected;
  1424. If Assigned(FProxy) then
  1425. FreeProxy;
  1426. FConnectorType:=AValue;
  1427. end;
  1428. end;
  1429. procedure TSQLConnector.SetTransaction(Value: TSQLTransaction);
  1430. begin
  1431. inherited SetTransaction(Value);
  1432. If Assigned(FProxy) and (FProxy.Transaction<>Value) then
  1433. FProxy.Transaction:=Value;
  1434. end;
  1435. procedure TSQLConnector.DoInternalConnect;
  1436. Var
  1437. D : TConnectionDef;
  1438. begin
  1439. inherited DoInternalConnect;
  1440. CreateProxy;
  1441. FProxy.DatabaseName:=Self.DatabaseName;
  1442. FProxy.HostName:=Self.HostName;
  1443. FProxy.UserName:=Self.UserName;
  1444. FProxy.Password:=Self.Password;
  1445. FProxy.Transaction:=Self.Transaction;
  1446. D:=GetConnectionDef(ConnectorType);
  1447. D.ApplyParams(Params,FProxy);
  1448. FProxy.Connected:=True;
  1449. end;
  1450. procedure TSQLConnector.DoInternalDisconnect;
  1451. begin
  1452. FProxy.Connected:=False;
  1453. inherited DoInternalDisconnect;
  1454. end;
  1455. procedure TSQLConnector.CheckProxy;
  1456. begin
  1457. If (FProxy=Nil) then
  1458. CreateProxy;
  1459. end;
  1460. procedure TSQLConnector.CreateProxy;
  1461. Var
  1462. D : TConnectionDef;
  1463. begin
  1464. D:=GetConnectionDef(ConnectorType);
  1465. If (D=Nil) then
  1466. DatabaseErrorFmt(SErrUnknownConnectorType,[ConnectorType],Self);
  1467. FProxy:=D.ConnectionClass.Create(Self);
  1468. end;
  1469. procedure TSQLConnector.FreeProxy;
  1470. begin
  1471. FProxy.Connected:=False;
  1472. FreeAndNil(FProxy);
  1473. end;
  1474. function TSQLConnector.StrToStatementType(s: string): TStatementType;
  1475. begin
  1476. CheckProxy;
  1477. Result:=FProxy.StrToStatementType(s);
  1478. end;
  1479. function TSQLConnector.GetAsSQLText(Field: TField): string;
  1480. begin
  1481. CheckProxy;
  1482. Result:=FProxy.GetAsSQLText(Field);
  1483. end;
  1484. function TSQLConnector.GetAsSQLText(Param: TParam): string;
  1485. begin
  1486. CheckProxy;
  1487. Result:=FProxy.GetAsSQLText(Param);
  1488. end;
  1489. function TSQLConnector.GetHandle: pointer;
  1490. begin
  1491. CheckProxy;
  1492. Result:=FProxy.GetHandle;
  1493. end;
  1494. function TSQLConnector.AllocateCursorHandle: TSQLCursor;
  1495. begin
  1496. CheckProxy;
  1497. Result:=FProxy.AllocateCursorHandle;
  1498. end;
  1499. procedure TSQLConnector.DeAllocateCursorHandle(var cursor: TSQLCursor);
  1500. begin
  1501. CheckProxy;
  1502. FProxy.DeAllocateCursorHandle(cursor);
  1503. end;
  1504. function TSQLConnector.AllocateTransactionHandle: TSQLHandle;
  1505. begin
  1506. CheckProxy;
  1507. Result:=FProxy.AllocateTransactionHandle;
  1508. end;
  1509. procedure TSQLConnector.PrepareStatement(cursor: TSQLCursor;
  1510. ATransaction: TSQLTransaction; buf: string; AParams: TParams);
  1511. begin
  1512. CheckProxy;
  1513. FProxy.PrepareStatement(cursor, ATransaction, buf, AParams);
  1514. end;
  1515. procedure TSQLConnector.Execute(cursor: TSQLCursor;
  1516. atransaction: tSQLtransaction; AParams: TParams);
  1517. begin
  1518. CheckProxy;
  1519. FProxy.Execute(cursor, atransaction, AParams);
  1520. end;
  1521. function TSQLConnector.Fetch(cursor: TSQLCursor): boolean;
  1522. begin
  1523. CheckProxy;
  1524. Result:=FProxy.Fetch(cursor);
  1525. end;
  1526. procedure TSQLConnector.AddFieldDefs(cursor: TSQLCursor; FieldDefs: TfieldDefs
  1527. );
  1528. begin
  1529. CheckProxy;
  1530. FProxy.AddFieldDefs(cursor, FieldDefs);
  1531. end;
  1532. procedure TSQLConnector.UnPrepareStatement(cursor: TSQLCursor);
  1533. begin
  1534. CheckProxy;
  1535. FProxy.UnPrepareStatement(cursor);
  1536. end;
  1537. procedure TSQLConnector.FreeFldBuffers(cursor: TSQLCursor);
  1538. begin
  1539. CheckProxy;
  1540. FProxy.FreeFldBuffers(cursor);
  1541. end;
  1542. function TSQLConnector.LoadField(cursor: TSQLCursor; FieldDef: TfieldDef;
  1543. buffer: pointer; out CreateBlob: boolean): boolean;
  1544. begin
  1545. CheckProxy;
  1546. Result:=FProxy.LoadField(cursor, FieldDef, buffer, CreateBlob);
  1547. end;
  1548. function TSQLConnector.RowsAffected(cursor: TSQLCursor): TRowsCount;
  1549. begin
  1550. CheckProxy;
  1551. Result := FProxy.RowsAffected(cursor);
  1552. end;
  1553. function TSQLConnector.GetTransactionHandle(trans: TSQLHandle): pointer;
  1554. begin
  1555. CheckProxy;
  1556. Result:=FProxy.GetTransactionHandle(trans);
  1557. end;
  1558. function TSQLConnector.Commit(trans: TSQLHandle): boolean;
  1559. begin
  1560. CheckProxy;
  1561. Result:=FProxy.Commit(trans);
  1562. end;
  1563. function TSQLConnector.RollBack(trans: TSQLHandle): boolean;
  1564. begin
  1565. CheckProxy;
  1566. Result:=FProxy.RollBack(trans);
  1567. end;
  1568. function TSQLConnector.StartdbTransaction(trans: TSQLHandle; aParams: string
  1569. ): boolean;
  1570. begin
  1571. CheckProxy;
  1572. Result:=FProxy.StartdbTransaction(trans, aParams);
  1573. end;
  1574. procedure TSQLConnector.CommitRetaining(trans: TSQLHandle);
  1575. begin
  1576. CheckProxy;
  1577. FProxy.CommitRetaining(trans);
  1578. end;
  1579. procedure TSQLConnector.RollBackRetaining(trans: TSQLHandle);
  1580. begin
  1581. CheckProxy;
  1582. FProxy.RollBackRetaining(trans);
  1583. end;
  1584. procedure TSQLConnector.UpdateIndexDefs(IndexDefs: TIndexDefs;
  1585. TableName: string);
  1586. begin
  1587. CheckProxy;
  1588. FProxy.UpdateIndexDefs(IndexDefs, TableName);
  1589. end;
  1590. function TSQLConnector.GetSchemaInfoSQL(SchemaType: TSchemaType;
  1591. SchemaObjectName, SchemaPattern: string): string;
  1592. begin
  1593. CheckProxy;
  1594. Result:=FProxy.GetSchemaInfoSQL(SchemaType, SchemaObjectName, SchemaPattern
  1595. );
  1596. end;
  1597. procedure TSQLConnector.LoadBlobIntoBuffer(FieldDef: TFieldDef;
  1598. ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction: TSQLTransaction);
  1599. begin
  1600. CheckProxy;
  1601. FProxy.LoadBlobIntoBuffer(FieldDef, ABlobBuf, cursor, ATransaction);
  1602. end;
  1603. { TConnectionDef }
  1604. class function TConnectionDef.TypeName: String;
  1605. begin
  1606. Result:='';
  1607. end;
  1608. class function TConnectionDef.ConnectionClass: TSQLConnectionClass;
  1609. begin
  1610. Result:=Nil;
  1611. end;
  1612. class function TConnectionDef.Description: String;
  1613. begin
  1614. Result:='';
  1615. end;
  1616. procedure TConnectionDef.ApplyParams(Params: TStrings;
  1617. AConnection: TSQLConnection);
  1618. begin
  1619. AConnection.Params.Assign(Params);
  1620. end;
  1621. { TServerIndexDefs }
  1622. constructor TServerIndexDefs.create(ADataset: TDataset);
  1623. begin
  1624. if not (ADataset is TCustomSQLQuery) then
  1625. DatabaseErrorFmt(SErrNotASQLQuery,[ADataset.Name]);
  1626. inherited create(ADataset);
  1627. end;
  1628. procedure TServerIndexDefs.Update;
  1629. begin
  1630. if (not updated) and assigned(Dataset) then
  1631. begin
  1632. TCustomSQLQuery(Dataset).UpdateServerIndexDefs;
  1633. updated := True;
  1634. end;
  1635. end;
  1636. Initialization
  1637. Finalization
  1638. DoneDefs;
  1639. end.