pqconnection.pp 21 KB

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