Skip to content

Commit a95d699

Browse files
committed
performance improvement
1 parent 4f5380e commit a95d699

File tree

2 files changed

+10
-56
lines changed

2 files changed

+10
-56
lines changed

api.php

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6091,51 +6091,6 @@ public function __construct(LazyPdo $pdo, string $driver, string $database, arra
60916091
$this->typeConverter = new TypeConverter($driver);
60926092
}
60936093

6094-
private function updateSqlLiteReflectionTables() /*: void */
6095-
{
6096-
$reflection = $this->query('SELECT "name" FROM "sqlite_master" WHERE "type" = \'table\' and name like \'sys/%\';', []);
6097-
if (count($reflection) == 0) {
6098-
//create reflection tables
6099-
$this->query('CREATE table "sys/version" ("version" integer);', []);
6100-
$this->query('CREATE table "sys/tables" ("name" text, "type" text);', []);
6101-
$this->query('CREATE table "sys/columns" ("self" text,"cid" integer,"name" text,"type" integer,"notnull" integer,"dflt_value" integer,"pk" integer);', []);
6102-
$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);', []);
6103-
}
6104-
$version = $this->query('pragma schema_version;', [])[0]["schema_version"];
6105-
$current = $this->query('SELECT "version" from "sys/version";', []);
6106-
if (!$current || count($current) == 0 || !isset($current[0]["schema_version"]) || $version != $current[0]["schema_version"]) {
6107-
// reflection may take a while
6108-
set_time_limit(3600);
6109-
// update version data
6110-
$this->query('DELETE FROM "sys/version";', []);
6111-
$this->query('INSERT into "sys/version" ("version") VALUES (?);', [$version]);
6112-
6113-
// update tables data
6114-
$this->query('DELETE FROM "sys/tables";', []);
6115-
$result = $this->query('SELECT "name", "type" FROM sqlite_master WHERE ("type" = \'table\' or "type" = \'view\') and name not like "sys/%" and name<>"sqlite_sequence";', []);
6116-
$tables = array();
6117-
foreach ($result as $row) {
6118-
$tables[] = $row['name'];
6119-
$this->query('INSERT into "sys/tables" ("name", "type") VALUES (?, ?);', [$row['name'], $row['type']]);
6120-
}
6121-
// update columns and foreign_keys data
6122-
$this->query('DELETE FROM "sys/columns";', []);
6123-
$this->query('DELETE FROM "sys/foreign_keys";', []);
6124-
foreach ($tables as $table) {
6125-
$result = $this->query("pragma table_info(`$table`);", []);
6126-
foreach ($result as $row) {
6127-
array_unshift($row, $table);
6128-
$this->query('INSERT into "sys/columns" ("self","cid","name","type","notnull","dflt_value","pk") VALUES (?,?,?,?,?,?,?);', array_values($row));
6129-
}
6130-
$result = $this->query("pragma foreign_key_list(`$table`);", []);
6131-
foreach ($result as $row) {
6132-
array_unshift($row, $table);
6133-
$this->query('INSERT into "sys/foreign_keys" ("self","id","seq","table","from","to","on_update","on_delete","match") VALUES (?,?,?,?,?,?,?,?,?);', array_values($row));
6134-
}
6135-
}
6136-
}
6137-
}
6138-
61396094
public function getIgnoredTables(): array
61406095
{
61416096
switch ($this->driver) {
@@ -6146,7 +6101,7 @@ public function getIgnoredTables(): array
61466101
case 'sqlsrv':
61476102
return [];
61486103
case 'sqlite':
6149-
return ['sys/version', 'sys/tables', 'sys/columns', 'sys/foreign_keys'];
6104+
return [];
61506105
}
61516106
}
61526107

@@ -6160,8 +6115,7 @@ private function getTablesSQL(): string
61606115
case 'sqlsrv':
61616116
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"';
61626117
case 'sqlite':
6163-
$this->updateSqlLiteReflectionTables();
6164-
return 'SELECT t.name as "TABLE_NAME", t.type as "TABLE_TYPE" FROM "sys/tables" t WHERE t.type IN (\'table\', \'view\') AND \'\' <> ? ORDER BY "TABLE_NAME"';
6118+
return 'SELECT t.name as "TABLE_NAME", t.type as "TABLE_TYPE" FROM sqlite_master t WHERE t.type IN (\'table\', \'view\') AND \'\' <> ? ORDER BY "TABLE_NAME"';
61656119
}
61666120
}
61676121

@@ -6175,8 +6129,7 @@ private function getTableColumnsSQL(): string
61756129
case 'sqlsrv':
61766130
return 'SELECT c.name AS "COLUMN_NAME", c.is_nullable AS "IS_NULLABLE", t.Name AS "DATA_TYPE", (c.max_length/2) AS "CHARACTER_MAXIMUM_LENGTH", c.precision AS "NUMERIC_PRECISION", c.scale AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID(?) AND \'\' <> ?';
61776131
case 'sqlite':
6178-
$this->updateSqlLiteReflectionTables();
6179-
return 'SELECT "name" AS "COLUMN_NAME", case when "notnull"==1 then \'no\' else \'yes\' end as "IS_NULLABLE", "type" AS "DATA_TYPE", 2147483647 AS "CHARACTER_MAXIMUM_LENGTH", 0 AS "NUMERIC_PRECISION", 0 AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM "sys/columns" WHERE "self" = ? AND \'\' <> ?';
6132+
return 'SELECT "name" AS "COLUMN_NAME", case when "notnull"==1 then \'no\' else \'yes\' end as "IS_NULLABLE", lower("type") AS "DATA_TYPE", 2147483647 AS "CHARACTER_MAXIMUM_LENGTH", 0 AS "NUMERIC_PRECISION", 0 AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM pragma_table_info(?) WHERE \'\' <> ?';
61806133
}
61816134
}
61826135

@@ -6190,7 +6143,7 @@ private function getTablePrimaryKeysSQL(): string
61906143
case 'sqlsrv':
61916144
return 'SELECT c.NAME as "COLUMN_NAME" FROM sys.key_constraints kc inner join sys.objects t on t.object_id = kc.parent_object_id INNER JOIN sys.index_columns ic ON kc.parent_object_id = ic.object_id and kc.unique_index_id = ic.index_id INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id WHERE kc.type = \'PK\' and t.object_id = OBJECT_ID(?) and \'\' <> ?';
61926145
case 'sqlite':
6193-
return 'SELECT "name" as "COLUMN_NAME" FROM "sys/columns" WHERE "pk"=1 AND "self"=? AND \'\' <> ?';
6146+
return 'SELECT "name" as "COLUMN_NAME" FROM pragma_table_info(?) WHERE "pk"=1 AND \'\' <> ?';
61946147
}
61956148
}
61966149

@@ -6204,7 +6157,7 @@ private function getTableForeignKeysSQL(): string
62046157
case 'sqlsrv':
62056158
return 'SELECT COL_NAME(fc.parent_object_id, fc.parent_column_id) AS "COLUMN_NAME", OBJECT_NAME (f.referenced_object_id) AS "REFERENCED_TABLE_NAME" FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id WHERE f.parent_object_id = OBJECT_ID(?) and \'\' <> ?';
62066159
case 'sqlite':
6207-
return 'SELECT "from" AS "COLUMN_NAME", "table" AS "REFERENCED_TABLE_NAME" FROM "sys/foreign_keys" WHERE "self" = ? AND \'\' <> ?';
6160+
return 'SELECT "from" AS "COLUMN_NAME", "table" AS "REFERENCED_TABLE_NAME" FROM pragma_foreign_key_list(?) WHERE \'\' <> ?';
62086161
}
62096162
}
62106163

@@ -6268,8 +6221,8 @@ public function getTableColumns(string $tableName, string $type): array
62686221
}
62696222
if ($this->driver == 'sqlite') {
62706223
foreach ($results as &$result) {
6271-
// mysql does not properly reflect display width of types
6272-
preg_match('|([a-zA-Z]+)(\(([0-9]+)(,([0-9]+))?\))?|', $result['DATA_TYPE'], $matches);
6224+
// sqlite does not properly reflect display width of types
6225+
preg_match('|([a-z]+)(\(([0-9]+)(,([0-9]+))?\))?|', $result['DATA_TYPE'], $matches);
62736226
if (isset($matches[1])) {
62746227
$result['DATA_TYPE'] = $matches[1];
62756228
} else {
@@ -6316,7 +6269,7 @@ public function toJdbcType(string $type, string $size): string
63166269
private function query(string $sql, array $parameters): array
63176270
{
63186271
$stmt = $this->pdo->prepare($sql);
6319-
// echo "- $sql -- " . json_encode($parameters, JSON_UNESCAPED_UNICODE) . "\n";
6272+
//echo "- $sql -- " . json_encode($parameters, JSON_UNESCAPED_UNICODE) . "\n";
63206273
$stmt->execute($parameters);
63216274
return $stmt->fetchAll();
63226275
}

composer.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)