table.php 2.9 KB

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