ibconnection.pp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. unit IBConnection;
  2. {$mode objfpc}{$H+}
  3. {$Define LinkDynamically}
  4. interface
  5. uses
  6. Classes, SysUtils, sqldb, db, dbconst, bufdataset,
  7. {$IfDef LinkDynamically}
  8. ibase60dyn;
  9. {$Else}
  10. ibase60;
  11. {$EndIf}
  12. const
  13. DEFDIALECT = 3;
  14. MAXBLOBSEGMENTSIZE = 65535; //Maximum number of bytes that fit in a blob segment.
  15. type
  16. TDatabaseInfo = record
  17. Dialect : integer; //Dialect set in database
  18. ODSMajorVersion : integer; //On-Disk Structure version of file
  19. ServerVersion : string; //Representation of major.minor (.build)
  20. ServerVersionString : string; //Complete version string, including name, platform
  21. end;
  22. EIBDatabaseError = class(ESQLDatabaseError)
  23. public
  24. property GDSErrorCode: integer read ErrorCode; deprecated 'Please use ErrorCode instead of GDSErrorCode'; // Nov 2014
  25. end;
  26. { TIBCursor }
  27. TIBCursor = Class(TSQLCursor)
  28. protected
  29. Status : array [0..19] of ISC_STATUS;
  30. Statement : pointer;
  31. SQLDA : PXSQLDA;
  32. in_SQLDA : PXSQLDA;
  33. ParamBinding : array of integer;
  34. FieldBinding : array of integer;
  35. end;
  36. TIBTrans = Class(TSQLHandle)
  37. protected
  38. TransactionHandle : pointer;
  39. TPB : string; // Transaction parameter buffer
  40. Status : array [0..19] of ISC_STATUS;
  41. end;
  42. { TIBConnection }
  43. TIBConnection = class (TSQLConnection)
  44. private
  45. FSQLDatabaseHandle : pointer;
  46. FStatus : array [0..19] of ISC_STATUS;
  47. FDatabaseInfo : TDatabaseInfo;
  48. FDialect : integer;
  49. FBlobSegmentSize : word; //required for backward compatibilty; not used
  50. procedure ConnectFB;
  51. procedure AllocSQLDA(var aSQLDA : PXSQLDA;Count : integer);
  52. // Metadata:
  53. procedure GetDatabaseInfo; //Queries for various information from server once connected
  54. procedure ResetDatabaseInfo; //Useful when disconnecting
  55. function GetDialect: integer;
  56. function GetODSMajorVersion: integer;
  57. function ParseServerVersion(const CompleteVersion: string): string; //Extract version info from complete version identification string
  58. // conversion methods
  59. procedure TranslateFldType(SQLType, SQLSubType, SQLLen, SQLScale : integer;
  60. var TrType : TFieldType; var TrLen : word);
  61. procedure GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  62. procedure SetDateTime(CurrBuff: pointer; PTime : TDateTime; AType : integer);
  63. procedure GetFloat(CurrBuff, Buffer : pointer; Size : Byte);
  64. procedure SetFloat(CurrBuff: pointer; Dbl: Double; Size: integer);
  65. procedure CheckError(ProcName : string; Status : PISC_STATUS);
  66. procedure SetParameters(cursor : TSQLCursor; aTransation : TSQLTransaction; AParams : TParams);
  67. procedure FreeSQLDABuffer(var aSQLDA : PXSQLDA);
  68. function IsDialectStored: boolean;
  69. protected
  70. procedure DoConnect; override;
  71. procedure DoInternalConnect; override;
  72. procedure DoInternalDisconnect; override;
  73. function GetHandle : pointer; override;
  74. Function AllocateCursorHandle : TSQLCursor; override;
  75. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  76. Function AllocateTransactionHandle : TSQLHandle; override;
  77. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  78. procedure UnPrepareStatement(cursor : TSQLCursor); override;
  79. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  80. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
  81. procedure AddFieldDefs(cursor: TSQLCursor;FieldDefs : TfieldDefs); override;
  82. function Fetch(cursor : TSQLCursor) : boolean; override;
  83. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; override;
  84. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  85. function Commit(trans : TSQLHandle) : boolean; override;
  86. function RollBack(trans : TSQLHandle) : boolean; override;
  87. function StartDBTransaction(trans : TSQLHandle; AParams : string) : boolean; override;
  88. procedure CommitRetaining(trans : TSQLHandle); override;
  89. procedure RollBackRetaining(trans : TSQLHandle); override;
  90. procedure UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string); override;
  91. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); override;
  92. function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
  93. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  94. function GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string; override;
  95. public
  96. constructor Create(AOwner : TComponent); override;
  97. function GetConnectionInfo(InfoType:TConnInfoType): string; override;
  98. procedure CreateDB; override;
  99. procedure DropDB; override;
  100. //Segment size is not used in the code; property kept for backward compatibility
  101. property BlobSegmentSize : word read FBlobSegmentSize write FBlobSegmentSize; deprecated;
  102. property ODSMajorVersion : integer read GetODSMajorVersion; //ODS major version number; influences database compatibility/feature level.
  103. published
  104. property DatabaseName;
  105. property Dialect : integer read GetDialect write FDialect stored IsDialectStored default DEFDIALECT;
  106. property KeepConnection;
  107. property LoginPrompt;
  108. property Params;
  109. property OnLogin;
  110. end;
  111. { TIBConnectionDef }
  112. TIBConnectionDef = Class(TConnectionDef)
  113. Class Function TypeName : String; override;
  114. Class Function ConnectionClass : TSQLConnectionClass; override;
  115. Class Function Description : String; override;
  116. Class Function DefaultLibraryName : String; override;
  117. Class Function LoadFunction : TLibraryLoadFunction; override;
  118. Class Function UnLoadFunction : TLibraryUnLoadFunction; override;
  119. Class Function LoadedLibraryName: string; override;
  120. end;
  121. implementation
  122. uses
  123. StrUtils, FmtBCD;
  124. const
  125. SQL_BOOLEAN_INTERBASE = 590;
  126. SQL_BOOLEAN_FIREBIRD = 32764;
  127. INVALID_DATA = -1;
  128. procedure TIBConnection.CheckError(ProcName : string; Status : PISC_STATUS);
  129. var
  130. ErrorCode : longint;
  131. Msg, SQLState : string;
  132. Buf : array [0..1023] of char;
  133. begin
  134. if ((Status[0] = 1) and (Status[1] <> 0)) then
  135. begin
  136. ErrorCode := Status[1];
  137. {$IFDEF LinkDynamically}
  138. if assigned(fb_sqlstate) then // >= Firebird 2.5
  139. begin
  140. fb_sqlstate(Buf, Status);
  141. SQLState := StrPas(Buf);
  142. end;
  143. {$ENDIF}
  144. Msg := '';
  145. while isc_interprete(Buf, @Status) > 0 do
  146. Msg := Msg + LineEnding + ' -' + StrPas(Buf);
  147. raise EIBDatabaseError.CreateFmt('%s : %s', [ProcName,Msg], Self, ErrorCode, SQLState);
  148. end;
  149. end;
  150. constructor TIBConnection.Create(AOwner : TComponent);
  151. begin
  152. inherited;
  153. FConnOptions := FConnOptions + [sqSupportParams, sqEscapeRepeat, sqSupportReturning];
  154. FBlobSegmentSize := 65535; //Shows we're using the maximum segment size
  155. FDialect := INVALID_DATA;
  156. ResetDatabaseInfo;
  157. end;
  158. function TIBConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  159. begin
  160. Result := (trans as TIBtrans).TransactionHandle;
  161. end;
  162. function TIBConnection.Commit(trans : TSQLHandle) : boolean;
  163. begin
  164. result := false;
  165. with (trans as TIBTrans) do
  166. if isc_commit_transaction(@Status[0], @TransactionHandle) <> 0 then
  167. CheckError('Commit', Status)
  168. else result := true;
  169. end;
  170. function TIBConnection.RollBack(trans : TSQLHandle) : boolean;
  171. begin
  172. result := false;
  173. if isc_rollback_transaction(@TIBTrans(trans).Status[0], @TIBTrans(trans).TransactionHandle) <> 0 then
  174. CheckError('Rollback', TIBTrans(trans).Status)
  175. else result := true;
  176. end;
  177. function TIBConnection.StartDBTransaction(trans: TSQLHandle; AParams: string
  178. ): boolean;
  179. var
  180. DBHandle : pointer;
  181. tr : TIBTrans;
  182. i : integer;
  183. s : string;
  184. begin
  185. result := false;
  186. DBHandle := GetHandle;
  187. tr := trans as TIBtrans;
  188. with tr do
  189. begin
  190. TPB := chr(isc_tpb_version3);
  191. i := 1;
  192. s := ExtractSubStr(AParams,i,stdWordDelims);
  193. while s <> '' do
  194. begin
  195. if s='isc_tpb_write' then TPB := TPB + chr(isc_tpb_write)
  196. else if s='isc_tpb_read' then TPB := TPB + chr(isc_tpb_read)
  197. else if s='isc_tpb_consistency' then TPB := TPB + chr(isc_tpb_consistency)
  198. else if s='isc_tpb_concurrency' then TPB := TPB + chr(isc_tpb_concurrency)
  199. else if s='isc_tpb_read_committed' then TPB := TPB + chr(isc_tpb_read_committed)
  200. else if s='isc_tpb_rec_version' then TPB := TPB + chr(isc_tpb_rec_version)
  201. else if s='isc_tpb_no_rec_version' then TPB := TPB + chr(isc_tpb_no_rec_version)
  202. else if s='isc_tpb_wait' then TPB := TPB + chr(isc_tpb_wait)
  203. else if s='isc_tpb_nowait' then TPB := TPB + chr(isc_tpb_nowait)
  204. else if s='isc_tpb_shared' then TPB := TPB + chr(isc_tpb_shared)
  205. else if s='isc_tpb_protected' then TPB := TPB + chr(isc_tpb_protected)
  206. else if s='isc_tpb_exclusive' then TPB := TPB + chr(isc_tpb_exclusive)
  207. else if s='isc_tpb_lock_read' then TPB := TPB + chr(isc_tpb_lock_read)
  208. else if s='isc_tpb_lock_write' then TPB := TPB + chr(isc_tpb_lock_write)
  209. else if s='isc_tpb_verb_time' then TPB := TPB + chr(isc_tpb_verb_time)
  210. else if s='isc_tpb_commit_time' then TPB := TPB + chr(isc_tpb_commit_time)
  211. else if s='isc_tpb_ignore_limbo' then TPB := TPB + chr(isc_tpb_ignore_limbo)
  212. else if s='isc_tpb_autocommit' then TPB := TPB + chr(isc_tpb_autocommit)
  213. else if s='isc_tpb_restart_requests' then TPB := TPB + chr(isc_tpb_restart_requests)
  214. else if s='isc_tpb_no_auto_undo' then TPB := TPB + chr(isc_tpb_no_auto_undo);
  215. s := ExtractSubStr(AParams,i,stdWordDelims);
  216. end;
  217. TransactionHandle := nil;
  218. if isc_start_transaction(@Status[0], @TransactionHandle, 1,
  219. [@DBHandle, Length(TPB), @TPB[1]]) <> 0 then
  220. CheckError('StartTransaction',Status)
  221. else Result := True;
  222. end;
  223. end;
  224. procedure TIBConnection.CommitRetaining(trans : TSQLHandle);
  225. begin
  226. with trans as TIBtrans do
  227. if isc_commit_retaining(@Status[0], @TransactionHandle) <> 0 then
  228. CheckError('CommitRetaining', Status);
  229. end;
  230. procedure TIBConnection.RollBackRetaining(trans : TSQLHandle);
  231. begin
  232. with trans as TIBtrans do
  233. if isc_rollback_retaining(@Status[0], @TransactionHandle) <> 0 then
  234. CheckError('RollBackRetaining', Status);
  235. end;
  236. procedure TIBConnection.DropDB;
  237. begin
  238. CheckDisConnected;
  239. {$IfDef LinkDynamically}
  240. InitialiseIBase60;
  241. {$EndIf}
  242. ConnectFB;
  243. if isc_drop_database(@FStatus[0], @FSQLDatabaseHandle) <> 0 then
  244. CheckError('DropDB', FStatus);
  245. {$IfDef LinkDynamically}
  246. ReleaseIBase60;
  247. {$EndIf}
  248. end;
  249. procedure TIBConnection.CreateDB;
  250. var ASQLDatabaseHandle,
  251. ASQLTransactionHandle : pointer;
  252. CreateSQL : String;
  253. pagesize : String;
  254. begin
  255. CheckDisConnected;
  256. {$IfDef LinkDynamically}
  257. InitialiseIBase60;
  258. {$EndIf}
  259. ASQLDatabaseHandle := nil;
  260. ASQLTransactionHandle := nil;
  261. CreateSQL := 'CREATE DATABASE ';
  262. if HostName <> '' then CreateSQL := CreateSQL + ''''+ HostName+':'+DatabaseName + ''''
  263. else CreateSQL := CreateSQL + '''' + DatabaseName + '''';
  264. if UserName <> '' then
  265. CreateSQL := CreateSQL + ' USER ''' + Username + '''';
  266. if Password <> '' then
  267. CreateSQL := CreateSQL + ' PASSWORD ''' + Password + '''';
  268. pagesize := params.Values['PAGE_SIZE'];
  269. if pagesize <> '' then
  270. CreateSQL := CreateSQL + ' PAGE_SIZE '+pagesize;
  271. if CharSet <> '' then
  272. CreateSQL := CreateSQL + ' DEFAULT CHARACTER SET ' + CharSet;
  273. if isc_dsql_execute_immediate(@FStatus[0],@ASQLDatabaseHandle,@ASQLTransactionHandle,length(CreateSQL),@CreateSQL[1],Dialect,nil) <> 0 then
  274. CheckError('CreateDB', FStatus);
  275. if isc_detach_database(@FStatus[0], @ASQLDatabaseHandle) <> 0 then
  276. CheckError('CreateDB', FStatus);
  277. {$IfDef LinkDynamically}
  278. ReleaseIBase60;
  279. {$EndIf}
  280. end;
  281. procedure TIBConnection.DoInternalConnect;
  282. begin
  283. {$IfDef LinkDynamically}
  284. InitialiseIBase60;
  285. {$EndIf}
  286. inherited dointernalconnect;
  287. ConnectFB;
  288. end;
  289. procedure TIBConnection.DoInternalDisconnect;
  290. begin
  291. Inherited;
  292. FDialect := INVALID_DATA;
  293. if not Connected then
  294. begin
  295. ResetDatabaseInfo;
  296. FSQLDatabaseHandle := nil;
  297. Exit;
  298. end;
  299. if isc_detach_database(@FStatus[0], @FSQLDatabaseHandle) <> 0 then
  300. CheckError('Close', FStatus);
  301. {$IfDef LinkDynamically}
  302. ReleaseIBase60;
  303. {$ELSE}
  304. // Shutdown embedded subsystem with timeout 300ms (Firebird 2.5+)
  305. // Required before unloading library; has no effect on non-embedded client
  306. if (pointer(fb_shutdown)<>nil) and (fb_shutdown(300,1)<>0) then
  307. begin
  308. //todo: log error; still try to unload library below as the timeout may have been insufficient
  309. end;
  310. {$EndIf}
  311. end;
  312. function TIBConnection.GetConnectionInfo(InfoType: TConnInfoType): string;
  313. begin
  314. result:='';
  315. {$IFDEF LinkDynamically}
  316. InitialiseIBase60;
  317. {$ENDIF}
  318. try
  319. case InfoType of
  320. citServerType:
  321. // Firebird returns own name in ServerVersion; Interbase 7.5 doesn't.
  322. if Pos('Firebird', FDatabaseInfo.ServerVersionString)=0 then
  323. result := 'Interbase'
  324. else
  325. result := 'Firebird';
  326. citServerVersion:
  327. // Firebird returns major.minor, Interbase major.minor.build
  328. result := FDatabaseInfo.ServerVersion;
  329. citServerVersionString:
  330. result := FDatabaseInfo.ServerVersionString;
  331. citClientName:
  332. result:=TIBConnectionDef.LoadedLibraryName;
  333. else
  334. //including citClientVersion, for which no single IB+FB and Win+*nux solution exists
  335. result:=inherited GetConnectionInfo(InfoType);
  336. end;
  337. finally
  338. {$IFDEF LinkDynamically}
  339. ReleaseIBase60;
  340. {$ENDIF}
  341. end;
  342. end;
  343. procedure TIBConnection.GetDatabaseInfo;
  344. // Asks server for multiple values
  345. const
  346. ResBufHigh = 512; //hopefully enough to include version string as well.
  347. var
  348. x : integer;
  349. Len : integer;
  350. ReqBuf : array [0..3] of byte;
  351. ResBuf : array [0..ResBufHigh] of byte; // should be big enough for version string etc
  352. begin
  353. ResetDatabaseInfo;
  354. if Connected then
  355. begin
  356. ReqBuf[0] := isc_info_ods_version;
  357. ReqBuf[1] := isc_info_version;
  358. ReqBuf[2] := isc_info_db_sql_dialect;
  359. ReqBuf[3] := isc_info_end;
  360. if isc_database_info(@FStatus[0], @FSQLDatabaseHandle, Length(ReqBuf),
  361. pchar(@ReqBuf[0]), SizeOf(ResBuf), pchar(@ResBuf[0])) <> 0 then
  362. CheckError('CacheServerInfo', FStatus);
  363. x := 0;
  364. while x < ResBufHigh+1 do
  365. case ResBuf[x] of
  366. isc_info_db_sql_dialect :
  367. begin
  368. Inc(x);
  369. Len := isc_vax_integer(pchar(@ResBuf[x]), 2);
  370. Inc(x, 2);
  371. FDatabaseInfo.Dialect := isc_vax_integer(pchar(@ResBuf[x]), Len);
  372. Inc(x, Len);
  373. end;
  374. isc_info_ods_version :
  375. begin
  376. Inc(x);
  377. Len := isc_vax_integer(pchar(@ResBuf[x]), 2);
  378. Inc(x, 2);
  379. FDatabaseInfo.ODSMajorVersion := isc_vax_integer(pchar(@ResBuf[x]), Len);
  380. Inc(x, Len);
  381. end;
  382. isc_info_version :
  383. begin
  384. Inc(x);
  385. Len := isc_vax_integer(pchar(@ResBuf[x]), 2);
  386. Inc(x, 2);
  387. SetString(FDatabaseInfo.ServerVersionString, PAnsiChar(@ResBuf[x + 2]), Len-2);
  388. FDatabaseInfo.ServerVersion := ParseServerVersion(FDatabaseInfo.ServerVersionString);
  389. Inc(x, Len);
  390. end;
  391. isc_info_end, isc_info_error : Break;
  392. isc_info_truncated : Break; //result buffer too small; fix your code!
  393. else
  394. inc(x);
  395. end;
  396. end;
  397. end;
  398. procedure TIBConnection.ResetDatabaseInfo;
  399. begin
  400. FDatabaseInfo.Dialect:=0;
  401. FDatabaseInfo.ODSMajorVersion:=0;
  402. FDatabaseInfo.ServerVersion:='';
  403. FDatabaseInfo.ServerVersionString:=''; // don't confuse applications with 'Firebird' or 'Interbase'
  404. end;
  405. function TIBConnection.GetODSMajorVersion: integer;
  406. begin
  407. result:=FDatabaseInfo.ODSMajorVersion;
  408. end;
  409. function TIBConnection.ParseServerVersion(const CompleteVersion: string): string;
  410. // String representation of integer version number derived from
  411. // major.minor.build => should give e.g. 020501
  412. const
  413. Delimiter = '.';
  414. DigitsPerNumber = 2;
  415. MaxNumbers = 3;
  416. var
  417. BeginPos,EndPos,StartLook,i: integer;
  418. NumericPart: string;
  419. begin
  420. result := '';
  421. // Ignore 6.x version number in front of "Firebird"
  422. StartLook := Pos('Firebird', CompleteVersion);
  423. if StartLook = 0 then
  424. StartLook := 1;
  425. BeginPos := 0;
  426. // Catch all numerics + decimal point:
  427. for i := StartLook to Length(CompleteVersion) do
  428. begin
  429. if (BeginPos > 0) and
  430. ((CompleteVersion[i] < '0') or (CompleteVersion[i] > '9')) and (CompleteVersion[i] <> '.') then
  431. begin
  432. EndPos := i - 1;
  433. break;
  434. end;
  435. if (BeginPos = 0) and
  436. (CompleteVersion[i] >= '0') and (CompleteVersion[i] <= '9') then
  437. begin
  438. BeginPos := i;
  439. end;
  440. end;
  441. if BeginPos > 0 then
  442. begin
  443. NumericPart := copy(CompleteVersion, BeginPos, 1+EndPos-BeginPos);
  444. BeginPos := 1;
  445. for i := 1 to MaxNumbers do
  446. begin
  447. EndPos := PosEx(Delimiter,NumericPart,BeginPos);
  448. if EndPos > 0 then
  449. begin
  450. result := result + rightstr(StringOfChar('0',DigitsPerNumber)+copy(NumericPart,BeginPos,EndPos-BeginPos),DigitsPerNumber);
  451. BeginPos := EndPos+1;
  452. end
  453. else
  454. begin
  455. result := result + rightstr(StringOfChar('0',DigitsPerNumber)+copy(NumericPart,BeginPos,Length(NumericPart)),DigitsPerNumber);
  456. break;
  457. end;
  458. end;
  459. result := leftstr(result + StringOfChar('0',DigitsPerNumber * MaxNumbers), DigitsPerNumber * MaxNumbers);
  460. end;
  461. end;
  462. procedure TIBConnection.ConnectFB;
  463. var
  464. ADatabaseName: String;
  465. DPB: string;
  466. begin
  467. DPB := chr(isc_dpb_version1);
  468. if (UserName <> '') then
  469. begin
  470. DPB := DPB + chr(isc_dpb_user_name) + chr(Length(UserName)) + UserName;
  471. if (Password <> '') then
  472. DPB := DPB + chr(isc_dpb_password) + chr(Length(Password)) + Password;
  473. end;
  474. if (Role <> '') then
  475. DPB := DPB + chr(isc_dpb_sql_role_name) + chr(Length(Role)) + Role;
  476. if Length(CharSet) > 0 then
  477. DPB := DPB + Chr(isc_dpb_lc_ctype) + Chr(Length(CharSet)) + CharSet;
  478. FSQLDatabaseHandle := nil;
  479. if HostName <> '' then ADatabaseName := HostName+':'+DatabaseName
  480. else ADatabaseName := DatabaseName;
  481. if isc_attach_database(@FStatus[0], Length(ADatabaseName), @ADatabaseName[1],
  482. @FSQLDatabaseHandle,
  483. Length(DPB), @DPB[1]) <> 0 then
  484. CheckError('DoInternalConnect', FStatus);
  485. end;
  486. function TIBConnection.GetDialect: integer;
  487. begin
  488. if FDialect = INVALID_DATA then
  489. begin
  490. if FDatabaseInfo.Dialect=0 then
  491. Result := DEFDIALECT
  492. else
  493. Result := FDatabaseInfo.Dialect;
  494. end else
  495. Result := FDialect;
  496. end;
  497. procedure TIBConnection.AllocSQLDA(var aSQLDA : PXSQLDA;Count : integer);
  498. begin
  499. FreeSQLDABuffer(aSQLDA);
  500. if count > -1 then
  501. begin
  502. reAllocMem(aSQLDA, XSQLDA_Length(Count));
  503. { Zero out the memory block to avoid problems with exceptions within the
  504. constructor of this class. }
  505. FillChar(aSQLDA^, XSQLDA_Length(Count), 0);
  506. aSQLDA^.Version := sqlda_version1;
  507. aSQLDA^.SQLN := Count;
  508. end
  509. else
  510. reAllocMem(aSQLDA,0);
  511. end;
  512. procedure TIBConnection.TranslateFldType(SQLType, SQLSubType, SQLLen, SQLScale : integer;
  513. var TrType : TFieldType; var TrLen : word);
  514. begin
  515. TrLen := 0;
  516. if SQLScale < 0 then
  517. begin
  518. TrLen := abs(SQLScale);
  519. if (TrLen <= MaxBCDScale) then //Note: NUMERIC(18,3) or (17,2) must be mapped to ftFmtBCD, but we do not know Precision
  520. TrType := ftBCD
  521. else
  522. TrType := ftFMTBcd;
  523. end
  524. else case (SQLType and not 1) of
  525. SQL_VARYING :
  526. begin
  527. TrType := ftString;
  528. TrLen := SQLLen;
  529. end;
  530. SQL_TEXT :
  531. begin
  532. TrType := ftFixedChar;
  533. TrLen := SQLLen;
  534. end;
  535. SQL_TYPE_DATE :
  536. TrType := ftDate;
  537. SQL_TYPE_TIME :
  538. TrType := ftTime;
  539. SQL_TIMESTAMP :
  540. TrType := ftDateTime;
  541. SQL_ARRAY :
  542. begin
  543. TrType := ftArray;
  544. TrLen := SQLLen;
  545. end;
  546. SQL_BLOB :
  547. begin
  548. if SQLSubType = isc_blob_text then
  549. TrType := ftMemo
  550. else
  551. TrType := ftBlob;
  552. TrLen := SQLLen;
  553. end;
  554. SQL_SHORT :
  555. TrType := ftSmallint;
  556. SQL_LONG :
  557. TrType := ftInteger;
  558. SQL_INT64 :
  559. TrType := ftLargeInt;
  560. SQL_DOUBLE :
  561. TrType := ftFloat;
  562. SQL_FLOAT :
  563. TrType := ftFloat;
  564. SQL_BOOLEAN_INTERBASE, SQL_BOOLEAN_FIREBIRD :
  565. TrType := ftBoolean;
  566. else
  567. TrType := ftUnknown;
  568. end;
  569. end;
  570. function TIBConnection.AllocateCursorHandle: TSQLCursor;
  571. var curs : TIBCursor;
  572. begin
  573. curs := TIBCursor.create;
  574. curs.sqlda := nil;
  575. curs.statement := nil;
  576. curs.FPrepared := False;
  577. AllocSQLDA(curs.SQLDA,0);
  578. result := curs;
  579. end;
  580. procedure TIBConnection.DeAllocateCursorHandle(var cursor : TSQLCursor);
  581. begin
  582. if assigned(cursor) then with cursor as TIBCursor do
  583. begin
  584. AllocSQLDA(SQLDA,-1);
  585. AllocSQLDA(in_SQLDA,-1);
  586. end;
  587. FreeAndNil(cursor);
  588. end;
  589. function TIBConnection.AllocateTransactionHandle: TSQLHandle;
  590. begin
  591. result := TIBTrans.create;
  592. end;
  593. procedure TIBConnection.PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams);
  594. var dh : pointer;
  595. tr : pointer;
  596. x : Smallint;
  597. info_request : string;
  598. resbuf : array[0..7] of byte;
  599. blockSize : integer;
  600. IBStatementType: integer;
  601. begin
  602. with cursor as TIBcursor do
  603. begin
  604. dh := GetHandle;
  605. if isc_dsql_allocate_statement(@Status[0], @dh, @Statement) <> 0 then
  606. CheckError('PrepareStatement', Status);
  607. tr := aTransaction.Handle;
  608. if assigned(AParams) and (AParams.count > 0) then
  609. buf := AParams.ParseSQL(buf,false,sqEscapeSlash in ConnOptions, sqEscapeRepeat in ConnOptions,psInterbase,paramBinding);
  610. if isc_dsql_prepare(@Status[0], @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
  611. CheckError('PrepareStatement', Status);
  612. if assigned(AParams) and (AParams.count > 0) then
  613. begin
  614. AllocSQLDA(in_SQLDA,Length(ParamBinding));
  615. if isc_dsql_describe_bind(@Status[0], @Statement, 1, in_SQLDA) <> 0 then
  616. CheckError('PrepareStatement', Status);
  617. if in_SQLDA^.SQLD > in_SQLDA^.SQLN then
  618. DatabaseError(SParameterCountIncorrect,self);
  619. {$push}
  620. {$R-}
  621. for x := 0 to in_SQLDA^.SQLD - 1 do with in_SQLDA^.SQLVar[x] do
  622. begin
  623. if ((SQLType and not 1) = SQL_VARYING) then
  624. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen+2)
  625. else
  626. SQLData := AllocMem(in_SQLDA^.SQLVar[x].SQLLen);
  627. // Always force the creation of slqind for parameters. It could be
  628. // that a database trigger takes care of inserting null values, so
  629. // it should always be possible to pass null parameters. If that fails,
  630. // the database server will generate the appropriate error.
  631. sqltype := sqltype or 1;
  632. new(sqlind);
  633. end;
  634. {$pop}
  635. end
  636. else
  637. AllocSQLDA(in_SQLDA,0);
  638. // Get the statement type from firebird/interbase
  639. info_request := chr(isc_info_sql_stmt_type);
  640. if isc_dsql_sql_info(@Status[0],@Statement,Length(info_request), @info_request[1],sizeof(resbuf),@resbuf) <> 0 then
  641. CheckError('PrepareStatement', Status);
  642. assert(resbuf[0]=isc_info_sql_stmt_type);
  643. BlockSize:=isc_vax_integer(@resbuf[1],2);
  644. IBStatementType:=isc_vax_integer(@resbuf[3],blockSize);
  645. assert(resbuf[3+blockSize]=isc_info_end);
  646. // If the StatementType is isc_info_sql_stmt_exec_procedure then
  647. // override the statement type derived by parsing the query.
  648. // This to recognize statements like 'insert into .. returning' correctly
  649. case IBStatementType of
  650. isc_info_sql_stmt_select: FStatementType := stSelect;
  651. isc_info_sql_stmt_insert: FStatementType := stInsert;
  652. isc_info_sql_stmt_update: FStatementType := stUpdate;
  653. isc_info_sql_stmt_delete: FStatementType := stDelete;
  654. isc_info_sql_stmt_exec_procedure: FStatementType := stExecProcedure;
  655. end;
  656. FSelectable := FStatementType in [stSelect,stExecProcedure];
  657. if FSelectable then
  658. begin
  659. if isc_dsql_describe(@Status[0], @Statement, 1, SQLDA) <> 0 then
  660. CheckError('PrepareSelect', Status);
  661. if SQLDA^.SQLD > SQLDA^.SQLN then
  662. begin
  663. AllocSQLDA(SQLDA,SQLDA^.SQLD);
  664. if isc_dsql_describe(@Status[0], @Statement, 1, SQLDA) <> 0 then
  665. CheckError('PrepareSelect', Status);
  666. end;
  667. FSelectable := SQLDA^.SQLD > 0;
  668. {$push}
  669. {$R-}
  670. for x := 0 to SQLDA^.SQLD - 1 do with SQLDA^.SQLVar[x] do
  671. begin
  672. if ((SQLType and not 1) = SQL_VARYING) then
  673. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen+2)
  674. else
  675. SQLData := AllocMem(SQLDA^.SQLVar[x].SQLLen);
  676. if (SQLType and 1) = 1 then New(SQLInd);
  677. end;
  678. {$pop}
  679. end;
  680. FPrepared := True;
  681. end;
  682. end;
  683. procedure TIBConnection.UnPrepareStatement(cursor : TSQLCursor);
  684. begin
  685. with cursor as TIBcursor do
  686. if assigned(Statement) Then
  687. begin
  688. if isc_dsql_free_statement(@Status[0], @Statement, DSQL_Drop) <> 0 then
  689. CheckError('FreeStatement', Status);
  690. Statement := nil;
  691. FPrepared := False;
  692. end;
  693. end;
  694. procedure TIBConnection.FreeSQLDABuffer(var aSQLDA : PXSQLDA);
  695. var x : Smallint;
  696. begin
  697. {$push}
  698. {$R-}
  699. if assigned(aSQLDA) then
  700. for x := 0 to aSQLDA^.SQLN - 1 do
  701. begin
  702. reAllocMem(aSQLDA^.SQLVar[x].SQLData,0);
  703. if assigned(aSQLDA^.SQLVar[x].sqlind) then
  704. begin
  705. Dispose(aSQLDA^.SQLVar[x].sqlind);
  706. aSQLDA^.SQLVar[x].sqlind := nil;
  707. end
  708. end;
  709. {$pop}
  710. end;
  711. function TIBConnection.IsDialectStored: boolean;
  712. begin
  713. result := (FDialect<>INVALID_DATA);
  714. end;
  715. procedure TIBConnection.DoConnect;
  716. const NoQuotes: TQuoteChars = (' ',' ');
  717. begin
  718. inherited DoConnect;
  719. GetDatabaseInfo; //Get db dialect, db metadata
  720. if Dialect < 3 then
  721. FieldNameQuoteChars := NoQuotes
  722. else
  723. FieldNameQuoteChars := DoubleQuotes;
  724. end;
  725. procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor);
  726. begin
  727. with cursor as TIBCursor do
  728. begin
  729. FreeSQLDABuffer(SQLDA);
  730. FreeSQLDABuffer(in_SQLDA);
  731. SetLength(FieldBinding,0);
  732. end;
  733. end;
  734. procedure TIBConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams);
  735. var tr : pointer;
  736. out_SQLDA : PXSQLDA;
  737. begin
  738. tr := aTransaction.Handle;
  739. if Assigned(APArams) and (AParams.count > 0) then SetParameters(cursor, atransaction, AParams);
  740. with cursor as TIBCursor do
  741. begin
  742. if FStatementType = stExecProcedure then
  743. out_SQLDA := SQLDA
  744. else
  745. out_SQLDA := nil;
  746. if isc_dsql_execute2(@Status[0], @tr, @Statement, 1, in_SQLDA, out_SQLDA) <> 0 then
  747. CheckError('Execute', Status);
  748. end;
  749. end;
  750. procedure TIBConnection.AddFieldDefs(cursor: TSQLCursor;FieldDefs : TfieldDefs);
  751. var
  752. x : integer;
  753. TransLen : word;
  754. TransType : TFieldType;
  755. FD : TFieldDef;
  756. begin
  757. {$push}
  758. {$R-}
  759. with cursor as TIBCursor do
  760. begin
  761. setlength(FieldBinding,SQLDA^.SQLD);
  762. for x := 0 to SQLDA^.SQLD - 1 do
  763. begin
  764. TranslateFldType(SQLDA^.SQLVar[x].SQLType, SQLDA^.SQLVar[x].sqlsubtype, SQLDA^.SQLVar[x].SQLLen, SQLDA^.SQLVar[x].SQLScale,
  765. TransType, TransLen);
  766. FD := FieldDefs.Add(FieldDefs.MakeNameUnique(SQLDA^.SQLVar[x].AliasName), TransType,
  767. TransLen, (SQLDA^.SQLVar[x].sqltype and 1)=0, (x + 1));
  768. if TransType in [ftBCD, ftFmtBCD] then
  769. case (SQLDA^.SQLVar[x].sqltype and not 1) of
  770. SQL_SHORT : FD.precision := 4;
  771. SQL_LONG : FD.precision := 9;
  772. SQL_DOUBLE,
  773. SQL_INT64 : FD.precision := 18;
  774. else FD.precision := SQLDA^.SQLVar[x].SQLLen;
  775. end;
  776. // FD.DisplayName := SQLDA^.SQLVar[x].AliasName;
  777. FieldBinding[FD.FieldNo-1] := x;
  778. end;
  779. end;
  780. {$pop}
  781. end;
  782. function TIBConnection.GetHandle: pointer;
  783. begin
  784. Result := FSQLDatabaseHandle;
  785. end;
  786. function TIBConnection.Fetch(cursor : TSQLCursor) : boolean;
  787. var
  788. retcode : integer;
  789. begin
  790. with cursor as TIBCursor do
  791. begin
  792. if FStatementType = stExecProcedure then
  793. //do not fetch from a non-select statement, i.e. statement which has no cursor
  794. //on Firebird 2.5+ it leads to error 'Invalid cursor reference'
  795. if SQLDA^.SQLD = 0 then
  796. retcode := 100 //no more rows to retrieve
  797. else
  798. begin
  799. retcode := 0;
  800. SQLDA^.SQLD := 0; //hack: mark after first fetch
  801. end
  802. else
  803. retcode := isc_dsql_fetch(@Status[0], @Statement, 1, SQLDA);
  804. if (retcode <> 0) and (retcode <> 100) then
  805. CheckError('Fetch', Status);
  806. end;
  807. Result := (retcode = 0);
  808. end;
  809. function IntPower10(e: integer): double;
  810. const PreComputedPower10: array[0..9] of integer = (1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000);
  811. var n: integer;
  812. begin
  813. n := abs(e); //exponent can't be greater than 18
  814. if n <= 9 then
  815. Result := PreComputedPower10[n]
  816. else
  817. Result := PreComputedPower10[9] * PreComputedPower10[n-9];
  818. if e < 0 then
  819. Result := 1 / Result;
  820. end;
  821. procedure TIBConnection.SetParameters(cursor : TSQLCursor; aTransation : TSQLTransaction; AParams : TParams);
  822. var ParNr,SQLVarNr : integer;
  823. s : string;
  824. i : integer;
  825. si : smallint;
  826. li : LargeInt;
  827. currbuff : pchar;
  828. w : word;
  829. TransactionHandle : pointer;
  830. blobId : ISC_QUAD;
  831. blobHandle : Isc_blob_Handle;
  832. BlobSize,
  833. BlobBytesWritten : longint;
  834. procedure SetBlobParam;
  835. begin
  836. {$push}
  837. {$R-}
  838. with cursor as TIBCursor do
  839. begin
  840. TransactionHandle := aTransation.Handle;
  841. blobhandle := FB_API_NULLHANDLE;
  842. if isc_create_blob(@FStatus[0], @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, @blobId) <> 0 then
  843. CheckError('TIBConnection.CreateBlobStream', FStatus);
  844. s := AParams[ParNr].AsString;
  845. BlobSize := length(s);
  846. BlobBytesWritten := 0;
  847. i := 0;
  848. // Write in segments of MAXBLOBSEGMENTSIZE, as that is the fastest.
  849. // We ignore BlobSegmentSize property.
  850. while BlobBytesWritten < (BlobSize-MAXBLOBSEGMENTSIZE) do
  851. begin
  852. isc_put_segment(@FStatus[0], @blobHandle, MAXBLOBSEGMENTSIZE, @s[(i*MAXBLOBSEGMENTSIZE)+1]);
  853. inc(BlobBytesWritten,MAXBLOBSEGMENTSIZE);
  854. inc(i);
  855. end;
  856. if BlobBytesWritten <> BlobSize then
  857. isc_put_segment(@FStatus[0], @blobHandle, BlobSize-BlobBytesWritten, @s[(i*MAXBLOBSEGMENTSIZE)+1]);
  858. if isc_close_blob(@FStatus[0], @blobHandle) <> 0 then
  859. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  860. Move(blobId, in_sqlda^.SQLvar[SQLVarNr].SQLData^, in_SQLDA^.SQLVar[SQLVarNr].SQLLen);
  861. end;
  862. {$pop}
  863. end;
  864. Const
  865. DateF = 'yyyy-mm-dd';
  866. TimeF = 'hh:nn:ss';
  867. DateTimeF = DateF+' '+TimeF;
  868. var
  869. // This should be a pointer, because the ORIGINAL variables must
  870. // be modified.
  871. VSQLVar: ^XSQLVAR;
  872. P: TParam;
  873. ft : TFieldType;
  874. begin
  875. {$push}
  876. {$R-}
  877. with cursor as TIBCursor do for SQLVarNr := 0 to High(ParamBinding){AParams.count-1} do
  878. begin
  879. ParNr := ParamBinding[SQLVarNr];
  880. VSQLVar := @in_sqlda^.SQLvar[SQLVarNr];
  881. if AParams[ParNr].IsNull then
  882. VSQLVar^.SQLInd^ := -1
  883. else
  884. begin
  885. VSQLVar^.SQLInd^ := 0;
  886. case (VSQLVar^.sqltype and not 1) of
  887. SQL_SHORT, SQL_BOOLEAN_INTERBASE :
  888. begin
  889. if VSQLVar^.sqlscale = 0 then
  890. si := AParams[ParNr].AsSmallint
  891. else
  892. si := Round(AParams[ParNr].AsCurrency * IntPower10(-VSQLVar^.sqlscale));
  893. i := si;
  894. Move(i, VSQLVar^.SQLData^, VSQLVar^.SQLLen);
  895. end;
  896. SQL_LONG :
  897. begin
  898. if VSQLVar^.sqlscale = 0 then
  899. i := AParams[ParNr].AsInteger
  900. else
  901. i := Round(AParams[ParNr].AsFloat * IntPower10(-VSQLVar^.sqlscale)); //*any number of digits
  902. Move(i, VSQLVar^.SQLData^, VSQLVar^.SQLLen);
  903. end;
  904. SQL_INT64:
  905. begin
  906. if VSQLVar^.sqlscale = 0 then
  907. li := AParams[ParNr].AsLargeInt
  908. else if AParams[ParNr].DataType = ftFMTBcd then
  909. li := AParams[ParNr].AsFMTBCD * IntPower10(-VSQLVar^.sqlscale)
  910. else
  911. li := Round(AParams[ParNr].AsCurrency * IntPower10(-VSQLVar^.sqlscale));
  912. Move(li, VSQLVar^.SQLData^, VSQLVar^.SQLLen);
  913. end;
  914. SQL_DOUBLE, SQL_FLOAT:
  915. SetFloat(VSQLVar^.SQLData, AParams[ParNr].AsFloat, VSQLVar^.SQLLen);
  916. SQL_BLOB :
  917. SetBlobParam;
  918. SQL_VARYING, SQL_TEXT :
  919. begin
  920. P:=AParams[ParNr];
  921. ft:=P.DataType;
  922. if Not (ft in [ftDate,ftTime,ftDateTime,ftTimeStamp]) then
  923. S:=P.AsString
  924. else
  925. begin
  926. Case ft of
  927. ftDate : S:=DateF;
  928. ftTime : S:=TimeF;
  929. ftDateTime,
  930. ftTimeStamp : S:=DateTimeF;
  931. end;
  932. S:=FormatDateTime(S,P.AsDateTime);
  933. end;
  934. w := length(s); // a word is enough, since the max-length of a string in interbase is 32k
  935. if ((VSQLVar^.SQLType and not 1) = SQL_VARYING) then
  936. begin
  937. VSQLVar^.SQLLen := w;
  938. ReAllocMem(VSQLVar^.SQLData, VSQLVar^.SQLLen+2);
  939. CurrBuff := VSQLVar^.SQLData;
  940. move(w,CurrBuff^,sizeof(w));
  941. inc(CurrBuff,2);
  942. end
  943. else
  944. begin
  945. // The buffer-length is always VSQLVar^.sqllen, nothing more, nothing less
  946. // so fill the complete buffer with valid data. Adding #0 will lead
  947. // to problems, because the #0 will be seen as a part of the (binary) string
  948. CurrBuff := VSQLVar^.SQLData;
  949. w := VSQLVar^.sqllen;
  950. s := PadRight(s,w);
  951. end;
  952. Move(s[1], CurrBuff^, w);
  953. end;
  954. SQL_TYPE_DATE, SQL_TYPE_TIME, SQL_TIMESTAMP :
  955. SetDateTime(VSQLVar^.SQLData, AParams[ParNr].AsDateTime, VSQLVar^.SQLType);
  956. SQL_BOOLEAN_FIREBIRD:
  957. PByte(VSQLVar^.SQLData)^ := Byte(AParams[ParNr].AsBoolean);
  958. else
  959. DatabaseErrorFmt(SUnsupportedParameter,[Fieldtypenames[AParams[ParNr].DataType]],self);
  960. end {case}
  961. end;
  962. end;
  963. {$pop}
  964. end;
  965. function TIBConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
  966. var
  967. VSQLVar : PXSQLVAR;
  968. VarcharLen : word;
  969. CurrBuff : pchar;
  970. c : currency;
  971. AFmtBcd : tBCD;
  972. function BcdDivPower10(Dividend: largeint; e: integer): TBCD;
  973. var d: double;
  974. begin
  975. d := Dividend / IntPower10(e);
  976. Result := StrToBCD( FloatToStr(d) );
  977. end;
  978. begin
  979. CreateBlob := False;
  980. with cursor as TIBCursor do
  981. begin
  982. {$push}
  983. {$R-}
  984. VSQLVar := @SQLDA^.SQLVar[ FieldBinding[FieldDef.FieldNo-1] ];
  985. // Joost, 5 jan 2006: I disabled the following, since it's useful for
  986. // debugging, but it also slows things down. In principle things can only go
  987. // wrong when FieldDefs is changed while the dataset is opened. A user just
  988. // shoudn't do that. ;) (The same is done in PQConnection)
  989. // if VSQLVar^.AliasName <> FieldDef.Name then
  990. // DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  991. if assigned(VSQLVar^.SQLInd) and (VSQLVar^.SQLInd^ = -1) then
  992. result := false
  993. else
  994. begin
  995. with VSQLVar^ do
  996. if ((SQLType and not 1) = SQL_VARYING) then
  997. begin
  998. Move(SQLData^, VarcharLen, 2);
  999. CurrBuff := SQLData + 2;
  1000. end
  1001. else
  1002. begin
  1003. CurrBuff := SQLData;
  1004. VarCharLen := FieldDef.Size;
  1005. end;
  1006. Result := true;
  1007. case FieldDef.DataType of
  1008. ftBCD :
  1009. begin
  1010. case VSQLVar^.SQLLen of
  1011. 2 : c := PSmallint(CurrBuff)^ / IntPower10(-VSQLVar^.SQLScale);
  1012. 4 : c := PLongint(CurrBuff)^ / IntPower10(-VSQLVar^.SQLScale);
  1013. 8 : if Dialect < 3 then
  1014. c := PDouble(CurrBuff)^
  1015. else
  1016. c := PLargeint(CurrBuff)^ / IntPower10(-VSQLVar^.SQLScale);
  1017. else
  1018. Result := False; // Just to be sure, in principle this will never happen
  1019. end; {case}
  1020. Move(c, buffer^ , sizeof(c));
  1021. end;
  1022. ftFMTBcd :
  1023. begin
  1024. case VSQLVar^.SQLLen of
  1025. 2 : AFmtBcd := BcdDivPower10(PSmallint(CurrBuff)^, -VSQLVar^.SQLScale);
  1026. 4 : AFmtBcd := BcdDivPower10(PLongint(CurrBuff)^, -VSQLVar^.SQLScale);
  1027. 8 : if Dialect < 3 then
  1028. AFmtBcd := PDouble(CurrBuff)^
  1029. else
  1030. AFmtBcd := BcdDivPower10(PLargeint(CurrBuff)^, -VSQLVar^.SQLScale);
  1031. else
  1032. Result := False; // Just to be sure, in principle this will never happen
  1033. end; {case}
  1034. Move(AFmtBcd, buffer^ , sizeof(AFmtBcd));
  1035. end;
  1036. ftInteger :
  1037. begin
  1038. FillByte(buffer^,sizeof(Longint),0);
  1039. Move(CurrBuff^, Buffer^, VSQLVar^.SQLLen);
  1040. end;
  1041. ftLargeint :
  1042. begin
  1043. FillByte(buffer^,sizeof(LargeInt),0);
  1044. Move(CurrBuff^, Buffer^, VSQLVar^.SQLLen);
  1045. end;
  1046. ftSmallint :
  1047. begin
  1048. FillByte(buffer^,sizeof(Smallint),0);
  1049. Move(CurrBuff^, Buffer^, VSQLVar^.SQLLen);
  1050. end;
  1051. ftDate, ftTime, ftDateTime:
  1052. GetDateTime(CurrBuff, Buffer, VSQLVar^.SQLType);
  1053. ftString, ftFixedChar :
  1054. begin
  1055. Move(CurrBuff^, Buffer^, VarCharLen);
  1056. PChar(Buffer + VarCharLen)^ := #0;
  1057. end;
  1058. ftFloat :
  1059. GetFloat(CurrBuff, Buffer, VSQLVar^.SQLLen);
  1060. ftBlob,
  1061. ftMemo :
  1062. begin // load the BlobIb in field's buffer
  1063. FillByte(buffer^,sizeof(TBufBlobField),0);
  1064. Move(CurrBuff^, Buffer^, VSQLVar^.SQLLen);
  1065. end;
  1066. ftBoolean :
  1067. begin
  1068. case VSQLVar^.SQLLen of
  1069. 1: PWordBool(Buffer)^ := PByte(CurrBuff)^ <> 0; // Firebird
  1070. 2: PWordBool(Buffer)^ := PSmallint(CurrBuff)^ <> 0; // Interbase
  1071. end;
  1072. end
  1073. else
  1074. begin
  1075. result := false;
  1076. databaseerrorfmt(SUnsupportedFieldType, [Fieldtypenames[FieldDef.DataType], Self]);
  1077. end
  1078. end; { case }
  1079. end; { if/else }
  1080. {$pop}
  1081. end; { with cursor }
  1082. end;
  1083. {$DEFINE SUPPORT_MSECS}
  1084. {$IFDEF SUPPORT_MSECS}
  1085. const
  1086. IBDateOffset = 15018; //an offset from 17 Nov 1858.
  1087. IBTimeFractionsPerDay = SecsPerDay * ISC_TIME_SECONDS_PRECISION; //Number of Firebird time fractions per day
  1088. {$ELSE}
  1089. {$PACKRECORDS C}
  1090. type
  1091. TTm = record
  1092. tm_sec : longint;
  1093. tm_min : longint;
  1094. tm_hour : longint;
  1095. tm_mday : longint;
  1096. tm_mon : longint;
  1097. tm_year : longint;
  1098. tm_wday : longint;
  1099. tm_yday : longint;
  1100. tm_isdst: longint;
  1101. __tm_gmtoff : PtrInt; // Seconds east of UTC
  1102. __tm_zone : PAnsiChar; // Timezone abbreviation
  1103. end;
  1104. {$PACKRECORDS DEFAULT}
  1105. {$ENDIF}
  1106. procedure TIBConnection.GetDateTime(CurrBuff, Buffer : pointer; AType : integer);
  1107. var
  1108. {$IFNDEF SUPPORT_MSECS}
  1109. CTime : TTm; // C struct time
  1110. STime : TSystemTime; // System time
  1111. {$ENDIF}
  1112. PTime : TDateTime; // Pascal time
  1113. begin
  1114. case (AType and not 1) of
  1115. SQL_TYPE_DATE :
  1116. {$IFNDEF SUPPORT_MSECS}
  1117. isc_decode_sql_date(PISC_DATE(CurrBuff), @CTime);
  1118. {$ELSE}
  1119. PTime := PISC_DATE(CurrBuff)^ - IBDateOffset;
  1120. {$ENDIF}
  1121. SQL_TYPE_TIME :
  1122. {$IFNDEF SUPPORT_MSECS}
  1123. isc_decode_sql_time(PISC_TIME(CurrBuff), @CTime);
  1124. {$ELSE}
  1125. PTime := PISC_TIME(CurrBuff)^ / IBTimeFractionsPerDay;
  1126. {$ENDIF}
  1127. SQL_TIMESTAMP :
  1128. begin
  1129. {$IFNDEF SUPPORT_MSECS}
  1130. isc_decode_timestamp(PISC_TIMESTAMP(CurrBuff), @CTime);
  1131. {$ELSE}
  1132. PTime := ComposeDateTime(
  1133. PISC_TIMESTAMP(CurrBuff)^.timestamp_date - IBDateOffset,
  1134. PISC_TIMESTAMP(CurrBuff)^.timestamp_time / IBTimeFractionsPerDay
  1135. );
  1136. {$ENDIF}
  1137. end
  1138. else
  1139. Raise EIBDatabaseError.CreateFmt('Invalid parameter type for date Decode : %d',[(AType and not 1)]);
  1140. end;
  1141. {$IFNDEF SUPPORT_MSECS}
  1142. STime.Year := CTime.tm_year + 1900;
  1143. STime.Month := CTime.tm_mon + 1;
  1144. STime.Day := CTime.tm_mday;
  1145. STime.Hour := CTime.tm_hour;
  1146. STime.Minute := CTime.tm_min;
  1147. STime.Second := CTime.tm_sec;
  1148. STime.Millisecond := 0;
  1149. PTime := SystemTimeToDateTime(STime);
  1150. {$ENDIF}
  1151. Move(PTime, Buffer^, SizeOf(PTime));
  1152. end;
  1153. procedure TIBConnection.SetDateTime(CurrBuff: pointer; PTime : TDateTime; AType : integer);
  1154. {$IFNDEF SUPPORT_MSECS}
  1155. var
  1156. CTime : TTm; // C struct time
  1157. STime : TSystemTime; // System time
  1158. {$ENDIF}
  1159. begin
  1160. {$IFNDEF SUPPORT_MSECS}
  1161. DateTimeToSystemTime(PTime,STime);
  1162. CTime.tm_year := STime.Year - 1900;
  1163. CTime.tm_mon := STime.Month -1;
  1164. CTime.tm_mday := STime.Day;
  1165. CTime.tm_hour := STime.Hour;
  1166. CTime.tm_min := STime.Minute;
  1167. CTime.tm_sec := STime.Second;
  1168. {$ENDIF}
  1169. case (AType and not 1) of
  1170. SQL_TYPE_DATE :
  1171. {$IFNDEF SUPPORT_MSECS}
  1172. isc_encode_sql_date(@CTime, PISC_DATE(CurrBuff));
  1173. {$ELSE}
  1174. PISC_DATE(CurrBuff)^ := Trunc(PTime) + IBDateOffset;
  1175. {$ENDIF}
  1176. SQL_TYPE_TIME :
  1177. {$IFNDEF SUPPORT_MSECS}
  1178. isc_encode_sql_time(@CTime, PISC_TIME(CurrBuff));
  1179. {$ELSE}
  1180. PISC_TIME(CurrBuff)^ := Round(abs(Frac(PTime)) * IBTimeFractionsPerDay);
  1181. {$ENDIF}
  1182. SQL_TIMESTAMP :
  1183. begin
  1184. {$IFNDEF SUPPORT_MSECS}
  1185. isc_encode_timestamp(@CTime, PISC_TIMESTAMP(CurrBuff));
  1186. {$ELSE}
  1187. PISC_TIMESTAMP(CurrBuff)^.timestamp_date := Trunc(PTime) + IBDateOffset;
  1188. PISC_TIMESTAMP(CurrBuff)^.timestamp_time := Round(abs(Frac(PTime)) * IBTimeFractionsPerDay);
  1189. {$ENDIF}
  1190. end
  1191. else
  1192. Raise EIBDatabaseError.CreateFmt('Invalid parameter type for date encode : %d',[(AType and not 1)]);
  1193. end;
  1194. end;
  1195. function TIBConnection.GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string;
  1196. var s : string;
  1197. begin
  1198. case SchemaType of
  1199. stTables : s := 'select '+
  1200. 'rdb$relation_id as recno, '+
  1201. '''' + DatabaseName + ''' as catalog_name, '+
  1202. ''''' as schema_name, '+
  1203. 'rdb$relation_name as table_name, '+
  1204. '0 as table_type '+
  1205. 'from '+
  1206. 'rdb$relations '+
  1207. 'where '+
  1208. '(rdb$system_flag = 0 or rdb$system_flag is null) ' + // and rdb$view_blr is null
  1209. 'order by rdb$relation_name';
  1210. stSysTables : s := 'select '+
  1211. 'rdb$relation_id as recno, '+
  1212. '''' + DatabaseName + ''' as catalog_name, '+
  1213. ''''' as schema_name, '+
  1214. 'rdb$relation_name as table_name, '+
  1215. '0 as table_type '+
  1216. 'from '+
  1217. 'rdb$relations '+
  1218. 'where '+
  1219. '(rdb$system_flag > 0) ' + // and rdb$view_blr is null
  1220. 'order by rdb$relation_name';
  1221. stProcedures : s := 'select '+
  1222. 'rdb$procedure_id as recno, '+
  1223. '''' + DatabaseName + ''' as catalog_name, '+
  1224. ''''' as schema_name, '+
  1225. 'rdb$procedure_name as procedure_name, '+
  1226. '0 as procedure_type, '+
  1227. 'rdb$procedure_inputs as in_params, '+
  1228. 'rdb$procedure_outputs as out_params '+
  1229. 'from '+
  1230. 'rdb$procedures '+
  1231. 'WHERE '+
  1232. '(rdb$system_flag = 0 or rdb$system_flag is null)';
  1233. stColumns : s := 'SELECT '+
  1234. 'rdb$field_id as recno, '+
  1235. '''' + DatabaseName + ''' as catalog_name, '+
  1236. ''''' as schema_name, '+
  1237. 'rdb$relation_name as table_name, '+
  1238. 'r.rdb$field_name as column_name, '+
  1239. 'rdb$field_position+1 as column_position, '+
  1240. '0 as column_type, '+
  1241. 'rdb$field_type as column_datatype, '+
  1242. 'rdb$type_name as column_typename, '+
  1243. 'rdb$field_sub_type as column_subtype, '+
  1244. 'rdb$field_precision as column_precision, '+
  1245. '-rdb$field_scale as column_scale, '+
  1246. 'rdb$field_length as column_length, '+
  1247. 'case r.rdb$null_flag when 1 then 0 else 1 end as column_nullable '+
  1248. 'FROM '+
  1249. 'rdb$relation_fields r '+
  1250. 'JOIN rdb$fields f ON r.rdb$field_source=f.rdb$field_name '+
  1251. 'JOIN rdb$types t ON f.rdb$field_type=t.rdb$type AND t.rdb$field_name=''RDB$FIELD_TYPE'' '+
  1252. 'WHERE '+
  1253. '(r.rdb$system_flag = 0 or r.rdb$system_flag is null) and (rdb$relation_name = ''' + Uppercase(SchemaObjectName) + ''') ' +
  1254. 'ORDER BY '+
  1255. 'r.rdb$field_name';
  1256. stSequences : s := 'SELECT ' +
  1257. 'rdb$generator_id as recno,' +
  1258. '''' + DatabaseName + ''' as sequence_catalog,' +
  1259. ''''' as sequence_schema,' +
  1260. 'rdb$generator_name as sequence_name ' +
  1261. 'FROM ' +
  1262. 'rdb$generators ' +
  1263. 'WHERE ' +
  1264. 'rdb$system_flag = 0 or rdb$system_flag is null ' +
  1265. 'ORDER BY ' +
  1266. 'rdb$generator_name';
  1267. else
  1268. DatabaseError(SMetadataUnavailable)
  1269. end; {case}
  1270. result := s;
  1271. end;
  1272. function TIBConnection.GetNextValueSQL(const SequenceName: string; IncrementBy: Integer): string;
  1273. begin
  1274. Result := Format('SELECT gen_id(%s, %d) FROM RDB$DATABASE', [SequenceName, IncrementBy]);
  1275. end;
  1276. procedure TIBConnection.UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string);
  1277. var qry : TSQLQuery;
  1278. begin
  1279. if not assigned(Transaction) then
  1280. DatabaseError(SErrConnTransactionnSet);
  1281. if (length(TableName)>2) and (TableName[1]='"') and (TableName[length(TableName)]='"') then
  1282. TableName := AnsiDequotedStr(TableName, '"')
  1283. else
  1284. TableName := UpperCase(TableName);
  1285. qry := tsqlquery.Create(nil);
  1286. qry.transaction := Transaction;
  1287. qry.database := Self;
  1288. with qry do
  1289. begin
  1290. ReadOnly := True;
  1291. sql.clear;
  1292. sql.add('select '+
  1293. 'ind.rdb$index_name, '+
  1294. 'ind.rdb$relation_name, '+
  1295. 'ind.rdb$unique_flag, '+
  1296. 'ind_seg.rdb$field_name, '+
  1297. 'rel_con.rdb$constraint_type, '+
  1298. 'ind.rdb$index_type '+
  1299. 'from '+
  1300. 'rdb$index_segments ind_seg, '+
  1301. 'rdb$indices ind '+
  1302. 'left outer join '+
  1303. 'rdb$relation_constraints rel_con '+
  1304. 'on '+
  1305. 'rel_con.rdb$index_name = ind.rdb$index_name '+
  1306. 'where '+
  1307. '(ind_seg.rdb$index_name = ind.rdb$index_name) and '+
  1308. '(ind.rdb$relation_name=' + QuotedStr(TableName) + ') '+
  1309. 'order by '+
  1310. 'ind.rdb$index_name;');
  1311. open;
  1312. end;
  1313. while not qry.eof do with IndexDefs.AddIndexDef do
  1314. begin
  1315. Name := trim(qry.fields[0].asstring);
  1316. Fields := trim(qry.Fields[3].asstring);
  1317. If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
  1318. If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
  1319. If qry.fields[5].asInteger = 1 then options:=options+[ixDescending];
  1320. qry.next;
  1321. while (name = trim(qry.fields[0].asstring)) and (not qry.eof) do
  1322. begin
  1323. Fields := Fields + ';' + trim(qry.Fields[3].asstring);
  1324. qry.next;
  1325. end;
  1326. end;
  1327. qry.close;
  1328. qry.free;
  1329. end;
  1330. procedure TIBConnection.SetFloat(CurrBuff: pointer; Dbl: Double; Size: integer);
  1331. var
  1332. Ext : extended;
  1333. Sin : single;
  1334. begin
  1335. case Size of
  1336. 4 :
  1337. begin
  1338. Sin := Dbl;
  1339. Move(Sin, CurrBuff^, 4);
  1340. end;
  1341. 8 :
  1342. begin
  1343. Move(Dbl, CurrBuff^, 8);
  1344. end;
  1345. 10:
  1346. begin
  1347. Ext := Dbl;
  1348. Move(Ext, CurrBuff^, 10);
  1349. end;
  1350. else
  1351. Raise EIBDatabaseError.CreateFmt('Invalid float size for float encode : %d',[Size]);
  1352. end;
  1353. end;
  1354. procedure TIBConnection.GetFloat(CurrBuff, Buffer: pointer; Size: Byte);
  1355. var
  1356. Ext : extended;
  1357. Dbl : double;
  1358. Sin : single;
  1359. begin
  1360. case Size of
  1361. 4 :
  1362. begin
  1363. Move(CurrBuff^, Sin, 4);
  1364. Dbl := Sin;
  1365. end;
  1366. 8 :
  1367. begin
  1368. Move(CurrBuff^, Dbl, 8);
  1369. end;
  1370. 10:
  1371. begin
  1372. Move(CurrBuff^, Ext, 10);
  1373. Dbl := double(Ext);
  1374. end;
  1375. else
  1376. Raise EIBDatabaseError.CreateFmt('Invalid float size for float Decode : %d',[Size]);
  1377. end;
  1378. Move(Dbl, Buffer^, 8);
  1379. end;
  1380. procedure TIBConnection.LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction);
  1381. const
  1382. isc_segstr_eof = 335544367; // It's not defined in ibase60 but in ibase40. Would it be better to define in ibase60?
  1383. var
  1384. blobHandle : Isc_blob_Handle;
  1385. blobSegment : pointer;
  1386. blobSegLen : word;
  1387. TransactionHandle : pointer;
  1388. blobId : PISC_QUAD;
  1389. ptr : Pointer;
  1390. begin
  1391. blobId := PISC_QUAD(@(ABlobBuf^.ConnBlobBuffer));
  1392. TransactionHandle := Atransaction.Handle;
  1393. blobHandle := FB_API_NULLHANDLE;
  1394. if isc_open_blob(@FStatus[0], @FSQLDatabaseHandle, @TransactionHandle, @blobHandle, blobId) <> 0 then
  1395. CheckError('TIBConnection.CreateBlobStream', FStatus);
  1396. //For performance, read as much as we can, regardless of any segment size set in database.
  1397. blobSegment := AllocMem(MAXBLOBSEGMENTSIZE);
  1398. with ABlobBuf^.BlobBuffer^ do
  1399. begin
  1400. Size := 0;
  1401. while (isc_get_segment(@FStatus[0], @blobHandle, @blobSegLen, MAXBLOBSEGMENTSIZE, blobSegment) = 0) do
  1402. begin
  1403. ReAllocMem(Buffer,Size+blobSegLen);
  1404. ptr := Buffer+Size;
  1405. move(blobSegment^,ptr^,blobSegLen);
  1406. inc(Size,blobSegLen);
  1407. end;
  1408. freemem(blobSegment);
  1409. if FStatus[1] = isc_segstr_eof then
  1410. begin
  1411. if isc_close_blob(@FStatus[0], @blobHandle) <> 0 then
  1412. CheckError('TIBConnection.CreateBlobStream isc_close_blob', FStatus);
  1413. end
  1414. else
  1415. CheckError('TIBConnection.CreateBlobStream isc_get_segment', FStatus);
  1416. end;
  1417. end;
  1418. function TIBConnection.RowsAffected(cursor: TSQLCursor): TRowsCount;
  1419. var info_request : string;
  1420. resbuf : array[0..63] of byte;
  1421. i : integer;
  1422. BlockSize,
  1423. subBlockSize : integer;
  1424. SelectedRows,
  1425. InsertedRows : integer;
  1426. begin
  1427. SelectedRows:=-1;
  1428. InsertedRows:=-1;
  1429. if assigned(cursor) then with cursor as TIBCursor do
  1430. if assigned(statement) then
  1431. begin
  1432. info_request := chr(isc_info_sql_records);
  1433. if isc_dsql_sql_info(@Status[0],@Statement,Length(info_request), @info_request[1],sizeof(resbuf),@resbuf) <> 0 then
  1434. CheckError('RowsAffected', Status);
  1435. i := 0;
  1436. while not (byte(resbuf[i]) in [isc_info_end,isc_info_truncated]) do
  1437. begin
  1438. BlockSize:=isc_vax_integer(@resbuf[i+1],2);
  1439. if resbuf[i]=isc_info_sql_records then
  1440. begin
  1441. inc(i,3);
  1442. BlockSize:=BlockSize+i;
  1443. while (resbuf[i] <> isc_info_end) and (i < BlockSize) do
  1444. begin
  1445. subBlockSize:=isc_vax_integer(@resbuf[i+1],2);
  1446. if resbuf[i] = isc_info_req_select_count then
  1447. SelectedRows := isc_vax_integer(@resbuf[i+3],subBlockSize)
  1448. else if resbuf[i] = isc_info_req_insert_count then
  1449. InsertedRows := isc_vax_integer(@resbuf[i+3],subBlockSize);
  1450. inc(i,subBlockSize+3);
  1451. end;
  1452. end
  1453. else
  1454. inc(i,BlockSize+3);
  1455. end;
  1456. end;
  1457. if SelectedRows>0 then result:=SelectedRows
  1458. else Result:=InsertedRows;
  1459. end;
  1460. { TIBConnectionDef }
  1461. class function TIBConnectionDef.TypeName: String;
  1462. begin
  1463. Result:='Firebird';
  1464. end;
  1465. class function TIBConnectionDef.ConnectionClass: TSQLConnectionClass;
  1466. begin
  1467. Result:=TIBConnection;
  1468. end;
  1469. class function TIBConnectionDef.Description: String;
  1470. begin
  1471. Result:='Connect to Firebird/Interbase directly via the client library';
  1472. end;
  1473. class function TIBConnectionDef.DefaultLibraryName: String;
  1474. begin
  1475. {$IFDEF LinkDynamically}
  1476. If UseEmbeddedFirebird then
  1477. Result:=fbembedlib
  1478. else
  1479. Result:=fbclib;
  1480. {$ELSE}
  1481. Result:='';
  1482. {$ENDIF}
  1483. end;
  1484. class function TIBConnectionDef.LoadFunction: TLibraryLoadFunction;
  1485. begin
  1486. {$IFDEF LinkDynamically}
  1487. Result:=@InitialiseIBase60;
  1488. {$ELSE}
  1489. Result:=nil;
  1490. {$ENDIF}
  1491. end;
  1492. class function TIBConnectionDef.UnLoadFunction: TLibraryUnLoadFunction;
  1493. begin
  1494. {$IFDEF LinkDynamically}
  1495. Result:=@ReleaseIBase60
  1496. {$ELSE}
  1497. Result:=nil;
  1498. {$ENDIF}
  1499. end;
  1500. class function TIBConnectionDef.LoadedLibraryName: string;
  1501. begin
  1502. {$IFDEF LinkDynamically}
  1503. Result:=IBaseLoadedLibrary;
  1504. {$ELSE}
  1505. Result:='';
  1506. {$ENDIF}
  1507. end;
  1508. initialization
  1509. RegisterConnection(TIBConnectionDef);
  1510. finalization
  1511. UnRegisterConnection(TIBConnectionDef);
  1512. end.