reasonableframework/helper/database.oracle.php

113 lines
2.5 KiB
PHP
Raw Normal View History

2018-03-27 02:02:20 +00:00
<?php
/**
* @file oracle.php
* @date 2018-03-27
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Oracle database helper for ReasonableFramework
*/
2018-09-13 16:09:20 +00:00
if(!function_exists("get_db_orable_binded_sql")) {
function get_db_orable_binded_sql($sql, $bind) {
return get_db_binded_sql($sql, $bind);
}
2018-03-27 02:02:20 +00:00
}
2018-09-13 16:09:20 +00:00
if(!function_exists("get_db_oracle_stmt")) {
function get_db_oracle_stmt($sql, $bind) {
$stmt = NULL;
$sql = get_db_orable_binded_sql($sql, $bind);
$stmt = oci_parse($conn, $sql);
2018-03-27 02:15:28 +00:00
2018-09-13 16:09:20 +00:00
return $stmt;
}
2018-03-27 02:15:28 +00:00
}
2018-09-13 16:09:20 +00:00
if(!function_exists("exec_db_oracle_connect")) {
function exec_db_oracle_connect($host, $port, $user, $password, $options=array()) {
$conn = NULL;
2018-09-13 16:24:14 +00:00
$envs = get_value_in_array("envs", $options, array());
2018-03-27 02:02:20 +00:00
2018-09-13 16:09:20 +00:00
if(!function_exists("oci_connect")) {
exit("OCI (Oracle Extension for PHP) not installed!");
}
2018-03-27 02:02:20 +00:00
2018-09-13 16:24:14 +00:00
if(array_key_empty("NLS_LANG", $envs)) {
$envs["NLS_LANG"] = "KOREAN_KOREA.AL32UTF8";
2018-03-27 02:02:20 +00:00
}
2018-09-13 16:24:14 +00:00
// set environment variables
2018-09-13 16:09:20 +00:00
foreach($envs as $env) {
putenv($env);
}
2018-09-13 16:24:14 +00:00
// get oracle db connection info
$dbs_id = read_storage_file("tnsname.orax", array(
"storage_type" => "config"
));
// set replace rules
$dbs_rules = array(
"protocol" => get_value_in_array("service_name", $options, "TCP"),
"service_name" => get_value_in_array("service_name", $options, "ORCL"),
"host" => $host,
"port" => $port
"server_type" => "DEDICATED"
);
// parse db connection info
foreach($dbs_rules as $k=>$v) {
$dbs_id = str_replace("%" . $k . "%", $v, $dbs_id);
}
2018-03-27 02:02:20 +00:00
2018-09-13 16:24:14 +00:00
// set db connection
$conn = @oci_connect($user, $password, $dbs_id);
2018-09-13 16:09:20 +00:00
return $conn;
}
2018-03-27 02:02:20 +00:00
}
2018-09-13 16:09:20 +00:00
if(!function_exists("exec_db_oracle_fetch_all")) {
function exec_db_oracle_fetch_all($sql, $bind, $conn) {
$rows = array();
2018-03-27 02:02:20 +00:00
2018-09-13 16:09:20 +00:00
$required_functions = array("oci_parse", "oci_execute", "oci_fetch_assoc", "oci_free_statement");
foreach($required_functions as $func_name) {
if(!function_exists($func_name)) {
exit("OCI (Oracle Extension for PHP) not installed!");
}
2018-03-27 02:02:20 +00:00
}
2018-09-13 16:09:20 +00:00
$stmt = get_db_oracle_stmt($sql, $bind);
oci_execute($stmt);
2018-03-27 02:02:20 +00:00
2018-09-13 16:09:20 +00:00
while($row = oci_fetch_assoc($stmt)) {
$rows[] = $row;
}
2018-03-27 02:02:20 +00:00
2018-09-13 16:09:20 +00:00
oci_free_statement($stmt);
2018-03-27 02:02:20 +00:00
2018-09-13 16:09:20 +00:00
return $rows;
}
2018-03-27 02:02:20 +00:00
}
2018-03-27 02:15:28 +00:00
2018-09-13 16:09:20 +00:00
if(!function_exists("exec_db_oracle_query")) {
function exec_db_oracle_query($sql, $bind, $conn) {
$flag = false;
2018-03-27 02:15:28 +00:00
2018-09-13 16:09:20 +00:00
$stmt = get_db_oracle_stmt($sql, $bind);
$flag = oci_execute($stmt);
2018-03-27 02:15:28 +00:00
2018-09-13 16:09:20 +00:00
oci_free_statement($stmt);
2018-03-27 02:15:28 +00:00
2018-09-13 16:09:20 +00:00
return $flag;
}
}
if(!function_exists("close_db_oracle_connect")) {
function close_db_oracle_connect() {
$dbc = get_scope("dbc");
return oci_close($dbc);
}
2018-03-27 02:15:28 +00:00
}