https://github.com/butschster/dbml-parser
DBML parser for PHP8. It's a PHP parser for DBML syntax.
https://github.com/butschster/dbml-parser
dbml parser parser-library php php8
Last synced: 16 days ago
JSON representation
DBML parser for PHP8. It's a PHP parser for DBML syntax.
- Host: GitHub
- URL: https://github.com/butschster/dbml-parser
- Owner: butschster
- License: mit
- Created: 2021-06-28T13:17:39.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-23T11:43:18.000Z (7 months ago)
- Last Synced: 2025-03-22T07:09:11.935Z (23 days ago)
- Topics: dbml, parser, parser-library, php, php8
- Language: PHP
- Homepage:
- Size: 104 KB
- Stars: 59
- Watchers: 3
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - butschster/dbml-parser - DBML parser for PHP8. It's a PHP parser for DBML syntax. (PHP)
README
# DBML parser written on PHP8
[](https://patreon.com/butschster)
[](https://packagist.org/packages/butschster/dbml-parser)
[](https://github.com/butschster/dbml-parser/actions/workflows/php.yml)
[](https://packagist.org/packages/butschster/dbml-parser)
[](https://packagist.org/packages/butschster/dbml-parser)
DBML (Database Markup Language) is a domain-specific language (DSL) designed to define database structures in a simple and readable format.
> **Note**
>
> This page has all the info on how it works. Check out https://www.dbml.org for more info.We were inspired by the cool service at https://dbdiagram.io/ to create this parser. It's super useful for visualizing databases and we thought it'd be great to use it to build schema and then turn it into an object tree for generating things like [CycleORM](https://cycle-orm.dev/) entity schemas, Laravel models, and migrations.
To parse DBML, we used a handy PHP package called https://phplrt.org.
## Features
- DBML parser
## Requirements
- PHP 8.0 and above
## Quick start
To install the package, run the following command from the root directory of your project:
```shell
composer require butschster/dbml-parser
```That's it!
## Usage
```php
use Butschster\Dbml\DbmlParserFactory;$parser = DbmlParserFactory::create();
$schema = $parser->parse(<< many-to-one; < one-to-many; - one-to-one
Ref{
U.country_code > countries.code
merchants.country_code > countries.code
}
//----------------------------------------------//
//// -- LEVEL 2
//// -- Adding column settings
Table order_items {
order_id int [ref: > orders.id]
product_id int
quantity int [default: 1] // default value
}
Ref: order_items.product_id > products.id
Table orders {
id int [pk] // primary key
user_id int [not null, unique]
status varchar
created_at varchar [note: '''When order created'''] // add column note
}
Table int {
id int
}
//----------------------------------------------//
//// -- Level 3
//// -- Enum, Indexes
// Enum for 'products' table below
Enum products_status {
out_of_stock
in_stock
running_low [note: 'less than 20'] // add column note
}
// Indexes: You can define a single or multi-column index
Table products {
id int [pk]
name varchar
merchant_id int [not null]
price int
status products_status
created_at datetime [default: `now()`]
Indexes {
(merchant_id, status) [name:'product_status', type: hash]
id [unique]
}
}
Ref: products.merchant_id > merchants.id // many-to-one
TableGroup hello_world {
just_test
just_a_test
}DBML
);
```### Schema data
```php
// List of tables
$tables = $schema->getTables(); // \Butschster\Dbml\Ast\TableNode[]// Check if table exists
$schema->hasTable('users');// Get table by name
$table = $schema->getTable('users'); // \Butschster\Dbml\Ast\TableNode[]// Get project data
$project = $schema->getProject(); // \Butschster\Dbml\Ast\ProjectNode// Get table groups
$tableGroups = $schema->getTableGroups(); // \Butschster\Dbml\Ast\TableGroupNode[]// Check if table group with given name exists
$schema->hasTableGroup('name');// Get table group object by name
$tableGroup = $schema->getTableGroup('name'); // \Butschster\Dbml\Ast\TableGroupNode// Get enums
$enums = $schema->getEnums(); // \Butschster\Dbml\Ast\EnumNode[]// Check if enum with given name exists
$schema->hasEnum('name');// Get enum object by name
$enum = $schema->getEnum('name'); // \Butschster\Dbml\Ast\EnumNode// Get refs
$refs = $schema->getRefs(); // \Butschster\Dbml\Ast\RefNode[]
```### Project data
```
Project test {
database_type: 'PostgreSQL'
Note: 'Description of the project'
}
``````php
/** @var \Butschster\Dbml\Ast\ProjectNode $project */
$project = $schema->getProject();
$name = $project->getName(); // test
$note = $project->getNote(); // Description of the project/** @var \Butschster\Dbml\Ast\Project\SettingNode $setting */
$setting = $project->getSetting('database_type');
$databaseType = $setting->getValue(); // PostgreSQL
$key = $setting->getKey(); // database_type
```### Table data
```
Table users as U {
id int [pk, unique, increment] // auto-increment
full_name varchar [not null, unique, default: 1]
created_at timestamp
country_code int
type int
note int
Note: 'khong hieu duoc'
}
``````php
/** @var \Butschster\Dbml\Ast\TableNode $table */
$table = $schema->getTable('users');$name = $table->getName(); // users
$alias = $table->getAlias(); // U$note = $table->getNote(); // khong hieu duoc
// Get table columns
$columns = $table->getColumns(); // \Butschster\Dbml\Ast\Table\ColumnNode[]// Check if table column exists
$table->hasColumn('id');// Get column by name
$column = $table->getColumn('id');// Get table indexes
$indexes = $table->getIndexes(); // \Butschster\Dbml\Ast\Table\IndexNode[]
```### Table column data
```
Table users as U {
id int [pk, unique, increment] // auto-increment
}
``````php
/** @var \Butschster\Dbml\Ast\Table\ColumnNode $column */
$column = $schema->getTable('users')->getColumn('id');$name = $column->getName(); // id
$type = $column->getType()->getName(); // int
$size = $column->getType()->getSize(); // null|int$note = $column->getNote(); // string
$refs = $column->getRefs(); // \Butschster\Dbml\Ast\RefNode[]
/** @var \Butschster\Dbml\Ast\Values\IntNode $default */
$default = $column->getDefault();
$value = $default->getValue(); // 1// Check if column is primary
$column->isPrimaryKey();// Check if column is auto increment
$column->isIncrement();// Check if column is unique
$column->isUnique();// heck if column is nullable
$column->isNull();
```### Table index data
```
Table products {
id int [pk]
name varchar
merchant_id int [not null]
price int
status products_status
created_at datetime [default: `now()`]
Indexes {
(merchant_id, status) [name:'product_status', type: hash]
id [unique]
}
}
``````php
/** @var \Butschster\Dbml\Ast\Table\IndexNode $index */
$index = $schema->getTable('products')->getIndexes()[0];/** @var \Butschster\Dbml\Ast\Values\StringNode[]|\Butschster\Dbml\Ast\Values\ExpressionNode[] $columns */
$columns = $index->getColumns();count($columns); // 2
$column1 = $index->getColumns()[0]->getValue(); // merchant_id
$column2 = $index->getColumns()[1]->getValue(); // status$type = $index->getType(); // hash
$name = $index->getName(); // product_status$note = $index->getNote();
// Check if index is pk
$index->isPrimaryKey();// Check if index is unique
$index->isUnique();
```### Enum data
```
Enum products_status {
out_of_stock
in_stock
running_low [note: 'less than 20'] // add column note
}
``````php
/** @var \Butschster\Dbml\Ast\EnumNode $enum */
$enum = $schema->getEnum('products_status');$name = $enum->getName(); // products_status
// Get amount of values
$enum->count(); // 3// Check if enum contains value
$enum->hasValue('out_of_stock'); // true// Get enum value object by name
$value = $enum->getValue('running_low'); // \Butschster\Dbml\Ast\Enum\ValueNode$note = $value->getNote();
$value = $value->getValue();
```### Table group data
```
TableGroup hello_world {
just_test
just_a_test
}
``````php
/** @var \Butschster\Dbml\Ast\TableGroupNode $group */
$group = $schema->getTableGroup('hello_world');$name = $group->getName(); // hello_world
// Check if table with given name contains in this group
$group->hasTable('just_test');// Get list of tables
$tables = $group->getTables(); // string[]
```### Ref data
```
Ref optional_name: products.merchant_id > merchants.(id, name) [delete: cascade, update: no action]
``````php
/** @var \Butschster\Dbml\Ast\RefNode $ref */
$ref = $schema->getRefs()[0];$name = $ref->getName(); // optional_name
$type = $ref->getType(); // \Butschster\Dbml\Ast\Ref\Type\ManyToOneNode
$leftTable = $ref->getLeftTable(); // \Butschster\Dbml\Ast\Ref\LeftTableNode
$table = $leftTable->getTable(); // products
$columns = $leftTable->getColumns(); // ['merchant_id']$rightTable = $ref->getRightTable(); // \Butschster\Dbml\Ast\Ref\RightTableNode
$table = $rightTable->getTable(); // merchants
$columns = $rightTable->getColumns(); // ['id', 'name']$onDelete = $ref->getAction('delete');
$action = $onDelete->getAction(); // cascade$onUpdate = $ref->getAction('update');
$action = $onUpdate->getAction(); // no action
```# Enjoy!