Skip to content

Commit d4d4081

Browse files
committed
Merge pull request #1 from Samshal/master
Added Foreign Key Support
2 parents 9e7c2d4 + 98e60c7 commit d4d4081

File tree

6 files changed

+108
-20
lines changed

6 files changed

+108
-20
lines changed

README.md

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,90 @@
1-
# Scripd [![Build Status](https://travis-ci.org/Samshal/Scripd.svg?branch=master)](https://travis-ci.org/Samshal/Scripd)
2-
A robust SQL Generator. Parses database structures defined in json based on the jsyn file format and generates corresponding sql queries.
1+
# Scripd [![Build Status](https://travis-ci.org/Samshal/Scripd.svg?branch=master)](https://travis-ci.org/Samshal/Scripd) [![Latest Stable Version](https://poser.pugx.org/samshal/scripd/v/stable)](https://packagist.org/packages/samshal/scripd) [![Total Downloads](https://poser.pugx.org/samshal/scripd/downloads)](https://packagist.org/packages/samshal/scripd) [![Latest Unstable Version](https://poser.pugx.org/samshal/scripd/v/unstable)](https://packagist.org/packages/samshal/scripd) [![License](https://poser.pugx.org/samshal/scripd/license)](https://packagist.org/packages/samshal/scripd) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Samshal/Scripd/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Samshal/Scripd/?branch=master)
2+
A robust SQL Generator. Parses database structures defined in json based on a custom jsyn file format and generates corresponding sql queries.
3+
4+
## Class Features
5+
6+
- A json like file format to define database structure
7+
- Support for multiple sql vendors / dialects
8+
- Compatible with PHP 5.0+
9+
- Support for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings
10+
- Much More!
11+
12+
## Why you might need it
13+
14+
This project was birthed as a result of the need to give users an opportunity to `create` their own custom database structure in a PHP Application.
15+
I wanted users to be able to modify database structures while there is support for multiple db vendors such as mysql, sqlite and sql server.
16+
17+
This library offers the ability to create database structures in a json-like format and generate sql compatible with several database vendors.
18+
19+
It is also very easy to use and integrate into your php based projects
20+
21+
## License
22+
23+
This software is distributed under the [MIT](https://opensource.org/licenses/MIT) license. Please read LICENSE for information on the
24+
software availability and distribution.
25+
26+
## Installation & loading
27+
Scripd is available via [Composer/Packagist](https://packagist.org/packages/samshal/scripd), so just add this line to your `composer.json` file:
28+
29+
```json
30+
"samshal/scripd": "~1.0"
31+
```
32+
33+
or
34+
35+
```sh
36+
composer require samshal/scripd
37+
```
38+
39+
## A Simple Example
40+
41+
#### JSON DB Structure (structure.json)
42+
```json
43+
{
44+
":database":{
45+
":crud-action":"create",
46+
"name":"dbname",
47+
48+
":table":[
49+
{
50+
":crud-action":"create",
51+
"name":"students",
52+
"columns":[
53+
{
54+
"name":"id",
55+
"data-type":"int",
56+
"primary-key":true
57+
},
58+
{
59+
"name":"first_name",
60+
"data-type":"varchar(20)",
61+
"default":"'samuel'"
62+
},
63+
{
64+
"name":"last_name",
65+
"data-type":"varchar(20)"
66+
},
67+
{
68+
"name":"class",
69+
"data-type":"varchar(10)"
70+
}
71+
]
72+
}
73+
]
74+
}
75+
}
76+
```
77+
78+
#### PHP (index.php)
79+
```php
80+
<?php
81+
require 'vendor/autoload.php';
82+
83+
$jsonDBStructure = new Samshal\Scripd\JsonDbStructure('./structure.json', 'mysql');
84+
85+
$jsonDBStructure->parseStructure();
86+
87+
$sql = $jsonDBStructure->getGeneratedSql();
88+
89+
echo $sql;
90+
```

index.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/JsonDbStructure.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ final class JsonDbStructure
5151
private $objectDefiners = [
5252
'columns',
5353
'add-column',
54+
'foreign-key'
5455
];
5556

5657
/**
@@ -65,6 +66,8 @@ final class JsonDbStructure
6566
'right-curly-brace' => '}',
6667
'left-square-bracket' => '[',
6768
'right-square-bracket' => ']',
69+
'left-bracket' => '(',
70+
'right-bracket' => ')',
6871
];
6972

7073
/**
@@ -302,14 +305,19 @@ public function generateSqlFromObjectDefiner($jsonStructures, $objectDefiner)
302305
$string = $jsyn[$i];
303306
$toSetValue = false;
304307
$isConstant = false;
308+
$replaceWithComma = false;
305309

306310
if (self::enclosed($this->specialCharacters['left-square-bracket'], $this->specialCharacters['right-square-bracket'], $string)) {
307311
$string = str_replace($this->specialCharacters['left-square-bracket'], null, str_replace($this->specialCharacters['right-square-bracket'], null, $string));
308312
if (self::enclosed($this->specialCharacters['left-curly-brace'], $this->specialCharacters['right-curly-brace'], $string)) {
309313
$string = str_replace($this->specialCharacters['left-curly-brace'], null, str_replace($this->specialCharacters['right-curly-brace'], null, $string));
310314
$toSetValue = true;
315+
} else if (self::enclosed($this->specialCharacters['left-bracket'], $this->specialCharacters['right-bracket'], $string)) {
316+
$string = str_replace($this->specialCharacters['left-bracket'], null, str_replace($this->specialCharacters['right-bracket'], null, $string));
317+
$toSetValue = false;
318+
$replaceWithComma = true;
311319
}
312-
} elseif (self::enclosed($this->specialCharacters['left-curly-brace'], $this->specialCharacters['right-curly-brace'], $string)) {
320+
} else if (self::enclosed($this->specialCharacters['left-curly-brace'], $this->specialCharacters['right-curly-brace'], $string)) {
313321
$string = str_replace($this->specialCharacters['left-curly-brace'], null, str_replace($this->specialCharacters['right-curly-brace'], null, $string));
314322
$toSetValue = true;
315323
} else {
@@ -321,7 +329,11 @@ public function generateSqlFromObjectDefiner($jsonStructures, $objectDefiner)
321329
if ($toSetValue && !is_bool($jsonStructure[$topLevelObject][$_string])) {
322330
$jsyn[$i] = $jsonStructure[$topLevelObject][$_string];
323331
} else {
332+
if ($replaceWithComma){
333+
$string = ", $string";
334+
}
324335
$jsyn[$i] = (isset($jsonStructure[$topLevelObject][$_string]) && $jsonStructure[$topLevelObject][$_string] == true) ? strtoupper($string) : null;
336+
325337
}
326338
} else {
327339
if (!$isConstant) {
@@ -333,7 +345,7 @@ public function generateSqlFromObjectDefiner($jsonStructures, $objectDefiner)
333345
}
334346
}
335347
}
336-
348+
337349
return implode(' ', $jsyn);
338350
}
339351

src/bin/columns.jsyn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"[{{COLUMN FORMAT}}]",
1616
"[STORAGE]",
1717
"[{{STORAGE}}]",
18+
"[(FOREIGN KEY)]",
19+
"[{{FOREIGN KEY}}]",
1820
"[REFERENCES]",
1921
"[{{REFERENCES}}]",
2022
"[MATCH FULL]",

tests/JsonDbStructureTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function dataProvider()
3434
'USE another_unify_schools;'.
3535
"CREATE TABLE students (id int PRIMARY KEY, first_name varchar(20) DEFAULT 'samuel', last_name varchar(20), class varchar(10));".
3636
'CREATE TABLE faculty (fac_id int AUTO_INCREMENT PRIMARY KEY, first_name varchar(20), last_name varchar(20));'.
37-
'CREATE TABLE subjects (subject_id int AUTO_INCREMENT PRIMARY KEY, subject_name varchar(30), subject_faculty int REFERENCES faculty('.'fac_id) ON UPDATE cascade ON DELETE set null)', __DIR__.'/json/1.json'
37+
'CREATE TABLE subjects (subject_id int AUTO_INCREMENT PRIMARY KEY, subject_name varchar(30), subject_faculty int , FOREIGN KEY (subject_faculty) REFERENCES faculty(fac_id) ON UPDATE cascade ON DELETE set null)', __DIR__.'/json/1.json'
3838
],
3939
'Alter Table'=>[
4040
"ALTER TABLE facultys ADD COLUMN (full_name varchar(30) NOT NULL DEFAULT 'john doe')", __DIR__.'/json/2.json'

tests/json/1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
{
6666
"name":"subject_faculty",
6767
"data-type":"int",
68+
"foreign-key":"(subject_faculty)",
6869
"references":"faculty(fac_id)",
6970
"on-update":"cascade",
7071
"on-delete":"set null"

0 commit comments

Comments
 (0)