1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inji\Model; |
4
|
|
|
|
5
|
|
|
use Inji\App; |
6
|
|
|
use Inji\Tools; |
7
|
|
|
|
8
|
|
|
class Builder { |
9
|
|
|
/** |
10
|
|
|
* @var string|\Inji\Db |
11
|
|
|
*/ |
12
|
|
|
public $connection = 'default'; |
13
|
|
|
public $modelName = ''; |
14
|
|
|
public $whereArray = []; |
15
|
|
|
public $dbOptions = []; |
16
|
|
|
public $count = false; |
17
|
|
|
public $colPrefix = ''; |
18
|
|
|
public $order = []; |
19
|
|
|
public $start = 0; |
20
|
|
|
public $limit = 0; |
21
|
|
|
/** |
22
|
|
|
* @var \Inji\App |
23
|
|
|
*/ |
24
|
|
|
public $app; |
25
|
|
|
|
26
|
4 |
|
public function __construct(string $modelName, ?App $app = null) { |
27
|
4 |
|
$this->modelName = $modelName; |
28
|
4 |
|
if (is_null($app)) { |
29
|
4 |
|
$this->app = App::$primary; |
30
|
|
|
} else { |
31
|
4 |
|
$this->app = $app; |
32
|
|
|
} |
33
|
4 |
|
$this->colPrefix($modelName::colPrefix()); |
34
|
4 |
|
} |
35
|
|
|
|
36
|
4 |
|
public function connection($connection = 'default') { |
37
|
4 |
|
$this->connection = $connection; |
38
|
4 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return \Inji\Db |
43
|
|
|
*/ |
44
|
4 |
|
public function getConnection() { |
45
|
4 |
|
return is_string($this->connection) ? $this->app->db($this->connection) : $this->connection; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function model(string $modelName) { |
49
|
|
|
$this->modelName = $modelName; |
50
|
|
|
$this->colPrefix($modelName::colPrefix()); |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
public function where($col, $value = true, $comparision = '=', $concatenation = 'AND') { |
55
|
4 |
|
if (is_array($col) && !Tools::isAssoc($col)) { |
56
|
|
|
$this->whereArray[] = $col; |
57
|
|
|
} else { |
58
|
4 |
|
$this->whereArray[] = [$col, $value, $comparision, $concatenation]; |
59
|
|
|
} |
60
|
4 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function order($order, $type = 'ASC') { |
64
|
|
|
if (is_array($order)) { |
65
|
|
|
$this->order[] = $order; |
66
|
|
|
} else { |
67
|
|
|
$this->order[] = [$order, $type]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function count($col = '*') { |
72
|
|
|
$this->count = $col; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function start($start = 0) { |
76
|
|
|
$this->start = $start; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function limit($limit = 0) { |
80
|
|
|
$this->limit = $limit; |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
public function colPrefix($colPrefix) { |
84
|
4 |
|
$this->colPrefix = $colPrefix; |
85
|
4 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return false|\Inji\Db\DriverQuery |
89
|
|
|
*/ |
90
|
4 |
|
public function createQuery() { |
91
|
4 |
|
$query = $this->getConnection()->newQuery(); |
92
|
|
|
|
93
|
4 |
|
if (!$query) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
4 |
|
$query->colPrefix($this->colPrefix); |
97
|
4 |
|
foreach ($this->dbOptions as $dbOption => $value) { |
98
|
4 |
|
$query->setDbOption($dbOption, $value); |
99
|
|
|
} |
100
|
4 |
|
if ($this->whereArray) { |
101
|
4 |
|
$query->where($this->whereArray); |
102
|
|
|
} |
103
|
4 |
|
$query->setTable($this->modelName::table()); |
104
|
4 |
|
if ($this->order) { |
105
|
|
|
$query->order($this->order); |
106
|
|
|
} |
107
|
4 |
|
$query->start($this->start); |
108
|
4 |
|
if ($this->limit) { |
109
|
|
|
$query->limit($this->limit); |
110
|
|
|
} |
111
|
4 |
|
if ($this->count) { |
112
|
|
|
$query->cols('COUNT(' . $this->count . ') as count'); |
113
|
|
|
} |
114
|
4 |
|
return $query; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $options |
119
|
|
|
* @return bool|\Inji\Model |
120
|
|
|
*/ |
121
|
4 |
|
public function get($options = []) { |
122
|
4 |
|
$query = $this->createQuery(); |
123
|
4 |
|
if (!$query) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
4 |
|
$result = $query->select(); |
127
|
4 |
|
if (!$result) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
4 |
|
if ($this->count) { |
131
|
|
|
$count = $result->fetch(); |
132
|
|
|
return $count ? $count['count'] : 0; |
133
|
|
|
} |
134
|
4 |
|
return $result->fetch(empty($options['array']) ? $this->modelName : ''); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param array $options |
139
|
|
|
* @return bool|\Inji\Model[] |
140
|
|
|
*/ |
141
|
1 |
|
public function getList($options = []) { |
142
|
1 |
|
$query = $this->createQuery(); |
143
|
1 |
|
$result = $query->select(); |
144
|
1 |
|
if (!$result) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
1 |
|
return $result->fetchAll(empty($options['array']) ? $this->modelName : '', $this->modelName::index()); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
public function insert($values) { |
151
|
1 |
|
$query = $this->createQuery(); |
152
|
1 |
|
return $query->insert('', $values); |
153
|
|
|
} |
154
|
|
|
|
155
|
2 |
|
public function update($values) { |
156
|
2 |
|
$query = $this->createQuery(); |
157
|
2 |
|
return $query->update('', $values); |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
public function delete() { |
161
|
1 |
|
$query = $this->createQuery(); |
162
|
1 |
|
return $query->delete(''); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function setDbOptions($options) { |
166
|
|
|
foreach ($options as $optionName => $value) { |
167
|
|
|
$this->setDbOption($optionName, $value); |
168
|
|
|
} |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
4 |
|
public function setDbOption($optionName, $value) { |
173
|
4 |
|
$this->dbOptions[$optionName] = $value; |
174
|
4 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
} |