@@ -42,45 +42,115 @@ protected function setUp(): void
4242
4343 public function testGetForeignKeyDataWithSpecialTableName (): void
4444 {
45- $ this ->db ->query ('DROP TABLE IF EXISTS `test-parent` ' );
46- $ this ->db ->query ('DROP TABLE IF EXISTS `test-child` ' );
47-
4845 $ this ->db ->query ('CREATE TABLE `test-parent` (id INTEGER PRIMARY KEY) ' );
4946 $ this ->db ->query ('CREATE TABLE `test-child` (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES `test-parent`(id)) ' );
5047
5148 $ foreignKeys = $ this ->db ->getForeignKeyData ('test-child ' );
5249
50+ $ this ->db ->query ('DROP TABLE IF EXISTS `test-child` ' );
51+ $ this ->db ->query ('DROP TABLE IF EXISTS `test-parent` ' );
52+
5353 $ this ->assertCount (1 , $ foreignKeys );
5454 $ firstKey = reset ($ foreignKeys );
5555 $ this ->assertSame ('test-child ' , $ firstKey ->table_name );
5656 $ this ->assertSame (['parent_id ' ], $ firstKey ->column_name );
5757 $ this ->assertSame ('test-parent ' , $ firstKey ->foreign_table_name );
5858 $ this ->assertSame (['id ' ], $ firstKey ->foreign_column_name );
59-
60- $ this ->db ->query ('DROP TABLE IF EXISTS `test-child` ' );
61- $ this ->db ->query ('DROP TABLE IF EXISTS `test-parent` ' );
6259 }
6360
6461 public function testGetForeignKeyDataWithAttachedDatabase (): void
6562 {
6663 $ this ->db ->query ('ATTACH DATABASE ":memory:" AS `test_schema` ' );
67- $ this ->db ->query ('DROP TABLE IF EXISTS `test_schema`.`attached-parent` ' );
68- $ this ->db ->query ('DROP TABLE IF EXISTS `test_schema`.`attached-child` ' );
6964
7065 $ this ->db ->query ('CREATE TABLE `test_schema`.`attached-parent` (id INTEGER PRIMARY KEY) ' );
7166 $ this ->db ->query ('CREATE TABLE `test_schema`.`attached-child` (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES `attached-parent`(id)) ' );
7267
7368 $ foreignKeys = $ this ->db ->getForeignKeyData ('test_schema.attached-child ' );
7469
70+ $ this ->db ->query ('DROP TABLE IF EXISTS `test_schema`.`attached-child` ' );
71+ $ this ->db ->query ('DROP TABLE IF EXISTS `test_schema`.`attached-parent` ' );
72+ $ this ->db ->query ('DETACH DATABASE `test_schema` ' );
73+
7574 $ this ->assertCount (1 , $ foreignKeys );
7675 $ firstKey = reset ($ foreignKeys );
7776 $ this ->assertSame ('test_schema.attached-child ' , $ firstKey ->table_name );
7877 $ this ->assertSame (['p_id ' ], $ firstKey ->column_name );
7978 $ this ->assertSame ('attached-parent ' , $ firstKey ->foreign_table_name );
8079 $ this ->assertSame (['id ' ], $ firstKey ->foreign_column_name );
80+ }
8181
82- $ this ->db ->query ('DROP TABLE IF EXISTS `test_schema`.`attached-child` ' );
83- $ this ->db ->query ('DROP TABLE IF EXISTS `test_schema`.`attached-parent` ' );
84- $ this ->db ->query ('DETACH DATABASE `test_schema` ' );
82+ public function testGetForeignKeyDataWithParenthesesInTableName (): void
83+ {
84+ $ this ->db ->query ('CREATE TABLE "parent(v1)" (id INTEGER PRIMARY KEY) ' );
85+ $ this ->db ->query ('CREATE TABLE "child(v2)" (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES "parent(v1)"(id)) ' );
86+
87+ $ foreignKeys = $ this ->db ->getForeignKeyData ('child(v2) ' );
88+
89+ $ this ->db ->query ('DROP TABLE IF EXISTS "child(v2)" ' );
90+ $ this ->db ->query ('DROP TABLE IF EXISTS "parent(v1)" ' );
91+
92+ $ this ->assertCount (1 , $ foreignKeys );
93+ $ firstKey = reset ($ foreignKeys );
94+ $ this ->assertSame ('child(v2) ' , $ firstKey ->table_name );
95+ $ this ->assertSame (['parent_id ' ], $ firstKey ->column_name );
96+ $ this ->assertSame ('parent(v1) ' , $ firstKey ->foreign_table_name );
97+ $ this ->assertSame (['id ' ], $ firstKey ->foreign_column_name );
98+ }
99+
100+ public function testGetForeignKeyDataWithQuotesAndSpacesInTableName (): void
101+ {
102+ $ this ->db ->query ('CREATE TABLE "parent table" (id INTEGER PRIMARY KEY) ' );
103+ $ this ->db ->query ('CREATE TABLE "child \'quote with space" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent table"(id)) ' );
104+
105+ $ foreignKeys = $ this ->db ->getForeignKeyData ("child'quote with space " );
106+
107+ $ this ->db ->query ('DROP TABLE IF EXISTS "child \'quote with space" ' );
108+ $ this ->db ->query ('DROP TABLE IF EXISTS "parent table" ' );
109+
110+ $ this ->assertCount (1 , $ foreignKeys );
111+ $ firstKey = reset ($ foreignKeys );
112+ $ this ->assertSame ("child'quote with space " , $ firstKey ->table_name );
113+ $ this ->assertSame (['p_id ' ], $ firstKey ->column_name );
114+ $ this ->assertSame ('parent table ' , $ firstKey ->foreign_table_name );
115+ $ this ->assertSame (['id ' ], $ firstKey ->foreign_column_name );
116+ }
117+
118+ public function testGetForeignKeyDataWithNumericAndReservedWordTableName (): void
119+ {
120+ $ this ->db ->query ('CREATE TABLE "order" (id INTEGER PRIMARY KEY) ' );
121+ $ this ->db ->query ('CREATE TABLE "12345" (id INTEGER PRIMARY KEY, order_id INTEGER, FOREIGN KEY (order_id) REFERENCES "order"(id)) ' );
122+
123+ $ foreignKeys = $ this ->db ->getForeignKeyData ('12345 ' );
124+
125+ $ this ->db ->query ('DROP TABLE IF EXISTS "12345" ' );
126+ $ this ->db ->query ('DROP TABLE IF EXISTS "order" ' );
127+
128+ $ this ->assertCount (1 , $ foreignKeys );
129+ $ firstKey = reset ($ foreignKeys );
130+ $ this ->assertSame ('12345 ' , $ firstKey ->table_name );
131+ $ this ->assertSame (['order_id ' ], $ firstKey ->column_name );
132+ $ this ->assertSame ('order ' , $ firstKey ->foreign_table_name );
133+ $ this ->assertSame (['id ' ], $ firstKey ->foreign_column_name );
134+ }
135+
136+ public function testGetForeignKeyDataWithAttachedDatabaseSpecialIdentifiers (): void
137+ {
138+ $ this ->db ->query ('ATTACH DATABASE ":memory:" AS "schema-db(1)" ' );
139+
140+ $ this ->db ->query ('CREATE TABLE "schema-db(1)"."parent(v1)" (id INTEGER PRIMARY KEY) ' );
141+ $ 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)) ' );
142+
143+ $ foreignKeys = $ this ->db ->getForeignKeyData ('schema-db(1).child(v2) ' );
144+
145+ $ this ->db ->query ('DROP TABLE IF EXISTS "schema-db(1)"."child(v2)" ' );
146+ $ this ->db ->query ('DROP TABLE IF EXISTS "schema-db(1)"."parent(v1)" ' );
147+ $ this ->db ->query ('DETACH DATABASE "schema-db(1)" ' );
148+
149+ $ this ->assertCount (1 , $ foreignKeys );
150+ $ firstKey = reset ($ foreignKeys );
151+ $ this ->assertSame ('schema-db(1).child(v2) ' , $ firstKey ->table_name );
152+ $ this ->assertSame (['p_id ' ], $ firstKey ->column_name );
153+ $ this ->assertSame ('parent(v1) ' , $ firstKey ->foreign_table_name );
154+ $ this ->assertSame (['id ' ], $ firstKey ->foreign_column_name );
85155 }
86156}
0 commit comments