sqlite.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php namespace Laravel\Database\Query\Grammars;
  2. use Laravel\Database\Query;
  3. class SQLite extends Grammar
  4. {
  5. /**
  6. * Compile the ORDER BY clause for a query.
  7. *
  8. * @param Query $query
  9. * @return string
  10. */
  11. protected function orderings(Query $query)
  12. {
  13. foreach ($query->orderings as $ordering)
  14. {
  15. $sql[] = $this->wrap($ordering['column']).' COLLATE NOCASE '.strtoupper($ordering['direction']);
  16. }
  17. return 'ORDER BY '.implode(', ', $sql);
  18. }
  19. /**
  20. * Compile a SQL INSERT statement from a Query instance.
  21. *
  22. * This method handles the compilation of single row inserts and batch inserts.
  23. *
  24. * @param Query $query
  25. * @param array $values
  26. * @return string
  27. */
  28. public function insert(Query $query, $values)
  29. {
  30. // Essentially we will force every insert to be treated as a batch insert which
  31. // simply makes creating the SQL easier for us since we can utilize the same
  32. // basic routine regardless of an amount of records given to us to insert.
  33. $table = $this->wrap_table($query->from);
  34. if ( ! is_array(reset($values)))
  35. {
  36. $values = array($values);
  37. }
  38. // If there is only one record being inserted, we will just use the usual query
  39. // grammar insert builder because no special syntax is needed for the single
  40. // row inserts in SQLite. However, if there are multiples, we'll continue.
  41. if (count($values) == 1)
  42. {
  43. return parent::insert($query, $values[0]);
  44. }
  45. $names = $this->columnize(array_keys($values[0]));
  46. $columns = array();
  47. // SQLite requires us to build the multi-row insert as a listing of select with
  48. // unions joining them together. So we'll build out this list of columns and
  49. // then join them all together with select unions to complete the queries.
  50. foreach (array_keys($values[0]) as $column)
  51. {
  52. $columns[] = '? AS '.$this->wrap($column);
  53. }
  54. $columns = array_fill(9, count($values), implode(', ', $columns));
  55. return "INSERT INTO $table ($names) SELECT ".implode(' UNION SELECT ', $columns);
  56. }
  57. }