BxDevDb.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Developer Developer
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDevDb extends BxDolModuleDb
  12. {
  13. function __construct(&$oConfig)
  14. {
  15. parent::__construct($oConfig);
  16. }
  17. function getQueryInsert($sTable, $aItems, $mixedComment = false, $aExclude = array('id'))
  18. {
  19. $bFirst = true;
  20. $sContent = $sComment = "";
  21. foreach($aItems as $aItem) {
  22. foreach($aExclude as $sKey)
  23. if(isset($aItem[$sKey]))
  24. unset($aItem[$sKey]);
  25. $aKeys = array_keys($aItem);
  26. $aValues = array_values($aItem);
  27. $iValues = count($aValues);
  28. foreach ($aValues as $iKey => $sValue)
  29. $aValues[$iKey] = BxDevFunctions::dbAddSlashes($sValue, true);
  30. if($bFirst) {
  31. $sContent .= "INSERT INTO `" . $sTable . "`(`" . implode("`, `", $aKeys) . "`) VALUES \n";
  32. $bFirst = false;
  33. }
  34. $sSql = "(" . implode(", ", array_fill(0, $iValues, "?")) . "),\n";
  35. $sSql = call_user_func_array(array($this, 'prepareAsString'), array_merge(array($sSql), $aValues));
  36. $sContent .= $sSql;
  37. }
  38. $sContent = substr($sContent, 0, -2) . ";\n\n";
  39. if($mixedComment === true || (is_string($mixedComment) && $mixedComment != ''))
  40. $sComment = "--\n-- " . ($mixedComment === true ? "Dumping data for table `" . $sTable . "`" : $mixedComment) . "\n--\n";
  41. return $sComment . $sContent;
  42. }
  43. function getQueryDelete($sTable, $sKey, $aItems, $mixedComment = false)
  44. {
  45. $sContent = $sComment = "";
  46. foreach($aItems as $aItem)
  47. if(isset($aItem[$sKey])) {
  48. $sSql = "DELETE FROM `" . $sTable . "` WHERE `" . $sKey . "`=?;\n";
  49. $sContent .= call_user_func_array(array($this, 'prepareAsString'), array($sSql, $aItem[$sKey]));
  50. }
  51. $sContent .= "\n";
  52. if($mixedComment === true || (is_string($mixedComment) && $mixedComment != ''))
  53. $sComment = "--\n-- " . ($mixedComment === true ? "Delete data from table `" . $sTable . "`" : $mixedComment) . "\n--\n";
  54. return $sComment . $sContent;
  55. }
  56. }
  57. /** @} */