Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add support for getting native column type #45598

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/Illuminate/Database/Query/Processors/MySqlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,4 @@

class MySqlProcessor extends Processor
{
/**
* Process the results of a column listing query.
*
* @param array $results
* @return array
*/
public function processColumnListing($results)
{
return array_map(function ($result) {
return ((object) $result)->column_name;
}, $results);
}
}
20 changes: 18 additions & 2 deletions src/Illuminate/Database/Query/Processors/PostgresProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,26 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
* @param array $results
* @return array
*/
public function processColumnListing($results)
public function processColumns($results)
{
return array_map(function ($result) {
return ((object) $result)->column_name;
$result = (object) $result;

$type = match ($typeName = $result->type_name) {
'bit', 'bit varying', 'character', 'character varying' => is_null($result->length) ? $typeName : $typeName."($result->length)",
'numeric' => is_null($result->total) ? $typeName : $typeName."($result->total,$result->places)",
'time without time zone' => is_null($result->precision) ? $typeName : "time($result->precision) without time zone",
'time with time zone' => is_null($result->precision) ? $typeName : "time($result->precision) with time zone",
'timestamp without time zone' => is_null($result->precision) ? $typeName : "timestamp($result->precision) without time zone",
'timestamp with time zone' => is_null($result->precision) ? $typeName : "timestamp($result->precision) with time zone",
default => $typeName,
};

return [
'name' => $result->name,
'type_name' => $result->type_name,
'type' => $type,
];
}, $results);
}
}
12 changes: 10 additions & 2 deletions src/Illuminate/Database/Query/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
* @param array $results
* @return array
*/
public function processColumnListing($results)
public function processColumns($results)
{
return $results;
return array_map(function ($result) {
$result = (object) $result;

return [
'name' => $result->name,
'type_name' => $result->type_name,
'type' => $result->type,
];
}, $results);
}
}
12 changes: 10 additions & 2 deletions src/Illuminate/Database/Query/Processors/SQLiteProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ class SQLiteProcessor extends Processor
* @param array $results
* @return array
*/
public function processColumnListing($results)
public function processColumns($results)
{
return array_map(function ($result) {
return ((object) $result)->name;
$result = (object) $result;

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

return [
'name' => $result->name,
'type_name' => strtok($type, '('),
'type' => $type,
];
}, $results);
}
}
17 changes: 15 additions & 2 deletions src/Illuminate/Database/Query/Processors/SqlServerProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,23 @@ protected function processInsertGetIdForOdbc(Connection $connection)
* @param array $results
* @return array
*/
public function processColumnListing($results)
public function processColumns($results)
{
return array_map(function ($result) {
return ((object) $result)->name;
$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,
];
}, $results);
}
}
64 changes: 49 additions & 15 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ public static function useNativeSchemaOperationsIfPossible(bool $value = true)
*/
public function createDatabase($name)
{
throw new LogicException('This database driver does not support creating databases.');
return $this->connection->statement(
$this->grammar->compileCreateDatabase($name, $this->connection)
);
}

/**
Expand All @@ -146,7 +148,9 @@ public function createDatabase($name)
*/
public function dropDatabaseIfExists($name)
{
throw new LogicException('This database driver does not support dropping databases.');
return $this->connection->statement(
$this->grammar->compileDropDatabaseIfExists($name)
);
}

/**
Expand All @@ -160,7 +164,7 @@ public function hasTable($table)
$table = $this->connection->getTablePrefix().$table;

return count($this->connection->selectFromWriteConnection(
$this->grammar->compileTableExists(), [$table]
$this->grammar->compileTableExists($table)
)) > 0;
}

Expand Down Expand Up @@ -228,18 +232,40 @@ public function whenTableDoesntHaveColumn(string $table, string $column, Closure
}
}

/**
* Get the columns for a given table.
*
* @param string $table
* @return array
*/
public function getColumns($table)
{
$table = $this->connection->getTablePrefix().$table;

$results = $this->connection->selectFromWriteConnection($this->grammar->compileColumns($table));

return $this->connection->getPostProcessor()->processColumns($results);
}

/**
* Get the data type for the given column name.
*
* @param string $table
* @param string $column
* @param bool $fullDefinition
* @return string
*/
public function getColumnType($table, $column)
public function getColumnType($table, $column, $fullDefinition = false)
{
$table = $this->connection->getTablePrefix().$table;
$columns = $this->getColumns($table);

return $this->connection->getDoctrineColumn($table, $column)->getType()->getName();
foreach ($columns as $value) {
if (strtolower($value['name']) === $column) {
return $fullDefinition ? $value['type'] : $value['type_name'];
}
}

throw new InvalidArgumentException("There is no column with name '$column' on table '$table'.");
}

/**
Expand All @@ -250,11 +276,7 @@ public function getColumnType($table, $column)
*/
public function getColumnListing($table)
{
$results = $this->connection->selectFromWriteConnection($this->grammar->compileColumnListing(
$this->connection->getTablePrefix().$table
));

return $this->connection->getPostProcessor()->processColumnListing($results);
return array_map(fn ($result) => $result['name'], $this->getColumns($table));
}

/**
Expand Down Expand Up @@ -364,13 +386,25 @@ public function dropAllTypes()
/**
* Get all of the table names for the database.
*
* @return void
*
* @throws \LogicException
* @return array
*/
public function getAllTables()
{
throw new LogicException('This database driver does not support getting all tables.');
return $this->connection->select(
$this->grammar->compileGetAllTables()
);
}

/**
* Get all of the view names for the database.
*
* @return array
*/
public function getAllViews()
{
return $this->connection->select(
$this->grammar->compileGetAllViews()
);
}

/**
Expand Down
21 changes: 17 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,34 @@ public function compileDropDatabaseIfExists($name)
/**
* Compile the query to determine the list of tables.
*
* @param string $database
* @param string $table
* @return string
*/
public function compileTableExists()
public function compileTableExists($database, $table)
{
return "select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'";
return sprintf(
"select * from information_schema.tables where table_schema = %s and table_name = %s and table_type = 'BASE TABLE'",
$this->quoteString($database),
$this->quoteString($table)
);
}

/**
* Compile the query to determine the list of columns.
*
* @param string $database
* @param string $table
* @return string
*/
public function compileColumnListing()
public function compileColumns($database, $table)
{
return 'select column_name as `column_name` from information_schema.columns where table_schema = ? and table_name = ?';
return sprintf(
'select column_name as `name`, column_type as `type`, data_type as `type_name` '
.'from information_schema.columns where table_schema = %s and table_name = %s',
$this->quoteString($database),
$this->quoteString($table)
);
}

/**
Expand Down
26 changes: 22 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,39 @@ public function compileDropDatabaseIfExists($name)
/**
* Compile the query to determine if a table exists.
*
* @param string $database
* @param string $schema
* @param string $table
* @return string
*/
public function compileTableExists()
public function compileTableExists($database, $schema, $table)
{
return "select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'";
return sprintf(
"select * from information_schema.tables where table_catalog = %s and table_schema = %s and table_name = %s and table_type = 'BASE TABLE'",
$this->quoteString($database),
$this->quoteString($schema),
$this->quoteString($table)
);
}

/**
* Compile the query to determine the list of columns.
*
* @param string $database
* @param string $schema
* @param string $table
* @return string
*/
public function compileColumnListing()
public function compileColumns($database, $schema, $table)
{
return 'select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?';
return sprintf(
'select column_name as "name", data_type as "type_name", '
.'character_maximum_length as "length", numeric_precision as "total", numeric_scale as "places", datetime_precision as "precision" '
.'from information_schema.columns where table_catalog = %s and table_schema = %s and table_name = %s',
$this->quoteString($database),
$this->quoteString($schema),
$this->quoteString($table)
);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ class SQLiteGrammar extends Grammar
/**
* Compile the query to determine if a table exists.
*
* @param string $table
* @return string
*/
public function compileTableExists()
public function compileTableExists($table)
{
return "select * from sqlite_master where type = 'table' and name = ?";
return "select * from sqlite_master where type = 'table' and name = ".$this->quoteString($table);
}

/**
Expand All @@ -42,7 +43,7 @@ public function compileTableExists()
* @param string $table
* @return string
*/
public function compileColumnListing($table)
public function compileColumns($table)
{
return 'pragma table_info('.$this->wrap(str_replace('.', '__', $table)).')';
}
Expand Down
18 changes: 14 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ public function compileDropDatabaseIfExists($name)
/**
* Compile the query to determine if a table exists.
*
* @param string $table
* @return string
*/
public function compileTableExists()
public function compileTableExists($table)
{
return "select * from sys.sysobjects where id = object_id(?) and xtype in ('U', 'V')";
return sprintf(
"select * from sys.sysobjects where id = object_id(%s) and xtype in ('U', 'V')",
$this->quoteString($table)
);
}

/**
Expand All @@ -82,9 +86,15 @@ public function compileTableExists()
* @param string $table
* @return string
*/
public function compileColumnListing($table)
public function compileColumns($table)
{
return "select name from sys.columns where object_id = object_id('$table')";
return sprintf(
'select col.name as name, type.name as type_name, '
.'col.max_length as length, col.precision as precision, col.scale as places '
.'from sys.columns as col join sys.types as type on col.user_type_id = type.user_type_id '
.'where object_id = object_id(%s)',
$this->quoteString($table)
);
}

/**
Expand Down
Loading