1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SQL Builder |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 22/12/2017 |
6
|
|
|
* Time: 4:16 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Database\SQL; |
10
|
|
|
|
11
|
|
|
use Carno\Database\Contracts\Executable; |
12
|
|
|
use Carno\Database\SQL\Actions\Aggregate; |
13
|
|
|
use Carno\Database\SQL\Actions\Delete; |
14
|
|
|
use Carno\Database\SQL\Actions\Getter; |
15
|
|
|
use Carno\Database\SQL\Actions\Insert; |
16
|
|
|
use Carno\Database\SQL\Actions\Observer; |
17
|
|
|
use Carno\Database\SQL\Actions\Update; |
18
|
|
|
use Carno\Database\SQL\Builders\Data; |
19
|
|
|
use Carno\Database\SQL\Builders\Group; |
20
|
|
|
use Carno\Database\SQL\Builders\Limit; |
21
|
|
|
use Carno\Database\SQL\Builders\Order; |
22
|
|
|
use Carno\Database\SQL\Builders\Params; |
23
|
|
|
use Carno\Database\SQL\Builders\Select; |
24
|
|
|
use Carno\Database\SQL\Builders\Where; |
25
|
|
|
use Carno\Database\SQL\Exception\UnknownActionException; |
26
|
|
|
use Carno\Database\SQL\Merging\Related; |
27
|
|
|
use Carno\Database\SQL\Paginator\Paged; |
28
|
|
|
|
29
|
|
|
class Builder |
30
|
|
|
{ |
31
|
|
|
use Observer, Params; |
32
|
|
|
use Select, Data, Where, Group, Order, Limit; |
33
|
|
|
use Insert, Update, Delete; |
34
|
|
|
use Getter, Aggregate; |
35
|
|
|
use Related, Paged; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $table = 'table'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Executable |
44
|
|
|
*/ |
45
|
|
|
protected $executor = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Builder constructor. |
49
|
|
|
* @param string $table |
50
|
|
|
* @param Executable $executor |
51
|
|
|
* @param array $observers |
52
|
|
|
*/ |
53
|
|
|
public function __construct(string $table, Executable $executor, array $observers = []) |
54
|
|
|
{ |
55
|
|
|
$this->table = $table; |
56
|
|
|
$this->executor = $executor; |
57
|
|
|
|
58
|
|
|
foreach ($observers as $action => $programs) { |
59
|
|
|
$this->actWatching($action, ...$programs); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $table |
65
|
|
|
* @return Builder |
66
|
|
|
*/ |
67
|
|
|
protected function new(string $table = null) : Builder |
68
|
|
|
{ |
69
|
|
|
return new static($table ?? $this->table, $this->executor); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $sql |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
protected function exec(string $sql) |
77
|
|
|
{ |
78
|
|
|
return $this->executor->execute($sql, $this->params()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $action |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function sql(int $action) : string |
86
|
|
|
{ |
87
|
|
|
$sql = null; |
88
|
|
|
|
89
|
|
|
switch ($action) { |
90
|
|
|
case Action::INSERT: |
91
|
|
|
$sql = sprintf( |
92
|
|
|
'INSERT INTO `%s` %s', |
93
|
|
|
$this->table, |
94
|
|
|
$this->gData() |
95
|
|
|
); |
96
|
|
|
break; |
97
|
|
|
case Action::SELECT: |
98
|
|
|
$sql = sprintf( |
99
|
|
|
'SELECT %s FROM `%s`%s%s%s%s', |
100
|
|
|
$this->gSelect(), |
101
|
|
|
$this->table, |
102
|
|
|
$this->gWheres(), |
103
|
|
|
$this->gGroups(), |
104
|
|
|
$this->gOrders(), |
105
|
|
|
$this->gLimit() |
106
|
|
|
); |
107
|
|
|
break; |
108
|
|
|
case Action::UPDATE: |
109
|
|
|
$sql = sprintf( |
110
|
|
|
'UPDATE `%s` %s%s%s%s', |
111
|
|
|
$this->table, |
112
|
|
|
$this->gData(), |
113
|
|
|
$this->gWheres(), |
114
|
|
|
$this->gOrders(), |
115
|
|
|
$this->gLimit() |
116
|
|
|
); |
117
|
|
|
break; |
118
|
|
|
case Action::DELETE: |
119
|
|
|
$sql = sprintf( |
120
|
|
|
'DELETE FROM `%s`%s%s%s', |
121
|
|
|
$this->table, |
122
|
|
|
$this->gWheres(), |
123
|
|
|
$this->gOrders(), |
124
|
|
|
$this->gLimit() |
125
|
|
|
); |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($sql) { |
130
|
|
|
return rtrim($sql); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
throw new UnknownActionException; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|