Update database.php

This commit is contained in:
Namhyeon Go 2020-01-10 11:33:14 +09:00 committed by GitHub
parent 7fd4143c71
commit 2b4c6eda84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -293,10 +293,11 @@ if(!check_function_exists("get_bind_to_sql_insert")) {
$setduplicate = get_array(get_value_in_array("setduplicate", $options, false)); $setduplicate = get_array(get_value_in_array("setduplicate", $options, false));
$setignore = get_array(get_value_in_array("setignore", $options, false)); $setignore = get_array(get_value_in_array("setignore", $options, false));
// get number of duplicated rows // set variables
$num_duplicates = 0; $num_duplicates = 0;
$num_ignores = 0;
// do process // check duplicates
if(count($setduplicate) > 0) { if(count($setduplicate) > 0) {
$_bind_K = array(); $_bind_K = array();
$_bind_V = array(); $_bind_V = array();
@ -307,18 +308,33 @@ if(!check_function_exists("get_bind_to_sql_insert")) {
$_bind_V[$k] = $v; $_bind_V[$k] = $v;
} }
} }
$_sql = get_bind_to_sql_select($tablename, $_bind_K, array( $_options = array_merge(array(
"getcount" => true, "getcount" => true
"setwheres" => $setignore ), $options);
)); $_sql = get_bind_to_sql_select($tablename, $_bind_K, $_options);
$_rows = exec_db_fetch_all($_sql, $_bind_K); $_rows = exec_db_fetch_all($_sql, $_bind_K);
foreach($_rows as $_row) { foreach($_rows as $_row) {
$num_duplicates += intval($_row['value']); $num_duplicates += intval($_row['value']);
} }
} }
// check ignores
if(count($setignore) > 0) {
$_options = array_merge(array(
"getcount" => true
"setwheres" => $setignore
), $options);
$_sql = get_bind_to_sql_select($tablename, $bind, $_options);
$_rows = exec_db_fetch_all($_sql, $bind);
foreach($_rows as $_row) {
$num_ignores += intval($_row['value']);
}
}
// make statements // make statements
if($num_duplicates > 0) { if($num_ignores > 0) {
$sql = "select 1";
} elseif($num_duplicates > 0) {
$sql = get_bind_to_sql_update($tablename, $bind, array( $sql = get_bind_to_sql_update($tablename, $bind, array(
"setkeys" => array_keys($_bind_K) "setkeys" => array_keys($_bind_K)
), $options); ), $options);
@ -457,31 +473,6 @@ if(!check_function_exists("get_bind_to_sql_where")) {
} }
} }
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;
}
}
if(!check_function_exists("check_table_is_separated")) { if(!check_function_exists("check_table_is_separated")) {
function check_table_is_separated($tablename) { function check_table_is_separated($tablename) {
$config = get_config(); $config = get_config();
@ -653,6 +644,31 @@ if(!check_function_exists("get_bind_to_sql_select")) {
} }
} }
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;
}
}
if(!check_function_exists("get_bind_to_sql_update")) { if(!check_function_exists("get_bind_to_sql_update")) {
function get_bind_to_sql_update($tablename, $bind, $options=array()) { function get_bind_to_sql_update($tablename, $bind, $options=array()) {
$sql = "update %s set %s where %s"; $sql = "update %s set %s where %s";