Update database.php

This commit is contained in:
Namhyeon Go 2020-01-09 20:14:16 +09:00 committed by GitHub
parent 61757a9ffe
commit f986054aee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,7 +28,7 @@ if(!check_function_exists("get_db_connect")) {
$db_driver = get_db_driver(); $db_driver = get_db_driver();
$dsn = "mysql:host=%s;dbname=%s;charset=utf8"; $dsn = "mysql:host=%s;dbname=%s;charset=utf8";
$_LIMIT_RETRY = get_value_in_array("db_retry_limit", $config, 3); $_RETRY_LIMIT = get_value_in_array("db_retry_limit", $config, 3);
if(in_array($db_driver, array("mysql", "mysql.pdo"))) { if(in_array($db_driver, array("mysql", "mysql.pdo"))) {
try { try {
@ -42,7 +42,7 @@ if(!check_function_exists("get_db_connect")) {
); );
//$conn->query("SET NAMES utf8"); //$conn->query("SET NAMES utf8");
} catch(Exception $e) { } catch(Exception $e) {
if($_RETRY > $_LIMIT_RETRY) { if($_RETRY > $_RETRY_LIMIT) {
set_error($e->getMessage()); set_error($e->getMessage());
show_errors(); show_errors();
} else { } else {
@ -69,12 +69,9 @@ if(!check_function_exists("exec_stmt_query")) {
if(!check_function_exists("get_dbc_object")) { if(!check_function_exists("get_dbc_object")) {
function get_dbc_object($renew=false) { function get_dbc_object($renew=false) {
$dbc = get_scope("dbc");
if($renew) { if($renew) {
set_scope("dbc", get_db_connect()); set_scope("dbc", get_db_connect());
} }
return get_scope("dbc"); return get_scope("dbc");
} }
} }
@ -218,19 +215,20 @@ if(!check_function_exists("exec_db_query")) {
if(!check_function_exists("exec_db_fetch_all")) { if(!check_function_exists("exec_db_fetch_all")) {
function exec_db_fetch_all($sql, $bind=array(), $options=array()) { function exec_db_fetch_all($sql, $bind=array(), $options=array()) {
$response = array(); $result = array();
$is_not_countable = false; // set is counted
$_cnt = 0; $is_counted = false;
$rows = array(); $rows = array();
$stmt = get_db_stmt($sql, $bind); $stmt = get_db_stmt($sql, $bind);
// get rows
if($stmt->execute() && $stmt->rowCount() > 0) { if($stmt->execute() && $stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} }
// 1.6 or above // get rows with removed keys (1.6 or above)
$_rows = array(); $_rows = array();
if(array_key_equals("getvalues", $options, true)) { if(array_key_equals("getvalues", $options, true)) {
foreach($rows as $row) { foreach($rows as $row) {
@ -239,54 +237,58 @@ if(!check_function_exists("exec_db_fetch_all")) {
$rows = $_rows; $rows = $_rows;
} }
if(array_key_equals("do_count", $options, true)) { // get number of rows
$_sql = sprintf("select count(*) as cnt from (%s) a", get_db_binded_sql($sql, $bind)); $num_rows = 0;
$_data = exec_db_fetch($_sql); if(array_key_equals("getcount", $options, true)) {
$_cnt = get_value_in_array("cnt", $_data, $_cnt); $sql = sprintf("select count(*) as value from (%s) a", get_db_binded_sql($sql, $bind));
} elseif(array_key_equals("do_count", $options, "count")) { $rows = exec_db_fetch_all($sql, $bind);
$_cnt = count($rows); foreach($rows as $row) {
} elseif(array_key_equals("do_count", $options, "PDOStatement::rowCount")) { $num_rows += intval($row['value']);
$_cnt = $stmt->rowCount(); }
} else { $is_counted = true;
$response = $rows; } elseif(array_key_equals("getcount", $options, "php")) {
$is_not_countable = true; $num_rows = count($rows);
$is_counted = true;
} elseif(array_key_equals("getcount", $options, "pdo")) {
$num_rows = $stmt->rowCount();
$is_counted = true;
} }
if(!$is_not_countable) { // make a result
$response = array(); if($is_counted) {
if(get_old_version() == "1.4") { // compatible 1.4 or below $result = array(
$response['length'] = $_cnt; "count" => $num_rows,
} "data" => $rows
$response['cnt'] = $_cnt; );
$response['data'] = $rows; } else {
$result = $rows;
} }
return $response; return $result;
} }
} }
if(!check_function_exists("exec_db_fetch")) { if(!check_function_exists("exec_db_fetch")) {
function exec_db_fetch($sql, $bind=array(), $start=0, $bind_limit=false) { function exec_db_fetch($sql, $bind=array(), $start=0) {
$fetched = NULL; $row = NULL;
$rows = array();
if($bind_limit == true) { $config = get_config();
$sql = $sql . " limit 1"; $fetch_mode = get_value_in_array("db_fetch_mode", $config, "sql");
}
$rows = exec_db_fetch_all($sql, $bind); if($fetch_mode == "sql") {
$_sql = sprintf("select * from (%s) a limit %s, 1", $sql, ($start > 0 ? $start : "0"));
if(check_array_length($rows, $start) > 0) { $_rows = exec_db_fetch_all($sql, $bind);
$idx = 0; } elseif($fetch_mode == "php") {
foreach($rows as $row) { $_sql = $sql;
if($idx >= $start) { $_rows = exec_db_fetch_all($sql, $bind);
$fetched = $row; $_rows = array_slice($_rows, $start, 1);
break;
}
$idx++;
}
} }
return $fetched; // get first of rows
$row = current($rows);
// return row
return $row;
} }
} }