Skip to content

Commit d765b63

Browse files
committed
Improve the JSONDB main class
* Update the update() query to work with the new file architecture * Fix issue when connect() to a database
1 parent 5a60993 commit d765b63

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/JSONDB/JSONDB.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ public function connect($server, $username, $password, $database = NULL)
375375
{
376376
$this->benchmark->mark('jsondb_(connect)_start');
377377
$config = $this->config->getConfig('users');
378-
$server = realpath($server);
379378

380379
if (!array_key_exists($server, $config)) {
381380
$this->benchmark->mark('jsondb_(connect)_end');
@@ -1135,7 +1134,7 @@ protected function _update($data)
11351134
}
11361135

11371136
if (!array_key_exists('with', $this->parsedQuery['extensions'])) {
1138-
throw new Exception("JSONDB Error: Can't execute the \"update()\" query without values.");
1137+
throw new Exception("JSONDB Error: Can't execute the \"update()\" query without values. The \"with()\" extension is required.");
11391138
}
11401139

11411140
$fields_nb = count($this->parsedQuery['parameters']);
@@ -1193,6 +1192,26 @@ protected function _update($data)
11931192
return $data['data'][$now]['#rowid'] < $data['data'][$after]['#rowid'];
11941193
});
11951194

1195+
$auto_increment = NULL;
1196+
foreach ((array)$data['properties'] as $column => $prop) {
1197+
if (is_array($prop) && array_key_exists('auto_increment', $prop) && $prop['auto_increment'] === TRUE) {
1198+
$auto_increment = $column;
1199+
break;
1200+
}
1201+
}
1202+
1203+
if (NULL !== $auto_increment) {
1204+
$last_insert_id = 0;
1205+
foreach ((array)$data['data'] as $d) {
1206+
if ($last_insert_id === 0) {
1207+
$last_insert_id = $d[$auto_increment];
1208+
} else {
1209+
$last_insert_id = max($last_insert_id, $d[$auto_increment]);
1210+
}
1211+
}
1212+
$data['properties']['last_insert_id'] = $last_insert_id;
1213+
}
1214+
11961215
$this->cache->update($this->_getTablePath(), $data);
11971216

11981217
return (bool)file_put_contents($this->_getTablePath(), json_encode($data));

0 commit comments

Comments
 (0)