|
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\App; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Connection; |
|
14
|
|
|
|
|
15
|
|
|
abstract class DB { |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Connection |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $connection; |
|
20
|
|
|
|
|
21
|
73 |
|
public function __construct(Connection $connection) { |
|
22
|
73 |
|
$this->connection = $connection; |
|
23
|
73 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return \Doctrine\DBAL\Query\Builder |
|
27
|
|
|
*/ |
|
28
|
72 |
|
public function builder() { |
|
29
|
72 |
|
return $this->connection->createQueryBuilder(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Initialize REGEXP for sqlite. |
|
34
|
|
|
*/ |
|
35
|
50 |
|
public function init_sqlite_regexp() { |
|
36
|
50 |
|
$pdo = $this->connection->getWrappedConnection(); |
|
37
|
50 |
|
if (!($pdo instanceof \PDO)) { |
|
38
|
|
|
throw new \RuntimeException( |
|
39
|
|
|
"Expected wrapped connection to be PDO-object."); |
|
40
|
|
|
} |
|
41
|
50 |
|
$pdo->sqliteCreateFunction("regexp", function($pattern, $data) { |
|
42
|
25 |
|
return preg_match("%$pattern%", $data) > 0; |
|
43
|
50 |
|
}); |
|
44
|
50 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Creation of database. |
|
47
|
|
|
|
|
48
|
55 |
|
public function is_inited() { |
|
49
|
55 |
|
$res = $this->builder() |
|
50
|
55 |
|
->select("COUNT(*)") |
|
51
|
55 |
|
->from("sqlite_master") |
|
52
|
55 |
|
->where("type = 'table'") |
|
53
|
55 |
|
->execute() |
|
54
|
55 |
|
->fetchColumn(); |
|
55
|
55 |
|
return $res > 0; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
54 |
|
public function maybe_init_database_schema() { |
|
59
|
54 |
|
if (!$this->is_inited()) { |
|
60
|
54 |
|
$this->init_database_schema(); |
|
61
|
54 |
|
} |
|
62
|
54 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
abstract public function init_database_schema(); |
|
65
|
|
|
} |
|
66
|
|
|
|