table.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. require_once __DIR__."/../config.php";
  3. require_once __DIR__."/../redis/function.php";
  4. require_once __DIR__."/../public/snowflakeid.php";
  5. // Require Composer's autoloader.
  6. require '../../vendor/autoload.php';
  7. // Using Medoo namespace.
  8. use Medoo\Medoo;
  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. protected $SnowFlake;
  20. function __construct($db,$table,$user="",$password="",$redis=false) {
  21. $this->dbh = new PDO($db, _DB_USERNAME_, _DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
  22. $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  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' => _DB_ENGIN_
  28. ]);
  29. $this->medoo = $database;
  30. $this->redis = $redis;
  31. $this->table = $table;
  32. $this->result = ["ok"=>true,"message"=>"","data"=>array()];
  33. $this->redisProfix = $table . "/:id";
  34. $this->SnowFlake = new SnowFlakeId();
  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. $this->medoo->insert(
  51. $this->table,
  52. $updateDate
  53. );
  54. //$updateDate["id"] = $newData;
  55. $this->result["data"] = $updateDate;
  56. return $this->result;
  57. }
  58. public function _update($data,$columns=null,$where=null){
  59. if($columns==null){
  60. $updateDate = $data;
  61. }else{
  62. foreach ($columns as $value) {
  63. # code...
  64. $updateDate[$value] = $data[$value];
  65. }
  66. }
  67. if($where==null){
  68. $where = ["id"=>$data["id"]];
  69. }
  70. $this->medoo->update(
  71. $this->table,
  72. $updateDate,
  73. $where
  74. );
  75. if($this->medoo->error){
  76. $this->result["ok"]=false;
  77. $this->result["message"]=$this->medoo->error;
  78. return false;
  79. }else{
  80. return true;
  81. }
  82. }
  83. public function _show($columns,$id){
  84. $output = $this->medoo->get(
  85. $this->table,
  86. $columns,
  87. ["id"=>$id]
  88. );
  89. if($this->medoo->error){
  90. $this->result["ok"]=false;
  91. $this->result["message"]=$this->medoo->error;
  92. }else{
  93. $this->result["data"] = $output;
  94. }
  95. return $this->result;
  96. }
  97. public function _deleteId($id){
  98. $output = $this->medoo->delete(
  99. $this->table,
  100. ["id"=>$id]
  101. );
  102. $this->result["data"] = $output->rowCount();
  103. return $this->result;
  104. }
  105. public function _delete($where){
  106. $output = $this->medoo->delete(
  107. $this->table,
  108. $where
  109. );
  110. $this->result["data"] = $output->rowCount();
  111. if($this->result["data"]==0){
  112. $this->result["ok"] = false;
  113. $this->result["message"] = ":no delete";
  114. }
  115. return $this->result;
  116. }
  117. protected function fetch($query,$params){
  118. if (isset($params)) {
  119. $stmt = $this->dbh->prepare($query);
  120. if($stmt){
  121. $stmt->execute($params);
  122. }
  123. } else {
  124. $stmt = $PDO->query($query);
  125. }
  126. if($stmt){
  127. return $stmt->fetch(PDO::FETCH_ASSOC);
  128. }
  129. else{
  130. return false;
  131. }
  132. }
  133. function execute($query, $params=null){
  134. if (isset($params)) {
  135. $stmt = $this->dbh->prepare($query);
  136. if($stmt){
  137. $stmt->execute($params);
  138. return $stmt;
  139. }
  140. else{
  141. return false;
  142. }
  143. } else {
  144. return $this->dbh->query($query);
  145. }
  146. }
  147. }
  148. ?>