reasonableframework/system/database.php

872 lines
30 KiB
PHP
Raw Normal View History

2017-12-17 20:40:04 +00:00
<?php
2018-03-26 02:52:49 +00:00
/**
* @file database.php
2018-04-13 05:12:36 +00:00
* @date 2018-04-13
2018-03-26 02:52:49 +00:00
* @author Go Namhyeon <gnh1201@gmail.com>
2018-04-13 05:12:36 +00:00
* @brief Database module
2018-03-26 02:52:49 +00:00
*/
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_db_driver")) {
2019-05-20 08:19:05 +00:00
function get_db_driver() {
$config = get_config();
return get_value_in_array("db_driver", $config, false);
}
2019-02-22 15:43:31 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("check_db_driver")) {
2019-05-20 08:19:05 +00:00
function check_db_driver($db_driver) {
return (get_db_driver() == $db_driver);
}
2019-02-22 15:51:27 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_db_connect")) {
2019-05-20 08:19:05 +00:00
function get_db_connect($a=3, $b=0) {
$conn = false;
$config = get_config();
$db_driver = get_db_driver();
if(in_array($db_driver, array("mysql", "mysql.pdo"))) {
try {
$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'");
} catch(Exception $e) {
if($b > $a) {
set_error($e->getMessage());
show_errors();
} else {
$b++;
sleep(0.03);
$conn = get_db_connect($a, $b);
}
}
} elseif(loadHelper("database.alt")) {
$conn = call_user_func("get_db_alt_connect", $db_driver);
}
return $conn;
}
2018-02-13 08:07:49 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("exec_stmt_query")) {
2019-05-20 08:19:05 +00:00
function exec_stmt_query($sql, $bind=array()) {
$stmt = get_db_stmt($sql, $bind);
$stmt->execute();
2018-02-13 08:07:49 +00:00
2019-05-20 08:19:05 +00:00
return $stmt;
}
2017-12-31 14:50:25 +00:00
}
2018-02-12 05:43:57 +00:00
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_dbc_object")) {
2019-05-20 08:19:05 +00:00
function get_dbc_object($renew=false) {
$dbc = get_scope("dbc");
2018-03-15 05:05:18 +00:00
2019-05-20 08:19:05 +00:00
if($renew) {
set_scope("dbc", get_db_connect());
}
2018-03-15 05:05:18 +00:00
2019-05-20 08:19:05 +00:00
return get_scope("dbc");
}
2018-02-12 05:58:54 +00:00
}
2018-02-12 05:43:57 +00:00
2019-04-07 15:06:43 +00:00
// 2018-08-19: support lower php version (not supported anonymous function)
if(!function_exists("compare_db_key_length")) {
2019-05-20 08:19:05 +00:00
function compare_db_key_length($a, $b) {
return strlen($b) - strlen($a);
}
2019-04-07 15:06:43 +00:00
}
if(!function_exists("get_db_binded_sql")) {
2019-05-24 07:28:09 +00:00
function get_db_binded_sql($sql, $bind=array()) {
2019-07-14 05:54:41 +00:00
if(is_array($bind) && check_array_length($bind, 0) > 0) {
$bind_keys = array_keys($bind);
2019-07-14 05:12:32 +00:00
// 2018-08-19: support lower php version (not supported anonymous function)
usort($bind_keys, "compare_db_key_length");
// bind values
foreach($bind_keys as $k) {
$sql = str_replace(":" . $k, "'" . addslashes($bind[$k]) . "'", $sql);
2019-05-20 08:19:05 +00:00
}
2019-07-14 05:12:32 +00:00
}
2019-05-20 08:19:05 +00:00
return $sql;
}
2019-04-07 15:06:43 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_db_stmt")) {
2019-05-20 08:19:05 +00:00
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);
if($show_sql) {
set_error($sql, "DATABASE-SQL");
show_errors(false);
}
// bind parameter by PDO statement
if($bind_pdo) {
if(check_array_length($bind, 0) > 0) {
foreach($bind as $k=>$v) {
$stmt->bindParam(':' . $k, $v);
}
}
}
return $stmt;
}
2018-02-12 05:43:57 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_db_last_id")) {
2019-05-20 08:19:05 +00:00
function get_db_last_id() {
$last_id = false;
2018-12-29 02:48:42 +00:00
2019-05-20 08:19:05 +00:00
$dbc = get_dbc_object();
2019-10-02 11:47:53 +00:00
$config = get_config();
$db_driver = get_db_driver();
2018-12-29 02:48:42 +00:00
2019-05-20 08:19:05 +00:00
if(in_array($db_driver, array("mysql", "mysql.pdo"))) {
$last_id = $dbc->lastInsertId();
} elseif(loadHelper("database.dbt")) {
$last_id = call_user_func("get_db_alt_last_id", $db_driver);
}
2018-12-29 02:48:42 +00:00
2019-05-20 08:19:05 +00:00
return $last_id;
}
2018-02-12 05:43:57 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("exec_db_query")) {
2019-05-20 08:19:05 +00:00
function exec_db_query($sql, $bind=array(), $options=array()) {
$dbc = get_dbc_object();
$validOptions = array();
$optionAvailables = array("is_check_count", "is_commit", "display_error", "show_debug", "show_sql");
foreach($optionAvailables as $opt) {
if(!array_key_empty($opt, $options)) {
$validOptions[$opt] = $options[$opt];
} else {
$validOptions[$opt] = false;
}
}
extract($validOptions);
$flag = false;
$is_insert_with_bind = false;
$sql_terms = explode(" ", $sql);
if($sql_terms[0] == "insert") {
$stmt = get_db_stmt($sql);
if(check_array_length($bind, 0) > 0) {
$is_insert_with_bind = true;
}
} else {
if($show_sql) {
$stmt = get_db_stmt($sql, $bind, false, true);
} else {
$stmt = get_db_stmt($sql, $bind);
}
}
if($is_commit) {
$dbc->beginTransaction();
}
// execute statement (insert->execute(bind) or if not, sql->bind->execute)
$stmt_executed = $is_insert_with_bind ? $stmt->execute($bind) : $stmt->execute();
if($show_debug) {
$stmt->debugDumpParams();
}
if($display_error) {
$error_info = $stmt->errorInfo();
if(check_array_length($error_info, 0) > 0) {
set_error(implode(" ", $error_info), "DATABASE-ERROR");
}
show_errors(false);
}
if($is_check_count == true) {
if($stmt_executed && $stmt->rowCount() > 0) {
$flag = true;
}
} else {
$flag = $stmt_executed;
}
if($is_commit) {
$dbc->commit();
}
if($flag === false) {
set_error(get_hashed_text($sql), "DATABASE-QUERY-FAILURE");
}
return $flag;
}
2018-02-12 05:43:57 +00:00
}
2019-07-14 05:00:53 +00:00
2019-02-26 05:40:03 +00:00
if(!check_function_exists("exec_db_fetch_all")) {
2019-05-20 08:19:05 +00:00
function exec_db_fetch_all($sql, $bind=array(), $options=array()) {
$response = array();
2019-07-14 04:34:58 +00:00
2019-05-20 08:19:05 +00:00
$is_not_countable = false;
2019-07-14 05:00:53 +00:00
$_cnt = 0;
2019-07-14 04:34:58 +00:00
2019-05-20 08:19:05 +00:00
$rows = array();
$stmt = get_db_stmt($sql, $bind);
if($stmt->execute() && $stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
2019-10-21 02:05:30 +00:00
// 1.6 or above
$_rows = array();
if(array_key_equals("getvalues", $options, true)) {
foreach($rows as $row) {
$_rows[] = array_values($row);
}
$rows = $_rows;
}
2019-05-20 08:19:05 +00:00
if(array_key_equals("do_count", $options, true)) {
2019-07-14 04:34:58 +00:00
$_sql = sprintf("select count(*) as cnt from (%s) a", get_db_binded_sql($sql, $bind));
$_data = exec_db_fetch($_sql);
2019-07-14 05:11:19 +00:00
$_cnt = get_value_in_array("cnt", $_data, $_cnt);
2019-07-14 04:34:58 +00:00
} elseif(array_key_equals("do_count", $options, "count")) {
2019-07-14 05:00:53 +00:00
$_cnt = count($rows);
2019-07-14 04:34:58 +00:00
} elseif(array_key_equals("do_count", $options, "PDOStatement::rowCount")) {
2019-07-14 05:00:53 +00:00
$_cnt = $stmt->rowCount();
2019-05-20 08:19:05 +00:00
} else {
$response = $rows;
$is_not_countable = true;
}
if(!$is_not_countable) {
2019-11-17 13:41:24 +00:00
$response = array();
if(get_old_version() == "1.4") { // compatible 1.4 or below
$response['length'] = $_cnt;
}
$response['cnt'] = $_cnt;
$response['data'] = $rows;
2019-05-20 08:19:05 +00:00
}
return $response;
}
2018-02-13 07:37:27 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("exec_db_fetch")) {
2019-05-20 08:19:05 +00:00
function exec_db_fetch($sql, $bind=array(), $start=0, $bind_limit=false) {
$fetched = NULL;
$rows = array();
if($bind_limit == true) {
$sql = $sql . " limit 1";
}
$rows = exec_db_fetch_all($sql, $bind);
if(check_array_length($rows, $start) > 0) {
$idx = 0;
foreach($rows as $row) {
if($idx >= $start) {
$fetched = $row;
break;
}
$idx++;
}
}
return $fetched;
}
2018-03-18 17:11:27 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_page_range")) {
2019-05-20 08:19:05 +00:00
function get_page_range($page=1, $limit=16) {
$append_sql = "";
2018-06-25 07:32:14 +00:00
2019-05-20 08:19:05 +00:00
if($page < 1) {
$page = 1;
}
2018-06-25 07:32:14 +00:00
2019-05-20 08:19:05 +00:00
if($limit > 0) {
$record_start = ($page - 1) * $limit;
$number_of_records = $limit;
$append_sql .= sprintf(" limit %s, %s", $record_start, $number_of_records);
}
2018-11-26 16:13:30 +00:00
2019-05-20 08:19:05 +00:00
return $append_sql;
}
2018-03-26 02:52:49 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sql_insert")) {
2019-09-25 07:48:08 +00:00
function get_bind_to_sql_insert($tablename, $bind, $options=array()) {
// check ignore
if(!array_key_empty("ignore", $options)) {
2019-09-25 08:05:36 +00:00
$cnt = intval(
get_value_in_array("cnt", exec_db_fetch(get_bind_to_sql_select($tablename, false, array(
"getcnt" => true,
"setwheres" => $options['ignore']
)), false), 0)
);
2019-09-25 07:48:08 +00:00
if($cnt > 0) {
return "select " . $cnt;
}
}
// make SQL statement
2019-05-20 08:19:05 +00:00
$bind_keys = array_keys($bind);
2019-12-30 12:15:00 +00:00
$sql = "insert into `%s` (%s) values (:%s)";
2018-04-13 05:08:11 +00:00
2019-05-20 08:19:05 +00:00
$s1 = $tablename;
2019-11-10 10:27:02 +00:00
$s2 = sprintf("`%s`", implode("`, `", $bind_keys));
2019-05-20 08:19:05 +00:00
$s3 = implode(", :", $bind_keys);
2018-04-13 05:08:11 +00:00
2019-05-20 08:19:05 +00:00
$sql = sprintf($sql, $s1, $s2, $s3);
2019-09-25 07:48:08 +00:00
2019-05-20 08:19:05 +00:00
return $sql;
}
2018-04-13 05:08:11 +00:00
}
2019-10-02 11:33:54 +00:00
// Deprecated: get_bind_to_sql_where($bind, $excludes) - lower than 1.6
2019-10-02 11:38:14 +00:00
// Now: get_bind_to_sql_where($bind, $options, $_options) - 1.6 or above
2019-10-02 11:33:54 +00:00
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sql_where")) {
2019-05-20 08:19:05 +00:00
// warning: variable k is not protected. do not use variable k and external variable without filter
2019-10-02 11:38:14 +00:00
function get_bind_to_sql_where($bind, $options=array(), $_options=array()) {
2019-10-02 11:28:48 +00:00
$s3 = "";
2019-10-04 01:26:06 +00:00
$sp = "";
2019-10-02 11:28:48 +00:00
2019-11-17 13:38:06 +00:00
$excludes = get_value_in_array("excludes", $options, array());
2019-11-17 13:41:24 +00:00
if(get_old_version() == "1.5") { // compatible 1.5 or below
2019-10-02 11:38:14 +00:00
$excludes = $options;
2019-10-02 11:33:54 +00:00
}
2019-10-02 11:28:48 +00:00
if(is_array($bind)) {
2019-06-12 12:06:52 +00:00
foreach($bind as $k=>$v) {
if(!in_array($k, $excludes)) {
2019-10-02 11:28:48 +00:00
$s3 .= sprintf(" and %s = :%s", $k, $k);
}
}
}
if(!array_keys_empty(array("settimefield", "setminutes"), $options)) {
$s3 .= get_bind_to_sql_past_minutes($options['settimefield'], $options['setminutes']);
}
2019-10-02 11:47:53 +00:00
2019-10-02 11:28:48 +00:00
if(!array_key_empty("setwheres", $options)) {
if(is_array($options['setwheres'])) {
foreach($options['setwheres'] as $opts) {
if(check_is_string_not_array($opts)) {
$s3 .= sprintf(" and (%s)", $opts);
} elseif(check_array_length($opts, 3) == 0 && is_array($opts[2])) {
$s3 .= sprintf(" %s (%s)", $opts[0], get_db_binded_sql($opts[1], $opts[2]));
} elseif(check_array_length($opts, 2) == 0 && is_array($opts[1])) {
2019-10-02 11:47:53 +00:00
if(is_array($opts[1][0])) {
// recursive
2019-10-04 01:26:06 +00:00
$s3 .= sprintf(" %s (%s)", $opts[0], get_bind_to_sql_where(false, array(
"setwheres" => $opts[1]
)));
2019-10-02 11:47:53 +00:00
} elseif($opts[1][0] == "like") {
2019-10-02 11:28:48 +00:00
if(check_array_length($opts[1][2], 0) > 0) {
$s3a = array();
foreach($opts[1][2] as $word) {
$s3a[] = sprintf("%s like '%s'", get_value_in_array($opts[1][1], $s1a, $opts[1][1]), "%{$word}%");
}
$s3 .= sprintf(" %s (%s)", $opts[0], implode(" and ", $s3a));
} else {
$s3 .= sprintf(" %s (%s like %s)", $opts[0], get_value_in_array($opts[1][1], $s1a, $opts[1][1]), "'%{$opts[1][2]}%'");
}
2019-10-16 10:59:14 +00:00
} elseif($opts[1][0] == "left") {
if(check_array_length($opts[1][2], 0) > 0) {
$s3a = array();
foreach($opts[1][2] as $word) {
$s3a[] = sprintf("%s like '%s'", get_value_in_array($opts[1][1], $s1a, $opts[1][1]), "{$word}%");
}
$s3 .= sprintf(" %s (%s)", $opts[0], implode(" and ", $s3a));
} else {
$s3 .= sprintf(" %s (%s like %s)", $opts[0], get_value_in_array($opts[1][1], $s1a, $opts[1][1]), "'{$opts[1][2]}%'");
}
} elseif($opts[1][0] == "right") {
if(check_array_length($opts[1][2], 0) > 0) {
$s3a = array();
foreach($opts[1][2] as $word) {
$s3a[] = sprintf("%s like '%s'", get_value_in_array($opts[1][1], $s1a, $opts[1][1]), "%{$word}");
}
$s3 .= sprintf(" %s (%s)", $opts[0], implode(" and ", $s3a));
} else {
$s3 .= sprintf(" %s (%s like %s)", $opts[0], get_value_in_array($opts[1][1], $s1a, $opts[1][1]), "'%{$opts[1][2]}'");
}
2019-10-02 11:28:48 +00:00
} elseif($opts[1][0] == "in") {
if(check_array_length($opts[1][2], 0) > 0) {
$s3 .= sprintf(" %s (%s in ('%s'))", $opts[0], $opts[1][1], implode("', '", $opts[1][2]));
}
} elseif($opts[1][0] == "set") {
if(check_array_length($opts[1][2], 0) > 0) {
$s3a = array();
foreach($opts[1][2] as $word) {
$s3a[] = sprintf("find_in_set('%s', %s)", $word, $opts[1][1]);
}
$s3 .= sprintf(" %s (%s)", $opts[0], implode(" and ", $s3a));
}
2019-12-05 06:49:04 +00:00
} elseif($opts[1][0] == "interval") {
$s3u = array("s" => 1, "m" => 60, "h" => 120, "d" => 86400);
// todo
2019-10-02 11:28:48 +00:00
} else {
$ssts = array(
"eq" => "=",
"lt" => "<",
"lte" => "<=",
"gt" => ">",
"gte" => ">=",
"not" => "<>"
);
$opcode = get_value_in_array($opts[1][0], $ssts, $opts[1][0]);
if(!empty($opcode)) {
$s3 .= sprintf(" %s (%s %s '%s')", $opts[0], $opts[1][1], $opcode, $opts[1][2]);
}
}
} elseif(check_array_length($opts, 2) == 0) {
$s3 .= sprintf(" %s (%s)", $opts[0], $opts[1]);
}
2019-06-12 12:06:52 +00:00
}
2019-05-20 08:19:05 +00:00
}
}
2019-10-02 11:28:48 +00:00
if(!array_key_empty("sql_where", $options)) {
$s3 .= sprintf(" %s", $options['sql_where']);
}
2019-10-02 11:33:54 +00:00
2019-10-04 01:26:06 +00:00
// set start prefix
$s3 = trim($s3);
$s3a = strpos($s3, " ");
$s3b = "";
if($s3a !== false) {
$s3b = substr($s3, 0, $s3a);
}
if($s3b == "and") {
$sp = "1";
} elseif($s3b == "or") {
$sp = "0";
}
return sprintf("%s %s", $sp, $s3);
2019-05-20 08:19:05 +00:00
}
2018-03-26 02:52:49 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sql_update_set")) {
2019-05-20 08:19:05 +00:00
// 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[] = sprintf("%s = :%s", $k, $k);
}
}
$sql_update_set = implode(", ", $set_items);
return $sql_update_set;
}
2018-06-28 08:21:20 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sql_select")) {
2019-05-20 08:19:05 +00:00
// warning: variable k is not protected. do not use variable k and external variable without filter
function get_bind_to_sql_select($tablename, $bind=array(), $options=array()) {
2019-10-04 01:26:06 +00:00
$sql = "select %s from %s where %s %s %s";
2019-05-20 08:19:05 +00:00
2019-12-31 06:57:36 +00:00
// db_separated_tables: check it is seperated table
$config = get_config();
$db_separated_tables = explode(",", $config['db_separated_tables']);
$is_separated = in_array($tablename, $db_seperated_tables);
2019-05-20 08:19:05 +00:00
// s1: select fields
$s1 = "";
if(!array_key_empty("fieldnames", $options)) {
$s1 .= (check_array_length($options['fieldnames'], 0) > 0) ? implode(", ", $options['fieldnames']) : "*";
2019-07-14 05:04:49 +00:00
} elseif(array_key_equals("getcnt", $options, true)) {
2019-07-14 05:00:53 +00:00
$s1 .= sprintf("count(%s) as cnt", ($options['getcnt'] === true ? "*" : $options['getcnt']));
2019-05-20 08:19:05 +00:00
} elseif(!array_key_empty("getsum", $options)) {
$s1 .= sprintf("sum(%s) as sum", $options['getsum']);
} else {
$s1 .= "*";
}
// s1a: s1 additonal (set new fields)
$s1a = array();
if(array_key_is_array("setfields", $options)) {
2019-07-14 05:04:49 +00:00
$setfields = $options['setfields'];
2019-05-20 08:19:05 +00:00
2019-07-14 05:04:49 +00:00
foreach($setfields as $k=>$v) {
2019-09-30 02:39:14 +00:00
// add
if(!array_keys_empty("add", $v)) {
$s1a[$k] = sprintf("(%s + %s)", $k, $v['add']);
}
// sub
if(!array_keys_empty("sub", $v)) {
$s1a[$k] = sprintf("(%s - %s)", $k, $v['sub']);
}
// mul
if(!array_key_empty("mul", $v)) {
$s1a[$k] = sprintf("(%s * %s)", $k, $v['mul']);
}
// div
if(!array_key_empty("div", $v)) {
$s1a[$k] = sprintf("(%s / %s)", $k, $v['div']);
}
2019-09-30 05:41:12 +00:00
// eval (warning: do not use if you did not understand enough)
if(!array_key_empty("eval", $v)) {
$s1a[$k] = sprintf("(%s)", $k, $v['eval']);
}
2019-09-30 02:39:14 +00:00
2019-05-20 08:19:05 +00:00
// concat and delimiter
2019-07-14 05:04:49 +00:00
if(!array_keys_empty("concat", $v)) {
$delimiter = get_value_in_array("delimiter", $v, " ");
$s1a[$k] = sprintf("concat(%s)", implode(sprintf(", '%s', ", $delimiter), $v['concat']));
2019-05-20 08:19:05 +00:00
}
2019-07-14 05:04:49 +00:00
// use mysql function
2019-09-27 05:43:40 +00:00
if(!array_key_empty("call", $v)) {
if(check_array_length($v['call'], 1) > 0) {
2019-05-20 08:19:05 +00:00
// add to s1a
2019-09-27 05:43:40 +00:00
$s1a[$k] = sprintf("%s(%s)", $v['call'][0], implode(", ", array_slice($v['call'], 1)));
2019-07-14 05:04:49 +00:00
}
}
// use simple distance
if(!array_key_empty("simple_distance", $v)) {
if(check_array_length($v['simple_distance'], 2) == 0) {
$a = floatval($v['simple_distance'][1]); // percentage (range 0 to 1)
$b = $v['simple_distance'][0]; // field or number
$s1a[$k] = sprintf("abs(1.0 - (abs(%s - %s) / %s))", $b, $a, $a);
2019-06-18 07:20:14 +00:00
}
2019-06-18 07:17:30 +00:00
}
2019-05-20 08:19:05 +00:00
}
}
// s2: set table name
$s2 = "";
if(!empty($tablename)) {
$s2 .= $tablename;
} else {
set_error("tablename can not empty");
show_errors();
}
// s3: fields of where clause
2019-10-02 11:28:48 +00:00
$s3 = get_bind_to_sql_where($bind, $options);
2019-05-20 08:19:05 +00:00
// s4: set orders
$s4 = "";
$s4a = array();
if(!array_key_empty("setorders", $options)) {
if(is_array($options['setorders'])) {
$s4 .= "order by ";
foreach($options['setorders'] as $opts) {
if(check_is_string_not_array($opts)) {
$s4a[] = $opts;
} elseif(check_array_length($opts, 2) == 0) {
// example: array("desc", "datetime")
2019-07-14 05:04:49 +00:00
$s4a[] = sprintf("%s %s", get_value_in_array($opts[1], $s1a, $opts[1]), $opts[0]);
2019-05-20 08:19:05 +00:00
}
}
$s4 .= implode(", ", $s4a);
}
}
// s5: set page and limit
$s5 = "";
if(!array_keys_empty(array("setpage", "setlimit"), $options)) {
$s5 .= get_page_range($options['setpage'], $options['setlimit']);
}
2019-12-31 06:57:36 +00:00
// sql: make completed SQL
if(!$is_separated) {
$sql = sprintf($sql, $s1, $s2, $s3, $s4, $s5);
} else {
$separated_sqls = array();
2019-12-31 06:59:10 +00:00
$_sql = sprintf("select table_name from `%s.tables`", $tablename);
$_rows = exec_db_fetch_all($_sql);
foreach($_rows as $_row) {
$separated_sqls[] = sprintf($sql, $s1, $_row['table_name'], $s3, $s4, $s5);
2019-12-31 06:57:36 +00:00
}
$sql = sprintf("(%s)", implode(") union (", $separated_sqls);
}
2019-05-20 08:19:05 +00:00
return $sql;
}
2018-06-08 06:47:46 +00:00
}
2019-10-02 11:33:54 +00:00
// Deprecated: get_bind_to_sql_update($tablename, $bind, $filters, $options) - lower than 1.6
// Now: get_bind_to_sql_update($tablename, $bind, $options, $_options) - 1.6 or above
2019-09-30 05:30:48 +00:00
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sql_update")) {
2019-10-01 09:30:50 +00:00
function get_bind_to_sql_update($tablename, $bind, $options=array(), $_options=array()) {
2019-05-20 08:19:05 +00:00
$excludes = array();
2019-10-04 01:26:06 +00:00
$_bind = array();
2019-10-02 11:40:50 +00:00
// compatible version 1.5
2019-11-17 13:38:06 +00:00
if(get_old_version() == "1.5") {
2019-10-02 11:40:50 +00:00
foreach($options as $k=>$v) {
if($v == true) {
$excludes[] = $k;
}
}
$options = $_options;
}
2019-11-17 13:38:06 +00:00
2019-10-01 09:30:50 +00:00
// setkeys
2019-09-30 05:30:48 +00:00
if(!array_key_empty("setkeys", $options)) {
$setkeys = $options['setkeys'];
foreach($bind as $k=>$v) {
if(in_array($k, $setkeys)) {
2019-10-04 01:26:06 +00:00
$_bind[$k] = $v;
2019-05-20 08:19:05 +00:00
$excludes[] = $k;
}
}
}
2019-10-04 01:26:06 +00:00
// add excludes to options
if(!array_key_exists("excludes", $options)) {
$options['excludes'] = array();
}
foreach($excludes as $k=>$v) {
$options['excludes'][$k] = $v;
}
2019-05-20 08:19:05 +00:00
// make sql 'where' clause
2019-10-04 01:26:06 +00:00
$sql_where = get_db_binded_sql(get_bind_to_sql_where($_bind, $options), $_bind);
2019-10-02 11:28:48 +00:00
2019-05-20 08:19:05 +00:00
// make sql 'update set' clause
$sql_update_set = get_bind_to_sql_update_set($bind, $excludes);
// make completed sql statement
2019-10-04 01:26:06 +00:00
$sql = sprintf("update %s set %s where %s", $tablename, $sql_update_set, $sql_where);
2019-05-20 08:19:05 +00:00
return $sql;
}
2018-06-08 06:47:46 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sql_delete")) {
2019-05-20 08:19:05 +00:00
function get_bind_to_sql_delete($tablename, $bind, $options=array()) {
2019-10-04 01:28:43 +00:00
$sql = sprintf("delete from %s where %s", $tablename, get_db_binded_sql(get_bind_to_sql_where($bind, $options), $bind));
2019-05-20 08:19:05 +00:00
return $sql;
}
2018-06-08 06:47:46 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sql_past_minutes")) {
2019-05-20 08:19:05 +00:00
function get_bind_to_sql_past_minutes($fieldname, $minutes=5) {
$sql_past_minutes = "";
if($minutes > 0) {
$sql_past_minutes = sprintf(" and %s > DATE_SUB(now(), INTERVAL %d MINUTE)", $fieldname, $minutes);
}
return $sql_past_minutes;
}
2018-05-31 03:56:44 +00:00
}
2018-06-13 08:50:07 +00:00
// SQL eXtensible
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sqlx")) {
2019-05-20 08:19:05 +00:00
function get_bind_to_sqlx($filename, $bind, $options=array()) {
$result = false;
$sql = read_storage_file(get_file_name($filename, "sqlx"), array(
"storage_type" => "sqlx"
));
if(!empty($sql)) {
$result = get_db_binded_sql($sql, $bind);
}
return $result;
}
2018-06-13 08:50:07 +00:00
}
2018-03-26 02:52:49 +00:00
// alias sql_query from exec_db_query
2019-02-26 05:40:03 +00:00
if(!check_function_exists("sql_query")) {
2019-05-20 08:19:05 +00:00
function sql_query($sql, $bind=array(), $options=array()) {
return exec_db_query($sql, $bind, $options);
}
2018-03-26 02:52:49 +00:00
}
2019-12-30 12:15:00 +00:00
2018-04-13 05:08:11 +00:00
// get timediff
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_timediff_on_query")) {
2019-05-20 08:19:05 +00:00
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);
2019-10-04 04:00:38 +00:00
return $dt;
2019-05-20 08:19:05 +00:00
}
2018-04-13 05:08:11 +00:00
}
2019-12-30 12:15:00 +00:00
// make sql statement to create table
if(!check_function_exists("get_bind_to_sql_create")) {
function get_bind_to_sql_create($schemes, $options=array()) {
$sql = false;
$_prefix = get_value_in_array("prefix", $options, "");
$_suffix = get_value_in_array("suffix", $options, "");
$_tablename = get_value_in_array("tablename", $options, "");
$_temporary = get_value_in_array("temporary", $options, false);
2019-10-15 10:25:31 +00:00
$_schemes = array();
2019-12-30 12:15:00 +00:00
if(!empty($_tablename)) {
$tablename = sprintf("%s%s%s", $_prefix, $_tablename, $_suffix);
foreach($schemes as $k=>$v) {
if(is_array($v)) {
$_argc = count($v);
if($_argc == 1) {
$_schemes[] = sprintf("%s %s", $k, $v[0]);
} elseif($_argc == 2) {
$_schemes[] = sprintf("%s %s(%s)", $k, $v[0], $v[1]);
} elseif($_argc == 3) {
$_schemes[] = sprintf("%s %s(%s) %s", $k, $v[0], $v[1], ($v[2] === true ? "not null" : ""));
}
2019-10-15 10:25:31 +00:00
}
2019-10-15 08:37:21 +00:00
}
2019-12-30 12:15:00 +00:00
if($_temporary !== false) {
$sql = sprintf("create temporary table if not exists `%s` (%s)", $tablename, implode(",", $_schemes));
} else {
$sql = sprintf("create table if not exists `%s` (%s)", $tablename, implode(",", $_schemes));
}
}
return $sql;
}
}
// table creation
if(!check_function_exists("exec_db_table_create")) {
function exec_db_table_create($schemes, $tablename, $options=array()) {
$_prefix = get_value_in_array("prefix", $options, "");
$_suffix = get_value_in_array("suffix", $options, "");
$_tablename = sprintf("%s%s%s", $_prefix, $tablename, $_suffix);
$_tablename_p = sprintf("%s%s", $_prefix, $tablename);
$_tablename_s = sprintf("%s%s", $tablename, $_suffix);
$_tablename_t = sprintf("%s.tables", $_tablename_p);
// get index options
$config = get_config();
$setindex = get_value_in_array("setindex", $options, false);
// check if exists table
$bind = array(
"table_schema" => $config['db_name'],
"table_name" => $_tablename
);
$sql = get_bind_to_sql_select("information_schema.tables", $bind);
$rows = exec_db_fetch_all($sql, $bind);
foreach($rows as $row) {
return $row['TABLE_NAME'];
}
// create table
$sql = get_bind_to_sql_create($schemes, array(
"tablename" => $_tablename
));
if(!exec_db_query($sql)) {
return false;
} else {
if($_suffix != ".tables") {
2019-12-30 12:17:27 +00:00
// create meta table
$schemes_t = array(
"table_name" => array("varchar", 255),
"datetime" => array("datetime")
);
$_tablename_t = exec_db_table_create($schemes_t, $tablename, array(
"prefix" => $_prefix,
"suffix" => ".tables",
"setindex" => array(
"index_1" => array("datetime")
)
));
// add table name to meta table
$bind = array(
"table_name" => $_tablename,
"datetime" => get_current_datetime()
);
$sql = get_bind_to_sql_insert($_tablename_t, $bind);
exec_db_query($sql, $bind);
2019-12-30 12:15:00 +00:00
}
// create index
foreach($setindex as $k=>$v) {
$sql = sprintf("create index `%s` on `%s` (%s)", $k, $_tablename, implode(", ", $v));
exec_db_query($sql);
}
2019-10-15 08:31:29 +00:00
}
2019-12-30 12:15:00 +00:00
return $_tablename;
}
}
// temporary table creation
if(!check_function_exists("exec_db_temp_create")) {
function exec_db_temp_create($schemes, $options=array()) {
$tablename = make_random_id();
$sql = get_bind_to_sql_create($schemes, array(
"tablename" => $tablename,
"temporary" => true
));
return (exec_db_query($sql) ? $tablename : false);
2019-10-15 08:31:29 +00:00
}
}
2019-10-04 03:56:37 +00:00
if(!check_function_exists("exec_db_temp_start")) {
2019-10-10 17:04:00 +00:00
function exec_db_temp_start($sql, $bind=array(), $options=array()) {
2019-10-04 03:56:37 +00:00
$_tablename = make_random_id();
$_sql = sprintf("create temporary table if not exists %s as (%s)", $_tablename, get_db_binded_sql($sql, $bind));
return (exec_db_query($_sql) ? $_tablename : false);
}
}
// temporary table
if(!check_function_exists("exec_db_temp_end")) {
2019-10-04 03:57:39 +00:00
function exec_db_temp_end($tablename, $options=array()) {
2019-10-04 03:56:37 +00:00
$_sql = sprintf("drop temporary table %s", $tablename);
return exec_db_query($_sql);
}
}
2018-05-29 06:38:09 +00:00
// close db connection
2019-02-26 05:40:03 +00:00
if(!check_function_exists("close_db_connect")) {
2019-05-20 08:19:05 +00:00
function close_db_connect() {
$dbc = get_scope("dbc");
$dbc->close();
set_scope("dbc", null);
}
2018-05-29 06:38:09 +00:00
}
2019-10-12 15:24:56 +00:00
// get assoc from json raw data
if(!check_function_exists("json_decode_to_assoc")) {
function json_decode_to_assoc($data) {
if(loadHelper("json.format")) {
return json_decode_ex($data, array("assoc" => true));
}
}
}