Feature 1255: modification in sql queries

- manage random function
    - manage regex syntax
    - manage quote (single instead of double)
    - manage interval

git-svn-id: http://piwigo.org/svn/trunk@4367 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
nikrou
2009-11-25 19:02:57 +00:00
parent d30639ec98
commit 13ea9d50e3
20 changed files with 134 additions and 112 deletions
+1 -1
View File
@@ -268,7 +268,7 @@ $this->get_date_where($level).'
$prev = $next =null;
if ( empty($page['chronology_date']) )
return;
$query = 'SELECT CONCAT_WS("-"';
$query = 'SELECT CONCAT_WS(\'-\'';
for ($i=0; $i<count($page['chronology_date']); $i++)
{
if ( 'any' === $page['chronology_date'][$i] )
+1 -1
View File
@@ -347,7 +347,7 @@ SELECT id, file,tn_ext,path, width, height, DAYOFWEEK('.$this->date_field.')-1 a
$query.= $this->inner_sql;
$query.= $this->get_date_where();
$query.= '
ORDER BY RAND()
ORDER BY '.DB_RANDOM_FUNCTION.'()
LIMIT 1';
unset ( $page['chronology_date'][CDAY] );
+3 -5
View File
@@ -35,9 +35,7 @@ SELECT
c.*, nb_images, date_last, max_date_last, count_images, count_categories
FROM '.CATEGORIES_TABLE.' c INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
ON id = cat_id and user_id = '.$user['id'].'
WHERE date_last >= SUBDATE(
CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY
)
WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']).'
'.get_sql_condition_FandF
(
array
@@ -104,7 +102,7 @@ SELECT image_id
),
"\n AND"
).'
ORDER BY RAND()
ORDER BY '.DB_RANDOM_FUNCTION.'()
LIMIT 1
;';
$subresult = pwg_query($query);
@@ -132,7 +130,7 @@ SELECT image_id
),
"\n AND"
).'
ORDER BY RAND()
ORDER BY '.DB_RANDOM_FUNCTION.'()
LIMIT 1
;';
$subresult = pwg_query($query);
+95
View File
@@ -21,6 +21,11 @@
// | USA. |
// +-----------------------------------------------------------------------+
define('DB_ENGINE', 'MySQL');
define('DB_REGEX_OPERATOR', 'REGEXP');
define('DB_RANDOM_FUNCTION', 'RAND');
/**
*
* simple functions
@@ -107,6 +112,16 @@ function pwg_query($query)
return $result;
}
function pwg_db_nextval($column, $table)
{
$query = '
SELECT IF(MAX('.$column.')+1 IS NULL, 1, MAX('.$column.')+1)
FROM '.$table;
list($next) = pwg_db_fetch_row(pwg_query($query));
return $next;
}
function pwg_db_changes($result)
{
return mysql_affected_rows($result);
@@ -443,6 +458,86 @@ function do_maintenance_all_tables()
}
}
function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE')
{
if ($date!='CURRENT_DATE')
{
$date = '\''.$date.'\'';
}
return 'SUBDATE('.$date.',INTERVAL '.$period.' DAY)';
}
function pwg_db_get_recent_period($period, $date='CURRENT_DATE')
{
$query = '
SELECT '.pwg_db_get_recent_period_expression($period);
list($d) = pwg_db_fetch_row(pwg_query($query));
return $d;
}
/**
* returns an array containing the possible values of an enum field
*
* @param string tablename
* @param string fieldname
*/
function get_enums($table, $field)
{
// retrieving the properties of the table. Each line represents a field :
// columns are 'Field', 'Type'
$result = pwg_query('desc '.$table);
while ($row = pwg_db_fetch_assoc($result))
{
// we are only interested in the the field given in parameter for the
// function
if ($row['Field'] == $field)
{
// retrieving possible values of the enum field
// enum('blue','green','black')
$options = explode(',', substr($row['Type'], 5, -1));
foreach ($options as $i => $option)
{
$options[$i] = str_replace("'", '',$option);
}
}
}
pwg_db_free_result($result);
return $options;
}
// get_boolean transforms a string to a boolean value. If the string is
// "false" (case insensitive), then the boolean value false is returned. In
// any other case, true is returned.
function get_boolean( $string )
{
$boolean = true;
if ( 'false' == strtolower($string) )
{
$boolean = false;
}
return $boolean;
}
/**
* returns boolean string 'true' or 'false' if the given var is boolean
*
* @param mixed $var
* @return mixed
*/
function boolean_to_string($var)
{
if (is_bool($var))
{
return $var ? 'true' : 'false';
}
else
{
return $var;
}
}
// my_error returns (or send to standard output) the message concerning the
// error occured for the last mysql query.
function my_error($header, $die)
+1 -2
View File
@@ -96,8 +96,7 @@ WHERE ';
category_id IN ('.$filter['visible_categories'].') and';
}
$query.= '
date_available >= SUBDATE(
CURRENT_DATE,INTERVAL '.$filter['recent_period'].' DAY)';
date_available >= '.pwg_db_get_recent_period_expression($filter['recent_period']);
$filter['visible_images'] = implode(',', array_from_query($query, 'image_id'));
+3 -66
View File
@@ -33,67 +33,6 @@ include_once( PHPWG_ROOT_PATH .'include/functions_plugins.inc.php' );
//----------------------------------------------------------- generic functions
/**
* returns an array containing the possible values of an enum field
*
* @param string tablename
* @param string fieldname
*/
function get_enums($table, $field)
{
// retrieving the properties of the table. Each line represents a field :
// columns are 'Field', 'Type'
$result = pwg_query('desc '.$table);
while ($row = pwg_db_fetch_assoc($result))
{
// we are only interested in the the field given in parameter for the
// function
if ($row['Field'] == $field)
{
// retrieving possible values of the enum field
// enum('blue','green','black')
$options = explode(',', substr($row['Type'], 5, -1));
foreach ($options as $i => $option)
{
$options[$i] = str_replace("'", '',$option);
}
}
}
pwg_db_free_result($result);
return $options;
}
// get_boolean transforms a string to a boolean value. If the string is
// "false" (case insensitive), then the boolean value false is returned. In
// any other case, true is returned.
function get_boolean( $string )
{
$boolean = true;
if ( 'false' == strtolower($string) )
{
$boolean = false;
}
return $boolean;
}
/**
* returns boolean string 'true' or 'false' if the given var is boolean
*
* @param mixed $var
* @return mixed
*/
function boolean_to_string($var)
{
if (is_bool($var))
{
return $var ? 'true' : 'false';
}
else
{
return $var;
}
}
// The function get_moment returns a float value coresponding to the number
// of seconds since the unix epoch (1st January 1970) and the microseconds
// are precised : e.g. 1052343429.89276600
@@ -540,8 +479,8 @@ INSERT INTO '.HISTORY_TABLE.'
)
VALUES
(
CURDATE(),
CURTIME(),
CURRENT_DATE,
CURRENT_TIME,
'.$user['id'].',
\''.$_SERVER['REMOTE_ADDR'].'\',
'.(isset($page['section']) ? "'".$page['section']."'" : 'NULL').',
@@ -1497,9 +1436,7 @@ function get_icon($date, $is_child_date = false)
if (!isset($cache['get_icon']['sql_recent_date']))
{
// Use MySql date in order to standardize all recent "actions/queries"
list($cache['get_icon']['sql_recent_date']) =
pwg_db_fetch_row(pwg_query('select SUBDATE(
CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)'));
$cache['get_icon']['sql_recent_date'] = pwg_db_get_recent_period($user['recent_period']);
}
$cache['get_icon'][$date] = $date > $cache['get_icon']['sql_recent_date'];
+1 -1
View File
@@ -377,7 +377,7 @@ SELECT DISTINCT(id)
$query.= '
OR ';
}
$query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
$query.= 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'';
}
$query.= '
;';
+1 -1
View File
@@ -463,7 +463,7 @@ SELECT DISTINCT id, path, name, tn_ext, file
'.$where_sql.'
AND date_available="'.$dates[$i]['date_available'].'"
AND tn_ext IS NOT NULL
ORDER BY RAND(NOW())
ORDER BY '.DB_RANDOM_FUNCTION.'())
LIMIT '.$max_elements.'
;';
$dates[$i]['elements'] = array();
+1 -1
View File
@@ -224,7 +224,7 @@ SELECT * FROM '.PLUGINS_TABLE;
$clauses = array();
if (!empty($state))
{
$clauses[] = 'state="'.$state.'"';
$clauses[] = 'state=\''.$state.'\'';
}
if (!empty($id))
{
+4 -4
View File
@@ -385,8 +385,8 @@ INSERT INTO '.USER_CACHE_TABLE.'
VALUES
('.$userdata['id'].',\''.boolean_to_string($userdata['need_update']).'\','
.$userdata['cache_update_time'].',\''
.$userdata['forbidden_categories'].'\','.$userdata['nb_total_images'].',"'
.$userdata['image_access_type'].'","'.$userdata['image_access_list'].'")';
.$userdata['forbidden_categories'].'\','.$userdata['nb_total_images'].',\''
.$userdata['image_access_type'].'\',\''.$userdata['image_access_list'].'\')';
pwg_query($query);
}
}
@@ -632,7 +632,7 @@ FROM '.CATEGORIES_TABLE.' as c
if ( isset($filter_days) )
{
$query .= ' AND i.date_available > SUBDATE(CURRENT_DATE,INTERVAL '.$filter_days.' DAY)';
$query .= ' AND i.date_available > '.pwg_db_get_recent_period_expression($filter_days);
}
if ( !empty($userdata['forbidden_categories']) )
@@ -1039,7 +1039,7 @@ function try_log_user($username, $password, $remember_me)
SELECT '.$conf['user_fields']['id'].' AS id,
'.$conf['user_fields']['password'].' AS password
FROM '.USERS_TABLE.'
WHERE '.$conf['user_fields']['username'].' = \''.mysql_real_escape_string($username).'\'
WHERE '.$conf['user_fields']['username'].' = \''.pwg_db_real_escape_string($username).'\'
;';
$row = pwg_db_fetch_assoc(pwg_query($query));
if ($row['password'] == $conf['pass_convert']($password))
+1 -2
View File
@@ -449,8 +449,7 @@ SELECT DISTINCT(id)
FROM '.IMAGES_TABLE.'
INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
WHERE
date_available >= SUBDATE(
CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)
date_available >= '.pwg_db_get_recent_period_expression($user['recent_period']).'
'.$forbidden.'
'.$conf['order_by'].'
;';
+5 -5
View File
@@ -121,15 +121,15 @@ function ws_std_image_sql_order( $params, $tbl_name='' )
case 'date_posted':
$matches[1][$i] = 'date_available'; break;
case 'rand': case 'random':
$matches[1][$i] = 'RAND()'; break;
$matches[1][$i] = DB_RANDOM_FUNCTION.'()'; break;
}
$sortable_fields = array('id', 'file', 'name', 'hit', 'average_rate',
'date_creation', 'date_available', 'RAND()' );
'date_creation', 'date_available', DB_RANDOM_FUNCTION.'()' );
if ( in_array($matches[1][$i], $sortable_fields) )
{
if (!empty($ret))
$ret .= ', ';
if ($matches[1][$i] != 'RAND()' )
if ($matches[1][$i] != DB_RANDOM_FUNCTION.'()' )
{
$ret .= $tbl_name;
}
@@ -230,7 +230,7 @@ function ws_categories_getImages($params, &$service)
continue;
if ($params['recursive'])
{
$where_clauses[] = 'uppercats REGEXP \'(^|,)'.$cat_id.'(,|$)\'';
$where_clauses[] = 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$cat_id.'(,|$)\'';
}
else
{
@@ -377,7 +377,7 @@ function ws_categories_getList($params, &$service)
}
else if ($params['cat_id']>0)
{
$where[] = 'uppercats REGEXP \'(^|,)'.
$where[] = 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.
(int)($params['cat_id'])
.'(,|$)\'';
}