reasonableframework/system/database.php

167 lines
3.3 KiB
PHP
Raw Normal View History

2017-12-17 20:40:04 +00:00
<?php
2018-03-18 17:13:15 +00:00
/**
* @file database.php
* @date 2018-01-01
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Database module for ReasonableFramework
*/
2018-02-13 08:07:49 +00:00
if(!function_exists("get_db_connect")) {
function get_db_connect() {
2018-03-15 05:05:18 +00:00
global $config;
2018-02-13 08:07:49 +00:00
$conn = new PDO(
sprintf(
"mysql:host=%s;dbname=%s;charset=utf8",
$config['db_host'],
$config['db_name']
),
$config['db_username'],
$config['db_password'],
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
$conn->query("SET NAMES 'utf8'");
return $conn;
}
}
if(!function_exists("exec_stmt_query")) {
function exec_stmt_query($sql, $bind=array()) {
$stmt = get_db_stmt($sql, $bind);
$stmt->execute();
return $stmt;
2017-12-31 14:50:25 +00:00
}
}
2018-02-12 05:43:57 +00:00
2018-02-13 08:07:49 +00:00
if(!function_exists("get_dbc_object")) {
function get_dbc_object($renew=false) {
global $dbc;
2018-03-15 05:05:18 +00:00
if($renew) {
$dbc = get_db_connect();
}
2018-02-13 08:07:49 +00:00
return $dbc;
}
2018-02-12 05:58:54 +00:00
}
2018-02-12 05:43:57 +00:00
2018-02-13 08:07:49 +00:00
if(!function_exists("get_db_stmt")) {
2018-03-15 08:36:30 +00:00
function get_db_stmt($sql, $bind=array(), $bind_pdo=false) {
if(!$bind_pdo) {
if(count($bind) > 0) {
foreach($bind as $k=>$v) {
$sql = str_replace(":" . $k, "'" . addslashes($v) . "'", $sql);
}
}
}
2018-02-13 08:07:49 +00:00
$stmt = get_dbc_object()->prepare($sql);
2018-03-15 08:36:30 +00:00
// bind parameter by PDO statement
if($bind_pdo) {
if(count($bind) > 0) {
foreach($bind as $k=>$v) {
$stmt->bindParam(':' . $k, $v);
}
2018-02-13 08:07:49 +00:00
}
2018-02-12 05:43:57 +00:00
}
2018-03-15 08:36:30 +00:00
2018-02-13 08:07:49 +00:00
return $stmt;
2018-02-12 05:43:57 +00:00
}
}
2018-02-13 08:07:49 +00:00
if(!function_exists("get_db_last_id")) {
function get_db_last_id() {
return get_dbc_object()->lastInsertId();
}
2018-02-12 05:43:57 +00:00
}
2018-02-13 08:07:49 +00:00
if(!function_exists("exec_db_query")) {
function exec_db_query($sql, $bind=array(), $options=array()) {
$dbc = get_dbc_object();
2018-02-12 05:43:57 +00:00
2018-02-13 08:07:49 +00:00
$flag = false;
2018-03-15 05:05:18 +00:00
$is_insert_with_bind = false;
$sql_terms = explode(" ", $sql);
2018-03-15 06:05:23 +00:00
if($sql_terms[0] == "insert") {
2018-03-15 05:05:18 +00:00
$stmt = get_db_stmt($sql);
if(count($bind) > 0) {
$is_insert_with_bind = true;
}
2018-03-15 08:36:30 +00:00
} else {
2018-03-15 06:05:23 +00:00
$stmt = get_db_stmt($sql, $bind);
2018-03-15 05:05:18 +00:00
}
2018-02-12 05:58:54 +00:00
2018-02-13 08:07:49 +00:00
$validOptions = array();
$optionAvailables = array("is_check_count", "is_commit");
foreach($optionAvailables as $opt) {
if(!array_key_empty($opt, $options)) {
$validOptions[$opt] = $options[$opt];
} else {
$validOptions[$opt] = false;
}
}
extract($validOptions);
if($is_commit) {
$dbc->beginTransaction();
}
2018-03-15 05:09:05 +00:00
// execute statement (insert->execute(bind) or if not, sql->bind->execute)
2018-03-15 05:05:18 +00:00
$stmt_executed = $is_insert_with_bind ? @$stmt->execute($bind) : @$stmt->execute();
2018-02-13 08:07:49 +00:00
if($is_check_count == true) {
2018-03-15 05:05:18 +00:00
if($stmt_executed && $stmt->rowCount() > 0) {
2018-02-13 08:07:49 +00:00
$flag = true;
}
2018-02-12 05:43:57 +00:00
} else {
2018-03-15 05:05:18 +00:00
$flag = $stmt_executed;
2018-02-12 05:43:57 +00:00
}
2018-02-13 08:07:49 +00:00
if($is_commit) {
$dbc->commit();
2018-02-12 05:43:57 +00:00
}
2018-02-13 08:07:49 +00:00
return $flag;
2018-02-12 05:43:57 +00:00
}
}
2018-02-12 05:44:32 +00:00
2018-02-13 08:07:49 +00:00
if(!function_exists("exec_db_fetch_all")) {
function exec_db_fetch_all($sql, $bind=array()) {
$rows = array();
$stmt = get_db_stmt($sql, $bind);
if($stmt->execute() && $stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
return $rows;
2018-02-13 07:37:27 +00:00
}
}
2018-02-26 09:15:56 +00:00
if(!function_exists("get_page_range")) {
function get_page_range($page=1, $limit=0) {
$append_sql = "";
2018-03-15 06:05:23 +00:00
2018-02-26 09:15:56 +00:00
if($limit > 0) {
$record_start = ($page - 1) * $limit;
$record_end = $record_start + $limit - 1;
$append_sql .= " limit $record_start, $record_end";
}
return $append_sql;
}
}
2018-03-18 17:12:11 +00:00
// alias sql_query from exec_db_query
2018-03-18 17:11:27 +00:00
if(!function_exists("sql_query")) {
function sql_query($sql, $bind=array()) {
2018-03-18 17:12:11 +00:00
return exec_db_query($sql, $bind);
2018-03-18 17:11:27 +00:00
}
}
2018-02-12 05:44:32 +00:00
// set global db connection variable
$dbc = get_db_connect();