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

    r8728 r11485  
    291291      if (isset($data[$key]) and $data[$key] != '')
    292292      {
    293         $query.= $separator.$key.' = \''.$data[$key].'\'';
     293        $query.= $separator.$key.' = \''.$data[$key].'\'';
    294294      }
    295295      else
    296296      {
    297         if ($flags & MASS_UPDATES_SKIP_EMPTY )
    298           continue; // next field
    299         $query.= "$separator$key = NULL";
     297        if ( $flags & MASS_UPDATES_SKIP_EMPTY )
     298          continue; // next field
     299        $query.= "$separator$key = NULL";
    300300      }
    301301      $is_first = false;
     
    308308      foreach ($dbfields['primary'] as $key)
    309309      {
    310         if (!$is_first)
     310        if (!$is_first)
    311311        {
    312           $query.= ' AND ';
    313         }
    314         if ( isset($data[$key]) )
     312          $query.= ' AND ';
     313        }
     314        if ( isset($data[$key]) )
    315315        {
    316           $query.= $key.' = \''.$data[$key].'\'';
    317         }
    318         else
     316          $query.= $key.' = \''.$data[$key].'\'';
     317        }
     318        else
    319319        {
    320           $query.= $key.' IS NULL';
    321         }
    322         $is_first = false;
     320          $query.= $key.' IS NULL';
     321        }
     322        $is_first = false;
    323323      }
    324324      pwg_query($query);
    325325    }
     326  }
     327}
     328
     329/**
     330 * updates on line in a table
     331 *
     332 * @param string table_name
     333 * @param array dbfields
     334 * @param array data
     335 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
     336 * @return void
     337 */
     338function single_update($tablename, $dbfields, $data, $flags=0)
     339{
     340  if (count($data) == 0)
     341    return;
     342
     343  $query = '
     344UPDATE '.$tablename.'
     345  SET ';
     346  $is_first = true;
     347  foreach ($dbfields['update'] as $key)
     348  {
     349    $separator = $is_first ? '' : ",\n    ";
     350   
     351    if (isset($data[$key]) and $data[$key] != '')
     352    {
     353      $query.= $separator.$key.' = \''.$data[$key].'\'';
     354    }
     355    else
     356    {
     357      if ( $flags & MASS_UPDATES_SKIP_EMPTY )
     358        continue; // next field
     359      $query.= "$separator$key = NULL";
     360    }
     361    $is_first = false;
     362  }
     363  if (!$is_first)
     364  {// only if one field at least updated
     365    $query.= '
     366  WHERE ';
     367    $is_first = true;
     368    foreach ($dbfields['primary'] as $key)
     369    {
     370      if (!$is_first)
     371      {
     372        $query.= ' AND ';
     373      }
     374      if ( isset($data[$key]) )
     375      {
     376        $query.= $key.' = \''.$data[$key].'\'';
     377      }
     378      else
     379      {
     380        $query.= $key.' IS NULL';
     381      }
     382      $is_first = false;
     383    }
     384    pwg_query($query);
    326385  }
    327386}
     
    336395 * @return void
    337396 */
    338 
    339397function mass_inserts($table_name, $dbfields, $datas)
    340398{
     
    393451
    394452/**
     453 * inserts one line in a table
     454 *
     455 * @param string table_name
     456 * @param array dbfields
     457 * @param array insert
     458 * @return void
     459 */
     460function single_insert($table_name, $dbfields, $insert)
     461{
     462  if (count($insert) != 0)
     463  {
     464    $query = '
     465INSERT INTO '.$table_name.'
     466  ('.implode(',', $dbfields).')
     467  VALUES';
     468
     469    $query .= '(';
     470    foreach ($dbfields as $field_id => $dbfield)
     471    {
     472      if ($field_id > 0)
     473      {
     474        $query .= ',';
     475      }
     476      if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
     477      {
     478        $query .= 'NULL';
     479      }
     480      else
     481      {
     482        $query .= "'".$insert[$dbfield]."'";
     483      }
     484    }
     485    $query .= ')';
     486   
     487    pwg_query($query);
     488  }
     489}
     490
     491/**
    395492 * Do maintenance on all PWG tables
    396493 *
Note: See TracChangeset for help on using the changeset viewer.