pqconnection.pp 21 KB

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