Ignore:
Timestamp:
Jun 22, 2011, 5:56:19 PM (13 years ago)
Author:
mistic100
Message:

feature:2359 add single_update and single_insert functions

File:
1 edited

Legend:

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

    r11364 r11485  
    220220  if (count($datas) == 0)
    221221    return;
     222 
    222223  // depending on the MySQL version, we use the multi table update or N update queries
    223224  if (count($datas) < 10)
     
    233234        $separator = $is_first ? '' : ",\n    ";
    234235
    235         if (isset($data[$key]) and $data[$key] !== '')
     236        if (isset($data[$key]) and $data[$key] != '')
    236237        {
    237238          $query.= $separator.$key.' = \''.$data[$key].'\'';
     
    239240        else
    240241        {
    241           if ($flags & MASS_UPDATES_SKIP_EMPTY )
     242          if ( $flags & MASS_UPDATES_SKIP_EMPTY )
    242243            continue; // next field
    243244          $query.= "$separator$key = NULL";
     
    346347}
    347348
     349/**
     350 * updates one line in a table
     351 *
     352 * @param string table_name
     353 * @param array dbfields
     354 * @param array data
     355 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
     356 * @return void
     357 */
     358function single_update($tablename, $dbfields, $data, $flags=0)
     359{
     360  if (count($data) == 0)
     361    return;
     362
     363  $query = '
     364UPDATE '.$tablename.'
     365  SET ';
     366  $is_first = true;
     367  foreach ($dbfields['update'] as $key)
     368  {
     369    $separator = $is_first ? '' : ",\n    ";
     370
     371    if (isset($data[$key]) and $data[$key] != '')
     372    {
     373      $query.= $separator.$key.' = \''.$data[$key].'\'';
     374    }
     375    else
     376    {
     377      if ( $flags & MASS_UPDATES_SKIP_EMPTY )
     378        continue; // next field
     379      $query.= "$separator$key = NULL";
     380    }
     381    $is_first = false;
     382  }
     383  if (!$is_first)
     384  {// only if one field at least updated
     385    $query.= '
     386  WHERE ';
     387    $is_first = true;
     388    foreach ($dbfields['primary'] as $key)
     389    {
     390      if (!$is_first)
     391      {
     392        $query.= ' AND ';
     393      }
     394      if ( isset($data[$key]) )
     395      {
     396        $query.= $key.' = \''.$data[$key].'\'';
     397      }
     398      else
     399      {
     400        $query.= $key.' IS NULL';
     401      }
     402      $is_first = false;
     403    }
     404    pwg_query($query);
     405  }
     406}
     407
    348408
    349409/**
     
    412472
    413473/**
     474 * inserts on line in a table
     475 *
     476 * @param string table_name
     477 * @param array dbfields
     478 * @param array insert
     479 * @return void
     480 */
     481function single_insert($table_name, $dbfields, $insert)
     482{
     483  if (count($insert) != 0)
     484  {
     485    $query = '
     486INSERT INTO '.$table_name.'
     487  ('.implode(',', $dbfields).')
     488  VALUES';
     489
     490    $query .= '(';
     491    foreach ($dbfields as $field_id => $dbfield)
     492    {
     493      if ($field_id > 0)
     494      {
     495        $query .= ',';
     496      }
     497      if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
     498      {
     499        $query .= 'NULL';
     500      }
     501      else
     502      {
     503        $query .= "'".$insert[$dbfield]."'";
     504      }
     505    }
     506    $query .= ')';
     507   
     508    pwg_query($query);
     509  }
     510}
     511
     512/**
    414513 * Do maintenance on all PWG tables
    415514 *
Note: See TracChangeset for help on using the changeset viewer.