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:
<?php
namespace SAREhub\Commons\Misc;
class SqliteHashStorage implements HashStorage
{
private $filePath;
private $tableName;
private $firstTime = false;
private $storage = null;
public function __construct($filePath, $tableName = 'hashes')
{
$this->filePath = $filePath;
$this->tableName = $tableName;
}
public function open()
{
if ($this->isOpened()) {
return;
}
$this->firstTime = !file_exists($this->filePath);
$this->storage = new \SQLite3($this->filePath);
$this->storage->exec("PRAGMA journal_mode = OFF;");
$this->storage->exec("PRAGMA synchronous = NORMAL;");
$this->storage->exec("PRAGMA locking_mode = EXCLUSIVE;");
$this->storage->exec("PRAGMA automatic_index = false;");
if ($this->firstTime) {
$createTableSql = "CREATE TABLE " . $this->tableName . "(id TEXT PRIMARY KEY, hash TEXT NOT NULL) WITHOUT ROWID;";
$this->storage->exec($createTableSql);
}
}
public function close()
{
if ($this->isOpened()) {
$this->storage->close();
$this->storage = null;
}
}
public function isOpened()
{
return $this->storage !== null;
}
public function findById($id)
{
return $this->storage->querySingle("SELECT hash FROM " . $this->tableName . " WHERE id='$id'");
}
public function insertMulti(array $hashes)
{
$valuesString = '';
foreach ($hashes as $id => $hash) {
$valuesString .= "('$id','$hash'),";
}
$this->storage->exec("INSERT INTO " . $this->tableName . "(id, hash) VALUES " . rtrim($valuesString, ','));
}
public function updateMulti(array $hashes)
{
$startSql = "UPDATE " . $this->tableName;
$this->storage->exec('BEGIN TRANSACTION');
foreach ($hashes as $id => $hash) {
$this->storage->exec($startSql . " SET hash='$hash' WHERE id='$id'");
}
$this->storage->exec('END TRANSACTION');
}
}