-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.php
183 lines (163 loc) · 4.53 KB
/
DB.php
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Qubus\Dbal
*
* @link https://github.com/QubusPHP/dbal
* @copyright 2020
* @author Joshua Parker <joshua@joshuaparker.dev>
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Dbal;
use Qubus\Dbal\Collector\Delete;
use Qubus\Dbal\Collector\Insert;
use Qubus\Dbal\Collector\Select;
use Qubus\Dbal\Collector\Update;
use Qubus\Exception\Exception;
use Qubus\ValueObjects\DateTime\DateTime;
use Qubus\ValueObjects\DateTime\Exception\InvalidDateException;
use function func_get_args;
/**
* @method Select select() Create select query.
* @method Query query() Returns a query object.
* @method Select selectArray() Creates a select object.
* @method Update update() Creates and update object.
* @method Delete delete() Creates a delete object.
*/
class DB
{
/**
* Query type constants.
*/
public const PLAIN = 'Plain';
public const INSERT = 'Insert';
public const SELECT = 'Select';
public const UPDATE = 'Update';
public const DELETE = 'Delete';
protected static Connection $connection;
/**
* Retrieve a database connection.
*
* @param array $config database connection config
* @throws Exception
*/
public static function connection(array $config = []): Connection
{
return self::$connection = Connection::instance($config);
}
/**
* Database expression shortcut.
*
* @param mixed $expression
* @return Expression
*/
public static function expr(mixed $expression): Expression
{
return new Expression($expression);
}
/**
* Database value shortcut.
*
* @param mixed $value value
*/
public static function value(mixed $value): Value
{
return new Value($value);
}
/**
* Database identifier shortcut.
*
* @param mixed $identifier identifier
*/
public static function identifier(mixed $identifier): Identifier
{
return new Identifier($identifier);
}
/**
* Database function shortcut.
*
* @param string|null $fnc function
* @param mixed $params function params
* @return Fnc
*/
public static function fnc(?string $fnc, mixed $params = []): Fnc
{
return new Fnc(fnc: $fnc, params: $params);
}
/**
* Returns a query object.
*
* @param mixed $query raw database query
* @param string $type query type
* @param array $bindings query bindings
*/
public static function query(mixed $query, string $type, array $bindings = []): Query
{
return new Query($query, $type, $bindings);
}
/**
* Create a select collector object.
*
* @param mixed $column String field names or arrays for alias
*/
public static function select(mixed $column = null): Select
{
$query = new Select();
return $query->selectArray(func_get_args());
}
/**
* Creates a select collector object.->select('user_login', 'user_fname')
*
* @param array $columns array of fields to select
*/
public static function selectArray(array $columns = []): Select
{
return static::select()->selectArray($columns);
}
/**
* Creates an update collector object.
*
* @param string $table table to update
* @param array $set associative array of new values
*/
public static function update(string $table, array $set = []): Update
{
return new Update($table, $set);
}
/**
* Creates a delete collector object.
*
* @param string|null $table table to delete from
* @return Delete
*/
public static function delete(?string $table = null): Delete
{
return new Delete($table);
}
/**
* Creates an insert collector object.
*
* @param string $table table to insert into
*/
public static function insert(string $table): Insert
{
return new Insert($table);
}
/**
* Creates a schema collector object.
*/
public static function schema(): Schema
{
return new Schema(self::$connection);
}
/**
* Return an Immutable YYYY-MM-DD HH:II:SS date format.
*
* @return DateTime Immutable datetime object.
* @throws InvalidDateException
*/
public static function now(): DateTime
{
return DateTime::now();
}
}