Skip to content

Commit c652121

Browse files
authored
refactor query builer (norbybaru#9)
1 parent 07838da commit c652121

File tree

7 files changed

+265
-283
lines changed

7 files changed

+265
-283
lines changed

src/Builder/Builder.php

Lines changed: 194 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace NorbyBaru\AwsTimestream\Builder;
44

5+
use Closure;
6+
use Illuminate\Support\Arr;
57
use Illuminate\Support\Str;
68
use NorbyBaru\AwsTimestream\Concerns\BuildersConcern;
79
use NorbyBaru\AwsTimestream\Contract\QueryBuilderContract;
@@ -20,112 +22,246 @@ abstract class Builder implements QueryBuilderContract
2022
protected string $limitByQuery = '';
2123
protected array $withQueries = [];
2224

23-
public function getDatabase(): ?string
25+
public function selectRaw(string $statement): self
2426
{
25-
return $this->database;
26-
}
27+
$this->selectStatement = $statement;
2728

28-
public function getTable(): ?string
29-
{
30-
return $this->table;
29+
return $this;
3130
}
3231

33-
public function getConnection(): string
32+
public function select(string $columns): self
3433
{
35-
return '"' . $this->getDatabase() . '"."' . $this->getTable() . '"';
34+
$this->selectStatement = sprintf('SELECT %s', $columns);
35+
36+
return $this;
3637
}
3738

38-
public function getSql(): string
39+
public function from(string $database, string $table, string $alias = null): self
3940
{
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;
4151
}
4252

43-
public function toSql(): string
53+
public function fromRaw(string $statement): self
4454
{
45-
return $this->getQueryString();
55+
$this->fromQuery = $statement;
56+
57+
return $this;
4658
}
4759

48-
public function getFromQuery(): string
60+
public function orderBy(string $column, string $direction = 'asc'): self
4961
{
50-
return $this->fromQuery;
62+
$this->orderByQuery = sprintf('ORDER BY %s %s', $column, $direction);
63+
64+
return $this;
5165
}
5266

53-
public function getSelectStatement(): string
67+
public function groupBy($args): self
5468
{
55-
return $this->selectStatement;
69+
$columns = func_get_args();
70+
$this->groupByQuery = sprintf('GROUP BY %s', implode(', ', $columns));
71+
72+
return $this;
5673
}
5774

58-
public function getWhereQuery(): string
75+
public function where(string $column, $value, string $operator = '=', string $boolean = 'and', bool $ago = false): self
5976
{
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;
61113
}
62114

63-
public function getOrderByQuery(): string
115+
public function whereAgo(string $column, $value, string $operator = '=', string $boolean = 'and'): self
64116
{
65-
return $this->orderByQuery;
117+
return $this->where($column, $value, $operator, $boolean, true);
66118
}
67119

68-
public function getGroupByQuery(): string
120+
public function andWhere(string $column, $value, string $operator = '='): self
69121
{
70-
return $this->groupByQuery;
122+
return $this->where($column, $value, $operator);
71123
}
72124

73-
public function getLimitByQuery(): string
125+
public function whereIn(string $column, array|Closure $values, string $boolean = 'and', $not = false): self
74126
{
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;
76170
}
77171

78-
public function getWithQueries(): array
172+
public function whereNotIn(string $column, array|Closure $values, string $boolean = 'and'): self
79173
{
80-
return $this->withQueries;
174+
return $this->whereIn($column, $values, $boolean, true);
81175
}
82176

83-
public function getQueryString(): string
177+
public function whereBetween(string $column, array $values, $boolean = 'and', $not = false): self
84178
{
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;
92181
}
93182

94-
if ($this->getFromQuery()) {
95-
$queryString = $queryString
96-
->append(' ')
97-
->append($this->getFromQuery());
98-
}
183+
$query = Str::of($this->whereQuery);
99184

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+
);
104191
}
105192

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;
110223
}
111224

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+
);
116233
}
117234

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+
}
122242
}
123243

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);
125252
}
126253

127-
private function strTrimFrom(string $string, string $part): string
254+
public function limitBy(int $limit): self
128255
{
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;
130266
}
131267
}

0 commit comments

Comments
 (0)