| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Mgleis\PhpSqliteKeyValueStore; |
| 5 |
|
| 6 |
class KeyValueStore { |
| 7 |
|
| 8 |
private \PDO $db; |
| 9 |
private string $table; |
| 10 |
|
| 11 |
public function __construct($dbFileOrPdo = null, string $table = 'key_value_store') { |
| 12 |
$this->table = $table; |
| 13 |
if (!preg_match('/^[a-zA-Z0-9_]+$/', $table)) { |
| 14 |
throw new \InvalidArgumentException("Invalid table name"); |
| 15 |
} |
| 16 |
|
| 17 |
if (is_null($dbFileOrPdo)) { |
| 18 |
$dbFileOrPdo = 'kvstore.sqlite'; |
| 19 |
} |
| 20 |
if (is_string($dbFileOrPdo)) { |
| 21 |
$this->db = new \PDO("sqlite:" . $dbFileOrPdo); |
| 22 |
$this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 23 |
} else if ($dbFileOrPdo instanceof \PDO) { |
| 24 |
$this->db = $dbFileOrPdo; |
| 25 |
} else { |
| 26 |
throw new \InvalidArgumentException("First argument needs to be a string or a PDO instance."); |
| 27 |
} |
| 28 |
|
| 29 |
$this->initializeDatabase(); |
| 30 |
} |
| 31 |
|
| 32 |
private function initializeDatabase(): void { |
| 33 |
$this->db->exec(" |
| 34 |
CREATE TABLE IF NOT EXISTS {$this->table} ( |
| 35 |
key TEXT PRIMARY KEY, |
| 36 |
value TEXT |
| 37 |
); |
| 38 |
"); |
| 39 |
} |
| 40 |
|
| 41 |
public function get(string $key, $default = null) { |
| 42 |
$stmt = $this->db->prepare("SELECT value FROM {$this->table} WHERE key = :key"); |
| 43 |
$stmt->bindParam(':key', $key, \PDO::PARAM_STR); |
| 44 |
$stmt->execute(); |
| 45 |
$result = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 46 |
|
| 47 |
if ($result === false) { |
| 48 |
return $default; |
| 49 |
} |
| 50 |
|
| 51 |
return json_decode($result['value'], true, JSON_THROW_ON_ERROR); |
| 52 |
} |
| 53 |
|
| 54 |
public function has(string $key): bool { |
| 55 |
$stmt = $this->db->prepare("SELECT 1 FROM {$this->table} WHERE key = :key"); |
| 56 |
$stmt->bindParam(':key', $key, \PDO::PARAM_STR); |
| 57 |
$stmt->execute(); |
| 58 |
|
| 59 |
return $stmt->fetch(\PDO::FETCH_ASSOC) !== false; |
| 60 |
} |
| 61 |
|
| 62 |
public function delete(string $key): void { |
| 63 |
$stmt = $this->db->prepare("DELETE FROM {$this->table} WHERE key = :key"); |
| 64 |
$stmt->bindParam(':key', $key, \PDO::PARAM_STR); |
| 65 |
$stmt->execute(); |
| 66 |
} |
| 67 |
|
| 68 |
public function set(string $key, $value): void { |
| 69 |
$jsonValue = json_encode($value, JSON_THROW_ON_ERROR); |
| 70 |
|
| 71 |
$stmt = $this->db->prepare(" |
| 72 |
INSERT INTO {$this->table} (key, value) |
| 73 |
VALUES (:key, :value) |
| 74 |
ON CONFLICT(key) DO UPDATE SET value = :value; |
| 75 |
"); |
| 76 |
$stmt->bindParam(':key', $key, \PDO::PARAM_STR); |
| 77 |
$stmt->bindParam(':value', $jsonValue, \PDO::PARAM_STR); |
| 78 |
$stmt->execute(); |
| 79 |
} |
| 80 |
|
| 81 |
private function search(string $searchText, string $column, string $likeCondition): array { |
| 82 |
if (!in_array($likeCondition, ['%%%s%%', '%%%s'])) { |
| 83 |
throw new \InvalidArgumentException('Invalid likeCondition'); |
| 84 |
} |
| 85 |
if (in_array($likeCondition, ['key', 'value'])) { |
| 86 |
throw new \InvalidArgumentException('Invalid column'); |
| 87 |
} |
| 88 |
|
| 89 |
$stmt = $this->db->prepare(" |
| 90 |
SELECT key, value |
| 91 |
FROM {$this->table} |
| 92 |
WHERE {$column} LIKE :text |
| 93 |
"); |
| 94 |
$searchText = '%' . $searchText . '%'; |
| 95 |
$stmt->bindParam(':text', $searchText, \PDO::PARAM_STR); |
| 96 |
$stmt->execute(); |
| 97 |
|
| 98 |
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
| 99 |
|
| 100 |
foreach ($results as &$row) { |
| 101 |
$row['value'] = json_decode($row['value'], true, JSON_THROW_ON_ERROR); |
| 102 |
} |
| 103 |
|
| 104 |
return $results ?: []; |
| 105 |
} |
| 106 |
|
| 107 |
public function searchValueContains(string $searchText): array { |
| 108 |
return $this->search($searchText, 'value', '%%%s%%'); |
| 109 |
} |
| 110 |
|
| 111 |
public function searchValueStartsWith(string $searchText): array { |
| 112 |
return $this->search($searchText, 'value', '%%%s'); |
| 113 |
} |
| 114 |
|
| 115 |
public function searchKeyContains(string $searchText): array { |
| 116 |
return $this->search($searchText, 'key', '%%%s%%'); |
| 117 |
} |
| 118 |
|
| 119 |
public function searchKeyStartsWith(string $searchText): array { |
| 120 |
return $this->search($searchText, 'key', '%%%s'); |
| 121 |
} |
| 122 |
|
| 123 |
} |