add rewrite logic for SHOW TABLE STATUS and a catch for calls to information_schema

This commit is contained in:
Matthew Bucci
2024-02-21 01:15:51 -08:00
parent 23da9c0dd2
commit ebbe2915c6
3 changed files with 90 additions and 1 deletions
@@ -0,0 +1,54 @@
<?php
class ShowTableStatusSQLRewriter extends AbstractSQLRewriter
{
public function rewrite(): string
{
$sql = $this->original();
return $this->generatePostgresShowTableStatus();
}
/**
* Generates a PostgreSQL-compatible SQL query to mimic MySQL's "SHOW TABLE STATUS".
*
* @return string The generated SQL query
*/
public function generatePostgresShowTableStatus($schema = "public")
{
$sql = <<<SQL
SELECT
'Postgres' AS Engine,
cls.relname AS TableName,
NULL AS Version,
NULL AS Row_format,
cls.reltuples AS Rows,
NULL AS Avg_row_length,
pg_relation_size(cls.oid) AS Data_length,
NULL AS Max_data_length,
pg_indexes_size(cls.oid) AS Index_length,
NULL AS Data_free,
NULL AS Auto_increment,
NULL AS Create_time,
NULL AS Update_time,
NULL AS Check_time,
'UTF8' AS Table_collation,
NULL AS Checksum,
NULL AS Create_options,
obj_description(cls.oid) AS Comment
FROM
pg_class cls
JOIN
pg_namespace nsp ON cls.relnamespace = nsp.oid
WHERE
cls.relkind = 'r'
AND nsp.nspname NOT LIKE 'pg_%' -- Ignore schemas with names starting with pg_
AND nsp.nspname != 'information_schema' -- Ignore the information_schema
AND nsp.nspname = $schema -- Select only tables in the 'myschema' schema
ORDER BY
cls.relname ASC;
SQL;
return $sql;
}
}