Skip to content

Commit

Permalink
Issue #1931: retrieve oid of user defined PostgreSQL data type CITEXT…
Browse files Browse the repository at this point in the history
… dynamically, which is different on each server/database.
  • Loading branch information
ansgarbecker committed Apr 24, 2024
1 parent 4ac4152 commit ae2abe2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 25 additions & 1 deletion source/dbconnection.pas
Expand Up @@ -2278,11 +2278,25 @@ function TDBConnection.GetDatatypeByNativeType(NativeType: Integer; Identifier:
i: Integer;
rx: TRegExpr;
TypeFound: Boolean;
TypeOid: String;
begin
rx := TRegExpr.Create;
TypeFound := False;
for i:=0 to High(Datatypes) do begin
if Datatypes[i].NativeTypes = '' then
if Datatypes[i].NativeTypes = '?' then begin
// PG oid is set to be populated via '?'
Datatypes[i].NativeTypes := '';
try
TypeOid := GetVar('SELECT '+EscapeString(Datatypes[i].Name.ToLower)+'::regtype::oid');
if IsNumeric(TypeOid) then begin
Datatypes[i].NativeTypes := TypeOid;
Log(lcInfo, 'Found oid/NativeTypes of '+Datatypes[i].Name+' data type: '+Datatypes[i].NativeTypes);
end;
except
end;
end;
// Skip if native ids / oid's are (still) empty
if Datatypes[i].NativeTypes.IsEmpty then
Continue;
rx.Expression := '\b('+Datatypes[i].NativeTypes+')\b';
if rx.Exec(IntToStr(NativeType)) then begin
Expand All @@ -2291,6 +2305,16 @@ function TDBConnection.GetDatatypeByNativeType(NativeType: Integer; Identifier:
break;
end;
end;

{ Dynamically retrieve data type from pg_type.
Problematic because we would not know which TDBDatatypeIndex to assign.
if (not TypeFound) and Parameters.IsAnyPostgreSQL then begin
PgType := GetResults('SELECT * FROM '+QuoteIdent('pg_type')+' WHERE '+QuoteIdent('oid')+'='+NativeType.ToString);
if PgType.RecordCount = 1 then begin
SetLength(FDatatypes, Length(FDatatypes)+1);
end;
end;}

if not TypeFound then begin
// Fall back to unknown type
Result := Datatypes[0];
Expand Down
2 changes: 1 addition & 1 deletion source/dbstructures.postgresql.pas
Expand Up @@ -236,7 +236,7 @@ TPostgreSQLLib = class(TDbLib)
),
(
Index: dbdtCiText;
NativeTypes: '24651';
NativeTypes: '?';
Name: 'CITEXT';
Names: 'citext';
Description: 'A case-insensitive character string type. Essentially, it internally calls lower when comparing values. Otherwise, it behaves almost exactly like text.';
Expand Down

0 comments on commit ae2abe2

Please sign in to comment.