2011-03-01 Miguel de Dios <miguel.dedios@artica.es>
* include/db/postgresql.php, include/db/mysql.php, include/functions_db.php: separate the function "get_db_value_sql", "get_db_row_sql" and "get_db_row_filter" into the two version for MySQL and PostgreSQL engine. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4034 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
parent
c24ffdac4b
commit
848dee5138
|
@ -1,3 +1,9 @@
|
||||||
|
2011-03-01 Miguel de Dios <miguel.dedios@artica.es>
|
||||||
|
|
||||||
|
* include/db/postgresql.php, include/db/mysql.php, include/functions_db.php:
|
||||||
|
separate the function "get_db_value_sql", "get_db_row_sql" and
|
||||||
|
"get_db_row_filter" into the two version for MySQL and PostgreSQL engine.
|
||||||
|
|
||||||
2011-03-01 Miguel de Dios <miguel.dedios@artica.es>
|
2011-03-01 Miguel de Dios <miguel.dedios@artica.es>
|
||||||
* include/db/postgresql.php, include/db/mysql.php, include/functions_db.php:
|
* include/db/postgresql.php, include/db/mysql.php, include/functions_db.php:
|
||||||
separate the function "get_db_value_filter" and
|
separate the function "get_db_value_filter" and
|
||||||
|
|
|
@ -548,4 +548,87 @@ function mysql_format_array_to_where_clause_sql ($values, $join = 'AND', $prefix
|
||||||
|
|
||||||
return (! empty ($query) ? $prefix: '').$query.$group.$order.$limit.$offset;
|
return (! empty ($query) ? $prefix: '').$query.$group.$order.$limit.$offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first value of the first row of a table result from query.
|
||||||
|
*
|
||||||
|
* @param string SQL select statement to execute.
|
||||||
|
*
|
||||||
|
* @return the first value of the first row of a table result from query.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function mysql_get_db_value_sql($sql) {
|
||||||
|
$sql .= " LIMIT 1";
|
||||||
|
$result = get_db_all_rows_sql ($sql);
|
||||||
|
|
||||||
|
if($result === false)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
foreach ($result[0] as $f)
|
||||||
|
return $f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first row of an SQL database query.
|
||||||
|
*
|
||||||
|
* @param string SQL select statement to execute.
|
||||||
|
*
|
||||||
|
* @return mixed The first row of the result or false
|
||||||
|
*/
|
||||||
|
function mysql_get_db_row_sql ($sql, $search_history_db = false) {
|
||||||
|
$sql .= " LIMIT 1";
|
||||||
|
$result = get_db_all_rows_sql ($sql, $search_history_db);
|
||||||
|
|
||||||
|
if($result === false)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return $result[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the row of a table in the database using a complex filter.
|
||||||
|
*
|
||||||
|
* @param string Table to retrieve the data (warning: not cleaned)
|
||||||
|
* @param mixed Filters elements. It can be an indexed array
|
||||||
|
* (keys would be the field name and value the expected value, and would be
|
||||||
|
* joined with an AND operator) or a string, including any SQL clause (without
|
||||||
|
* the WHERE keyword). Example:
|
||||||
|
<code>
|
||||||
|
Both are similars:
|
||||||
|
get_db_row_filter ('table', array ('disabled', 0));
|
||||||
|
get_db_row_filter ('table', 'disabled = 0');
|
||||||
|
|
||||||
|
Both are similars:
|
||||||
|
get_db_row_filter ('table', array ('disabled' => 0, 'history_data' => 0), 'name, description', 'OR');
|
||||||
|
get_db_row_filter ('table', 'disabled = 0 OR history_data = 0', 'name, description');
|
||||||
|
get_db_row_filter ('table', array ('disabled' => 0, 'history_data' => 0), array ('name', 'description'), 'OR');
|
||||||
|
</code>
|
||||||
|
* @param mixed Fields of the table to retrieve. Can be an array or a coma
|
||||||
|
* separated string. All fields are retrieved by default
|
||||||
|
* @param string Condition to join the filters (AND, OR).
|
||||||
|
*
|
||||||
|
* @return mixed Array of the row or false in case of error.
|
||||||
|
*/
|
||||||
|
function mysql_get_db_row_filter ($table, $filter, $fields = false, $where_join = 'AND') {
|
||||||
|
if (empty ($fields)) {
|
||||||
|
$fields = '*';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (is_array ($fields))
|
||||||
|
$fields = implode (',', $fields);
|
||||||
|
else if (! is_string ($fields))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array ($filter))
|
||||||
|
$filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE ');
|
||||||
|
else if (is_string ($filter))
|
||||||
|
$filter = 'WHERE '.$filter;
|
||||||
|
else
|
||||||
|
$filter = '';
|
||||||
|
|
||||||
|
$sql = sprintf ('SELECT %s FROM %s %s', $fields, $table, $filter);
|
||||||
|
|
||||||
|
return get_db_row_sql ($sql);
|
||||||
|
}
|
||||||
?>
|
?>
|
|
@ -567,4 +567,87 @@ function postgresql_format_array_to_where_clause_sql ($values, $join = 'AND', $p
|
||||||
|
|
||||||
return (! empty ($query) ? $prefix: '').$query.$group.$order.$limit.$offset;
|
return (! empty ($query) ? $prefix: '').$query.$group.$order.$limit.$offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first value of the first row of a table result from query.
|
||||||
|
*
|
||||||
|
* @param string SQL select statement to execute.
|
||||||
|
*
|
||||||
|
* @return the first value of the first row of a table result from query.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function postgresql_get_db_value_sql($sql) {
|
||||||
|
$sql .= " LIMIT 1";
|
||||||
|
$result = get_db_all_rows_sql ($sql);
|
||||||
|
|
||||||
|
if($result === false)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
foreach ($result[0] as $f)
|
||||||
|
return $f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first row of an SQL database query.
|
||||||
|
*
|
||||||
|
* @param string SQL select statement to execute.
|
||||||
|
*
|
||||||
|
* @return mixed The first row of the result or false
|
||||||
|
*/
|
||||||
|
function postgresql_get_db_row_sql ($sql, $search_history_db = false) {
|
||||||
|
$sql .= " LIMIT 1";
|
||||||
|
$result = get_db_all_rows_sql ($sql, $search_history_db);
|
||||||
|
|
||||||
|
if($result === false)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return $result[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the row of a table in the database using a complex filter.
|
||||||
|
*
|
||||||
|
* @param string Table to retrieve the data (warning: not cleaned)
|
||||||
|
* @param mixed Filters elements. It can be an indexed array
|
||||||
|
* (keys would be the field name and value the expected value, and would be
|
||||||
|
* joined with an AND operator) or a string, including any SQL clause (without
|
||||||
|
* the WHERE keyword). Example:
|
||||||
|
<code>
|
||||||
|
Both are similars:
|
||||||
|
get_db_row_filter ('table', array ('disabled', 0));
|
||||||
|
get_db_row_filter ('table', 'disabled = 0');
|
||||||
|
|
||||||
|
Both are similars:
|
||||||
|
get_db_row_filter ('table', array ('disabled' => 0, 'history_data' => 0), 'name, description', 'OR');
|
||||||
|
get_db_row_filter ('table', 'disabled = 0 OR history_data = 0', 'name, description');
|
||||||
|
get_db_row_filter ('table', array ('disabled' => 0, 'history_data' => 0), array ('name', 'description'), 'OR');
|
||||||
|
</code>
|
||||||
|
* @param mixed Fields of the table to retrieve. Can be an array or a coma
|
||||||
|
* separated string. All fields are retrieved by default
|
||||||
|
* @param string Condition to join the filters (AND, OR).
|
||||||
|
*
|
||||||
|
* @return mixed Array of the row or false in case of error.
|
||||||
|
*/
|
||||||
|
function postgresql_get_db_row_filter ($table, $filter, $fields = false, $where_join = 'AND') {
|
||||||
|
if (empty ($fields)) {
|
||||||
|
$fields = '*';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (is_array ($fields))
|
||||||
|
$fields = implode (',', $fields);
|
||||||
|
else if (! is_string ($fields))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array ($filter))
|
||||||
|
$filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE ');
|
||||||
|
else if (is_string ($filter))
|
||||||
|
$filter = 'WHERE '.$filter;
|
||||||
|
else
|
||||||
|
$filter = '';
|
||||||
|
|
||||||
|
$sql = sprintf ('SELECT %s FROM %s %s', $fields, $table, $filter);
|
||||||
|
|
||||||
|
return get_db_row_sql ($sql);
|
||||||
|
}
|
||||||
?>
|
?>
|
|
@ -2093,15 +2093,17 @@ function get_db_value_filter ($field, $table, $filter, $where_join = 'AND') {
|
||||||
* @return the first value of the first row of a table result from query.
|
* @return the first value of the first row of a table result from query.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function get_db_value_sql ($sql) {
|
function get_db_value_sql($sql) {
|
||||||
$sql .= " LIMIT 1";
|
global $config;
|
||||||
$result = get_db_all_rows_sql ($sql);
|
|
||||||
|
|
||||||
if($result === false)
|
switch ($config["dbtype"]) {
|
||||||
return false;
|
case "mysql":
|
||||||
|
return mysql_get_db_value_sql($sql);
|
||||||
foreach ($result[0] as $f)
|
break;
|
||||||
return $f;
|
case "postgresql":
|
||||||
|
return postgresql_get_db_value_sql($sql);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2111,14 +2113,17 @@ function get_db_value_sql ($sql) {
|
||||||
*
|
*
|
||||||
* @return mixed The first row of the result or false
|
* @return mixed The first row of the result or false
|
||||||
*/
|
*/
|
||||||
function get_db_row_sql ($sql, $search_history_db = false) {
|
function get_db_row_sql($sql, $search_history_db = false) {
|
||||||
$sql .= " LIMIT 1";
|
global $config;
|
||||||
$result = get_db_all_rows_sql ($sql, $search_history_db);
|
|
||||||
|
|
||||||
if($result === false)
|
switch ($config["dbtype"]) {
|
||||||
return false;
|
case "mysql":
|
||||||
|
return mysql_get_db_row_sql($sql, $search_history_db);
|
||||||
return $result[0];
|
break;
|
||||||
|
case "postgresql":
|
||||||
|
return postgresql_get_db_row_sql($sql, $search_history_db);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2171,27 +2176,17 @@ function get_db_row ($table, $field_search, $condition, $fields = false) {
|
||||||
*
|
*
|
||||||
* @return mixed Array of the row or false in case of error.
|
* @return mixed Array of the row or false in case of error.
|
||||||
*/
|
*/
|
||||||
function get_db_row_filter ($table, $filter, $fields = false, $where_join = 'AND') {
|
function get_db_row_filter($table, $filter, $fields = false, $where_join = 'AND') {
|
||||||
if (empty ($fields)) {
|
global $config;
|
||||||
$fields = '*';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (is_array ($fields))
|
|
||||||
$fields = implode (',', $fields);
|
|
||||||
else if (! is_string ($fields))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_array ($filter))
|
switch ($config["dbtype"]) {
|
||||||
$filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE ');
|
case "mysql":
|
||||||
else if (is_string ($filter))
|
return mysql_get_db_row_filter($table, $filter, $fields, $where_join);
|
||||||
$filter = 'WHERE '.$filter;
|
break;
|
||||||
else
|
case "postgresql":
|
||||||
$filter = '';
|
return postgresql_get_db_row_filter($table, $filter, $fields, $where_join);
|
||||||
$sql = sprintf ('SELECT %s FROM %s %s',
|
break;
|
||||||
$fields, $table, $filter);
|
}
|
||||||
|
|
||||||
return get_db_row_sql ($sql);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue