|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2001 Fabien Seisen <[email protected]> |
|
3
|
|
|
* Copyright (C) 2002-2005 Rodolphe Quiedeville <[email protected]> |
|
4
|
|
|
* Copyright (C) 2004-2011 Laurent Destailleur <[email protected]> |
|
5
|
|
|
* Copyright (C) 2006 Andre Cianfarani <[email protected]> |
|
6
|
|
|
* Copyright (C) 2005-2012 Regis Houssin <[email protected]> |
|
7
|
|
|
* Copyright (C) 2015 Raphaël Doursenaud <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* \file htdocs/core/db/mysqli.class.php |
|
25
|
|
|
* \brief Class file to manage Dolibarr database access for a MySQL database |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
require_once DOL_DOCUMENT_ROOT .'/core/db/DoliDB.class.php'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class to manage Dolibarr database access for a MySQL database using the MySQLi extension |
|
32
|
|
|
*/ |
|
33
|
|
|
class DoliDBMysqli extends DoliDB |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var mysqli Database object */ |
|
36
|
|
|
public $db; |
|
37
|
|
|
//! Database type |
|
38
|
|
|
public $type='mysqli'; |
|
39
|
|
|
//! Database label |
|
40
|
|
|
const LABEL='MySQL or MariaDB'; |
|
41
|
|
|
//! Version min database |
|
42
|
|
|
const VERSIONMIN='5.0.3'; |
|
43
|
|
|
/** @var mysqli_result Resultset of last query */ |
|
44
|
|
|
private $_results; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor. |
|
48
|
|
|
* This create an opened connexion to a database server and eventually to a database |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $type Type of database (mysql, pgsql...) |
|
51
|
|
|
* @param string $host Address of database server |
|
52
|
|
|
* @param string $user Nom de l'utilisateur autorise |
|
53
|
|
|
* @param string $pass Mot de passe |
|
54
|
|
|
* @param string $name Nom de la database |
|
55
|
|
|
* @param int $port Port of database server |
|
56
|
|
|
*/ |
|
57
|
|
|
function __construct($type, $host, $user, $pass, $name='', $port=0) |
|
58
|
|
|
{ |
|
59
|
|
|
global $conf,$langs; |
|
60
|
|
|
|
|
61
|
|
|
// Note that having "static" property for "$forcecharset" and "$forcecollate" will make error here in strict mode, so they are not static |
|
62
|
|
|
if (! empty($conf->db->character_set)) $this->forcecharset=$conf->db->character_set; |
|
63
|
|
|
if (! empty($conf->db->dolibarr_main_db_collation)) $this->forcecollate=$conf->db->dolibarr_main_db_collation; |
|
64
|
|
|
|
|
65
|
|
|
$this->database_user=$user; |
|
66
|
|
|
$this->database_host=$host; |
|
67
|
|
|
$this->database_port=$port; |
|
68
|
|
|
|
|
69
|
|
|
$this->transaction_opened=0; |
|
70
|
|
|
|
|
71
|
|
|
//print "Name DB: $host,$user,$pass,$name<br>"; |
|
72
|
|
|
|
|
73
|
|
|
if (! class_exists('mysqli')) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->connected = false; |
|
76
|
|
|
$this->ok = false; |
|
77
|
|
|
$this->error="Mysqli PHP functions for using Mysqli driver are not available in this version of PHP. Try to use another driver."; |
|
78
|
|
|
dol_syslog(get_class($this)."::DoliDBMysqli : Mysqli PHP functions for using Mysqli driver are not available in this version of PHP. Try to use another driver.",LOG_ERR); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (! $host) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->connected = false; |
|
84
|
|
|
$this->ok = false; |
|
85
|
|
|
$this->error=$langs->trans("ErrorWrongHostParameter"); |
|
86
|
|
|
dol_syslog(get_class($this)."::DoliDBMysqli : Connect error, wrong host parameters",LOG_ERR); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Try server connection |
|
90
|
|
|
// We do not try to connect to database, only to server. Connect to database is done later in constrcutor |
|
91
|
|
|
$this->db = $this->connect($host, $user, $pass, '', $port); |
|
92
|
|
|
|
|
93
|
|
|
if ($this->db->connect_errno) { |
|
94
|
|
|
$this->connected = false; |
|
95
|
|
|
$this->ok = false; |
|
96
|
|
|
$this->error = $this->db->connect_error; |
|
97
|
|
|
dol_syslog(get_class($this) . "::DoliDBMysqli Connect error: " . $this->error, LOG_ERR); |
|
98
|
|
|
} else { |
|
99
|
|
|
$this->connected = true; |
|
100
|
|
|
$this->ok = true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// If server connection is ok, we try to connect to the database |
|
104
|
|
|
if ($this->connected && $name) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($this->select_db($name)) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->database_selected = true; |
|
109
|
|
|
$this->database_name = $name; |
|
110
|
|
|
$this->ok = true; |
|
111
|
|
|
|
|
112
|
|
|
// If client connected with different charset than Dolibarr HTML output |
|
113
|
|
|
$clientmustbe=''; |
|
114
|
|
|
if (preg_match('/UTF-8/i',$conf->file->character_set_client)) $clientmustbe='utf8'; |
|
115
|
|
|
if (preg_match('/ISO-8859-1/i',$conf->file->character_set_client)) $clientmustbe='latin1'; |
|
116
|
|
|
if ($this->db->character_set_name() != $clientmustbe) { |
|
117
|
|
|
$this->db->set_charset($clientmustbe); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
else |
|
121
|
|
|
{ |
|
122
|
|
|
$this->database_selected = false; |
|
123
|
|
|
$this->database_name = ''; |
|
124
|
|
|
$this->ok = false; |
|
125
|
|
|
$this->error=$this->error(); |
|
126
|
|
|
dol_syslog(get_class($this)."::DoliDBMysqli : Select_db error ".$this->error,LOG_ERR); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
else |
|
130
|
|
|
{ |
|
131
|
|
|
// Pas de selection de base demandee, ok ou ko |
|
132
|
|
|
$this->database_selected = false; |
|
133
|
|
|
|
|
134
|
|
|
if ($this->connected) |
|
135
|
|
|
{ |
|
136
|
|
|
// If client connected with different charset than Dolibarr HTML output |
|
137
|
|
|
$clientmustbe=''; |
|
138
|
|
|
if (preg_match('/UTF-8/i',$conf->file->character_set_client)) $clientmustbe='utf8'; |
|
139
|
|
|
if (preg_match('/ISO-8859-1/i',$conf->file->character_set_client)) $clientmustbe='latin1'; |
|
140
|
|
|
if ($this->db->character_set_name() != $clientmustbe) { |
|
141
|
|
|
$this->db->set_charset($clientmustbe); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Convert a SQL request in Mysql syntax to native syntax |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $line SQL request line to convert |
|
152
|
|
|
* @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...) |
|
153
|
|
|
* @return string SQL request line converted |
|
154
|
|
|
*/ |
|
155
|
|
|
static function convertSQLFromMysql($line,$type='ddl') |
|
156
|
|
|
{ |
|
157
|
|
|
return $line; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Select a database |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $database Name of database |
|
164
|
|
|
* @return boolean true if OK, false if KO |
|
165
|
|
|
*/ |
|
166
|
|
|
function select_db($database) |
|
167
|
|
|
{ |
|
168
|
|
|
dol_syslog(get_class($this)."::select_db database=".$database, LOG_DEBUG); |
|
169
|
|
|
return $this->db->select_db($database); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Connect to server |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $host database server host |
|
177
|
|
|
* @param string $login login |
|
178
|
|
|
* @param string $passwd password |
|
179
|
|
|
* @param string $name name of database (not used for mysql, used for pgsql) |
|
180
|
|
|
* @param integer $port Port of database server |
|
181
|
|
|
* @return mysqli Database access object |
|
182
|
|
|
* @see close |
|
183
|
|
|
*/ |
|
184
|
|
|
function connect($host, $login, $passwd, $name, $port = 0) |
|
185
|
|
|
{ |
|
186
|
|
|
dol_syslog(get_class($this) . "::connect host=$host, port=$port, login=$login, passwd=--hidden--, name=$name", LOG_DEBUG); |
|
187
|
|
|
|
|
188
|
|
|
return new mysqli($host, $login, $passwd, $name, $port); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Return version of database server |
|
193
|
|
|
* |
|
194
|
|
|
* @return string Version string |
|
195
|
|
|
*/ |
|
196
|
|
|
function getVersion() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->db->server_info; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Return version of database client driver |
|
203
|
|
|
* |
|
204
|
|
|
* @return string Version string |
|
205
|
|
|
*/ |
|
206
|
|
|
function getDriverInfo() |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->db->client_info; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Close database connexion |
|
214
|
|
|
* |
|
215
|
|
|
* @return bool True if disconnect successfull, false otherwise |
|
216
|
|
|
* @see connect |
|
217
|
|
|
*/ |
|
218
|
|
|
function close() |
|
219
|
|
|
{ |
|
220
|
|
|
if ($this->db) |
|
221
|
|
|
{ |
|
222
|
|
|
if ($this->transaction_opened > 0) dol_syslog(get_class($this)."::close Closing a connection with an opened transaction depth=".$this->transaction_opened,LOG_ERR); |
|
223
|
|
|
$this->connected=false; |
|
224
|
|
|
return $this->db->close(); |
|
225
|
|
|
} |
|
226
|
|
|
return false; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Execute a SQL request and return the resultset |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $query SQL query string |
|
233
|
|
|
* @param int $usesavepoint 0=Default mode, 1=Run a savepoint before and a rollbock to savepoint if error (this allow to have some request with errors inside global transactions). |
|
234
|
|
|
* Note that with Mysql, this parameter is not used as Myssql can already commit a transaction even if one request is in error, without using savepoints. |
|
235
|
|
|
* @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...) |
|
236
|
|
|
* @return bool|mysqli_result Resultset of answer |
|
237
|
|
|
*/ |
|
238
|
|
|
function query($query,$usesavepoint=0,$type='auto') |
|
239
|
|
|
{ |
|
240
|
|
|
global $conf; |
|
241
|
|
|
|
|
242
|
|
|
$query = trim($query); |
|
243
|
|
|
|
|
244
|
|
|
if (! in_array($query,array('BEGIN','COMMIT','ROLLBACK'))) dol_syslog('sql='.$query, LOG_DEBUG); |
|
245
|
|
|
|
|
246
|
|
|
if (! $this->database_name) |
|
247
|
|
|
{ |
|
248
|
|
|
// Ordre SQL ne necessitant pas de connexion a une base (exemple: CREATE DATABASE) |
|
249
|
|
|
$ret = $this->db->query($query); |
|
250
|
|
|
} |
|
251
|
|
|
else |
|
252
|
|
|
{ |
|
253
|
|
|
$ret = $this->db->query($query); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if (! preg_match("/^COMMIT/i",$query) && ! preg_match("/^ROLLBACK/i",$query)) |
|
257
|
|
|
{ |
|
258
|
|
|
// Si requete utilisateur, on la sauvegarde ainsi que son resultset |
|
259
|
|
|
if (! $ret) |
|
260
|
|
|
{ |
|
261
|
|
|
$this->lastqueryerror = $query; |
|
262
|
|
|
$this->lasterror = $this->error(); |
|
263
|
|
|
$this->lasterrno = $this->errno(); |
|
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
if ($conf->global->SYSLOG_LEVEL < LOG_DEBUG) dol_syslog(get_class($this)."::query SQL Error query: ".$query, LOG_ERR); // Log of request was not yet done previously |
|
266
|
|
|
dol_syslog(get_class($this)."::query SQL Error message: ".$this->lasterrno." ".$this->lasterror, LOG_ERR); |
|
267
|
|
|
} |
|
268
|
|
|
$this->lastquery=$query; |
|
269
|
|
|
$this->_results = $ret; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return $ret; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Renvoie la ligne courante (comme un objet) pour le curseur resultset |
|
277
|
|
|
* |
|
278
|
|
|
* @param mysqli_result $resultset Curseur de la requete voulue |
|
279
|
|
|
* @return object|null Object result line or null if KO or end of cursor |
|
280
|
|
|
*/ |
|
281
|
|
|
function fetch_object($resultset) |
|
282
|
|
|
{ |
|
283
|
|
|
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion |
|
284
|
|
|
if (! is_object($resultset)) { $resultset=$this->_results; } |
|
285
|
|
|
return $resultset->fetch_object(); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Return datas as an array |
|
291
|
|
|
* |
|
292
|
|
|
* @param mysqli_result $resultset Resultset of request |
|
293
|
|
|
* @return array|null Array or null if KO or end of cursor |
|
294
|
|
|
*/ |
|
295
|
|
|
function fetch_array($resultset) |
|
296
|
|
|
{ |
|
297
|
|
|
// If resultset not provided, we take the last used by connexion |
|
298
|
|
|
if (! is_object($resultset)) { $resultset=$this->_results; } |
|
299
|
|
|
return $resultset->fetch_array(); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Return datas as an array |
|
304
|
|
|
* |
|
305
|
|
|
* @param mysqli_result $resultset Resultset of request |
|
306
|
|
|
* @return array|null|0 Array or null if KO or end of cursor or 0 if resultset is bool |
|
307
|
|
|
*/ |
|
308
|
|
|
function fetch_row($resultset) |
|
309
|
|
|
{ |
|
310
|
|
|
// If resultset not provided, we take the last used by connexion |
|
311
|
|
|
if (! is_bool($resultset)) |
|
312
|
|
|
{ |
|
313
|
|
|
if (! is_object($resultset)) { $resultset=$this->_results; } |
|
314
|
|
|
return $resultset->fetch_row(); |
|
315
|
|
|
} |
|
316
|
|
|
else |
|
317
|
|
|
{ |
|
318
|
|
|
// si le curseur est un booleen on retourne la valeur 0 |
|
319
|
|
|
return 0; |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Return number of lines for result of a SELECT |
|
325
|
|
|
* |
|
326
|
|
|
* @param mysqli_result $resultset Resulset of requests |
|
327
|
|
|
* @return int Nb of lines |
|
328
|
|
|
* @see affected_rows |
|
329
|
|
|
*/ |
|
330
|
|
|
function num_rows($resultset) |
|
331
|
|
|
{ |
|
332
|
|
|
// If resultset not provided, we take the last used by connexion |
|
333
|
|
|
if (! is_object($resultset)) { $resultset=$this->_results; } |
|
334
|
|
|
return $resultset->num_rows; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Renvoie le nombre de lignes dans le resultat d'une requete INSERT, DELETE ou UPDATE |
|
339
|
|
|
* |
|
340
|
|
|
* @param mysqli_result $resultset Curseur de la requete voulue |
|
341
|
|
|
* @return int Nombre de lignes |
|
342
|
|
|
* @see num_rows |
|
343
|
|
|
*/ |
|
344
|
|
|
function affected_rows($resultset) |
|
345
|
|
|
{ |
|
346
|
|
|
// If resultset not provided, we take the last used by connexion |
|
347
|
|
|
if (! is_object($resultset)) { $resultset=$this->_results; } |
|
348
|
|
|
// mysql necessite un link de base pour cette fonction contrairement |
|
349
|
|
|
// a pqsql qui prend un resultset |
|
350
|
|
|
return $this->db->affected_rows; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* Libere le dernier resultset utilise sur cette connexion |
|
356
|
|
|
* |
|
357
|
|
|
* @param mysqli_result $resultset Curseur de la requete voulue |
|
358
|
|
|
* @return void |
|
359
|
|
|
*/ |
|
360
|
|
|
function free($resultset=null) |
|
361
|
|
|
{ |
|
362
|
|
|
// If resultset not provided, we take the last used by connexion |
|
363
|
|
|
if (! is_object($resultset)) { $resultset=$this->_results; } |
|
364
|
|
|
// Si resultset en est un, on libere la memoire |
|
365
|
|
|
if (is_object($resultset)) $resultset->free_result(); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Escape a string to insert data |
|
370
|
|
|
* |
|
371
|
|
|
* @param string $stringtoencode String to escape |
|
372
|
|
|
* @return string String escaped |
|
373
|
|
|
*/ |
|
374
|
|
|
function escape($stringtoencode) |
|
375
|
|
|
{ |
|
376
|
|
|
return $this->db->real_escape_string($stringtoencode); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Return generic error code of last operation. |
|
381
|
|
|
* |
|
382
|
|
|
* @return string Error code (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...) |
|
383
|
|
|
*/ |
|
384
|
|
|
function errno() |
|
385
|
|
|
{ |
|
386
|
|
|
if (! $this->connected) { |
|
387
|
|
|
// Si il y a eu echec de connexion, $this->db n'est pas valide. |
|
388
|
|
|
return 'DB_ERROR_FAILED_TO_CONNECT'; |
|
389
|
|
|
} else { |
|
390
|
|
|
// Constants to convert a MySql error code to a generic Dolibarr error code |
|
391
|
|
|
$errorcode_map = array( |
|
392
|
|
|
1004 => 'DB_ERROR_CANNOT_CREATE', |
|
393
|
|
|
1005 => 'DB_ERROR_CANNOT_CREATE', |
|
394
|
|
|
1006 => 'DB_ERROR_CANNOT_CREATE', |
|
395
|
|
|
1007 => 'DB_ERROR_ALREADY_EXISTS', |
|
396
|
|
|
1008 => 'DB_ERROR_CANNOT_DROP', |
|
397
|
|
|
1022 => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS', |
|
398
|
|
|
1025 => 'DB_ERROR_NO_FOREIGN_KEY_TO_DROP', |
|
399
|
|
|
1044 => 'DB_ERROR_ACCESSDENIED', |
|
400
|
|
|
1046 => 'DB_ERROR_NODBSELECTED', |
|
401
|
|
|
1048 => 'DB_ERROR_CONSTRAINT', |
|
402
|
|
|
1050 => 'DB_ERROR_TABLE_ALREADY_EXISTS', |
|
403
|
|
|
1051 => 'DB_ERROR_NOSUCHTABLE', |
|
404
|
|
|
1054 => 'DB_ERROR_NOSUCHFIELD', |
|
405
|
|
|
1060 => 'DB_ERROR_COLUMN_ALREADY_EXISTS', |
|
406
|
|
|
1061 => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS', |
|
407
|
|
|
1062 => 'DB_ERROR_RECORD_ALREADY_EXISTS', |
|
408
|
|
|
1064 => 'DB_ERROR_SYNTAX', |
|
409
|
|
|
1068 => 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS', |
|
410
|
|
|
1075 => 'DB_ERROR_CANT_DROP_PRIMARY_KEY', |
|
411
|
|
|
1091 => 'DB_ERROR_NOSUCHFIELD', |
|
412
|
|
|
1100 => 'DB_ERROR_NOT_LOCKED', |
|
413
|
|
|
1136 => 'DB_ERROR_VALUE_COUNT_ON_ROW', |
|
414
|
|
|
1146 => 'DB_ERROR_NOSUCHTABLE', |
|
415
|
|
|
1215 => 'DB_ERROR_CANNOT_ADD_FOREIGN_KEY_CONSTRAINT', |
|
416
|
|
|
1216 => 'DB_ERROR_NO_PARENT', |
|
417
|
|
|
1217 => 'DB_ERROR_CHILD_EXISTS', |
|
418
|
|
|
1396 => 'DB_ERROR_USER_ALREADY_EXISTS', // When creating user already existing |
|
419
|
|
|
1451 => 'DB_ERROR_CHILD_EXISTS' |
|
420
|
|
|
); |
|
421
|
|
|
|
|
422
|
|
|
if (isset($errorcode_map[$this->db->errno])) { |
|
423
|
|
|
return $errorcode_map[$this->db->errno]; |
|
424
|
|
|
} |
|
425
|
|
|
$errno=$this->db->errno; |
|
426
|
|
|
return ($errno?'DB_ERROR_'.$errno:'0'); |
|
427
|
|
|
} |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* Return description of last error |
|
432
|
|
|
* |
|
433
|
|
|
* @return string Error text |
|
434
|
|
|
*/ |
|
435
|
|
|
function error() |
|
436
|
|
|
{ |
|
437
|
|
|
if (! $this->connected) { |
|
438
|
|
|
// Si il y a eu echec de connexion, $this->db n'est pas valide pour mysqli_error. |
|
439
|
|
|
return 'Not connected. Check setup parameters in conf/conf.php file and your mysql client and server versions'; |
|
440
|
|
|
} |
|
441
|
|
|
else { |
|
442
|
|
|
return $this->db->error; |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Get last ID after an insert INSERT |
|
448
|
|
|
* |
|
449
|
|
|
* @param string $tab Table name concerned by insert. Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql |
|
450
|
|
|
* @param string $fieldid Field name |
|
451
|
|
|
* @return int|string Id of row |
|
452
|
|
|
*/ |
|
453
|
|
|
function last_insert_id($tab,$fieldid='rowid') |
|
454
|
|
|
{ |
|
455
|
|
|
return $this->db->insert_id; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* Encrypt sensitive data in database |
|
460
|
|
|
* Warning: This function includes the escape, so it must use direct value |
|
461
|
|
|
* |
|
462
|
|
|
* @param string $fieldorvalue Field name or value to encrypt |
|
463
|
|
|
* @param int $withQuotes Return string with quotes |
|
464
|
|
|
* @return string XXX(field) or XXX('value') or field or 'value' |
|
465
|
|
|
* |
|
466
|
|
|
*/ |
|
467
|
|
|
function encrypt($fieldorvalue, $withQuotes=0) |
|
468
|
|
|
{ |
|
469
|
|
|
global $conf; |
|
470
|
|
|
|
|
471
|
|
|
// Type of encryption (2: AES (recommended), 1: DES , 0: no encryption) |
|
472
|
|
|
$cryptType = (!empty($conf->db->dolibarr_main_db_encryption)?$conf->db->dolibarr_main_db_encryption:0); |
|
473
|
|
|
|
|
474
|
|
|
//Encryption key |
|
475
|
|
|
$cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:''); |
|
476
|
|
|
|
|
477
|
|
|
$return = ($withQuotes?"'":"").$this->escape($fieldorvalue).($withQuotes?"'":""); |
|
478
|
|
|
|
|
479
|
|
|
if ($cryptType && !empty($cryptKey)) |
|
480
|
|
|
{ |
|
481
|
|
|
if ($cryptType == 2) |
|
482
|
|
|
{ |
|
483
|
|
|
$return = 'AES_ENCRYPT('.$return.',\''.$cryptKey.'\')'; |
|
484
|
|
|
} |
|
485
|
|
|
else if ($cryptType == 1) |
|
486
|
|
|
{ |
|
487
|
|
|
$return = 'DES_ENCRYPT('.$return.',\''.$cryptKey.'\')'; |
|
488
|
|
|
} |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
return $return; |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
/** |
|
495
|
|
|
* Decrypt sensitive data in database |
|
496
|
|
|
* |
|
497
|
|
|
* @param string $value Value to decrypt |
|
498
|
|
|
* @return string Decrypted value if used |
|
499
|
|
|
*/ |
|
500
|
|
|
function decrypt($value) |
|
501
|
|
|
{ |
|
502
|
|
|
global $conf; |
|
503
|
|
|
|
|
504
|
|
|
// Type of encryption (2: AES (recommended), 1: DES , 0: no encryption) |
|
505
|
|
|
$cryptType = (!empty($conf->db->dolibarr_main_db_encryption)?$conf->db->dolibarr_main_db_encryption:0); |
|
506
|
|
|
|
|
507
|
|
|
//Encryption key |
|
508
|
|
|
$cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:''); |
|
509
|
|
|
|
|
510
|
|
|
$return = $value; |
|
511
|
|
|
|
|
512
|
|
|
if ($cryptType && !empty($cryptKey)) |
|
513
|
|
|
{ |
|
514
|
|
|
if ($cryptType == 2) |
|
515
|
|
|
{ |
|
516
|
|
|
$return = 'AES_DECRYPT('.$value.',\''.$cryptKey.'\')'; |
|
517
|
|
|
} |
|
518
|
|
|
else if ($cryptType == 1) |
|
519
|
|
|
{ |
|
520
|
|
|
$return = 'DES_DECRYPT('.$value.',\''.$cryptKey.'\')'; |
|
521
|
|
|
} |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
return $return; |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
|
|
|
|
528
|
|
|
/** |
|
529
|
|
|
* Return connexion ID |
|
530
|
|
|
* |
|
531
|
|
|
* @return string Id connexion |
|
532
|
|
|
*/ |
|
533
|
|
|
function DDLGetConnectId() |
|
534
|
|
|
{ |
|
535
|
|
|
$resql=$this->query('SELECT CONNECTION_ID()'); |
|
536
|
|
|
if ($resql) |
|
537
|
|
|
{ |
|
538
|
|
|
$row=$this->fetch_row($resql); |
|
539
|
|
|
return $row[0]; |
|
540
|
|
|
} |
|
541
|
|
|
else return '?'; |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
/** |
|
545
|
|
|
* Create a new database |
|
546
|
|
|
* Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated |
|
547
|
|
|
* We force to create database with charset this->forcecharset and collate this->forcecollate |
|
548
|
|
|
* |
|
549
|
|
|
* @param string $database Database name to create |
|
550
|
|
|
* @param string $charset Charset used to store data |
|
551
|
|
|
* @param string $collation Charset used to sort data |
|
552
|
|
|
* @param string $owner Username of database owner |
|
553
|
|
|
* @return bool|mysqli_result resource defined if OK, null if KO |
|
554
|
|
|
*/ |
|
555
|
|
|
function DDLCreateDb($database,$charset='',$collation='',$owner='') |
|
556
|
|
|
{ |
|
557
|
|
|
if (empty($charset)) $charset=$this->forcecharset; |
|
558
|
|
|
if (empty($collation)) $collation=$this->forcecollate; |
|
559
|
|
|
|
|
560
|
|
|
// ALTER DATABASE dolibarr_db DEFAULT CHARACTER SET latin DEFAULT COLLATE latin1_swedish_ci |
|
561
|
|
|
$sql = "CREATE DATABASE `".$this->escape($database)."`"; |
|
562
|
|
|
$sql.= " DEFAULT CHARACTER SET `".$this->escape($charset)."` DEFAULT COLLATE `".$this->escape($collation)."`"; |
|
563
|
|
|
|
|
564
|
|
|
dol_syslog($sql,LOG_DEBUG); |
|
565
|
|
|
$ret=$this->query($sql); |
|
566
|
|
|
if (! $ret) |
|
567
|
|
|
{ |
|
568
|
|
|
// We try again for compatibility with Mysql < 4.1.1 |
|
569
|
|
|
$sql = "CREATE DATABASE `".$this->escape($database)."`"; |
|
570
|
|
|
dol_syslog($sql,LOG_DEBUG); |
|
571
|
|
|
$ret=$this->query($sql); |
|
572
|
|
|
} |
|
573
|
|
|
return $ret; |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
/** |
|
577
|
|
|
* List tables into a database |
|
578
|
|
|
* |
|
579
|
|
|
* @param string $database Name of database |
|
580
|
|
|
* @param string $table Nmae of table filter ('xxx%') |
|
581
|
|
|
* @return array List of tables in an array |
|
582
|
|
|
*/ |
|
583
|
|
|
function DDLListTables($database, $table='') |
|
584
|
|
|
{ |
|
585
|
|
|
$listtables=array(); |
|
586
|
|
|
|
|
587
|
|
|
$like = ''; |
|
588
|
|
|
if ($table) $like = "LIKE '".$table."'"; |
|
589
|
|
|
$sql="SHOW TABLES FROM ".$database." ".$like.";"; |
|
590
|
|
|
//print $sql; |
|
591
|
|
|
$result = $this->query($sql); |
|
592
|
|
|
if ($result) |
|
593
|
|
|
{ |
|
594
|
|
|
while($row = $this->fetch_row($result)) |
|
595
|
|
|
{ |
|
596
|
|
|
$listtables[] = $row[0]; |
|
597
|
|
|
} |
|
598
|
|
|
} |
|
599
|
|
|
return $listtables; |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
/** |
|
603
|
|
|
* List information of columns into a table. |
|
604
|
|
|
* |
|
605
|
|
|
* @param string $table Name of table |
|
606
|
|
|
* @return array Tableau des informations des champs de la table |
|
607
|
|
|
*/ |
|
608
|
|
|
function DDLInfoTable($table) |
|
609
|
|
|
{ |
|
610
|
|
|
$infotables=array(); |
|
611
|
|
|
|
|
612
|
|
|
$sql="SHOW FULL COLUMNS FROM ".$table.";"; |
|
613
|
|
|
|
|
614
|
|
|
dol_syslog($sql,LOG_DEBUG); |
|
615
|
|
|
$result = $this->query($sql); |
|
616
|
|
|
if ($result) |
|
617
|
|
|
{ |
|
618
|
|
|
while($row = $this->fetch_row($result)) |
|
619
|
|
|
{ |
|
620
|
|
|
$infotables[] = $row; |
|
621
|
|
|
} |
|
622
|
|
|
} |
|
623
|
|
|
return $infotables; |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
/** |
|
627
|
|
|
* Create a table into database |
|
628
|
|
|
* |
|
629
|
|
|
* @param string $table Nom de la table |
|
630
|
|
|
* @param array $fields Tableau associatif [nom champ][tableau des descriptions] |
|
631
|
|
|
* @param string $primary_key Nom du champ qui sera la clef primaire |
|
632
|
|
|
* @param string $type Type de la table |
|
633
|
|
|
* @param array $unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur |
|
634
|
|
|
* @param array $fulltext_keys Tableau des Nom de champs qui seront indexes en fulltext |
|
635
|
|
|
* @param array $keys Tableau des champs cles noms => valeur |
|
636
|
|
|
* @return int <0 if KO, >=0 if OK |
|
637
|
|
|
*/ |
|
638
|
|
|
function DDLCreateTable($table,$fields,$primary_key,$type,$unique_keys=null,$fulltext_keys=null,$keys=null) |
|
639
|
|
|
{ |
|
640
|
|
|
// FIXME: $fulltext_keys parameter is unused |
|
641
|
|
|
|
|
642
|
|
|
// cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra |
|
643
|
|
|
// ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment'); |
|
644
|
|
|
$sql = "CREATE TABLE ".$table."("; |
|
645
|
|
|
$i=0; |
|
646
|
|
|
foreach($fields as $field_name => $field_desc) |
|
647
|
|
|
{ |
|
648
|
|
|
$sqlfields[$i] = $field_name." "; |
|
|
|
|
|
|
649
|
|
|
$sqlfields[$i] .= $field_desc['type']; |
|
|
|
|
|
|
650
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['value'])) { |
|
651
|
|
|
$sqlfields[$i] .= "(".$field_desc['value'].")"; |
|
652
|
|
|
} |
|
653
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['attribute'])) { |
|
654
|
|
|
$sqlfields[$i] .= " ".$field_desc['attribute']; |
|
655
|
|
|
} |
|
656
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['default'])) |
|
657
|
|
|
{ |
|
658
|
|
|
if ((preg_match("/null/i",$field_desc['default'])) || (preg_match("/CURRENT_TIMESTAMP/i",$field_desc['default']))) { |
|
659
|
|
|
$sqlfields[$i] .= " default ".$field_desc['default']; |
|
660
|
|
|
} |
|
661
|
|
|
else { |
|
662
|
|
|
$sqlfields[$i] .= " default '".$field_desc['default']."'"; |
|
663
|
|
|
} |
|
664
|
|
|
} |
|
665
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['null'])) { |
|
666
|
|
|
$sqlfields[$i] .= " ".$field_desc['null']; |
|
667
|
|
|
} |
|
668
|
|
|
if( preg_match("/^[^\s]/i",$field_desc['extra'])) { |
|
669
|
|
|
$sqlfields[$i] .= " ".$field_desc['extra']; |
|
670
|
|
|
} |
|
671
|
|
|
$i++; |
|
672
|
|
|
} |
|
673
|
|
|
if($primary_key != "") |
|
674
|
|
|
$pk = "primary key(".$primary_key.")"; |
|
675
|
|
|
|
|
676
|
|
|
if(is_array($unique_keys)) { |
|
677
|
|
|
$i = 0; |
|
678
|
|
|
foreach($unique_keys as $key => $value) |
|
679
|
|
|
{ |
|
680
|
|
|
$sqluq[$i] = "UNIQUE KEY '".$key."' ('".$value."')"; |
|
|
|
|
|
|
681
|
|
|
$i++; |
|
682
|
|
|
} |
|
683
|
|
|
} |
|
684
|
|
|
if(is_array($keys)) |
|
685
|
|
|
{ |
|
686
|
|
|
$i = 0; |
|
687
|
|
|
foreach($keys as $key => $value) |
|
688
|
|
|
{ |
|
689
|
|
|
$sqlk[$i] = "KEY ".$key." (".$value.")"; |
|
|
|
|
|
|
690
|
|
|
$i++; |
|
691
|
|
|
} |
|
692
|
|
|
} |
|
693
|
|
|
$sql .= implode(',',$sqlfields); |
|
694
|
|
|
if($primary_key != "") |
|
695
|
|
|
$sql .= ",".$pk; |
|
|
|
|
|
|
696
|
|
|
if($unique_keys != "") |
|
697
|
|
|
$sql .= ",".implode(',',$sqluq); |
|
|
|
|
|
|
698
|
|
|
if(is_array($keys)) |
|
699
|
|
|
$sql .= ",".implode(',',$sqlk); |
|
|
|
|
|
|
700
|
|
|
$sql .=") engine=".$type; |
|
701
|
|
|
|
|
702
|
|
|
dol_syslog($sql,LOG_DEBUG); |
|
703
|
|
|
if(! $this -> query($sql)) |
|
704
|
|
|
return -1; |
|
705
|
|
|
else |
|
706
|
|
|
return 1; |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* Return a pointer of line with description of a table or field |
|
711
|
|
|
* |
|
712
|
|
|
* @param string $table Name of table |
|
713
|
|
|
* @param string $field Optionnel : Name of field if we want description of field |
|
714
|
|
|
* @return bool|mysqli_result Resultset x (x->Field, x->Type, ...) |
|
715
|
|
|
*/ |
|
716
|
|
|
function DDLDescTable($table,$field="") |
|
717
|
|
|
{ |
|
718
|
|
|
$sql="DESC ".$table." ".$field; |
|
719
|
|
|
|
|
720
|
|
|
dol_syslog(get_class($this)."::DDLDescTable ".$sql,LOG_DEBUG); |
|
721
|
|
|
$this->_results = $this->query($sql); |
|
|
|
|
|
|
722
|
|
|
return $this->_results; |
|
723
|
|
|
} |
|
724
|
|
|
|
|
725
|
|
|
/** |
|
726
|
|
|
* Create a new field into table |
|
727
|
|
|
* |
|
728
|
|
|
* @param string $table Name of table |
|
729
|
|
|
* @param string $field_name Name of field to add |
|
730
|
|
|
* @param string $field_desc Tableau associatif de description du champ a inserer[nom du parametre][valeur du parametre] |
|
731
|
|
|
* @param string $field_position Optionnel ex.: "after champtruc" |
|
732
|
|
|
* @return int <0 if KO, >0 if OK |
|
733
|
|
|
*/ |
|
734
|
|
|
function DDLAddField($table,$field_name,$field_desc,$field_position="") |
|
735
|
|
|
{ |
|
736
|
|
|
// cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra |
|
737
|
|
|
// ex. : $field_desc = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment'); |
|
738
|
|
|
$sql= "ALTER TABLE ".$table." ADD ".$field_name." "; |
|
739
|
|
|
$sql.= $field_desc['type']; |
|
740
|
|
|
if(preg_match("/^[^\s]/i",$field_desc['value'])) |
|
741
|
|
|
if (! in_array($field_desc['type'],array('date','datetime'))) |
|
742
|
|
|
{ |
|
743
|
|
|
$sql.= "(".$field_desc['value'].")"; |
|
744
|
|
|
} |
|
745
|
|
|
if(preg_match("/^[^\s]/i",$field_desc['attribute'])) |
|
746
|
|
|
$sql.= " ".$field_desc['attribute']; |
|
747
|
|
|
if(preg_match("/^[^\s]/i",$field_desc['null'])) |
|
748
|
|
|
$sql.= " ".$field_desc['null']; |
|
749
|
|
|
if(preg_match("/^[^\s]/i",$field_desc['default'])) |
|
750
|
|
|
{ |
|
751
|
|
|
if(preg_match("/null/i",$field_desc['default'])) |
|
752
|
|
|
$sql.= " default ".$field_desc['default']; |
|
753
|
|
|
else |
|
754
|
|
|
$sql.= " default '".$field_desc['default']."'"; |
|
755
|
|
|
} |
|
756
|
|
|
if(preg_match("/^[^\s]/i",$field_desc['extra'])) |
|
757
|
|
|
$sql.= " ".$field_desc['extra']; |
|
758
|
|
|
$sql.= " ".$field_position; |
|
759
|
|
|
|
|
760
|
|
|
dol_syslog(get_class($this)."::DDLAddField ".$sql,LOG_DEBUG); |
|
761
|
|
|
if($this->query($sql)) { |
|
762
|
|
|
return 1; |
|
763
|
|
|
} |
|
764
|
|
|
return -1; |
|
765
|
|
|
} |
|
766
|
|
|
|
|
767
|
|
|
/** |
|
768
|
|
|
* Update format of a field into a table |
|
769
|
|
|
* |
|
770
|
|
|
* @param string $table Name of table |
|
771
|
|
|
* @param string $field_name Name of field to modify |
|
772
|
|
|
* @param string $field_desc Array with description of field format |
|
773
|
|
|
* @return int <0 if KO, >0 if OK |
|
774
|
|
|
*/ |
|
775
|
|
|
function DDLUpdateField($table,$field_name,$field_desc) |
|
776
|
|
|
{ |
|
777
|
|
|
$sql = "ALTER TABLE ".$table; |
|
778
|
|
|
$sql .= " MODIFY COLUMN ".$field_name." ".$field_desc['type']; |
|
779
|
|
|
if ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int' || $field_desc['type'] == 'varchar') { |
|
780
|
|
|
$sql.="(".$field_desc['value'].")"; |
|
781
|
|
|
} |
|
782
|
|
|
if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL') |
|
783
|
|
|
{ |
|
784
|
|
|
// We will try to change format of column to NOT NULL. To be sure the ALTER works, we try to update fields that are NULL |
|
785
|
|
|
if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text') |
|
786
|
|
|
{ |
|
787
|
|
|
$sqlbis="UPDATE ".$table." SET ".$field_name." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$field_name." IS NULL"; |
|
788
|
|
|
$this->query($sqlbis); |
|
789
|
|
|
} |
|
790
|
|
|
elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') |
|
791
|
|
|
{ |
|
792
|
|
|
$sqlbis="UPDATE ".$table." SET ".$field_name." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$field_name." IS NULL"; |
|
793
|
|
|
$this->query($sqlbis); |
|
794
|
|
|
} |
|
795
|
|
|
|
|
796
|
|
|
$sql.=" NOT NULL"; |
|
797
|
|
|
} |
|
798
|
|
|
|
|
799
|
|
|
if ($field_desc['default']) |
|
800
|
|
|
{ |
|
801
|
|
|
if ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql.=" DEFAULT ".$this->escape($field_desc['default']); |
|
802
|
|
|
elseif ($field_desc['type'] == 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields |
|
803
|
|
|
} |
|
804
|
|
|
|
|
805
|
|
|
dol_syslog(get_class($this)."::DDLUpdateField ".$sql,LOG_DEBUG); |
|
806
|
|
|
if (! $this->query($sql)) |
|
807
|
|
|
return -1; |
|
808
|
|
|
else |
|
809
|
|
|
return 1; |
|
810
|
|
|
} |
|
811
|
|
|
|
|
812
|
|
|
/** |
|
813
|
|
|
* Drop a field from table |
|
814
|
|
|
* |
|
815
|
|
|
* @param string $table Name of table |
|
816
|
|
|
* @param string $field_name Name of field to drop |
|
817
|
|
|
* @return int <0 if KO, >0 if OK |
|
818
|
|
|
*/ |
|
819
|
|
|
function DDLDropField($table,$field_name) |
|
820
|
|
|
{ |
|
821
|
|
|
$sql= "ALTER TABLE ".$table." DROP COLUMN `".$field_name."`"; |
|
822
|
|
|
dol_syslog(get_class($this)."::DDLDropField ".$sql,LOG_DEBUG); |
|
823
|
|
|
if ($this->query($sql)) { |
|
824
|
|
|
return 1; |
|
825
|
|
|
} |
|
826
|
|
|
$this->error=$this->lasterror(); |
|
827
|
|
|
return -1; |
|
828
|
|
|
} |
|
829
|
|
|
|
|
830
|
|
|
|
|
831
|
|
|
/** |
|
832
|
|
|
* Create a user and privileges to connect to database (even if database does not exists yet) |
|
833
|
|
|
* |
|
834
|
|
|
* @param string $dolibarr_main_db_host Ip server or '%' |
|
835
|
|
|
* @param string $dolibarr_main_db_user Nom user a creer |
|
836
|
|
|
* @param string $dolibarr_main_db_pass Mot de passe user a creer |
|
837
|
|
|
* @param string $dolibarr_main_db_name Database name where user must be granted |
|
838
|
|
|
* @return int <0 if KO, >=0 if OK |
|
839
|
|
|
*/ |
|
840
|
|
|
function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name) |
|
841
|
|
|
{ |
|
842
|
|
|
$sql = "CREATE USER '".$this->escape($dolibarr_main_db_user)."'"; |
|
843
|
|
|
dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log |
|
844
|
|
|
$resql=$this->query($sql); |
|
845
|
|
|
if (! $resql) |
|
846
|
|
|
{ |
|
847
|
|
|
if ($this->lasterrno != 'DB_ERROR_USER_ALREADY_EXISTS') |
|
848
|
|
|
{ |
|
849
|
|
|
return -1; |
|
850
|
|
|
} |
|
851
|
|
|
else |
|
852
|
|
|
{ |
|
853
|
|
|
// If user already exists, we continue to set permissions |
|
854
|
|
|
dol_syslog(get_class($this)."::DDLCreateUser sql=".$sql, LOG_WARNING); |
|
855
|
|
|
} |
|
856
|
|
|
} |
|
857
|
|
|
$sql = "GRANT ALL PRIVILEGES ON ".$this->escape($dolibarr_main_db_name).".* TO '".$this->escape($dolibarr_main_db_user)."'@'".$this->escape($dolibarr_main_db_host)."' IDENTIFIED BY '".$this->escape($dolibarr_main_db_pass)."'"; |
|
858
|
|
|
dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log |
|
859
|
|
|
$resql=$this->query($sql); |
|
860
|
|
|
if (! $resql) |
|
861
|
|
|
{ |
|
862
|
|
|
return -1; |
|
863
|
|
|
} |
|
864
|
|
|
|
|
865
|
|
|
$sql="FLUSH Privileges"; |
|
866
|
|
|
|
|
867
|
|
|
dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); |
|
868
|
|
|
$resql=$this->query($sql); |
|
869
|
|
|
if (! $resql) |
|
870
|
|
|
{ |
|
871
|
|
|
return -1; |
|
872
|
|
|
} |
|
873
|
|
|
|
|
874
|
|
|
return 1; |
|
875
|
|
|
} |
|
876
|
|
|
|
|
877
|
|
|
/** |
|
878
|
|
|
* Return charset used to store data in current database (same result than using SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "databasename";) |
|
879
|
|
|
* |
|
880
|
|
|
* @return string Charset |
|
881
|
|
|
* @see getDefaultCollationDatabase |
|
882
|
|
|
*/ |
|
883
|
|
|
function getDefaultCharacterSetDatabase() |
|
884
|
|
|
{ |
|
885
|
|
|
$resql=$this->query('SHOW VARIABLES LIKE \'character_set_database\''); |
|
886
|
|
|
if (!$resql) |
|
887
|
|
|
{ |
|
888
|
|
|
// version Mysql < 4.1.1 |
|
889
|
|
|
return $this->forcecharset; |
|
890
|
|
|
} |
|
891
|
|
|
$liste=$this->fetch_array($resql); |
|
892
|
|
|
$tmpval = $liste['Value']; |
|
893
|
|
|
|
|
894
|
|
|
return $tmpval; |
|
895
|
|
|
} |
|
896
|
|
|
|
|
897
|
|
|
/** |
|
898
|
|
|
* Return list of available charset that can be used to store data in database |
|
899
|
|
|
* |
|
900
|
|
|
* @return array|null List of Charset |
|
901
|
|
|
*/ |
|
902
|
|
|
function getListOfCharacterSet() |
|
903
|
|
|
{ |
|
904
|
|
|
$resql=$this->query('SHOW CHARSET'); |
|
905
|
|
|
$liste = array(); |
|
906
|
|
|
if ($resql) |
|
907
|
|
|
{ |
|
908
|
|
|
$i = 0; |
|
909
|
|
|
while ($obj = $this->fetch_object($resql) ) |
|
910
|
|
|
{ |
|
911
|
|
|
$liste[$i]['charset'] = $obj->Charset; |
|
912
|
|
|
$liste[$i]['description'] = $obj->Description; |
|
913
|
|
|
$i++; |
|
914
|
|
|
} |
|
915
|
|
|
$this->free($resql); |
|
916
|
|
|
} else { |
|
917
|
|
|
// version Mysql < 4.1.1 |
|
918
|
|
|
return null; |
|
919
|
|
|
} |
|
920
|
|
|
return $liste; |
|
921
|
|
|
} |
|
922
|
|
|
|
|
923
|
|
|
/** |
|
924
|
|
|
* Return collation used in current database |
|
925
|
|
|
* |
|
926
|
|
|
* @return string Collation value |
|
927
|
|
|
* @see getDefaultCharacterSetDatabase |
|
928
|
|
|
*/ |
|
929
|
|
|
function getDefaultCollationDatabase() |
|
930
|
|
|
{ |
|
931
|
|
|
$resql=$this->query('SHOW VARIABLES LIKE \'collation_database\''); |
|
932
|
|
|
if (!$resql) |
|
933
|
|
|
{ |
|
934
|
|
|
// version Mysql < 4.1.1 |
|
935
|
|
|
return $this->forcecollate; |
|
936
|
|
|
} |
|
937
|
|
|
$liste=$this->fetch_array($resql); |
|
938
|
|
|
$tmpval = $liste['Value']; |
|
939
|
|
|
|
|
940
|
|
|
return $tmpval; |
|
941
|
|
|
} |
|
942
|
|
|
|
|
943
|
|
|
/** |
|
944
|
|
|
* Return list of available collation that can be used for database |
|
945
|
|
|
* |
|
946
|
|
|
* @return array|null Liste of Collation |
|
947
|
|
|
*/ |
|
948
|
|
|
function getListOfCollation() |
|
949
|
|
|
{ |
|
950
|
|
|
$resql=$this->query('SHOW COLLATION'); |
|
951
|
|
|
$liste = array(); |
|
952
|
|
|
if ($resql) |
|
953
|
|
|
{ |
|
954
|
|
|
$i = 0; |
|
955
|
|
|
while ($obj = $this->fetch_object($resql) ) |
|
956
|
|
|
{ |
|
957
|
|
|
$liste[$i]['collation'] = $obj->Collation; |
|
958
|
|
|
$i++; |
|
959
|
|
|
} |
|
960
|
|
|
$this->free($resql); |
|
961
|
|
|
} else { |
|
962
|
|
|
// version Mysql < 4.1.1 |
|
963
|
|
|
return null; |
|
964
|
|
|
} |
|
965
|
|
|
return $liste; |
|
966
|
|
|
} |
|
967
|
|
|
|
|
968
|
|
|
/** |
|
969
|
|
|
* Return full path of dump program |
|
970
|
|
|
* |
|
971
|
|
|
* @return string Full path of dump program |
|
972
|
|
|
*/ |
|
973
|
|
|
function getPathOfDump() |
|
974
|
|
|
{ |
|
975
|
|
|
$fullpathofdump='/pathtomysqldump/mysqldump'; |
|
976
|
|
|
|
|
977
|
|
|
$resql=$this->query('SHOW VARIABLES LIKE \'basedir\''); |
|
978
|
|
|
if ($resql) |
|
979
|
|
|
{ |
|
980
|
|
|
$liste=$this->fetch_array($resql); |
|
981
|
|
|
$basedir=$liste['Value']; |
|
982
|
|
|
$fullpathofdump=$basedir.(preg_match('/\/$/',$basedir)?'':'/').'bin/mysqldump'; |
|
983
|
|
|
} |
|
984
|
|
|
return $fullpathofdump; |
|
985
|
|
|
} |
|
986
|
|
|
|
|
987
|
|
|
/** |
|
988
|
|
|
* Return full path of restore program |
|
989
|
|
|
* |
|
990
|
|
|
* @return string Full path of restore program |
|
991
|
|
|
*/ |
|
992
|
|
|
function getPathOfRestore() |
|
993
|
|
|
{ |
|
994
|
|
|
$fullpathofimport='/pathtomysql/mysql'; |
|
995
|
|
|
|
|
996
|
|
|
$resql=$this->query('SHOW VARIABLES LIKE \'basedir\''); |
|
997
|
|
|
if ($resql) |
|
998
|
|
|
{ |
|
999
|
|
|
$liste=$this->fetch_array($resql); |
|
1000
|
|
|
$basedir=$liste['Value']; |
|
1001
|
|
|
$fullpathofimport=$basedir.(preg_match('/\/$/',$basedir)?'':'/').'bin/mysql'; |
|
1002
|
|
|
} |
|
1003
|
|
|
return $fullpathofimport; |
|
1004
|
|
|
} |
|
1005
|
|
|
|
|
1006
|
|
|
/** |
|
1007
|
|
|
* Return value of server parameters |
|
1008
|
|
|
* |
|
1009
|
|
|
* @param string $filter Filter list on a particular value |
|
1010
|
|
|
* @return array Array of key-values (key=>value) |
|
1011
|
|
|
*/ |
|
1012
|
|
|
function getServerParametersValues($filter='') |
|
1013
|
|
|
{ |
|
1014
|
|
|
$result=array(); |
|
1015
|
|
|
|
|
1016
|
|
|
$sql='SHOW VARIABLES'; |
|
1017
|
|
|
if ($filter) $sql.=" LIKE '".$this->escape($filter)."'"; |
|
1018
|
|
|
$resql=$this->query($sql); |
|
1019
|
|
|
if ($resql) |
|
1020
|
|
|
{ |
|
1021
|
|
|
while($obj=$this->fetch_object($resql)) $result[$obj->Variable_name]=$obj->Value; |
|
1022
|
|
|
} |
|
1023
|
|
|
|
|
1024
|
|
|
return $result; |
|
1025
|
|
|
} |
|
1026
|
|
|
|
|
1027
|
|
|
/** |
|
1028
|
|
|
* Return value of server status (current indicators on memory, cache...) |
|
1029
|
|
|
* |
|
1030
|
|
|
* @param string $filter Filter list on a particular value |
|
1031
|
|
|
* @return array Array of key-values (key=>value) |
|
1032
|
|
|
*/ |
|
1033
|
|
|
function getServerStatusValues($filter='') |
|
1034
|
|
|
{ |
|
1035
|
|
|
$result=array(); |
|
1036
|
|
|
|
|
1037
|
|
|
$sql='SHOW STATUS'; |
|
1038
|
|
|
if ($filter) $sql.=" LIKE '".$this->escape($filter)."'"; |
|
1039
|
|
|
$resql=$this->query($sql); |
|
1040
|
|
|
if ($resql) |
|
1041
|
|
|
{ |
|
1042
|
|
|
while($obj=$this->fetch_object($resql)) $result[$obj->Variable_name]=$obj->Value; |
|
1043
|
|
|
} |
|
1044
|
|
|
|
|
1045
|
|
|
return $result; |
|
1046
|
|
|
} |
|
1047
|
|
|
} |
|
1048
|
|
|
|
|
1049
|
|
|
|