PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/bodega2.0/dm/Sql.php

https://github.com/orlando28021/Bodeguero
PHP | 76 lines | 65 code | 10 blank | 1 comment | 3 complexity | a61fa2547824dc76321ab386317971f0 MD5 | raw file
  1. <?php
  2. class Sql {
  3. private $_colWhere = array();
  4. private $_colSelect = array('*');
  5. private $_colFrom = array();
  6. private $_colValues=array();
  7. private $_colInto=array();
  8. private $_colSet=array();
  9. private $_accion;
  10. public function addTable($table){
  11. $this->_colFrom[] = $table;
  12. }
  13. public function addSet($set){
  14. $this->_colSet[]=$set;
  15. }
  16. public function addWhere($where){
  17. $this->_colWhere[] = $where;
  18. }
  19. public function addInto($into){
  20. $this->_colInto[]=$into;
  21. }
  22. public function addValues($value){
  23. $this->_colValues[]=$value;
  24. }
  25. public function setOpcion($accion){
  26. $this->_accion=$accion;
  27. }
  28. public function getOpcion(){return $this->_accion;}
  29. private function _generar(){
  30. switch ($this->_accion){
  31. case 'listar':
  32. $select = implode(',',array_unique($this->_colSelect));
  33. $from = implode(',',array_unique($this->_colFrom));
  34. $where = implode(' AND ', array_unique($this->_colWhere));
  35. if($where != null)
  36. {
  37. return 'SELECT '.$select.' FROM '."`".$from."`".' WHERE '.$where;
  38. }else{
  39. return 'SELECT '.$select.' FROM '."`".$from."`";
  40. }
  41. break;
  42. case 'insert':
  43. $insert=implode(',', array_unique($this->_colFrom));
  44. $into=implode("`".','."`", array_unique($this->_colInto));
  45. $values=implode("'".','."'",($this->_colValues));
  46. return 'INSERT INTO '."`".$insert."`".' ('."`".$into."`".')'.' VALUES('."'".$values."'".')';
  47. break;
  48. case 'update':
  49. $update=implode(',',array_unique($this->_colFrom));
  50. $set=implode(',',array_unique($this->_colSet));
  51. $where=implode('AND', array_unique($this->_colWhere));
  52. return 'UPDATE '."`".$update."`".' SET '.$set.' WHERE '.$where;
  53. break;
  54. case 'delete':
  55. $delete= implode(',',array_unique($this->_colFrom));
  56. $where=implode('AND', array_unique($this->_colWhere));
  57. return 'DELETE FROM '."`".$delete."`".' WHERE '.$where;
  58. }
  59. }
  60. public function __toString() {
  61. $sql=$this->_generar();
  62. // print_r($sql);
  63. return $this->_generar();
  64. }
  65. }
  66. ?>