public function instance()
 {
     try {
         $settings = $this->settings;
         $host = Properties::string("host", $settings, false, "127.0.0.1");
         $user = Properties::string("user", $settings, false);
         $password = Properties::string("password", $settings, false);
         $database = Properties::string("database", $settings, false, "");
         $port = Properties::int("port", $settings, false, 3306);
         $autoCommit = Properties::bool("autoCommit", $settings, false, true);
         $charSet = Properties::string("charSet", $settings, false, "utf8");
         //Turn off error reporting for MariaDB compliance.
         $err_level = error_reporting(E_ALL ^ E_WARNING);
         $mysql = new \mysqli($host, $user, $password, $database, $port);
         error_reporting($err_level);
         if ($mysql->connect_error) {
             throw new DbException($mysql->connect_error, $mysql->connect_errno);
         }
         if (!$mysql->set_charset($charSet)) {
             throw new DbException("Could not set charset {$charSet}");
         }
         if ($autoCommit !== null) {
             if (!$mysql->autocommit($autoCommit)) {
                 throw new DbException("Could not set autoCommit to {$autoCommit}");
             }
         }
         return $mysql;
     } catch (PropertyException $e) {
         throw new DbException("Missing setting property {$e->getProperty()}");
     }
 }
Exemple #2
0
 /**
  * Creates a new DB.
  * @param array|\mysqli|MysqlInstanceProvider $config
  * @throws DbException
  */
 public function __construct($mysqlConfig, array $config = null)
 {
     if (is_array($mysqlConfig) || is_object($mysqlConfig)) {
         $this->instanceProvider = new MysqlInstanceProviderArray($mysqlConfig);
     } else {
         if ($mysqlConfig instanceof \mysqli) {
             $this->instanceProvider = new MysqlInstanceProviderConnection($mysqlConfig);
         } else {
             if ($mysqlConfig instanceof MysqlInstanceProvider) {
                 $this->instanceProvider = $mysqlConfig;
             } else {
                 throw new DbException("Db must be configured with an array, mysqli instance or MysqlInstanceProvider");
             }
         }
     }
     if ($rowWrapperFactory = Properties::any("rowWrapperFactory", $config, false)) {
         if ($rowWrapperFactory instanceof RowWrapperFactory) {
             $this->rowWrapperFactory = $rowWrapperFactory;
         } else {
             if (is_string($rowWrapperFactory)) {
                 $this->rowWrapperFactory = new $rowWrapperFactory();
             } else {
                 throw new DbException("config.rowWrapperFactory must be either an instance of RowWrapperFactory or a class name");
             }
         }
     } else {
         $this->rowWrapperFactory = new DefaultRowWrapperFactory();
     }
 }