initial commit
This commit is contained in:
194
Connection.php
Normal file
194
Connection.php
Normal file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace BSR\Lib\db;
|
||||
|
||||
use BSR\Lib\Configuration;
|
||||
use BSR\Lib\Exception\SqlException;
|
||||
|
||||
class Connection
|
||||
{
|
||||
// Internal variable to hold the connection
|
||||
private static $db;
|
||||
|
||||
final private function __construct() {}
|
||||
|
||||
/**
|
||||
* @param $query
|
||||
* @param bool $throw_error
|
||||
* @return OdbcResultSet|resource|string
|
||||
* @throws SqlException
|
||||
*/
|
||||
public static function execute($query, $throw_error = false)
|
||||
{
|
||||
$result = odbc_exec(self::get(), utf8_decode($query));
|
||||
$result = new OdbcResultSet($result);
|
||||
|
||||
if ($result->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 $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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user