reasonableframework/system/database.php

1044 lines
36 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
2019-12-31 07:05:28 +00:00
* @created_on 2018-04-13
2020-01-19 04:35:16 +00:00
* @updated_on 2020-01-19
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")) {
2020-01-09 10:45:01 +00:00
function get_db_connect($_RETRY=0) {
2019-05-20 08:19:05 +00:00
$conn = false;
2020-01-09 10:45:01 +00:00
2020-01-09 10:39:52 +00:00
$config = get_config();
2019-05-20 08:19:05 +00:00
$db_driver = get_db_driver();
2020-01-09 10:45:01 +00:00
$dsn = "mysql:host=%s;dbname=%s;charset=utf8";
2020-01-09 11:14:16 +00:00
$_RETRY_LIMIT = get_value_in_array("db_retry_limit", $config, 3);
2019-05-20 08:19:05 +00:00
if(in_array($db_driver, array("mysql", "mysql.pdo"))) {
try {
$conn = new PDO(
2020-01-09 10:45:01 +00:00
sprintf($dsn, $config['db_host'], $config['db_name']),
2019-05-20 08:19:05 +00:00
$config['db_username'],
$config['db_password'],
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
)
);
2020-01-09 10:45:01 +00:00
//$conn->query("SET NAMES utf8");
2019-05-20 08:19:05 +00:00
} catch(Exception $e) {
2020-01-09 11:14:16 +00:00
if($_RETRY > $_RETRY_LIMIT) {
2019-05-20 08:19:05 +00:00
set_error($e->getMessage());
show_errors();
} else {
2020-01-09 10:45:01 +00:00
$_RETRY++;
$conn = get_db_connect($_RETRY);
2019-05-20 08:19:05 +00:00
}
}
} 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) {
if($renew) {
set_scope("dbc", get_db_connect());
}
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")) {
2020-01-09 12:26:03 +00:00
function get_db_stmt($sql, $bind=array(), $options=array()) {
$stmt = NULL;
$dbc = get_dbc_object();
$binder = get_value_in_array("binder", $options, "php");
2019-05-20 08:19:05 +00:00
2020-01-09 12:26:03 +00:00
if($binder == "pdo") {
$stmt = $dbc->prepare($sql);
foreach($bind as $k=>$v) {
$stmt->bindParam(sprintf(":%s", $k), $v);
2019-05-20 08:19:05 +00:00
}
2020-01-09 12:26:03 +00:00
} else {
$sql = get_db_binded_sql($sql, $bind);
$stmt = $dbc->prepare($sql);
2019-05-20 08:19:05 +00:00
}
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()) {
$flag = false;
2020-01-09 12:17:02 +00:00
// set variable
$dbc = get_dbc_object();
$terms = explode(" ", trim($sql));
2019-05-20 08:19:05 +00:00
2020-01-09 12:17:02 +00:00
// before transaction
$dbc->beginTransaction();
// check sql insert or not
if($terms[0] == "insert") {
2020-01-09 12:26:03 +00:00
$stmt = get_db_stmt($sql, $bind, array(
"binder" => "pdo"
));
$flag = $stmt->execute($bind);
2019-05-20 08:19:05 +00:00
} else {
2020-01-09 12:26:03 +00:00
$stmt = get_db_stmt($sql, $bind, array(
"binder" => "php"
));
2020-01-09 12:17:02 +00:00
$flag = $stmt->execute();
2019-05-20 08:19:05 +00:00
}
2020-01-09 12:17:02 +00:00
// commit transaction
$dbc->commit();
2020-01-09 12:34:25 +00:00
// get errors
$errors = $stmt->errorInfo();
2019-05-20 08:19:05 +00:00
2020-01-09 12:17:02 +00:00
// if failed
if(!$flag) {
write_common_log(get_hashed_text($sql), "DATABASE-FAILED-EXECUTE");
write_common_log($sql, "DATABASE-FAILED-QUERY");
2020-01-09 12:45:36 +00:00
write_common_log(get_db_binded_sql($sql, $bind), "DATABASE-FAILED-BINDED-QUERY");
2020-01-09 12:34:25 +00:00
write_common_log(implode(",", $errors), "DATABASE-FAILED-ERROR");
2019-05-20 08:19:05 +00:00
}
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()) {
2020-01-09 11:14:16 +00:00
$result = array();
2019-07-14 04:34:58 +00:00
2020-01-09 11:14:16 +00:00
// set is counted
$is_counted = false;
2019-07-14 04:34:58 +00:00
2019-05-20 08:19:05 +00:00
$rows = array();
$stmt = get_db_stmt($sql, $bind);
2020-01-09 11:14:16 +00:00
// get rows
2019-05-20 08:19:05 +00:00
if($stmt->execute() && $stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
2020-01-09 11:14:16 +00:00
// get rows with removed keys (1.6 or above)
2019-10-21 02:05:30 +00:00
$_rows = array();
if(array_key_equals("getvalues", $options, true)) {
foreach($rows as $row) {
$_rows[] = array_values($row);
}
$rows = $_rows;
}
2020-01-09 11:14:16 +00:00
// get number of rows
$num_rows = 0;
if(array_key_equals("getcount", $options, true)) {
$sql = sprintf("select count(*) as value from (%s) a", get_db_binded_sql($sql, $bind));
$rows = exec_db_fetch_all($sql, $bind);
foreach($rows as $row) {
$num_rows += intval($row['value']);
}
$is_counted = true;
} elseif(array_key_equals("getcount", $options, "php")) {
$num_rows = count($rows);
$is_counted = true;
} elseif(array_key_equals("getcount", $options, "pdo")) {
$num_rows = $stmt->rowCount();
$is_counted = true;
2019-05-20 08:19:05 +00:00
}
2020-01-09 11:14:16 +00:00
// make a result
if($is_counted) {
$result = array(
"count" => $num_rows,
"data" => $rows
);
} else {
$result = $rows;
2019-05-20 08:19:05 +00:00
}
2020-01-09 11:14:16 +00:00
return $result;
2019-05-20 08:19:05 +00:00
}
2018-02-13 07:37:27 +00:00
}
2020-01-09 11:20:52 +00:00
if(!check_function_exists("get_db_zero")) {
function get_db_zero($value) {
2020-01-09 11:34:13 +00:00
return ($value > 0 ? $value : '0');
2020-01-09 11:20:52 +00:00
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("exec_db_fetch")) {
2020-01-09 11:14:16 +00:00
function exec_db_fetch($sql, $bind=array(), $start=0) {
$row = NULL;
2020-01-09 12:01:23 +00:00
$rows = NULL;
2019-05-20 08:19:05 +00:00
2020-01-09 11:14:16 +00:00
$config = get_config();
$fetch_mode = get_value_in_array("db_fetch_mode", $config, "sql");
if($fetch_mode == "sql") {
2020-01-09 12:01:23 +00:00
$_bind = $bind;
$_sql = sprintf("%s limit %s, 1", $sql, get_db_zero($start));
$rows = exec_db_fetch_all($_sql, $_bind);
2020-01-09 11:14:16 +00:00
} elseif($fetch_mode == "php") {
2020-01-09 12:01:23 +00:00
$_bind = $bind;
2020-01-09 11:14:16 +00:00
$_sql = $sql;
2020-01-09 12:01:23 +00:00
$rows = exec_db_fetch_all($_sql, $_bind);
$rows = array_slice($rows, get_db_zero($start), 1);
2019-05-20 08:19:05 +00:00
}
2020-01-09 11:14:16 +00:00
// get first of rows
$row = current($rows);
// return row
return $row;
2019-05-20 08:19:05 +00:00
}
2018-03-18 17:11:27 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_page_range")) {
2020-01-09 11:20:52 +00:00
function get_page_range($page=1, $limit=15) {
$sql = false;
$page = ($page > 1 ? $page : 1);
2019-05-20 08:19:05 +00:00
if($limit > 0) {
2020-01-09 11:23:27 +00:00
$_START = get_db_zero(($page - 1) * $limit);
$_LIMIT = get_db_zero($limit);
$sql = sprintf(" limit %s, %s", $_START, $_LIMIT);
2019-05-20 08:19:05 +00:00
}
2018-11-26 16:13:30 +00:00
2020-01-09 11:20:52 +00:00
return $sql;
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_insert")) {
2019-09-25 07:48:08 +00:00
function get_bind_to_sql_insert($tablename, $bind, $options=array()) {
2020-01-09 09:19:15 +00:00
$sql = false;
2020-01-09 08:15:09 +00:00
2020-01-09 08:20:12 +00:00
// get not duplicatable fieldnames
2020-01-16 06:32:36 +00:00
$setkeys = get_array(get_value_in_array("setkeys", $options, false));
$setignores = get_array(get_value_in_array("setignores", $options, false));
2020-01-21 15:37:11 +00:00
$setwheres = get_array(get_value_in_array("setwheres", $options, false));
2020-01-21 15:33:29 +00:00
// safemode_off (default: false)
$safemode_off = array_key_equals("safemode_off", $options, true);
2020-01-09 08:25:32 +00:00
2020-01-10 02:33:14 +00:00
// set variables
2020-01-09 08:15:09 +00:00
$num_duplicates = 0;
2020-01-10 02:33:14 +00:00
$num_ignores = 0;
2020-01-09 08:48:37 +00:00
2020-01-10 02:33:14 +00:00
// check duplicates
2020-01-16 06:32:36 +00:00
if(count($setkeys) > 0) {
2020-01-09 13:03:41 +00:00
$_bind_K = array();
$_bind_V = array();
foreach($bind as $k=>$v) {
2020-01-16 06:32:36 +00:00
if(in_array($k, $setkeys)) {
2020-01-09 13:03:41 +00:00
$_bind_K[$k] = $v;
} else {
$_bind_V[$k] = $v;
}
}
2020-01-10 10:32:43 +00:00
$_options = array(
2020-01-10 02:33:14 +00:00
"getcount" => true
2020-01-10 10:32:43 +00:00
);
2020-01-10 02:33:14 +00:00
$_sql = get_bind_to_sql_select($tablename, $_bind_K, $_options);
2020-01-09 13:03:41 +00:00
$_rows = exec_db_fetch_all($_sql, $_bind_K);
foreach($_rows as $_row) {
$num_duplicates += intval($_row['value']);
2019-09-25 07:48:08 +00:00
}
2020-01-21 15:37:11 +00:00
}
// preventing accidentally query
$num_conditions = count($setkeys) + count($setwheres);
if($num_conditions == 0 && $safemode_off !== true) {
2020-01-21 15:33:29 +00:00
write_common_log("Blocked this SQL because it is maybe accidentally query. If you want continue, set safemode_off option to true", "system/database");
return false;
2019-09-25 07:48:08 +00:00
}
2020-01-09 08:20:12 +00:00
2020-01-10 02:33:14 +00:00
// check ignores
if(count($setignore) > 0) {
2020-01-19 04:35:16 +00:00
$_bind = false;
2020-01-10 10:32:43 +00:00
$_options = array(
2020-01-10 02:34:04 +00:00
"getcount" => true,
2020-01-16 06:32:36 +00:00
"setwheres" => $setignores
2020-01-10 10:32:43 +00:00
);
2020-01-19 04:35:16 +00:00
$_sql = get_bind_to_sql_select($tablename, $_bind, $_options);
$_rows = exec_db_fetch_all($_sql, $_bind);
2020-01-10 02:33:14 +00:00
foreach($_rows as $_row) {
$num_ignores += intval($_row['value']);
}
}
2020-01-09 08:20:12 +00:00
// make statements
2020-01-10 02:33:14 +00:00
if($num_ignores > 0) {
$sql = "select 1";
} elseif($num_duplicates > 0) {
2020-01-09 08:15:09 +00:00
$sql = get_bind_to_sql_update($tablename, $bind, array(
2020-01-09 08:53:00 +00:00
"setkeys" => array_keys($_bind_K)
2020-01-10 10:36:22 +00:00
));
2020-01-09 08:15:09 +00:00
} else {
2020-01-09 09:19:15 +00:00
$sql = "insert into `%s` (%s) values (:%s)";
2020-01-09 08:15:09 +00:00
$bind_keys = array_keys($bind);
$s1 = $tablename;
$s2 = sprintf("`%s`", implode("`, `", $bind_keys));
$s3 = implode(", :", $bind_keys);
$sql = sprintf($sql, $s1, $s2, $s3);
}
2018-04-13 05:08:11 +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()) {
2020-01-08 06:46:16 +00:00
$s3 = "";
2019-10-04 01:26:06 +00:00
$sp = "";
2020-01-08 06:46:16 +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);
2020-01-08 06:46:16 +00:00
$s3a = explode(" ", $s3);
2020-01-08 13:06:14 +00:00
if(in_array($s3a[0], array("and", "or"))) {
$sp = ($s3a[0] == "and" ? "1" : "0");
} else {
$sp = "1";
}
2019-10-04 01:26:06 +00:00
return sprintf("%s %s", $sp, $s3);
2019-05-20 08:19:05 +00:00
}
2018-03-26 02:52:49 +00:00
}
2020-01-02 04:39:28 +00:00
if(!check_function_exists("check_table_is_separated")) {
function check_table_is_separated($tablename) {
$config = get_config();
$db_separated_tables = explode(",", $config['db_separated_tables']);
return in_array($tablename, $db_separated_tables);
}
}
2020-01-02 05:35:16 +00:00
if(!check_function_exists("get_db_tablenames")) {
2020-01-27 05:36:15 +00:00
function get_db_tablenames($tablename, $end_dt="", $start_dt="") {
2020-01-02 05:35:16 +00:00
$tablenames = array();
2020-01-09 08:48:37 +00:00
2020-01-02 05:35:16 +00:00
$is_separated = check_table_is_separated($tablename);
if(!$is_separated) {
$tablenames[] = $tablename;
} else {
2020-01-27 07:39:52 +00:00
$a = empty($end_dt);
$b = empty($start_dt);
$c = array( (!$a && !$b), ($a && !$b), (!$a && $b) );
$setwheres = array();
2020-01-27 05:51:03 +00:00
foreach($c as $k=>$v) {
if($v !== false) {
2020-01-27 07:39:52 +00:00
switch($v) {
2020-01-27 05:51:03 +00:00
case 0:
2020-01-27 07:39:52 +00:00
$setwheres[] = array("and", array("lte", "datetime", $end_dt));
$setwheres[] = array("and", array("gte", "datetime", $start_dt));
2020-01-27 05:51:03 +00:00
break;
case 1:
2020-01-27 07:39:52 +00:00
$setwheres[] = array("and", array("gte", "datetime", $start_dt));
2020-01-27 05:51:03 +00:00
break;
case 2:
2020-01-27 07:39:52 +00:00
$setwheres[] = array("and", array("lte", "datetime", $end_dt));
2020-01-27 05:51:03 +00:00
break;
}
}
}
2020-01-27 07:39:52 +00:00
$bind = false;
$sql = get_bind_to_sql_select(sprintf("%s.tables", $tablename), $bind, array(
"setwheres" => $setwheres,
"setorders" => array(
array("asc", "datetime")
)
));
$rows = exec_db_fetch_all($sql, $bind);
2020-01-02 05:35:16 +00:00
foreach($rows as $row) {
2020-01-09 11:37:35 +00:00
$tablenames[] = $row['table_name'];
2020-01-02 05:35:16 +00:00
}
}
return $tablenames;
}
}
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-12-31 08:01:05 +00:00
$sql = "select %s from `%s` where %s %s %s";
2019-05-20 08:19:05 +00:00
2020-01-02 04:39:28 +00:00
// is_separated: check it is seperated table
$is_separated = check_table_is_separated($tablename);
2019-12-31 06:57:36 +00:00
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']) : "*";
2020-01-09 10:39:52 +00:00
} elseif(array_key_equals("getcount", $options, true)) {
$s1 .= sprintf("count(%s) as value", ($options['getcount'] === true ? "*" : $options['getcount']));
2019-05-20 08:19:05 +00:00
} elseif(!array_key_empty("getsum", $options)) {
2020-01-09 10:39:52 +00:00
$s1 .= sprintf("sum(%s) as value", $options['getsum']);
2019-05-20 08:19:05 +00:00
} 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)) {
2020-01-09 04:23:15 +00:00
$delimiter = get_value_in_array("delimiter", $v, ",");
2019-07-14 05:04:49 +00:00
$s1a[$k] = sprintf("concat(%s)", implode(sprintf(", '%s', ", $delimiter), $v['concat']));
2019-05-20 08:19:05 +00:00
}
2020-01-09 04:23:15 +00:00
// group_concat and delimiter, condition
if(!array_keys_empty("group_concat", $v)) {
$arguments = $v['group_concat'];
$delimiter = get_value_in_array("delimiter", $v, ",");
2020-01-09 04:29:24 +00:00
// group_concat(a, b, c); a=fieldname or value(if true), b=condition, c=fieldname or value(if false)
2020-01-09 04:23:15 +00:00
if(check_array_length($arguments, 3) == 0) {
2020-01-09 04:29:24 +00:00
$s1a[$k] = sprintf("group_concat(if(%s, '%s', '%s'))", $arguments[1], make_safe_argument($arguments[0]), make_safe_argument($arguments[2]));
2020-01-09 04:25:15 +00:00
} elseif(check_array_length($arguments, 2) == 0) {
2020-01-09 04:29:24 +00:00
$s1a[$k] = sprintf("group_concat(if(%s, '%s', null))", $arguments[1], make_safe_argument($arguments[0]));
2020-01-09 04:25:15 +00:00
} elseif(check_array_length($arguments, 1) == 0) {
$s1a[$k] = sprintf("group_concat(%s)", $arguments[0]);
2020-01-09 04:23:15 +00:00
} else {
$s1a[$k] = sprintf("group_concat(%s)", $arguments);
}
}
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);
2020-01-16 06:51:20 +00:00
// s3i: set groups
$s3i = "";
if(!array_key_empty("setgroups", $options)) {
$s3i = sprintf(" group by `%s`", implode("`, `", get_array($options['setgroups'])));
$s3 .= $s3i;
}
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();
2020-01-02 05:35:16 +00:00
$tablenames = get_db_tablenames($tablename);
foreach($tablenames as $_tablename) {
$separated_sqls[] = sprintf($sql, $s1, $_tablename, $s3, $s4, $s5);
2019-12-31 06:57:36 +00:00
}
2020-01-08 13:34:57 +00:00
$sql = sprintf("%s", implode(" union all ", $separated_sqls));
2019-12-31 06:57:36 +00:00
}
2019-05-20 08:19:05 +00:00
return $sql;
}
2018-06-08 06:47:46 +00:00
}
2020-01-10 02:33:14 +00:00
if(!check_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, $options=array()) {
$sql = "";
// set variables
$sa = array();
// setkeys
$setkeys = get_array(get_value_in_array("setkeys", $options, false));
// do process
foreach($bind as $k=>$v) {
if(!in_array($k, $setkeys)) {
$sa[] = sprintf("%s = :%s", $k, $k);
}
}
// set SQL statements
$sql = implode(", ", $sa);
return $sql;
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_bind_to_sql_update")) {
2020-01-09 12:53:02 +00:00
function get_bind_to_sql_update($tablename, $bind, $options=array()) {
2020-01-09 08:15:09 +00:00
$sql = "update %s set %s where %s";
// bind `where` clause
2020-01-09 08:53:00 +00:00
$_bind_K = array();
$_bind_V = array();
2020-01-09 08:27:50 +00:00
2019-10-01 09:30:50 +00:00
// setkeys
2020-01-09 08:15:09 +00:00
$setkeys = get_array(get_value_in_array("setkeys", $options, false));
foreach($bind as $k=>$v) {
if(in_array($k, $setkeys)) {
2020-01-09 11:37:35 +00:00
$_bind_K[$k] = $v;
2020-01-09 08:15:09 +00:00
} else {
2020-01-09 11:37:35 +00:00
$_bind_V[$k] = $v;
2019-05-20 08:19:05 +00:00
}
}
2019-10-02 11:28:48 +00:00
2020-01-09 08:15:09 +00:00
// s1: make `tablename` clause
$s1 = $tablename;
2020-01-09 08:20:12 +00:00
2020-01-09 08:15:09 +00:00
// s2: make 'update set' clause
2020-01-09 12:53:02 +00:00
$s2 = get_bind_to_sql_update_set($bind, $options);
2019-05-20 08:19:05 +00:00
2020-01-09 08:15:09 +00:00
// s3: make 'where' clause
2020-01-09 08:53:00 +00:00
$s3 = get_bind_to_sql_where($_bind_K, $options);
2020-01-09 08:27:50 +00:00
2020-01-09 08:20:12 +00:00
// make completed statements
2020-01-09 08:27:50 +00:00
$sql = get_db_binded_sql(sprintf($sql, $s1, $s2, $s3), $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_delete")) {
2019-05-20 08:19:05 +00:00
function get_bind_to_sql_delete($tablename, $bind, $options=array()) {
2020-01-19 05:50:14 +00:00
$sql = sprintf("delete from `%s` where %s", $tablename, get_bind_to_sql_where($bind, $options));
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
}
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) {
2020-01-01 15:27:36 +00:00
$_schemes[] = sprintf("`%s` %s", $k, $v[0]);
2019-12-30 12:15:00 +00:00
} elseif($_argc == 2) {
2020-01-01 15:27:36 +00:00
$_schemes[] = sprintf("`%s` %s(%s)", $k, $v[0], $v[1]);
2019-12-30 12:15:00 +00:00
} elseif($_argc == 3) {
2020-01-01 15:27:36 +00:00
$_schemes[] = sprintf("`%s` %s(%s) %s", $k, $v[0], $v[1], ($v[2] === true ? "not null" : ""));
2019-12-30 12:15:00 +00:00
}
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);
2019-12-31 08:03:45 +00:00
$setunique = get_value_in_array("setunique", $options, false);
2020-01-09 04:08:05 +00:00
$setfulltext = get_value_in_array("setfulltext", $options, false);
$setspatial = get_value_in_array("setspatial", $options, false);
2019-12-30 12:15:00 +00:00
// check if exists table
$bind = array(
2019-12-31 08:01:05 +00:00
"TABLE_SCHEMA" => $config['db_name'],
"TABLE_NAME" => $_tablename
2019-12-30 12:15:00 +00:00
);
2019-12-31 08:01:05 +00:00
$sql = "select TABLE_NAME from information_schema.tables where TABLE_SCHEMA = :TABLE_SCHEMA and TABLE_NAME = :TABLE_NAME";
2019-12-30 12:15:00 +00:00
$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
));
2020-01-01 15:27:36 +00:00
2019-12-30 12:15:00 +00:00
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")
2019-12-31 08:08:11 +00:00
),
"setunique" => array(
"unique_1" => array("table_name")
2019-12-30 12:17:27 +00:00
)
));
// 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-12-31 08:03:45 +00:00
// create unique (type of index)
foreach($setunique as $k=>$v) {
$sql = sprintf("create unique index `%s` on `%s` (%s)", $k, $_tablename, implode(", ", $v));
exec_db_query($sql);
2020-01-09 04:08:05 +00:00
}
// create fulltext (type of index)
foreach($setfulltext as $k=>$v) {
$sql = sprintf("create fulltext index `%s` on `%s` (%s)", $k, $_tablename, implode(", ", $v));
exec_db_query($sql);
}
// create spatial(geometry) (type of index)
foreach($setspatial as $k=>$v) {
$sql = sprintf("create spatial 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;
}
}
2020-01-02 04:39:28 +00:00
if(!check_function_exists("exec_db_table_drop")) {
2020-01-27 05:36:15 +00:00
function exec_db_table_drop($tablename, $options=array()) {
2020-01-02 04:39:28 +00:00
$flag = true;
2020-01-27 05:36:15 +00:00
$end_dt = get_value_in_array("end_dt", $options, "");
$start_dt = get_value_in_array("start_dt", $options, "");
$tablenames = get_db_tablenames($tablename, $end_dt, $start_dt);
2020-01-02 05:35:16 +00:00
foreach($tablenames as $_tablename) {
$sql = sprintf("drop table `%s`", $_tablename);
$flag &= exec_db_query($sql);
2020-01-02 04:39:28 +00:00
}
return $flag;
}
}
2020-01-02 05:35:16 +00:00
if(!check_function_exists("exec_db_table_update")) {
function exec_db_table_update($tablename, $bind=array(), $options=array()) {
$flag = true;
$tablenames = get_db_tablenames($tablename);
foreach($tablenames as $_tablename) {
$sql = get_bind_to_sql_update($tablename, $bind, $options);
$flag &= exec_db_query($sql);
}
return $flag;
}
}
2020-01-09 08:48:37 +00:00
if(!check_function_exists("exec_db_table_insert")) {
function exec_db_table_insert($tablename, $bind=array(), $options=array()) {
$flag = true;
// set replica
$replica = get_value_in_array("setreplica", $options, 1);
// get tablenames
$tablenames = get_db_tablenames($tablename);
if($replica > 0) {
$_num_tables = count($tablenames);
$_replica = min($replica, $_num_tables);
if($replica < $_num_tables) {
2020-01-09 08:49:26 +00:00
$tablenames = array_slice(get_db_tablenames($tablename), 0, $_replica);
2020-01-09 08:48:37 +00:00
}
}
// do process
foreach($tablenames as $_tablename) {
$sql = get_bind_to_sql_insert($tablename, $bind, $options);
$flag &= exec_db_query($sql);
}
return $flag;
}
}
2019-12-30 12:15:00 +00:00
// 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();
2020-01-01 08:12:57 +00:00
$_sql = sprintf("create temporary table if not exists `%s` %s", $_tablename, get_db_binded_sql($sql, $bind));
2019-10-04 03:56:37 +00:00
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));
}
}
}