diff --git a/src/Illuminate/Database/Query/Processors/MySqlProcessor.php b/src/Illuminate/Database/Query/Processors/MySqlProcessor.php index ce9183858d2b..3aa8c9cb9852 100644 --- a/src/Illuminate/Database/Query/Processors/MySqlProcessor.php +++ b/src/Illuminate/Database/Query/Processors/MySqlProcessor.php @@ -7,6 +7,8 @@ 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 */ @@ -16,4 +18,28 @@ public function processColumnListing($results) return ((object) $result)->column_name; }, $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; + + 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, + ]; + }, $results); + } } diff --git a/src/Illuminate/Database/Query/Processors/PostgresProcessor.php b/src/Illuminate/Database/Query/Processors/PostgresProcessor.php index 5956a8fb3148..0ec35de6853b 100755 --- a/src/Illuminate/Database/Query/Processors/PostgresProcessor.php +++ b/src/Illuminate/Database/Query/Processors/PostgresProcessor.php @@ -33,6 +33,8 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu /** * Process the results of a column listing query. * + * @deprecated Will be removed in a future Laravel version. + * * @param array $results * @return array */ @@ -42,4 +44,30 @@ public function processColumnListing($results) return ((object) $result)->column_name; }, $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' => $autoincrement ? null : $result->default, + 'auto_increment' => $autoincrement, + 'comment' => $result->comment, + ]; + }, $results); + } } diff --git a/src/Illuminate/Database/Query/Processors/Processor.php b/src/Illuminate/Database/Query/Processors/Processor.php index 0069b436d553..8fb8b0e7ab48 100755 --- a/src/Illuminate/Database/Query/Processors/Processor.php +++ b/src/Illuminate/Database/Query/Processors/Processor.php @@ -39,6 +39,8 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu /** * Process the results of a column listing query. * + * @deprecated Will be removed in a future Laravel version. + * * @param array $results * @return array */ @@ -46,4 +48,15 @@ public function processColumnListing($results) { return $results; } + + /** + * Process the results of a columns query. + * + * @param array $results + * @return array + */ + public function processColumns($results) + { + return $results; + } } diff --git a/src/Illuminate/Database/Query/Processors/SQLiteProcessor.php b/src/Illuminate/Database/Query/Processors/SQLiteProcessor.php index 65da1dff7c26..cb851efb4cc6 100644 --- a/src/Illuminate/Database/Query/Processors/SQLiteProcessor.php +++ b/src/Illuminate/Database/Query/Processors/SQLiteProcessor.php @@ -7,6 +7,8 @@ class SQLiteProcessor extends Processor /** * Process the results of a column listing query. * + * @deprecated Will be removed in a future Laravel version. + * * @param array $results * @return array */ @@ -16,4 +18,32 @@ public function processColumnListing($results) return ((object) $result)->name; }, $results); } + + /** + * Process the results of a columns query. + * + * @param array $results + * @return array + */ + public function processColumns($results) + { + $hasPrimaryKey = array_sum(array_column($results, 'primary')) === 1; + + return array_map(function ($result) use ($hasPrimaryKey) { + $result = (object) $result; + + $type = strtolower($result->type); + + return [ + 'name' => $result->name, + 'type_name' => strtok($type, '('), + 'type' => $type, + 'collation' => null, + 'nullable' => (bool) $result->nullable, + 'default' => $result->default, + 'auto_increment' => $hasPrimaryKey && $result->primary && $type === 'integer', + 'comment' => null, + ]; + }, $results); + } } diff --git a/src/Illuminate/Database/Query/Processors/SqlServerProcessor.php b/src/Illuminate/Database/Query/Processors/SqlServerProcessor.php index 49476f095594..0384335ac6db 100755 --- a/src/Illuminate/Database/Query/Processors/SqlServerProcessor.php +++ b/src/Illuminate/Database/Query/Processors/SqlServerProcessor.php @@ -58,6 +58,8 @@ protected function processInsertGetIdForOdbc(Connection $connection) /** * Process the results of a column listing query. * + * @deprecated Will be removed in a future Laravel version. + * * @param array $results * @return array */ @@ -67,4 +69,35 @@ public function processColumnListing($results) return ((object) $result)->name; }, $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; + + $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, + ]; + }, $results); + } } diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index a38b526c9a33..6597dcf57cb7 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -233,13 +233,26 @@ public function whenTableDoesntHaveColumn(string $table, string $column, Closure * * @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; - return $this->connection->getDoctrineColumn($table, $column)->getType()->getName(); + if (! $this->connection->usingNativeSchemaOperations()) { + return $this->connection->getDoctrineColumn($table, $column)->getType()->getName(); + } + + $columns = $this->getColumns($table); + + 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'."); } /** @@ -250,11 +263,22 @@ public function getColumnType($table, $column) */ public function getColumnListing($table) { - $results = $this->connection->selectFromWriteConnection($this->grammar->compileColumnListing( - $this->connection->getTablePrefix().$table - )); + return array_column($this->getColumns($table), 'name'); + } + + /** + * Get the columns for a given table. + * + * @param string $table + * @return array + */ + public function getColumns($table) + { + $table = $this->connection->getTablePrefix().$table; - return $this->connection->getPostProcessor()->processColumnListing($results); + return $this->connection->getPostProcessor()->processColumns( + $this->connection->selectFromWriteConnection($this->grammar->compileColumns($table)) + ); } /** diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index 124aeb7a2fed..7f940b95f975 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -78,6 +78,8 @@ public function compileTableExists() /** * Compile the query to determine the list of columns. * + * @deprecated Will be removed in a future Laravel version. + * * @return string */ public function compileColumnListing() @@ -85,6 +87,26 @@ public function compileColumnListing() return 'select column_name as `column_name` from information_schema.columns where table_schema = ? and table_name = ?'; } + /** + * Compile the query to determine the columns. + * + * @param string $database + * @param string $table + * @return string + */ + public function compileColumns($database, $table) + { + return sprintf( + 'select column_name as `name`, data_type as `type_name`, column_type as `type`, ' + .'collation_name as `collation`, is_nullable as `nullable`, ' + .'column_default as `default`, column_comment AS `comment`, extra as `extra` ' + .'from information_schema.columns where table_schema = %s and table_name = %s ' + .'order by ordinal_position asc', + $this->quoteString($database), + $this->quoteString($table) + ); + } + /** * Compile a create table command. * diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 2efb2541525f..845e818de542 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -81,6 +81,8 @@ public function compileTableExists() /** * Compile the query to determine the list of columns. * + * @deprecated Will be removed in a future Laravel version. + * * @return string */ public function compileColumnListing() @@ -88,6 +90,30 @@ public function compileColumnListing() return 'select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?'; } + /** + * Compile the query to determine the columns. + * + * @param string $database + * @param string $schema + * @param string $table + * @return string + */ + public function compileColumns($database, $schema, $table) + { + return sprintf( + 'select quote_ident(a.attname) as name, t.typname as type_name, format_type(a.atttypid, a.atttypmod) as type, ' + .'(select tc.collcollate from pg_catalog.pg_collation tc where tc.oid = a.attcollation) as collation, ' + .'not a.attnotnull as nullable, ' + .'(select pg_get_expr(adbin, adrelid) from pg_attrdef where c.oid = pg_attrdef.adrelid and pg_attrdef.adnum = a.attnum) as default, ' + .'(select pg_description.description from pg_description where pg_description.objoid = c.oid and a.attnum = pg_description.objsubid) as comment ' + .'from pg_attribute a, pg_class c, pg_type t, pg_namespace n ' + .'where c.relname = %s and n.nspname = %s and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid and n.oid = c.relnamespace ' + .'order by a.attnum', + $this->quoteString($table), + $this->quoteString($schema) + ); + } + /** * Compile a create table command. * diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index 9c3b1c9fb61e..60171610d020 100755 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -39,6 +39,8 @@ public function compileTableExists() /** * Compile the query to determine the list of columns. * + * @deprecated Will be removed in a future Laravel version. + * * @param string $table * @return string */ @@ -47,6 +49,21 @@ public function compileColumnListing($table) return 'pragma table_info('.$this->wrap(str_replace('.', '__', $table)).')'; } + /** + * Compile the query to determine the columns. + * + * @param string $table + * @return string + */ + public function compileColumns($table) + { + return sprintf( + "select name, type, not 'notnull' as 'nullable', dflt_value as 'default', pk as 'primary' " + .'from pragma_table_info(%s) order by cid asc', + $this->wrap(str_replace('.', '__', $table)) + ); + } + /** * Compile a create table command. * diff --git a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php index a2b56afacf61..d70aedadc0e9 100755 --- a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php @@ -79,6 +79,8 @@ public function compileTableExists() /** * Compile the query to determine the list of columns. * + * @deprecated Will be removed in a future Laravel version. + * * @param string $table * @return string */ @@ -87,6 +89,31 @@ public function compileColumnListing($table) return "select name from sys.columns where object_id = object_id('$table')"; } + /** + * Compile the query to determine the columns. + * + * @param string $table + * @return string + */ + public function compileColumns($table) + { + return sprintf( + 'select col.name, type.name as type_name, ' + .'col.max_length as length, col.precision as precision, col.scale as places, ' + .'col.is_nullable as nullable, def.definition as [default], ' + .'col.is_identity as autoincrement, col.collation_name as collation, ' + .'cast(prop.value as nvarchar(max)) as comment ' + .'from sys.columns as col ' + .'join sys.types as type on col.user_type_id = type.user_type_id ' + .'join sys.objects as obj on col.object_id = obj.object_id ' + .'join sys.schemas as scm on obj.schema_id = scm.schema_id ' + .'left join sys.default_constraints def on col.default_object_id = def.object_id and col.object_id = def.parent_object_id ' + ."left join sys.extended_properties as prop on obj.object_id = prop.major_id and col.column_id = prop.minor_id and prop.name = 'MS_Description' " + ."where obj.type = 'U' and obj.name = %s and scm.name = SCHEMA_NAME()", + $this->quoteString($table), + ); + } + /** * Compile a create table command. * diff --git a/src/Illuminate/Database/Schema/MySqlBuilder.php b/src/Illuminate/Database/Schema/MySqlBuilder.php index bbb0763a721d..3fcb73764fbe 100755 --- a/src/Illuminate/Database/Schema/MySqlBuilder.php +++ b/src/Illuminate/Database/Schema/MySqlBuilder.php @@ -46,20 +46,20 @@ public function hasTable($table) } /** - * Get the column listing for a given table. + * Get the columns for a given table. * * @param string $table * @return array */ - public function getColumnListing($table) + public function getColumns($table) { $table = $this->connection->getTablePrefix().$table; $results = $this->connection->selectFromWriteConnection( - $this->grammar->compileColumnListing(), [$this->connection->getDatabaseName(), $table] + $this->grammar->compileColumns($this->connection->getDatabaseName(), $table) ); - return $this->connection->getPostProcessor()->processColumnListing($results); + return $this->connection->getPostProcessor()->processColumns($results); } /** diff --git a/src/Illuminate/Database/Schema/PostgresBuilder.php b/src/Illuminate/Database/Schema/PostgresBuilder.php index adfbd688ee28..34a75ceb5fd5 100755 --- a/src/Illuminate/Database/Schema/PostgresBuilder.php +++ b/src/Illuminate/Database/Schema/PostgresBuilder.php @@ -176,22 +176,22 @@ public function getAllTypes() } /** - * Get the column listing for a given table. + * Get the columns for a given table. * * @param string $table * @return array */ - public function getColumnListing($table) + public function getColumns($table) { [$database, $schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; $results = $this->connection->selectFromWriteConnection( - $this->grammar->compileColumnListing(), [$database, $schema, $table] + $this->grammar->compileColumns($database, $schema, $table) ); - return $this->connection->getPostProcessor()->processColumnListing($results); + return $this->connection->getPostProcessor()->processColumns($results); } /** diff --git a/src/Illuminate/Support/Facades/Schema.php b/src/Illuminate/Support/Facades/Schema.php index d4dc8f55dc2f..4a87bcc25cec 100755 --- a/src/Illuminate/Support/Facades/Schema.php +++ b/src/Illuminate/Support/Facades/Schema.php @@ -15,8 +15,9 @@ * @method static bool hasColumns(string $table, array $columns) * @method static void whenTableHasColumn(string $table, string $column, \Closure $callback) * @method static void whenTableDoesntHaveColumn(string $table, string $column, \Closure $callback) - * @method static string getColumnType(string $table, string $column) + * @method static string getColumnType(string $table, string $column, bool $fullDefinition = false) * @method static array getColumnListing(string $table) + * @method static array getColumns(string $table) * @method static void table(string $table, \Closure $callback) * @method static void create(string $table, \Closure $callback) * @method static void drop(string $table) diff --git a/tests/Database/DatabaseMySQLSchemaBuilderTest.php b/tests/Database/DatabaseMySQLSchemaBuilderTest.php index ee9fe903e1b4..2a89d31957c3 100755 --- a/tests/Database/DatabaseMySQLSchemaBuilderTest.php +++ b/tests/Database/DatabaseMySQLSchemaBuilderTest.php @@ -38,11 +38,11 @@ public function testGetColumnListing() $connection->shouldReceive('getDatabaseName')->andReturn('db'); $connection->shouldReceive('getSchemaGrammar')->andReturn($grammar); $connection->shouldReceive('getPostProcessor')->andReturn($processor); - $grammar->shouldReceive('compileColumnListing')->once()->andReturn('sql'); - $processor->shouldReceive('processColumnListing')->once()->andReturn(['column']); + $grammar->shouldReceive('compileColumns')->with('db', 'prefix_table')->once()->andReturn('sql'); + $processor->shouldReceive('processColumns')->once()->andReturn([['name' => 'column']]); $builder = new MySqlBuilder($connection); $connection->shouldReceive('getTablePrefix')->once()->andReturn('prefix_'); - $connection->shouldReceive('selectFromWriteConnection')->once()->with('sql', ['db', 'prefix_table'])->andReturn(['column']); + $connection->shouldReceive('selectFromWriteConnection')->once()->with('sql')->andReturn([['name' => 'column']]); $this->assertEquals(['column'], $builder->getColumnListing('table')); } diff --git a/tests/Database/DatabaseMySqlProcessorTest.php b/tests/Database/DatabaseMySqlProcessorTest.php index 6f7b4bbdb53d..5ac56b1ce990 100644 --- a/tests/Database/DatabaseMySqlProcessorTest.php +++ b/tests/Database/DatabaseMySqlProcessorTest.php @@ -7,18 +7,26 @@ class DatabaseMySqlProcessorTest extends TestCase { - public function testProcessColumnListing() + public function testProcessColumns() { $processor = new MySqlProcessor; - $listing = [['column_name' => 'id'], ['column_name' => 'name'], ['column_name' => 'email']]; - $expected = ['id', 'name', 'email']; - $this->assertEquals($expected, $processor->processColumnListing($listing)); + $listing = [ + ['name' => 'id', 'type_name' => 'bigint', 'type' => 'bigint', 'collation' => 'collate', 'nullable' => 'YES', 'default' => '', 'extra' => 'auto_increment', 'comment' => 'bar'], + ['name' => 'name', 'type_name' => 'varchar', 'type' => 'varchar(100)', 'collation' => 'collate', 'nullable' => 'NO', 'default' => 'foo', 'extra' => '', 'comment' => ''], + ['name' => 'email', 'type_name' => 'varchar', 'type' => 'varchar(100)', 'collation' => 'collate', 'nullable' => 'YES', 'default' => 'NULL', 'extra' => 'on update CURRENT_TIMESTAMP', 'comment' => 'NULL'], + ]; + $expected = [ + ['name' => 'id', 'type_name' => 'bigint', 'type' => 'bigint', 'collation' => 'collate', 'nullable' => true, 'default' => '', 'auto_increment' => true, 'comment' => 'bar'], + ['name' => 'name', 'type_name' => 'varchar', 'type' => 'varchar(100)', 'collation' => 'collate', 'nullable' => false, 'default' => 'foo', 'auto_increment' => false, 'comment' => ''], + ['name' => 'email', 'type_name' => 'varchar', 'type' => 'varchar(100)', 'collation' => 'collate', 'nullable' => true, 'default' => 'NULL', 'auto_increment' => false, 'comment' => 'NULL'], + ]; + $this->assertEquals($expected, $processor->processColumns($listing)); // convert listing to objects to simulate PDO::FETCH_CLASS foreach ($listing as &$row) { $row = (object) $row; } - $this->assertEquals($expected, $processor->processColumnListing($listing)); + $this->assertEquals($expected, $processor->processColumns($listing)); } } diff --git a/tests/Database/DatabasePostgresBuilderTest.php b/tests/Database/DatabasePostgresBuilderTest.php index b5e5af603462..11c2b9ca41ac 100644 --- a/tests/Database/DatabasePostgresBuilderTest.php +++ b/tests/Database/DatabasePostgresBuilderTest.php @@ -146,13 +146,13 @@ public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathMissing() $connection->shouldReceive('getConfig')->with('schema')->andReturn(null); $grammar = m::mock(PostgresGrammar::class); $connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar); - $grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?'); - $connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'public', 'foo'])->andReturn(['countable_result']); + $grammar->shouldReceive('compileColumns')->with('laravel', 'public', 'foo')->andReturn('sql'); + $connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn(['countable_result']); $connection->shouldReceive('getTablePrefix'); $connection->shouldReceive('getConfig')->with('database')->andReturn('laravel'); $processor = m::mock(PostgresProcessor::class); $connection->shouldReceive('getPostProcessor')->andReturn($processor); - $processor->shouldReceive('processColumnListing')->andReturn(['some_column']); + $processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]); $builder = $this->getBuilder($connection); $builder->getColumnListing('foo'); @@ -164,13 +164,13 @@ public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathFilled() $connection->shouldReceive('getConfig')->with('search_path')->andReturn('myapp,public'); $grammar = m::mock(PostgresGrammar::class); $connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar); - $grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?'); - $connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']); + $grammar->shouldReceive('compileColumns')->with('laravel', 'myapp', 'foo')->andReturn('sql'); + $connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn(['countable_result']); $connection->shouldReceive('getTablePrefix'); $connection->shouldReceive('getConfig')->with('database')->andReturn('laravel'); $processor = m::mock(PostgresProcessor::class); $connection->shouldReceive('getPostProcessor')->andReturn($processor); - $processor->shouldReceive('processColumnListing')->andReturn(['some_column']); + $processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]); $builder = $this->getBuilder($connection); $builder->getColumnListing('foo'); @@ -183,13 +183,13 @@ public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathIsUserVari $connection->shouldReceive('getConfig')->with('search_path')->andReturn('$user'); $grammar = m::mock(PostgresGrammar::class); $connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar); - $grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?'); - $connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'foouser', 'foo'])->andReturn(['countable_result']); + $grammar->shouldReceive('compileColumns')->with('laravel', 'foouser', 'foo')->andReturn('sql'); + $connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn(['countable_result']); $connection->shouldReceive('getTablePrefix'); $connection->shouldReceive('getConfig')->with('database')->andReturn('laravel'); $processor = m::mock(PostgresProcessor::class); $connection->shouldReceive('getPostProcessor')->andReturn($processor); - $processor->shouldReceive('processColumnListing')->andReturn(['some_column']); + $processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]); $builder = $this->getBuilder($connection); $builder->getColumnListing('foo'); @@ -201,13 +201,13 @@ public function testGetColumnListingWhenSchemaQualifiedAndSearchPathMismatches() $connection->shouldReceive('getConfig')->with('search_path')->andReturn('public'); $grammar = m::mock(PostgresGrammar::class); $connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar); - $grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?'); - $connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']); + $grammar->shouldReceive('compileColumns')->with('laravel', 'myapp', 'foo')->andReturn('sql'); + $connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn(['countable_result']); $connection->shouldReceive('getTablePrefix'); $connection->shouldReceive('getConfig')->with('database')->andReturn('laravel'); $processor = m::mock(PostgresProcessor::class); $connection->shouldReceive('getPostProcessor')->andReturn($processor); - $processor->shouldReceive('processColumnListing')->andReturn(['some_column']); + $processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]); $builder = $this->getBuilder($connection); $builder->getColumnListing('myapp.foo'); @@ -219,13 +219,13 @@ public function testGetColumnWhenDatabaseAndSchemaQualifiedAndSearchPathMismatch $connection->shouldReceive('getConfig')->with('search_path')->andReturn('public'); $grammar = m::mock(PostgresGrammar::class); $connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar); - $grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?'); - $connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['mydatabase', 'myapp', 'foo'])->andReturn(['countable_result']); + $grammar->shouldReceive('compileColumns')->with('mydatabase', 'myapp', 'foo')->andReturn('sql'); + $connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn(['countable_result']); $connection->shouldReceive('getTablePrefix'); $connection->shouldReceive('getConfig')->with('database')->andReturn('laravel'); $processor = m::mock(PostgresProcessor::class); $connection->shouldReceive('getPostProcessor')->andReturn($processor); - $processor->shouldReceive('processColumnListing')->andReturn(['some_column']); + $processor->shouldReceive('processColumns')->andReturn([['name' => 'some_column']]); $builder = $this->getBuilder($connection); $builder->getColumnListing('mydatabase.myapp.foo'); diff --git a/tests/Database/DatabasePostgresProcessorTest.php b/tests/Database/DatabasePostgresProcessorTest.php index 6ce284f86de8..8213fc68aba7 100644 --- a/tests/Database/DatabasePostgresProcessorTest.php +++ b/tests/Database/DatabasePostgresProcessorTest.php @@ -7,20 +7,30 @@ class DatabasePostgresProcessorTest extends TestCase { - public function testProcessColumnListing() + public function testProcessColumns() { $processor = new PostgresProcessor; - $listing = [['column_name' => 'id'], ['column_name' => 'name'], ['column_name' => 'email']]; - $expected = ['id', 'name', 'email']; + $listing = [ + ['name' => 'id', 'type_name' => 'int4', 'type' => 'integer', 'collation' => '', 'nullable' => true, 'default' => "nextval('employee_id_seq'::regclass)", 'comment' => ''], + ['name' => 'name', 'type_name' => 'varchar', 'type' => 'character varying(100)', 'collation' => 'collate', 'nullable' => false, 'default' => '', 'comment' => 'foo'], + ['name' => 'balance', 'type_name' => 'numeric', 'type' => 'numeric(8,2)', 'collation' => '', 'nullable' => true, 'default' => '4', 'comment' => 'NULL'], + ['name' => 'birth_date', 'type_name' => 'timestamp', 'type' => 'timestamp(6) without time zone', 'collation' => '', 'nullable' => false, 'default' => '', 'comment' => ''], + ]; + $expected = [ + ['name' => 'id', 'type_name' => 'int4', 'type' => 'integer', 'collation' => '', 'nullable' => true, 'default' => null, 'auto_increment' => true, 'comment' => ''], + ['name' => 'name', 'type_name' => 'varchar', 'type' => 'character varying(100)', 'collation' => 'collate', 'nullable' => false, 'default' => '', 'auto_increment' => false, 'comment' => 'foo'], + ['name' => 'balance', 'type_name' => 'numeric', 'type' => 'numeric(8,2)', 'collation' => '', 'nullable' => true, 'default' => '4', 'auto_increment' => false, 'comment' => 'NULL'], + ['name' => 'birth_date', 'type_name' => 'timestamp', 'type' => 'timestamp(6) without time zone', 'collation' => '', 'nullable' => false, 'default' => '', 'auto_increment' => false, 'comment' => ''], + ]; - $this->assertEquals($expected, $processor->processColumnListing($listing)); + $this->assertEquals($expected, $processor->processColumns($listing)); // convert listing to objects to simulate PDO::FETCH_CLASS foreach ($listing as &$row) { $row = (object) $row; } - $this->assertEquals($expected, $processor->processColumnListing($listing)); + $this->assertEquals($expected, $processor->processColumns($listing)); } } diff --git a/tests/Database/DatabasePostgresSchemaBuilderTest.php b/tests/Database/DatabasePostgresSchemaBuilderTest.php index 7d917c36fe6b..448b41138a84 100755 --- a/tests/Database/DatabasePostgresSchemaBuilderTest.php +++ b/tests/Database/DatabasePostgresSchemaBuilderTest.php @@ -44,11 +44,11 @@ public function testGetColumnListing() $connection->shouldReceive('getConfig')->with('database')->andReturn('db'); $connection->shouldReceive('getConfig')->with('schema')->andReturn('schema'); $connection->shouldReceive('getConfig')->with('search_path')->andReturn('public'); - $grammar->shouldReceive('compileColumnListing')->once()->andReturn('sql'); - $processor->shouldReceive('processColumnListing')->once()->andReturn(['column']); + $grammar->shouldReceive('compileColumns')->with('db', 'public', 'prefix_table')->once()->andReturn('sql'); + $processor->shouldReceive('processColumns')->once()->andReturn([['name' => 'column']]); $builder = new PostgresBuilder($connection); $connection->shouldReceive('getTablePrefix')->once()->andReturn('prefix_'); - $connection->shouldReceive('selectFromWriteConnection')->once()->with('sql', ['db', 'public', 'prefix_table'])->andReturn(['column']); + $connection->shouldReceive('selectFromWriteConnection')->once()->with('sql')->andReturn([['name' => 'column']]); $this->assertEquals(['column'], $builder->getColumnListing('table')); } diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 8ff15e2e14a7..db139e523280 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -1186,11 +1186,11 @@ public function testCompileTableExists() $this->assertSame('select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = \'BASE TABLE\'', $statement); } - public function testCompileColumnListing() + public function testCompileColumns() { - $statement = $this->getGrammar()->compileColumnListing(); + $statement = $this->getGrammar()->compileColumns('db', 'public', 'table'); - $this->assertSame('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', $statement); + $this->assertStringContainsString("where c.relname = 'table' and n.nspname = 'public'", $statement); } protected function getConnection() diff --git a/tests/Database/DatabaseSQLiteProcessorTest.php b/tests/Database/DatabaseSQLiteProcessorTest.php index 8ab4b2b6520e..4aeb4438f7d7 100644 --- a/tests/Database/DatabaseSQLiteProcessorTest.php +++ b/tests/Database/DatabaseSQLiteProcessorTest.php @@ -7,20 +7,28 @@ class DatabaseSQLiteProcessorTest extends TestCase { - public function testProcessColumnListing() + public function testProcessColumns() { $processor = new SQLiteProcessor; - $listing = [['name' => 'id'], ['name' => 'name'], ['name' => 'email']]; - $expected = ['id', 'name', 'email']; + $listing = [ + ['name' => 'id', 'type' => 'INTEGER', 'nullable' => '0', 'default' => '', 'primary' => '1'], + ['name' => 'name', 'type' => 'varchar', 'nullable' => '1', 'default' => 'foo', 'primary' => '0'], + ['name' => 'is_active', 'type' => 'tinyint(1)', 'nullable' => '0', 'default' => '1', 'primary' => '0'], + ]; + $expected = [ + ['name' => 'id', 'type_name' => 'integer', 'type' => 'integer', 'collation' => null, 'nullable' => false, 'default' => '', 'auto_increment' => true, 'comment' => null], + ['name' => 'name', 'type_name' => 'varchar', 'type' => 'varchar', 'collation' => null, 'nullable' => true, 'default' => 'foo', 'auto_increment' => false, 'comment' => null], + ['name' => 'is_active', 'type_name' => 'tinyint', 'type' => 'tinyint(1)', 'collation' => null, 'nullable' => false, 'default' => '1', 'auto_increment' => false, 'comment' => null], + ]; - $this->assertEquals($expected, $processor->processColumnListing($listing)); + $this->assertEquals($expected, $processor->processColumns($listing)); // convert listing to objects to simulate PDO::FETCH_CLASS foreach ($listing as &$row) { $row = (object) $row; } - $this->assertEquals($expected, $processor->processColumnListing($listing)); + $this->assertEquals($expected, $processor->processColumns($listing)); } } diff --git a/tests/Database/DatabaseSchemaBuilderTest.php b/tests/Database/DatabaseSchemaBuilderTest.php index b22bfd7dc70e..2fdd98e1225b 100755 --- a/tests/Database/DatabaseSchemaBuilderTest.php +++ b/tests/Database/DatabaseSchemaBuilderTest.php @@ -77,6 +77,7 @@ public function testGetColumnTypeAddsPrefix() $builder = new Builder($connection); $connection->shouldReceive('getTablePrefix')->once()->andReturn('prefix_'); $connection->shouldReceive('getDoctrineColumn')->once()->with('prefix_users', 'id')->andReturn($column); + $connection->shouldReceive('usingNativeSchemaOperations')->once()->andReturn(false); $column->shouldReceive('getType')->once()->andReturn($type); $type->shouldReceive('getName')->once()->andReturn('integer');