Changeset 11485 for trunk


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

feature:2359 add single_update and single_insert functions

Location:
trunk/include/dblayer
Files:
4 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 *
  • 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 *
  • 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 *
  • 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.