5
5
namespace MakinaCorpus \QueryBuilder \Platform \Writer ;
6
6
7
7
use MakinaCorpus \QueryBuilder \Error \UnsupportedFeatureError ;
8
+ use MakinaCorpus \QueryBuilder \Expression ;
8
9
use MakinaCorpus \QueryBuilder \Expression \Aggregate ;
9
10
use MakinaCorpus \QueryBuilder \Expression \Cast ;
10
11
use MakinaCorpus \QueryBuilder \Expression \Concat ;
11
12
use MakinaCorpus \QueryBuilder \Expression \CurrentTimestamp ;
12
13
use MakinaCorpus \QueryBuilder \Expression \DateAdd ;
13
14
use MakinaCorpus \QueryBuilder \Expression \DateInterval ;
15
+ use MakinaCorpus \QueryBuilder \Expression \DateIntervalUnit ;
14
16
use MakinaCorpus \QueryBuilder \Expression \DateSub ;
15
17
use MakinaCorpus \QueryBuilder \Expression \Lpad ;
16
18
use MakinaCorpus \QueryBuilder \Expression \Random ;
17
19
use MakinaCorpus \QueryBuilder \Expression \StringHash ;
18
20
use MakinaCorpus \QueryBuilder \Expression \TableName ;
19
- use MakinaCorpus \QueryBuilder \Expression \Value ;
20
21
use MakinaCorpus \QueryBuilder \Writer \Writer ;
21
22
use MakinaCorpus \QueryBuilder \Writer \WriterContext ;
22
23
@@ -36,6 +37,16 @@ protected function shouldEscapeAggregateFunctionName(): bool
36
37
return false ;
37
38
}
38
39
40
+ /**
41
+ * This is nasty, but we don't what the user will want, just cast dates
42
+ * to the maximum extent possible.
43
+ */
44
+ #[\Override]
45
+ protected function getDateTimeCastType (): string
46
+ {
47
+ return 'datetime2 ' ;
48
+ }
49
+
39
50
#[\Override]
40
51
protected function formatCurrentTimestamp (CurrentTimestamp $ expression , WriterContext $ context ): string
41
52
{
@@ -109,22 +120,33 @@ protected function formatStringHash(StringHash $expression, WriterContext $conte
109
120
return 'lower(convert(nvarchar(32), hashbytes( ' . $ escapedAlgo . ', ' . $ this ->format ($ value , $ context ) . '), 2)) ' ;
110
121
}
111
122
123
+ protected function formatDateAddRecursion (Expression $ date , array $ values , WriterContext $ context , bool $ negate = false ): string
124
+ {
125
+ if (empty ($ values )) {
126
+ return $ this ->format ($ this ->toDate ($ date , $ context ), $ context );
127
+ }
128
+
129
+ $ unit = \array_shift ($ values );
130
+ \assert ($ unit instanceof DateIntervalUnit);
131
+
132
+ $ delta = $ this ->format ($ this ->toInt ($ unit ->getValue (), $ context ), $ context );
133
+ if ($ negate ) {
134
+ $ delta = '0 - ' . $ delta ;
135
+ }
136
+
137
+ return 'dateadd( ' . $ unit ->getUnit () . ', ' . $ delta . ', ' . $ this ->formatDateAddRecursion ($ date , $ values , $ context , $ negate ) . ') ' ;
138
+ }
139
+
112
140
#[\Override]
113
141
protected function formatDateAdd (DateAdd $ expression , WriterContext $ context ): string
114
142
{
115
143
$ interval = $ expression ->getInterval ();
116
144
117
145
if ($ interval instanceof DateInterval) {
118
- $ ret = $ this ->format ($ expression ->getDate (), $ context );
119
-
120
- foreach ($ interval ->getValues () as $ unit ) {
121
- $ ret = 'dateadd( ' . $ unit ->getUnit () . ', ' . $ this ->format ($ unit ->getValue (), $ context ) . ', ' . $ ret . ') ' ;
122
- }
123
- } else {
124
- throw new UnsupportedFeatureError ("SQLServer does not support DATEADD(expr,expr). " );
146
+ return $ this ->formatDateAddRecursion ($ expression ->getDate (), $ interval ->getValues (), $ context , false );
125
147
}
126
148
127
- return $ ret ;
149
+ throw new UnsupportedFeatureError ( " SQLServer does not support DATEADD(expr,expr). " ) ;
128
150
}
129
151
130
152
#[\Override]
@@ -133,16 +155,10 @@ protected function formatDateSub(DateSub $expression, WriterContext $context): s
133
155
$ interval = $ expression ->getInterval ();
134
156
135
157
if ($ interval instanceof DateInterval) {
136
- $ ret = $ this ->format ($ expression ->getDate (), $ context );
137
-
138
- foreach ($ interval ->getValues () as $ unit ) {
139
- $ ret = 'dateadd( ' . $ unit ->getUnit () . ', 0 - ' . $ this ->format ($ unit ->getValue (), $ context ) . ', ' . $ ret . ') ' ;
140
- }
141
- } else {
142
- throw new UnsupportedFeatureError ("SQLServer does not support DATEADD(expr,expr). " );
158
+ return $ this ->formatDateAddRecursion ($ expression ->getDate (), $ interval ->getValues (), $ context , true );
143
159
}
144
160
145
- return $ ret ;
161
+ throw new UnsupportedFeatureError ( " SQLServer does not support DATEADD(expr,expr). " ) ;
146
162
}
147
163
148
164
#[\Override]
0 commit comments