diff --git a/src/Blueprint/BlueprintFactory.php b/src/Blueprint/BlueprintFactory.php new file mode 100644 index 00000000..e1d51bcb --- /dev/null +++ b/src/Blueprint/BlueprintFactory.php @@ -0,0 +1,80 @@ +laravelDatabaseManager = $databaseManager; + $this->config = $config; + $this->adapterFactory = $adapterFactory; + } + + /** + * @param $connectionName + * @return DatabaseBlueprint + */ + public function database($connectionName) + { + if (!empty($this->databaseBlueprints[$connectionName])) { + return $this->databaseBlueprints[$connectionName]; + } + + $connection = $this->laravelDatabaseManager->connection($connectionName); + + $databaseBlueprint = new DatabaseBlueprint( + $this->adapterFactory->database($connection), + $connectionName, + $connection + ); + + return $this->databaseBlueprints[$connectionName] = $databaseBlueprint; + } +} \ No newline at end of file diff --git a/src/Blueprint/DatabaseBlueprint.php b/src/Blueprint/DatabaseBlueprint.php new file mode 100644 index 00000000..7028353d --- /dev/null +++ b/src/Blueprint/DatabaseBlueprint.php @@ -0,0 +1,77 @@ +connectionName = $connectionName; + $this->connection = $connection; + $this->databaseAdapter = $databaseAdapter; + } + + /** + * If a schema name is not provided, then the default schema for the connection will be used + * @param string|null $schemaName + * @return SchemaBlueprint + */ + public function schema($schemaName) + { + if (!empty($this->schemaBlueprints[$schemaName])) { + return $this->schemaBlueprints[$schemaName]; + } + + return $this->schemaBlueprints[$schemaName] = new SchemaBlueprint( + $this, + $this->databaseAdapter->getSchema($schemaName), + $schemaName + ); + } + + # region Accessors + /** + * @return \Illuminate\Database\Connection + */ + public function getConnection() + { + return $this->connection; + } + # endregion Accessors +} \ No newline at end of file diff --git a/src/Blueprint/SchemaBlueprint.php b/src/Blueprint/SchemaBlueprint.php new file mode 100644 index 00000000..206f6e01 --- /dev/null +++ b/src/Blueprint/SchemaBlueprint.php @@ -0,0 +1,85 @@ +schemaAdapter = $schemaAdapter; + $this->schemaName = $schemaName; + $this->databaseBlueprint = $databaseBlueprint; + + $this->schemaManager = new SchemaManager( + $databaseBlueprint->getConnection() + ); + } + + public function table($tableName) + { + if (!empty($this->tableBlueprints[$tableName])) { + return $this->tableBlueprints[$tableName]; + } + + $blueprint = $this->schemaAdapter->table($tableName); + + return $this->tableBlueprints[$tableName] = new TableBlueprint( + $this, + $tableName, + $blueprint + ); + } + + public function schemaAdapter() + { + + } + + # region Accessors + + # endregion Accessors +} \ No newline at end of file diff --git a/src/Blueprint/TableBlueprint.php b/src/Blueprint/TableBlueprint.php new file mode 100644 index 00000000..50f5462b --- /dev/null +++ b/src/Blueprint/TableBlueprint.php @@ -0,0 +1,40 @@ +tableName = $tableName; + $this->schemaBlueprint = $schemaBlueprint; + $this->deprecatedTableBlueprint = $depricatedTableBlueprint; + } +} \ No newline at end of file diff --git a/src/Meta/AdapterFactory.php b/src/Meta/AdapterFactory.php new file mode 100644 index 00000000..7e1a474b --- /dev/null +++ b/src/Meta/AdapterFactory.php @@ -0,0 +1,43 @@ +connection = $connection; + } + + /** + * @inheritDoc + */ + public function getSchemaNames() + { + $schemas = $this->connection->getDoctrineSchemaManager()->listDatabases(); + + return array_diff($schemas, [ + 'information_schema', + 'sys', + 'mysql', + 'performance_schema', + ]); + } + + /** + * @param string $schemaName + * @return Schema + */ + public function getSchema($schemaName) + { + if (!empty($this->schemaAdapters[$schemaName])) { + return $this->schemaAdapters[$schemaName]; + } + return $this->schemaAdapters[$schemaName] = new Schema($schemaName, $this->connection); + } +} \ No newline at end of file diff --git a/src/Meta/MySql/Schema.php b/src/Meta/MySql/Schema.php index 186010f7..e8b755fc 100644 --- a/src/Meta/MySql/Schema.php +++ b/src/Meta/MySql/Schema.php @@ -265,19 +265,12 @@ protected function resolveForeignTable($table, Blueprint $blueprint) /** * @param \Illuminate\Database\Connection $connection - * + * @deprecated use \Reliese\Meta\MySql\Database::getSchemaNames instead * @return array */ public static function schemas(Connection $connection) { - $schemas = $connection->getDoctrineSchemaManager()->listDatabases(); - - return array_diff($schemas, [ - 'information_schema', - 'sys', - 'mysql', - 'performance_schema', - ]); + return (new Database($connection))->getSchemaNames(); } /** diff --git a/src/Meta/Postgres/Database.php b/src/Meta/Postgres/Database.php new file mode 100644 index 00000000..243e2004 --- /dev/null +++ b/src/Meta/Postgres/Database.php @@ -0,0 +1,58 @@ +connection = $connection; + } + + /** + * @inheritDoc + */ + public function getSchemaNames() + { + $schemas = $this->connection->getDoctrineSchemaManager()->listDatabases(); + + return array_diff($schemas, [ + 'postgres', + 'template0', + 'template1', + ]); + } + + /** + * @param string $schemaName + * @return Schema + */ + public function getSchema($schemaName) + { + if (!empty($this->schemaAdapters[$schemaName])) { + return $this->schemaAdapters[$schemaName]; + } + return $this->schemaAdapters[$schemaName] = new Schema($schemaName, $this->connection); + } +} \ No newline at end of file diff --git a/src/Meta/Postgres/Schema.php b/src/Meta/Postgres/Schema.php index abe2c455..6f81620f 100644 --- a/src/Meta/Postgres/Schema.php +++ b/src/Meta/Postgres/Schema.php @@ -268,18 +268,12 @@ protected function wrap($table) /** * @param \Illuminate\Database\Connection $connection - * + * @deprecated use \Reliese\Meta\Postgres\Database::getSchemaNames * @return array */ public static function schemas(Connection $connection) { - $schemas = $connection->getDoctrineSchemaManager()->listDatabases(); - - return array_diff($schemas, [ - 'postgres', - 'template0', - 'template1', - ]); + return (new Database($connection))->getSchemaNames(); } /** diff --git a/src/Meta/Sqlite/Database.php b/src/Meta/Sqlite/Database.php new file mode 100644 index 00000000..b4376cfe --- /dev/null +++ b/src/Meta/Sqlite/Database.php @@ -0,0 +1,50 @@ +connection = $connection; + } + + /** + * @inheritDoc + */ + public function getSchemaNames() + { + return ['database']; + } + + /** + * @param string $schemaName + * @return Schema + */ + public function getSchema($schemaName) + { + if (!empty($this->schemaAdapters[$schemaName])) { + return $this->schemaAdapters[$schemaName]; + } + return $this->schemaAdapters[$schemaName] = new Schema($schemaName, $this->connection); + } +} \ No newline at end of file diff --git a/src/Meta/Sqlite/Schema.php b/src/Meta/Sqlite/Schema.php index 7c611692..9f99e0c2 100644 --- a/src/Meta/Sqlite/Schema.php +++ b/src/Meta/Sqlite/Schema.php @@ -36,7 +36,7 @@ class Schema implements \Reliese\Meta\Schema * Mapper constructor. * * @param string $schema - * @param \Illuminate\Database\MySqlConnection $connection + * @param \Illuminate\Database\SQLiteConnection $connection */ public function __construct($schema, $connection) { @@ -194,12 +194,12 @@ protected function fillRelations(Blueprint $blueprint) /** * @param \Illuminate\Database\Connection $connection - * + * @deprecated use \Reliese\Meta\Sqlite\Database::getSchemaNames * @return array */ public static function schemas(Connection $connection) { - return ['database']; + return (new Database($connection))->getSchemaNames(); } /**