pqconnection.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. unit pqconnection;
  2. {$mode objfpc}{$H+}
  3. {$Define LinkDynamically}
  4. interface
  5. uses
  6. Classes, SysUtils, sqldb, db, dbconst,
  7. {$IfDef LinkDynamically}
  8. postgres3dyn;
  9. {$Else}
  10. postgres3;
  11. {$EndIf}
  12. type
  13. TPQTrans = Class(TSQLHandle)
  14. protected
  15. TransactionHandle : PPGConn;
  16. end;
  17. TPQCursor = Class(TSQLCursor)
  18. protected
  19. Statement : string;
  20. tr : Pointer;
  21. nFields : integer;
  22. res : PPGresult;
  23. Nr : string;
  24. end;
  25. { TPQConnection }
  26. TPQConnection = class (TSQLConnection)
  27. private
  28. FCursorCount : word;
  29. FConnectString : string;
  30. FSQLDatabaseHandle : pointer;
  31. function TranslateFldType(Type_Oid : integer) : TFieldType;
  32. protected
  33. procedure DoInternalConnect; override;
  34. procedure DoInternalDisconnect; override;
  35. function GetHandle : pointer; override;
  36. Function AllocateCursorHandle : TSQLCursor; override;
  37. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  38. Function AllocateTransactionHandle : TSQLHandle; override;
  39. procedure PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams); override;
  40. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  41. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
  42. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
  43. function Fetch(cursor : TSQLCursor) : boolean; override;
  44. procedure UnPrepareStatement(cursor : TSQLCursor); override;
  45. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean; override;
  46. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  47. function RollBack(trans : TSQLHandle) : boolean; override;
  48. function Commit(trans : TSQLHandle) : boolean; override;
  49. procedure CommitRetaining(trans : TSQLHandle); override;
  50. function StartdbTransaction(trans : TSQLHandle; AParams : string) : boolean; override;
  51. procedure RollBackRetaining(trans : TSQLHandle); override;
  52. procedure UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string); override;
  53. function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
  54. public
  55. constructor Create(AOwner : TComponent); override;
  56. published
  57. property DatabaseName;
  58. property KeepConnection;
  59. property LoginPrompt;
  60. property Params;
  61. property OnLogin;
  62. end;
  63. implementation
  64. ResourceString
  65. SErrRollbackFailed = 'Rollback transaction failed';
  66. SErrCommitFailed = 'Commit transaction failed';
  67. SErrConnectionFailed = 'Connection to database failed';
  68. SErrTransactionFailed = 'Start of transacion failed';
  69. SErrClearSelection = 'Clear of selection failed';
  70. SErrExecuteFailed = 'Execution of query failed';
  71. SErrFieldDefsFailed = 'Can not extract field information from query';
  72. SErrFetchFailed = 'Fetch of data failed';
  73. SErrPrepareFailed = 'Preparation of query failed.';
  74. const Oid_Bool = 16;
  75. Oid_Text = 25;
  76. Oid_Oid = 26;
  77. Oid_Name = 19;
  78. Oid_Int8 = 20;
  79. Oid_int2 = 21;
  80. Oid_Int4 = 23;
  81. Oid_Float4 = 700;
  82. Oid_Float8 = 701;
  83. Oid_Unknown = 705;
  84. Oid_bpchar = 1042;
  85. Oid_varchar = 1043;
  86. Oid_timestamp = 1114;
  87. oid_date = 1082;
  88. oid_time = 1083;
  89. oid_numeric = 1700;
  90. constructor TPQConnection.Create(AOwner : TComponent);
  91. begin
  92. inherited;
  93. FConnOptions := FConnOptions + [sqSupportParams];
  94. end;
  95. function TPQConnection.GetTransactionHandle(trans : TSQLHandle): pointer;
  96. begin
  97. Result := (trans as TPQtrans).TransactionHandle;
  98. end;
  99. function TPQConnection.RollBack(trans : TSQLHandle) : boolean;
  100. var
  101. res : PPGresult;
  102. tr : TPQTrans;
  103. begin
  104. result := false;
  105. tr := trans as TPQTrans;
  106. res := PQexec(tr.TransactionHandle, 'ROLLBACK');
  107. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  108. begin
  109. PQclear(res);
  110. result := false;
  111. DatabaseError(SErrRollbackFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  112. end
  113. else
  114. begin
  115. PQclear(res);
  116. PQFinish(tr.TransactionHandle);
  117. result := true;
  118. end;
  119. end;
  120. function TPQConnection.Commit(trans : TSQLHandle) : boolean;
  121. var
  122. res : PPGresult;
  123. tr : TPQTrans;
  124. begin
  125. result := false;
  126. tr := trans as TPQTrans;
  127. res := PQexec(tr.TransactionHandle, 'COMMIT');
  128. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  129. begin
  130. PQclear(res);
  131. result := false;
  132. DatabaseError(SErrCommitFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  133. end
  134. else
  135. begin
  136. PQclear(res);
  137. PQFinish(tr.TransactionHandle);
  138. result := true;
  139. end;
  140. end;
  141. function TPQConnection.StartdbTransaction(trans : TSQLHandle; AParams : string) : boolean;
  142. var
  143. res : PPGresult;
  144. tr : TPQTrans;
  145. msg : string;
  146. begin
  147. result := false;
  148. tr := trans as TPQTrans;
  149. tr.TransactionHandle := PQconnectdb(pchar(FConnectString));
  150. if (PQstatus(tr.TransactionHandle) = CONNECTION_BAD) then
  151. begin
  152. result := false;
  153. PQFinish(tr.TransactionHandle);
  154. DatabaseError(SErrConnectionFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  155. end
  156. else
  157. begin
  158. res := PQexec(tr.TransactionHandle, 'BEGIN');
  159. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  160. begin
  161. result := false;
  162. PQclear(res);
  163. msg := PQerrorMessage(tr.transactionhandle);
  164. PQFinish(tr.TransactionHandle);
  165. DatabaseError(sErrTransactionFailed + ' (PostgreSQL: ' + msg + ')',self);
  166. end
  167. else
  168. begin
  169. PQclear(res);
  170. result := true;
  171. end;
  172. end;
  173. end;
  174. procedure TPQConnection.RollBackRetaining(trans : TSQLHandle);
  175. var
  176. res : PPGresult;
  177. tr : TPQTrans;
  178. msg : string;
  179. begin
  180. tr := trans as TPQTrans;
  181. res := PQexec(tr.TransactionHandle, 'ROLLBACK');
  182. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  183. begin
  184. PQclear(res);
  185. DatabaseError(SErrRollbackFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  186. end
  187. else
  188. begin
  189. PQclear(res);
  190. res := PQexec(tr.TransactionHandle, 'BEGIN');
  191. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  192. begin
  193. PQclear(res);
  194. msg := PQerrorMessage(tr.transactionhandle);
  195. PQFinish(tr.TransactionHandle);
  196. DatabaseError(sErrTransactionFailed + ' (PostgreSQL: ' + msg + ')',self);
  197. end
  198. else
  199. PQclear(res);
  200. end;
  201. end;
  202. procedure TPQConnection.CommitRetaining(trans : TSQLHandle);
  203. var
  204. res : PPGresult;
  205. tr : TPQTrans;
  206. msg : string;
  207. begin
  208. tr := trans as TPQTrans;
  209. res := PQexec(tr.TransactionHandle, 'COMMIT');
  210. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  211. begin
  212. PQclear(res);
  213. DatabaseError(SErrCommitFailed + ' (PostgreSQL: ' + PQerrorMessage(tr.transactionhandle) + ')',self);
  214. end
  215. else
  216. begin
  217. PQclear(res);
  218. res := PQexec(tr.TransactionHandle, 'BEGIN');
  219. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  220. begin
  221. PQclear(res);
  222. msg := PQerrorMessage(tr.transactionhandle);
  223. PQFinish(tr.TransactionHandle);
  224. DatabaseError(sErrTransactionFailed + ' (PostgreSQL: ' + msg + ')',self);
  225. end
  226. else
  227. PQclear(res);
  228. end;
  229. end;
  230. procedure TPQConnection.DoInternalConnect;
  231. var msg : string;
  232. begin
  233. {$IfDef LinkDynamically}
  234. InitialisePostgres3;
  235. {$EndIf}
  236. inherited dointernalconnect;
  237. FConnectString := '';
  238. if (UserName <> '') then FConnectString := FConnectString + ' user=''' + UserName + '''';
  239. if (Password <> '') then FConnectString := FConnectString + ' password=''' + Password + '''';
  240. if (HostName <> '') then FConnectString := FConnectString + ' host=''' + HostName + '''';
  241. if (DatabaseName <> '') then FConnectString := FConnectString + ' dbname=''' + DatabaseName + '''';
  242. if (Params.Text <> '') then FConnectString := FConnectString + ' '+Params.Text;
  243. FSQLDatabaseHandle := PQconnectdb(pchar(FConnectString));
  244. if (PQstatus(FSQLDatabaseHandle) = CONNECTION_BAD) then
  245. begin
  246. msg := PQerrorMessage(FSQLDatabaseHandle);
  247. dointernaldisconnect;
  248. DatabaseError(sErrConnectionFailed + ' (PostgreSQL: ' + msg + ')',self);
  249. end;
  250. end;
  251. procedure TPQConnection.DoInternalDisconnect;
  252. begin
  253. PQfinish(FSQLDatabaseHandle);
  254. {$IfDef LinkDynamically}
  255. ReleasePostgres3;
  256. {$EndIf}
  257. end;
  258. function TPQConnection.TranslateFldType(Type_Oid : integer) : TFieldType;
  259. begin
  260. case Type_Oid of
  261. Oid_varchar,Oid_bpchar,
  262. Oid_name : Result := ftstring;
  263. Oid_text : Result := ftstring;
  264. Oid_oid : Result := ftInteger;
  265. Oid_int8 : Result := ftLargeInt;
  266. Oid_int4 : Result := ftInteger;
  267. Oid_int2 : Result := ftSmallInt;
  268. Oid_Float4 : Result := ftFloat;
  269. Oid_Float8 : Result := ftFloat;
  270. Oid_TimeStamp : Result := ftDateTime;
  271. Oid_Date : Result := ftDate;
  272. Oid_Time : Result := ftTime;
  273. Oid_Bool : Result := ftBoolean;
  274. Oid_Numeric : Result := ftBCD;
  275. Oid_Unknown : Result := ftUnknown;
  276. else
  277. Result := ftUnknown;
  278. end;
  279. end;
  280. Function TPQConnection.AllocateCursorHandle : TSQLCursor;
  281. begin
  282. result := TPQCursor.create;
  283. end;
  284. Procedure TPQConnection.DeAllocateCursorHandle(var cursor : TSQLCursor);
  285. begin
  286. FreeAndNil(cursor);
  287. end;
  288. Function TPQConnection.AllocateTransactionHandle : TSQLHandle;
  289. begin
  290. result := TPQTrans.create;
  291. end;
  292. procedure TPQConnection.PrepareStatement(cursor: TSQLCursor;ATransaction : TSQLTransaction;buf : string; AParams : TParams);
  293. const TypeStrings : array[TFieldType] of string =
  294. (
  295. 'Unknown',
  296. 'text',
  297. 'int',
  298. 'int',
  299. 'int',
  300. 'bool',
  301. 'float',
  302. 'numeric',
  303. 'numeric',
  304. 'date',
  305. 'time',
  306. 'timestamp',
  307. 'Unknown',
  308. 'Unknown',
  309. 'Unknown',
  310. 'Unknown',
  311. 'Unknown',
  312. 'Unknown',
  313. 'Unknown',
  314. 'Unknown',
  315. 'Unknown',
  316. 'Unknown',
  317. 'Unknown',
  318. 'Unknown',
  319. 'Unknown',
  320. 'int',
  321. 'Unknown',
  322. 'Unknown',
  323. 'Unknown',
  324. 'Unknown',
  325. 'Unknown',
  326. 'Unknown',
  327. 'Unknown',
  328. 'Unknown',
  329. 'Unknown',
  330. 'Unknown',
  331. 'Unknown',
  332. 'Unknown'
  333. );
  334. var s : string;
  335. i : integer;
  336. begin
  337. with (cursor as TPQCursor) do
  338. begin
  339. FPrepared := False;
  340. nr := inttostr(FCursorcount);
  341. inc(FCursorCount);
  342. // Prior to v8 there is no support for cursors and parameters.
  343. // So that's not supported.
  344. if FStatementType = stselect then
  345. statement := 'DECLARE slctst' + name + nr +' BINARY CURSOR FOR ' + buf
  346. else if FStatementType in [stInsert,stUpdate,stDelete] then
  347. begin
  348. tr := aTransaction.Handle;
  349. // Only available for pq 8.0, so don't use it...
  350. // Res := pqprepare(tr,'prepst'+name+nr,pchar(buf),params.Count,pchar(''));
  351. s := 'prepare prepst'+nr+' ';
  352. if Assigned(AParams) and (AParams.count > 0) then
  353. begin
  354. s := s + '(';
  355. for i := 0 to AParams.count-1 do
  356. s := s + TypeStrings[AParams[i].DataType] + ',';
  357. s[length(s)] := ')';
  358. buf := AParams.ParseSQL(buf,false,psPostgreSQL);
  359. end;
  360. s := s + ' as ' + buf;
  361. res := pqexec(tr,pchar(s));
  362. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  363. begin
  364. pqclear(res);
  365. DatabaseError(SErrPrepareFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  366. end;
  367. FPrepared := True;
  368. end
  369. else
  370. statement := buf;
  371. end;
  372. end;
  373. procedure TPQConnection.UnPrepareStatement(cursor : TSQLCursor);
  374. begin
  375. with (cursor as TPQCursor) do if FPrepared then
  376. begin
  377. res := pqexec(tr,pchar('deallocate prepst'+nr));
  378. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  379. begin
  380. pqclear(res);
  381. DatabaseError(SErrPrepareFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  382. end;
  383. pqclear(res);
  384. FPrepared := False;
  385. end;
  386. end;
  387. procedure TPQConnection.FreeFldBuffers(cursor : TSQLCursor);
  388. begin
  389. with cursor as TPQCursor do
  390. if (PQresultStatus(res) <> PGRES_FATAL_ERROR) then //Don't try to do anything if the transaction has already encountered an error.
  391. begin
  392. if FStatementType = stselect then
  393. begin
  394. Res := pqexec(tr,pchar('CLOSE slctst' + name + nr));
  395. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  396. begin
  397. pqclear(res);
  398. DatabaseError(SErrClearSelection + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  399. end
  400. end;
  401. pqclear(res);
  402. end;
  403. end;
  404. procedure TPQConnection.Execute(cursor: TSQLCursor;atransaction:tSQLtransaction;AParams : TParams);
  405. var ar : array of pointer;
  406. i : integer;
  407. s : string;
  408. begin
  409. with cursor as TPQCursor do
  410. begin
  411. if FStatementType in [stInsert,stUpdate,stDelete] then
  412. begin
  413. if Assigned(AParams) and (AParams.count > 0) then
  414. begin
  415. setlength(ar,Aparams.count);
  416. for i := 0 to AParams.count -1 do
  417. begin
  418. case AParams[i].DataType of
  419. ftdatetime : s := formatdatetime('YYYY-MM-DD',AParams[i].AsDateTime);
  420. else
  421. s := AParams[i].asstring;
  422. end; {case}
  423. GetMem(ar[i],length(s)+1);
  424. StrMove(PChar(ar[i]),Pchar(s),Length(S)+1);
  425. end;
  426. res := PQexecPrepared(tr,pchar('prepst'+nr),Aparams.count,@Ar[0],nil,nil,0);
  427. for i := 0 to AParams.count -1 do
  428. FreeMem(ar[i]);
  429. end
  430. else
  431. res := PQexecPrepared(tr,pchar('prepst'+nr),0,nil,nil,nil,0);
  432. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  433. begin
  434. pqclear(res);
  435. DatabaseError(SErrExecuteFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self);
  436. end;
  437. end
  438. else
  439. begin
  440. tr := aTransaction.Handle;
  441. s := statement;
  442. //Should be altered, just like in TSQLQuery.ApplyRecUpdate
  443. if assigned(AParams) then for i := 0 to AParams.count-1 do
  444. s := stringreplace(s,':'+AParams[i].Name,AParams[i].asstring,[rfReplaceAll,rfIgnoreCase]);
  445. res := pqexec(tr,pchar(s));
  446. if (PQresultStatus(res) <> PGRES_COMMAND_OK) then
  447. begin
  448. pqclear(res);
  449. DatabaseError(SErrExecuteFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self);
  450. end;
  451. end;
  452. end;
  453. end;
  454. procedure TPQConnection.AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs);
  455. var
  456. i : integer;
  457. size : integer;
  458. st : string;
  459. fieldtype : tfieldtype;
  460. BaseRes : PPGresult;
  461. begin
  462. with cursor as TPQCursor do
  463. begin
  464. // BaseRes := pqexecParams(tr,'FETCH 0 IN selectst' + pchar(name) ,0,nil,nil,nil,nil,1);
  465. st := pchar('FETCH 0 IN slctst' + name+nr);
  466. BaseRes := pqexec(tr,pchar(st));
  467. if (PQresultStatus(BaseRes) <> PGRES_TUPLES_OK) then
  468. begin
  469. pqclear(BaseRes);
  470. DatabaseError(SErrFieldDefsFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  471. end;
  472. nFields := PQnfields(BaseRes);
  473. for i := 0 to nFields-1 do
  474. begin
  475. size := PQfsize(BaseRes, i);
  476. fieldtype := TranslateFldType(PQftype(BaseRes, i));
  477. if (fieldtype = ftstring) and (size = -1) then
  478. begin
  479. size := pqfmod(baseres,i)-3;
  480. if size = -4 then size := dsMaxStringSize;
  481. end;
  482. if fieldtype = ftdate then
  483. size := sizeof(double);
  484. TFieldDef.Create(FieldDefs, PQfname(BaseRes, i), fieldtype,size, False, (i + 1));
  485. end;
  486. pqclear(baseres);
  487. end;
  488. end;
  489. function TPQConnection.GetHandle: pointer;
  490. begin
  491. Result := FSQLDatabaseHandle;
  492. end;
  493. function TPQConnection.Fetch(cursor : TSQLCursor) : boolean;
  494. var st : string;
  495. begin
  496. with cursor as TPQCursor do
  497. begin
  498. st := pchar('FETCH NEXT IN slctst' + name+nr);
  499. Res := pqexec(tr,pchar(st));
  500. if (PQresultStatus(res) <> PGRES_TUPLES_OK) then
  501. begin
  502. pqclear(Res);
  503. DatabaseError(SErrfetchFailed + ' (PostgreSQL: ' + PQerrorMessage(tr) + ')',self)
  504. end;
  505. Result := (PQntuples(res)<>0);
  506. end;
  507. end;
  508. function TPQConnection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer) : boolean;
  509. var
  510. x,i : integer;
  511. li : Longint;
  512. CurrBuff : pchar;
  513. tel : byte;
  514. dbl : pdouble;
  515. begin
  516. with cursor as TPQCursor do
  517. begin
  518. for x := 0 to PQnfields(res)-1 do
  519. if PQfname(Res, x) = FieldDef.Name then break;
  520. if PQfname(Res, x) <> FieldDef.Name then
  521. DatabaseErrorFmt(SFieldNotFound,[FieldDef.Name],self);
  522. if pqgetisnull(res,0,x)=1 then
  523. result := false
  524. else
  525. begin
  526. i := PQfsize(res, x);
  527. CurrBuff := pqgetvalue(res,0,x);
  528. case FieldDef.DataType of
  529. ftInteger, ftSmallint, ftLargeInt,ftfloat :
  530. begin
  531. for tel := 1 to i do // postgres returns big-endian numbers
  532. pchar(Buffer)[tel-1] := CurrBuff[i-tel];
  533. end;
  534. ftString :
  535. begin
  536. li := pqgetlength(res,0,x);
  537. Move(CurrBuff^, Buffer^, li);
  538. pchar(Buffer + li)^ := #0;
  539. i := pqfmod(res,x)-3;
  540. end;
  541. ftdate :
  542. begin
  543. li := 0;
  544. for tel := 1 to i do // postgres returns big-endian numbers
  545. pchar(@li)[tel-1] := CurrBuff[i-tel];
  546. // double(buffer^) := x + 36526; This doesn't work, please tell me what is wrong with it?
  547. dbl := pointer(buffer);
  548. dbl^ := li + 36526;
  549. i := sizeof(double);
  550. end;
  551. ftDateTime, fttime :
  552. begin
  553. dbl := pointer(buffer);
  554. dbl^ := 0;
  555. for tel := 1 to i do // postgres returns big-endian numbers
  556. pchar(Buffer)[tel-1] := CurrBuff[i-tel];
  557. dbl^ := (dbl^+3.1558464E+009)/86400; // postgres counts seconds elapsed since 1-1-2000
  558. end;
  559. ftBCD:
  560. begin
  561. // not implemented
  562. end;
  563. ftBoolean:
  564. pchar(buffer)[0] := CurrBuff[0]
  565. end;
  566. result := true;
  567. end;
  568. end;
  569. end;
  570. procedure TPQConnection.UpdateIndexDefs(var IndexDefs : TIndexDefs;TableName : string);
  571. var qry : TSQLQuery;
  572. begin
  573. if not assigned(Transaction) then
  574. DatabaseError(SErrConnTransactionnSet);
  575. qry := tsqlquery.Create(nil);
  576. qry.transaction := Transaction;
  577. qry.database := Self;
  578. with qry do
  579. begin
  580. ReadOnly := True;
  581. sql.clear;
  582. sql.add('select '+
  583. 'ic.relname as indexname, '+
  584. 'tc.relname as tablename, '+
  585. 'ia.attname, '+
  586. 'i.indisprimary, '+
  587. 'i.indisunique '+
  588. 'from '+
  589. 'pg_attribute ta, '+
  590. 'pg_attribute ia, '+
  591. 'pg_class tc, '+
  592. 'pg_class ic, '+
  593. 'pg_index i '+
  594. 'where '+
  595. '(i.indrelid = tc.oid) and '+
  596. '(ta.attrelid = tc.oid) and '+
  597. '(ia.attrelid = i.indexrelid) and '+
  598. '(ic.oid = i.indexrelid) and '+
  599. '(ta.attnum = i.indkey[ia.attnum-1]) and '+
  600. '(upper(tc.relname)=''' + UpperCase(TableName) +''') '+
  601. 'order by '+
  602. 'ic.relname;');
  603. open;
  604. end;
  605. while not qry.eof do with IndexDefs.AddIndexDef do
  606. begin
  607. Name := trim(qry.fields[0].asstring);
  608. Fields := trim(qry.Fields[2].asstring);
  609. If qry.fields[3].asboolean then options := options + [ixPrimary];
  610. If qry.fields[4].asboolean then options := options + [ixUnique];
  611. qry.next;
  612. while (name = qry.fields[0].asstring) and (not qry.eof) do
  613. begin
  614. Fields := Fields + ';' + trim(qry.Fields[2].asstring);
  615. qry.next;
  616. end;
  617. end;
  618. qry.close;
  619. qry.free;
  620. end;
  621. function TPQConnection.GetSchemaInfoSQL(SchemaType: TSchemaType;
  622. SchemaObjectName, SchemaPattern: string): string;
  623. var s : string;
  624. begin
  625. case SchemaType of
  626. stTables : s := 'select '+
  627. 'relfilenode as recno, '+
  628. '''' + DatabaseName + ''' as catalog_name, '+
  629. ''''' as schema_name, '+
  630. 'relname as table_name, '+
  631. '0 as table_type '+
  632. 'from '+
  633. 'pg_class '+
  634. 'where '+
  635. '(relowner > 1) and relkind=''r''' +
  636. 'order by relname';
  637. stSysTables : s := 'select '+
  638. 'relfilenode as recno, '+
  639. '''' + DatabaseName + ''' as catalog_name, '+
  640. ''''' as schema_name, '+
  641. 'relname as table_name, '+
  642. '0 as table_type '+
  643. 'from '+
  644. 'pg_class '+
  645. 'where '+
  646. 'relkind=''r''' +
  647. 'order by relname';
  648. else
  649. DatabaseError(SMetadataUnavailable)
  650. end; {case}
  651. result := s;
  652. end;
  653. end.