|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Inji\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Inji\App; |
|
6
|
|
|
use Inji\Tools; |
|
7
|
|
|
|
|
8
|
|
|
class Builder { |
|
9
|
|
|
public $connection = 'default'; |
|
10
|
|
|
public $modelName = ''; |
|
11
|
|
|
public $whereArray = []; |
|
12
|
|
|
public $dbOptions = []; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Inji\App |
|
15
|
|
|
*/ |
|
16
|
|
|
public $app; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(string $modelName, App $app = null) { |
|
19
|
|
|
$this->modelName = $modelName; |
|
20
|
|
|
if (is_null($app)) { |
|
21
|
|
|
$this->app = App::$primary; |
|
22
|
|
|
} else { |
|
23
|
|
|
$this->app = $app; |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function connection($connection = 'default') { |
|
28
|
|
|
$this->connection = $connection; |
|
29
|
|
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getConnection() { |
|
33
|
|
|
return is_string($this->connection) ? $this->app->db($this->connection) : $this->connection; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function model(string $modelName) { |
|
37
|
|
|
$this->modelName = $modelName; |
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
View Code Duplication |
public function where($col, $value = true, $comparision = '=', $concatenation = 'AND') { |
|
42
|
|
|
if (is_array($col) && !Tools::isAssoc($col)) { |
|
43
|
|
|
$this->whereArray[] = $col; |
|
44
|
|
|
} else { |
|
45
|
|
|
$this->whereArray[] = [$col, $value, $comparision, $concatenation]; |
|
46
|
|
|
} |
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return \Inji\Db\DriverQuery |
|
52
|
|
|
*/ |
|
53
|
|
|
public function createQuery() { |
|
54
|
|
|
/** |
|
55
|
|
|
* @var \Inji\Db\DriverQuery |
|
56
|
|
|
*/ |
|
57
|
|
|
$query = $this->getConnection()->newQuery(); |
|
58
|
|
|
foreach ($this->dbOptions as $dbOption => $value) { |
|
59
|
|
|
$query->setDbOption($dbOption, $value); |
|
60
|
|
|
} |
|
61
|
|
|
if ($this->whereArray) { |
|
62
|
|
|
$query->where($this->whereArray); |
|
63
|
|
|
} |
|
64
|
|
|
$query->setTable($this->modelName::table()); |
|
|
|
|
|
|
65
|
|
|
return $query; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array $options |
|
70
|
|
|
* @return bool||\Inji\Model |
|
|
|
|
|
|
71
|
|
|
*/ |
|
72
|
|
|
public function get($options = []) { |
|
73
|
|
|
$query = $this->createQuery(); |
|
74
|
|
|
$result = $query->select(); |
|
75
|
|
|
if (!$result) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
return $result->fetch(empty($options['array']) ? $this->modelName : ''); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param array $options |
|
83
|
|
|
* @return bool||\Inji\Model |
|
|
|
|
|
|
84
|
|
|
*/ |
|
85
|
|
|
public function getList($options = []) { |
|
86
|
|
|
$query = $this->createQuery(); |
|
87
|
|
|
$result = $query->select(); |
|
88
|
|
|
if (!$result) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
return $result->fetchAll(empty($options['array']) ? $this->modelName : '', $this->modelName::index()); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function insert($values) { |
|
95
|
|
|
$query = $this->createQuery(); |
|
96
|
|
|
return $query->insert('', $values); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function update($values) { |
|
100
|
|
|
$query = $this->createQuery(); |
|
101
|
|
|
return $query->update('', $values); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function delete() { |
|
105
|
|
|
$query = $this->createQuery(); |
|
106
|
|
|
return $query->delete(''); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function setDbOption($optionName, $value) { |
|
110
|
|
|
$this->dbOptions[$optionName] = $value; |
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
} |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.