Skip to content

Commit

Permalink
Make SQLiteProcessor cope with '/' in column names (#52490)
Browse files Browse the repository at this point in the history
Add test and fix syntax
  • Loading branch information
vroomfondle authored Aug 14, 2024
1 parent cc31ca2 commit 6f7908f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Database/Query/Processors/SQLiteProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ public function processColumns($results, $sql = '')

$type = strtolower($result->type);

$safeName = preg_quote($result->name, '/');

$collation = preg_match(
'/\b'.preg_quote($result->name).'\b[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:default|check|as)\s*(?:\(.*?\))?[^,]*)*collate\s+["\'`]?(\w+)/i',
'/\b'.$safeName.'\b[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:default|check|as)\s*(?:\(.*?\))?[^,]*)*collate\s+["\'`]?(\w+)/i',
$sql,
$matches
) === 1 ? strtolower($matches[1]) : null;

$isGenerated = in_array($result->extra, [2, 3]);

$expression = $isGenerated && preg_match(
'/\b'.preg_quote($result->name).'\b[^,]+\s+as\s+\(((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\)/i',
'/\b'.$safeName.'\b[^,]+\s+as\s+\(((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\)/i',
$sql,
$matches
) === 1 ? $matches[1] : null;
Expand Down
2 changes: 2 additions & 0 deletions tests/Database/DatabaseSQLiteProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public function testProcessColumns()
['name' => 'id', 'type' => 'INTEGER', 'nullable' => '0', 'default' => '', 'primary' => '1', 'extra' => 1],
['name' => 'name', 'type' => 'varchar', 'nullable' => '1', 'default' => 'foo', 'primary' => '0', 'extra' => 1],
['name' => 'is_active', 'type' => 'tinyint(1)', 'nullable' => '0', 'default' => '1', 'primary' => '0', 'extra' => 1],
['name' => 'with/slash', 'type' => 'tinyint(1)', 'nullable' => '0', 'default' => '1', 'primary' => '0', 'extra' => 1],
];
$expected = [
['name' => 'id', 'type_name' => 'integer', 'type' => 'integer', 'collation' => null, 'nullable' => false, 'default' => '', 'auto_increment' => true, 'comment' => null, 'generation' => null],
['name' => 'name', 'type_name' => 'varchar', 'type' => 'varchar', 'collation' => null, 'nullable' => true, 'default' => 'foo', 'auto_increment' => false, 'comment' => null, 'generation' => null],
['name' => 'is_active', 'type_name' => 'tinyint', 'type' => 'tinyint(1)', 'collation' => null, 'nullable' => false, 'default' => '1', 'auto_increment' => false, 'comment' => null, 'generation' => null],
['name' => 'with/slash', 'type_name' => 'tinyint', 'type' => 'tinyint(1)', 'collation' => null, 'nullable' => false, 'default' => '1', 'auto_increment' => false, 'comment' => null, 'generation' => null],
];

$this->assertEquals($expected, $processor->processColumns($listing));
Expand Down

0 comments on commit 6f7908f

Please sign in to comment.