init
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Query\Processors;
|
||||
|
||||
class MariaDbProcessor extends MySqlProcessor
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a columns query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of an indexes query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a foreign keys query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a types query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
public function processTypes($results)
|
||||
{
|
||||
return array_map(function ($result) {
|
||||
$result = (object) $result;
|
||||
|
||||
return [
|
||||
'name' => $result->name,
|
||||
'schema' => $result->schema,
|
||||
'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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a columns query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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',
|
||||
default => null,
|
||||
},
|
||||
'expression' => $result->default,
|
||||
] : null,
|
||||
];
|
||||
}, $results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of an indexes query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a foreign keys query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
<?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 tables query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
public function processTables($results)
|
||||
{
|
||||
return array_map(function ($result) {
|
||||
$result = (object) $result;
|
||||
|
||||
return [
|
||||
'name' => $result->name,
|
||||
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
|
||||
'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 array $results
|
||||
* @return array
|
||||
*/
|
||||
public function processViews($results)
|
||||
{
|
||||
return array_map(function ($result) {
|
||||
$result = (object) $result;
|
||||
|
||||
return [
|
||||
'name' => $result->name,
|
||||
'schema' => $result->schema ?? null, // PostgreSQL and SQL Server
|
||||
'definition' => $result->definition,
|
||||
];
|
||||
}, $results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a types query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
public function processTypes($results)
|
||||
{
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a columns query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
public function processColumns($results)
|
||||
{
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of an indexes query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
public function processIndexes($results)
|
||||
{
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a foreign keys query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
public function processForeignKeys($results)
|
||||
{
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Database\Query\Processors;
|
||||
|
||||
class SQLiteProcessor extends Processor
|
||||
{
|
||||
/**
|
||||
* Process the results of a columns query.
|
||||
*
|
||||
* @param array $results
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of an indexes query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a foreign keys query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
public function processForeignKeys($results)
|
||||
{
|
||||
return array_map(function ($result) {
|
||||
$result = (object) $result;
|
||||
|
||||
return [
|
||||
'name' => null,
|
||||
'columns' => explode(',', $result->columns),
|
||||
'foreign_schema' => null,
|
||||
'foreign_table' => $result->foreign_table,
|
||||
'foreign_columns' => explode(',', $result->foreign_columns),
|
||||
'on_update' => strtolower($result->on_update),
|
||||
'on_delete' => strtolower($result->on_delete),
|
||||
];
|
||||
}, $results);
|
||||
}
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
<?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'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a columns query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of an indexes query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the results of a foreign keys query.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user