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_pdo-sqlite.inc.php

    r8728 r11485  
    279279      if (isset($data[$key]) and $data[$key] != '')
    280280      {
    281         $query.= $separator.$key.' = \''.$data[$key].'\'';
     281        $query.= $separator.$key.' = \''.$data[$key].'\'';
    282282      }
    283283      else
    284284      {
    285         if ($flags & MASS_UPDATES_SKIP_EMPTY )
    286           continue; // next field
    287         $query.= "$separator$key = NULL";
     285        if ( $flags & MASS_UPDATES_SKIP_EMPTY )
     286          continue; // next field
     287        $query.= "$separator$key = NULL";
    288288      }
    289289      $is_first = false;
     
    296296      foreach ($dbfields['primary'] as $key)
    297297      {
    298         if (!$is_first)
     298        if (!$is_first)
    299299        {
    300           $query.= ' AND ';
    301         }
    302         if ( isset($data[$key]) )
     300          $query.= ' AND ';
     301        }
     302        if ( isset($data[$key]) )
    303303        {
    304           $query.= $key.' = \''.$data[$key].'\'';
    305         }
    306         else
     304          $query.= $key.' = \''.$data[$key].'\'';
     305        }
     306        else
    307307        {
    308           $query.= $key.' IS NULL';
    309         }
    310         $is_first = false;
     308          $query.= $key.' IS NULL';
     309        }
     310        $is_first = false;
    311311      }
    312312      pwg_query($query);
    313313    }
     314  }
     315}
     316
     317/**
     318 * updates on line in a table
     319 *
     320 * @param string table_name
     321 * @param array dbfields
     322 * @param array data
     323 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
     324 * @return void
     325 */
     326function single_update($tablename, $dbfields, $data, $flags=0)
     327{
     328  if (count($data) == 0)
     329    return;
     330
     331  $query = '
     332UPDATE '.$tablename.'
     333  SET ';
     334  $is_first = true;
     335  foreach ($dbfields['update'] as $key)
     336  {
     337    $separator = $is_first ? '' : ",\n    ";
     338   
     339    if (isset($data[$key]) and $data[$key] != '')
     340    {
     341      $query.= $separator.$key.' = \''.$data[$key].'\'';
     342    }
     343    else
     344    {
     345      if ( $flags & MASS_UPDATES_SKIP_EMPTY )
     346        continue; // next field
     347      $query.= "$separator$key = NULL";
     348    }
     349    $is_first = false;
     350  }
     351  if (!$is_first)
     352  {// only if one field at least updated
     353    $query.= '
     354  WHERE ';
     355    $is_first = true;
     356    foreach ($dbfields['primary'] as $key)
     357    {
     358      if (!$is_first)
     359      {
     360        $query.= ' AND ';
     361      }
     362      if ( isset($data[$key]) )
     363      {
     364        $query.= $key.' = \''.$data[$key].'\'';
     365      }
     366      else
     367      {
     368        $query.= $key.' IS NULL';
     369      }
     370      $is_first = false;
     371    }
     372    pwg_query($query);
    314373  }
    315374}
     
    324383 * @return void
    325384 */
    326 
    327385function mass_inserts($table_name, $dbfields, $datas)
    328386{
     
    381439
    382440/**
     441 * inserts one line in a table
     442 *
     443 * @param string table_name
     444 * @param array dbfields
     445 * @param array insert
     446 * @return void
     447 */
     448function single_insert($table_name, $dbfields, $insert)
     449{
     450  if (count($insert) != 0)
     451  {
     452    $query = '
     453INSERT INTO '.$table_name.'
     454  ('.implode(',', $dbfields).')
     455  VALUES';
     456
     457    $query .= '(';
     458    foreach ($dbfields as $field_id => $dbfield)
     459    {
     460      if ($field_id > 0)
     461      {
     462        $query .= ',';
     463      }
     464      if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
     465      {
     466        $query .= 'NULL';
     467      }
     468      else
     469      {
     470        $query .= "'".$insert[$dbfield]."'";
     471      }
     472    }
     473    $query .= ')';
     474   
     475    pwg_query($query);
     476  }
     477}
     478
     479/**
    383480 * Do maintenance on all PWG tables
    384481 *
Note: See TracChangeset for help on using the changeset viewer.