139 lines
3.0 KiB
PHP
139 lines
3.0 KiB
PHP
<?php
|
|
namespace BSR\Db;
|
|
|
|
class OdbcResultSet implements \Iterator, \ArrayAccess
|
|
{
|
|
public $length;
|
|
|
|
private $results;
|
|
private $error;
|
|
private $num_fields;
|
|
private $num_rows;
|
|
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);
|
|
$this->num_rows = odbc_num_rows($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 get_num_rows()
|
|
{
|
|
return $this->num_rows;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|