|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WebServCo\Framework\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use WebServCo\Framework\Database\QueryType; |
|
8
|
|
|
|
|
9
|
|
|
trait DatabaseTrait |
|
10
|
|
|
{ |
|
11
|
|
|
abstract public function escapeIdentifier(string $string): string; |
|
12
|
|
|
|
|
13
|
|
|
abstract public function escapeTableName(string $string): string; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param array<int,float|int|string> $params |
|
17
|
|
|
* @return bool|int|string|null |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract public function getColumn(string $query, array $params = [], int $columnNumber = 0); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param array<int,float|int|string|null> $params |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract public function query(string $query, array $params = []): \PDOStatement; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array<int,array<int,mixed>> $queries |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract public function transaction(array $queries): int; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array<string,float|int|string|null> $addData |
|
33
|
|
|
* @param array<string,float|int|string|null> $updateData |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract protected function generateAddQuery( |
|
36
|
|
|
string $queryType, |
|
37
|
|
|
string $tableName, |
|
38
|
|
|
array $addData = [], |
|
39
|
|
|
array $updateData = [] |
|
40
|
|
|
): string; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array<mixed> $addData |
|
44
|
|
|
* @param array<mixed> $updateData |
|
45
|
|
|
* |
|
46
|
|
|
* Returns lastInsertId |
|
47
|
|
|
*/ |
|
48
|
|
|
final public function insert(string $tableName, array $addData = [], array $updateData = []): int |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->add(QueryType::INSERT, $tableName, $addData, $updateData); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array<mixed> $data |
|
55
|
|
|
* |
|
56
|
|
|
* Returns lastInsertId |
|
57
|
|
|
*/ |
|
58
|
|
|
final public function insertIgnore(string $tableName, array $data = []): int |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->add(QueryType::INSERT_IGNORE, $tableName, $data); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array<mixed> $data |
|
65
|
|
|
* |
|
66
|
|
|
* Returns lastInsertId |
|
67
|
|
|
*/ |
|
68
|
|
|
final public function replace(string $tableName, array $data = []): int |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->add(QueryType::REPLACE, $tableName, $data); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param float|int|string $value |
|
75
|
|
|
*/ |
|
76
|
|
|
final public function valueExists(string $table, string $field, $value): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return (bool) $this->getColumn( |
|
79
|
|
|
\sprintf( |
|
80
|
|
|
"SELECT 1 FROM %s WHERE %s = ? LIMIT 1", |
|
81
|
|
|
$this->escapeTableName($table), |
|
82
|
|
|
$this->escapeIdentifier($field), |
|
83
|
|
|
), |
|
84
|
|
|
[$value], |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
final public function tableExists(string $table): bool |
|
89
|
|
|
{ |
|
90
|
|
|
$name = $this->escapeTableName($table); |
|
91
|
|
|
|
|
92
|
|
|
try { |
|
93
|
|
|
$this->query(\sprintf('SELECT 1 FROM %s LIMIT 1', $name)); |
|
94
|
|
|
return true; |
|
95
|
|
|
} catch (\WebServCo\Framework\Exceptions\DatabaseException $e) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param array<mixed> $addData |
|
102
|
|
|
* @param array<mixed> $updateData |
|
103
|
|
|
* |
|
104
|
|
|
* Returns lastInsertId |
|
105
|
|
|
*/ |
|
106
|
|
|
final protected function add(string $queryType, string $tableName, array $addData = [], array $updateData = []): int |
|
107
|
|
|
{ |
|
108
|
|
|
if (!$tableName) { |
|
109
|
|
|
throw new \WebServCo\Framework\Exceptions\ApplicationException('No data specified.'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$query = $this->generateAddQuery($queryType, $tableName, $addData, $updateData); |
|
113
|
|
|
|
|
114
|
|
|
$queryData = []; |
|
115
|
|
|
foreach ($addData as $item) { |
|
116
|
|
|
$queryData[] = $item; |
|
117
|
|
|
} |
|
118
|
|
|
foreach ($updateData as $item) { |
|
119
|
|
|
$queryData[] = $item; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $this->transaction( |
|
123
|
|
|
[ |
|
124
|
|
|
[$query, $queryData], // item |
|
125
|
|
|
], // array |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|