1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
<?php
/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
/**
* La classe Model_sql représente le modèle interragissant avec les bases de données
*/
class Minz_ModelPdo {
/**
* Partage la connexion à la base de données entre toutes les instances.
*/
public static $usesSharedPdo = true;
private static $sharedPdo = null;
private static $sharedPrefix;
private static $sharedCurrentUser;
protected $pdo;
protected $current_user;
/**
* Créé la connexion à la base de données à l'aide des variables
* HOST, BASE, USER et PASS définies dans le fichier de configuration
*/
public function __construct($currentUser = null, $currentPdo = null) {
if ($currentUser === null) {
$currentUser = Minz_Session::param('currentUser');
}
if ($currentPdo != null) {
$this->pdo = $currentPdo;
return;
}
if (self::$usesSharedPdo && self::$sharedPdo != null &&
($currentUser == '' || $currentUser === self::$sharedCurrentUser)) {
$this->pdo = self::$sharedPdo;
$this->current_user = self::$sharedCurrentUser;
return;
}
$this->current_user = $currentUser;
self::$sharedCurrentUser = $currentUser;
$conf = Minz_Configuration::get('system');
$db = $conf->db;
$driver_options = isset($db['pdo_options']) && is_array($db['pdo_options']) ? $db['pdo_options'] : [];
$dbServer = parse_url('db://' . $db['host']);
$dsn = '';
try {
switch ($db['type']) {
case 'mysql':
$dsn = 'mysql:host=' . (empty($dbServer['host']) ? $db['host'] : $dbServer['host']) . ';charset=utf8mb4';
if (!empty($db['base'])) {
$dsn .= ';dbname=' . $db['base'];
}
if (!empty($dbServer['port'])) {
$dsn .= ';port=' . $dbServer['port'];
}
$driver_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8mb4';
$this->pdo = new MinzPDOMySql($dsn, $db['user'], $db['password'], $driver_options);
$this->pdo->setPrefix($db['prefix'] . $currentUser . '_');
break;
case 'sqlite':
$dsn = 'sqlite:' . join_path(DATA_PATH, 'users', $currentUser, 'db.sqlite');
$this->pdo = new MinzPDOSQLite($dsn, $db['user'], $db['password'], $driver_options);
$this->pdo->setPrefix('');
break;
case 'pgsql':
$dsn = 'pgsql:host=' . (empty($dbServer['host']) ? $db['host'] : $dbServer['host']);
if (!empty($db['base'])) {
$dsn .= ';dbname=' . $db['base'];
}
if (!empty($dbServer['port'])) {
$dsn .= ';port=' . $dbServer['port'];
}
$this->pdo = new MinzPDOPGSQL($dsn, $db['user'], $db['password'], $driver_options);
$this->pdo->setPrefix($db['prefix'] . $currentUser . '_');
break;
default:
throw new Minz_PDOConnectionException(
'Invalid database type!',
$db['user'], Minz_Exception::ERROR
);
break;
}
self::$sharedPdo = $this->pdo;
} catch (Exception $e) {
throw new Minz_PDOConnectionException(
$dsn,
$db['user'], Minz_Exception::ERROR
);
}
}
public function beginTransaction() {
$this->pdo->beginTransaction();
}
public function inTransaction() {
return $this->pdo->inTransaction();
}
public function commit() {
$this->pdo->commit();
}
public function rollBack() {
$this->pdo->rollBack();
}
public static function clean() {
self::$sharedPdo = null;
self::$sharedCurrentUser = '';
}
}
abstract class MinzPDO extends PDO {
public function __construct($dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
abstract public function dbType();
private $prefix = '';
public function prefix() { return $this->prefix; }
public function setPrefix($prefix) { $this->prefix = $prefix; }
private function autoPrefix($sql) {
return str_replace('`_', '`' . $this->prefix, $sql);
}
protected function preSql($statement) {
if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
invalidateHttpCache();
}
return $this->autoPrefix($statement);
}
public function lastInsertId($name = null) {
if ($name != null) {
$name = $this->preSql($name);
}
return parent::lastInsertId($name);
}
public function prepare($statement, $driver_options = array()) {
$statement = $this->preSql($statement);
return parent::prepare($statement, $driver_options);
}
public function exec($statement) {
$statement = $this->preSql($statement);
return parent::exec($statement);
}
public function query($statement) {
$statement = $this->preSql($statement);
return parent::query($statement);
}
}
class MinzPDOMySql extends MinzPDO {
public function __construct($dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
}
public function dbType() {
return 'mysql';
}
public function lastInsertId($name = null) {
return parent::lastInsertId(); //We discard the name, only used by PostgreSQL
}
}
class MinzPDOSQLite extends MinzPDO {
public function __construct($dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->exec('PRAGMA foreign_keys = ON;');
}
public function dbType() {
return 'sqlite';
}
public function lastInsertId($name = null) {
return parent::lastInsertId(); //We discard the name, only used by PostgreSQL
}
}
class MinzPDOPGSQL extends MinzPDO {
public function __construct($dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->exec("SET NAMES 'UTF8';");
}
public function dbType() {
return 'pgsql';
}
protected function preSql($statement) {
$statement = parent::preSql($statement);
return str_replace(array('`', ' LIKE '), array('"', ' ILIKE '), $statement);
}
}
|