medoo::__construct PHP Method

__construct() public method

public __construct ( $options = null )
    public function __construct($options = null)
    {
        try {
            $commands = array();
            if (is_string($options) && !empty($options)) {
                if (strtolower($this->database_type) == 'sqlite') {
                    $this->database_file = $options;
                } else {
                    $this->database_name = $options;
                }
            } elseif (is_array($options)) {
                foreach ($options as $option => $value) {
                    $this->{$option} = $value;
                }
            }
            if (isset($this->port) && is_int($this->port * 1)) {
                $port = $this->port;
            }
            $type = strtolower($this->database_type);
            $is_port = isset($port);
            switch ($type) {
                case 'mariadb':
                    $type = 'mysql';
                case 'mysql':
                    if ($this->socket) {
                        $dsn = $type . ':unix_socket=' . $this->socket . ';dbname=' . $this->database_name;
                    } else {
                        $dsn = $type . ':host=' . $this->server . ($is_port ? ';port=' . $port : '') . ';dbname=' . $this->database_name;
                    }
                    $commands[] = 'SET SQL_MODE=ANSI_QUOTES';
                    break;
                case 'pgsql':
                    $dsn = $type . ':host=' . $this->server . ($is_port ? ';port=' . $port : '') . ';dbname=' . $this->database_name;
                    break;
                case 'sybase':
                    $dsn = 'dblib:host=' . $this->server . ($is_port ? ':' . $port : '') . ';dbname=' . $this->database_name;
                    break;
                case 'oracle':
                    $dbname = $this->server ? '//' . $this->server . ($is_port ? ':' . $port : ':1521') . '/' . $this->database_name : $this->database_name;
                    $dsn = 'oci:dbname=' . $dbname . ($this->charset ? ';charset=' . $this->charset : '');
                    break;
                case 'mssql':
                    $dsn = strstr(PHP_OS, 'WIN') ? 'sqlsrv:server=' . $this->server . ($is_port ? ',' . $port : '') . ';database=' . $this->database_name : 'dblib:host=' . $this->server . ($is_port ? ':' . $port : '') . ';dbname=' . $this->database_name;
                    $commands[] = 'SET QUOTED_IDENTIFIER ON';
                    break;
                case 'sqlite':
                    $dsn = $type . ':' . $this->database_file;
                    $this->username = null;
                    $this->password = null;
                    break;
            }
            if (in_array($type, explode(' ', 'mariadb mysql pgsql sybase mssql')) && $this->charset) {
                $commands[] = "SET NAMES '" . $this->charset . "'";
            }
            $this->pdo = new PDO($dsn, $this->username, $this->password, $this->option);
            foreach ($commands as $value) {
                $this->pdo->exec($value);
            }
        } catch (PDOException $e) {
            throw new Exception($e->getMessage());
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * @param null $options
  * @throws \Exception
  */
 public function __construct($options = null)
 {
     parent::__construct($options);
     if (isset($options['prefix'])) {
         $this->prefix = $options['prefix'];
     }
 }
All Usage Examples Of medoo::__construct