Update database.php
This commit is contained in:
parent
85927fec44
commit
8377fa6961
|
|
@ -311,28 +311,41 @@ if(!check_function_exists("get_page_range")) {
|
||||||
|
|
||||||
if(!check_function_exists("get_bind_to_sql_insert")) {
|
if(!check_function_exists("get_bind_to_sql_insert")) {
|
||||||
function get_bind_to_sql_insert($tablename, $bind, $options=array()) {
|
function get_bind_to_sql_insert($tablename, $bind, $options=array()) {
|
||||||
// check ignore
|
$sql = "insert into `%s` (%s) values (:%s)";
|
||||||
if(!array_key_empty("ignore", $options)) {
|
|
||||||
$cnt = intval(
|
$setduplicate = get_array(get_value_in_array("setduplicate", $options, false));
|
||||||
get_value_in_array("cnt", exec_db_fetch(get_bind_to_sql_select($tablename, false, array(
|
|
||||||
"getcnt" => true,
|
// get number of duplicated rows
|
||||||
"setwheres" => $options['ignore']
|
$num_duplicates = 0;
|
||||||
)), false), 0)
|
$_bind_T = array();
|
||||||
);
|
$_bind_F = array();
|
||||||
if($cnt > 0) {
|
foreach($bind as $k=>$v) {
|
||||||
return "select " . $cnt;
|
if(in_array($k, $setduplicate)) {
|
||||||
|
$_bind_T[$k] = $v;
|
||||||
|
} else {
|
||||||
|
$_bind_F[$k] = $v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$sql = get_bind_to_sql_select($tablename, $_bind_T, array(
|
||||||
|
"getcnt" => true
|
||||||
|
));
|
||||||
|
$rows = exec_db_fetch_all($sql, $du_bind);
|
||||||
|
foreach($rows as $row) {
|
||||||
|
$num_duplicates = intval($row['cnt']);
|
||||||
|
}
|
||||||
|
|
||||||
// make SQL statement
|
// make SQL statement
|
||||||
|
if($num_duplicates > 0) {
|
||||||
|
$sql = get_bind_to_sql_update($tablename, $bind, array(
|
||||||
|
"setkeys" => array_keys($_bind_F)
|
||||||
|
), $options);
|
||||||
|
} else {
|
||||||
$bind_keys = array_keys($bind);
|
$bind_keys = array_keys($bind);
|
||||||
$sql = "insert into `%s` (%s) values (:%s)";
|
|
||||||
|
|
||||||
$s1 = $tablename;
|
$s1 = $tablename;
|
||||||
$s2 = sprintf("`%s`", implode("`, `", $bind_keys));
|
$s2 = sprintf("`%s`", implode("`, `", $bind_keys));
|
||||||
$s3 = implode(", :", $bind_keys);
|
$s3 = implode(", :", $bind_keys);
|
||||||
|
|
||||||
$sql = sprintf($sql, $s1, $s2, $s3);
|
$sql = sprintf($sql, $s1, $s2, $s3);
|
||||||
|
}
|
||||||
|
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|
@ -462,18 +475,18 @@ if(!check_function_exists("get_bind_to_sql_where")) {
|
||||||
|
|
||||||
if(!check_function_exists("get_bind_to_sql_update_set")) {
|
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
|
// 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()) {
|
function get_bind_to_sql_update_set($bind, $excludes=array(), $options=array()) {
|
||||||
$sql_update_set = "";
|
$sql = "";
|
||||||
$set_items = "";
|
$sps = array();
|
||||||
|
|
||||||
foreach($bind as $k=>$v) {
|
foreach($bind as $k=>$v) {
|
||||||
if(!in_array($k, $excludes)) {
|
if(!in_array($k, $excludes)) {
|
||||||
$set_items[] = sprintf("%s = :%s", $k, $k);
|
$sps[] = sprintf("%s = :%s", $k, $k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$sql_update_set = implode(", ", $set_items);
|
$sql = implode(", ", $sps);
|
||||||
|
|
||||||
return $sql_update_set;
|
return $sql;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -653,46 +666,33 @@ if(!check_function_exists("get_bind_to_sql_select")) {
|
||||||
|
|
||||||
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(), $_options=array()) {
|
function get_bind_to_sql_update($tablename, $bind, $options=array(), $_options=array()) {
|
||||||
$excludes = array();
|
$sql = "update %s set %s where %s";
|
||||||
$_bind = array();
|
|
||||||
|
|
||||||
// compatible version 1.5
|
// bind `where` clause
|
||||||
if(get_old_version() == "1.5") {
|
$_bind_T = array();
|
||||||
foreach($options as $k=>$v) {
|
$_bind_F = array();
|
||||||
if($v == true) {
|
|
||||||
$excludes[] = $k;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$options = $_options;
|
|
||||||
}
|
|
||||||
|
|
||||||
// setkeys
|
// setkeys
|
||||||
if(!array_key_empty("setkeys", $options)) {
|
$setkeys = get_array(get_value_in_array("setkeys", $options, false));
|
||||||
$setkeys = $options['setkeys'];
|
|
||||||
foreach($bind as $k=>$v) {
|
foreach($bind as $k=>$v) {
|
||||||
if(in_array($k, $setkeys)) {
|
if(in_array($k, $setkeys)) {
|
||||||
$_bind[$k] = $v;
|
$_bind_T[$k] => $v;
|
||||||
$excludes[] = $k;
|
} else {
|
||||||
}
|
$_bind_F[$k] => $v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add excludes to options
|
// s1: make `tablename` clause
|
||||||
if(!array_key_exists("excludes", $options)) {
|
$s1 = $tablename;
|
||||||
$options['excludes'] = array();
|
|
||||||
}
|
|
||||||
foreach($excludes as $k=>$v) {
|
|
||||||
$options['excludes'][$k] = $v;
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sql 'where' clause
|
// s2: make 'update set' clause
|
||||||
$sql_where = get_db_binded_sql(get_bind_to_sql_where($_bind, $options), $_bind);
|
$s2 = get_bind_to_sql_update_set($bind, array_keys($_bind_F), $options);
|
||||||
|
|
||||||
// make sql 'update set' clause
|
// s3: make 'where' clause
|
||||||
$sql_update_set = get_bind_to_sql_update_set($bind, $excludes);
|
$s3 = get_bind_to_sql_where($_bind_T, $options);
|
||||||
|
|
||||||
// make completed sql statement
|
// make completed sql statement
|
||||||
$sql = sprintf("update %s set %s where %s", $tablename, $sql_update_set, $sql_where);
|
$sql = get_db_binded_sql(sprintf("update %s set %s where %s", $s1, $s2, $s3), $bind);
|
||||||
|
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user