table.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. protected $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. $newId = $database->id;
  51. $this->result["data"] = $newId;
  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 $this->result;
  68. }
  69. public function get($columns,$where){
  70. $output = $this->medoo->get(
  71. $this->table,
  72. $columns,
  73. $where
  74. );
  75. $this->result["data"] = $output;
  76. return $this->result;
  77. }
  78. public function delete($where){
  79. $output = $this->medoo->delete(
  80. $this->table,
  81. $where
  82. );
  83. $this->result["data"] = $output->rowCount();
  84. return $this->result;
  85. }
  86. protected function fetch($query,$params){
  87. if (isset($params)) {
  88. $stmt = $this->dbh->prepare($query);
  89. if($stmt){
  90. $stmt->execute($params);
  91. }
  92. } else {
  93. $stmt = $PDO->query($query);
  94. }
  95. if($stmt){
  96. return $stmt->fetch(PDO::FETCH_ASSOC);
  97. }
  98. else{
  99. return false;
  100. }
  101. }
  102. function execute($query, $params=null){
  103. if (isset($params)) {
  104. $stmt = $this->dbh->prepare($query);
  105. if($stmt){
  106. $stmt->execute($params);
  107. return $stmt;
  108. }
  109. else{
  110. return false;
  111. }
  112. } else {
  113. return $this->dbh->query($query);
  114. }
  115. }
  116. }
  117. ?>