Skip to content

Commit 74456af

Browse files
committed
Adding some reflection tables
1 parent b0132a3 commit 74456af

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/Tqdev/PhpCrudApi/Database/GenericReflection.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,54 @@ public function __construct(LazyPdo $pdo, string $driver, string $database, arra
2121
$this->typeConverter = new TypeConverter($driver);
2222
}
2323

24+
private function createSqlLiteReflectionTables() /*: void */
25+
{
26+
$reflection = $this->query('SELECT "name" FROM "sqlite_master" WHERE "type" = \'table\' and name like \'sys/%\';',[]);
27+
if (count($reflection->fetchAll())==0) {
28+
//create reflection tables
29+
$this->query('CREATE table "sys/version" ("version" integer);',[]);
30+
$this->query('CREATE table "sys/tables" ("name" text, "type" text);',[]);
31+
$this->query('CREATE table "sys/columns" ("self" text,"cid" integer,"name" text,"type" integer,"notnull" integer,"dflt_value" integer,"pk" integer);',[]);
32+
$this->query('CREATE table "sys/foreign_keys" ("self" text,"id" integer,"seq" integer,"table" text,"from" text,"to" text,"on_update" text,"on_delete" text,"match" text);',[]);
33+
}
34+
$version = $this->query('pragma schema_version',[])->fetchColumn(0);
35+
if ($version != $this->query('SELECT "version" from "sys/version";')->fetchColumn(0)) {
36+
// reflection may take a while
37+
set_time_limit(3600);
38+
// update version data
39+
$this->query('DELETE FROM "sys/version";');
40+
$stmt = $this->pdo->prepare('INSERT into "sys/version" ("version") VALUES (?);');
41+
$stmt->execute(array($version));
42+
43+
// update tables data
44+
$this->query('DELETE FROM "sys/tables";');
45+
$result = $this->query('SELECT "name", "type" FROM sqlite_master WHERE ("type" = \'table\' or "type" = \'view\') and name not like "sys/%" and name<>"sqlite_sequence";');
46+
$tables = array();
47+
foreach($result as $row) {
48+
$tables[] = $row['name'];
49+
$stmt = $this->pdo->prepare('INSERT into "sys/tables" ("name", "type") VALUES (?, ?);');
50+
$stmt->execute(array($row['name'], $row['type']));
51+
}
52+
// update columns and foreign_keys data
53+
$this->query('DELETE FROM "sys/columns"');
54+
$this->query('DELETE FROM "sys/foreign_keys"');
55+
foreach ($tables as $table) {
56+
$result = $this->query("pragma table_info(`$table`);");
57+
foreach($result as $row) {
58+
array_unshift($row, $table);
59+
$stmt = $this->pdo->prepare('INSERT into "sys/columns" ("self","cid","name","type","notnull","dflt_value","pk") VALUES (?,?,?,?,?,?,?);');
60+
$stmt->execute(array_values($row));
61+
}
62+
$result = $this->query("pragma foreign_key_list(`$table`);");
63+
foreach($result as $row) {
64+
array_unshift($row, $table);
65+
$stmt = $this->pdo->prepare('INSERT into "sys/foreign_keys" ("self","id","seq","table","from","to","on_update","on_delete","match") VALUES (?,?,?,?,?,?,?,?,?);');
66+
$stmt->execute(array_values($row));
67+
}
68+
}
69+
}
70+
}
71+
2472
public function getIgnoredTables(): array
2573
{
2674
switch ($this->driver) {
@@ -30,9 +78,13 @@ public function getIgnoredTables(): array
3078
return ['spatial_ref_sys', 'raster_columns', 'raster_overviews', 'geography_columns', 'geometry_columns'];
3179
case 'sqlsrv':
3280
return [];
81+
case 'sqlite':
82+
return ['sys/version','sys/tables','sys/columns','sys/foreign_keys'];
3383
}
3484
}
3585

86+
87+
3688
private function getTablesSQL(): string
3789
{
3890
switch ($this->driver) {
@@ -42,6 +94,9 @@ private function getTablesSQL(): string
4294
return 'SELECT c.relname as "TABLE_NAME", c.relkind as "TABLE_TYPE" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN (\'r\', \'v\') AND n.nspname <> \'pg_catalog\' AND n.nspname <> \'information_schema\' AND n.nspname !~ \'^pg_toast\' AND pg_catalog.pg_table_is_visible(c.oid) AND \'\' <> ? ORDER BY "TABLE_NAME";';
4395
case 'sqlsrv':
4496
return 'SELECT o.name as "TABLE_NAME", o.xtype as "TABLE_TYPE" FROM sysobjects o WHERE o.xtype IN (\'U\', \'V\') ORDER BY "TABLE_NAME"';
97+
case 'sqlite':
98+
$this->createSqlLiteReflectionTables();
99+
return 'SELECT t.name as "TABLE_NAME", t.type as "TABLE_TYPE" FROM "sys/tables" t WHERE t.type IN (\'table\', \'view\') ORDER BY "TABLE_NAME"';
45100
}
46101
}
47102

0 commit comments

Comments
 (0)