|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace tomzx\IRCStats; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
|
6
|
|
|
use Illuminate\Database\Connection; |
|
7
|
|
|
|
|
8
|
|
|
class DatabaseProxy { |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $configuration = []; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Illuminate\Database\Capsule\Manager |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $capsule; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \tomzx\IRCStats\DatabaseSchema |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $databaseSchema; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $booted = false; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $configuration |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function __construct(array $configuration) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$this->configuration = $configuration; |
|
32
|
1 |
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function __destruct() |
|
35
|
|
|
{ |
|
36
|
1 |
|
if ($this->capsule) { |
|
37
|
1 |
|
$this->getConnection()->disconnect(); |
|
38
|
1 |
|
} |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param \Illuminate\Database\Capsule\Manager $capsule |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function setCapsule(Capsule $capsule) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->capsule = $capsule; |
|
48
|
|
|
|
|
49
|
1 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return \Illuminate\Database\Capsule\Manager |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function getCapsule() |
|
56
|
|
|
{ |
|
57
|
1 |
|
if ( ! $this->capsule) { |
|
58
|
|
|
$this->capsule = new Capsule; |
|
59
|
|
|
$this->capsule->addConnection($this->configuration); |
|
60
|
|
|
$this->capsule->setAsGlobal(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return $this->capsule; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return \tomzx\IRCStats\DatabaseSchema |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function getDatabaseSchema() |
|
70
|
|
|
{ |
|
71
|
1 |
|
if ( ! $this->databaseSchema) { |
|
72
|
|
|
$this->databaseSchema = new DatabaseSchema(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
return $this->databaseSchema; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param \tomzx\IRCStats\DatabaseSchema $databaseSchema |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function setDatabaseSchema(DatabaseSchema $databaseSchema) |
|
83
|
|
|
{ |
|
84
|
1 |
|
$this->databaseSchema = $databaseSchema; |
|
85
|
|
|
|
|
86
|
1 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return \Illuminate\Database\Connection |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getConnection() |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->bootDatabase(); |
|
95
|
1 |
|
return $this->getCapsule()->connection(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
protected function bootDatabase() |
|
99
|
|
|
{ |
|
100
|
1 |
|
if ($this->booted) { |
|
101
|
1 |
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
$this->booted = true; |
|
105
|
1 |
|
$db = $this->getCapsule()->connection(); |
|
106
|
1 |
|
$this->configurePragma($db); |
|
107
|
1 |
|
$this->setupDatabase($db); |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param \Illuminate\Database\Connection $db |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
1 |
|
protected function setupDatabase(Connection $db) |
|
115
|
|
|
{ |
|
116
|
1 |
|
$this->getDatabaseSchema()->initialize($db); |
|
117
|
1 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param \Illuminate\Database\Connection $db |
|
121
|
|
|
*/ |
|
122
|
1 |
|
protected function configurePragma(Connection $db) |
|
123
|
|
|
{ |
|
124
|
1 |
|
if ($this->configuration['driver'] !== 'sqlite') { |
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// Enable foreign keys for the current connection/file |
|
129
|
1 |
|
$db->statement('PRAGMA foreign_keys = ON;'); |
|
130
|
|
|
// Create sqlite-journal in memory only (instead of creating disk files) |
|
131
|
1 |
|
$db->statement('PRAGMA journal_mode = MEMORY;'); |
|
132
|
|
|
// Do not wait for OS after sending write commands |
|
133
|
1 |
|
$db->statement('PRAGMA synchronous = OFF;'); |
|
134
|
1 |
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|