This commit is contained in:
2026-05-01 23:40:14 +08:00
commit b8f599a617
3867 changed files with 478663 additions and 0 deletions
@@ -0,0 +1,8 @@
<?php
namespace Illuminate\Database\Query\Processors;
class MariaDbProcessor extends MySqlProcessor
{
//
}
@@ -0,0 +1,102 @@
<?php
namespace Illuminate\Database\Query\Processors;
use Illuminate\Database\Query\Builder;
class MySqlProcessor extends Processor
{
/**
* Process the results of a column listing query.
*
* @deprecated Will be removed in a future Laravel version.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
{
return array_map(function ($result) {
return ((object) $result)->column_name;
}, $results);
}
/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string|null $sequence
* @return int
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$query->getConnection()->insert($sql, $values, $sequence);
$id = $query->getConnection()->getLastInsertId();
return is_numeric($id) ? (int) $id : $id;
}
/** @inheritDoc */
public function processColumns($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'type_name' => $result->type_name,
'type' => $result->type,
'collation' => $result->collation,
'nullable' => $result->nullable === 'YES',
'default' => $result->default,
'auto_increment' => $result->extra === 'auto_increment',
'comment' => $result->comment ?: null,
'generation' => $result->expression ? [
'type' => match ($result->extra) {
'STORED GENERATED' => 'stored',
'VIRTUAL GENERATED' => 'virtual',
default => null,
},
'expression' => $result->expression,
] : null,
];
}, $results);
}
/** @inheritDoc */
public function processIndexes($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $name = strtolower($result->name),
'columns' => $result->columns ? explode(',', $result->columns) : [],
'type' => strtolower($result->type),
'unique' => (bool) $result->unique,
'primary' => $name === 'primary',
];
}, $results);
}
/** @inheritDoc */
public function processForeignKeys($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'columns' => explode(',', $result->columns),
'foreign_schema' => $result->foreign_schema,
'foreign_table' => $result->foreign_table,
'foreign_columns' => explode(',', $result->foreign_columns),
'on_update' => strtolower($result->on_update),
'on_delete' => strtolower($result->on_delete),
];
}, $results);
}
}
+153
View File
@@ -0,0 +1,153 @@
<?php
namespace Illuminate\Database\Query\Processors;
use Illuminate\Database\Query\Builder;
class PostgresProcessor extends Processor
{
/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string|null $sequence
* @return int
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$connection = $query->getConnection();
$connection->recordsHaveBeenModified();
$result = $connection->selectFromWriteConnection($sql, $values)[0];
$sequence = $sequence ?: 'id';
$id = is_object($result) ? $result->{$sequence} : $result[$sequence];
return is_numeric($id) ? (int) $id : $id;
}
/** @inheritDoc */
public function processTypes($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'schema' => $result->schema,
'schema_qualified_name' => $result->schema.'.'.$result->name,
'implicit' => (bool) $result->implicit,
'type' => match (strtolower($result->type)) {
'b' => 'base',
'c' => 'composite',
'd' => 'domain',
'e' => 'enum',
'p' => 'pseudo',
'r' => 'range',
'm' => 'multirange',
default => null,
},
'category' => match (strtolower($result->category)) {
'a' => 'array',
'b' => 'boolean',
'c' => 'composite',
'd' => 'date_time',
'e' => 'enum',
'g' => 'geometric',
'i' => 'network_address',
'n' => 'numeric',
'p' => 'pseudo',
'r' => 'range',
's' => 'string',
't' => 'timespan',
'u' => 'user_defined',
'v' => 'bit_string',
'x' => 'unknown',
'z' => 'internal_use',
default => null,
},
];
}, $results);
}
/** @inheritDoc */
public function processColumns($results)
{
return array_map(function ($result) {
$result = (object) $result;
$autoincrement = $result->default !== null && str_starts_with($result->default, 'nextval(');
return [
'name' => $result->name,
'type_name' => $result->type_name,
'type' => $result->type,
'collation' => $result->collation,
'nullable' => (bool) $result->nullable,
'default' => $result->generated ? null : $result->default,
'auto_increment' => $autoincrement,
'comment' => $result->comment,
'generation' => $result->generated ? [
'type' => match ($result->generated) {
's' => 'stored',
'v' => 'virtual',
default => null,
},
'expression' => $result->default,
] : null,
];
}, $results);
}
/** @inheritDoc */
public function processIndexes($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => strtolower($result->name),
'columns' => $result->columns ? explode(',', $result->columns) : [],
'type' => strtolower($result->type),
'unique' => (bool) $result->unique,
'primary' => (bool) $result->primary,
];
}, $results);
}
/** @inheritDoc */
public function processForeignKeys($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'columns' => explode(',', $result->columns),
'foreign_schema' => $result->foreign_schema,
'foreign_table' => $result->foreign_table,
'foreign_columns' => explode(',', $result->foreign_columns),
'on_update' => match (strtolower($result->on_update)) {
'a' => 'no action',
'r' => 'restrict',
'c' => 'cascade',
'n' => 'set null',
'd' => 'set default',
default => null,
},
'on_delete' => match (strtolower($result->on_delete)) {
'a' => 'no action',
'r' => 'restrict',
'c' => 'cascade',
'n' => 'set null',
'd' => 'set default',
default => null,
},
];
}, $results);
}
}
+144
View File
@@ -0,0 +1,144 @@
<?php
namespace Illuminate\Database\Query\Processors;
use Illuminate\Database\Query\Builder;
class Processor
{
/**
* Process the results of a "select" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $results
* @return array
*/
public function processSelect(Builder $query, $results)
{
return $results;
}
/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string|null $sequence
* @return int
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$query->getConnection()->insert($sql, $values);
$id = $query->getConnection()->getPdo()->lastInsertId($sequence);
return is_numeric($id) ? (int) $id : $id;
}
/**
* Process the results of a schemas query.
*
* @param list<array<string, mixed>> $results
* @return list<array{name: string, path: string|null, default: bool}>
*/
public function processSchemas($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'path' => $result->path ?? null, // SQLite Only...
'default' => (bool) $result->default,
];
}, $results);
}
/**
* Process the results of a tables query.
*
* @param list<array<string, mixed>> $results
* @return list<array{name: string, schema: string|null, schema_qualified_name: string, size: int|null, comment: string|null, collation: string|null, engine: string|null}>
*/
public function processTables($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'schema' => $result->schema ?? null,
'schema_qualified_name' => isset($result->schema) ? $result->schema.'.'.$result->name : $result->name,
'size' => isset($result->size) ? (int) $result->size : null,
'comment' => $result->comment ?? null, // MySQL and PostgreSQL
'collation' => $result->collation ?? null, // MySQL only
'engine' => $result->engine ?? null, // MySQL only
];
}, $results);
}
/**
* Process the results of a views query.
*
* @param list<array<string, mixed>> $results
* @return list<array{name: string, schema: string, schema_qualified_name: string, definition: string}>
*/
public function processViews($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'schema' => $result->schema ?? null,
'schema_qualified_name' => isset($result->schema) ? $result->schema.'.'.$result->name : $result->name,
'definition' => $result->definition,
];
}, $results);
}
/**
* Process the results of a types query.
*
* @param list<array<string, mixed>> $results
* @return list<array{name: string, schema: string, type: string, type: string, category: string, implicit: bool}>
*/
public function processTypes($results)
{
return $results;
}
/**
* Process the results of a columns query.
*
* @param list<array<string, mixed>> $results
* @return list<array{name: string, type: string, type_name: string, nullable: bool, default: mixed, auto_increment: bool, comment: string|null, generation: array{type: string, expression: string|null}|null}>
*/
public function processColumns($results)
{
return $results;
}
/**
* Process the results of an indexes query.
*
* @param list<array<string, mixed>> $results
* @return list<array{name: string, columns: list<string>, type: string, unique: bool, primary: bool}>
*/
public function processIndexes($results)
{
return $results;
}
/**
* Process the results of a foreign keys query.
*
* @param list<array<string, mixed>> $results
* @return list<array{name: string, columns: list<string>, foreign_schema: string, foreign_table: string, foreign_columns: list<string>, on_update: string, on_delete: string}>
*/
public function processForeignKeys($results)
{
return $results;
}
}
@@ -0,0 +1,99 @@
<?php
namespace Illuminate\Database\Query\Processors;
class SQLiteProcessor extends Processor
{
/** @inheritDoc */
public function processColumns($results, $sql = '')
{
$hasPrimaryKey = array_sum(array_column($results, 'primary')) === 1;
return array_map(function ($result) use ($hasPrimaryKey, $sql) {
$result = (object) $result;
$type = strtolower($result->type);
$safeName = preg_quote($result->name, '/');
$collation = preg_match(
'/\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'.$safeName.'\b[^,]+\s+as\s+\(((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\)/i',
$sql,
$matches
) === 1 ? $matches[1] : null;
return [
'name' => $result->name,
'type_name' => strtok($type, '(') ?: '',
'type' => $type,
'collation' => $collation,
'nullable' => (bool) $result->nullable,
'default' => $result->default,
'auto_increment' => $hasPrimaryKey && $result->primary && $type === 'integer',
'comment' => null,
'generation' => $isGenerated ? [
'type' => match ((int) $result->extra) {
3 => 'stored',
2 => 'virtual',
default => null,
},
'expression' => $expression,
] : null,
];
}, $results);
}
/** @inheritDoc */
public function processIndexes($results)
{
$primaryCount = 0;
$indexes = array_map(function ($result) use (&$primaryCount) {
$result = (object) $result;
if ($isPrimary = (bool) $result->primary) {
$primaryCount += 1;
}
return [
'name' => strtolower($result->name),
'columns' => $result->columns ? explode(',', $result->columns) : [],
'type' => null,
'unique' => (bool) $result->unique,
'primary' => $isPrimary,
];
}, $results);
if ($primaryCount > 1) {
$indexes = array_filter($indexes, fn ($index) => $index['name'] !== 'primary');
}
return $indexes;
}
/** @inheritDoc */
public function processForeignKeys($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => null,
'columns' => explode(',', $result->columns),
'foreign_schema' => $result->foreign_schema,
'foreign_table' => $result->foreign_table,
'foreign_columns' => explode(',', $result->foreign_columns),
'on_update' => strtolower($result->on_update),
'on_delete' => strtolower($result->on_delete),
];
}, $results);
}
}
+121
View File
@@ -0,0 +1,121 @@
<?php
namespace Illuminate\Database\Query\Processors;
use Exception;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Builder;
class SqlServerProcessor extends Processor
{
/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string|null $sequence
* @return int
*/
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{
$connection = $query->getConnection();
$connection->insert($sql, $values);
if ($connection->getConfig('odbc') === true) {
$id = $this->processInsertGetIdForOdbc($connection);
} else {
$id = $connection->getPdo()->lastInsertId();
}
return is_numeric($id) ? (int) $id : $id;
}
/**
* Process an "insert get ID" query for ODBC.
*
* @param \Illuminate\Database\Connection $connection
* @return int
*
* @throws \Exception
*/
protected function processInsertGetIdForOdbc(Connection $connection)
{
$result = $connection->selectFromWriteConnection(
'SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) AS insertid'
);
if (! $result) {
throw new Exception('Unable to retrieve lastInsertID for ODBC.');
}
$row = $result[0];
return is_object($row) ? $row->insertid : $row['insertid'];
}
/** @inheritDoc */
public function processColumns($results)
{
return array_map(function ($result) {
$result = (object) $result;
$type = match ($typeName = $result->type_name) {
'binary', 'varbinary', 'char', 'varchar', 'nchar', 'nvarchar' => $result->length == -1 ? $typeName.'(max)' : $typeName."($result->length)",
'decimal', 'numeric' => $typeName."($result->precision,$result->places)",
'float', 'datetime2', 'datetimeoffset', 'time' => $typeName."($result->precision)",
default => $typeName,
};
return [
'name' => $result->name,
'type_name' => $result->type_name,
'type' => $type,
'collation' => $result->collation,
'nullable' => (bool) $result->nullable,
'default' => $result->default,
'auto_increment' => (bool) $result->autoincrement,
'comment' => $result->comment,
'generation' => $result->expression ? [
'type' => $result->persisted ? 'stored' : 'virtual',
'expression' => $result->expression,
] : null,
];
}, $results);
}
/** @inheritDoc */
public function processIndexes($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => strtolower($result->name),
'columns' => $result->columns ? explode(',', $result->columns) : [],
'type' => strtolower($result->type),
'unique' => (bool) $result->unique,
'primary' => (bool) $result->primary,
];
}, $results);
}
/** @inheritDoc */
public function processForeignKeys($results)
{
return array_map(function ($result) {
$result = (object) $result;
return [
'name' => $result->name,
'columns' => explode(',', $result->columns),
'foreign_schema' => $result->foreign_schema,
'foreign_table' => $result->foreign_table,
'foreign_columns' => explode(',', $result->foreign_columns),
'on_update' => strtolower(str_replace('_', ' ', $result->on_update)),
'on_delete' => strtolower(str_replace('_', ' ', $result->on_delete)),
];
}, $results);
}
}