SqlTable.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. if (! empty($options['debug'])) {
  62. echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
  63. }
  64. if (! empty($options['dry'])) {
  65. return true;
  66. }
  67. return $this->db->createQuery()->executeInsertAll($query);
  68. }
  69. public function insertSelect($insert_table, $insert_keys=null, $where=null, $options=[])
  70. {
  71. $options_select = array_slice($options, 0);
  72. unset($options_select['debug']);
  73. $ignore_sql = empty($options['ignore']) ? '' : 'ignore';
  74. $on_duplicate_key_updates_sql = empty($options['on_duplicate_key_updates']) ? "" : ("on duplicate key update " . $options['on_duplicate_key_updates']);
  75. $sql_keys = $insert_keys ? ("(" . $insert_keys . ")") : "";
  76. $insert = "insert " . $ignore_sql . " into " . $insert_table . " " . $sql_keys . PHP_EOL;
  77. $query = $this->buildQuery($where, $options_select);
  78. $query = $insert . " " . $query . " " . $on_duplicate_key_updates_sql;
  79. if (! empty($options['debug'])) {
  80. echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
  81. }
  82. if (! empty($options['dry'])) {
  83. return true;
  84. }
  85. return $this->db->createQuery()->execute($query);
  86. }
  87. public function update(array $updates=[], array $where=[], $options=[]) /* : int */
  88. {
  89. $limit_sql = (isset($options['limit']) && ! is_null($options['limit'])) ? ("limit " . $options['limit']) : "";
  90. $join_sql = isset($options['join']) ? implode(" ", $options['join']) : "";
  91. /*
  92. if (isset($options['group by']) && empty($options['group_by'])) {
  93. $options['group_by'] = $options['group by'];
  94. }
  95. $group_by_sql = isset($options['group_by']) ? ("group by " . $options['group_by']) : "";
  96. */
  97. $query = "update " . $this->table_name . "
  98. " . $join_sql . "
  99. set " . $this->db->buildSqlUpdateValues($updates) . "
  100. where " . $this->db->buildSqlWhere($where) . "
  101. " . $limit_sql;
  102. if (! empty($options['debug'])) {
  103. echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
  104. }
  105. if (! empty($options['dry'])) {
  106. return true;
  107. }
  108. return $this->db->createQuery()->executeUpdate($query);
  109. }
  110. public function delete(array $where=[], $options=[]) /* : int */
  111. {
  112. $limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
  113. $query = "delete from " . $this->table_name . "
  114. where " . $this->db->buildSqlWhere($where) . "
  115. " . $limit_sql;
  116. if (! empty($options['debug'])) {
  117. echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
  118. }
  119. if (! empty($options['dry'])) {
  120. return true;
  121. }
  122. return $this->db->createQuery()->executeDelete($query);
  123. }
  124. public function all($where=null, $options=[])
  125. {
  126. // Alias of getAll
  127. return $this->getAll($where, $options);
  128. }
  129. public function select($where=null, $options=[])
  130. {
  131. // Alias of getAll
  132. return $this->getAll($where, $options);
  133. }
  134. public function selectAll($where=null, $options=[])
  135. {
  136. // Alias of getAll
  137. return $this->getAll($where, $options);
  138. }
  139. public function getAll($where=null, $options=[]) /* : array */
  140. {
  141. $query = $this->buildQuery($where, $options);
  142. if (! empty($options['dry'])) {
  143. return [];
  144. }
  145. return $this->db->createQuery()->executeSelectAll($query);
  146. }
  147. public function getAllWithFoundRows($where=null, $options=[]) /* : array */
  148. {
  149. $options['CALC_FOUND_ROWS'] = true;
  150. $query = $this->buildQuery($where, $options);
  151. if (! empty($options['dry'])) {
  152. $data = [];
  153. $found_rows = 0;
  154. } else {
  155. $rs = $this->db->createQuery()->execute($query);
  156. $data = $rs->fetchAll();
  157. $found_rows = $rs->getfoundRowsCount();
  158. }
  159. return ['found_rows' => $found_rows, 'data' => $data];
  160. }
  161. public function selectCount($where=null, $options=[])
  162. {
  163. $options['select'] = 'count(*) as nb';
  164. $row = $this->getOne($where, $options);
  165. return empty($row['nb']) ? 0 : $row['nb'];
  166. }
  167. public function selectOne($where=null, $options=[])
  168. {
  169. // Alias of getOne
  170. return $this->getOne($where, $options);
  171. }
  172. public function getOne($where=null, $options=[]) /* : array */
  173. {
  174. $options['limit'] = 1;
  175. //return $this->getAll($where, $options)->fetchOne();
  176. $query = $this->buildQuery($where, $options);
  177. if (! empty($options['dry'])) {
  178. return [];
  179. }
  180. return $this->db->createQuery()->executeSelectOne($query);
  181. }
  182. public function buildQuery($where=null, $options=[]) /* : string */
  183. {
  184. if (empty($options['order_by']) && ! empty($options['order by'])) {
  185. // alias "order by" to "order_by"
  186. $options['order_by'] = $options['order by'];
  187. }
  188. if (empty($options['group_by']) && ! empty($options['group by'])) {
  189. // alias "group by" to "group_by"
  190. $options['group_by'] = $options['group by'];
  191. }
  192. $limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
  193. $group_by_sql = isset($options['group_by']) ? ("group by " . $options['group_by']) : "";
  194. $order_by_sql = isset($options['order_by']) ? ("order by " . $options['order_by']) : "";
  195. $having_sql = isset($options['having']) ? ("having " . $options['having']) : "";
  196. $table_name = isset($options['from']) ? $options['from'] : $this->table_name;
  197. $select_sql = '*';
  198. if (! empty($options['select'])) {
  199. $options['select'] = is_array($options['select']) ? $options['select'] : [$options['select']];
  200. $select_sql = implode(', ', $options['select']);
  201. }
  202. if (! empty($options['CALC_FOUND_ROWS']) && stripos($select_sql, 'SQL_CALC_FOUND_ROWS') === false) {
  203. $select_sql = 'SQL_CALC_FOUND_ROWS ' . $select_sql;
  204. }
  205. if (! empty($options['where'])) {
  206. if (empty($where)) {
  207. $where = $options['where'];
  208. } else {
  209. $where = array_merge($where, $options['where']);
  210. }
  211. }
  212. $table_alias = empty($options['alias']) ? "" : $options['alias'];
  213. $joins_sql = '';
  214. if (! empty($options['join'])) {
  215. $options['join'] = is_array($options['join']) ? $options['join'] : [$options['join']];
  216. $joins_sql = implode(' ', $options['join']);
  217. }
  218. $query = "select " . $select_sql . "
  219. from " . $this->table_name . " " . $table_alias . "
  220. " . $joins_sql . "
  221. where " . $this->db->buildSqlWhere($where) . "
  222. " . $group_by_sql . "
  223. " . $having_sql . "
  224. " . $order_by_sql . "
  225. " . $limit_sql;
  226. if (! empty($options['debug'])) {
  227. echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
  228. }
  229. return $query;
  230. }
  231. }