@@ -119,6 +119,7 @@ public function hasCollection(string $collectionName): bool
119
119
SELECT TABLE_NAME
120
120
FROM information_schema.tables
121
121
WHERE TABLE_NAME = ' {$ this ->tableName ($ collectionName )}'
122
+ AND TABLE_SCHEMA = ' {$ this ->schemaName ($ collectionName )}'
122
123
EOT ;
123
124
124
125
$ stmt = $ this ->connection ->prepare ($ query );
@@ -147,8 +148,10 @@ public function addCollection(string $collectionName, Index ...$indices): void
147
148
}
148
149
}
149
150
151
+ $ createSchemaCmd = "CREATE SCHEMA IF NOT EXISTS {$ this ->schemaName ($ collectionName )}" ;
152
+
150
153
$ cmd = <<<EOT
151
- CREATE TABLE {$ this ->tableName ($ collectionName )} (
154
+ CREATE TABLE {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )} (
152
155
id {$ this ->docIdSchema },
153
156
doc JSONB NOT NULL,
154
157
$ metadataColumns
@@ -160,7 +163,8 @@ public function addCollection(string $collectionName, Index ...$indices): void
160
163
return $ this ->indexToSqlCmd ($ index , $ collectionName );
161
164
}, $ indices );
162
165
163
- $ this ->transactional (function () use ($ cmd , $ indicesCmds ) {
166
+ $ this ->transactional (function () use ($ createSchemaCmd , $ cmd , $ indicesCmds ) {
167
+ $ this ->connection ->prepare ($ createSchemaCmd )->execute ();
164
168
$ this ->connection ->prepare ($ cmd )->execute ();
165
169
166
170
array_walk ($ indicesCmds , function ($ cmd ) {
@@ -176,7 +180,7 @@ public function addCollection(string $collectionName, Index ...$indices): void
176
180
public function dropCollection (string $ collectionName ): void
177
181
{
178
182
$ cmd = <<<EOT
179
- DROP TABLE {$ this ->tableName ($ collectionName )};
183
+ DROP TABLE {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )};
180
184
EOT ;
181
185
182
186
$ this ->transactional (function () use ($ cmd ) {
@@ -190,6 +194,7 @@ public function hasCollectionIndex(string $collectionName, string $indexName): b
190
194
SELECT INDEXNAME
191
195
FROM pg_indexes
192
196
WHERE TABLENAME = ' {$ this ->tableName ($ collectionName )}'
197
+ AND SCHEMANAME = ' {$ this ->schemaName ($ collectionName )}'
193
198
AND INDEXNAME = ' $ indexName'
194
199
EOT ;
195
200
@@ -222,7 +227,7 @@ public function addCollectionIndex(string $collectionName, Index $index): void
222
227
$ columnsSql = substr ($ columnsSql , 2 );
223
228
224
229
$ metadataColumnCmd = <<<EOT
225
- ALTER TABLE {$ this ->tableName ($ collectionName )}
230
+ ALTER TABLE {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
226
231
$ columnsSql;
227
232
EOT ;
228
233
@@ -262,7 +267,7 @@ public function dropCollectionIndex(string $collectionName, $index): void
262
267
$ columnsSql = substr ($ columnsSql , 2 );
263
268
264
269
$ metadataColumnCmd = <<<EOT
265
- ALTER TABLE {$ this ->tableName ($ collectionName )}
270
+ ALTER TABLE {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
266
271
$ columnsSql;
267
272
EOT ;
268
273
$ index = $ index ->indexCmd ();
@@ -312,7 +317,9 @@ public function addDoc(string $collectionName, string $docId, array $doc): void
312
317
}
313
318
314
319
$ cmd = <<<EOT
315
- INSERT INTO {$ this ->tableName ($ collectionName )} (id, doc {$ metadataKeysStr }) VALUES (:id, :doc {$ metadataValsStr });
320
+ INSERT INTO {$ this ->schemaName ($ collectionName )}. {$ this ->tableName ($ collectionName )} (
321
+ id, doc {$ metadataKeysStr }) VALUES (:id, :doc {$ metadataValsStr }
322
+ );
316
323
EOT ;
317
324
318
325
$ this ->transactional (function () use ($ cmd , $ docId , $ doc , $ metadata ) {
@@ -345,7 +352,7 @@ public function updateDoc(string $collectionName, string $docId, array $docOrSub
345
352
}
346
353
347
354
$ cmd = <<<EOT
348
- UPDATE {$ this ->tableName ($ collectionName )}
355
+ UPDATE {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
349
356
SET doc = (to_jsonb(doc) || :doc) {$ metadataStr }
350
357
WHERE id = :id
351
358
;
@@ -384,7 +391,7 @@ public function updateMany(string $collectionName, Filter $filter, array $set):
384
391
}
385
392
386
393
$ cmd = <<<EOT
387
- UPDATE {$ this ->tableName ($ collectionName )}
394
+ UPDATE {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
388
395
SET doc = (to_jsonb(doc) || :doc) {$ metadataStr }
389
396
$ where;
390
397
EOT ;
@@ -424,7 +431,7 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
424
431
public function deleteDoc (string $ collectionName , string $ docId ): void
425
432
{
426
433
$ cmd = <<<EOT
427
- DELETE FROM {$ this ->tableName ($ collectionName )}
434
+ DELETE FROM {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
428
435
WHERE id = :id
429
436
EOT ;
430
437
@@ -447,7 +454,7 @@ public function deleteMany(string $collectionName, Filter $filter): void
447
454
$ where = $ filterStr ? "WHERE $ filterStr " : '' ;
448
455
449
456
$ cmd = <<<EOT
450
- DELETE FROM {$ this ->tableName ($ collectionName )}
457
+ DELETE FROM {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
451
458
$ where;
452
459
EOT ;
453
460
@@ -465,7 +472,7 @@ public function getDoc(string $collectionName, string $docId): ?array
465
472
{
466
473
$ query = <<<EOT
467
474
SELECT doc
468
- FROM {$ this ->tableName ($ collectionName )}
475
+ FROM {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
469
476
WHERE id = :id
470
477
EOT ;
471
478
$ stmt = $ this ->connection ->prepare ($ query );
@@ -502,7 +509,7 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
502
509
503
510
$ query = <<<EOT
504
511
SELECT doc
505
- FROM {$ this ->tableName ($ collectionName )}
512
+ FROM {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
506
513
$ where
507
514
$ orderBy
508
515
$ limit
@@ -701,7 +708,7 @@ private function indexToSqlCmd(Index $index, string $collectionName): string
701
708
$ name = $ index ->name () ?? '' ;
702
709
703
710
$ cmd = <<<EOT
704
- CREATE $ type $ name ON {$ this ->tableName ($ collectionName )}
711
+ CREATE $ type $ name ON {$ this ->schemaName ( $ collectionName )} . { $ this -> tableName ($ collectionName )}
705
712
$ fields;
706
713
EOT ;
707
714
@@ -751,6 +758,19 @@ private function extractFieldPartFromFieldIndex(DocumentStore\FieldIndex $fieldI
751
758
752
759
private function tableName (string $ collectionName ): string
753
760
{
761
+ if (false !== $ dotPosition = strpos ($ collectionName , '. ' )) {
762
+ $ collectionName = substr ($ collectionName , $ dotPosition +1 );
763
+ }
764
+
754
765
return mb_strtolower ($ this ->tablePrefix . $ collectionName );
755
766
}
767
+
768
+ private function schemaName (string $ collectionName ): string
769
+ {
770
+ $ schemaName = 'public ' ;
771
+ if (false !== $ dotPosition = strpos ($ collectionName , '. ' )) {
772
+ $ schemaName = substr ($ collectionName , 0 , $ dotPosition );
773
+ }
774
+ return mb_strtolower ($ schemaName );
775
+ }
756
776
}
0 commit comments