aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorplegall <plg@piwigo.org>2011-08-24 19:50:54 +0000
committerplegall <plg@piwigo.org>2011-08-24 19:50:54 +0000
commitc1d7fbebdde17a90f8cb42e7a4c3fa551dcb6b79 (patch)
tree64b3882e2a586b50812af9dd033e1c35fd76537a /include
parente780496fbe18e8444be8793c52e891a454525143 (diff)
feature 2359: simpler function single_update and single_insert
git-svn-id: http://piwigo.org/svn/trunk@11991 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'include')
-rw-r--r--include/dblayer/functions_mysql.inc.php44
-rw-r--r--include/dblayer/functions_pdo-sqlite.inc.php47
-rw-r--r--include/dblayer/functions_pgsql.inc.php48
-rw-r--r--include/dblayer/functions_sqlite.inc.php47
4 files changed, 108 insertions, 78 deletions
diff --git a/include/dblayer/functions_mysql.inc.php b/include/dblayer/functions_mysql.inc.php
index de63c0cac..e74cddb0d 100644
--- a/include/dblayer/functions_mysql.inc.php
+++ b/include/dblayer/functions_mysql.inc.php
@@ -350,27 +350,29 @@ DROP TABLE '.$temporary_tablename;
* updates one line in a table
*
* @param string table_name
- * @param array dbfields
- * @param array data
+ * @param array set_fields
+ * @param array where_fields
* @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
* @return void
*/
-function single_update($tablename, $dbfields, $data, $flags=0)
+function single_update($tablename, $set_fields, $where_fields, $flags=0)
{
- if (count($data) == 0)
+ if (count($set_fields) == 0)
+ {
return;
+ }
$query = '
UPDATE '.$tablename.'
SET ';
$is_first = true;
- foreach ($dbfields['update'] as $key)
+ foreach ($set_fields as $key => $value)
{
$separator = $is_first ? '' : ",\n ";
- if (isset($data[$key]) and $data[$key] != '')
+ if (isset($value) and $value != '')
{
- $query.= $separator.$key.' = \''.$data[$key].'\'';
+ $query.= $separator.$key.' = \''.$value.'\'';
}
else
{
@@ -385,15 +387,15 @@ UPDATE '.$tablename.'
$query.= '
WHERE ';
$is_first = true;
- foreach ($dbfields['primary'] as $key)
+ foreach ($where_fields as $key => $value)
{
if (!$is_first)
{
$query.= ' AND ';
}
- if ( isset($data[$key]) )
+ if ( isset($value) )
{
- $query.= $key.' = \''.$data[$key].'\'';
+ $query.= $key.' = \''.$value.'\'';
}
else
{
@@ -471,36 +473,42 @@ INSERT INTO '.$table_name.'
}
/**
- * inserts on line in a table
+ * inserts one line in a table
*
* @param string table_name
* @param array dbfields
* @param array insert
* @return void
*/
-function single_insert($table_name, $dbfields, $insert)
+function single_insert($table_name, $data)
{
- if (count($insert) != 0)
+ if (count($data) != 0)
{
$query = '
INSERT INTO '.$table_name.'
- ('.implode(',', $dbfields).')
+ ('.implode(',', array_keys($data)).')
VALUES';
$query .= '(';
- foreach ($dbfields as $field_id => $dbfield)
+ $is_first = true;
+ foreach ($data as $key => $value)
{
- if ($field_id > 0)
+ if (!$is_first)
{
$query .= ',';
}
- if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
+ else
+ {
+ $is_first = false;
+ }
+
+ if ($value === '')
{
$query .= 'NULL';
}
else
{
- $query .= "'".$insert[$dbfield]."'";
+ $query .= "'".$value."'";
}
}
$query .= ')';
diff --git a/include/dblayer/functions_pdo-sqlite.inc.php b/include/dblayer/functions_pdo-sqlite.inc.php
index 22751d04a..a5b4af600 100644
--- a/include/dblayer/functions_pdo-sqlite.inc.php
+++ b/include/dblayer/functions_pdo-sqlite.inc.php
@@ -315,30 +315,32 @@ UPDATE '.$tablename.'
}
/**
- * updates on line in a table
+ * updates one line in a table
*
* @param string table_name
- * @param array dbfields
- * @param array data
+ * @param array set_fields
+ * @param array where_fields
* @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
* @return void
*/
-function single_update($tablename, $dbfields, $data, $flags=0)
+function single_update($tablename, $set_fields, $where_fields, $flags=0)
{
- if (count($data) == 0)
+ if (count($set_fields) == 0)
+ {
return;
+ }
$query = '
UPDATE '.$tablename.'
SET ';
$is_first = true;
- foreach ($dbfields['update'] as $key)
+ foreach ($set_fields as $key => $value)
{
$separator = $is_first ? '' : ",\n ";
-
- if (isset($data[$key]) and $data[$key] != '')
+
+ if (isset($value) and $value != '')
{
- $query.= $separator.$key.' = \''.$data[$key].'\'';
+ $query.= $separator.$key.' = \''.$value.'\'';
}
else
{
@@ -353,15 +355,15 @@ UPDATE '.$tablename.'
$query.= '
WHERE ';
$is_first = true;
- foreach ($dbfields['primary'] as $key)
+ foreach ($where_fields as $key => $value)
{
if (!$is_first)
{
$query.= ' AND ';
}
- if ( isset($data[$key]) )
+ if ( isset($value) )
{
- $query.= $key.' = \''.$data[$key].'\'';
+ $query.= $key.' = \''.$value.'\'';
}
else
{
@@ -373,7 +375,6 @@ UPDATE '.$tablename.'
}
}
-
/**
* inserts multiple lines in a table
*
@@ -445,29 +446,35 @@ INSERT INTO '.$table_name.'
* @param array insert
* @return void
*/
-function single_insert($table_name, $dbfields, $insert)
+function single_insert($table_name, $data)
{
- if (count($insert) != 0)
+ if (count($data) != 0)
{
$query = '
INSERT INTO '.$table_name.'
- ('.implode(',', $dbfields).')
+ ('.implode(',', array_keys($data)).')
VALUES';
$query .= '(';
- foreach ($dbfields as $field_id => $dbfield)
+ $is_first = true;
+ foreach ($data as $key => $value)
{
- if ($field_id > 0)
+ if (!$is_first)
{
$query .= ',';
}
- if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
+ else
+ {
+ $is_first = false;
+ }
+
+ if ($value === '')
{
$query .= 'NULL';
}
else
{
- $query .= "'".$insert[$dbfield]."'";
+ $query .= "'".$value."'";
}
}
$query .= ')';
diff --git a/include/dblayer/functions_pgsql.inc.php b/include/dblayer/functions_pgsql.inc.php
index c8a8dedbd..cfd39a1bc 100644
--- a/include/dblayer/functions_pgsql.inc.php
+++ b/include/dblayer/functions_pgsql.inc.php
@@ -426,30 +426,32 @@ DROP TABLE '.$temporary_tablename;
}
/**
- * updates on line in a table
+ * updates one line in a table
*
* @param string table_name
- * @param array dbfields
- * @param array data
+ * @param array set_fields
+ * @param array where_fields
* @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
* @return void
*/
-function single_update($tablename, $dbfields, $data, $flags=0)
+function single_update($tablename, $set_fields, $where_fields, $flags=0)
{
- if (count($data) == 0)
+ if (count($set_fields) == 0)
+ {
return;
+ }
$query = '
UPDATE '.$tablename.'
SET ';
$is_first = true;
- foreach ($dbfields['update'] as $key)
+ foreach ($set_fields as $key => $value)
{
$separator = $is_first ? '' : ",\n ";
- if (isset($data[$key]) and $data[$key] != '')
+ if (isset($value) and $value != '')
{
- $query.= $separator.$key.' = \''.$data[$key].'\'';
+ $query.= $separator.$key.' = \''.$value.'\'';
}
else
{
@@ -464,15 +466,15 @@ UPDATE '.$tablename.'
$query.= '
WHERE ';
$is_first = true;
- foreach ($dbfields['primary'] as $key)
+ foreach ($where_fields as $key => $value)
{
if (!$is_first)
{
$query.= ' AND ';
}
- if ( isset($data[$key]) )
+ if ( isset($value) )
{
- $query.= $key.' = \''.$data[$key].'\'';
+ $query.= $key.' = \''.$value.'\'';
}
else
{
@@ -481,7 +483,7 @@ UPDATE '.$tablename.'
$is_first = false;
}
pwg_query($query);
- }
+ }
}
/**
@@ -548,36 +550,42 @@ INSERT INTO '.$table_name.'
}
/**
- * inserts on line in a table
+ * inserts one line in a table
*
* @param string table_name
* @param array dbfields
* @param array insert
* @return void
*/
-function single_insert($table_name, $dbfields, $insert)
+function single_insert($table_name, $data)
{
- if (count($insert) != 0)
+ if (count($data) != 0)
{
$query = '
INSERT INTO '.$table_name.'
- ('.implode(',', $dbfields).')
+ ('.implode(',', array_keys($data)).')
VALUES';
$query .= '(';
- foreach ($dbfields as $field_id => $dbfield)
+ $is_first = true;
+ foreach ($data as $key => $value)
{
- if ($field_id > 0)
+ if (!$is_first)
{
$query .= ',';
}
- if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
+ else
+ {
+ $is_first = false;
+ }
+
+ if ($value === '')
{
$query .= 'NULL';
}
else
{
- $query .= "'".$insert[$dbfield]."'";
+ $query .= "'".$value."'";
}
}
$query .= ')';
diff --git a/include/dblayer/functions_sqlite.inc.php b/include/dblayer/functions_sqlite.inc.php
index 5f7d14d9e..b260e7ada 100644
--- a/include/dblayer/functions_sqlite.inc.php
+++ b/include/dblayer/functions_sqlite.inc.php
@@ -327,30 +327,32 @@ UPDATE '.$tablename.'
}
/**
- * updates on line in a table
+ * updates one line in a table
*
* @param string table_name
- * @param array dbfields
- * @param array data
+ * @param array set_fields
+ * @param array where_fields
* @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
* @return void
*/
-function single_update($tablename, $dbfields, $data, $flags=0)
+function single_update($tablename, $set_fields, $where_fields, $flags=0)
{
- if (count($data) == 0)
+ if (count($set_fields) == 0)
+ {
return;
+ }
$query = '
UPDATE '.$tablename.'
SET ';
$is_first = true;
- foreach ($dbfields['update'] as $key)
+ foreach ($set_fields as $key => $value)
{
$separator = $is_first ? '' : ",\n ";
-
- if (isset($data[$key]) and $data[$key] != '')
+
+ if (isset($value) and $value != '')
{
- $query.= $separator.$key.' = \''.$data[$key].'\'';
+ $query.= $separator.$key.' = \''.$value.'\'';
}
else
{
@@ -365,15 +367,15 @@ UPDATE '.$tablename.'
$query.= '
WHERE ';
$is_first = true;
- foreach ($dbfields['primary'] as $key)
+ foreach ($where_fields as $key => $value)
{
if (!$is_first)
{
$query.= ' AND ';
}
- if ( isset($data[$key]) )
+ if ( isset($value) )
{
- $query.= $key.' = \''.$data[$key].'\'';
+ $query.= $key.' = \''.$value.'\'';
}
else
{
@@ -385,7 +387,6 @@ UPDATE '.$tablename.'
}
}
-
/**
* inserts multiple lines in a table
*
@@ -457,29 +458,35 @@ INSERT INTO '.$table_name.'
* @param array insert
* @return void
*/
-function single_insert($table_name, $dbfields, $insert)
+function single_insert($table_name, $data)
{
- if (count($insert) != 0)
+ if (count($data) != 0)
{
$query = '
INSERT INTO '.$table_name.'
- ('.implode(',', $dbfields).')
+ ('.implode(',', array_keys($data)).')
VALUES';
$query .= '(';
- foreach ($dbfields as $field_id => $dbfield)
+ $is_first = true;
+ foreach ($data as $key => $value)
{
- if ($field_id > 0)
+ if (!$is_first)
{
$query .= ',';
}
- if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
+ else
+ {
+ $is_first = false;
+ }
+
+ if ($value === '')
{
$query .= 'NULL';
}
else
{
- $query .= "'".$insert[$dbfield]."'";
+ $query .= "'".$value."'";
}
}
$query .= ')';