Update database.php

This commit is contained in:
Namhyeon Go 2018-03-26 11:49:01 +09:00 committed by GitHub
parent e1bae93666
commit e74ea366e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,11 +1,4 @@
<?php <?php
/**
* @file database.php
* @date 2018-01-01
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Database module for ReasonableFramework
*/
if(!function_exists("get_db_connect")) { if(!function_exists("get_db_connect")) {
function get_db_connect() { function get_db_connect() {
global $config; global $config;
@ -164,8 +157,8 @@ if(!function_exists("exec_db_fetch_all")) {
} }
if(!function_exists("exec_db_fetch")) { if(!function_exists("exec_db_fetch")) {
function exec_db_fetch($sql, $bind=array(), $bind_limit=true) { function exec_db_fetch($sql, $bind=array(), $start=0, $bind_limit=false) {
$row = NULL; $fetched = NULL;
$rows = array(); $rows = array();
if($bind_limit == true) { if($bind_limit == true) {
@ -173,62 +166,19 @@ if(!function_exists("exec_db_fetch")) {
} }
$rows = exec_db_fetch_all($sql, $bind); $rows = exec_db_fetch_all($sql, $bind);
if(count($rows) > 0) { if(count($rows) > $start) {
$row = $rows[0]; $idx = 0;
foreach($rows as $row) {
if($idx >= $start) {
$fetched = $row;
break;
} }
return $row; $idx++;
} }
} }
if(!function_exists("get_page_range")) { return $fetched;
function get_page_range($page=1, $limit=0) {
$append_sql = "";
if($limit > 0) {
$record_start = ($page - 1) * $limit;
$record_end = $record_start + $limit - 1;
$append_sql .= " limit $record_start, $record_end";
}
return $append_sql;
}
}
if(!function_exists("get_bind_to_sql_where")) {
// 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()) {
$sql_where = "";
foreach($bind as $k=>$v) {
if(!in_array($k, $excludes)) {
$sql_where .= " and {$k} = :{$k}";
}
}
return $sql_where;
}
}
if(!function_exists("get_bind_to_sql_update_set")) {
// warning: variable k is not protected. do not use variable k and external variable without filter
function get_bind_to_sql_update_set($bind, $excludes=array()) {
$sql_update_set = "";
$set_items = "";
foreach($bind as $k=>$v) {
if(!in_array($k, $excludes)) {
$set_items[] = "{$k} = :{$k}";
}
}
$sql_update_set = implode(", ", $set_items);
return $sql_update_set;
}
}
// alias sql_query from exec_db_query
if(!function_exists("sql_query")) {
function sql_query($sql, $bind=array()) {
return exec_db_query($sql, $bind);
} }
} }