2
2
3
3
namespace NorbyBaru \AwsTimestream \Builder ;
4
4
5
+ use Closure ;
6
+ use Illuminate \Support \Arr ;
5
7
use Illuminate \Support \Str ;
6
8
use NorbyBaru \AwsTimestream \Concerns \BuildersConcern ;
7
9
use NorbyBaru \AwsTimestream \Contract \QueryBuilderContract ;
@@ -20,112 +22,246 @@ abstract class Builder implements QueryBuilderContract
20
22
protected string $ limitByQuery = '' ;
21
23
protected array $ withQueries = [];
22
24
23
- public function getDatabase ( ): ? string
25
+ public function selectRaw ( string $ statement ): self
24
26
{
25
- return $ this ->database ;
26
- }
27
+ $ this ->selectStatement = $ statement ;
27
28
28
- public function getTable (): ?string
29
- {
30
- return $ this ->table ;
29
+ return $ this ;
31
30
}
32
31
33
- public function getConnection ( ): string
32
+ public function select ( string $ columns ): self
34
33
{
35
- return '" ' . $ this ->getDatabase () . '"." ' . $ this ->getTable () . '" ' ;
34
+ $ this ->selectStatement = sprintf ('SELECT %s ' , $ columns );
35
+
36
+ return $ this ;
36
37
}
37
38
38
- public function getSql ( ): string
39
+ public function from ( string $ database , string $ table , string $ alias = null ): self
39
40
{
40
- return $ this ->toSql ();
41
+ $ this ->database = $ database ;
42
+ $ this ->table = $ table ;
43
+
44
+ $ this ->fromQuery = 'FROM " ' . $ database . '"." ' . $ table . '" ' ;
45
+
46
+ if ($ alias ) {
47
+ $ this ->fromQuery = Str::of ($ this ->fromQuery )->append (" {$ alias }" );
48
+ }
49
+
50
+ return $ this ;
41
51
}
42
52
43
- public function toSql ( ): string
53
+ public function fromRaw ( string $ statement ): self
44
54
{
45
- return $ this ->getQueryString ();
55
+ $ this ->fromQuery = $ statement ;
56
+
57
+ return $ this ;
46
58
}
47
59
48
- public function getFromQuery ( ): string
60
+ public function orderBy ( string $ column , string $ direction = ' asc ' ): self
49
61
{
50
- return $ this ->fromQuery ;
62
+ $ this ->orderByQuery = sprintf ('ORDER BY %s %s ' , $ column , $ direction );
63
+
64
+ return $ this ;
51
65
}
52
66
53
- public function getSelectStatement ( ): string
67
+ public function groupBy ( $ args ): self
54
68
{
55
- return $ this ->selectStatement ;
69
+ $ columns = func_get_args ();
70
+ $ this ->groupByQuery = sprintf ('GROUP BY %s ' , implode (', ' , $ columns ));
71
+
72
+ return $ this ;
56
73
}
57
74
58
- public function getWhereQuery ( ): string
75
+ public function where ( string $ column , $ value , string $ operator = ' = ' , string $ boolean = ' and ' , bool $ ago = false ): self
59
76
{
60
- return $ this ->whereQuery ;
77
+ $ query = Str::of ($ this ->whereQuery );
78
+
79
+ $ value = $ value instanceof Closure
80
+ // If the value is a Closure, it means the developer is performing an entire
81
+ ? '( ' . call_user_func ($ value ) . ') '
82
+ : $ value ;
83
+
84
+ if ($ query ->length () == 0 ) {
85
+ $ whereQuery = $ query ->append (
86
+ sprintf ('WHERE %s %s %s ' , $ column , $ operator , $ value )
87
+ );
88
+
89
+ if ($ ago ) {
90
+ $ whereQuery = $ query ->append (
91
+ sprintf ('WHERE %s %s ago(%s) ' , $ column , $ operator , $ value )
92
+ );
93
+ }
94
+
95
+ $ this ->whereQuery = $ whereQuery ;
96
+
97
+ return $ this ;
98
+ }
99
+
100
+ $ whereQuery = $ query ->append (
101
+ sprintf (' %s %s %s %s ' , mb_strtoupper ($ boolean ), $ column , $ operator , $ value )
102
+ );
103
+
104
+ if ($ ago ) {
105
+ $ whereQuery = $ query ->append (
106
+ sprintf (' %s %s %s ago(%s) ' , mb_strtoupper ($ boolean ), $ column , $ operator , $ value )
107
+ );
108
+ }
109
+
110
+ $ this ->whereQuery = $ whereQuery ;
111
+
112
+ return $ this ;
61
113
}
62
114
63
- public function getOrderByQuery ( ): string
115
+ public function whereAgo ( string $ column , $ value , string $ operator = ' = ' , string $ boolean = ' and ' ): self
64
116
{
65
- return $ this ->orderByQuery ;
117
+ return $ this ->where ( $ column , $ value , $ operator , $ boolean , true ) ;
66
118
}
67
119
68
- public function getGroupByQuery ( ): string
120
+ public function andWhere ( string $ column , $ value , string $ operator = ' = ' ): self
69
121
{
70
- return $ this ->groupByQuery ;
122
+ return $ this ->where ( $ column , $ value , $ operator ) ;
71
123
}
72
124
73
- public function getLimitByQuery ( ): string
125
+ public function whereIn ( string $ column , array | Closure $ values , string $ boolean = ' and ' , $ not = false ): self
74
126
{
75
- return $ this ->limitByQuery ;
127
+ if (empty ($ values )) {
128
+ return $ this ;
129
+ }
130
+
131
+ $ query = Str::of ($ this ->whereQuery );
132
+
133
+ if ($ query ->length () == 0 ) {
134
+ $ query = $ query ->append ('WHERE ' );
135
+ } else {
136
+ $ query = $ query ->append (
137
+ sprintf (' %s ' , mb_strtoupper ($ boolean ))
138
+ );
139
+ }
140
+
141
+ $ operator = $ not ? 'NOT IN ' : 'IN ' ;
142
+ $ query = $ query
143
+ ->append (
144
+ sprintf ('%s %s ( ' , $ column , $ operator )
145
+ );
146
+
147
+ if ($ values instanceof Closure) {
148
+ $ query = Str::of ($ query )
149
+ ->append (
150
+ sprintf ('%s ' , trim (call_user_func ($ values )))
151
+ );
152
+ } else {
153
+ $ counter = count ($ values );
154
+
155
+ collect ($ values )->each (function ($ value ) use (&$ counter , &$ query ) {
156
+ $ query = Str::of ($ query )
157
+ ->append (
158
+ sprintf ('%s ' , trim ("' $ value' " ))
159
+ );
160
+ $ counter --;
161
+ if ($ counter !== 0 ) {
162
+ $ query = Str::of ($ query )->append (', ' );
163
+ }
164
+ });
165
+ }
166
+
167
+ $ this ->whereQuery = Str::of ($ query )->append (') ' );
168
+
169
+ return $ this ;
76
170
}
77
171
78
- public function getWithQueries ( ): array
172
+ public function whereNotIn ( string $ column , array | Closure $ values , string $ boolean = ' and ' ): self
79
173
{
80
- return $ this ->withQueries ;
174
+ return $ this ->whereIn ( $ column , $ values , $ boolean , true ) ;
81
175
}
82
176
83
- public function getQueryString ( ): string
177
+ public function whereBetween ( string $ column , array $ values , $ boolean = ' and ' , $ not = false ): self
84
178
{
85
- if ($ this ->getWithQueries ()) {
86
- $ withQueries = 'WITH ' . implode (', ' , $ this ->getWithQueries ());
87
- $ queryString = Str::of ($ withQueries )
88
- ->append (' ' )
89
- ->append ($ this ->getSelectStatement ());
90
- } else {
91
- $ queryString = Str::of ($ this ->getSelectStatement ());
179
+ if (empty ($ values )) {
180
+ return $ this ;
92
181
}
93
182
94
- if ($ this ->getFromQuery ()) {
95
- $ queryString = $ queryString
96
- ->append (' ' )
97
- ->append ($ this ->getFromQuery ());
98
- }
183
+ $ query = Str::of ($ this ->whereQuery );
99
184
100
- if ($ this ->getWhereQuery ()) {
101
- $ queryString = $ queryString
102
- ->append (' ' )
103
- ->append ($ this ->getWhereQuery ());
185
+ if ($ query ->length () == 0 ) {
186
+ $ query = $ query ->append ('WHERE ' );
187
+ } else {
188
+ $ query = $ query ->append (
189
+ sprintf (' %s ' , mb_strtoupper ($ boolean ))
190
+ );
104
191
}
105
192
106
- if ($ this ->getGroupByQuery ()) {
107
- $ queryString = $ queryString
108
- ->append (' ' )
109
- ->append ($ this ->getGroupByQuery ());
193
+ $ type = 'BETWEEN ' ;
194
+ $ operator = $ not ? 'NOT ' : '' ;
195
+ $ operator .= $ type ;
196
+ $ query = $ query
197
+ ->append (
198
+ sprintf ('%s %s ' , $ column , $ operator )
199
+ );
200
+
201
+ [$ firstKey , $ secondKey ] = array_slice (Arr::flatten ($ values ), 0 , 2 );
202
+
203
+ $ this ->whereQuery = Str::of ($ query )
204
+ ->append (sprintf ("%s and %s " , $ firstKey , $ secondKey ));
205
+
206
+ return $ this ;
207
+ }
208
+
209
+ public function whereNotBetween (string $ column , array $ values , $ boolean = 'and ' ): self
210
+ {
211
+ return $ this ->whereBetween ($ column , $ values , $ boolean , true );
212
+ }
213
+
214
+ public function whereNull (string |array $ columns , $ boolean = 'and ' , $ not = false ): self
215
+ {
216
+ $ type = 'NULL ' ;
217
+
218
+ $ operator = $ not ? 'IS NOT ' : 'IS ' ;
219
+ $ operator .= $ type ;
220
+
221
+ if (empty ($ columns )) {
222
+ return $ this ;
110
223
}
111
224
112
- if ($ this ->getOrderByQuery ()) {
113
- $ queryString = $ queryString
114
- ->append (' ' )
115
- ->append ($ this ->getOrderByQuery ());
225
+ $ query = Str::of ($ this ->whereQuery );
226
+
227
+ if ($ query ->length () == 0 ) {
228
+ $ query = $ query ->append ('WHERE ' );
229
+ } else {
230
+ $ query = $ query ->append (
231
+ sprintf (' %s ' , mb_strtoupper ($ boolean ))
232
+ );
116
233
}
117
234
118
- if ($ this ->getLimitByQuery ()) {
119
- $ queryString = $ queryString
120
- ->append (' ' )
121
- ->append ($ this ->getLimitByQuery ());
235
+ $ counter = 0 ;
236
+ foreach (Arr::wrap ($ columns ) as $ column ) {
237
+ $ counter ++;
238
+ $ query = $ query ->append (sprintf ('%s %s ' , $ column , $ operator ));
239
+ if ($ counter < count (Arr::wrap ($ columns ))) {
240
+ $ query = $ query ->append (sprintf (' %s ' , mb_strtoupper ($ boolean )));
241
+ }
122
242
}
123
243
124
- return $ queryString ;
244
+ $ this ->whereQuery = Str::of ($ query );
245
+
246
+ return $ this ;
247
+ }
248
+
249
+ public function whereNotNull (string |array $ columns , $ boolean = 'and ' ): self
250
+ {
251
+ return $ this ->whereNull ($ columns , $ boolean , true );
125
252
}
126
253
127
- private function strTrimFrom ( string $ string , string $ part ): string
254
+ public function limitBy ( int $ limit ): self
128
255
{
129
- return mb_substr ($ string , mb_strlen ($ part ));
256
+ $ this ->limitByQuery = sprintf ('LIMIT %s ' , $ limit );
257
+
258
+ return $ this ;
259
+ }
260
+
261
+ public function withQuery (string $ as , Closure $ callback ): self
262
+ {
263
+ $ this ->withQueries = array_merge ($ this ->withQueries , [$ as . ' AS ( ' . call_user_func ($ callback ) . ') ' ]);
264
+
265
+ return $ this ;
130
266
}
131
267
}
0 commit comments