2017 © Pedro Peláez
 

library sql-generator

SQL generator.

image

czproject/sql-generator

SQL generator.

  • Sunday, June 24, 2018
  • by janpecha
  • Repository
  • 1 Watchers
  • 1 Stars
  • 369 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 10 Versions
  • 54 % Grown

The README.md

CzProject\SqlGenerator

Build Status Downloads this Month Latest Stable Version License, (*1)

Donate, (*2)

Installation

Download a latest package or use Composer:, (*3)

composer require czproject/sql-generator

CzProject\SqlGenerator requires PHP 8.0 or later., (*4)

Usage

``` php use CzProject\SqlGenerator\Drivers; use CzProject\SqlGenerator\SqlDocument;, (*5)

$sql = new SqlDocument; $driver = new Drivers\MysqlDriver;, (*6)

$contactTable = $sql->createTable('contact') ->setComment('Clients table.') ->setOption('ENGINE', 'InnoDB'); $contactTable->addColumn('id', 'INT', NULL, array('UNSIGNED' => NULL)) ->setAutoIncrement(); $contactTable->addColumn('name', 'VARCHAR(100)') ->setComment('Client name'); $contactTable->addColumn('surname', 'VARCHAR(100)'); $contactTable->addColumn('active', 'unsigned TINYINT') ->setDefaultValue(TRUE); $contactTable->addColumn('created', 'DATETIME'); $contactTable->addColumn('removed', 'DATETIME') ->setNullable();, (*7)

$contactTable->addIndex(NULL, IndexDefinition::TYPE_PRIMARY) ->addColumn('id');, (*8)

$contactTable->addIndex('name_surname', IndexDefinition::TYPE_UNIQUE) ->addColumn('name', 'ASC', 100) ->addColumn('surname', 'DESC', 100);, (*9)

$output = $sql->toSql($driver);, (*10)


Outputs: ``` sql CREATE TABLE `contact` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) NOT NULL COMMENT 'Client name', `surname` VARCHAR(100) NOT NULL, `active` TINYINT(1) UNSIGNED NOT NULL DEFAULT 1, `created` DATETIME NOT NULL, `removed` DATETIME NULL, PRIMARY KEY (`id`), UNIQUE KEY `name_surname` (`name` (100), `surname` (100) DESC) ) COMMENT 'Clients table.' ENGINE=InnoDB;

Statements

There is few predefined statements:, (*11)

$sql->createTable($tableName);
$sql->dropTable($tableName);
$sql->renameTable($old, $new);
$sql->alterTable($tableName);
$sql->insert($tableName, $data);
$sql->command($command); // for example $sql->command('SET NAMES "utf8"');
$sql->comment($comment);

You can add custom statements:, (*12)

$sql->addStatement(new Statements\CreateTable($tableName));

Check if is SQL document empty:, (*13)

$sql->isEmpty();

Generate SQL:, (*14)

$sql->toSql($driver); // returns string
$sql->getSqlQueries($driver); // returns string[]
$sql->save($file, $driver); // saves SQL into file

Special Values

There are value objects for specific cases:, (*15)

TableName

Delimited table name., (*16)

use CzProject\SqlGenerator\TableName;

$table = $sql->createTable(TableName::create('schema.table'))
$table->addForeignKey('fk_table_id', 'id', TableName::create('schema2.table2'), 'id');
// and more ($sql->renameTable(),...)

Value

Scalar/stringable/datetime value. It can be used in option values., (*17)

use CzProject\SqlGenerator\Value;

$table->setOption('AUTO_INCREMENT', Value::create(123)); // generates AUTO_INCREMENT=123
$table->setOption('CHECKSUM', Value::create(FALSE)); // generates CHECKSUM=0
$table->setOption('COMPRESSION', Value::create('NONE')); // generates COMPRESSION='NONE'

Supported database

Currently is supported common SQL and MySQL., (*18)


License: New BSD License
Author: Jan Pecha, https://www.janpecha.cz/, (*19)

The Versions

24/06 2018

dev-master

9999999-dev

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

06/06 2018

v1.2.3

1.2.3.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

05/06 2018

v1.2.2

1.2.2.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

04/06 2018

v1.2.1

1.2.1.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

24/04 2018

v1.2.0

1.2.0.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

30/12 2017

v1.1.1

1.1.1.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

18/09 2017

v1.1.0

1.1.0.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

08/08 2017

v1.0.2

1.0.2.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

08/08 2017

v1.0.1

1.0.1.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

28/06 2017

v1.0.0

1.0.0.0

SQL generator.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires