/**
  * Connect
  *
  * @param   bool reconnect default FALSE
  * @return  bool success
  * @throws  rdbms.SQLConnectException
  */
 public function connect($reconnect = false)
 {
     if (is_resource($this->handle)) {
         return true;
     }
     // Already connected
     if (!$reconnect && false === $this->handle) {
         return false;
     }
     // Previously failed connecting
     $this->_obs && $this->notifyObservers(new \rdbms\DBEvent(\rdbms\DBEvent::CONNECT, $reconnect));
     if ($this->flags & DB_PERSISTENT) {
         $this->handle = mssql_pconnect($this->dsn->getHost(), $this->dsn->getUser(), $this->dsn->getPassword());
     } else {
         $this->handle = mssql_connect($this->dsn->getHost(), $this->dsn->getUser(), $this->dsn->getPassword());
     }
     if (!is_resource($this->handle)) {
         $e = new \rdbms\SQLConnectException(trim(mssql_get_last_message()), $this->dsn);
         \xp::gc(__FILE__);
         throw $e;
     }
     \xp::gc(__FILE__);
     $this->_obs && $this->notifyObservers(new \rdbms\DBEvent(\rdbms\DBEvent::CONNECTED, $reconnect));
     return parent::connect();
 }
Esempio n. 2
0
 /**
  * Connect
  *
  * @param   bool reconnect default FALSE
  * @return  bool success
  * @throws  rdbms.SQLConnectException
  */
 public function connect($reconnect = false)
 {
     if ($this->handle->connected) {
         return true;
     }
     // Already connected
     if (!$reconnect && null === $this->handle->connected) {
         return false;
     }
     // Previously failed connecting
     $this->_obs && $this->notifyObservers(new DBEvent(DBEvent::CONNECT, $reconnect));
     try {
         $this->handle->connect($this->dsn->getUser(), $this->dsn->getPassword(), $this->dsn->getProperty('charset', null));
         $this->_obs && $this->notifyObservers(new DBEvent(DBEvent::CONNECTED, $reconnect));
     } catch (\io\IOException $e) {
         $this->handle->connected = null;
         $this->_obs && $this->notifyObservers(new DBEvent(DBEvent::CONNECTED, $reconnect));
         $message = '';
         do {
             $message .= $e->getMessage() . ': ';
         } while ($e = $e->getCause());
         throw new SQLConnectException(substr($message, 0, -2), $this->dsn);
     }
     return parent::connect();
 }
 /**
  * Connect
  *
  * @param   bool reconnect default FALSE
  * @return  bool success
  * @throws  rdbms.SQLConnectException
  */
 public function connect($reconnect = false)
 {
     if ($this->handle->connected) {
         return true;
     }
     // Already connected
     if (!$reconnect && null === $this->handle->connected) {
         return false;
     }
     // Previously failed connecting
     $this->_obs && $this->notifyObservers(new DBEvent(DBEvent::CONNECT, $reconnect));
     try {
         $this->handle->connect($this->dsn->getUser(), $this->dsn->getPassword());
         $this->_obs && $this->notifyObservers(new DBEvent(DBEvent::CONNECTED, $reconnect));
     } catch (IOException $e) {
         $this->handle->connected = null;
         $this->_obs && $this->notifyObservers(new DBEvent(DBEvent::CONNECTED, $reconnect));
         throw new SQLConnectException($e->getMessage(), $this->dsn);
     }
     try {
         // Figure out sql_mode and update formatter's escaperules accordingly
         // - See: http://bugs.mysql.com/bug.php?id=10214
         // - Possible values: http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
         // "modes is a list of different modes separated by comma (,) characters."
         $query = $this->handle->consume($this->handle->query("show variables like 'sql_mode'"));
         $modes = array_flip(explode(',', $query[0][1]));
     } catch (IOException $e) {
         // Ignore
     }
     // NO_BACKSLASH_ESCAPES: Disable the use of the backslash character
     // (\) as an escape character within strings. With this mode enabled,
     // backslash becomes any ordinary character like any other.
     // (Implemented in MySQL 5.0.1)
     isset($modes['NO_BACKSLASH_ESCAPES']) && $this->formatter->dialect->setEscapeRules(['"' => '""']);
     return parent::connect();
 }
 /**
  * Connect
  *
  * @param   bool reconnect default FALSE
  * @return  bool success
  * @throws  rdbms.SQLConnectException
  */
 public function connect($reconnect = false)
 {
     if (is_resource($this->handle)) {
         return true;
     }
     // Already connected
     if (!$reconnect && false === $this->handle) {
         return false;
     }
     // Previously failed connecting
     // Connect via local sockets if "." is passed. This will not work on
     // Windows with the mysqlnd extension (see PHP bug #48082: "mysql_connect
     // does not work with named pipes"). For mysqlnd, we default to mysqlx
     // anyways, so this works transparently.
     $host = $this->dsn->getHost();
     $ini = null;
     if ('.' === $host) {
         $sock = $this->dsn->getProperty('socket', null);
         if (0 === strncasecmp(PHP_OS, 'Win', 3)) {
             $connect = '.';
             if (null !== $sock) {
                 $ini = ini_set('mysql.default_socket');
                 ini_set('mysql.default_socket', substr($sock, 9));
                 // 9 = strlen("\\\\.\\pipe\\")
             }
         } else {
             $connect = null === $sock ? 'localhost' : ':' . $sock;
         }
     } else {
         if ('localhost' === $host) {
             $connect = '127.0.0.1:' . $this->dsn->getPort(3306);
             // Force TCP/IP
         } else {
             $connect = $host . ':' . $this->dsn->getPort(3306);
         }
     }
     $this->_obs && $this->notifyObservers(new \rdbms\DBEvent(\rdbms\DBEvent::CONNECT, $reconnect));
     if ($this->flags & DB_PERSISTENT) {
         $this->handle = mysql_pconnect($connect, $this->dsn->getUser(), $this->dsn->getPassword());
     } else {
         $this->handle = mysql_connect($connect, $this->dsn->getUser(), $this->dsn->getPassword(), $this->flags & DB_NEWLINK);
     }
     $this->_obs && $this->notifyObservers(new \rdbms\DBEvent(\rdbms\DBEvent::CONNECTED, $reconnect));
     $ini && ini_set('mysql.default_socket', $ini);
     if (!is_resource($this->handle)) {
         $e = new \rdbms\SQLConnectException('#' . mysql_errno() . ': ' . mysql_error(), $this->dsn);
         \xp::gc(__FILE__);
         throw $e;
     }
     mysql_query('set names utf8mb4', $this->handle);
     // Figure out sql_mode and update formatter's escaperules accordingly
     // - See: http://bugs.mysql.com/bug.php?id=10214
     // - Possible values: http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
     // "modes is a list of different modes separated by comma (,) characters."
     $modes = array_flip(explode(',', current(mysql_fetch_row(mysql_query("show variables like 'sql_mode'", $this->handle)))));
     // NO_BACKSLASH_ESCAPES: Disable the use of the backslash character
     // (\) as an escape character within strings. With this mode enabled,
     // backslash becomes any ordinary character like any other.
     // (Implemented in MySQL 5.0.1)
     isset($modes['NO_BACKSLASH_ESCAPES']) && $this->formatter->dialect->setEscapeRules(['"' => '""']);
     return parent::connect();
 }