table.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. require_once __DIR__."/../config.php";
  3. require_once __DIR__."/../redis/function.php";
  4. // Require Composer's autoloader.
  5. require '../../vendor/autoload.php';
  6. // Using Medoo namespace.
  7. use Medoo\Medoo;
  8. class Table
  9. {
  10. protected $dbh;
  11. protected $table;
  12. protected $redis;
  13. protected $errorMessage;
  14. protected $field_setting;
  15. protected $result;
  16. public $medoo;
  17. protected $redisProfix;
  18. function __construct($db,$table,$user="",$password="",$redis=false) {
  19. $this->dbh = new PDO($db, _DB_USERNAME_, _DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
  20. $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  21. $database = new Medoo([
  22. // Initialized and connected PDO object.
  23. 'pdo' => $this->dbh,
  24. // [optional] Medoo will have different handle method according to different database type.
  25. 'type' => _DB_ENGIN_
  26. ]);
  27. $this->medoo = $database;
  28. $this->redis = $redis;
  29. $this->table = $table;
  30. $this->result = ["ok"=>true,"message"=>"","data"=>array()];
  31. $this->redisProfix = $table . "/:id";
  32. }
  33. public function _index($columns,$where){
  34. $output = $this->medoo->select(
  35. $this->table,
  36. $columns,
  37. $where
  38. );
  39. $this->result["data"] = $output;
  40. return $this->result;
  41. }
  42. public function _create($data,$columns){
  43. foreach ($columns as $value) {
  44. # code...
  45. $updateDate[$value] = $data[$value];
  46. }
  47. $this->medoo->insert(
  48. $this->table,
  49. $updateDate
  50. );
  51. $updateDate["id"] = $this->medoo->id();
  52. $this->result["data"] = $updateDate;
  53. return $this->result;
  54. }
  55. public function _update($data,$columns=null,$where=null){
  56. if($columns==null){
  57. $updateDate = $data;
  58. }else{
  59. foreach ($columns as $value) {
  60. # code...
  61. $updateDate[$value] = $data[$value];
  62. }
  63. }
  64. if($where==null){
  65. $where = ["id"=>$data["id"]];
  66. }
  67. $this->medoo->update(
  68. $this->table,
  69. $updateDate,
  70. $where
  71. );
  72. if($this->medoo->error){
  73. $this->result["ok"]=false;
  74. $this->result["message"]=$this->medoo->error;
  75. return false;
  76. }else{
  77. return true;
  78. }
  79. }
  80. public function _show($columns,$id){
  81. $output = $this->medoo->get(
  82. $this->table,
  83. $columns,
  84. ["id"=>$id]
  85. );
  86. if($this->medoo->error){
  87. $this->result["ok"]=false;
  88. $this->result["message"]=$this->medoo->error;
  89. }else{
  90. $this->result["data"] = $output;
  91. }
  92. return $this->result;
  93. }
  94. public function _deleteId($id){
  95. $output = $this->medoo->delete(
  96. $this->table,
  97. ["id"=>$id]
  98. );
  99. $this->result["data"] = $output->rowCount();
  100. return $this->result;
  101. }
  102. public function _delete($where){
  103. $output = $this->medoo->delete(
  104. $this->table,
  105. $where
  106. );
  107. $this->result["data"] = $output->rowCount();
  108. if($this->result["data"]==0){
  109. $this->result["ok"] = false;
  110. $this->result["message"] = ":no delete";
  111. }
  112. return $this->result;
  113. }
  114. protected function fetch($query,$params){
  115. if (isset($params)) {
  116. $stmt = $this->dbh->prepare($query);
  117. if($stmt){
  118. $stmt->execute($params);
  119. }
  120. } else {
  121. $stmt = $PDO->query($query);
  122. }
  123. if($stmt){
  124. return $stmt->fetch(PDO::FETCH_ASSOC);
  125. }
  126. else{
  127. return false;
  128. }
  129. }
  130. function execute($query, $params=null){
  131. if (isset($params)) {
  132. $stmt = $this->dbh->prepare($query);
  133. if($stmt){
  134. $stmt->execute($params);
  135. return $stmt;
  136. }
  137. else{
  138. return false;
  139. }
  140. } else {
  141. return $this->dbh->query($query);
  142. }
  143. }
  144. }
  145. ?>