Skip to content

Commit a378341

Browse files
authored
Merge pull request #225 from sagittaracc/develop
Develop
2 parents 72c04d9 + a289712 commit a378341

11 files changed

+44
-74
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**New features:**
66
- Add filter modifier
77
- Remove commands (no need anymore since we use orm approach)
8+
- Add examples of extending SuQL syntax
89

910
---
1011

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SuQL is syntactic sugar for SQL.
3232
3. Expand SuQL syntax on your own. SQL isn't the limit. There's no limit really.
3333

3434
### Examples
35-
There are couple examples in the ```tests``` directory
35+
There are couple examples in the ```tests``` directory and an example of extending SuQL syntax in the ```syntax``` directory.
3636

3737
## Conclusion
3838
SuQL is all about modifiers and commands. Modifiers already replace standart SQL clauses such as `WHERE`, `GROUP`, `JOIN`, `ORDER` and SQL functions etc. Meanwhile commands can do everything that SuQL or SQL can\'t.

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
]
1616
},
1717
"require": {
18-
"sagittaracc/config": "@dev",
1918
"sagittaracc/regexp": "@dev",
2019
"sagittaracc/strings": "@dev",
2120
"sagittaracc/arrays": "@dev",

composer.lock

+1-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/main.php

-17
This file was deleted.

src/core/SuQLObject.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22
namespace core;
33

44
use builder\SQLAdapter;
5-
use sagittaracc\Config;
65

76
class SuQLObject {
87
private $queries = [];
98
private $scheme = ['rel' => [], 'temp_rel' => []];
109
protected $adapter = null;
1110
private $log = [];
12-
private $configFile = __DIR__ . '/../../config/main.php';
11+
12+
protected function modifierList()
13+
{
14+
return [
15+
'SQLBaseModifier',
16+
'SQLWhereModifier',
17+
'SQLFilterModifier',
18+
'SQLOrderModifier',
19+
'SQLGroupModifier',
20+
'SQLFunctionModifier',
21+
'SQLCaseModifier',
22+
'SQLConditionModifier',
23+
];
24+
}
1325

1426
public function clear() {
1527
$this->queries = [];
@@ -141,11 +153,6 @@ public function addUnionTable($name, $unionType, $table) {
141153
$this->queries[$name]->addUnionTable($unionType, $table);
142154
}
143155

144-
// PHP command as a store procedure
145-
public function addCommand($name, $instruction, $args) {
146-
$this->queries[$name] = new SuQLCommand($this, $instruction, $args);
147-
}
148-
149156
public function getQuery($name) {
150157
return $this->queries[$name];
151158
}
@@ -155,17 +162,11 @@ public function hasQuery($name) {
155162
}
156163

157164
public function getModifierClass($modifierHandler) {
158-
$modifierClassList = Config::load($this->configFile)->get('modifier.handler');
159-
160-
foreach ($modifierClassList as $modifierClass) {
165+
foreach ($this->modifierList() as $modifierClass) {
161166
if (method_exists($modifierClass, $modifierHandler))
162167
return $modifierClass;
163168
}
164169

165170
return null;
166171
}
167-
168-
public function getCommandClass() {
169-
return class_exists('SuQLExtCommand') ? 'SuQLExtCommand' : 'SuQLBaseCommand';
170-
}
171172
}

src/core/SuQLRegexp.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ class SuQLRegexp extends Regexp {
66
function __construct($regex, $flags = '') {
77
parent::registerSequence('v', [SuQLSpecialSymbols::$prefix_declare_variable]);
88
parent::registerSequence('f', [SuQLSpecialSymbols::$prefix_declare_field_alias]);
9-
parent::registerSequence('c', [SuQLSpecialSymbols::$prefix_declare_command]);
10-
parent::registerSequence('p', [SuQLSpecialSymbols::$prefix_declare_command, SuQLSpecialSymbols::$prefix_declare_variable]);
9+
parent::registerSequence('p', [SuQLSpecialSymbols::$prefix_declare_variable]);
1110
parent::__construct($regex, $flags);
1211
}
1312
}

src/core/SuQLSpecialSymbols.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
class SuQLSpecialSymbols {
55
public static $prefix_declare_variable = '@';
66
public static $prefix_declare_field_alias = ':';
7-
public static $prefix_declare_command = '%';
87

98
public static function nestedQueryPlaceholder($query) {
109
return self::$prefix_declare_variable . $query;

src/model/User.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace sagittaracc\model;
44

55
use \SuQL;
6-
use \SuQLExt;
6+
use \SuQLExtensionExample;
77

8-
class User extends SuQLExt
8+
class User extends SuQLExtensionExample
99
{
1010
public function query()
1111
{

src/syntax/SuQLExt.php src/syntax/SuQLExtensionExample.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
<?php
22

3-
// Example: max function
3+
// Example: extend SuQL functionality on your own
44

5-
class SuQLExt extends SuQL
5+
class SuQLExtensionExample extends SuQL
66
{
7+
protected function modifierList()
8+
{
9+
return array_merge(
10+
parent::modifierList(),
11+
[
12+
'SQLArithmeticModifier',
13+
]
14+
);
15+
}
16+
717
public function max($field)
818
{
919
$this->field($field, [

tests/SuQLTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function testFunction(): void
133133
);
134134
}
135135

136-
public function testSuQLExt(): void
136+
public function testSuQLExtension(): void
137137
{
138138
$this->assertEquals(
139139
User::find()->max('id')->getRawSql(),
@@ -150,4 +150,14 @@ public function testSuQLExt(): void
150150
'select users.name from users'
151151
);
152152
}
153+
154+
public function testSuQLExtensionArithmeticModifier(): void
155+
{
156+
$this->assertEquals(
157+
User::find()->field('id', [
158+
'div' => [2]
159+
])->getRawSql(),
160+
'select users.id / 2 from users'
161+
);
162+
}
153163
}

0 commit comments

Comments
 (0)