master

laravel/framework

Last updated at: 29/12/2023 09:22

MySqlBuilder.php

TLDR

The MySqlBuilder.php file is a PHP file that contains a class MySqlBuilder which extends another class Builder within the Illuminate\Database\Schema namespace. The class contains methods for creating and dropping databases, retrieving tables, views, columns, indexes, and foreign keys, as well as dropping all tables and views from the database.

Methods

createDatabase($name)

This method creates a database in the schema. It takes a string parameter $name which represents the name of the database. It returns a boolean value indicating whether the database creation was successful or not.

dropDatabaseIfExists($name)

This method drops a database from the schema if the database exists. It takes a string parameter $name which represents the name of the database. It returns a boolean value indicating whether the database drop was successful or not.

getTables()

This method retrieves the tables for the database. It returns an array of table names.

getViews()

This method retrieves the views for the database. It returns an array of view names.

getAllTables()

This method retrieves all table names for the database. Note: This method is deprecated and will be removed in a future Laravel version.

getAllViews()

This method retrieves all view names for the database. Note: This method is deprecated and will be removed in a future Laravel version.

getColumns($table)

This method retrieves the columns for a given table. It takes a string parameter $table which represents the table name. It returns an array of column names.

getIndexes($table)

This method retrieves the indexes for a given table. It takes a string parameter $table which represents the table name. It returns an array of indexes.

getForeignKeys($table)

This method retrieves the foreign keys for a given table. It takes a string parameter $table which represents the table name. It returns an array of foreign keys.

dropAllTables()

This method drops all tables from the database. It does not return any value.

dropAllViews()

This method drops all views from the database. It does not return any value.

Classes

There are no additional classes in this file.

<?php

namespace Illuminate\Database\Schema;

class MySqlBuilder extends Builder
{
    /**
     * Create a database in the schema.
     *
     * @param  string  $name
     * @return bool
     */
    public function createDatabase($name)
    {
        return $this->connection->statement(
            $this->grammar->compileCreateDatabase($name, $this->connection)
        );
    }

    /**
     * Drop a database from the schema if the database exists.
     *
     * @param  string  $name
     * @return bool
     */
    public function dropDatabaseIfExists($name)
    {
        return $this->connection->statement(
            $this->grammar->compileDropDatabaseIfExists($name)
        );
    }

    /**
     * Get the tables for the database.
     *
     * @return array
     */
    public function getTables()
    {
        return $this->connection->getPostProcessor()->processTables(
            $this->connection->selectFromWriteConnection(
                $this->grammar->compileTables($this->connection->getDatabaseName())
            )
        );
    }

    /**
     * Get the views for the database.
     *
     * @return array
     */
    public function getViews()
    {
        return $this->connection->getPostProcessor()->processViews(
            $this->connection->selectFromWriteConnection(
                $this->grammar->compileViews($this->connection->getDatabaseName())
            )
        );
    }

    /**
     * Get all of the table names for the database.
     *
     * @deprecated Will be removed in a future Laravel version.
     *
     * @return array
     */
    public function getAllTables()
    {
        return $this->connection->select(
            $this->grammar->compileGetAllTables()
        );
    }

    /**
     * Get all of the view names for the database.
     *
     * @deprecated Will be removed in a future Laravel version.
     *
     * @return array
     */
    public function getAllViews()
    {
        return $this->connection->select(
            $this->grammar->compileGetAllViews()
        );
    }

    /**
     * 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($this->connection->getDatabaseName(), $table)
        );

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

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

        return $this->connection->getPostProcessor()->processIndexes(
            $this->connection->selectFromWriteConnection(
                $this->grammar->compileIndexes($this->connection->getDatabaseName(), $table)
            )
        );
    }

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

        return $this->connection->getPostProcessor()->processForeignKeys(
            $this->connection->selectFromWriteConnection(
                $this->grammar->compileForeignKeys($this->connection->getDatabaseName(), $table)
            )
        );
    }

    /**
     * Drop all tables from the database.
     *
     * @return void
     */
    public function dropAllTables()
    {
        $tables = array_column($this->getTables(), 'name');

        if (empty($tables)) {
            return;
        }

        $this->disableForeignKeyConstraints();

        $this->connection->statement(
            $this->grammar->compileDropAllTables($tables)
        );

        $this->enableForeignKeyConstraints();
    }

    /**
     * Drop all views from the database.
     *
     * @return void
     */
    public function dropAllViews()
    {
        $views = array_column($this->getViews(), 'name');

        if (empty($views)) {
            return;
        }

        $this->connection->statement(
            $this->grammar->compileDropAllViews($views)
        );
    }
}