SQLiteProcessor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Illuminate\Database\Query\Processors;
  3. class SQLiteProcessor extends Processor
  4. {
  5. /**
  6. * Process the results of a columns query.
  7. *
  8. * @param array $results
  9. * @param string $sql
  10. * @return array
  11. */
  12. public function processColumns($results, $sql = '')
  13. {
  14. $hasPrimaryKey = array_sum(array_column($results, 'primary')) === 1;
  15. return array_map(function ($result) use ($hasPrimaryKey, $sql) {
  16. $result = (object) $result;
  17. $type = strtolower($result->type);
  18. $collation = preg_match(
  19. '/\b'.preg_quote($result->name).'\b[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:default|check|as)\s*(?:\(.*?\))?[^,]*)*collate\s+["\'`]?(\w+)/i',
  20. $sql,
  21. $matches
  22. ) === 1 ? strtolower($matches[1]) : null;
  23. $isGenerated = in_array($result->extra, [2, 3]);
  24. $expression = $isGenerated && preg_match(
  25. '/\b'.preg_quote($result->name).'\b[^,]+\s+as\s+\(((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\)/i',
  26. $sql,
  27. $matches
  28. ) === 1 ? $matches[1] : null;
  29. return [
  30. 'name' => $result->name,
  31. 'type_name' => strtok($type, '(') ?: '',
  32. 'type' => $type,
  33. 'collation' => $collation,
  34. 'nullable' => (bool) $result->nullable,
  35. 'default' => $result->default,
  36. 'auto_increment' => $hasPrimaryKey && $result->primary && $type === 'integer',
  37. 'comment' => null,
  38. 'generation' => $isGenerated ? [
  39. 'type' => match ((int) $result->extra) {
  40. 3 => 'stored',
  41. 2 => 'virtual',
  42. default => null,
  43. },
  44. 'expression' => $expression,
  45. ] : null,
  46. ];
  47. }, $results);
  48. }
  49. /**
  50. * Process the results of an indexes query.
  51. *
  52. * @param array $results
  53. * @return array
  54. */
  55. public function processIndexes($results)
  56. {
  57. $primaryCount = 0;
  58. $indexes = array_map(function ($result) use (&$primaryCount) {
  59. $result = (object) $result;
  60. if ($isPrimary = (bool) $result->primary) {
  61. $primaryCount += 1;
  62. }
  63. return [
  64. 'name' => strtolower($result->name),
  65. 'columns' => explode(',', $result->columns),
  66. 'type' => null,
  67. 'unique' => (bool) $result->unique,
  68. 'primary' => $isPrimary,
  69. ];
  70. }, $results);
  71. if ($primaryCount > 1) {
  72. $indexes = array_filter($indexes, fn ($index) => $index['name'] !== 'primary');
  73. }
  74. return $indexes;
  75. }
  76. /**
  77. * Process the results of a foreign keys query.
  78. *
  79. * @param array $results
  80. * @return array
  81. */
  82. public function processForeignKeys($results)
  83. {
  84. return array_map(function ($result) {
  85. $result = (object) $result;
  86. return [
  87. 'name' => null,
  88. 'columns' => explode(',', $result->columns),
  89. 'foreign_schema' => null,
  90. 'foreign_table' => $result->foreign_table,
  91. 'foreign_columns' => explode(',', $result->foreign_columns),
  92. 'on_update' => strtolower($result->on_update),
  93. 'on_delete' => strtolower($result->on_delete),
  94. ];
  95. }, $results);
  96. }
  97. }