-
Notifications
You must be signed in to change notification settings - Fork 0
Simple Syntax
Razy uses a compact "Simple Syntax" DSL in the Statement builder's where(), order(), and update() methods to express SQL operations concisely. The SimpleSyntax class parses these expressions.
Used inside Statement::where() strings. Named parameters use the :param prefix.
| Syntax | SQL Output | Example |
| --- | --- | --- |
| col = :val | col = ? | 'user_id = :id' |
| col != :val | col != ? | 'status != :status' |
| col > :val | col > ? | 'age > :minAge' |
| col < :val | col < ? | 'price < :maxPrice' |
| col >= :val | col >= ? | 'score >= :min' |
| col <= :val | col <= ? | 'amount <= :limit' |
| col LIKE :val | col LIKE ? | 'name LIKE :search' |
| col IN (:vals) | col IN (?, ?, ...) | 'id IN (:ids)' |
| col IS NULL | col IS NULL | 'deleted_at IS NULL' |
| col IS NOT NULL | col IS NOT NULL | 'email IS NOT NULL' |
// AND conditions
$stmt->where('active = :active AND role = :role');
// OR conditions
$stmt->where('status = :s1 OR status = :s2');
// Grouped conditions
$stmt->where('(role = :role OR permissions LIKE :perm) AND active = :active');
// Bind parameters
$stmt->assign([
'role' => 'admin',
'perm' => '%manage%',
'active' => 1,
]);The order() method uses < for ASC and > for DESC:
| Syntax | SQL Output |
| --- | --- |
| <column | column ASC |
| >column | column DESC |
| <col1, >col2 | col1 ASC, col2 DESC |
// Sort by name ASC, then created_at DESC
$stmt->order('<name, >created_at');The update() method accepts an array of SET expressions:
$stmt = $db->update('users', [
'name = :name', // Parameterised
'updated_at = NOW()', // Raw SQL expression
'login_count = login_count + 1', // Increment
]);The insert() method takes a column→parameter map:
$stmt = $db->insert('users', [
'name' => ':name',
'email' => ':email',
'created_at' => 'NOW()', // Raw SQL if no : prefix
]);
// ON DUPLICATE KEY UPDATE
$stmt = $db->insert('counters', [
'key' => ':key',
'value' => ':value',
], ['value']); // 3rd arg: duplicate update keysUse a TableJoin closure in from():
$stmt->from(function(TableJoin $tj) {
$tj->from('orders', 'o')
->innerJoin('users', 'u', 'o.user_id = u.id')
->leftJoin('payments', 'p', 'o.id = p.order_id')
->rightJoin('shipping', 's', 'o.id = s.order_id');
});// Generate multi-column LIKE WHERE clause
$where = Statement::GetSearchTextSyntax(
'search', // parameter name
['name', 'email', 'bio'] // columns to search
);
// Generates: (name LIKE :search OR email LIKE :search OR bio LIKE :search)
$stmt->where($where)
->assign(['search' => '%john%']);Columns can be defined using a compact config string in Table::addColumn():
| Syntax | Meaning |
| --- | --- |
| id:auto_id | Auto-increment primary key |
| name:text(100) | VARCHAR(100) |
| email:text(255)|unique | VARCHAR(255) with UNIQUE key |
| price:decimal(10,2) | DECIMAL(10,2) |
| is_active:bool|nullable | TINYINT(1) NULL |
| bio:full_text|nullable | TEXT NULL |
| data:json | JSON column |
| ts:timestamp|default(CURRENT_TIMESTAMP) | TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
The underlying parser for all the above syntax forms. SimpleSyntax is a utility class with static methods only — no constructor needed.
Parses a syntax string by delimiters while respecting quoted strings and parenthesized groups.
SimpleSyntax::ParseSyntax(
string $syntax, // The string to parse
string $delim, // Delimiter characters (e.g., ',' for AND)
string $neg, // Negative lookahead characters
?callable $parser, // Optional callback for each parsed segment
bool $noCap // Whether to skip capturing (default: false)
): array| Parameter | Type | Description |
| --- | --- | --- |
| $syntax | string | The input string to parse. Can contain quoted strings ('...' or "...") and parenthesized groups (...) that will be preserved as atomic units during splitting. |
| $delim | string | A string of delimiter characters used to split the syntax. Each character acts as a separate delimiter. For example, ',' splits on commas, ',|' splits on both commas and pipes. |
| $neg | string | Negative lookahead characters. When a delimiter is immediately followed by one of these characters, the split is suppressed. Useful for distinguishing operators like = from != or >=. |
| $parser | ?callable | An optional callback invoked for each parsed segment. Receives the segment string and its index. Return value replaces the segment in the result array. Pass null to skip. |
| $noCap | bool | When true, delimiters are not captured in the output array. When false (default), delimiters are included as separate elements between the split segments. |
use Razy\SimpleSyntax;
// Basic comma-delimited split
$result = SimpleSyntax::ParseSyntax('name, age, email', ',', '', null, true);
// Result: ['name', 'age', 'email']
// Split on '=' but not '!=' or '>='
$result = SimpleSyntax::ParseSyntax('col = :val', '=', '!', null, false);
// Result: ['col ', '=', ' :val']
// With parser callback to trim each segment
$result = SimpleSyntax::ParseSyntax(
'a , b , c',
',',
'',
function (string $segment, int $index): string {
return trim($segment);
},
true
);
// Result: ['a', 'b', 'c']
// Quoted strings are preserved as atomic units
$result = SimpleSyntax::ParseSyntax("'hello, world', foo", ',', '', null, true);
// Result: ["'hello, world'", 'foo'] → comma inside quotes is NOT a split pointExtracts parenthesized groups from a string into a nested array structure. Useful for parsing grouped conditions, function call arguments, or any nested syntax.
SimpleSyntax::ParseParens(string $text): arrayuse Razy\SimpleSyntax;
// Simple parenthesized extraction
$result = SimpleSyntax::ParseParens('func(a, b)');
// Result: ['func', ['a, b']]
// Nested parentheses
$result = SimpleSyntax::ParseParens('outer(inner(x, y), z)');
// Result: ['outer', ['inner', ['x, y'], ', z']]
// Multiple groups at the same level
$result = SimpleSyntax::ParseParens('a(b) AND c(d)');
// Result: ['a', ['b'], ' AND c', ['d']]
// WHERE clause grouping
$result = SimpleSyntax::ParseParens('(role = :role OR perm LIKE :perm) AND active = :active');
// Result: [['role = :role OR perm LIKE :perm'], ' AND active = :active']
// Column type definition with nested parens
$result = SimpleSyntax::ParseParens('decimal(10,2)|default(0.00)');
// Result: ['decimal', ['10,2'], '|default', ['0.00']]| Method | Return | Description |
| --- | --- | --- |
| ParseSyntax(string $syntax, string $delim, string $neg, ?callable $parser, bool $noCap) | array | Parse by delimiters, respecting quotes and parens |
| ParseParens(string $text) | array | Extract parenthesized groups into nested arrays |
📝 Note:
SimpleSyntaxis a utility class with static methods only — no constructor needed.