|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cycle ORM package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Cycle\Database\Query; |
|
13
|
|
|
|
|
14
|
|
|
use Cycle\Database\Driver\DriverInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Initiates active queries. |
|
18
|
|
|
*/ |
|
19
|
|
|
final class QueryBuilder implements BuilderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
private DriverInterface $driver; |
|
22
|
|
|
|
|
23
|
76 |
|
public function __construct( |
|
24
|
|
|
private SelectQuery $selectQuery, |
|
25
|
|
|
private InsertQuery $insertQuery, |
|
26
|
|
|
private UpsertQuery $upsertQuery, |
|
27
|
|
|
private UpdateQuery $updateQuery, |
|
28
|
|
|
private DeleteQuery $deleteQuery, |
|
29
|
76 |
|
) {} |
|
30
|
|
|
|
|
31
|
76 |
|
public static function defaultBuilder(): self |
|
32
|
|
|
{ |
|
33
|
76 |
|
return new self( |
|
34
|
76 |
|
new SelectQuery(), |
|
35
|
|
|
new InsertQuery(), |
|
36
|
76 |
|
new UpsertQuery(), |
|
37
|
|
|
new UpdateQuery(), |
|
38
|
|
|
new DeleteQuery(), |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
480 |
|
public function withDriver(DriverInterface $driver): BuilderInterface |
|
43
|
|
|
{ |
|
44
|
|
|
$builder = clone $this; |
|
45
|
|
|
$builder->driver = $driver; |
|
46
|
480 |
|
|
|
47
|
|
|
return $builder; |
|
48
|
480 |
|
} |
|
49
|
422 |
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get InsertQuery builder with driver specific query compiler. |
|
52
|
480 |
|
*/ |
|
53
|
|
|
public function insertQuery( |
|
54
|
|
|
string $prefix, |
|
55
|
|
|
?string $table = null, |
|
56
|
|
|
): InsertQuery { |
|
57
|
|
|
$insert = $this->insertQuery->withDriver($this->driver, $prefix); |
|
58
|
1896 |
|
|
|
59
|
|
|
if ($table !== null) { |
|
60
|
|
|
$insert->into($table); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1896 |
|
return $insert; |
|
64
|
|
|
} |
|
65
|
1896 |
|
|
|
66
|
1376 |
|
/** |
|
67
|
|
|
* Get UpsertQuery builder with driver specific query compiler. |
|
68
|
|
|
*/ |
|
69
|
1896 |
|
public function upsertQuery( |
|
70
|
|
|
string $prefix, |
|
71
|
|
|
?string $table = null, |
|
72
|
80 |
|
): UpsertQuery { |
|
73
|
|
|
$upsert = $this->upsertQuery->withDriver($this->driver, $prefix); |
|
74
|
|
|
|
|
75
|
|
|
if ($table !== null) { |
|
76
|
|
|
$upsert->into($table); |
|
77
|
80 |
|
} |
|
78
|
|
|
|
|
79
|
80 |
|
return $upsert; |
|
80
|
48 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
80 |
|
* Get SelectQuery builder with driver specific query compiler. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function selectQuery( |
|
86
|
|
|
string $prefix, |
|
87
|
|
|
array $from = [], |
|
88
|
|
|
array $columns = [], |
|
89
|
128 |
|
): SelectQuery { |
|
90
|
|
|
$select = $this->selectQuery->withDriver($this->driver, $prefix); |
|
91
|
|
|
|
|
92
|
|
|
if ($columns === []) { |
|
93
|
|
|
$columns = ['*']; |
|
94
|
|
|
} |
|
95
|
128 |
|
|
|
96
|
|
|
return $select->from($from)->columns($columns); |
|
97
|
128 |
|
} |
|
98
|
88 |
|
|
|
99
|
|
|
public function deleteQuery( |
|
100
|
|
|
string $prefix, |
|
101
|
128 |
|
?string $from = null, |
|
102
|
|
|
array $where = [], |
|
103
|
|
|
): DeleteQuery { |
|
104
|
38 |
|
$delete = $this->deleteQuery->withDriver($this->driver, $prefix); |
|
105
|
|
|
|
|
106
|
38 |
|
if ($from !== null) { |
|
107
|
38 |
|
$delete->from($from); |
|
108
|
38 |
|
} |
|
109
|
38 |
|
|
|
110
|
38 |
|
return $delete->where($where); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get UpdateQuery builder with driver specific query compiler. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function updateQuery( |
|
117
|
|
|
string $prefix, |
|
118
|
|
|
?string $table = null, |
|
119
|
|
|
array $where = [], |
|
120
|
|
|
array $values = [], |
|
121
|
|
|
): UpdateQuery { |
|
122
|
|
|
$update = $this->updateQuery->withDriver($this->driver, $prefix); |
|
123
|
|
|
|
|
124
|
|
|
if ($table !== null) { |
|
125
|
|
|
$update->in($table); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $update->where($where)->values($values); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|