From fa9d3363bd592837db086c56fdefc5e17ab04cdc Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Tue, 1 Mar 2011 14:20:42 +0000 Subject: [PATCH] 2011-03-01 Miguel de Dios * include/db/postgresql.php, include/db/mysql.php, include/functions_db.php: separate the function "get_db_all_rows_filter", "get_db_num_rows", "get_db_all_rows_field_filter" and "get_db_all_fields_in_table" into the two version for MySQL and PostgreSQL engine. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4035 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 7 ++ pandora_console/include/db/mysql.php | 115 ++++++++++++++++++++++ pandora_console/include/db/postgresql.php | 115 ++++++++++++++++++++++ pandora_console/include/functions_db.php | 95 ++++++++---------- 4 files changed, 278 insertions(+), 54 deletions(-) diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index d4def6fc0b..db00fa3a1a 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,10 @@ +2011-03-01 Miguel de Dios + + * include/db/postgresql.php, include/db/mysql.php, include/functions_db.php: + separate the function "get_db_all_rows_filter", "get_db_num_rows", + "get_db_all_rows_field_filter" and "get_db_all_fields_in_table" into the two + version for MySQL and PostgreSQL engine. + 2011-03-01 Miguel de Dios * include/db/postgresql.php, include/db/mysql.php, include/functions_db.php: diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index 180064a2a6..05b91e058c 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -631,4 +631,119 @@ function mysql_get_db_row_filter ($table, $filter, $fields = false, $where_join return get_db_row_sql ($sql); } + +/** + * Get all the rows of a table in the database that matches a 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: + * + * Both are similars: + * get_db_all_rows_filter ('table', array ('disabled', 0)); + * get_db_all_rows_filter ('table', 'disabled = 0'); + * + * Both are similars: + * get_db_all_rows_filter ('table', array ('disabled' => 0, 'history_data' => 0), 'name', 'OR'); + * get_db_all_rows_filter ('table', 'disabled = 0 OR history_data = 0', 'name'); + * + * @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 of the filter (AND, OR). + * @param bool $returnSQL Return a string with SQL instead the data, by default false. + * + * @return mixed Array of the row or false in case of error. + */ +function mysql_get_db_all_rows_filter ($table, $filter = array(), $fields = false, $where_join = 'AND', $search_history_db = false, $returnSQL = false) { + //TODO: Validate and clean fields + if (empty ($fields)) { + $fields = '*'; + } + elseif (is_array ($fields)) { + $fields = implode (',', $fields); + } + elseif (! is_string ($fields)) { + return false; + } + + //TODO: Validate and clean filter options + if (is_array ($filter)) { + $filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE '); + } + elseif (is_string ($filter)) { + $filter = 'WHERE '.$filter; + } + else { + $filter = ''; + } + + $sql = sprintf ('SELECT %s FROM %s %s', $fields, $table, $filter); + + if ($returnSQL) + return $sql; + else + return get_db_all_rows_sql ($sql, $search_history_db); +} + +/** + * Return the count of rows of query. + * + * @param $sql + * @return integer The count of rows of query. + */ +function mysql_get_db_num_rows ($sql) { + $result = mysql_query($sql); + + return mysql_num_rows($result); +} + +/** + * Get all the rows in a table of the databes filtering from a field. + * + * @param string Database table name. + * @param string Field of the table. + * @param string Condition the field must have to be selected. + * @param string Field to order by. + * + * @return mixed A matrix with all the values in the table that matches the condition in the field or false + */ +function mysql_get_db_all_rows_field_filter ($table, $field, $condition, $order_field = "") { + if (is_int ($condition) || is_bool ($condition)) { + $sql = sprintf ("SELECT * FROM `%s` WHERE `%s` = %d", $table, $field, $condition); + } + else if (is_float ($condition) || is_double ($condition)) { + $sql = sprintf ("SELECT * FROM `%s` WHERE `%s` = %f", $table, $field, $condition); + } + else { + $sql = sprintf ("SELECT * FROM `%s` WHERE `%s` = '%s'", $table, $field, $condition); + } + + if ($order_field != "") + $sql .= sprintf (" ORDER BY %s", $order_field); + + return get_db_all_rows_sql ($sql); +} + +/** + * Get all the rows in a table of the databes filtering from a field. + * + * @param string Database table name. + * @param string Field of the table. + * + * @return mixed A matrix with all the values in the table that matches the condition in the field + */ +function mysql_get_db_all_fields_in_table ($table, $field = '', $condition = '', $order_field = '') { + $sql = sprintf ("SELECT * FROM `%s`", $table); + + if ($condition != '') { + $sql .= sprintf (" WHERE `%s` = '%s'", $field, $condition); + } + + if ($order_field != "") + $sql .= sprintf (" ORDER BY %s", $order_field); + + return get_db_all_rows_sql ($sql); +} ?> \ No newline at end of file diff --git a/pandora_console/include/db/postgresql.php b/pandora_console/include/db/postgresql.php index 9adc76b8c6..b0d7606025 100644 --- a/pandora_console/include/db/postgresql.php +++ b/pandora_console/include/db/postgresql.php @@ -650,4 +650,119 @@ function postgresql_get_db_row_filter ($table, $filter, $fields = false, $where_ return get_db_row_sql ($sql); } + +/** + * Get all the rows of a table in the database that matches a 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: + * + * Both are similars: + * get_db_all_rows_filter ('table', array ('disabled', 0)); + * get_db_all_rows_filter ('table', 'disabled = 0'); + * + * Both are similars: + * get_db_all_rows_filter ('table', array ('disabled' => 0, 'history_data' => 0), 'name', 'OR'); + * get_db_all_rows_filter ('table', 'disabled = 0 OR history_data = 0', 'name'); + * + * @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 of the filter (AND, OR). + * @param bool $returnSQL Return a string with SQL instead the data, by default false. + * + * @return mixed Array of the row or false in case of error. + */ +function postgresql_get_db_all_rows_filter ($table, $filter = array(), $fields = false, $where_join = 'AND', $search_history_db = false, $returnSQL = false) { + //TODO: Validate and clean fields + if (empty($fields)) { + $fields = '*'; + } + elseif (is_array($fields)) { + $fields = '"' . implode('" , "', $fields) . '"'; + } + elseif (!is_string($fields)) { + return false; + } + + //TODO: Validate and clean filter options + if (is_array ($filter)) { + $filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE '); + } + elseif (is_string ($filter)) { + $filter = 'WHERE '.$filter; + } + else { + $filter = ''; + } + + $sql = sprintf ('SELECT %s FROM "%s" %s', $fields, $table, $filter); + + if ($returnSQL) + return $sql; + else + return get_db_all_rows_sql ($sql, $search_history_db); +} + +/** + * Return the count of rows of query. + * + * @param $sql + * @return integer The count of rows of query. + */ +function postgresql_get_db_num_rows ($sql) { + $result = pg_query($sql); + + return pg_num_rows($result); +} + +/** + * Get all the rows in a table of the databes filtering from a field. + * + * @param string Database table name. + * @param string Field of the table. + * @param string Condition the field must have to be selected. + * @param string Field to order by. + * + * @return mixed A matrix with all the values in the table that matches the condition in the field or false + */ +function postgresql_get_db_all_rows_field_filter ($table, $field, $condition, $order_field = "") { + if (is_int ($condition) || is_bool ($condition)) { + $sql = sprintf ("SELECT * FROM \"%s\" WHERE \"%s\" = %d", $table, $field, $condition); + } + else if (is_float ($condition) || is_double ($condition)) { + $sql = sprintf ("SELECT * FROM \"%s\" WHERE \"%s\" = %f", $table, $field, $condition); + } + else { + $sql = sprintf ("SELECT * FROM \"%s\" WHERE \"%s\" = '%s'", $table, $field, $condition); + } + + if ($order_field != "") + $sql .= sprintf (" ORDER BY %s", $order_field); + + return get_db_all_rows_sql ($sql); +} + +/** + * Get all the rows in a table of the databes filtering from a field. + * + * @param string Database table name. + * @param string Field of the table. + * + * @return mixed A matrix with all the values in the table that matches the condition in the field + */ +function postgresql_get_db_all_fields_in_table ($table, $field = '', $condition = '', $order_field = '') { + $sql = sprintf ("SELECT * FROM \"%s\"", $table); + + if ($condition != '') { + $sql .= sprintf (" WHERE \"%s\" = '%s'", $field, $condition); + } + + if ($order_field != "") + $sql .= sprintf (" ORDER BY \"%s\"", $order_field); + + return get_db_all_rows_sql ($sql); +} ?> \ No newline at end of file diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index f50134c4c9..0649cfe51d 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -2259,34 +2259,17 @@ function get_db_all_rows_sql($sql, $search_history_db = false, $cache = true) { * * @return mixed Array of the row or false in case of error. */ -function get_db_all_rows_filter ($table, $filter = array(), $fields = false, $where_join = 'AND', $search_history_db = false, $returnSQL = false) { - //TODO: Validate and clean fields - if (empty ($fields)) { - $fields = '*'; - } - elseif (is_array ($fields)) { - $fields = implode (',', $fields); - } - elseif (! is_string ($fields)) { - return false; - } +function get_db_all_rows_filter($table, $filter = array(), $fields = false, $where_join = 'AND', $search_history_db = false, $returnSQL = false) { + global $config; - //TODO: Validate and clean filter options - if (is_array ($filter)) { - $filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE '); + switch ($config["dbtype"]) { + case "mysql": + return mysql_get_db_all_rows_filter($table, $filter, $fields, $where_join, $search_history_db, $returnSQL); + break; + case "postgresql": + return postgresql_get_db_all_rows_sql($table, $filter, $fields, $where_join, $search_history_db, $returnSQL); + break; } - elseif (is_string ($filter)) { - $filter = 'WHERE '.$filter; - } - else { - $filter = ''; - } - - $sql = sprintf ('SELECT %s FROM %s %s', $fields, $table, $filter); - if ($returnSQL) - return $sql; - else - return get_db_all_rows_sql ($sql, $search_history_db); } /** @@ -2312,10 +2295,17 @@ function get_db_all_row_by_steps_sql($new = true, &$result, $sql = null) { * @param $sql * @return integer The count of rows of query. */ -function get_db_num_rows ($sql) { - $result = mysql_query($sql); +function get_db_num_rows($sql) { + global $config; - return mysql_num_rows($result); + switch ($config["dbtype"]) { + case "mysql": + return mysql_get_db_num_rows($sql); + break; + case "postgresql": + return postgresql_get_db_num_rows($sql); + break; + } } /** @@ -2331,10 +2321,10 @@ function sql_error_handler ($errno, $errstr) { /* If debug is activated, this will also show the backtrace */ if (debug ($errstr)) - return false; + return false; if (error_reporting () <= $errno) - return false; + return false; echo "SQL error: ".$errstr."
\n"; @@ -2448,21 +2438,17 @@ function get_db_all_rows_in_table ($table, $order_field = "", $order = 'ASC') { * * @return mixed A matrix with all the values in the table that matches the condition in the field or false */ -function get_db_all_rows_field_filter ($table, $field, $condition, $order_field = "") { - if (is_int ($condition) || is_bool ($condition)) { - $sql = sprintf ("SELECT * FROM `%s` WHERE `%s` = %d", $table, $field, $condition); - } - else if (is_float ($condition) || is_double ($condition)) { - $sql = sprintf ("SELECT * FROM `%s` WHERE `%s` = %f", $table, $field, $condition); - } - else { - $sql = sprintf ("SELECT * FROM `%s` WHERE `%s` = '%s'", $table, $field, $condition); - } +function get_db_all_rows_field_filter($table, $field, $condition, $order_field = "") { + global $config; - if ($order_field != "") - $sql .= sprintf (" ORDER BY %s", $order_field); - - return get_db_all_rows_sql ($sql); + switch ($config["dbtype"]) { + case "mysql": + return mysql_get_db_all_rows_field_filter($table, $field, $condition, $order_field); + break; + case "postgresql": + return postgresql_get_db_all_rows_field_filter($table, $field, $condition, $order_field); + break; + } } /** @@ -2473,16 +2459,17 @@ function get_db_all_rows_field_filter ($table, $field, $condition, $order_field * * @return mixed A matrix with all the values in the table that matches the condition in the field */ -function get_db_all_fields_in_table ($table, $field = '', $condition = '', $order_field = '') { - $sql = sprintf ("SELECT * FROM `%s`", $table); - if ($condition != '') { - $sql .= sprintf (" WHERE `%s` = '%s'", $field, $condition); +function get_db_all_fields_in_table($table, $field = '', $condition = '', $order_field = '') { + global $config; + + switch ($config["dbtype"]) { + case "mysql": + return mysql_get_db_all_fields_in_table($table, $field, $condition, $order_field); + break; + case "postgresql": + return postgresql_get_db_all_fields_in_table($table, $field, $condition, $order_field); + break; } - - if ($order_field != "") - $sql .= sprintf (" ORDER BY %s", $order_field); - - return get_db_all_rows_sql ($sql); } /**