table.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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,$where=null){
  55. foreach ($columns as $value) {
  56. # code...
  57. $updateDate[$value] = $data[$value];
  58. }
  59. if($where==null){
  60. $where = ["id"=>$data["id"]];
  61. }
  62. $this->medoo->update(
  63. $this->table,
  64. $updateDate,
  65. $where
  66. );
  67. return true;
  68. }
  69. public function _show($columns,$id){
  70. $output = $this->medoo->get(
  71. $this->table,
  72. $columns,
  73. ["id"=>$id]
  74. );
  75. $this->result["data"] = $output;
  76. return $this->result;
  77. }
  78. public function _deleteId($id){
  79. $output = $this->medoo->delete(
  80. $this->table,
  81. ["id"=>$id]
  82. );
  83. $this->result["data"] = $output->rowCount();
  84. return $this->result;
  85. }
  86. public function _delete($where){
  87. $output = $this->medoo->delete(
  88. $this->table,
  89. $where
  90. );
  91. $this->result["data"] = $output->rowCount();
  92. if($this->result["data"]==0){
  93. $this->result["ok"] = false;
  94. $this->result["message"] = ":no delete";
  95. }
  96. return $this->result;
  97. }
  98. protected function fetch($query,$params){
  99. if (isset($params)) {
  100. $stmt = $this->dbh->prepare($query);
  101. if($stmt){
  102. $stmt->execute($params);
  103. }
  104. } else {
  105. $stmt = $PDO->query($query);
  106. }
  107. if($stmt){
  108. return $stmt->fetch(PDO::FETCH_ASSOC);
  109. }
  110. else{
  111. return false;
  112. }
  113. }
  114. function execute($query, $params=null){
  115. if (isset($params)) {
  116. $stmt = $this->dbh->prepare($query);
  117. if($stmt){
  118. $stmt->execute($params);
  119. return $stmt;
  120. }
  121. else{
  122. return false;
  123. }
  124. } else {
  125. return $this->dbh->query($query);
  126. }
  127. }
  128. }
  129. ?>