1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\DB; |
12
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Connection; |
14
|
|
|
use Doctrine\DBAL\DriverManager; |
15
|
|
|
|
16
|
|
|
abstract class DB { |
17
|
|
|
/** |
18
|
|
|
* @var Connection |
19
|
|
|
*/ |
20
|
|
|
protected $connection; |
21
|
|
|
|
22
|
45 |
|
public function __construct(Connection $connection) { |
23
|
45 |
|
$this->connection = $connection; |
24
|
45 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return \Doctrine\DBAL\Query\Builder |
28
|
|
|
*/ |
29
|
38 |
|
public function builder() { |
30
|
38 |
|
return $this->connection->createQueryBuilder(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Connection |
35
|
|
|
*/ |
36
|
2 |
|
public function connection() { |
37
|
2 |
|
return $this->connection; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initialize REGEXP for sqlite. |
42
|
|
|
*/ |
43
|
1 |
|
public function init_sqlite_regexp() { |
44
|
1 |
|
$pdo = $this->connection->getWrappedConnection(); |
45
|
1 |
|
if (!($pdo instanceof \PDO)) { |
46
|
|
|
throw new \RuntimeException( |
47
|
|
|
"Expected wrapped connection to be PDO-object."); |
48
|
|
|
} |
49
|
1 |
|
$pdo->sqliteCreateFunction("regexp", function($pattern, $data) { |
50
|
|
|
return preg_match("%$pattern%", $data) > 0; |
51
|
1 |
|
}); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
// Creation of database. |
55
|
|
|
|
56
|
4 |
|
public function is_inited() { |
57
|
4 |
|
$res = $this->builder() |
58
|
4 |
|
->select("COUNT(*)") |
59
|
4 |
|
->from("sqlite_master") |
60
|
4 |
|
->where("type = 'table'") |
61
|
4 |
|
->execute() |
62
|
4 |
|
->fetchColumn(); |
63
|
4 |
|
return $res > 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
public function maybe_init_database_schema() { |
67
|
3 |
|
if (!$this->is_inited()) { |
68
|
3 |
|
$this->init_database_schema(); |
69
|
3 |
|
} |
70
|
3 |
|
} |
71
|
|
|
|
72
|
|
|
abstract public function init_database_schema(); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Build pdo_sqlite connection to some file if path is given or |
76
|
|
|
* to memory instead. |
77
|
|
|
* |
78
|
|
|
* @param string|null $path |
79
|
|
|
* @return Connection |
80
|
|
|
*/ |
81
|
4 |
|
public static function sqlite_connection($path = null) { |
82
|
4 |
|
assert('is_string($path) || is_null($path)'); |
83
|
4 |
|
if ($path !== null) { |
84
|
|
|
return DriverManager::getConnection |
85
|
4 |
|
(["driver" => "pdo_sqlite" |
86
|
4 |
|
, "memory" => false |
87
|
4 |
|
, "path" => $path |
88
|
4 |
|
]); |
89
|
|
|
} |
90
|
|
|
else { |
91
|
|
|
return DriverManager::getConnection |
92
|
1 |
|
(["driver" => "pdo_sqlite" |
93
|
1 |
|
, "memory" => true |
94
|
1 |
|
]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|