-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathInsertProcessor.php
More file actions
167 lines (135 loc) · 5.49 KB
/
InsertProcessor.php
File metadata and controls
167 lines (135 loc) · 5.49 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
164
165
166
167
<?php
namespace Vimeo\MysqlEngine\Processor;
use Vimeo\MysqlEngine\DataIntegrity;
use Vimeo\MysqlEngine\Query\InsertQuery;
use Vimeo\MysqlEngine\Schema\Column\IntegerColumn;
final class InsertProcessor extends Processor
{
/**
* @throws SQLFakeUniqueKeyViolation
* @throws ProcessorException
* @throws \Exception
*/
public static function process(
\Vimeo\MysqlEngine\FakePdoInterface $conn,
Scope $scope,
InsertQuery $stmt
) : int {
list($database, $table_name) = self::parseTableName($conn, $stmt->table);
$table = $conn->getServer()->getTable($database, $table_name) ?? [];
//Metrics::trackQuery(QueryType::INSERT, $conn->getServer()->name, $table_name, $this->sql);
$table_definition = $conn->getServer()->getTableDefinition($database, $table_name);
if ($table_definition === null) {
throw new ProcessorException("Table {$table_name} not found in schema");
}
$rows_affected = 0;
$conn->setLastInsertId("0");
foreach ($stmt->values as $value_list) {
$row = [];
foreach ($stmt->insertColumns as $key => $col) {
$row[$col] = Expression\Evaluator::evaluate(
$conn,
$scope,
$value_list[$key],
[],
new QueryResult([], [])
);
}
$row = DataIntegrity::coerceToSchema($conn, $row, $table_definition);
$unique_key_violation = DataIntegrity::checkUniqueConstraints($table, $row, $table_definition);
if ($unique_key_violation !== null) {
list($msg, $row_id) = $unique_key_violation;
if ($stmt->updateExpressions) {
$existing_row = $table[$row_id];
list($affected, $table) = self::applySet(
$conn,
$scope,
$database,
$table_name,
[$row_id => $existing_row],
$table,
$stmt->updateExpressions,
$table_definition,
$row
);
$rows_affected += $affected * 2;
continue;
}
if ($stmt->ignoreDupes) {
continue;
} else {
throw new SQLFakeUniqueKeyViolation($msg);
}
}
foreach ($row as $column_name => $value) {
$column = $table_definition->columns[$column_name];
if ($column instanceof IntegerColumn && $column->isAutoIncrement()) {
$last_incremented_value = $conn->getServer()->addAutoIncrementMinValue(
$database,
$table_name,
$column_name,
$value
);
if ($conn->lastInsertId() === "0") {
$conn->setLastInsertId((string)$last_incremented_value);
}
}
}
$table[] = $row;
$rows_affected++;
}
$conn->getServer()->saveTable($database, $table_name, $table);
if ($stmt->setClause) {
list($rows_affected) = self::applySet(
$conn,
$scope,
$database,
$table_name,
null,
$table,
$stmt->setClause,
$table_definition
);
}
if ($stmt->selectQuery) {
$selectResult = SelectProcessor::process(
$conn,
$scope,
$stmt->selectQuery
)->rows;
foreach ($selectResult as $selected_param) {
$row = [];
foreach ($stmt->selectQuery->selectExpressions as $index => $expr) {
if (key_exists($index, $stmt->insertColumns)
&& (is_string($selected_param[$expr->name]) || is_int($selected_param[$expr->name]))) {
$row[$stmt->insertColumns[$index]] = $selected_param[$expr->name];
}
}
$row = DataIntegrity::coerceToSchema($conn, $row, $table_definition);
$result = DataIntegrity::checkUniqueConstraints($table, $row, $table_definition);
if ($result !== null) {
throw new SQLFakeUniqueKeyViolation($result[0]);
}
/**
* @psalm-suppress MixedAssignment
*/
foreach ($row as $column_name => $value) {
$column = $table_definition->columns[$column_name];
if ($column instanceof IntegerColumn && $column->isAutoIncrement() && is_int($value)) {
$last_incremented_value = $conn->getServer()->addAutoIncrementMinValue(
$database,
$table_name,
$column_name,
$value
);
}
}
$table[] = $row;
}
$conn->getServer()->saveTable($database, $table_name, $table);
$conn->setLastInsertId((string)($last_incremented_value ?? 0));
return count($selectResult);
}
return $rows_affected;
}
}