Update database.php

This commit is contained in:
Namhyeon Go 2018-04-13 14:08:11 +09:00 committed by GitHub
parent e4c937496f
commit ecda684150
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,7 @@
if(!function_exists("get_db_connect")) { if(!function_exists("get_db_connect")) {
function get_db_connect() { function get_db_connect() {
global $config; $config = get_scope("config");
$conn = new PDO( $conn = new PDO(
sprintf( sprintf(
@ -37,18 +37,19 @@ if(!function_exists("exec_stmt_query")) {
if(!function_exists("get_dbc_object")) { if(!function_exists("get_dbc_object")) {
function get_dbc_object($renew=false) { function get_dbc_object($renew=false) {
global $dbc; $dbc = get_scope("dbc");
if($renew) { if($renew) {
$dbc = get_db_connect(); set_scope("dbc", get_db_connect());
} }
return $dbc; return get_scope("dbc");
} }
} }
if(!function_exists("get_db_binded_sql")) { if(!function_exists("get_db_stmt")) {
function get_db_binded_sql($sql, $bind) { function get_db_stmt($sql, $bind=array(), $bind_pdo=false, $show_sql=false) {
if(!$bind_pdo) {
if(count($bind) > 0) { if(count($bind) > 0) {
$bind_keys = array_keys($bind); $bind_keys = array_keys($bind);
@ -60,14 +61,7 @@ if(!function_exists("get_db_binded_sql")) {
$sql = str_replace(":" . $k, "'" . addslashes($bind[$k]) . "'", $sql); $sql = str_replace(":" . $k, "'" . addslashes($bind[$k]) . "'", $sql);
} }
} }
return $sql;
} }
}
if(!function_exists("get_db_stmt")) {
function get_db_stmt($sql, $bind=array(), $bind_pdo=false, $show_sql=false) {
$sql = !$bind_pdo ? get_db_binded_sql($sql, $bind) : $sql;
$stmt = get_dbc_object()->prepare($sql); $stmt = get_dbc_object()->prepare($sql);
if($show_sql) { if($show_sql) {
@ -177,8 +171,8 @@ if(!function_exists("exec_db_fetch")) {
if($bind_limit == true) { if($bind_limit == true) {
$sql = $sql . " limit 1"; $sql = $sql . " limit 1";
} }
$rows = exec_db_fetch_all($sql, $bind); $rows = exec_db_fetch_all($sql, $bind);
if(count($rows) > $start) { if(count($rows) > $start) {
$idx = 0; $idx = 0;
foreach($rows as $row) { foreach($rows as $row) {
@ -186,7 +180,6 @@ if(!function_exists("exec_db_fetch")) {
$fetched = $row; $fetched = $row;
break; break;
} }
$idx++; $idx++;
} }
} }
@ -209,6 +202,21 @@ if(!function_exists("get_page_range")) {
} }
} }
if(!function_exists("get_bind_to_sql_insert")) {
function get_bind_to_sql_insert($tablename, $bind) {
$bind_keys = array_keys($bind);
$sql = "insert into %s (%s) values (:%s)";
$sql_p1 = $tablename;
$sql_p2 = implode(", ", $bind_keys);
$sql_p3 = implode(", :", $bind_keys);
$sql = sprintf($sql, $sql_p1, $sql_p2, $sql_p3);
return $sql;
}
}
if(!function_exists("get_bind_to_sql_where")) { if(!function_exists("get_bind_to_sql_where")) {
// warning: variable k is not protected. do not use variable k and external variable without filter // warning: variable k is not protected. do not use variable k and external variable without filter
function get_bind_to_sql_where($bind, $excludes=array()) { function get_bind_to_sql_where($bind, $excludes=array()) {
@ -246,5 +254,34 @@ if(!function_exists("sql_query")) {
} }
} }
// set global db connection variable // get timediff
$dbc = get_db_connect(); if(!function_exists("get_timediff_on_query")) {
function get_timediff_on_query($a, $b) {
$dt = 0;
$sql = "select timediff(:a, :b) as dt";
$bind = array(
"a" => $a,
"b" => $b
);
$row = exec_db_fetch($sql, $bind);
$dt = get_value_in_array("dt", $row, $dt);
return $dt;
}
}
// get assoc from json raw data
if(!function_exists("json_decode_to_assoc")) {
function json_decode_to_assoc($data) {
$result = array();
$obj = @json_decode($data, true);
$result = (@json_last_error() === 0) ? $obj : $result;
return $result;
}
}
// set scope dbc
set_scope("dbc", get_db_connect());