table.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. return true;
  72. }
  73. public function _show($columns,$id){
  74. $output = $this->medoo->get(
  75. $this->table,
  76. $columns,
  77. ["id"=>$id]
  78. );
  79. $this->result["data"] = $output;
  80. return $this->result;
  81. }
  82. public function _deleteId($id){
  83. $output = $this->medoo->delete(
  84. $this->table,
  85. ["id"=>$id]
  86. );
  87. $this->result["data"] = $output->rowCount();
  88. return $this->result;
  89. }
  90. public function _delete($where){
  91. $output = $this->medoo->delete(
  92. $this->table,
  93. $where
  94. );
  95. $this->result["data"] = $output->rowCount();
  96. if($this->result["data"]==0){
  97. $this->result["ok"] = false;
  98. $this->result["message"] = ":no delete";
  99. }
  100. return $this->result;
  101. }
  102. protected function fetch($query,$params){
  103. if (isset($params)) {
  104. $stmt = $this->dbh->prepare($query);
  105. if($stmt){
  106. $stmt->execute($params);
  107. }
  108. } else {
  109. $stmt = $PDO->query($query);
  110. }
  111. if($stmt){
  112. return $stmt->fetch(PDO::FETCH_ASSOC);
  113. }
  114. else{
  115. return false;
  116. }
  117. }
  118. function execute($query, $params=null){
  119. if (isset($params)) {
  120. $stmt = $this->dbh->prepare($query);
  121. if($stmt){
  122. $stmt->execute($params);
  123. return $stmt;
  124. }
  125. else{
  126. return false;
  127. }
  128. } else {
  129. return $this->dbh->query($query);
  130. }
  131. }
  132. }
  133. ?>