Skip to content

Commit 7da8053

Browse files
committed
fix: properly escape table name and schema in SQLite3 getForeignKeyData
1 parent 6e0a6f8 commit 7da8053

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

‎system/Database/SQLite3/Connection.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ protected function _foreignKeyData(string $table): array
366366
$parts = explode('.', $table);
367367
$tableName = array_pop($parts);
368368
$schema = array_pop($parts);
369-
$pragma = ($schema === null ? '' : $this->escapeIdentifiers($schema) . '.')
370-
. 'foreign_key_list(' . $this->escapeIdentifiers($tableName) . ')';
369+
$pragma = ($schema === null || $schema === '' ? '' : $this->escapeIdentifier($schema) . '.')
370+
. 'foreign_key_list(' . $this->escape($tableName) . ')';
371371

372372
$query = $this->query('PRAGMA ' . $pragma)->getResult();
373373
$indexes = [];

‎tests/system/Database/Live/SQLite3/GetForeignKeyDataTest.php‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,90 @@ public function testGetForeignKeyDataWithAttachedDatabase(): void
8383
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`');
8484
$this->db->query('DETACH DATABASE `test_schema`');
8585
}
86+
87+
public function testGetForeignKeyDataWithParenthesesInTableName(): void
88+
{
89+
$this->db->query('DROP TABLE IF EXISTS "parent(v1)"');
90+
$this->db->query('DROP TABLE IF EXISTS "child(v2)"');
91+
92+
$this->db->query('CREATE TABLE "parent(v1)" (id INTEGER PRIMARY KEY)');
93+
$this->db->query('CREATE TABLE "child(v2)" (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES "parent(v1)"(id))');
94+
95+
$foreignKeys = $this->db->getForeignKeyData('child(v2)');
96+
97+
$this->assertCount(1, $foreignKeys);
98+
$firstKey = reset($foreignKeys);
99+
$this->assertSame('child(v2)', $firstKey->table_name);
100+
$this->assertSame(['parent_id'], $firstKey->column_name);
101+
$this->assertSame('parent(v1)', $firstKey->foreign_table_name);
102+
$this->assertSame(['id'], $firstKey->foreign_column_name);
103+
104+
$this->db->query('DROP TABLE IF EXISTS "child(v2)"');
105+
$this->db->query('DROP TABLE IF EXISTS "parent(v1)"');
106+
}
107+
108+
public function testGetForeignKeyDataWithQuotesAndSpacesInTableName(): void
109+
{
110+
$this->db->query('DROP TABLE IF EXISTS "parent table"');
111+
$this->db->query('DROP TABLE IF EXISTS "child\'quote with space"');
112+
113+
$this->db->query('CREATE TABLE "parent table" (id INTEGER PRIMARY KEY)');
114+
$this->db->query('CREATE TABLE "child\'quote with space" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent table"(id))');
115+
116+
$foreignKeys = $this->db->getForeignKeyData("child'quote with space");
117+
118+
$this->assertCount(1, $foreignKeys);
119+
$firstKey = reset($foreignKeys);
120+
$this->assertSame("child'quote with space", $firstKey->table_name);
121+
$this->assertSame(['p_id'], $firstKey->column_name);
122+
$this->assertSame('parent table', $firstKey->foreign_table_name);
123+
$this->assertSame(['id'], $firstKey->foreign_column_name);
124+
125+
$this->db->query('DROP TABLE IF EXISTS "child\'quote with space"');
126+
$this->db->query('DROP TABLE IF EXISTS "parent table"');
127+
}
128+
129+
public function testGetForeignKeyDataWithNumericAndReservedWordTableName(): void
130+
{
131+
$this->db->query('DROP TABLE IF EXISTS "order"');
132+
$this->db->query('DROP TABLE IF EXISTS "12345"');
133+
134+
$this->db->query('CREATE TABLE "order" (id INTEGER PRIMARY KEY)');
135+
$this->db->query('CREATE TABLE "12345" (id INTEGER PRIMARY KEY, order_id INTEGER, FOREIGN KEY (order_id) REFERENCES "order"(id))');
136+
137+
$foreignKeys = $this->db->getForeignKeyData('12345');
138+
139+
$this->assertCount(1, $foreignKeys);
140+
$firstKey = reset($foreignKeys);
141+
$this->assertSame('12345', $firstKey->table_name);
142+
$this->assertSame(['order_id'], $firstKey->column_name);
143+
$this->assertSame('order', $firstKey->foreign_table_name);
144+
$this->assertSame(['id'], $firstKey->foreign_column_name);
145+
146+
$this->db->query('DROP TABLE IF EXISTS "12345"');
147+
$this->db->query('DROP TABLE IF EXISTS "order"');
148+
}
149+
150+
public function testGetForeignKeyDataWithAttachedDatabaseSpecialIdentifiers(): void
151+
{
152+
$this->db->query('ATTACH DATABASE ":memory:" AS "schema-db(1)"');
153+
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."parent(v1)"');
154+
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."child(v2)"');
155+
156+
$this->db->query('CREATE TABLE "schema-db(1)"."parent(v1)" (id INTEGER PRIMARY KEY)');
157+
$this->db->query('CREATE TABLE "schema-db(1)"."child(v2)" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent(v1)"(id))');
158+
159+
$foreignKeys = $this->db->getForeignKeyData('schema-db(1).child(v2)');
160+
161+
$this->assertCount(1, $foreignKeys);
162+
$firstKey = reset($foreignKeys);
163+
$this->assertSame('schema-db(1).child(v2)', $firstKey->table_name);
164+
$this->assertSame(['p_id'], $firstKey->column_name);
165+
$this->assertSame('parent(v1)', $firstKey->foreign_table_name);
166+
$this->assertSame(['id'], $firstKey->foreign_column_name);
167+
168+
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."child(v2)"');
169+
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."parent(v1)"');
170+
$this->db->query('DETACH DATABASE "schema-db(1)"');
171+
}
86172
}

0 commit comments

Comments
 (0)