{"id":17089493,"url":"https://github.com/iamcal/sqlparser","last_synced_at":"2025-04-09T07:07:30.145Z","repository":{"id":6217903,"uuid":"7449202","full_name":"iamcal/SQLParser","owner":"iamcal","description":"Parse MySQL schemas in PHP, fast","archived":false,"fork":false,"pushed_at":"2024-03-22T22:46:43.000Z","size":169,"stargazers_count":58,"open_issues_count":2,"forks_count":19,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-23T20:15:17.838Z","etag":null,"topics":["autoloader","composer","database","lex","php","sql-parser"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iamcal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-01-04T23:49:17.000Z","updated_at":"2024-06-18T16:57:07.487Z","dependencies_parsed_at":"2024-03-22T23:35:57.832Z","dependency_job_id":"e62473dc-fd6b-477d-8792-2ea26a547901","html_url":"https://github.com/iamcal/SQLParser","commit_stats":{"total_commits":111,"total_committers":7,"mean_commits":"15.857142857142858","dds":"0.18018018018018023","last_synced_commit":"2c5eb8b229d4937a16a1bd17c5e2b137ce3b4597"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamcal%2FSQLParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamcal%2FSQLParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamcal%2FSQLParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamcal%2FSQLParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamcal","download_url":"https://codeload.github.com/iamcal/SQLParser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247994121,"owners_count":21030050,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autoloader","composer","database","lex","php","sql-parser"],"created_at":"2024-10-14T13:47:33.755Z","updated_at":"2025-04-09T07:07:30.126Z","avatar_url":"https://github.com/iamcal.png","language":"PHP","readme":"# SQLParser - Parse MySQL schemas in PHP, fast\n\n[![Build Status](https://github.com/iamcal/SQLParser/actions/workflows/php.yml/badge.svg)](https://github.com/iamcal/SQLParser/actions)\n[![Coverage Status](https://coveralls.io/repos/github/iamcal/SQLParser/badge.svg?branch=master)](https://coveralls.io/github/iamcal/SQLParser?branch=master)\n\nThis library takes MySQL `CREATE TABLE` statements and returns a data structure representing the table that it defines.\nMySQL syntax [version 5.7](https://dev.mysql.com/doc/refman/5.7/en/create-table.html) is supported.\nThis library does not try to validate input - the goal is to deconstruct valid `CREATE TABLE` statements.\n\n## Installation\n\nYou can install this package using composer. To add it to your `composer.json`:\n\n```plain\ncomposer require iamcal/sql-parser\n```\n\nYou can then load it using the composer autoloader:\n\n```php\nrequire_once 'vendor/autoload.php';\nuse iamcal\\SQLParser;\n\n$parser = new SQLParser();\n```\n\nIf you don't use composer, you can skip the autoloader and include `src/SQLParser.php` directly.\n\n## Usage\n\nTo extract the tables defined in SQL:\n\n```php\n$parser = new SQLParser();\n$parser-\u003eparse($sql);\n\nprint_r($parser-\u003etables);\n```\n\nThe `tables` property is an array of tables, each of which is a nested array structure defining the\ntable's structure:\n\n```SQL\nCREATE TABLE `achievements_counts` (\n  `achievement_id` int(10) unsigned NOT NULL,\n  `num_players` int(10) unsigned NOT NULL,\n  `date_updated` int(10) unsigned NOT NULL,\n  PRIMARY KEY (`achievement_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n```\n\n```php\n[\n    'achievements_counts' =\u003e [\n        'name' =\u003e 'achievements_counts',\n        'fields' =\u003e [\n            [\n                'name' =\u003e 'achievement_id',\n                'type' =\u003e 'INT',\n                'length' =\u003e '10',\n                'unsigned' =\u003e true,\n                'null' =\u003e false,\n            ],\n            [\n                'name' =\u003e 'num_players',\n                'type' =\u003e 'INT',\n                'length' =\u003e '10',\n                'unsigned' =\u003e true,\n                'null' =\u003e false,\n            ],\n            [\n                'name' =\u003e 'date_updated',\n                'type' =\u003e 'INT',\n                'length' =\u003e '10',\n                'unsigned' =\u003e true,\n                'null' =\u003e false,\n            ],\n        ],\n        'indexes' =\u003e [\n            [\n                'type' =\u003e 'PRIMARY',\n                'cols' =\u003e [\n                    [\n                        'name' =\u003e 'achievement_id',\n                    ],\n                ],\n            ],\n        ],\n        'props' =\u003e [\n            'ENGINE' =\u003e 'InnoDB',\n            'CHARSET' =\u003e 'utf8',\n        ],\n    ],\n]\n```\n\nYou can also use the lexer directly to work with other piece of SQL:\n\n```php\n$parser = new SQLParser();\n$parser-\u003elex($sql);\n\nprint($parser-\u003etokens);\n```\n\nThe `tokens` property contains an array of tokens. SQL keywords are returned as uppercase,\nwith multi-word terms (e.g. `DEFAULT CHARACTER SET`) as a single token. Strings and escaped\nidentifiers are not further processed; they are returned exactly as expressed in the input SQL.\n\nBy default, the tokenizer will ignore unterminated comments and strings, and stop parsing at\nthat point, producing no further tokens. You can set `$parser-\u003ethrow_on_bad_syntax = true;` to\nthrow an exception of type `iamcal\\SQLParserSyntaxException` instead.\n\n## Performance\n\nMy test target is an 88K SQL file containing 114 tables from Glitch's main database.\n\nThe first version, using [php-sql-parser](http://code.google.com/p/php-sql-parser/), took over 60\nseconds just to lex the input. This was obviously not a great option.\n\nThe current implementation uses a hand-written lexer which takes around 140ms to lex the same\ninput and imposes less odd restrictions. This seems to be the way to go.\n\n## History\n\nThis library was created to parse multiple `CREATE TABLE` schemas and compare them, so\nfigure out what needs to be done to migrate one to the other.\n\nThis is based on the system used at b3ta, Flickr and then Tiny Speck to check the differences\nbetween production and development databases and between shard instances. The original system\njust showed a diff (see [SchemaDiff](https://github.com/iamcal/SchemaDiff)), but that was a bit\nof a pain.\n\n## Unsupported features\n\nMySQL table definitions have a *lot* of options, so some things just aren't supported. They include:\n\n* `UNION` table properties\n* `TABLESPACE` table properties\n* table partitions\n* `FLOAT[(bits)]` fields\n* Deprecated `YEAR(2|4)` fields\n* `ASCII` attribute as a shorthand for `CHARACTER SET latin1`\n* `UNICODE` attribute as a shorthand for `CHARACTER SET ucs2`\n* `NATIONAL` modified for `CHAR` and `VARCHAR` fields\n\nIf you need support for one of these features, open an issue or (better) send a pull request with tests.\n\nThe specs for each of the four field groupings can be found here:\n\n* https://dev.mysql.com/doc/refman/5.7/en/numeric-type-syntax.html\n* https://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-syntax.html\n* https://dev.mysql.com/doc/refman/5.7/en/string-type-syntax.html\n* https://dev.mysql.com/doc/refman/5.7/en/spatial-type-overview.html\n\n## Alternatives\n\nIf you're using PHP, then [Modyllic](https://github.com/onlinebuddies/modyllic) is a great SQL parser and set of schema management tools.\n\nIf you're using Hack, then [Hack SQL Fake](https://github.com/slackhq/hack-sql-fake) allows you to parse SQL and create a fake MySQL\nserver for testing, with many (but not all!) features of MySQL.\n\n## Publishing\n\nTo publish a new version:\n\n* Commit changes to master\n* Add git tag\n* Go to https://packagist.org/packages/iamcal/sql-parser and hit \"Update\"\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamcal%2Fsqlparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamcal%2Fsqlparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamcal%2Fsqlparser/lists"}