@@ -31,10 +31,10 @@ And you can also create new powerful queries:
31
31
``` php
32
32
// Aggregate multiple statistics with one query for dashboards:
33
33
Movie::select([
34
- new CountFilter(new Equal('released', new Number (2021))),
35
- new CountFilter(new Equal('released', new Number (2022))),
36
- new CountFilter(new Equal('genre_id ', new Number(12 ))),
37
- new CountFilter(new Equal('genre_id ', new Number(35 ))),
34
+ new CountFilter(new Equal('released', new Value (2021))),
35
+ new CountFilter(new Equal('released', new Value (2022))),
36
+ new CountFilter(new Equal('genre ', new Value('Drama' ))),
37
+ new CountFilter(new Equal('genre ', new Value('Comedy' ))),
38
38
])->where('streamingservice', 'netflix');
39
39
```
40
40
@@ -65,25 +65,26 @@ Whenever an expression class needs a `string|Expression` parameter, you can pass
65
65
66
66
As stated before, an expression is always a column name.
67
67
But if you want to e.g. do an equality check, you may want to compare something to a specific value.
68
- That's where you should use the ` Number ` class.
68
+ That's where you should use the ` Value ` class.
69
+ Its values will always be automatically escaped within the query.
69
70
70
71
``` php
71
- use Tpetry\QueryExpressions\Value\Number ;
72
+ use Tpetry\QueryExpressions\Value\Value ;
72
73
73
- new Number(44 );
74
- new Number(3.1415 );
74
+ new Value(42 );
75
+ new Value("Robert'); DROP TABLE students;--" );
75
76
```
76
77
77
78
> ** Note**
78
- > The ` Number ` class in isolation is not that usefull.
79
+ > The ` Value ` class in isolation is not that usefull.
79
80
> But it will be used more in the next examples.
80
81
81
82
#### Alias
82
83
83
84
``` php
84
85
use Illuminate\Contracts\Database\Query\Expression;
85
86
use Tpetry\QueryExpressions\Language\Alias;
86
- use Tpetry\QueryExpressions\Value\Number ;
87
+ use Tpetry\QueryExpressions\Value\Value ;
87
88
88
89
new Alias(string|Expression $expression, string $name)
89
90
@@ -106,7 +107,7 @@ use Illuminate\Contracts\Database\Query\Expression;
106
107
use Tpetry\QueryExpressions\Operator\Arithmetic\{
107
108
Add, Divide, Modulo, Multiply, Power, Subtract,
108
109
};
109
- use Tpetry\QueryExpressions\Operator\Value\Number ;
110
+ use Tpetry\QueryExpressions\Operator\Value\Value ;
110
111
111
112
new Add(string|Expression $value1, string|Expression $value2);
112
113
new Divide(string|Expression $value1, string|Expression $value2);
@@ -117,14 +118,14 @@ new Subtract(string|Expression $value1, string|Expression $value2);
117
118
118
119
// UPDATE user_quotas SET credits = credits - 15 WHERE id = 1985
119
120
$quota->update([
120
- 'credits' => new Subtract('credits', new Number (15)),
121
+ 'credits' => new Subtract('credits', new Value (15)),
121
122
]);
122
123
123
124
// SELECT id, name, (price - discount) * 0.2 AS vat FROM products
124
125
Product::select([
125
126
'id',
126
127
'name',
127
- new Alias(new Multiply(new Subtract('price', 'discount'), new Number (0.2)), 'vat')
128
+ new Alias(new Multiply(new Subtract('price', 'discount'), new Value (0.2)), 'vat')
128
129
])->get();
129
130
```
130
131
@@ -135,7 +136,7 @@ use Illuminate\Contracts\Database\Query\Expression;
135
136
use Tpetry\QueryExpressions\Operator\Bitwise\{
136
137
BitAnd, BitNot, BitOr, BitXor, ShiftLeft, ShiftRight,
137
138
};
138
- use Tpetry\QueryExpressions\Operator\Value\Number ;
139
+ use Tpetry\QueryExpressions\Operator\Value\Value ;
139
140
140
141
new BitAnd(string|Expression $value1, string|Expression $value2);
141
142
new BitNot(string|Expression $value);
@@ -145,7 +146,7 @@ new ShiftLeft(string|Expression $value, string|Expression $times);
145
146
new ShiftRight(string|Expression $value, string|Expression $times);
146
147
147
148
// SELECT * FROM users WHERE (acl & 0x8000) = 0x8000
148
- User::where(new BitAnd('acl', 0x8000), 0x8000)
149
+ User::where(new BitAnd('acl', new Value( 0x8000) ), 0x8000)
149
150
->get();
150
151
```
151
152
@@ -159,7 +160,6 @@ use Tpetry\QueryExpressions\Operator\Comparison\{
159
160
use Tpetry\QueryExpressions\Operator\Logical\{
160
161
CondAnd, CondNot, CondOr, CondXor
161
162
};
162
- use Tpetry\QueryExpressions\Operator\Value\Number;
163
163
164
164
new Between(string|Expression $value, string|Expression $min, string|Expression $max);
165
165
new DistinctFrom(string|Expression $value1, string|Expression $value2);
@@ -193,7 +193,7 @@ use Illuminate\Contracts\Database\Query\Expression;
193
193
use Tpetry\QueryExpressions\Function\Aggregate\{
194
194
Avg, Count, CountFilter, Max, Min, Sum, SumFilter,
195
195
};
196
- use Tpetry\QueryExpressions\Operator\Value\Number ;
196
+ use Tpetry\QueryExpressions\Operator\Value\Value ;
197
197
198
198
new Avg(string|Expression $value);
199
199
new Count(string|Expression $value);
@@ -214,15 +214,15 @@ BlogVisit::select([
214
214
// SELECT
215
215
// COUNT(*) FILTER (WHERE (released = 2021)) AS released_2021,
216
216
// COUNT(*) FILTER (WHERE (released = 2022)) AS released_20212,
217
- // COUNT(*) FILTER (WHERE (genre_id = 12 )) AS genre_12 ,
218
- // COUNT(*) FILTER (WHERE (genre_id = 35 )) AS genre_35
217
+ // COUNT(*) FILTER (WHERE (genre = 'Drama' )) AS genre_drama ,
218
+ // COUNT(*) FILTER (WHERE (genre = 'Comedy' )) AS genre_comedy
219
219
// FROM movies
220
220
// WHERE streamingservice = 'netflix'
221
221
Movie::select([
222
- new Alias(new CountFilter(new Equal('released', new Number (2021))), 'released_2021'),
223
- new Alias(new CountFilter(new Equal('released', new Number (2022))), 'released_2022'),
224
- new Alias(new CountFilter(new Equal('genre_id ', new Number(12 ))), 'genre_12 '),
225
- new Alias(new CountFilter(new Equal('genre_id ', new Number(35 ))), 'genre_35 '),
222
+ new Alias(new CountFilter(new Equal('released', new Value (2021))), 'released_2021'),
223
+ new Alias(new CountFilter(new Equal('released', new Value (2022))), 'released_2022'),
224
+ new Alias(new CountFilter(new Equal('genre ', new Value('Drama' ))), 'genre_drama '),
225
+ new Alias(new CountFilter(new Equal('genre ', new Value('Comedy' ))), 'genre_comedy '),
226
226
])
227
227
->where('streamingservice', 'netflix')
228
228
->get();
0 commit comments