-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.php
More file actions
163 lines (144 loc) · 2.77 KB
/
Query.php
File metadata and controls
163 lines (144 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Silver\Database;
use Silver\Database\Query\Drop;
use Silver\Database\Parts\Fn;
use Silver\Database\Parts\Column;
abstract class Query extends Db
{
private $bindings = [];
private $sources = [];
use Compiler;
/**
* @param array ...$columns
* @return mixed
*/
public static function select(...$columns): object
{
return self::instance('select', [$columns]);
}
/**
* @param string $column
* @return mixed
*/
public static function count(string $column = 'count'): object
{
return self::select(
Column::ensure(
[
null,
Fn::count(),
$column
]
)
);
}
/**
* @param array ...$columns
* @return mixed
*/
public static function delete(...$columns): object
{
return self::instance('delete', [$columns]);
}
/**
* @param $table
* @param array $updates
* @return mixed
*/
public static function update(string $table, array $updates = []): object
{
return self::instance('update', [$table, $updates]);
}
/**
* @param $table
* @param null $data
* @return mixed
*/
public static function insert(string $table, array $data = null): object
{
return self::instance('insert', [$table, $data]);
}
/**
* @param $table
* @param $cb
* @return mixed
*/
public static function create(string $table, $cb): object
{
return self::instance('create', [$table, $cb]);
}
/**
* @param $table
* @return mixed
*/
public static function drop(string $table): object
{
return self::instance('drop', [$table]);
}
/**
* @param $table
* @param null $cb
* @return mixed
*/
public static function alter(string $table, $cb = null): object
{
return self::instance('alter', [$table, $cb]);
}
/**
* @param $type
* @param array $args
* @return mixed
*/
protected static function instance(string $type, array $args = []): object
{
$class = 'Silver\\Database\\Query\\' . ucfirst($type);
return new $class(...$args);
}
/**
* @param $value
*/
public function bind($value): void
{
if (is_array($value)) {
$this->bindings = array_merge($this->bindings, $value);
} else {
$this->bindings[] = $value;
}
}
/**
* @return array
*/
public function getBindings(): array
{
return $this->bindings;
}
/**
*
*/
public function clearBindings(): void
{
$this->bindings = [];
}
public function addSource(object $source): void
{
$this->sources[$source->name()] = $source;
}
public function getSource(string $name): ?object
{
if (isset($this->sources[$name])) {
return $this->sources[$name];
}
return null;
}
public function getSourceByModel(object $class): ?object
{
foreach($this->sources as $source) {
if ($source instanceof \Silver\Database\Source\Model) {
if ($source->model() == $class) {
return $source;
}
}
}
return null;
}
}