Oricle Database Operations Class
Define (' Oci_return_alls ', Oci_both + oci_return_lobs);
Class Oracle {
/**
* Maximum number of records returned by the Select method
*/
Const MAX_ROW_NUM = 1000;
/**
* Data query result set object
* @var Object $dataSet
*/
Public $dataSet = NULL;
/**
* Data Source objects
* @var Object $ds
*/
Public $ds = NULL;
/**
* SQL statement for query
* @var String $sql
*/
Public $sql = ';
/**
* The mode of executing the query, the value is oci_commit_on_success or Oci_default
* @var String $excuteMode
*/
Public $executeMode = oci_commit_on_success;
/**
* Constructor
* @param Object $ds Database
* @param string $sql The SQL statement to initialize the query
*/
function __construct ($ds =null, $sql =null) {
if (! $ds) {
$this->error (dbexception::D b_unconnected, ' database also
Not connected. ');
} else {
$this->ds = $ds;
if ($sql) {
$this->open ($sql);
}
}
}
/**
* Free up the memory occupied
* @param object $dataSet The result set of the resource that needs to be freed
* @access Public
*/
Public function Close ($dataSet =null) {
if ($dataSet) {
@oci_free_statement ($dataSet);
} else {
@oci_free_statement ($this->dataset);
$this->eof = false;
$this->recordcount = 0;
$this->recno =-1;
}
}
/**
* Database encryption for $pass, returns the value after encryption
* @param string $pass strings to encrypt
* @return String
* @access Public
*/
Public Function Encodepassword ($pass) {
return MD5 ($PASS);
}
/**
* Get error message and error code
* @param integer $queryResult query results
* @return Array
* @access protected
*/
protected function errorinfo ($queryResult = NULL) {
$result = Oci_error ($this->ds->connect);
return $result;
}
/**
* Error Information Processing
* @param string $errorId Error ID
* @param string $errorMessage error message
* @access protected
*/
protected function Error ($ERRORID, $errorMessage) {
throw new Dbexception ($errorMessage, $errorId);
}
/**
* Execute SQL statement
* @param string $sql SQL statement
* @return Object
* @param int $rowFrom start line number, starting from 1
* @param int $rowTo end line number, and a value of 0 indicates
* @access Public
* @see Dbquery::open
*/
Public Function Execute ($sql = ', $rowFrom = 0, $rowTo =
Self::max_row_num) {
if ($rowFrom!= 0 | | | $rowTo!= self::max_row_num) {
$sql = ' SELECT * FROM (select row_.*, RowNum rownum_
From ('
. $sql. ') Row_ where RowNum <= '
. $rowTo. ' Where Rownum_ >= '.
$rowFrom;
}
Echo $sql. ' <br> ';
$start = Microtime (true);
$dataSet = @oci_parse ($this->ds->connect, $sql);
$executeSucceed = @oci_execute ($dataSet, $this-
>executemode);
echo ' sql: '. ((String) (Microtime (True)-$start)). ' <br> ';
if (! $dataSet | |! $executeSucceed) {
$sqlError = $this->errorinfo ();
$errorMessage = ' Execute [<b><font color= ' #FF0000 ' > '.
$sql
. ' </font></b>] An error! <br> <font
color= #FF0000 > ['
. $sqlError [' Code ']. ']: '
. $sqlError [' message ']. ' </font> ';
$this->error (dbexception::D b_query_error,
$errorMessage);
}
return $dataSet;
}
/**
* Execute SQL statement, result set saved to attribute $dataset
* @param string $sql SQL statement
* @param int $rowFrom start line number, starting from 1
* @param int $rowTo end line number, and a value of 0 indicates
* @return Object
* @access Public
* @see Dbquery::execute
*/
Public Function open ($sql = ', $rowFrom = 0, $rowTo =
Self::max_row_num) {
$this->dataset = $this->execute ($sql, $rowFrom, $rowTo);
$this->sql = $sql;
return $this->dataset;
}
/**
* Split a row of field values into an array
* @param object $dataSet result set
* @param integer $resultType return type, OCI_ASSOC, Oci_num, or
Oci_both
* @return Array
*/
Public Function Fetchrecord ($dataSet =null, $resultType =oci_both) {
$result = @oci_fetch_array ($dataSet)? $dataSet: $this-
>dataset, $resultType);
if (Is_array ($result)) {
foreach ($result as $key => $value) {
if (!is_numeric ($key)) {
$result [Strtolower ($key)] = $value;
}
}
}
return $result;
}
Public Function Snext () {
$result = @oci_fetch_array ($this->dataset, Oci_both);
return $result;
}
/**
* Get the number of fields
* @param object $dataSet result set
* @return Integer
*/
Public Function GetFieldCount ($dataSet = NULL) {
Return Oci_num_fields ($dataSet)? $dataSet: $this-
>dataset);
}
/**
* Get the next record. Returns the record number, or False if the end of the record is returned
* @return Integer
* @access Public
* @see Getprior ()
*/
Public function next () {
return $this->fetchrecord ();
}
/**
* Get the current database time, the format is: Yyyy-mm-dd hh:mm:ss
* @return String
* @access Public
*/
Public Function Getnow () {
return $this->getvalue (' SELECT to_char (sysdate, ' yyyy-mm-dd
HH24:MI:SS ') Dateofnow from DUAL ');
}
/**
* to fetch data from a datasheet from a SQL statement, only the value of the first record,
* If there is only one field in the record, only the field value is returned.
* Not found return FALSE
*
* @param string $sql SQL statement
* @return Array
* @acce SS Public
*/
public function getValue ($sql = ', $hasClob = False) {
$dataSet = $thi S->execute ($sql, 1, 1);
if ($hasClob) {
$returnType = oci_return_alls;
} else {
$returnType = Oci_both;
}
if ($result = $this->fetchrecord ($dataSet, $returnType)) {
$fieldCount = $ This->getfieldcount ($dataSet);
$this->close ($dataSet);
return ($fieldCount <=2)? $result [0]: $result;
} else {
return false;
}
Public Function getseq ($seqName = ' ") {
$dataSet = $this->execute (' SELECT '. $seqName. ') Nextval from
Sys.dual ');
if ($result = $this->fetchrecord ($dataSet)) {
$fieldCount = $this->getfieldcount ($dataSet);
$this->close ($dataSet);
Return ($fieldCount <=2)? $result [0]: $result;
} else {
return false;
}
}
/**
* Whether the table exists, returns true
* @param string $tableName the name of the table to query
* @return BOOL
* @access Public
*/
Public Function tableisexists ($tableName) {
return false;
}
/**
* Start a transaction
* @access Public
*/
Public function begin () {
$this->executemode = Oci_default;
}
/**
* Submit a transaction
* @access Public
*/
Public Function commit () {
Oci_commit ($this->ds->connect);
$this->executemode = oci_commit_on_success;
}
/**
* Roll Back transaction
* @access Public
*/
Public Function rollback () {
Oci_rollback ($this->ds->connect);
$this->executemode = oci_commit_on_success;
}
/**
* Insert a record
* @param string $tableName table name
* @param array $fieldArray field
* @param string $whereForUnique uniqueness Condition
* @return int
* @access Public
*/
Public Function Insert ($tableName, $fieldArray, $whereForUnique =
NULL, $clobField = null) {
if (! $tableName | |! $fieldArray | |!is_array ($fieldArray)) {
throw new Exception (' parameter $tableName or $fieldArray
The value is not legal! ');
}
if ($whereForUnique) {
$where = ' where '. $whereForUnique;
$isExisted = $this->getvalue (' SELECT COUNT (*) from '
. $tableName. $where);
if ($isExisted) {
throw new Dbexception (' record already exists! ',
Dbexception::D b_record_is_existed);
}
}
$fieldNameList = Array ();
$fieldValueList = Array ();
foreach ($fieldArray as $fieldName => $fieldValue) {
if (!is_int ($fieldName)) {
$fieldNameList [] = $fieldName;
if ($clobField && $clobField = = $fieldName) {
$fieldValueList [] = ' empty_clob () ';
$hasClob = true;
$CLOBSTR = Str_replace
("'", "" ", $fieldValue);
} else {
$fieldValueList [] = '.
$fieldValue. ''';
}
}
}
$fieldName = Implode (', ', $fieldNameList);
$fieldValue = Implode (', ', $fieldValueList);
$sql = ' INSERT into '. $tableName. '('
. $fieldName. ') VALUES ('.
$fieldValue. ')';
if ($hasClob) {
$sql. = ' returning content into:clob_string ';
$dataSet = Oci_parse ($this->ds->connect, $sql);
$clob = Oci_new_descriptor ($this->ds->connect,
OCI_D_LOB);
Oci_bind_by_name ($dataSet, ': clob_string ', $clob,-
1, Oci_b_clob);
Oci_execute ($dataSet, Oci_default);
$clob->save ($CLOBSTR);
Oci_commit ($this->ds->connect);
return $dataSet;
} else {
return $this->execute ($sql);
}
}
/**
* Update a record
* @param string $tableName table name
* @param array $fieldArray field
* @param string $whereForUpdate query criteria
* @param string $whereForUnique uniqueness Condition
* @return int
* @access Public
*/
Public Function Update ($tableName, $fieldArray,
$whereForUpdate =null, $whereForUnique =null, $clobField = NULL) {
if (! $tableName | |! $fieldArray | |!is_array ($fieldArray)) {
throw new Exception (' parameter $tableName or $fieldArray
The value is not legal! ');
}
if ($whereForUnique) {
$where = ' where '. $whereForUnique;
$isExisted = $this->getvalue (' SELECT COUNT (*) from '
. $tableName. $where);
if ($isExisted) {
throw new Dbexception (' record already exists! ',
Dbexception::D b_record_is_existed);
}
}
$fieldNameValueList = Array ();
foreach ($fieldArray as $fieldName => $fieldValue) {
if (!is_int ($fieldName)) {
if ($clobField && $clobField = = $fieldName) {
$fieldNameValueList [] = $fieldName.
' =empty_clob () ';
$hasClob = true;
$CLOBSTR = Str_replace
("'", "" ", $fieldValue);
} else {
$fieldNameValueList [] = $fieldName.
'='' . $fieldValue. ''';
}
}
}
$fieldNameValue = Implode (', ', $fieldNameValueList);
if ($whereForUpdate) {
$whereForUpdate = ' WHERE '. $whereForUpdate;
}
$sql = ' UPDATE '. $tableName
. ' SET '. $fieldNameValue.
$whereForUpdate;
if ($hasClob) {
$sql. = ' returning content into:clob_string ';
$dataSet = Oci_parse ($this->ds->connect, $sql);
$clob = Oci_new_descriptor ($this->ds->connect,
OCI_D_LOB);
Oci_bind_by_name ($dataSet, ': clob_string ', $clob,-
1, Oci_b_clob);
Oci_execute ($dataSet, Oci_default);
$clob->save ($CLOBSTR);
Oci_commit ($this->ds->connect);
return $dataSet;
} else {
return $this->execute ($sql);
}
}
/**
* Select a record
* @param string $sql SQL statement
* @param string $dataFormat Returns the data format, the value
There are "array", "HashMap", "Hashmap_str", "DataSet"
* @param int $rowFrom start line number, starting from 1
* @param int $rowTo end line number, and a value of 0 indicates
* @result Array
* @access Public
*/
Public Function Select ($sql, $dataFormat = ' array ', $rowFrom = 0,
$rowTo = Self::max_row_num, $hasClob = False) {
$dataSet = $this->execute ($sql, $rowFrom, $rowTo);
Switch ($dataFormat) {
Case ' array '://array
$result = Array ();
$isMultiField = ($this->getfieldcount ($dataSet) >
1);
$i = 0;
if ($hasClob) {
$returnType = Oci_return_alls;
} else {
$returnType = Oci_both;
}
while ($data = $this->fetchrecord
($dataSet, $returnType)) {
$result [$i] = ($isMultiField)? $data:
$data [0];
$i + +;
}
$this->close ($dataSet);
Break
Case ' HashMap '://Hash list
$result = Array ();
while ($data = $this->fetchrecord ($dataSet)) {
$result [$data [0]] = $data [1];
}
$this->close ($dataSet);
Break
Case ' hashmap_str '://Hash List string
$result = Array ();
while ($data = $this->fetchrecord ($dataSet,
Oci_num)) {
$result [] = $data [0]. '=' . $data [1];
}
$result = implode (' | ', $result);
$this->close ($dataSet);
Break
Default://dataset DataSet, when the return data format is a dataset, select
Method has the same functionality as the Execute method
$result = $dataSet;
}
return $result;
}
/**
* Returns the maximum value
* @param string $tableName table name
* @param string $idField field name
* @param string $where query criteria
* @return int
* @access Public
*/
Public Function Getmax ($tableName, $idField, $where = NULL) {
$where = ($where)? (' WHERE '. $where): ';
return $this->getvalue (' SELECT MAX ('. $idField. ') from '
. $tableName. $where);
}
}