Oricle operation class define (oci_return_balls, OCI_BOTH + records); classOracle {& nbsp; ** & nbsp; * Maximum number of records returned by the select Method & nbsp; * & nbsp; constMAX_ROW_NUM1000; ** & nbsp; * data query result set object & nb
Oricle operation class
Define ('oci _ return_all', 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 object
* @ Var object $ ds
*/
Public $ ds = NULL;
/**
* The queried SQL statement
* @ Var string $ SQL
*/
Public $ SQL = '';
/**
* Query mode. 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 be initialized and queried
*/
Function _ construct ($ ds = NULL, $ SQL = NULL ){
If (! $ Ds ){
$ This-> error (DbException: DB_UNCONNECTED, 'database also
Not connected. ');
} Else {
$ This-> ds = $ ds;
If ($ SQL ){
$ This-> open ($ SQL );
}
}
}
/**
* Release the memory occupied
* @ Param object $ dataSet: result set of the resource to be released
* @ 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;
}
}
/**
* Encrypt the $ pass database and return the encrypted value.
* @ Param string $ pass the string to be encrypted
* @ Return string
* @ Access public
*/
Public function encodePassword ($ pass ){
Return md5 ($ pass );
}
/**
* Get the error message and error code
* @ Param integer $ queryResult Query Result
* @ Return array
* @ Access protected
*/
Protected function errorInfo ($ queryResult = NULL ){
$ Result = oci_error ($ this-> ds-> connect );
Return $ result;
}
/**
* Handle error messages
* @ Param string $ errorId error ID
* @ Param string $ error message
* @ Access protected
*/
Protected function error ($ errorId, $ errorMessage ){
Throw new DbException ($ errorMessage, $ errorId );
}
/**
* Execute SQL statements
* @ Param string $ SQL SQL statement
* @ Return object
* @ Param int $ rowFrom start row number, which starts from 1
* @ Param int $ rowTo end row number. The value 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 _
From ('
. $ SQL. ') row _ where rownum <='
. $ RowTo. ') where rownum _> = '.
$ RowFrom;
}
// Echo $ SQL .'
';
// $ Start = microtime (true );
$ DataSet = @ oci_parse ($ this-> ds-> connect, $ SQL );
$ ExecuteSucceed = @ oci_execute ($ dataSet, $ this-
> ExecuteMode );
// Echo 'SQL:'. (string) (microtime (true)-$ start )).'
';
If (! $ DataSet |! $ ExecuteSucceed ){
$ SqlError = $ this-> errorInfo ();
$ ErrorMessage = 'Run ['.
$ SQL
.'] Error!
Color = # FF0000> ['
. $ SqlError ['code']. ']:'
. $ SqlError ['message']. '';
$ This-> error (DbException: DB_QUERY_ERROR,
$ ErrorMessage );
}
Return $ dataSet;
}
/**
* Execute the SQL statement and save the result set to the attribute $ dataSet.
* @ Param string $ SQL SQL statement
* @ Param int $ rowFrom start row number, which starts from 1
* @ Param int $ rowTo end row number. The value 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 the field values of a row 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;
}
/**
* Obtain the number of fields
* @ Param object $ dataSet result set
* @ Return integer
*/
Public function getFieldCount ($ dataSet = NULL ){
Return oci_num_fields ($ dataSet )? $ DataSet: $ this-
> DataSet );
}
/**
* Obtain the next record. Returns the record number. If it reaches the end of the record, FALSE is returned.
* @ Return integer
* @ Access public
* @ See getPrior ()
*/
Public function next (){
Return $ this-> fetchRecord ();
}
/**
* Obtain the current database time in the format of 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 ');
}
/**
* Retrieve data from the data table according to the SQL statement. Only the value of the first record is obtained,
* If the record contains only one field, only the field value is returned.
* If not found, FALSE is returned.
*
* @ Param string $ SQL SQL statement
* @ Return array
* @ Access public
*/
Public function getValue ($ SQL = '', $ hasClob = false ){
$ DataSet = $ this-> execute ($ SQL, 1, 1 );
If ($ hasClob ){
$ ReturnType = oci_return_balls;
} 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;
}
}
/**
* Returns true if the table exists.
* @ Param string $ tableName name of the table to be queried
* @ Return bool
* @ Access public
*/
Public function tableIsExists ($ tableName ){
Return false;
}
/**
* Start 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 a 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 array
* @ 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 of is invalid! ');
}
If ($ whereForUnique ){
$ Where = 'where'. $ whereForUnique;
$ IsExisted = $ this-> getValue ('select COUNT (*) from'
. $ TableName. $ where );
If ($ isExisted ){
Throw new DbException ('record already exists! ',
DbException: DB_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'. $ 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 array
* @ Param string $ whereForUpdate query Condition
* @ 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 of is invalid! ');
}
If ($ whereForUnique ){
$ Where = 'where'. $ whereForUnique;
$ IsExisted = $ this-> getValue ('select COUNT (*) from'
. $ TableName. $ where );
If ($ isExisted ){
Throw new DbException ('record already exists! ',
DbException: DB_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 statement
* @ Param string $ dataFormat returns the data format, Value
"Array", "hashmap", "hashmap_str", "dataset"
* @ Param int $ rowFrom start row number, which starts from 1
* @ Param int $ rowTo end row number. The value 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_balls;
} Else {
$ ReturnType = OCI_BOTH;
}
While ($ data = $ this-> fetchRecord
($ DataSet, $ returnType )){
$ Result [$ I] = ($ isMultiField )? $ Data:
$ Data [0];
$ I ++;
}
$ This-> close ($ dataSet );
Break;
Case 'hashmap': // hash
$ Result = array ();
While ($ data = $ this-> fetchRecord ($ dataSet )){
$ Result [$ data [0] = $ data [1];
}
$ This-> close ($ dataSet );
Break;
Case 'hashmap _ str': // hash string
$ Result = array ();
While ($ data = $ this-> fetchRecord ($ dataSet,
OCI_NUM )){
$ Result [] = $ data [0]. '='. $ data [1];
}
$ Result = implode ('|', $ result );
$ This-> close ($ dataSet );
Break;
Default: // dataset. When the returned data format is dataset, select
The function of the method is the same as that of 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 Condition
* @ Return int
* @ Access public
*/
Public function getMax ($ tableName, $ idField, $ where = NULL ){
$ Where = ($ where )? ('Where'. $ WHERE ):'';
Return $ this-> getValue ('select MAX ('. $ idField.') from'
. $ TableName. $ where );
}
}