PHPackages                             czproject/sql-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. czproject/sql-generator

ActiveLibrary[Database &amp; ORM](/categories/database)

czproject/sql-generator
=======================

SQL generator.

v2.3.1(1y ago)1016.2k↓68.8%5[1 PRs](https://github.com/czproject/sql-generator/pulls)1BSD-3-ClausePHPPHP 8.0 - 8.4CI failing

Since Jun 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/czproject/sql-generator)[ Packagist](https://packagist.org/packages/czproject/sql-generator)[ Fund](https://www.janpecha.cz/donate/)[ RSS](/packages/czproject-sql-generator/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (1)Versions (17)Used By (1)

CzProject\\SqlGenerator
=======================

[](#czprojectsqlgenerator)

[![Build Status](https://github.com/czproject/sql-generator/workflows/Build/badge.svg)](https://github.com/czproject/sql-generator/actions)[![Downloads this Month](https://camo.githubusercontent.com/6672d69c972736038963ae5f8f977978de2630caa4235349ce4667beadad549a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f637a70726f6a6563742f73716c2d67656e657261746f722e737667)](https://packagist.org/packages/czproject/sql-generator)[![Latest Stable Version](https://camo.githubusercontent.com/93da22caef1c4a4fff48007364492e164675e39cce0eb99d172b3b7f82deabbf/68747470733a2f2f706f7365722e707567782e6f72672f637a70726f6a6563742f73716c2d67656e657261746f722f762f737461626c65)](https://github.com/czproject/sql-generator/releases)[![License](https://camo.githubusercontent.com/fa7d5fcf2c84b580327af52da95dd751703af65f079dc3c5a0081beac0789718/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4e65772532304253442d626c75652e737667)](https://github.com/czproject/sql-generator/blob/master/license.md)

[![Donate](https://camo.githubusercontent.com/101b981194f1dafbf9c42e19c3034fe2d724e75be972cef0f4477074997834db/68747470733a2f2f6275796d65636f666665652e696e746d2e6f72672f696d672f646f6e6174652d62616e6e65722e76312e737667)](https://www.janpecha.cz/donate/)

Installation
------------

[](#installation)

[Download a latest package](https://github.com/czproject/sql-generator/releases) or use [Composer](http://getcomposer.org/):

```
composer require czproject/sql-generator

```

`CzProject\SqlGenerator` requires PHP 8.0 or later.

Usage
-----

[](#usage)

```
use CzProject\SqlGenerator\Drivers;
use CzProject\SqlGenerator\SqlDocument;

$sql = new SqlDocument;
$driver = new Drivers\MysqlDriver;

$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();

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

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

$output = $sql->toSql($driver);
```

Outputs:

```
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
----------

[](#statements)

There is few predefined statements:

```
$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:

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

Check if is SQL document empty:

```
$sql->isEmpty();
```

Generate SQL:

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

Special Values
--------------

[](#special-values)

There are value objects for specific cases:

### TableName

[](#tablename)

Delimited table name.

```
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

[](#value)

Scalar/stringable/datetime value. It can be used in option values.

```
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
------------------

[](#supported-database)

Currently is supported common SQL and MySQL.

---

License: [New BSD License](license.md)
Author: Jan Pecha,

###  Health Score

50

—

FairBetter than 95% of packages

Maintenance48

Moderate activity, may be stable

Popularity31

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity89

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~194 days

Recently: every ~375 days

Total

16

Last Release

380d ago

Major Versions

v1.3.0 → v2.0.02021-01-06

PHP version history (3 changes)v1.0.0PHP &gt;=5.4.0

v2.0.0PHP &gt;=5.6.0

v2.3.0PHP 8.0 - 8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c980b1511b4a0350442dc23d89c99d4d9a2411b7e765d52c133ccacf616968b?d=identicon)[janpecha](/maintainers/janpecha)

---

Top Contributors

[![janpecha](https://avatars.githubusercontent.com/u/637719?v=4)](https://github.com/janpecha "janpecha (51 commits)")

---

Tags

phpsqlsql-generation

### Embed Badge

![Health badge](/badges/czproject-sql-generator/health.svg)

```
[![Health](https://phpackages.com/badges/czproject-sql-generator/health.svg)](https://phpackages.com/packages/czproject-sql-generator)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[pgvector/pgvector

pgvector support for PHP

198741.5k12](/packages/pgvector-pgvector)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
