is_error()) { if($throw_error) { throw new SqlException($result->get_error(), $query); } return $result->get_error(); } return $result; } public static function get() { if (is_null(self::$db)) { $dsn = sprintf( "Driver={%s};Server=%s,%s;Database=%s;", Configuration::get('db.driver'), Configuration::get('db.server'), Configuration::get('db.port'), Configuration::get('db.name') ); self::$db = odbc_pconnect($dsn, Configuration::get('db.username'), Configuration::get('db.password')); if (self::$db === false) { throw new SqlException("Unable to connect to the server."); } } // Return the connection return self::$db; } final private function __clone() {} } class OdbcResultSet implements Iterator, ArrayAccess { public $length; private $results; private $error; private $num_fields; private $cursor_index; public function __construct($odbc_result) { if ($odbc_result === false) { $this->error = odbc_errormsg(Connection::get()); } else { try { $this->results = array(); $this->num_fields = odbc_num_fields($odbc_result); if ($this->num_fields > 0) { while ($row = odbc_fetch_row($odbc_result)) { $data = array(); for ($i = 1; $i <= $this->num_fields; ++$i) { $data[odbc_field_name($odbc_result, $i)] = utf8_encode(odbc_result($odbc_result, $i)); } $this->results[] = $data; } }; } catch (Exception $e) { print($e->getMessage()); } $this->cursor_index = 0; $this->length = count($this->results); odbc_free_result($odbc_result); } } public function is_error() { return ($this->error ? true : false); } public function get_error() { return $this->error; } public function get_row() { return $this->current(); } public function to_array() { return $this->results; } // ArrayAccess /** * @param int $offset * @return bool */ public function offsetExists($offset) { return !$this->error && $this->cursor_index < $this->length && $this->cursor_index >= 0; } /** * @param int $offset * @return bool|array */ public function offsetGet($offset) { return $this->offsetExists($offset) ? $this->results[$offset] : false; } public function offsetSet($offset, $value) { if($this->offsetExists($offset)) { $this->results[$offset] = $value; } } public function offsetUnset($offset) { throw new RuntimeException("This makes no sense at all."); } // Iterator /** * @return bool|array */ public function current() { return $this->offsetGet($this->cursor_index); } /** * @return int */ public function key() { return $this->cursor_index; } /** * @return array|bool */ public function next() { $current = $this->current(); ++$this->cursor_index; return $current; } public function rewind() { $this->cursor_index = 0; } /** * @return bool */ public function valid() { return $this->offsetExists($this->cursor_index); } }