1
|
|
|
<?php |
2
|
|
|
namespace Kir\MySQL\Builder; |
3
|
|
|
|
4
|
|
|
use Kir\MySQL\Builder\Internal\DefaultValue; |
5
|
|
|
use Kir\MySQL\Builder\Internal\Types; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @phpstan-import-type DBParameterValueType from Types |
9
|
|
|
*/ |
10
|
|
|
abstract class InsertUpdateStatement extends Statement { |
11
|
|
|
/** @var array<int, string> */ |
12
|
|
|
private ?array $mask = null; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return array<int, string>|null |
16
|
|
|
*/ |
17
|
|
|
public function getMask(): ?array { |
18
|
|
|
return $this->mask; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array<int, string> $mask |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
|
|
public function setMask(array $mask) { |
26
|
|
|
$this->mask = $mask; |
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array<int|string, DBParameterValueType> $fields |
32
|
|
|
* @param array<int, string> $query |
33
|
|
|
* @return string[] |
34
|
|
|
*/ |
35
|
|
|
protected function buildFieldList(array $fields, array $query = []): array { |
36
|
|
|
foreach ($fields as $fieldName => $fieldValue) { |
37
|
|
|
if($fieldValue instanceof DefaultValue) { |
38
|
|
|
$fieldValue = 'DEFAULT'; |
39
|
|
|
} |
40
|
|
|
if(is_array($this->mask) && !in_array($fieldName, $this->mask, true)) { |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
if(is_int($fieldName)) { |
44
|
|
|
if (is_array($fieldValue)) { |
45
|
|
|
// @phpstan-ignore-next-line |
46
|
|
|
$fieldValue = $this->db()->quoteExpression($fieldValue[0], array_slice($fieldValue, 1)); |
47
|
|
|
} |
48
|
|
|
// @phpstan-ignore-next-line |
49
|
|
|
$query[] = "\t{$fieldValue}"; |
50
|
|
|
} else { |
51
|
|
|
$fieldName = $this->db()->quoteField($fieldName); |
52
|
|
|
if (is_array($fieldValue)) { |
53
|
|
|
// @phpstan-ignore-next-line |
54
|
|
|
$fieldValue = $this->db()->quoteExpression($fieldValue[0], array_slice($fieldValue, 1)); |
55
|
|
|
} |
56
|
|
|
// @phpstan-ignore-next-line |
57
|
|
|
$query[] = "\t{$fieldName}={$fieldValue}"; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return $query; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $fieldName |
65
|
|
|
* @param array<int, string> $tableFields |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
protected function isFieldAccessible(string $fieldName, array $tableFields): bool { |
69
|
|
|
if (!in_array($fieldName, $tableFields)) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
if (!is_array($this->mask)) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
return in_array($fieldName, $this->mask, true); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|