@@ -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