Skip to content

Commit

Permalink
Improvements core (#5)
Browse files Browse the repository at this point in the history
* fix: Fix dependencies

* fix: Remove use of collections in favor of native array methods

* feat: Introduce abstract Schema class for extending

* fix: Update error message when using unsupport connection. Use UnsupportSchema as fallback schema.

Co-authored-by: Owen Conti <[email protected]>
  • Loading branch information
dinhquochan and owenconti committed May 2, 2020
1 parent 6bd7864 commit 6bb9803
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 44 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
],
"require": {
"php": "^7.1",
"illuminate/support": ">=7.0",
"illuminate/console": ">=7.0",
"illuminate/database": ">=7.0",
"squizlabs/php_codesniffer": "*"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/ListColumnsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class ListColumnsCommand extends Command
/** @var string */
protected $description = 'Lists the columns in the given table.';

public function handle(ConnectionResolverInterface $connections, SchemaContract $schema)
public function handle(SchemaContract $schema)
{
$headers = ['Field', 'Type', 'Nullable', 'Key', 'Default Value', 'Extra'];
$rows = $schema->getColumns($connections->connection(), $this->argument('table'));
$rows = $schema->getColumns($this->argument('table'));

$this->table($headers, $rows);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/ListTablesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace OhSeeSoftware\LaravelSchemaList\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\ConnectionResolverInterface;
use OhSeeSoftware\LaravelSchemaList\Schemas\SchemaContract;

class ListTablesCommand extends Command
Expand All @@ -14,10 +13,10 @@ class ListTablesCommand extends Command
/** @var string */
protected $description = 'Lists the tables in the default database.';

public function handle(ConnectionResolverInterface $connections, SchemaContract $schema)
public function handle(SchemaContract $schema)
{
$headers = ['Tables'];
$rows = $schema->getTables($connections->connection());
$rows = $schema->getTables();

$this->table($headers, $rows);
}
Expand Down
15 changes: 7 additions & 8 deletions src/LaravelSchemaListServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OhSeeSoftware\LaravelSchemaList\Schemas\MySQLSchema;
use OhSeeSoftware\LaravelSchemaList\Schemas\PostgresSchema;
use OhSeeSoftware\LaravelSchemaList\Schemas\SchemaContract;
use OhSeeSoftware\LaravelSchemaList\Schemas\UnsupportedSchema;

class LaravelSchemaListServiceProvider extends ServiceProvider
{
Expand All @@ -21,23 +22,21 @@ class LaravelSchemaListServiceProvider extends ServiceProvider
public function boot()
{
if ($this->app->runningInConsole()) {
$this->app->bind('schema.list:tables', ListTablesCommand::class);
$this->app->bind('schema.list:columns', ListColumnsCommand::class);

$this->commands([
'schema.list:tables',
'schema.list:columns'
ListTablesCommand::class,
ListColumnsCommand::class
]);

$this->app->bind(SchemaContract::class, function () {
$connection = resolve(ConnectionInterface::class);

if ($connection instanceof MySqlConnection) {
return new MySQLSchema;
return new MySQLSchema($connection);
} elseif ($connection instanceof PostgresConnection) {
return new PostgresSchema;
return new PostgresSchema($connection);
}

throw new Exception('Connection type is not supported!');
return new UnsupportedSchema($connection);
});
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/Schemas/MySQLSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Facades\DB;

class MySQLSchema implements SchemaContract
class MySQLSchema extends Schema
{
public function getTables(ConnectionInterface $connection): array
public function getTables(): array
{
$output = $connection->select(DB::raw('SHOW TABLES'));
$output = $this->connection->select(DB::raw('SHOW TABLES'));

return collect($output)->values()->map(function ($value) {
return array_values((array)$value);
})->toArray();
return array_map(function ($value) {
return array_values((array) $value);
}, $output);
}

public function getColumns(ConnectionInterface $connection, string $table): array
public function getColumns(string $table): array
{
$output = $connection->select("DESCRIBE {$table}");
$output = $this->connection->select("DESCRIBE {$table}");

return collect($output)->map(function ($value) {
return array_map(function ($value) {
return array_values((array) $value);
})->toArray();
}, $output);
}
}
34 changes: 18 additions & 16 deletions src/Schemas/PostgresSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

namespace OhSeeSoftware\LaravelSchemaList\Schemas;

use Illuminate\Database\ConnectionInterface;

class PostgresSchema implements SchemaContract
class PostgresSchema extends Schema
{
public function getTables(ConnectionInterface $connection): array
public function getTables(): array
{
$output = $connection->table('pg_catalog.pg_tables')
->select('tablename')
->whereNotIn('schemaname', ['pg_catalog', 'information_schema'])
->get();
$output = $this->connection
->table('pg_catalog.pg_tables')
->select('tablename')
->whereNotIn('schemaname', ['pg_catalog', 'information_schema'])
->get()
->toArray();

return collect($output)->values()->map(function ($value) {
return array_values((array)$value);
})->toArray();
return array_map(function ($value) {
return array_values((array) $value);
}, $output);
}

public function getColumns(ConnectionInterface $connection, string $table): array
public function getColumns(string $table): array
{
$output = $connection->table('information_schema.columns', 'c')
$output = $this->connection
->table('information_schema.columns', 'c')
->select([
'c.column_name as Field',
'c.data_type as Type',
Expand All @@ -45,11 +46,12 @@ public function getColumns(ConnectionInterface $connection, string $table): arra
->where('c.table_name', $table)
->orderBy('c.ordinal_position', 'asc')
->distinct()
->get();
->get()
->toArray();

return collect($output)->map(function ($value) {
return array_map(function ($value) {
unset($value->ordinal_position);
return array_values((array) $value);
})->toArray();
}, $output);
}
}
27 changes: 27 additions & 0 deletions src/Schemas/Schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace OhSeeSoftware\LaravelSchemaList\Schemas;

use Illuminate\Database\ConnectionInterface;
use LogicException;

abstract class Schema implements SchemaContract
{
/** @var \Illuminate\Database\ConnectionInterface */
protected $connection;

public function __construct(ConnectionInterface $connection)
{
$this->connection = $connection;
}

public function getTables(): array
{
throw new LogicException('This database driver does not support listing schema tables.');
}

public function getColumns(string $table): array
{
throw new LogicException('This database driver does not support listing columns of a table.');
}
}
6 changes: 2 additions & 4 deletions src/Schemas/SchemaContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace OhSeeSoftware\LaravelSchemaList\Schemas;

use Illuminate\Database\ConnectionInterface;

interface SchemaContract
{
public function getTables(ConnectionInterface $connection): array;
public function getTables(): array;

public function getColumns(ConnectionInterface $connection, string $table): array;
public function getColumns(string $table): array;
}
7 changes: 7 additions & 0 deletions src/Schemas/UnsupportedSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace OhSeeSoftware\LaravelSchemaList\Schemas;

class UnsupportedSchema extends Schema
{
}

0 comments on commit 6bb9803

Please sign in to comment.