MOON
Server: Apache
System: Linux server1.studioinfinity.com.br 2.6.32-954.3.5.lve1.4.90.el6.x86_64 #1 SMP Tue Feb 21 12:26:30 UTC 2023 x86_64
User: artinside (517)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/artinside/sites.artinside.com.br/mainpro/vendor/aplus/database/src/Definition/CreateTable.php
<?php declare(strict_types=1);
/*
 * This file is part of Aplus Framework Database Library.
 *
 * (c) Natan Felles <natanfelles@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Framework\Database\Definition;

use Framework\Database\Definition\Table\TableDefinition;
use Framework\Database\Definition\Table\TableStatement;
use LogicException;

/**
 * Class CreateTable.
 *
 * @see https://mariadb.com/kb/en/create-table/
 *
 * @package database
 */
class CreateTable extends TableStatement
{
    /**
     * Adds a OR REPLACE part.
     *
     * WARNING: This feature is MariaDB only. It is not compatible with MySQL.
     *
     * @return static
     */
    public function orReplace() : static
    {
        $this->sql['or_replace'] = true;
        return $this;
    }

    protected function renderOrReplace() : ?string
    {
        if (!isset($this->sql['or_replace'])) {
            return null;
        }
        return ' OR REPLACE';
    }

    /**
     * @return static
     */
    public function temporary() : static
    {
        $this->sql['temporary'] = true;
        return $this;
    }

    protected function renderTemporary() : ?string
    {
        if (!isset($this->sql['temporary'])) {
            return null;
        }
        return ' TEMPORARY';
    }

    /**
     * @return static
     */
    public function ifNotExists() : static
    {
        $this->sql['if_not_exists'] = true;
        return $this;
    }

    protected function renderIfNotExists() : ?string
    {
        if (!isset($this->sql['if_not_exists'])) {
            return null;
        }
        if (isset($this->sql['or_replace'])) {
            throw new LogicException(
                'Clauses OR REPLACE and IF NOT EXISTS can not be used together'
            );
        }
        return ' IF NOT EXISTS';
    }

    /**
     * @param string $tableName
     *
     * @return static
     */
    public function table(string $tableName) : static
    {
        $this->sql['table'] = $tableName;
        return $this;
    }

    protected function renderTable() : string
    {
        if (isset($this->sql['table'])) {
            return ' ' . $this->database->protectIdentifier($this->sql['table']);
        }
        throw new LogicException('TABLE name must be set');
    }

    /**
     * @param callable $definition
     *
     * @return static
     */
    public function definition(callable $definition) : static
    {
        $this->sql['definition'] = $definition;
        return $this;
    }

    protected function renderDefinition() : string
    {
        if (!isset($this->sql['definition'])) {
            throw new LogicException('Table definition must be set');
        }
        $definition = new TableDefinition($this->database);
        $this->sql['definition']($definition);
        return $definition->sql();
    }

    public function sql() : string
    {
        $sql = 'CREATE' . $this->renderOrReplace() . $this->renderTemporary();
        $sql .= ' TABLE' . $this->renderIfNotExists();
        $sql .= $this->renderTable() . ' (' . \PHP_EOL;
        $sql .= $this->renderDefinition() . \PHP_EOL;
        $sql .= ')' . $this->renderOptions();
        return $sql;
    }

    /**
     * Runs the CREATE TABLE statement.
     *
     * @return int|string The number of affected rows
     */
    public function run() : int | string
    {
        return $this->database->exec($this->sql());
    }
}