Oihana PHP System

QueryIDTrait uses trait:short

Provides a consistent mechanism for managing an internal query identifier (`queryId`), which can be used in dynamically constructed queries (e.g., AQL), log tracing, or caching.

This trait:

  • Stores a private query identifier string
  • Allows programmatic access to get or set the query ID
  • Automatically generates a default ID if none is provided
  • Supports initialization via associative arrays (e.g., input parameters or config)

Depends on:

  • ExpressionTrait for consistent expression formatting
  • Param::QUERY_ID constant to standardize key access
  • Char::UNDERLINE for default ID generation
Tags
example
use oihana\traits\QueryIDTrait;

class MyQueryBuilder {
use QueryIDTrait;

public function __construct(array $options = []) {
$this->setQueryID($options);
}
}

$builder = new MyQueryBuilder(['queryId' => 'custom_query']);
echo $builder->getQueryID(); // 'custom_query'

$builder->setQueryID(null);
echo $builder->getQueryID(); // 'query_8237412' (random suffix)
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Properties

$queryId  : string
The internal query identifier.

Methods

betweenBraces()  : string
Encapsulates an expression between braces (`{}`).
betweenBrackets()  : string
Encapsulates an expression between brackets (`[]`).
betweenChars()  : mixed
Encapsulates an expression between specific characters.
betweenDoubleQuotes()  : mixed
Encapsulates an expression between double quotes.
betweenParentheses()  : string
Encapsulates an expression between parentheses (`()`).
betweenQuotes()  : mixed
Encapsulates an expression between single quotes or custom characters.
clean()  : array<string|int, mixed>
Clean an array by removing null values and empty strings.
compile()  : string
Compile a list of expressions into a single string using a separator.
func()  : string
Build a function expression like `NAME(arg1,arg2)`.
getQueryID()  : string
Returns the internal query identifier.
key()  : string
Transform a key by optionally prefixing it.
keyValue()  : string
Build a key-value expression like `key: value`.
object()  : string
Create an object expression, e.g., `{ name: 'Eka', age: 48 }`.
predicate()  : string
Generate a predicate expression.
predicates()  : string|null
Generate a complex logical expression with multiple predicates.
setQueryID()  : void
Sets the internal query identifier.
wrap()  : string
Wrap a string in grave accent characters.

Properties

$queryId

The internal query identifier.

protected string $queryId

This property holds the unique identifier string used internally for query referencing, debugging, or mapping purposes.

Methods

betweenBraces()

Encapsulates an expression between braces (`{}`).

public betweenBraces([mixed $expression = Char::EMPTY ][, bool $useBraces = true ][, string $separator = Char::SPACE ]) : string
Parameters
$expression : mixed = Char::EMPTY

The expression to wrap.

$useBraces : bool = true

Whether to apply the braces or not.

$separator : string = Char::SPACE

Separator for arrays (default: space).

Tags
example
$this->betweenBraces('id: 1');           // '{ id: 1 }'
$this->betweenBraces(['x', 'y']);        // '{ x y }'
$this->betweenBraces(['x', 'y'], false); // 'x y'
Return values
string

The wrapped string.

betweenBrackets()

Encapsulates an expression between brackets (`[]`).

public betweenBrackets([mixed $expression = Char::EMPTY ][, bool $useBrackets = true ][, string $separator = Char::SPACE ]) : string
Parameters
$expression : mixed = Char::EMPTY

The expression to wrap.

$useBrackets : bool = true

Whether to apply the brackets.

$separator : string = Char::SPACE

Separator for arrays.

Tags
example
$this->betweenBrackets(['a', 'b']);     // '[a b]'
$this->betweenBrackets('index: 3');     // '[index: 3]'
$this->betweenBrackets('value', false); // 'value'
Return values
string

The wrapped string.

betweenChars()

Encapsulates an expression between specific characters.

public betweenChars([mixed $expression = null ][, string $left = Char::EMPTY ][, string|null $right = null ][, bool $flag = true ][, string $separator = Char::SPACE ]) : mixed
Parameters
$expression : mixed = null

The expression to encapsulate between two characters.

$left : string = Char::EMPTY

The left character.

$right : string|null = null

The right character. If null, uses the left character.

$flag : bool = true

Indicates whether to apply the wrapping.

$separator : string = Char::SPACE

The separator used to join arrays.

Tags
example
$this->betweenChars('x', '<', '>');         // '<x>'
$this->betweenChars(['a', 'b'], '[', ']');  // '[a b]'
$this->betweenChars('y', '"', null, false); // 'y'
Return values
mixed

The wrapped string or original expression.

betweenDoubleQuotes()

Encapsulates an expression between double quotes.

public betweenDoubleQuotes([mixed $expression = Char::EMPTY ][, string $char = Char::DOUBLE_QUOTE ][, bool $useQuotes = true ][, string $separator = Char::SPACE ]) : mixed
Parameters
$expression : mixed = Char::EMPTY

The expression to wrap.

$char : string = Char::DOUBLE_QUOTE

The quote character (default: ").

$useQuotes : bool = true

Whether to apply quotes.

$separator : string = Char::SPACE

Separator for arrays.

Tags
example
$this->betweenDoubleQuotes('hello');         // '"hello"'
$this->betweenDoubleQuotes(['a', 'b']);      // '"a b"'
$this->betweenDoubleQuotes('x', '"', false); // 'x'
Return values
mixed

The wrapped string or original.

betweenParentheses()

Encapsulates an expression between parentheses (`()`).

public betweenParentheses([mixed $expression = Char::EMPTY ][, bool $useParentheses = true ][, string $separator = Char::SPACE ]) : string
Parameters
$expression : mixed = Char::EMPTY

The expression to wrap.

$useParentheses : bool = true

Whether to apply the parentheses.

$separator : string = Char::SPACE

Separator for arrays.

Tags
example
$this->betweenParentheses('sum: 10');       // '(sum: 10)'
$this->betweenParentheses(['a', 'b', 'c']); // '(a b c)'
$this->betweenParentheses('val', false);    // 'val'
Return values
string

The wrapped string.

betweenQuotes()

Encapsulates an expression between single quotes or custom characters.

public betweenQuotes([mixed $expression = Char::EMPTY ][, string $char = Char::SIMPLE_QUOTE ][, bool $useQuotes = true ][, string $separator = Char::SPACE ]) : mixed
Parameters
$expression : mixed = Char::EMPTY

The expression to wrap.

$char : string = Char::SIMPLE_QUOTE

The quote character (default: ').

$useQuotes : bool = true

Whether to apply the quotes.

$separator : string = Char::SPACE

Separator for arrays.

Tags
example
$this->betweenQuotes('world');           // '\'world\''
$this->betweenQuotes(['foo', 'bar']);    // '\'foo bar\''
$this->betweenQuotes('data', '`');       // '`data`'
$this->betweenQuotes('raw', "'", false); // 'raw'
Return values
mixed

The wrapped string or original.

clean()

Clean an array by removing null values and empty strings.

public clean([array<string|int, mixed> $array = [] ]) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed> = []

The array to clean.

Tags
example
$this->clean(['foo', '', null, 'bar']); // ['foo', 'bar']
Return values
array<string|int, mixed>

The filtered array.

compile()

Compile a list of expressions into a single string using a separator.

public compile(string|array<string|int, mixed>|null $expressions[, string $separator = Char::SPACE ]) : string
Parameters
$expressions : string|array<string|int, mixed>|null

The expressions to compile.

$separator : string = Char::SPACE

The separator to use (default: Char::SPACE).

Tags
example
$this->compile(['name = "foo"', 'age > 21']); // 'name = "foo" age > 21'
$this->compile(null);                         // ''
Return values
string

The compiled string.

func()

Build a function expression like `NAME(arg1,arg2)`.

public func(string $name[, mixed $arguments = null ][, string $separator = Char::COMMA ]) : string
Parameters
$name : string

The function name.

$arguments : mixed = null

The arguments for the function.

$separator : string = Char::COMMA

The separator between arguments (default: Char::COMMA).

Tags
example
$this->func('CALL', ['a', 'b']); // 'CALL(a,b)'
Return values
string

The function expression.

getQueryID()

Returns the internal query identifier.

public getQueryID() : string
Tags
example
$id = $this->getQueryID();
Return values
string

The current value of the query ID.

key()

Transform a key by optionally prefixing it.

public key(string $key, string|null $prefix) : string
Parameters
$key : string

The key to transform.

$prefix : string|null

The prefix to prepend.

Tags
example
$this->key('name');           // 'name'
$this->key('name', 'doc');    // 'doc.name'
Return values
string

The transformed key.

keyValue()

Build a key-value expression like `key: value`.

public keyValue(mixed $key, mixed $value) : string
Parameters
$key : mixed

The key.

$value : mixed

The value.

Tags
example
$this->keyValue('name', 'Eka'); // 'name: Eka'
Return values
string

The key-value expression.

object()

Create an object expression, e.g., `{ name: 'Eka', age: 48 }`.

public object([string|array<string|int, mixed>|null $keyValues = [] ]) : string
Parameters
$keyValues : string|array<string|int, mixed>|null = []

The properties to include.

Tags
example
$this->object([['name', "'Eka'"], ['age', 47]]);
// '{ name: 'Eka', age: 47 }'
Return values
string

The object-like string expression.

predicate()

Generate a predicate expression.

public predicate(mixed $leftOperand[, string|null $operator = null ][, mixed $rightOperand = null ]) : string
Parameters
$leftOperand : mixed

The left-hand value.

$operator : string|null = null

The operator (e.g., '==', '!=').

$rightOperand : mixed = null

The right-hand value.

Tags
example
$this->predicate('age', '>=', 18); // 'age >= 18'
Return values
string

The predicate expression.

predicates()

Generate a complex logical expression with multiple predicates.

public predicates(array<string|int, mixed>|null $conditions, string $logicalOperator[, bool $useParentheses = false ]) : string|null
Parameters
$conditions : array<string|int, mixed>|null

List of predicate expressions.

$logicalOperator : string

The logical operator to join predicates (e.g., 'AND', 'OR').

$useParentheses : bool = false

Whether to wrap the result in parentheses.

Tags
example
$predicates = [
    $this->predicate('a', '==', 1),
    $this->predicate('b', '>', 5)
];
$this->predicates( $predicates , 'AND' , true ) ; // '(a == 1 AND b > 5)'
Return values
string|null

The combined expression or null if empty.

setQueryID()

Sets the internal query identifier.

public setQueryID(string|array<string|int, mixed>|null $init) : void

Accepts a string value or an associative array that contains the key Param::QUERY_ID. If null is passed, a default ID is auto-generated using query_<random>.

Parameters
$init : string|array<string|int, mixed>|null

The initial ID value, or an array with key Param::QUERY_ID.

Tags
example
$this->setQueryID('my_query'); // sets queryId to 'my_query'

$this->setQueryID(['queryId' => 'users_by_name']);

$this->setQueryID(null); // auto-generates ID like 'query_2384721'

wrap()

Wrap a string in grave accent characters.

public wrap(string $value) : string
Parameters
$value : string

The value to wrap.

Tags
example
$this->wrap('column'); // '`column`'
Return values
string

The wrapped value.


        
On this page

Search results