DatabaseDao.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. class DatabaseDao {
  4. //说明
  5. function select($sqlString,$resultType){
  6. $data = M()->query($sqlString);
  7. if($resultType=="entity"){
  8. return $data[0];
  9. }else if($resultType=="list") {
  10. return $data;
  11. }else if($resultType=="rows") {
  12. return $data[0]["rows"];
  13. }else if($resultType=="return"){
  14. if($data){
  15. return array("return"=>"true");
  16. }else{
  17. return false;
  18. }
  19. }else{
  20. return $data;
  21. }
  22. }
  23. function update($sqlString,$resultType){
  24. $data = M()->execute($sqlString);
  25. //if($resultType=="boolean"){
  26. if($data){
  27. return array("return"=>"true");
  28. }else{
  29. return false;
  30. }
  31. //}else{
  32. // return $data;
  33. //}
  34. }
  35. function insert ($sqlString,$resultType){
  36. if($resultType=="boolean"){
  37. $data = M()->execute($sqlString);
  38. if($data){
  39. return array("return"=>"true");
  40. }else{
  41. return false;
  42. }
  43. }else if($resultType=="autoid"){
  44. $data = M()->executeinsert($sqlString);
  45. if($data){
  46. return array("autoid"=>$data);
  47. }else{
  48. return "";
  49. }
  50. } else{
  51. $data = M()->execute($sqlString);
  52. if($data){
  53. return array("return"=>"true");
  54. }else{
  55. return false;
  56. }
  57. }
  58. }
  59. function delete($sqlString,$resultType){
  60. $data = M()->execute($sqlString);
  61. return $data;
  62. }
  63. /**
  64. * [array_to_sql 根据数组key和value拼接成需要的sql]
  65. * @param [type] $array [key, value结构数组]
  66. * @param string $type [sql类型insert,update]
  67. * @param array $exclude [排除的字段]
  68. * @return [string] [返回拼接好的sql]
  69. */
  70. function array_to_sql($array, $type='insert', $exclude = array()){
  71. $sql = '';
  72. if(count($array) > 0){
  73. foreach ($exclude as $exkey) {
  74. unset($array[$exkey]);//剔除不要的key
  75. }
  76. if('insert' == $type){
  77. $keys = array_keys($array);
  78. $values = array_values($array);
  79. $col = implode("`, `", $keys);
  80. $val = implode("', '", $values);
  81. $sql = "(`$col`) values('$val')";
  82. }else if('update' == $type){
  83. $tempsql = '';
  84. $temparr = array();
  85. foreach ($array as $key => $value) {
  86. $tempsql = "'$key' = '$value'";
  87. $temparr[] = $tempsql;
  88. }
  89. $sql = implode(",", $temparr);
  90. }
  91. }
  92. return $sql;
  93. }
  94. }
  95. ?>