/extensible/master/examples/server/php/db.php

https://github.com/vivid-planet/library · PHP · 153 lines · 122 code · 28 blank · 3 comment · 11 complexity · b85b9b99ce7671a07863a3054904c260 MD5 · raw file

  1. <?php
  2. Class Database {
  3. private $db;
  4. private $config;
  5. private $error_mode = PDO::ERRMODE_EXCEPTION;
  6. private $fetch_type = PDO::FETCH_ASSOC;
  7. public function __construct($config = null) {
  8. $this->config = isset($config) ? $config : new Config();
  9. }
  10. public function connect() {
  11. if (!isset($this->db)) {
  12. $cfg = $this->config;
  13. $this->db = new PDO('mysql:host='.$cfg->host.';dbname='.$cfg->dbname, $cfg->username, $cfg->password);
  14. $this->db->setAttribute(PDO::ATTR_ERRMODE, $this->error_mode);
  15. }
  16. return $this->db;
  17. }
  18. public function select($table, $id = null, $id_col = null) {
  19. $sql = 'SELECT * FROM '.$table;
  20. $id_col = isset($id_col) ? $id_col : 'id';
  21. $this->connect();
  22. if (isset($id)) {
  23. $query = $this->db->prepare($sql.' WHERE '.$id_col.' = :id');
  24. $query->bindParam(':id', $id);
  25. $query->execute();
  26. }
  27. else {
  28. $query = $this->db->prepare($sql);
  29. $query->execute();
  30. }
  31. $result = $query->fetchAll($this->fetch_type);
  32. return $result;
  33. }
  34. public function query($table, array $values) {
  35. $sql = 'SELECT * FROM '.$table;
  36. $param_mappings = array();
  37. $col_mappings = array();
  38. $count = 1;
  39. foreach ($values as $col => $value) {
  40. // $param is auto-numbered rather than named to support querying
  41. // multiple times on the same column easily
  42. $param = ':'.$count++;
  43. if (is_array($value)) {
  44. $col = isset($value['column']) ? $value['column'] : $col;
  45. $param_mappings[$param] = $value['value'];
  46. $comparator = isset($value['comparator']) ? $value['comparator'] : '=';
  47. $conjunction = isset($value['conjunction']) ? $value['conjunction'] : 'AND';
  48. }
  49. else {
  50. $param_mappings[$param] = $value;
  51. $comparator = '=';
  52. $conjunction = 'AND';
  53. }
  54. if (count($col_mappings) === 0) {
  55. $conjunction = '';
  56. }
  57. if (strtoupper($comparator) === 'LIKE') {
  58. // Enable partial matching:
  59. $param_mappings[$param] = '%'.$param_mappings[$param].'%';
  60. }
  61. array_push($col_mappings, $conjunction.' '.$col.' '.$comparator.' '.$param);
  62. }
  63. if (count($col_mappings) > 0) {
  64. $param_list = implode(' ', $col_mappings);
  65. $sql = $sql.' WHERE '.$param_list;
  66. }
  67. return $this->querySql($sql, $param_mappings);
  68. }
  69. public function querySql($sql, array $params) {
  70. $this->connect();
  71. $query = $this->db->prepare($sql);
  72. $query->execute($params);
  73. $result = $query->fetchAll($this->fetch_type);
  74. return $result;
  75. }
  76. public function execSql($sql, array $params) {
  77. $this->connect();
  78. $query = $this->db->prepare($sql);
  79. return $query->execute($params);
  80. }
  81. private function save($action, $table, array $values) {
  82. $param_names = array();
  83. $param_mappings = array();
  84. $col_mappings = array();
  85. foreach ($values as $col => $value) {
  86. $param = ':'.$col;
  87. array_push($param_names, $param);
  88. $param_mappings[$param] = $value;
  89. array_push($col_mappings, $col.'='.$param);
  90. }
  91. if ($action === 'INSERT') {
  92. $cols = implode(',', array_keys($values));
  93. $param_list = implode(',', $param_names);
  94. $sql = 'INSERT INTO '.$table.' ('.$cols.') VALUES ('.$param_list.')';
  95. }
  96. else {
  97. $cols = implode(',', $col_mappings);
  98. $id = $values['id'];
  99. $sql = 'UPDATE '.$table.' SET '.$cols.' WHERE id = '.$id;
  100. }
  101. $this->connect();
  102. $query = $this->db->prepare($sql);
  103. $query->execute($param_mappings);
  104. $id = $action == 'INSERT' ? $this->db->lastInsertId() : $id;
  105. $result = $this->select($table, $id);
  106. return $result;
  107. }
  108. public function insert($table, array $values) {
  109. return $this->save('INSERT', $table, $values);
  110. }
  111. public function update($table, array $values) {
  112. return $this->save('UPDATE', $table, $values);
  113. }
  114. public function delete($table, $id, $id_col = null) {
  115. $id_col = isset($id_col) ? $id_col : 'id';
  116. $sql = 'DELETE FROM '.$table.' WHERE '.$id_col.' = :id';
  117. $this->connect();
  118. $query = $this->db->prepare($sql);
  119. $query->bindParam(':id', $id);
  120. $query->execute();
  121. $result = $query->rowCount();
  122. return $result;
  123. }
  124. }
  125. ?>