PHPackages                             pinahq/sql-parser - 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. pinahq/sql-parser

ActiveLibrary

pinahq/sql-parser
=================

MySQL schema parser

0.3.1(6mo ago)04761MITPHP

Since Sep 10Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/pinahq/SQLParser)[ Packagist](https://packagist.org/packages/pinahq/sql-parser)[ RSS](/packages/pinahq-sql-parser/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (2)Versions (6)Used By (1)

SQLParser - Parse MySQL schemas in PHP, fast
============================================

[](#sqlparser---parse-mysql-schemas-in-php-fast)

[![Build Status](https://camo.githubusercontent.com/042fc8f7f19d4d4946b7fce3ac38957bd2ef01ea5491b023eda27428d31afd1d/68747470733a2f2f7472617669732d63692e6f72672f69616d63616c2f53514c5061727365722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/iamcal/SQLParser)[![Coverage Status](https://camo.githubusercontent.com/7877aad6e0d001bddd50950a6843640d77751a74aa4d8f02e2da887d751b78cb/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f69616d63616c2f53514c5061727365722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/iamcal/SQLParser?branch=master)

This library takes MySQL `CREATE TABLE` statements and returns a data structure representing the table that it defines. MySQL syntax [version 5.7](https://dev.mysql.com/doc/refman/5.7/en/create-table.html) is supported. This library does not try to validate input - the goal is to deconstruct valid `CREATE TABLE` statements.

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

[](#installation)

You can install this package using composer. To add it to your `composer.json`:

```
composer require iamcal/sql-parser

```

You can then load it using the composer autoloader:

```
require_once 'vendor/autoload.php';
use iamcal\SQLParser;

$parser = new SQLParser();

```

If you don't use composer, you can skip the autoloader and include `src/SQLParser.php` directly.

Usage
-----

[](#usage)

To extract the tables defined in SQL:

```
$parser = new SQLParser();
$parser->parse($sql);

print_r($parser->tables);

```

The `tables` property is an array of tables, each of which is a nested array structure defining the table's structure:

```
CREATE TABLE `achievements_counts` (
  `achievement_id` int(10) unsigned NOT NULL,
  `num_players` int(10) unsigned NOT NULL,
  `date_updated` int(10) unsigned NOT NULL,
  PRIMARY KEY (`achievement_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

[
	'achievements_counts' => [
		'name' => 'achievements_counts',
		'fields' => [
			[
				'name' => 'achievement_id',
				'type' => 'INT',
				'length' => '10',
				'unsigned' => true,
				'null' => false,
			],
			[
				'name' => 'num_players',
				'type' => 'INT',
				'length' => '10',
				'unsigned' => true,
				'null' => false,
			],
			[
				'name' => 'date_updated',
				'type' => 'INT',
				'length' => '10',
				'unsigned' => true,
				'null' => false,
			],
		],
		'indexes' => [
			[
				'type' => 'PRIMARY',
				'cols' => [
					[
						'name' => 'achievement_id',
					],
				],
			],
		],
		'props' => [
			'ENGINE' => 'InnoDB',
			'CHARSET' => 'utf8',
		],
	],
]

```

You can also use the lexer directly to work with other piece of SQL:

```
$parser = new SQLParser();
$parser->lex($sql);

print($parser->tokens);

```

The `tokens` property contains an array of tokens. SQL keywords are returned as uppercase, with multi-word terms (e.g. `DEFAULT CHARACTER SET`) as a single token. Strings and escaped identifiers are not further processed; they are returned exactly as expressed in the input SQL.

Performance
-----------

[](#performance)

My test target is an 88K SQL file containing 114 tables from Glitch's main database.

The first version, using [php-sql-parser](http://code.google.com/p/php-sql-parser/), took over 60 seconds just to lex the input. This was obviously not a great option.

The current implementation uses a hand-written lexer which takes around 140ms to lex the same input and imposes less odd restrictions. This seems to be the way to go.

History
-------

[](#history)

This library was created to parse multiple `CREATE TABLE` schemas and compare them, so figure out what needs to be done to migrate one to the other.

This is based on the system used at b3ta, Flickr and then Tiny Speck to check the differences between production and development databases and between shard instances. The original system just showed a diff (see [SchemaDiff](https://github.com/iamcal/SchemaDiff)), but that was a bit of a pain.

Unsupported features
--------------------

[](#unsupported-features)

MySQL table definitions have a *lot* of options, so some things just aren't supported. They include:

- `UNION` table properties
- `TABLESPACE` table properties
- table partitions
- Spatial field types

If you need support for one of these features, open an issue or (better) send a pull request with tests.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance66

Regular maintenance activity

Popularity15

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.9% 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 ~989 days

Total

4

Last Release

198d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a1ed878b0d36cc3da38b5748f2eb7ca26a1934d17f82c2d0dc4c6052d86b1a31?d=identicon)[alex-yashin](/maintainers/alex-yashin)

---

Top Contributors

[![iamcal](https://avatars.githubusercontent.com/u/173750?v=4)](https://github.com/iamcal "iamcal (92 commits)")[![alex-yashin](https://avatars.githubusercontent.com/u/521141?v=4)](https://github.com/alex-yashin "alex-yashin (4 commits)")[![demmer](https://avatars.githubusercontent.com/u/3308504?v=4)](https://github.com/demmer "demmer (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pinahq-sql-parser/health.svg)

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

PHPackages © 2026

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