LGV_TZ_Lookup 1.4.2
Time zone lookup for PHP applications and HTTP clients
Loading...
Searching...
No Matches
SQLiteDatabase.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5// An isolated benchmark database. Production uses the configured MySQL or PostgreSQL database.
7 public PDO $connection;
8 public int $statements = 0;
9
10 public function __construct(string $path) {
11 $this->connection = new PDO('sqlite:'.$path);
12 $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
13 }
14
15 public function preparedStatement($sql, $params = [], $fetchResponse = false, $paramTypes = []) {
17 $statement = $this->connection->prepare($sql);
18 foreach (array_values($params) as $index => $value) {
19 $type = $paramTypes[$index] ?? (str_starts_with($sql, 'INSERT') && $index === 5 ? PDO::PARAM_LOB : PDO::PARAM_STR);
20 $statement->bindValue($index + 1, $value, $type);
21 }
22 $statement->execute();
23 return $fetchResponse ? $statement->fetchAll(PDO::FETCH_ASSOC) : true;
24 }
25
26 public function preparedRows($sql, $params = [], $buffered = true): Generator {
27 ++$this->statements;
28 $statement = $this->connection->prepare($sql);
29 $statement->execute(array_values($params));
30 try {
31 while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
32 yield $row;
33 }
34 } finally {
35 $statement->closeCursor();
36 }
37 }
38}
39
41 public function __construct(string $path) {
42 $this->pdo_instance = new SQLiteStatements($path);
43 }
44
45 public function reset_database() {
46 $this->pdo_instance->connection->exec('DROP TABLE IF EXISTS timezones;
47 CREATE TABLE timezones (
48 id INTEGER PRIMARY KEY AUTOINCREMENT, tzname TEXT NOT NULL,
49 east REAL NOT NULL, west REAL NOT NULL, north REAL NOT NULL, south REAL NOT NULL,
50 polygon BLOB NOT NULL
51 );
52 CREATE INDEX east ON timezones(east);
53 CREATE INDEX west ON timezones(west);
54 CREATE INDEX north ON timezones(north);
55 CREATE INDEX south ON timezones(south);');
56 }
57}
__construct(string $path)
__construct(string $path)
preparedStatement($sql, $params=[], $fetchResponse=false, $paramTypes=[])
preparedRows($sql, $params=[], $buffered=true)
$database pdo_instance
Definition composer.php:53