table.php 3.3 KB

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