1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2021 Aleksandar Panic |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Sql\Query; |
19
|
|
|
|
20
|
|
|
use ArekX\PQL\Query; |
21
|
|
|
use ArekX\PQL\Sql\Query\Traits\ConfigureTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Represents a query containing raw query or a value. |
25
|
|
|
*/ |
26
|
|
|
class Raw extends Query |
27
|
|
|
{ |
28
|
|
|
use ConfigureTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create an instance of raw query from params and query. |
32
|
|
|
* |
33
|
|
|
* Query value can be a string or a different value |
34
|
|
|
* depending on the driver in use. |
35
|
|
|
* |
36
|
|
|
* @param mixed $query Driver dependent query |
37
|
|
|
* @param array|null $params Params to be bound to the query. |
38
|
|
|
* @return static |
39
|
|
|
*/ |
40
|
53 |
|
public static function from(mixed $query, array $params = null): static |
41
|
|
|
{ |
42
|
53 |
|
return static::create()->query($query)->params($params); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set a query to be used. |
47
|
|
|
* |
48
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
49
|
|
|
* and should not be used to pass values from the user input to it. If you need to |
50
|
|
|
* pass user value then define the parameters in this query and use params() function. |
51
|
|
|
* |
52
|
|
|
* @param mixed $query Driver dependent query. |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
55 |
|
public function query(mixed $query): static |
56
|
|
|
{ |
57
|
55 |
|
$this->use('query', $query); |
58
|
55 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Define parameters in query to be used. |
63
|
|
|
* |
64
|
|
|
* These are the parameters to be used by this query. |
65
|
|
|
* They will be implemented based on the driver used. |
66
|
|
|
* |
67
|
|
|
* Usual use case for these parameters are SQL parameters |
68
|
|
|
* when running an SQL query. |
69
|
|
|
* |
70
|
|
|
* @param ?array $params |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
54 |
|
public function params(?array $params): static |
74
|
|
|
{ |
75
|
54 |
|
$this->use('params', $params); |
76
|
54 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|