Skip to content

Instantly share code, notes, and snippets.

@mikeyp mikeyp/FakePDO.php
Created Dec 4, 2013

Embed
What would you like to do?
<?php
/**
* @file
* Fake db statement wrapper to make mssql_query work more like PDO.
*/
/**
* Class FakePDO
*/
class FakePDO implements Iterator {
const FETCH_NUM = 1;
const FETCH_OBJ = 2;
const FETCH_ASSOC = 3;
const FETCH_BOTH = 4;
const FETCH_NAMED = 5;
const FETCH_COLUMN = 6;
/**
* @var MSSQL Query result $result
*/
private $result;
/**
* @var integer $num_rows
* The number of rows returned
*/
private $num_rows;
/**
* @param $position
*/
private $position;
/**
* @var integer $fetch_mode
* The constant representing the fetch mode to be used.
*/
private $fetch_mode;
/**
* @var integer $fetch_argument
* The fetch argument to be used.
*/
private $fetch_argument;
public function __construct($result) {
$this->result = $result;
$this->position = 0;
$this->num_rows = mssql_num_rows($this->result);
}
function rewind() {
$this->position = 0;
}
function current() {
return $this->_fetch();
}
function key() {
return $this->position;
}
function next() {
++$this->position;
}
function valid() {
return $this->position < $this->num_rows;
}
function __destruct() {
mssql_free_result($this->result);
}
public function setFetchMode($mode) {
$this->fetch_mode = $mode;
}
private function _fetch() {
if (!$this->valid()) {
return FALSE;
}
switch ($this->fetch_mode) {
case NULL:
case self::FETCH_OBJ:
mssql_data_seek($this->result, $this->position);
return mssql_fetch_object($this->result);
case self::FETCH_ASSOC:
case self::FETCH_NAMED:
mssql_data_seek($this->result, $this->position);
return mssql_fetch_assoc($this->result);
case self::FETCH_NUM:
mssql_data_seek($this->result, $this->position);
return mssql_fetch_row($this->result);
case self::FETCH_BOTH:
$return = array();
mssql_data_seek($this->result, $this->position);
$return +=mssql_fetch_row($this->result);
mssql_data_seek($this->result, $this->position);
$return += mssql_fetch_assoc($this->result);
return $return;
case self::FETCH_COLUMN:
mssql_data_seek($this->result, $this->position);
return mssql_result($this->result, $this->position, $this->fetch_argument);
}
}
public function fetch($fetch_mode = NULL) {
if ($fetch_mode) {
$this->setFetchMode($fetch_mode);
}
$return = $this->_fetch();
$this->next();
return $return;
}
public function fetchAssoc() {
$return = $this->fetch(self::FETCH_ASSOC);
$this->next();
return $return;
}
public function fetchObject() {
$return = $this->fetch(self::FETCH_OBJ);
$this->next();
return $return;
}
public function fetchField($index = 0) {
if (!$this->valid()) {
return FALSE;
}
$return = mssql_result($this->result, $this->position, $index);
$this->next();
return $return;
}
public function rowCount() {
return $this->num_rows;
}
public function fetchAll($fetch_mode = NULL, $fetch_argument = NULL) {
$return = array();
if ($fetch_mode) {
$this->setFetchMode($fetch_mode);
}
if ($fetch_argument) {
$this->fetch_argument = $fetch_argument;
}
foreach ($this as $record) {
$return[] = $record;
}
return $return;
}
public function fetchAllAssoc($key, $fetch_mode = NULL) {
$return = array();
if ($fetch_mode) {
$this->setFetchMode($fetch_mode);
}
foreach ($this as $record) {
$record_key = is_object($record) ? $record->$key : $record[$key];
$return[$record_key] = $record;
}
return $return;
}
public function fetchAllKeyed($key_index = 0, $value_index = 1) {
$return = array();
$this->setFetchMode(self::FETCH_NUM);
foreach ($this as $record) {
$return[$record[$key_index]] = $record[$value_index];
}
return $return;
}
public function fetchCol($index = 0) {
return $this->fetchAll(self::FETCH_COLUMN, $index);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.