|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helix\DB; |
|
4
|
|
|
|
|
5
|
|
|
use Countable; |
|
6
|
|
|
use Helix\DB\SQL\ExpressionInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Static helper for building SQL. |
|
10
|
|
|
* |
|
11
|
|
|
* The methods here are driver agnostic, and do not quote values. |
|
12
|
|
|
*/ |
|
13
|
|
|
class SQL { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Returns an array of `?` placeholders. |
|
17
|
|
|
* |
|
18
|
|
|
* @param int|array|Countable $count |
|
19
|
|
|
* @return ExpressionInterface[] |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function marks ($count): array { |
|
22
|
|
|
static $mark; |
|
23
|
|
|
$mark ??= new class implements ExpressionInterface { |
|
24
|
|
|
|
|
25
|
|
|
public function __toString () { |
|
26
|
|
|
return '?'; |
|
27
|
|
|
} |
|
28
|
|
|
}; |
|
29
|
|
|
|
|
30
|
|
|
if (is_array($count) or $count instanceof Countable) { |
|
31
|
|
|
$count = count($count); |
|
32
|
|
|
} |
|
33
|
|
|
return array_fill(0, $count, $mark); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Converts an array of columns to `:named` placeholders for prepared queries. |
|
38
|
|
|
* |
|
39
|
|
|
* Qualified columns are slotted as `qualifier__column` (two underscores). |
|
40
|
|
|
* |
|
41
|
|
|
* @param string[] $columns |
|
42
|
|
|
* @return string[] `["column" => ":column"]` |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function slots (array $columns): array { |
|
45
|
|
|
$slots = []; |
|
46
|
|
|
foreach ($columns as $column) { |
|
47
|
|
|
$slots[$column] = ':' . str_replace('.', '__', $column); |
|
48
|
|
|
} |
|
49
|
|
|
return $slots; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string[] $columns |
|
54
|
|
|
* @return string[] `["column" => "column=:column"]` |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function slotsEqual (array $columns): array { |
|
57
|
|
|
$slots = static::slots($columns); |
|
58
|
|
|
foreach ($slots as $column => $slot) { |
|
59
|
|
|
$slots[$column] = "{$column} = {$slot}"; |
|
60
|
|
|
} |
|
61
|
|
|
return $slots; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
final private function __construct () { |
|
65
|
|
|
} |
|
66
|
|
|
} |