master

laravel/framework

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

SQLiteBuilder.php

TLDR

This file is located at src/Illuminate/Database/Schema/SQLiteBuilder.php and contains the SQLiteBuilder class. This class extends the Builder class and provides methods for creating and dropping databases, retrieving tables and views, and performing other actions related to SQLite databases.

Methods

createDatabase

This method creates a database in the schema. It takes a string parameter $name as the name of the database, and returns a boolean value to indicate if the creation was successful.

dropDatabaseIfExists

This method drops a database from the schema if the database exists. It takes a string parameter $name as the name of the database, and returns a boolean value to indicate if the deletion was successful.

getTables

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

getAllTables

This method (deprecated) returns an array of all table names for the database.

getAllViews

This method (deprecated) returns an array of all view names for the database.

dropAllTables

This method drops all tables from the database.

dropAllViews

This method drops all views from the database.

refreshDatabaseFile

This method empties the database file.

Classes

SQLiteBuilder

This class extends the Builder class and provides methods for creating and dropping databases, retrieving tables and views, and performing other actions related to SQLite databases.

<?php

namespace Illuminate\Database\Schema;

use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\File;

class SQLiteBuilder extends Builder
{
    /**
     * Create a database in the schema.
     *
     * @param  string  $name
     * @return bool
     */
    public function createDatabase($name)
    {
        return File::put($name, '') !== false;
    }

    /**
     * Drop a database from the schema if the database exists.
     *
     * @param  string  $name
     * @return bool
     */
    public function dropDatabaseIfExists($name)
    {
        return File::exists($name)
            ? File::delete($name)
            : true;
    }

    /**
     * Get the tables for the database.
     *
     * @return array
     */
    public function getTables()
    {
        $withSize = false;

        try {
            $withSize = $this->connection->scalar($this->grammar->compileDbstatExists());
        } catch (QueryException $e) {
            //
        }

        return $this->connection->getPostProcessor()->processTables(
            $this->connection->selectFromWriteConnection($this->grammar->compileTables($withSize))
        );
    }

    /**
     * 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()
        );
    }

    /**
     * Drop all tables from the database.
     *
     * @return void
     */
    public function dropAllTables()
    {
        if ($this->connection->getDatabaseName() !== ':memory:') {
            return $this->refreshDatabaseFile();
        }

        $this->connection->select($this->grammar->compileEnableWriteableSchema());

        $this->connection->select($this->grammar->compileDropAllTables());

        $this->connection->select($this->grammar->compileDisableWriteableSchema());

        $this->connection->select($this->grammar->compileRebuild());
    }

    /**
     * Drop all views from the database.
     *
     * @return void
     */
    public function dropAllViews()
    {
        $this->connection->select($this->grammar->compileEnableWriteableSchema());

        $this->connection->select($this->grammar->compileDropAllViews());

        $this->connection->select($this->grammar->compileDisableWriteableSchema());

        $this->connection->select($this->grammar->compileRebuild());
    }

    /**
     * Empty the database file.
     *
     * @return void
     */
    public function refreshDatabaseFile()
    {
        file_put_contents($this->connection->getDatabaseName(), '');
    }
}