[bug_fixed] Bug in ZDbcPostgreSqlMetadata.pas(6.6 beta)
Moderators: EgonHugeist, mdaems
-
- Expert Boarder
- Posts: 158
- Joined: 06.11.2005, 01:43
[bug_fixed] Bug in ZDbcPostgreSqlMetadata.pas(6.6 beta)
Hi
TZSQLMetadata is not returning the list of tables for PostgreSQL. The problem is in the following method. Here is my fix for this. I have tested it and it is working ok for me.
[syntax="delphi"]function TZPostgreSQLDatabaseMetadata.GetTables(const Catalog: string;
const SchemaPattern: string; const TableNamePattern: string;
const Types: TStringDynArray): IZResultSet;
var
I: Integer;
Key: string;
TableType, OrderBy, SQL: string;
UseSchemas: Boolean;
LTypes: TStringDynArray;
begin
Key := GetTablesMetaDataCacheKey(Catalog,SchemaPattern,TableNamePattern,Types);
Result := GetResultSetFromCache(Key);
if Result = nil then
begin
UseSchemas := True;
if HaveMinimumServerVersion(7, 3) then
begin
SQL := ' SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM,'
+ ' c.relname AS TABLE_NAME, '
+ ' CASE (n.nspname LIKE ''pg\\_%'')'
+ ' OR (n.nspname=''information_schema'')'
+ ' WHEN true THEN CASE n.nspname '
+ ' WHEN ''pg_catalog'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'''
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN ''information_schema'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'''
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN ''pg_toast'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TOAST TABLE'' '
+ ' WHEN ''i'' THEN ''SYSTEM TOAST INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE CASE c.relkind '
+ ' WHEN ''r'' THEN ''TEMPORARY TABLE'' '
+ ' WHEN ''i'' THEN ''TEMPORARY INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' END '
+ ' WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TABLE'' '
+ ' WHEN ''i'' THEN ''INDEX'' '
+ ' WHEN ''S'' THEN ''SEQUENCE'' '
+ ' WHEN ''v'' THEN ''VIEW'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE NULL '
+ ' END '
+ ' AS TABLE_TYPE, d.description AS REMARKS '
+ ' FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c '
+ ' LEFT JOIN pg_catalog.pg_description d'
+ ' ON (c.oid = d.objoid AND d.objsubid = 0) '
+ ' LEFT JOIN pg_catalog.pg_class dc ON (d.classoid=dc.oid'
+ ' AND dc.relname=''pg_class'') LEFT JOIN pg_catalog.pg_namespace dn'
+ ' ON (dn.oid=dc.relnamespace AND dn.nspname=''pg_catalog'') '
+ ' WHERE c.relnamespace = n.oid ';
if SchemaPattern <> '' then // cannot happen due to SchemaPattern := '%'
begin
SQL := SQL + ' AND n.nspname LIKE '''
+ EscapeQuotes(ToLikeString(SchemaPattern)) + ''' ';
end;
OrderBy := ' ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME ';
end
else
begin
UseSchemas := False;
TableType := ' CASE c.relname LIKE ''pg\\_%'' '
+ 'WHEN true THEN CASE c.relname LIKE ''pg\\_toast\\_%'' '
+ 'WHEN true THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TOAST TABLE'' '
+ ' WHEN ''i'' THEN ''SYSTEM TOAST INDEX'' '
+ ' ELSE NULL '
+ 'END '
+ 'WHEN false THEN CASE c.relname LIKE ''pg\\_temp\\_%'' '
+ ' WHEN true THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TEMPORARY TABLE'' '
+ ' WHEN ''i'' THEN ''TEMPORARY INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'' '
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE NULL '
+ 'END '
+ 'ELSE NULL '
+ 'END '
+ 'WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TABLE'' '
+ ' WHEN ''i'' THEN ''INDEX'' '
+ ' WHEN ''S'' THEN ''SEQUENCE'' '
+ ' WHEN ''v'' THEN ''VIEW'' '
+ ' ELSE NULL '
+ 'END '
+ 'ELSE NULL '
+ ' END ';
OrderBy := ' ORDER BY TABLE_TYPE,TABLE_NAME ';
SQL := 'SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM,'
+ ' c.relname AS TABLE_NAME, ' + TableType + ' AS TABLE_TYPE,'
+ ' NULL AS REMARKS FROM pg_class c WHERE true ';
end;
if (Types = nil) or (High(Types) = 0) then
begin
SetLength(LTypes, 5);
LTypes[0] := 'TABLE';
LTypes[1] := 'VIEW';
LTypes[2] := 'INDEX';
LTypes[3] := 'SEQUENCE';
LTypes[4] := 'TEMPORARY TABLE';
end
else
LTypes := Types;
SQL := SQL + ' AND c.relname LIKE ''' + EscapeQuotes(ToLikeString(TableNamePattern))
+ ''' ';
//SC 21/10/2006
if (Length(Types) > 0) then
SQL := SQL + 'AND (false ';
//SC 21/10/2006
for I := 0 to High(Types) do
SQL := SQL + ' OR ( ' + TableTypeSQLExpression(LTypes, UseSchemas) + ' ) ';
//SC 21/10/2006
if (Length(Types) > 0) then
SQL := SQL + ' )';
//SC 21/10/2006
SQL := SQL + OrderBy;
Result := CopyToVirtualResultSet(
GetConnection.CreateStatement.ExecuteQuery(SQL),
ConstructVirtualResultSet(TableColumnsDynArray));
AddResultSetToCache(Key, Result);
end;
end;[/syntax]
Regards
Sandeep
[edit]Sourcecode formatted to make it readable [/edit]
TZSQLMetadata is not returning the list of tables for PostgreSQL. The problem is in the following method. Here is my fix for this. I have tested it and it is working ok for me.
[syntax="delphi"]function TZPostgreSQLDatabaseMetadata.GetTables(const Catalog: string;
const SchemaPattern: string; const TableNamePattern: string;
const Types: TStringDynArray): IZResultSet;
var
I: Integer;
Key: string;
TableType, OrderBy, SQL: string;
UseSchemas: Boolean;
LTypes: TStringDynArray;
begin
Key := GetTablesMetaDataCacheKey(Catalog,SchemaPattern,TableNamePattern,Types);
Result := GetResultSetFromCache(Key);
if Result = nil then
begin
UseSchemas := True;
if HaveMinimumServerVersion(7, 3) then
begin
SQL := ' SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM,'
+ ' c.relname AS TABLE_NAME, '
+ ' CASE (n.nspname LIKE ''pg\\_%'')'
+ ' OR (n.nspname=''information_schema'')'
+ ' WHEN true THEN CASE n.nspname '
+ ' WHEN ''pg_catalog'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'''
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN ''information_schema'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'''
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN ''pg_toast'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TOAST TABLE'' '
+ ' WHEN ''i'' THEN ''SYSTEM TOAST INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE CASE c.relkind '
+ ' WHEN ''r'' THEN ''TEMPORARY TABLE'' '
+ ' WHEN ''i'' THEN ''TEMPORARY INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' END '
+ ' WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TABLE'' '
+ ' WHEN ''i'' THEN ''INDEX'' '
+ ' WHEN ''S'' THEN ''SEQUENCE'' '
+ ' WHEN ''v'' THEN ''VIEW'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE NULL '
+ ' END '
+ ' AS TABLE_TYPE, d.description AS REMARKS '
+ ' FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c '
+ ' LEFT JOIN pg_catalog.pg_description d'
+ ' ON (c.oid = d.objoid AND d.objsubid = 0) '
+ ' LEFT JOIN pg_catalog.pg_class dc ON (d.classoid=dc.oid'
+ ' AND dc.relname=''pg_class'') LEFT JOIN pg_catalog.pg_namespace dn'
+ ' ON (dn.oid=dc.relnamespace AND dn.nspname=''pg_catalog'') '
+ ' WHERE c.relnamespace = n.oid ';
if SchemaPattern <> '' then // cannot happen due to SchemaPattern := '%'
begin
SQL := SQL + ' AND n.nspname LIKE '''
+ EscapeQuotes(ToLikeString(SchemaPattern)) + ''' ';
end;
OrderBy := ' ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME ';
end
else
begin
UseSchemas := False;
TableType := ' CASE c.relname LIKE ''pg\\_%'' '
+ 'WHEN true THEN CASE c.relname LIKE ''pg\\_toast\\_%'' '
+ 'WHEN true THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TOAST TABLE'' '
+ ' WHEN ''i'' THEN ''SYSTEM TOAST INDEX'' '
+ ' ELSE NULL '
+ 'END '
+ 'WHEN false THEN CASE c.relname LIKE ''pg\\_temp\\_%'' '
+ ' WHEN true THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TEMPORARY TABLE'' '
+ ' WHEN ''i'' THEN ''TEMPORARY INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'' '
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE NULL '
+ 'END '
+ 'ELSE NULL '
+ 'END '
+ 'WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TABLE'' '
+ ' WHEN ''i'' THEN ''INDEX'' '
+ ' WHEN ''S'' THEN ''SEQUENCE'' '
+ ' WHEN ''v'' THEN ''VIEW'' '
+ ' ELSE NULL '
+ 'END '
+ 'ELSE NULL '
+ ' END ';
OrderBy := ' ORDER BY TABLE_TYPE,TABLE_NAME ';
SQL := 'SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM,'
+ ' c.relname AS TABLE_NAME, ' + TableType + ' AS TABLE_TYPE,'
+ ' NULL AS REMARKS FROM pg_class c WHERE true ';
end;
if (Types = nil) or (High(Types) = 0) then
begin
SetLength(LTypes, 5);
LTypes[0] := 'TABLE';
LTypes[1] := 'VIEW';
LTypes[2] := 'INDEX';
LTypes[3] := 'SEQUENCE';
LTypes[4] := 'TEMPORARY TABLE';
end
else
LTypes := Types;
SQL := SQL + ' AND c.relname LIKE ''' + EscapeQuotes(ToLikeString(TableNamePattern))
+ ''' ';
//SC 21/10/2006
if (Length(Types) > 0) then
SQL := SQL + 'AND (false ';
//SC 21/10/2006
for I := 0 to High(Types) do
SQL := SQL + ' OR ( ' + TableTypeSQLExpression(LTypes, UseSchemas) + ' ) ';
//SC 21/10/2006
if (Length(Types) > 0) then
SQL := SQL + ' )';
//SC 21/10/2006
SQL := SQL + OrderBy;
Result := CopyToVirtualResultSet(
GetConnection.CreateStatement.ExecuteQuery(SQL),
ConstructVirtualResultSet(TableColumnsDynArray));
AddResultSetToCache(Key, Result);
end;
end;[/syntax]
Regards
Sandeep
[edit]Sourcecode formatted to make it readable [/edit]
-
- Expert Boarder
- Posts: 158
- Joined: 06.11.2005, 01:43
-
- Expert Boarder
- Posts: 158
- Joined: 06.11.2005, 01:43
- mdaems
- Zeos Project Manager
- Posts: 2766
- Joined: 20.09.2005, 15:28
- Location: Brussels, Belgium
- Contact:
Hi Sandeep,
I hoped the editor of your post would have done this, but apparently not. I just added your 4 lines of code to the SVN Testing branch. (Rev 162)
Please check and confirm. So I can close this topic.
In about a week your change will be merged to Trunk and it will be included in next release build.
Mark
I hoped the editor of your post would have done this, but apparently not. I just added your 4 lines of code to the SVN Testing branch. (Rev 162)
Please check and confirm. So I can close this topic.
In about a week your change will be merged to Trunk and it will be included in next release build.
Mark
hi , i using Rev 182
there is a bug here
the fix from sandeep_c24 doesn't work for me
the real problem is
delphi:
the working code
delphi:
there is a bug here
Code: Select all
if (Length(LTypes) > 0) then
SQL := SQL + 'AND (false ';
for I := 0 to High(Types) do
SQL := SQL + ' OR ( ' + TableTypeSQLExpression(LTypes[i], UseSchemas) + ' ) ';
SQL := SQL + ' )' + OrderBy;
if (Length(LTypes) > 0) then
SQL := SQL + ' )';
the real problem is
delphi:
Code: Select all
for I := 0 to High(Types) do // must be LTypes
delphi:
Code: Select all
function TZPostgreSQLDatabaseMetadata.GetTables(const Catalog: string;
const SchemaPattern: string; const TableNamePattern: string;
const Types: TStringDynArray): IZResultSet;
var
I: Integer;
Key: string;
TableType, OrderBy, SQL: string;
UseSchemas: Boolean;
LTypes: TStringDynArray;
begin
Key := GetTablesMetaDataCacheKey(Catalog,SchemaPattern,TableNamePattern,Types);
Result := GetResultSetFromCache(Key);
if Result = nil then
begin
UseSchemas := True;
if HaveMinimumServerVersion(7, 3) then
begin
SQL := ' SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM,'
+ ' c.relname AS TABLE_NAME, '
+ ' CASE (n.nspname LIKE ''pg\\_%'')'
+ ' OR (n.nspname=''information_schema'')'
+ ' WHEN true THEN CASE n.nspname '
+ ' WHEN ''pg_catalog'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'''
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN ''information_schema'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'''
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN ''pg_toast'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TOAST TABLE'' '
+ ' WHEN ''i'' THEN ''SYSTEM TOAST INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE CASE c.relkind '
+ ' WHEN ''r'' THEN ''TEMPORARY TABLE'' '
+ ' WHEN ''i'' THEN ''TEMPORARY INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' END '
+ ' WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TABLE'' '
+ ' WHEN ''i'' THEN ''INDEX'' '
+ ' WHEN ''S'' THEN ''SEQUENCE'' '
+ ' WHEN ''v'' THEN ''VIEW'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE NULL '
+ ' END '
+ ' AS TABLE_TYPE, d.description AS REMARKS '
+ ' FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c '
+ ' LEFT JOIN pg_catalog.pg_description d'
+ ' ON (c.oid = d.objoid AND d.objsubid = 0) '
+ ' LEFT JOIN pg_catalog.pg_class dc ON (d.classoid=dc.oid'
+ ' AND dc.relname=''pg_class'') LEFT JOIN pg_catalog.pg_namespace dn'
+ ' ON (dn.oid=dc.relnamespace AND dn.nspname=''pg_catalog'') '
+ ' WHERE c.relnamespace = n.oid ';
// if SchemaPattern <> '' then // cannot happen due to SchemaPattern := '%'
begin
SQL := SQL + ' AND n.nspname LIKE '''
+ EscapeQuotes(ToLikeString(SchemaPattern)) + ''' ';
end;
OrderBy := ' ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME ';
end
else
begin
UseSchemas := False;
TableType := ' CASE c.relname LIKE ''pg\\_%'' '
+ 'WHEN true THEN CASE c.relname LIKE ''pg\\_toast\\_%'' '
+ 'WHEN true THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TOAST TABLE'' '
+ ' WHEN ''i'' THEN ''SYSTEM TOAST INDEX'' '
+ ' ELSE NULL '
+ 'END '
+ 'WHEN false THEN CASE c.relname LIKE ''pg\\_temp\\_%'' '
+ ' WHEN true THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TEMPORARY TABLE'' '
+ ' WHEN ''i'' THEN ''TEMPORARY INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'' '
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE NULL '
+ 'END '
+ 'ELSE NULL '
+ 'END '
+ 'WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TABLE'' '
+ ' WHEN ''i'' THEN ''INDEX'' '
+ ' WHEN ''S'' THEN ''SEQUENCE'' '
+ ' WHEN ''v'' THEN ''VIEW'' '
+ ' ELSE NULL '
+ 'END '
+ 'ELSE NULL '
+ ' END ';
OrderBy := ' ORDER BY TABLE_TYPE,TABLE_NAME ';
SQL := 'SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM,'
+ ' c.relname AS TABLE_NAME, ' + TableType + ' AS TABLE_TYPE,'
+ ' NULL AS REMARKS FROM pg_class c WHERE true ';
end;
if (Types = nil) or (High(Types) = 0) then
begin
SetLength(LTypes, 5);
LTypes[0] := 'TABLE';
LTypes[1] := 'VIEW';
LTypes[2] := 'INDEX';
LTypes[3] := 'SEQUENCE';
LTypes[4] := 'TEMPORARY TABLE';
end
else
LTypes := Types;
SQL := SQL + ' AND c.relname LIKE ''' + EscapeQuotes(ToLikeString(TableNamePattern))
+ ''' AND (false ';
// if (Length(LTypes) > 0) then
// SQL := SQL + 'AND (false ';
for I := 0 to High(LTypes) do
SQL := SQL + ' OR ( ' + TableTypeSQLExpression(LTypes[i], UseSchemas) + ' ) ';
SQL := SQL + ' )' + OrderBy;
// if (Length(LTypes) > 0) then
// SQL := SQL + ' )';
Result := CopyToVirtualResultSet(
GetConnection.CreateStatement.ExecuteQuery(SQL),
ConstructVirtualResultSet(TableColumnsDynArray));
AddResultSetToCache(Key, Result);
end;
end;
- mdaems
- Zeos Project Manager
- Posts: 2766
- Joined: 20.09.2005, 15:28
- Location: Brussels, Belgium
- Contact:
Merci Damien,
This fix looks logical. I've commited it to SVN (Testing Rev 188).
@Sandeep : can you please check this solution? I want this reportto be closed.
Mark
This fix looks logical. I've commited it to SVN (Testing Rev 188).
@Sandeep : can you please check this solution? I want this reportto be closed.
Code: Select all
SQL := SQL + ' AND c.relname LIKE ''' + EscapeQuotes(ToLikeString(TableNamePattern))
+ ''' AND (false ';
for I := 0 to High(LTypes) do
SQL := SQL + ' OR ( ' + TableTypeSQLExpression(LTypes[i], UseSchemas) + ' ) ';
SQL := SQL + ' )' + OrderBy;
-
- Expert Boarder
- Posts: 158
- Joined: 06.11.2005, 01:43
Hi Mdaems
I had a look at the fix provided by Merci Damien. I agree with Damien that LTypes should have been used and I think it's ok to use LTypes there.
The only thing wrong in following code...
...is the use of 'AND (false '. If Length of LTypes is 0 then the sql statement ends up looking like
which doesn't fetch anything.
The proper fix I think would be as follows
Regards
Sandeep
I had a look at the fix provided by Merci Damien. I agree with Damien that LTypes should have been used and I think it's ok to use LTypes there.
The only thing wrong in following code...
Code: Select all
SQL := SQL + ' AND c.relname LIKE ''' + EscapeQuotes(ToLikeString(TableNamePattern))
+ ''' AND (false ';
for I := 0 to High(LTypes) do
SQL := SQL + ' OR ( ' + TableTypeSQLExpression(LTypes[i], UseSchemas) + ' ) ';
SQL := SQL + ' )' + OrderBy;
Code: Select all
select [something] from [table] where [condition] AND (false)
The proper fix I think would be as follows
Code: Select all
SQL := SQL + ' AND c.relname LIKE ''' + EscapeQuotes(ToLikeString(TableNamePattern))
+ ''' ';
//SC 21/10/2006
if (Length(LTypes) > 0) then
SQL := SQL + 'AND (false ';
//SC 21/10/2006
for I := 0 to High(LTypes) do
SQL := SQL + ' OR ( ' + TableTypeSQLExpression(LTypes[i], UseSchemas) + ' ) ';
//SC 21/10/2006
if (Length(LTypes) > 0) then
SQL := SQL + ' )';
//SC 21/10/2006
SQL := SQL + OrderBy;
Sandeep
-
- Expert Boarder
- Posts: 158
- Joined: 06.11.2005, 01:43
Here is the complete method as I have it right now
Please also have a look at line
I think it is better to test for Length rather than High.
Regards
Sandeep
Code: Select all
function TZPostgreSQLDatabaseMetadata.GetTables(const Catalog: string;
const SchemaPattern: string; const TableNamePattern: string;
const Types: TStringDynArray): IZResultSet;
var
I: Integer;
Key: string;
TableType, OrderBy, SQL: string;
UseSchemas: Boolean;
LTypes: TStringDynArray;
begin
Key := GetTablesMetaDataCacheKey(Catalog,SchemaPattern,TableNamePattern,Types);
Result := GetResultSetFromCache(Key);
if Result = nil then
begin
UseSchemas := True;
if HaveMinimumServerVersion(7, 3) then
begin
SQL := ' SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM,'
+ ' c.relname AS TABLE_NAME, '
+ ' CASE (n.nspname LIKE ''pg\\_%'')'
+ ' OR (n.nspname=''information_schema'')'
+ ' WHEN true THEN CASE n.nspname '
+ ' WHEN ''pg_catalog'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'''
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN ''information_schema'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'''
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN ''pg_toast'' THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TOAST TABLE'' '
+ ' WHEN ''i'' THEN ''SYSTEM TOAST INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE CASE c.relkind '
+ ' WHEN ''r'' THEN ''TEMPORARY TABLE'' '
+ ' WHEN ''i'' THEN ''TEMPORARY INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' END '
+ ' WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TABLE'' '
+ ' WHEN ''i'' THEN ''INDEX'' '
+ ' WHEN ''S'' THEN ''SEQUENCE'' '
+ ' WHEN ''v'' THEN ''VIEW'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE NULL '
+ ' END '
+ ' AS TABLE_TYPE, d.description AS REMARKS '
+ ' FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c '
+ ' LEFT JOIN pg_catalog.pg_description d'
+ ' ON (c.oid = d.objoid AND d.objsubid = 0) '
+ ' LEFT JOIN pg_catalog.pg_class dc ON (d.classoid=dc.oid'
+ ' AND dc.relname=''pg_class'') LEFT JOIN pg_catalog.pg_namespace dn'
+ ' ON (dn.oid=dc.relnamespace AND dn.nspname=''pg_catalog'') '
+ ' WHERE c.relnamespace = n.oid ';
//if SchemaPattern <> '' then // cannot happen due to SchemaPattern := '%'
begin
SQL := SQL + ' AND n.nspname LIKE '''
+ EscapeQuotes(ToLikeString(SchemaPattern)) + ''' ';
end;
OrderBy := ' ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME ';
end
else
begin
UseSchemas := False;
TableType := ' CASE c.relname LIKE ''pg\\_%'' '
+ 'WHEN true THEN CASE c.relname LIKE ''pg\\_toast\\_%'' '
+ 'WHEN true THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TOAST TABLE'' '
+ ' WHEN ''i'' THEN ''SYSTEM TOAST INDEX'' '
+ ' ELSE NULL '
+ 'END '
+ 'WHEN false THEN CASE c.relname LIKE ''pg\\_temp\\_%'' '
+ ' WHEN true THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TEMPORARY TABLE'' '
+ ' WHEN ''i'' THEN ''TEMPORARY INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''SYSTEM TABLE'' '
+ ' WHEN ''v'' THEN ''SYSTEM VIEW'' '
+ ' WHEN ''i'' THEN ''SYSTEM INDEX'' '
+ ' ELSE NULL '
+ ' END '
+ ' ELSE NULL '
+ 'END '
+ 'ELSE NULL '
+ 'END '
+ 'WHEN false THEN CASE c.relkind '
+ ' WHEN ''r'' THEN ''TABLE'' '
+ ' WHEN ''i'' THEN ''INDEX'' '
+ ' WHEN ''S'' THEN ''SEQUENCE'' '
+ ' WHEN ''v'' THEN ''VIEW'' '
+ ' ELSE NULL '
+ 'END '
+ 'ELSE NULL '
+ ' END ';
OrderBy := ' ORDER BY TABLE_TYPE,TABLE_NAME ';
SQL := 'SELECT NULL AS TABLE_CAT, NULL AS TABLE_SCHEM,'
+ ' c.relname AS TABLE_NAME, ' + TableType + ' AS TABLE_TYPE,'
+ ' NULL AS REMARKS FROM pg_class c WHERE true ';
end;
if (Types = nil) or (Length(Types) = 0) then //SC 08/12/2006
begin
SetLength(LTypes, 5);
LTypes[0] := 'TABLE';
LTypes[1] := 'VIEW';
LTypes[2] := 'INDEX';
LTypes[3] := 'SEQUENCE';
LTypes[4] := 'TEMPORARY TABLE';
end
else
LTypes := Types;
SQL := SQL + ' AND c.relname LIKE ''' + EscapeQuotes(ToLikeString(TableNamePattern))
+ ''' ';
//SC 21/10/2006
if (Length(LTypes) > 0) then
SQL := SQL + 'AND (false ';
//SC 21/10/2006
for I := 0 to High(LTypes) do
SQL := SQL + ' OR ( ' + TableTypeSQLExpression(LTypes[i], UseSchemas) + ' ) ';
//SC 21/10/2006
if (Length(LTypes) > 0) then
SQL := SQL + ' )';
//SC 21/10/2006
SQL := SQL + OrderBy;
Result := CopyToVirtualResultSet(
GetConnection.CreateStatement.ExecuteQuery(SQL),
ConstructVirtualResultSet(TableColumnsDynArray));
AddResultSetToCache(Key, Result);
end;
end;
Code: Select all
if (Types = nil) or (Length(Types) = 0) then
Regards
Sandeep
-
- Expert Boarder
- Posts: 158
- Joined: 06.11.2005, 01:43