首页 > 数据库 > Oracle > 正文

封装了ORACLE函数的数据库操作类

2024-08-29 13:32:40
字体:
来源:转载
供稿:网友

 

<?php // -*- c++ -*-
/*
* $id: db-oci8.phl,v 1.2 1998/10/01
19:16:06 ssb exp $
*/

$db_error_code = 0;
$db_error_msg = false;
$db_error_source = false;

/*
* database specific notes:
*
* - you must configure oracle listener to use this abstraction layer.
*
*/


/**
* @function db_connect
* @purpose connect to a database
* @desc
* connects to a database and returns and identifier for the connection.
* @arg database
* data source name or database host to connect to.

* @arg user
* name of user to connect as.

* @arg password
* the user's password.
*/
function db_connect($database, $user, $password)
{
$ret = @ocilogon($user, $password, $database);
db_check_errors($php_errormsg);
return $ret;
}

/*
* function: db_query
* arguments: $conn (int) - connection identifier
* $query (string) - sql statement to execute
* description: executes an sql statement
* returns: false - query failed
* integer - query succeeded, value is result handle
*/
function db_query($conn, $query)
{
$stmt = @ociparse($conn, $query);
db_check_errors($php_errormsg);
if (!$stmt) {
return false;
}
if (@ociexecute($stmt)) {
return $stmt;
}
db_check_errors($php_errormsg);
@ocifreestatement($stmt);
return false;
}

/*
* function: db_fetch_row
* arguments: $stmt (int) - result identifier
* description: returns an array containing data from a fetched row.
* returns: false - error
* (array) - returned row, first column at index 0
*/
function db_fetch_row($stmt)
{
$cols = @ocifetchinto($stmt, &$row);
if (!$cols) {
db_check_errors($php_errormsg);
return false;
}
return $row;
}

/*
* function: db_free_result
* arguments: $stmt (int) - result identifier
* description: frees all memory associated with a result identifier.
* returns: false - failure
* true - success
*/
function db_free_result($stmt)
{
global $db_oci8_pieces;

if (isset($db_oci8_pieces[$stmt])) {
unset($db_oci8_pieces[$stmt]);
}
$ret = @ocifreestatement($stmt);
db_check_errors($php_errormsg);
return $ret;
}

/*
* function: db_disconnect
* arguments: $connection (int) - connection identifier
* description: closes a database connection
* returns: false - failure
* true - success
*/
function db_disconnect($connection)
{
$ret = @ocilogoff($connection);
db_check_errors($php_errormsg);
return $ret;
}

/*
* function: db_autocommit
* arguments: $connection (int) - connection identifier
* description: turn autocommit on or off
* returns: false - failure
* true - success
*/
function db_autocommit($connection, $enabled)
{
if (!$enabled) {
db_post_error(0, "transactions not yet implemented");
return false;
}
return true;
}


function db_commit($connection)
{
return true;
}


function db_rollback($connection)
{
db_post_error(0, "transactions not yet implemented");
return false;
}


function db_quote_string($string)
{
$ret = ereg_replace( "'", "''", $string);
return $ret;
}


function db_prepare($connection, $query)
{
global $db_oci8_pieces;

$pieces = explode( "?", $query);
$new_query = "";
$last_piece = sizeof($pieces) - 1;
print "<br>last_piece=$last_piece/n";
while (list($i, $piece) = each($pieces)) {
$new_query .= $piece;
if ($i < $last_piece) {
$new_query .= ":var$i";
}
}
print "<br>new_query=$new_query/n";
$stmt = @ociparse($connection, $new_query);
if (!$stmt) {
db_check_errors($php_errormsg);
return false;
}
for ($i = 0; $i < $last_piece; $i++) {
$bindvar = ":var$i";
print "<br>trying to bind $bindvar/n";
if ([email protected]ocibindbyname($stmt, $bindvar, &$db_oci8_pieces[$stmt][$i])) {
db_check_errors($php_errormsg);
@ocifreestatement($stmt);
return false;
}
}
return $stmt;
}


function db_execute($stmt, $data)
{
global $db_oci8_pieces;

while (list($i, $value) = each($data)) {
$db_oci8_pieces[$stmt][$i] = $data[$i];
}
$ret = @ociexecute($stmt);
if (!$ret) {
db_check_errors($php_errormsg);
return false;
}
return true;
}


function db_error_code()
{
global $db_error_code;
return $db_error_code;
}


function db_error_msg()
{
global $db_error_msg;
return $db_error_msg;
}


function db_error_source()
{
global $db_error_source;
return $db_error_source;
}


function db_check_errors($errormsg)
{
global $db_error_code, $db_error_msg, $db_error_source;
if (ereg( '^([^:]*): (...-.....): (.*)', $errormsg, &$data)) {
list($foo, $function, $db_error_code, $db_error_msg) = $data;
$db_error_msg = "$function: $db_error_msg";
$db_error_source = "[oracle][php][oci8]";
} elseif (ereg( '^([^:]*): (.*)', $errormsg, &$data)) {
list($foo, $function, $db_error_msg) = $data;
$db_error_msg = "$function: $db_error_msg";
$db_error_code = 0;
$db_error_source = "[php][oci8][db-oci8]";
} else {
$db_error_msg = $errormsg;
$db_error_code = 0;
$db_error_source = "[php][oci8][db-oci8]";
}
}


function db_post_error($code, $message)
{
global $db_error_code, $db_error_msg, $db_error_source;
$db_error_code = $code;
$db_error_msg = $message;
$db_error_source = "[php][oci8][db-oci8]";
}


function db_api_version()
{
return 10; // 1.0
}

?>

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表