Exemple #1
0
 /**
  * Connects to a database.
  * @return void
  * @throws DibiException
  */
 public function connect(array &$config)
 {
     if (isset($config['resource'])) {
         $this->connection = $config['resource'];
     } else {
         if (!isset($config['charset'])) {
             $config['charset'] = 'utf8';
         }
         if (isset($config['string'])) {
             $string = $config['string'];
         } else {
             $string = '';
             DibiConnection::alias($config, 'user', 'username');
             DibiConnection::alias($config, 'dbname', 'database');
             foreach (array('host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service') as $key) {
                 if (isset($config[$key])) {
                     $string .= $key . '=' . $config[$key] . ' ';
                 }
             }
         }
         DibiDriverException::tryError();
         if (empty($config['persistent'])) {
             $this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW);
         } else {
             $this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW);
         }
         if (DibiDriverException::catchError($msg)) {
             throw new DibiDriverException($msg, 0);
         }
     }
     if (!is_resource($this->connection)) {
         throw new DibiDriverException('Connecting error.');
     }
     if (isset($config['charset'])) {
         DibiDriverException::tryError();
         pg_set_client_encoding($this->connection, $config['charset']);
         if (DibiDriverException::catchError($msg)) {
             throw new DibiDriverException($msg, 0);
         }
     }
     if (isset($config['schema'])) {
         $this->query('SET search_path TO "' . $config['schema'] . '"');
     }
     $this->escMethod = version_compare(PHP_VERSION, '5.2.0', '>=');
 }
 /**
  * Executes the SQL query.
  * @param  string      SQL statement.
  * @return IDibiResultDriver|NULL
  * @throws DibiDriverException
  */
 public function query($sql)
 {
     if ($this->dbcharset !== NULL) {
         $sql = iconv($this->charset, $this->dbcharset . '//IGNORE', $sql);
     }
     DibiDriverException::tryError();
     if ($this->buffered) {
         $res = sqlite_query($this->connection, $sql);
     } else {
         $res = sqlite_unbuffered_query($this->connection, $sql);
     }
     if (DibiDriverException::catchError($msg)) {
         throw new DibiDriverException($msg, sqlite_last_error($this->connection), $sql);
     } elseif (is_resource($res)) {
         return $this->createResultDriver($res);
     }
 }
Exemple #3
0
 /**
  * Executes the SQL query.
  *
  * @param  string      SQL statement.
  * @return IDibiDriver|NULL
  * @throws DibiDriverException
  */
 public function query($sql)
 {
     DibiDriverException::tryError();
     if ($this->buffered) {
         $this->resultSet = sqlite_query($this->connection, $sql);
     } else {
         $this->resultSet = sqlite_unbuffered_query($this->connection, $sql);
     }
     if (DibiDriverException::catchError($msg)) {
         throw new DibiDriverException($msg, sqlite_last_error($this->connection), $sql);
     }
     return is_resource($this->resultSet) ? clone $this : NULL;
 }
 /**
  * Fetches the row at current position and moves the internal cursor to the next position.
  * @param  bool     TRUE for associative array, FALSE for numeric
  * @return array    array on success, nonarray if no next record
  */
 public function fetch($assoc)
 {
     DibiDriverException::tryError();
     $result = $assoc ? ibase_fetch_assoc($this->resultSet, IBASE_TEXT) : ibase_fetch_row($this->resultSet, IBASE_TEXT);
     // intentionally @
     if (DibiDriverException::catchError($msg)) {
         if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) {
             preg_match('/exception (\\d+) (\\w+) (.*)/is', ibase_errmsg(), $match);
             throw new DibiProcedureException($match[3], $match[1], $match[2], dibi::$sql);
         } else {
             throw new DibiDriverException($msg, ibase_errcode(), dibi::$sql);
         }
     }
     return $result;
 }