/sql/oracle
[return to app]1
<?php
/**
* Converts the procedural Oracle PHP class to object-oriented
* @author Eric David Benari, Chris Jones
*/
class oracle {
/**
* Database connection resource ID and statement resource handle
* @var object
*/
public $connectionResource, $statementResource;
/**
* The database method used to sanitize user input strings
*/
const ESCAPE_STRING = 'escapeString';
/**
* Creates a string sanitation method as it is missing in the Oracle PHP extension
*
* @param string $var
* @return string
*/
public function escapeString($var) {
return str_replace("'", "''", $var);
}
/**
* Parse statement to create a statement resource
* @param string $query
*/
public function parse($query) {
$this->statementResource = oci_parse($this->connectionResource, $query);
}
/**
* Alias for oci_parse() + oci_execute() to update OCI8 to use the modern naming convention
* and not require passing the connection to every statement via argument
*
* @param string $query
*/
public function query($query) {
$this->parse($query);
$this->execute();
}
/**
* Oracle prepared query using "IN" binds (i.e that pass values from PHP to Oracle).
* Pass an array where the keys are the bind variable names and the values are the bind data values. Eg:
* $sql = 'select name, description from widgets where itemid=:it';
* $this->db->opquery($sql, array(":it" => $itemid));
*
* @param string $query
* @param array $bindArgs
*/
public function opquery($query, $bindArgs) {
$this->parse($query);
foreach ($bindArgs as $key => $val) { // location needs to be passed by reference to oci_bind_by_name
oci_bind_by_name($this->statementResource, $key, $bindArgs[$key]);
}
$this->execute();
}
/**
* Connect to Oracle via OCI8
*
* @param string $username
* @param string $passwd
* @param string $connectionString Optional
* @param boolean $persistent Optional default is true
* @param string $charset Optional
* @param int $sessionMode Optional
*/
public function __construct($username, $password, $connectionString = null, $persistent = true,
$charset = null, $sessionMode = null) {
try {
$connect = 'oci_' . ($persistent ? 'pconnect' : 'connect');
$this->connectionResource = $connect($username, $password, $connectionString, $charset,
$sessionMode);
if (!$this->connectionResource) {
throw new Exception('Cannot connect to Oracle');
}
} catch (Exception $e) {
$this->error = $e->getMessage();
}
}
/**
* Functions that require the OCI connection resource as the first argument
* @var array
*/
protected $_connectionFunctions = array(
'close', 'commit', 'new_collection', 'new_cursor', 'new_descriptor', 'rollback', 'server_version',
'set_action', 'set_client_identifier', 'set_client_info', 'set_module_name'
);
/**
* Functions that require the statement resource as the first argument
* @var array
*/
protected $_statementFunctions = array(
'bind_array_by_name', 'bind_by_name', 'cancel', 'define_by_name', 'error', 'execute',
'fetch_all', 'fetch_array', 'fetch_assoc', 'fetch_object', 'fetch_row', 'fetch', 'field_is_null',
'field_name',
'field_precision', 'field_scale', 'field_size', 'field_type_raw', 'field_type', 'free_statement',
'num_fields', 'num_rows', 'result', 'set_prefetch', 'statement_type'
);
/**
* Catch-all method allowing you to call any OCI8 function via camel-cap object-oriented syntax
* and skipping/omitting the initial argument for a connection resource or statement resource.
*
* From your models you could access:
*
* $this->db->fetchAll()
* $this->db->fetchAssoc()
* $this->db->commit()
* $this->db->parse()
* $this->db->result()
* $this->db->rollback()
* $this->db->close()
* etc.
*
* @param string $name
* @param mixed $val
*/
public function __call($name, $val) {
$name = strtolower(preg_replace('/[A-Z]/', '_$0', $name));
if (function_exists('oci_' . $name)) {
if (in_array($name, $this->_statementFunctions)) {
array_unshift($val, $this->statementResource);
} else if (in_array($name, $this->_connectionFunctions)) {
array_unshift($val, $this->connectionResource);
}
return call_user_func_array('oci_' . $name, $val);
}
}
}

