17
17
use EventEngine \DocumentStore \OrderBy \OrderBy ;
18
18
use EventEngine \DocumentStore \PartialSelect ;
19
19
use EventEngine \DocumentStore \Postgres \Exception \RuntimeException ;
20
- use EventEngine \DocumentStore \Postgres \Filter \PostgresFilterProcessor ;
21
20
use EventEngine \DocumentStore \Postgres \Filter \FilterProcessor ;
21
+ use EventEngine \DocumentStore \Postgres \Filter \PostgresFilterProcessor ;
22
+ use EventEngine \DocumentStore \Postgres \OrderBy \OrderByClause ;
23
+ use EventEngine \DocumentStore \Postgres \OrderBy \OrderByProcessor ;
24
+ use EventEngine \DocumentStore \Postgres \OrderBy \PostgresOrderByProcessor ;
22
25
use EventEngine \Util \VariableType ;
23
26
24
27
use function implode ;
@@ -43,6 +46,11 @@ final class PostgresDocumentStore implements DocumentStore\DocumentStore
43
46
*/
44
47
private $ filterProcessor ;
45
48
49
+ /**
50
+ * @var OrderByProcessor
51
+ */
52
+ private $ orderByProcessor ;
53
+
46
54
private $ tablePrefix = 'em_ds_ ' ;
47
55
48
56
private $ docIdSchema = 'UUID NOT NULL ' ;
@@ -57,7 +65,8 @@ public function __construct(
57
65
string $ docIdSchema = null ,
58
66
bool $ transactional = true ,
59
67
bool $ useMetadataColumns = false ,
60
- FilterProcessor $ filterProcessor = null
68
+ FilterProcessor $ filterProcessor = null ,
69
+ OrderByProcessor $ orderByProcessor = null
61
70
) {
62
71
$ this ->connection = $ connection ;
63
72
$ this ->connection ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION );
@@ -67,6 +76,11 @@ public function __construct(
67
76
}
68
77
$ this ->filterProcessor = $ filterProcessor ;
69
78
79
+ if (null === $ orderByProcessor ) {
80
+ $ orderByProcessor = new PostgresOrderByProcessor ($ useMetadataColumns );
81
+ }
82
+ $ this ->orderByProcessor = $ orderByProcessor ;
83
+
70
84
if (null !== $ tablePrefix ) {
71
85
$ this ->tablePrefix = $ tablePrefix ;
72
86
}
@@ -441,7 +455,7 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
441
455
{
442
456
$ doc = $ this ->getDoc ($ collectionName , $ docId );
443
457
444
- if ($ doc !== null ) {
458
+ if ($ doc !== null ) {
445
459
$ this ->updateDoc ($ collectionName , $ docId , $ docOrSubset );
446
460
} else {
447
461
$ this ->addDoc ($ collectionName , $ docId , $ docOrSubset );
@@ -625,12 +639,16 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
625
639
$ filterStr = $ filterClause ->clause ();
626
640
$ args = $ filterClause ->args ();
627
641
642
+ $ orderByClause = $ orderBy ? $ this ->orderByProcessor ->process ($ orderBy ) : new OrderByClause (null , []);
643
+ $ orderByStr = $ orderByClause ->clause ();
644
+ $ orderByArgs = $ orderByClause ->args ();
645
+
628
646
$ where = $ filterStr ? "WHERE $ filterStr " : '' ;
629
647
630
648
$ offset = $ skip !== null ? "OFFSET $ skip " : '' ;
631
649
$ limit = $ limit !== null ? "LIMIT $ limit " : '' ;
632
650
633
- $ orderBy = $ orderBy ? "ORDER BY " . implode ( ' , ' , $ this -> orderByToSort ( $ orderBy )) : '' ;
651
+ $ orderBy = $ orderByStr ? "ORDER BY $ orderByStr " : '' ;
634
652
635
653
$ query = <<<EOT
636
654
SELECT doc
@@ -642,7 +660,7 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
642
660
EOT ;
643
661
$ stmt = $ this ->connection ->prepare ($ query );
644
662
645
- $ stmt ->execute ($ args );
663
+ $ stmt ->execute (array_merge ( $ args, $ orderByArgs ) );
646
664
647
665
while ($ row = $ stmt ->fetch (\PDO ::FETCH_ASSOC )) {
648
666
yield json_decode ($ row ['doc ' ], true );
@@ -658,12 +676,16 @@ public function findDocs(string $collectionName, Filter $filter, int $skip = nul
658
676
$ filterStr = $ filterClause ->clause ();
659
677
$ args = $ filterClause ->args ();
660
678
679
+ $ orderByClause = $ orderBy ? $ this ->orderByProcessor ->process ($ orderBy ) : new OrderByClause (null , []);
680
+ $ orderByStr = $ orderByClause ->clause ();
681
+ $ orderByArgs = $ orderByClause ->args ();
682
+
661
683
$ where = $ filterStr ? "WHERE $ filterStr " : '' ;
662
684
663
685
$ offset = $ skip !== null ? "OFFSET $ skip " : '' ;
664
686
$ limit = $ limit !== null ? "LIMIT $ limit " : '' ;
665
687
666
- $ orderBy = $ orderBy ? "ORDER BY " . implode ( ' , ' , $ this -> orderByToSort ( $ orderBy )) : '' ;
688
+ $ orderBy = $ orderByStr ? "ORDER BY $ orderByStr " : '' ;
667
689
668
690
$ query = <<<EOT
669
691
SELECT id, doc
@@ -675,7 +697,7 @@ public function findDocs(string $collectionName, Filter $filter, int $skip = nul
675
697
EOT ;
676
698
$ stmt = $ this ->connection ->prepare ($ query );
677
699
678
- $ stmt ->execute ($ args );
700
+ $ stmt ->execute (array_merge ( $ args, $ orderByArgs ) );
679
701
680
702
while ($ row = $ stmt ->fetch (\PDO ::FETCH_ASSOC )) {
681
703
yield $ row ['id ' ] => json_decode ($ row ['doc ' ], true );
@@ -688,14 +710,18 @@ public function findPartialDocs(string $collectionName, PartialSelect $partialSe
688
710
$ filterStr = $ filterClause ->clause ();
689
711
$ args = $ filterClause ->args ();
690
712
713
+ $ orderByClause = $ orderBy ? $ this ->orderByProcessor ->process ($ orderBy ) : new OrderByClause (null , []);
714
+ $ orderByStr = $ orderByClause ->clause ();
715
+ $ orderByArgs = $ orderByClause ->args ();
716
+
691
717
$ select = $ this ->makeSelect ($ partialSelect );
692
718
693
719
$ where = $ filterStr ? "WHERE $ filterStr " : '' ;
694
720
695
721
$ offset = $ skip !== null ? "OFFSET $ skip " : '' ;
696
722
$ limit = $ limit !== null ? "LIMIT $ limit " : '' ;
697
723
698
- $ orderBy = $ orderBy ? "ORDER BY " . implode ( ' , ' , $ this -> orderByToSort ( $ orderBy )) : '' ;
724
+ $ orderBy = $ orderByStr ? "ORDER BY $ orderByStr " : '' ;
699
725
700
726
$ query = <<<EOT
701
727
SELECT $ select
@@ -708,7 +734,7 @@ public function findPartialDocs(string $collectionName, PartialSelect $partialSe
708
734
709
735
$ stmt = $ this ->connection ->prepare ($ query );
710
736
711
- $ stmt ->execute ($ args );
737
+ $ stmt ->execute (array_merge ( $ args, $ orderByArgs ) );
712
738
713
739
while ($ row = $ stmt ->fetch (\PDO ::FETCH_ASSOC )) {
714
740
yield $ row [self ::PARTIAL_SELECT_DOC_ID ] => $ this ->transformPartialDoc ($ partialSelect , $ row );
@@ -870,28 +896,6 @@ private function transformPartialDoc(PartialSelect $partialSelect, array $select
870
896
return $ partialDoc ;
871
897
}
872
898
873
- private function orderByToSort (DocumentStore \OrderBy \OrderBy $ orderBy ): array
874
- {
875
- $ sort = [];
876
-
877
- if ($ orderBy instanceof DocumentStore \OrderBy \AndOrder) {
878
- /** @var DocumentStore\OrderBy\Asc|DocumentStore\OrderBy\Desc $orderByA */
879
- $ orderByA = $ orderBy ->a ();
880
- $ direction = $ orderByA instanceof DocumentStore \OrderBy \Asc ? 'ASC ' : 'DESC ' ;
881
- $ prop = $ this ->propToJsonPath ($ orderByA ->prop ());
882
- $ sort [] = "{$ prop } $ direction " ;
883
-
884
- $ sortB = $ this ->orderByToSort ($ orderBy ->b ());
885
-
886
- return array_merge ($ sort , $ sortB );
887
- }
888
-
889
- /** @var DocumentStore\OrderBy\Asc|DocumentStore\OrderBy\Desc $orderBy */
890
- $ direction = $ orderBy instanceof DocumentStore \OrderBy \Asc ? 'ASC ' : 'DESC ' ;
891
- $ prop = $ this ->propToJsonPath ($ orderBy ->prop ());
892
- return ["{$ prop } $ direction " ];
893
- }
894
-
895
899
private function indexToSqlCmd (Index $ index , string $ collectionName ): string
896
900
{
897
901
if ($ index instanceof DocumentStore \FieldIndex) {
0 commit comments