dolibarr  7.0.0-beta
mysqli.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001 Fabien Seisen <seisen@linuxfr.org>
3  * Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
4  * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
6  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
7  * Copyright (C) 2015 RaphaĆ«l Doursenaud <rdoursenaud@gpcsolutions.fr>
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 
28 require_once DOL_DOCUMENT_ROOT .'/core/db/DoliDB.class.php';
29 
33 class DoliDBMysqli extends DoliDB
34 {
36  public $db;
38  public $type='mysqli';
40  const LABEL='MySQL or MariaDB';
42  const VERSIONMIN='5.0.3';
44  private $_results;
45 
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 is old latin, we force utf8
113  $clientmustbe=empty($conf->db->dolibarr_main_db_character_set)?'utf8':$conf->db->dolibarr_main_db_character_set;
114  if (preg_match('/latin1/', $clientmustbe)) $clientmustbe='utf8';
115 
116  if ($this->db->character_set_name() != $clientmustbe) {
117  $this->db->set_charset($clientmustbe); // This set charset, but with a bad collation
118 
119  $collation = $conf->db->dolibarr_main_db_collation;
120  if (preg_match('/latin1/', $collation)) $collation='utf8_unicode_ci';
121 
122  if (! preg_match('/general/', $collation)) $this->db->query("SET collation_connection = ".$collation);
123  }
124  }
125  else
126  {
127  $this->database_selected = false;
128  $this->database_name = '';
129  $this->ok = false;
130  $this->error=$this->error();
131  dol_syslog(get_class($this)."::DoliDBMysqli : Select_db error ".$this->error,LOG_ERR);
132  }
133  }
134  else
135  {
136  // Pas de selection de base demandee, ok ou ko
137  $this->database_selected = false;
138 
139  if ($this->connected)
140  {
141  // If client is old latin, we force utf8
142  $clientmustbe=empty($conf->db->dolibarr_main_db_character_set)?'utf8':$conf->db->dolibarr_main_db_character_set;
143  if (preg_match('/latin1/', $clientmustbe)) $clientmustbe='utf8';
144 
145  if ($this->db->character_set_name() != $clientmustbe) {
146  $this->db->set_charset($clientmustbe); // This set utf8_general_ci
147 
148  $collation = $conf->db->dolibarr_main_db_collation;
149  if (preg_match('/latin1/', $collation)) $collation='utf8_unicode_ci';
150 
151  if (! preg_match('/general/', $collation)) $this->db->query("SET collation_connection = ".$collation);
152  }
153  }
154  }
155  }
156 
157 
165  static function convertSQLFromMysql($line,$type='ddl')
166  {
167  return $line;
168  }
169 
176  function select_db($database)
177  {
178  dol_syslog(get_class($this)."::select_db database=".$database, LOG_DEBUG);
179  return $this->db->select_db($database);
180  }
181 
182 
194  function connect($host, $login, $passwd, $name, $port = 0)
195  {
196  dol_syslog(get_class($this) . "::connect host=$host, port=$port, login=$login, passwd=--hidden--, name=$name", LOG_DEBUG);
197 
198  // Can also be
199  // mysqli::init(); mysql::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0'); mysqli::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
200  // return mysqli::real_connect($host, $user, $pass, $db, $port);
201  return new mysqli($host, $login, $passwd, $name, $port);
202  }
203 
209  function getVersion()
210  {
211  return $this->db->server_info;
212  }
213 
219  function getDriverInfo()
220  {
221  return $this->db->client_info;
222  }
223 
224 
231  function close()
232  {
233  if ($this->db)
234  {
235  if ($this->transaction_opened > 0) dol_syslog(get_class($this)."::close Closing a connection with an opened transaction depth=".$this->transaction_opened,LOG_ERR);
236  $this->connected=false;
237  return $this->db->close();
238  }
239  return false;
240  }
241 
251  function query($query,$usesavepoint=0,$type='auto')
252  {
253  global $conf;
254 
255  $query = trim($query);
256 
257  if (! in_array($query,array('BEGIN','COMMIT','ROLLBACK'))) dol_syslog('sql='.$query, LOG_DEBUG);
258 
259  if (! $this->database_name)
260  {
261  // Ordre SQL ne necessitant pas de connexion a une base (exemple: CREATE DATABASE)
262  $ret = $this->db->query($query);
263  }
264  else
265  {
266  $ret = $this->db->query($query);
267  }
268 
269  if (! preg_match("/^COMMIT/i",$query) && ! preg_match("/^ROLLBACK/i",$query))
270  {
271  // Si requete utilisateur, on la sauvegarde ainsi que son resultset
272  if (! $ret)
273  {
274  $this->lastqueryerror = $query;
275  $this->lasterror = $this->error();
276  $this->lasterrno = $this->errno();
277 
278  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
279  dol_syslog(get_class($this)."::query SQL Error message: ".$this->lasterrno." ".$this->lasterror, LOG_ERR);
280  }
281  $this->lastquery=$query;
282  $this->_results = $ret;
283  }
284 
285  return $ret;
286  }
287 
294  function fetch_object($resultset)
295  {
296  // Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
297  if (! is_object($resultset)) { $resultset=$this->_results; }
298  return $resultset->fetch_object();
299  }
300 
301 
308  function fetch_array($resultset)
309  {
310  // If resultset not provided, we take the last used by connexion
311  if (! is_object($resultset)) { $resultset=$this->_results; }
312  return $resultset->fetch_array();
313  }
314 
321  function fetch_row($resultset)
322  {
323  // If resultset not provided, we take the last used by connexion
324  if (! is_bool($resultset))
325  {
326  if (! is_object($resultset)) { $resultset=$this->_results; }
327  return $resultset->fetch_row();
328  }
329  else
330  {
331  // si le curseur est un booleen on retourne la valeur 0
332  return 0;
333  }
334  }
335 
343  function num_rows($resultset)
344  {
345  // If resultset not provided, we take the last used by connexion
346  if (! is_object($resultset)) { $resultset=$this->_results; }
347  return $resultset->num_rows;
348  }
349 
357  function affected_rows($resultset)
358  {
359  // If resultset not provided, we take the last used by connexion
360  if (! is_object($resultset)) { $resultset=$this->_results; }
361  // mysql necessite un link de base pour cette fonction contrairement
362  // a pqsql qui prend un resultset
363  return $this->db->affected_rows;
364  }
365 
366 
373  function free($resultset=null)
374  {
375  // If resultset not provided, we take the last used by connexion
376  if (! is_object($resultset)) { $resultset=$this->_results; }
377  // Si resultset en est un, on libere la memoire
378  if (is_object($resultset)) $resultset->free_result();
379  }
380 
387  function escape($stringtoencode)
388  {
389  return $this->db->real_escape_string($stringtoencode);
390  }
391 
397  function errno()
398  {
399  if (! $this->connected) {
400  // Si il y a eu echec de connexion, $this->db n'est pas valide.
401  return 'DB_ERROR_FAILED_TO_CONNECT';
402  } else {
403  // Constants to convert a MySql error code to a generic Dolibarr error code
404  $errorcode_map = array(
405  1004 => 'DB_ERROR_CANNOT_CREATE',
406  1005 => 'DB_ERROR_CANNOT_CREATE',
407  1006 => 'DB_ERROR_CANNOT_CREATE',
408  1007 => 'DB_ERROR_ALREADY_EXISTS',
409  1008 => 'DB_ERROR_CANNOT_DROP',
410  1022 => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
411  1025 => 'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
412  1044 => 'DB_ERROR_ACCESSDENIED',
413  1046 => 'DB_ERROR_NODBSELECTED',
414  1048 => 'DB_ERROR_CONSTRAINT',
415  1050 => 'DB_ERROR_TABLE_ALREADY_EXISTS',
416  1051 => 'DB_ERROR_NOSUCHTABLE',
417  1054 => 'DB_ERROR_NOSUCHFIELD',
418  1060 => 'DB_ERROR_COLUMN_ALREADY_EXISTS',
419  1061 => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
420  1062 => 'DB_ERROR_RECORD_ALREADY_EXISTS',
421  1064 => 'DB_ERROR_SYNTAX',
422  1068 => 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
423  1075 => 'DB_ERROR_CANT_DROP_PRIMARY_KEY',
424  1091 => 'DB_ERROR_NOSUCHFIELD',
425  1100 => 'DB_ERROR_NOT_LOCKED',
426  1136 => 'DB_ERROR_VALUE_COUNT_ON_ROW',
427  1146 => 'DB_ERROR_NOSUCHTABLE',
428  1215 => 'DB_ERROR_CANNOT_ADD_FOREIGN_KEY_CONSTRAINT',
429  1216 => 'DB_ERROR_NO_PARENT',
430  1217 => 'DB_ERROR_CHILD_EXISTS',
431  1396 => 'DB_ERROR_USER_ALREADY_EXISTS', // When creating user already existing
432  1451 => 'DB_ERROR_CHILD_EXISTS'
433  );
434 
435  if (isset($errorcode_map[$this->db->errno])) {
436  return $errorcode_map[$this->db->errno];
437  }
438  $errno=$this->db->errno;
439  return ($errno?'DB_ERROR_'.$errno:'0');
440  }
441  }
442 
448  function error()
449  {
450  if (! $this->connected) {
451  // Si il y a eu echec de connexion, $this->db n'est pas valide pour mysqli_error.
452  return 'Not connected. Check setup parameters in conf/conf.php file and your mysql client and server versions';
453  }
454  else {
455  return $this->db->error;
456  }
457  }
458 
466  function last_insert_id($tab,$fieldid='rowid')
467  {
468  return $this->db->insert_id;
469  }
470 
480  function encrypt($fieldorvalue, $withQuotes=0)
481  {
482  global $conf;
483 
484  // Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
485  $cryptType = (!empty($conf->db->dolibarr_main_db_encryption)?$conf->db->dolibarr_main_db_encryption:0);
486 
487  //Encryption key
488  $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:'');
489 
490  $return = ($withQuotes?"'":"").$this->escape($fieldorvalue).($withQuotes?"'":"");
491 
492  if ($cryptType && !empty($cryptKey))
493  {
494  if ($cryptType == 2)
495  {
496  $return = 'AES_ENCRYPT('.$return.',\''.$cryptKey.'\')';
497  }
498  else if ($cryptType == 1)
499  {
500  $return = 'DES_ENCRYPT('.$return.',\''.$cryptKey.'\')';
501  }
502  }
503 
504  return $return;
505  }
506 
513  function decrypt($value)
514  {
515  global $conf;
516 
517  // Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
518  $cryptType = (!empty($conf->db->dolibarr_main_db_encryption)?$conf->db->dolibarr_main_db_encryption:0);
519 
520  //Encryption key
521  $cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:'');
522 
523  $return = $value;
524 
525  if ($cryptType && !empty($cryptKey))
526  {
527  if ($cryptType == 2)
528  {
529  $return = 'AES_DECRYPT('.$value.',\''.$cryptKey.'\')';
530  }
531  else if ($cryptType == 1)
532  {
533  $return = 'DES_DECRYPT('.$value.',\''.$cryptKey.'\')';
534  }
535  }
536 
537  return $return;
538  }
539 
540 
546  function DDLGetConnectId()
547  {
548  $resql=$this->query('SELECT CONNECTION_ID()');
549  if ($resql)
550  {
551  $row=$this->fetch_row($resql);
552  return $row[0];
553  }
554  else return '?';
555  }
556 
568  function DDLCreateDb($database,$charset='',$collation='',$owner='')
569  {
570  if (empty($charset)) $charset=$this->forcecharset;
571  if (empty($collation)) $collation=$this->forcecollate;
572 
573  // ALTER DATABASE dolibarr_db DEFAULT CHARACTER SET latin DEFAULT COLLATE latin1_swedish_ci
574  $sql = "CREATE DATABASE `".$this->escape($database)."`";
575  $sql.= " DEFAULT CHARACTER SET `".$this->escape($charset)."` DEFAULT COLLATE `".$this->escape($collation)."`";
576 
577  dol_syslog($sql,LOG_DEBUG);
578  $ret=$this->query($sql);
579  if (! $ret)
580  {
581  // We try again for compatibility with Mysql < 4.1.1
582  $sql = "CREATE DATABASE `".$this->escape($database)."`";
583  dol_syslog($sql,LOG_DEBUG);
584  $ret=$this->query($sql);
585  }
586  return $ret;
587  }
588 
596  function DDLListTables($database, $table='')
597  {
598  $listtables=array();
599 
600  $like = '';
601  if ($table) $like = "LIKE '".$table."'";
602  $sql="SHOW TABLES FROM ".$database." ".$like.";";
603  //print $sql;
604  $result = $this->query($sql);
605  if ($result)
606  {
607  while($row = $this->fetch_row($result))
608  {
609  $listtables[] = $row[0];
610  }
611  }
612  return $listtables;
613  }
614 
621  function DDLInfoTable($table)
622  {
623  $infotables=array();
624 
625  $sql="SHOW FULL COLUMNS FROM ".$table.";";
626 
627  dol_syslog($sql,LOG_DEBUG);
628  $result = $this->query($sql);
629  if ($result)
630  {
631  while($row = $this->fetch_row($result))
632  {
633  $infotables[] = $row;
634  }
635  }
636  return $infotables;
637  }
638 
651  function DDLCreateTable($table,$fields,$primary_key,$type,$unique_keys=null,$fulltext_keys=null,$keys=null)
652  {
653  // FIXME: $fulltext_keys parameter is unused
654 
655  // cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra
656  // ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
657  $sql = "CREATE TABLE ".$table."(";
658  $i=0;
659  foreach($fields as $field_name => $field_desc)
660  {
661  $sqlfields[$i] = $field_name." ";
662  $sqlfields[$i] .= $field_desc['type'];
663  if( preg_match("/^[^\s]/i",$field_desc['value'])) {
664  $sqlfields[$i] .= "(".$field_desc['value'].")";
665  }
666  if( preg_match("/^[^\s]/i",$field_desc['attribute'])) {
667  $sqlfields[$i] .= " ".$field_desc['attribute'];
668  }
669  if( preg_match("/^[^\s]/i",$field_desc['default']))
670  {
671  if ((preg_match("/null/i",$field_desc['default'])) || (preg_match("/CURRENT_TIMESTAMP/i",$field_desc['default']))) {
672  $sqlfields[$i] .= " default ".$field_desc['default'];
673  }
674  else {
675  $sqlfields[$i] .= " default '".$field_desc['default']."'";
676  }
677  }
678  if( preg_match("/^[^\s]/i",$field_desc['null'])) {
679  $sqlfields[$i] .= " ".$field_desc['null'];
680  }
681  if( preg_match("/^[^\s]/i",$field_desc['extra'])) {
682  $sqlfields[$i] .= " ".$field_desc['extra'];
683  }
684  $i++;
685  }
686  if($primary_key != "")
687  $pk = "primary key(".$primary_key.")";
688 
689  if(is_array($unique_keys)) {
690  $i = 0;
691  foreach($unique_keys as $key => $value)
692  {
693  $sqluq[$i] = "UNIQUE KEY '".$key."' ('".$value."')";
694  $i++;
695  }
696  }
697  if(is_array($keys))
698  {
699  $i = 0;
700  foreach($keys as $key => $value)
701  {
702  $sqlk[$i] = "KEY ".$key." (".$value.")";
703  $i++;
704  }
705  }
706  $sql .= implode(',',$sqlfields);
707  if($primary_key != "")
708  $sql .= ",".$pk;
709  if($unique_keys != "")
710  $sql .= ",".implode(',',$sqluq);
711  if(is_array($keys))
712  $sql .= ",".implode(',',$sqlk);
713  $sql .=") engine=".$type;
714 
715  if(! $this->query($sql))
716  return -1;
717  else
718  return 1;
719  }
720 
727  function DDLDropTable($table)
728  {
729  $sql = "DROP TABLE ".$table;
730 
731  if (! $this->query($sql))
732  return -1;
733  else
734  return 1;
735  }
736 
744  function DDLDescTable($table,$field="")
745  {
746  $sql="DESC ".$table." ".$field;
747 
748  dol_syslog(get_class($this)."::DDLDescTable ".$sql,LOG_DEBUG);
749  $this->_results = $this->query($sql);
750  return $this->_results;
751  }
752 
762  function DDLAddField($table,$field_name,$field_desc,$field_position="")
763  {
764  // cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
765  // ex. : $field_desc = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
766  $sql= "ALTER TABLE ".$table." ADD ".$field_name." ";
767  $sql.= $field_desc['type'];
768  if(preg_match("/^[^\s]/i",$field_desc['value']))
769  if (! in_array($field_desc['type'],array('date','datetime')))
770  {
771  $sql.= "(".$field_desc['value'].")";
772  }
773  if(preg_match("/^[^\s]/i",$field_desc['attribute']))
774  $sql.= " ".$field_desc['attribute'];
775  if(preg_match("/^[^\s]/i",$field_desc['null']))
776  $sql.= " ".$field_desc['null'];
777  if(preg_match("/^[^\s]/i",$field_desc['default']))
778  {
779  if(preg_match("/null/i",$field_desc['default']))
780  $sql.= " default ".$field_desc['default'];
781  else
782  $sql.= " default '".$field_desc['default']."'";
783  }
784  if(preg_match("/^[^\s]/i",$field_desc['extra']))
785  $sql.= " ".$field_desc['extra'];
786  $sql.= " ".$field_position;
787 
788  dol_syslog(get_class($this)."::DDLAddField ".$sql,LOG_DEBUG);
789  if($this->query($sql)) {
790  return 1;
791  }
792  return -1;
793  }
794 
803  function DDLUpdateField($table,$field_name,$field_desc)
804  {
805  $sql = "ALTER TABLE ".$table;
806  $sql .= " MODIFY COLUMN ".$field_name." ".$field_desc['type'];
807  if ($field_desc['type'] == 'double' || $field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int' || $field_desc['type'] == 'varchar') {
808  $sql.="(".$field_desc['value'].")";
809  }
810  if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL')
811  {
812  // 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
813  if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text')
814  {
815  $sqlbis="UPDATE ".$table." SET ".$field_name." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$field_name." IS NULL";
816  $this->query($sqlbis);
817  }
818  elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int')
819  {
820  $sqlbis="UPDATE ".$table." SET ".$field_name." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$field_name." IS NULL";
821  $this->query($sqlbis);
822  }
823 
824  $sql.=" NOT NULL";
825  }
826 
827  if ($field_desc['default'] != '')
828  {
829  if ($field_desc['type'] == 'double' || $field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql.=" DEFAULT ".$this->escape($field_desc['default']);
830  elseif ($field_desc['type'] == 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields
831  }
832 
833  dol_syslog(get_class($this)."::DDLUpdateField ".$sql,LOG_DEBUG);
834  if (! $this->query($sql))
835  return -1;
836  else
837  return 1;
838  }
839 
847  function DDLDropField($table,$field_name)
848  {
849  $sql= "ALTER TABLE ".$table." DROP COLUMN `".$field_name."`";
850  dol_syslog(get_class($this)."::DDLDropField ".$sql,LOG_DEBUG);
851  if ($this->query($sql)) {
852  return 1;
853  }
854  $this->error=$this->lasterror();
855  return -1;
856  }
857 
858 
868  function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name)
869  {
870  $sql = "CREATE USER '".$this->escape($dolibarr_main_db_user)."'";
871  dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
872  $resql=$this->query($sql);
873  if (! $resql)
874  {
875  if ($this->lasterrno != 'DB_ERROR_USER_ALREADY_EXISTS')
876  {
877  return -1;
878  }
879  else
880  {
881  // If user already exists, we continue to set permissions
882  dol_syslog(get_class($this)."::DDLCreateUser sql=".$sql, LOG_WARNING);
883  }
884  }
885  $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)."'";
886  dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
887  $resql=$this->query($sql);
888  if (! $resql)
889  {
890  return -1;
891  }
892 
893  $sql="FLUSH Privileges";
894 
895  dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);
896  $resql=$this->query($sql);
897  if (! $resql)
898  {
899  return -1;
900  }
901 
902  return 1;
903  }
904 
911  function getDefaultCharacterSetDatabase()
912  {
913  $resql=$this->query('SHOW VARIABLES LIKE \'character_set_database\'');
914  if (!$resql)
915  {
916  // version Mysql < 4.1.1
917  return $this->forcecharset;
918  }
919  $liste=$this->fetch_array($resql);
920  $tmpval = $liste['Value'];
921 
922  return $tmpval;
923  }
924 
931  {
932  $resql=$this->query('SHOW CHARSET');
933  $liste = array();
934  if ($resql)
935  {
936  $i = 0;
937  while ($obj = $this->fetch_object($resql) )
938  {
939  $liste[$i]['charset'] = $obj->Charset;
940  $liste[$i]['description'] = $obj->Description;
941  $i++;
942  }
943  $this->free($resql);
944  } else {
945  // version Mysql < 4.1.1
946  return null;
947  }
948  return $liste;
949  }
950 
958  {
959  $resql=$this->query('SHOW VARIABLES LIKE \'collation_database\'');
960  if (!$resql)
961  {
962  // version Mysql < 4.1.1
963  return $this->forcecollate;
964  }
965  $liste=$this->fetch_array($resql);
966  $tmpval = $liste['Value'];
967 
968  return $tmpval;
969  }
970 
977  {
978  $resql=$this->query('SHOW COLLATION');
979  $liste = array();
980  if ($resql)
981  {
982  $i = 0;
983  while ($obj = $this->fetch_object($resql) )
984  {
985  $liste[$i]['collation'] = $obj->Collation;
986  $i++;
987  }
988  $this->free($resql);
989  } else {
990  // version Mysql < 4.1.1
991  return null;
992  }
993  return $liste;
994  }
995 
1001  function getPathOfDump()
1002  {
1003  $fullpathofdump='/pathtomysqldump/mysqldump';
1004 
1005  $resql=$this->query('SHOW VARIABLES LIKE \'basedir\'');
1006  if ($resql)
1007  {
1008  $liste=$this->fetch_array($resql);
1009  $basedir=$liste['Value'];
1010  $fullpathofdump=$basedir.(preg_match('/\/$/',$basedir)?'':'/').'bin/mysqldump';
1011  }
1012  return $fullpathofdump;
1013  }
1014 
1020  function getPathOfRestore()
1021  {
1022  $fullpathofimport='/pathtomysql/mysql';
1023 
1024  $resql=$this->query('SHOW VARIABLES LIKE \'basedir\'');
1025  if ($resql)
1026  {
1027  $liste=$this->fetch_array($resql);
1028  $basedir=$liste['Value'];
1029  $fullpathofimport=$basedir.(preg_match('/\/$/',$basedir)?'':'/').'bin/mysql';
1030  }
1031  return $fullpathofimport;
1032  }
1033 
1040  function getServerParametersValues($filter='')
1041  {
1042  $result=array();
1043 
1044  $sql='SHOW VARIABLES';
1045  if ($filter) $sql.=" LIKE '".$this->escape($filter)."'";
1046  $resql=$this->query($sql);
1047  if ($resql)
1048  {
1049  while($obj=$this->fetch_object($resql)) $result[$obj->Variable_name]=$obj->Value;
1050  }
1051 
1052  return $result;
1053  }
1054 
1061  function getServerStatusValues($filter='')
1062  {
1063  $result=array();
1064 
1065  $sql='SHOW STATUS';
1066  if ($filter) $sql.=" LIKE '".$this->escape($filter)."'";
1067  $resql=$this->query($sql);
1068  if ($resql)
1069  {
1070  while($obj=$this->fetch_object($resql)) $result[$obj->Variable_name]=$obj->Value;
1071  }
1072 
1073  return $result;
1074  }
1075 }
1076 
getPathOfRestore()
Return full path of restore program.
lastqueryerror()
Return last query in error.
getListOfCharacterSet()
Return list of available charset that can be used to store data in database.
$type
Database type.
getServerStatusValues($filter='')
Return value of server status (current indicators on memory, cache...)
escape($stringtoencode)
Escape a string to insert data.
lastquery()
Return last request executed with query()
fetch_row($resultset)
Return datas as an array.
getPathOfDump()
Return full path of dump program.
select_db($database)
Select a database.
Class to manage Dolibarr database access.
const LABEL
Database label.
last_insert_id($tab, $fieldid='rowid')
Get last ID after an insert INSERT.
lasterror()
Return last error label.
getServerParametersValues($filter='')
Return value of server parameters.
errno()
Return generic error code of last operation.
const VERSIONMIN
Version min database.
error()
Return description of last error.
fetch_object($resultset)
Renvoie la ligne courante (comme un objet) pour le curseur resultset.
connect($host, $login, $passwd, $name, $port=0)
Connect to server.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
encrypt($fieldorvalue, $withQuotes=0)
Encrypt sensitive data in database Warning: This function includes the escape, so it must use direct ...
getDriverInfo()
Return version of database client driver.
query($query, $usesavepoint=0, $type='auto')
Execute a SQL request and return the resultset.
static convertSQLFromMysql($line, $type='ddl')
Convert a SQL request in Mysql syntax to native syntax.
close()
Close database connexion.
num_rows($resultset)
Return number of lines for result of a SELECT.
fetch_array($resultset)
Return datas as an array.
getDefaultCollationDatabase()
Return collation used in current database.
Class to manage Dolibarr database access for a MySQL database using the MySQLi extension.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
lasterrno()
Return last error code.
__construct($type, $host, $user, $pass, $name='', $port=0)
Constructor.
affected_rows($resultset)
Renvoie le nombre de lignes dans le resultat d'une requete INSERT, DELETE ou UPDATE.
getVersion()
Return version of database server.
free($resultset=null)
Libere le dernier resultset utilise sur cette connexion.
type
Definition: viewcat.php:283
getListOfCollation()
Return list of available collation that can be used for database.