Ignore:
Timestamp:
Nov 25, 2009, 8:02:57 PM (15 years ago)
Author:
nikrou
Message:

Feature 1255: modification in sql queries

  • manage random function
  • manage regex syntax
  • manage quote (single instead of double)
  • manage interval
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/dblayer/functions_mysql.inc.php

    r4335 r4367  
    2222// +-----------------------------------------------------------------------+
    2323
     24define('DB_ENGINE', 'MySQL');
     25
     26define('DB_REGEX_OPERATOR', 'REGEXP');
     27define('DB_RANDOM_FUNCTION', 'RAND');
     28
    2429/**
    2530 *
     
    106111
    107112  return $result;
     113}
     114
     115function pwg_db_nextval($column, $table)
     116{
     117  $query = '
     118SELECT IF(MAX('.$column.')+1 IS NULL, 1, MAX('.$column.')+1)
     119  FROM '.$table;
     120  list($next) = pwg_db_fetch_row(pwg_query($query));
     121
     122  return $next;
    108123}
    109124
     
    444459}
    445460
     461function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE')
     462{
     463  if ($date!='CURRENT_DATE')
     464  {
     465    $date = '\''.$date.'\'';
     466  }
     467
     468  return 'SUBDATE('.$date.',INTERVAL '.$period.' DAY)';
     469}
     470
     471function pwg_db_get_recent_period($period, $date='CURRENT_DATE')
     472{
     473  $query = '
     474SELECT '.pwg_db_get_recent_period_expression($period);
     475  list($d) = pwg_db_fetch_row(pwg_query($query));
     476
     477  return $d;
     478}
     479
     480/**
     481 * returns an array containing the possible values of an enum field
     482 *
     483 * @param string tablename
     484 * @param string fieldname
     485 */
     486function get_enums($table, $field)
     487{
     488  // retrieving the properties of the table. Each line represents a field :
     489  // columns are 'Field', 'Type'
     490  $result = pwg_query('desc '.$table);
     491  while ($row = pwg_db_fetch_assoc($result))
     492  {
     493    // we are only interested in the the field given in parameter for the
     494    // function
     495    if ($row['Field'] == $field)
     496    {
     497      // retrieving possible values of the enum field
     498      // enum('blue','green','black')
     499      $options = explode(',', substr($row['Type'], 5, -1));
     500      foreach ($options as $i => $option)
     501      {
     502        $options[$i] = str_replace("'", '',$option);
     503      }
     504    }
     505  }
     506  pwg_db_free_result($result);
     507  return $options;
     508}
     509
     510// get_boolean transforms a string to a boolean value. If the string is
     511// "false" (case insensitive), then the boolean value false is returned. In
     512// any other case, true is returned.
     513function get_boolean( $string )
     514{
     515  $boolean = true;
     516  if ( 'false' == strtolower($string) )
     517  {
     518    $boolean = false;
     519  }
     520  return $boolean;
     521}
     522
     523/**
     524 * returns boolean string 'true' or 'false' if the given var is boolean
     525 *
     526 * @param mixed $var
     527 * @return mixed
     528 */
     529function boolean_to_string($var)
     530{
     531  if (is_bool($var))
     532  {
     533    return $var ? 'true' : 'false';
     534  }
     535  else
     536  {
     537    return $var;
     538  }
     539}
     540
    446541// my_error returns (or send to standard output) the message concerning the
    447542// error occured for the last mysql query.
Note: See TracChangeset for help on using the changeset viewer.