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_pgsql.inc.php

    r10431 r11485  
    356356        else
    357357        {
    358           if ($flags & MASS_UPDATES_SKIP_EMPTY )
     358          if ( $flags & MASS_UPDATES_SKIP_EMPTY )
    359359            continue; // next field
    360360          $query.= "$separator$key = NULL";
     
    426426}
    427427
     428/**
     429 * updates on line in a table
     430 *
     431 * @param string table_name
     432 * @param array dbfields
     433 * @param array data
     434 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
     435 * @return void
     436 */
     437function single_update($tablename, $dbfields, $data, $flags=0)
     438{
     439  if (count($data) == 0)
     440    return;
     441
     442  $query = '
     443UPDATE '.$tablename.'
     444  SET ';
     445  $is_first = true;
     446  foreach ($dbfields['update'] as $key)
     447  {
     448    $separator = $is_first ? '' : ",\n    ";
     449
     450    if (isset($data[$key]) and $data[$key] != '')
     451    {
     452      $query.= $separator.$key.' = \''.$data[$key].'\'';
     453    }
     454    else
     455    {
     456      if ( $flags & MASS_UPDATES_SKIP_EMPTY )
     457        continue; // next field
     458      $query.= "$separator$key = NULL";
     459    }
     460    $is_first = false;
     461  }
     462  if (!$is_first)
     463  {// only if one field at least updated
     464    $query.= '
     465  WHERE ';
     466    $is_first = true;
     467    foreach ($dbfields['primary'] as $key)
     468    {
     469      if (!$is_first)
     470      {
     471        $query.= ' AND ';
     472      }
     473      if ( isset($data[$key]) )
     474      {
     475        $query.= $key.' = \''.$data[$key].'\'';
     476      }
     477      else
     478      {
     479        $query.= $key.' IS NULL';
     480      }
     481      $is_first = false;
     482    }
     483    pwg_query($query);
     484  }
     485}
    428486
    429487/**
     
    435493 * @return void
    436494 */
    437 
    438495function mass_inserts($table_name, $dbfields, $datas)
    439496{
     
    492549
    493550/**
     551 * inserts on line in a table
     552 *
     553 * @param string table_name
     554 * @param array dbfields
     555 * @param array insert
     556 * @return void
     557 */
     558function single_insert($table_name, $dbfields, $insert)
     559{
     560  if (count($insert) != 0)
     561  {
     562    $query = '
     563INSERT INTO '.$table_name.'
     564  ('.implode(',', $dbfields).')
     565  VALUES';
     566
     567    $query .= '(';
     568    foreach ($dbfields as $field_id => $dbfield)
     569    {
     570      if ($field_id > 0)
     571      {
     572        $query .= ',';
     573      }
     574      if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
     575      {
     576        $query .= 'NULL';
     577      }
     578      else
     579      {
     580        $query .= "'".$insert[$dbfield]."'";
     581      }
     582    }
     583    $query .= ')';
     584   
     585    pwg_query($query);
     586  }
     587}
     588
     589/**
    494590 * Do maintenance on all PWG tables
    495591 *
Note: See TracChangeset for help on using the changeset viewer.