1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Adds support for SQLite databases |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Core |
9
|
|
|
* @package Database |
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
11
|
|
|
* @copyright 2013 cSphere Team |
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
13
|
|
|
* @link http://www.csphere.eu |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace csphere\core\database; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Adds support for SQLite databases |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package Database |
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
24
|
|
|
* @copyright 2013 cSphere Team |
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
26
|
|
|
* @link http://www.csphere.eu |
27
|
|
|
**/ |
28
|
|
|
|
29
|
|
|
class Driver_PDO_SQLITE extends Base_PDO |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Creates the database handler object |
33
|
|
|
* |
34
|
|
|
* @param array $config Configuration details as an array |
35
|
|
|
* |
36
|
|
|
* @throws \Exception |
37
|
|
|
* |
38
|
|
|
* @return \csphere\core\database\Driver_PDO_SQLite |
39
|
|
|
**/ |
|
|
|
|
40
|
|
|
|
41
|
|
|
public function __construct(array $config) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($config); |
44
|
|
|
|
45
|
|
|
if (!extension_loaded('pdo_sqlite')) { |
46
|
|
|
|
47
|
|
|
throw new \Exception('Extension "pdo_sqlite" not found'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$file = \csphere\core\init\path() |
51
|
|
|
. 'csphere/storage/database/' . $config['file']; |
52
|
|
|
|
53
|
|
|
$options = []; |
54
|
|
|
|
55
|
|
|
// Use try catch to hide connection details |
56
|
|
|
try { |
57
|
|
|
|
58
|
|
|
$this->con = new \PDO('sqlite:' . $file, null, null, $options); |
59
|
|
|
|
60
|
|
|
$this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
61
|
|
|
} |
62
|
|
|
catch(\PDOException $pdo_error) { |
63
|
|
|
|
64
|
|
|
$this->error('Connect', [], $pdo_error->getMessage(), false); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Builds the query string part for limit and offset |
70
|
|
|
* |
71
|
|
|
* @param integer $first Number of the first dataset to show |
72
|
|
|
* @param integer $max Number of datasets to show from first on |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
**/ |
|
|
|
|
76
|
|
|
|
77
|
|
|
protected function limits($first, $max) |
78
|
|
|
{ |
79
|
|
|
$string = 'LIMIT ' . (int)$first . ',' . (int)$max; |
80
|
|
|
|
81
|
|
|
return $string; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Replaces driver specific query placeholders |
86
|
|
|
* |
87
|
|
|
* @param string $replace The string to use for replaces |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
**/ |
|
|
|
|
91
|
|
|
|
92
|
|
|
protected function replace($replace) |
93
|
|
|
{ |
94
|
|
|
$change_sqlite = ['{engine}' => '', |
95
|
|
|
'{integer}' => 'integer', |
96
|
|
|
'{optimize}' => 'VACUUM', |
97
|
|
|
'{serial}' => 'integer', |
98
|
|
|
'{text}' => 'text', |
99
|
|
|
'{longtext}' => 'text', |
100
|
|
|
'{varchar}' => 'varchar']; |
101
|
|
|
|
102
|
|
|
foreach ($change_sqlite AS $key => $sqlite) { |
103
|
|
|
|
104
|
|
|
$replace = str_replace($key, $sqlite, $replace); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $replace; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns a formatted array with statistics |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
**/ |
|
|
|
|
115
|
|
|
|
116
|
|
|
public function info() |
117
|
|
|
{ |
118
|
|
|
// Get general PDO information |
119
|
|
|
$info = parent::info(); |
120
|
|
|
|
121
|
|
|
// Get encoding of database |
122
|
|
|
$encoding = $this->query('PRAGMA encoding', [], 0, 0); |
123
|
|
|
|
124
|
|
|
$info['encoding'] = $encoding[0]['encoding']; |
125
|
|
|
|
126
|
|
|
// Get size of database |
127
|
|
|
$file = \csphere\core\init\path() |
128
|
|
|
. 'csphere/storage/database/' . $this->config['file']; |
129
|
|
|
|
130
|
|
|
$info['size'] = filesize($file); |
131
|
|
|
|
132
|
|
|
// Get amount of tables |
133
|
|
|
$query = 'SELECT COUNT(*) AS tables FROM sqlite_master ' |
134
|
|
|
. 'WHERE type = \'table\' AND name LIKE \'' |
135
|
|
|
. $this->config['prefix'] . '%\''; |
136
|
|
|
|
137
|
|
|
$tables = $this->query($query, [], 0, 0); |
138
|
|
|
|
139
|
|
|
$info['tables'] = $tables[0]['tables']; |
140
|
|
|
|
141
|
|
|
return $info; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.