SqlTable.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace KarmaFW\Database\Sql;
  3. use \KarmaFW\App;
  4. class SqlTable
  5. {
  6. protected $db;
  7. protected $table_name;
  8. protected $columns = null;
  9. public function __construct($table_name, $db=null)
  10. {
  11. if (is_null($db)) {
  12. $db = App::getDb();
  13. }
  14. $this->table_name = $table_name;
  15. $this->db = $db;
  16. }
  17. public function getTableName()
  18. {
  19. return $this->table_name;
  20. }
  21. public function __toString()
  22. {
  23. return $this->getTableName();
  24. }
  25. public function listColumns() /* : array */
  26. {
  27. $schema = new SqlSchema($this->db);
  28. $this->columns = $schema->listTableColumns($table, $column=null);
  29. return $this->columns;
  30. }
  31. public function insert($values=[], $options=[]) /* : int */
  32. {
  33. $this->insertAll([$values], $options);
  34. return $this->db->getInsertId();
  35. }
  36. public function insertAll($rows=[], $options=[])
  37. {
  38. /*
  39. $rows = [
  40. ['id' => 1, 'name' => 'foo'],
  41. ['id' => 2, 'name' => 'bar'],
  42. ];
  43. */
  44. $values_array = [];
  45. $fields_sql = '';
  46. foreach ($rows as $values) {
  47. $insert = $this->db->buildSqlInsertValues($values);
  48. $values_array[] = "(" . $insert['values'] . ")";
  49. if (empty($fields_sql)) {
  50. $fields_sql = $insert['fields'];
  51. }
  52. }
  53. if (empty($values_array)) {
  54. return null;
  55. }
  56. $inserts_sql = implode(', ', $values_array);
  57. $ignore_sql = empty($options['ignore']) ? '' : 'ignore';
  58. $on_duplicate_key_updates_sql = empty($options['on_duplicate_key_updates']) ? "" : ("on duplicate key update " . $options['on_duplicate_key_updates']);
  59. // TODO: gerer on_duplicate_key_updates comme le where, dans un tableau et non dans un string
  60. $query = "insert " . $ignore_sql . " into " . $this->table_name . " (" . $fields_sql . ") values " . $inserts_sql . " " . $on_duplicate_key_updates_sql;
  61. return $this->db->createQuery()->executeInsertAll($query);
  62. }
  63. public function update(array $updates=[], array $where=[], $options=[]) /* : int */
  64. {
  65. $limit_sql = (isset($options['limit']) && ! is_null($options['limit'])) ? ("limit " . $options['limit']) : "";
  66. $join_sql = isset($options['join']) ? implode(" ", $options['join']) : "";
  67. /*
  68. if (isset($options['group by']) && empty($options['group_by'])) {
  69. $options['group_by'] = $options['group by'];
  70. }
  71. $group_by_sql = isset($options['group_by']) ? ("group by " . $options['group_by']) : "";
  72. */
  73. $query = "update " . $this->table_name . "
  74. " . $join_sql . "
  75. set " . $this->db->buildSqlUpdateValues($updates) . "
  76. where " . $this->db->buildSqlWhere($where) . "
  77. " . $limit_sql;
  78. return $this->db->createQuery()->executeUpdate($query);
  79. }
  80. public function delete(array $where=[], $options=[]) /* : int */
  81. {
  82. $limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
  83. $query = "delete from " . $this->table_name . "
  84. where " . $this->db->buildSqlWhere($where) . "
  85. " . $limit_sql;
  86. return $this->db->createQuery()->executeDelete($query);
  87. }
  88. public function all($where=null, $options=[])
  89. {
  90. // Alias of getAll
  91. return $this->getAll($where, $options);
  92. }
  93. public function select($where=null, $options=[])
  94. {
  95. // Alias of getAll
  96. return $this->getAll($where, $options);
  97. }
  98. public function selectAll($where=null, $options=[])
  99. {
  100. // Alias of getAll
  101. return $this->getAll($where, $options);
  102. }
  103. public function getAll($where=null, $options=[]) /* : array */
  104. {
  105. $query = $this->buildQuery($where, $options);
  106. return $this->db->createQuery()->executeSelectAll($query);
  107. }
  108. public function getAllWithFoundRows($where=null, $options=[]) /* : array */
  109. {
  110. $query = $this->buildQuery($where, $options);
  111. $rs = $this->db->createQuery()->execute($query);
  112. $data = $rs->fetchAll();
  113. $found_rows = $rs->getfoundRowsCount();
  114. return ['found_rows' => $found_rows, 'data' => $data];
  115. }
  116. public function selectCount($where=null, $options=[])
  117. {
  118. $options['select'] = 'count(*) as nb';
  119. $row = $this->getOne($where, $options);
  120. return empty($row['nb']) ? 0 : $row['nb'];
  121. }
  122. public function selectOne($where=null, $options=[])
  123. {
  124. // Alias of getOne
  125. return $this->getOne($where, $options);
  126. }
  127. public function getOne($where=null, $options=[]) /* : array */
  128. {
  129. $options['limit'] = 1;
  130. //return $this->getAll($where, $options)->fetchOne();
  131. $query = $this->buildQuery($where, $options);
  132. return $this->db->createQuery()->executeSelectOne($query);
  133. }
  134. public function buildQuery($where=null, $options=[]) /* : string */
  135. {
  136. if (empty($options['order_by']) && ! empty($options['order by'])) {
  137. // alias "order by" to "order_by"
  138. $options['order_by'] = $options['order by'];
  139. }
  140. if (empty($options['group_by']) && ! empty($options['group by'])) {
  141. // alias "group by" to "group_by"
  142. $options['group_by'] = $options['group by'];
  143. }
  144. $limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
  145. $group_by_sql = isset($options['group_by']) ? ("group by " . $options['group_by']) : "";
  146. $order_by_sql = isset($options['order_by']) ? ("order by " . $options['order_by']) : "";
  147. $having_sql = isset($options['having']) ? ("having " . $options['having']) : "";
  148. $table_name = isset($options['from']) ? $options['from'] : $this->table_name;
  149. $select_sql = '*';
  150. if (! empty($options['select'])) {
  151. $options['select'] = is_array($options['select']) ? $options['select'] : [$options['select']];
  152. $select_sql = implode(', ', $options['select']);
  153. }
  154. if (! empty($options['CALC_FOUND_ROWS'])) {
  155. $select_sql = 'SQL_CALC_FOUND_ROWS ' . $select_sql;
  156. }
  157. if (! empty($options['where'])) {
  158. if (empty($where)) {
  159. $where = $options['where'];
  160. } else {
  161. $where = array_merge($where, $options['where']);
  162. }
  163. }
  164. $table_alias = empty($options['alias']) ? "" : $options['alias'];
  165. $joins_sql = '';
  166. if (! empty($options['join'])) {
  167. $options['join'] = is_array($options['join']) ? $options['join'] : [$options['join']];
  168. $joins_sql = implode(' ', $options['join']);
  169. }
  170. $query = "select " . $select_sql . "
  171. from " . $this->table_name . " " . $table_alias . "
  172. " . $joins_sql . "
  173. where " . $this->db->buildSqlWhere($where) . "
  174. " . $having_sql . "
  175. " . $group_by_sql . "
  176. " . $order_by_sql . "
  177. " . $limit_sql;
  178. if (! empty($options['debug'])) {
  179. echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
  180. }
  181. return $query;
  182. }
  183. }