Construct a complex SQL expression

package salt\sql
inherited_from \salt\SqlBindField

 Methods

Create a new SqlExpr for an SQL function

__callstatic(string $func, mixed | \salt\SqlExpr $args) : \salt\SqlExpr
Static

Parameters

$func

string

name of the SQL function with an underscore prefix for avoid collision with PHP keywords or other defined functions

$args

mixed\salt\SqlExpr

... $args list of function arguments. All arg that is not a SqlExpr is converted with SqlExpr::value($arg)

Returns

\salt\SqlExpr

Add something after the SqlExpr

after(mixed|\salt\SqlExpr $arg) : \salt\SqlExpr

Parameters

$arg

mixed\salt\SqlExpr

converted with SqlExpr::value() if needed

Returns

\salt\SqlExprcurrent object

Set the SqlExpr type to BOOLEAN

asBoolean() : \salt\SqlExpr

Returns

\salt\SqlExprcurrent object

Set the SqlExpr type to DATE

asDate(string $format = NULL) : \salt\SqlExpr

Parameters

$format

string

format of the source, used for convert to date.

Exceptions

\salt\SaltException

Returns

\salt\SqlExprcurrent object

Set the SqlExpr type to NUMBER

asNumber() : \salt\SqlExpr

Returns

\salt\SqlExprcurrent object

Set the SqlExpr type to the field type

asSetter(\salt\Field $field) : \salt\SqlExpr

Parameters

$field

\salt\Field

the field to use for configure SqlExpr

Returns

\salt\SqlExprcurrent object

Set the SqlExpr type to TEXT

asText() : \salt\SqlExpr

Returns

\salt\SqlExprcurrent object

Set the SqlExpr type to DATE, as TIMESTAMP format

asTimestamp(string $format = NULL) : \salt\SqlExpr

Parameters

$format

string

format of the source, used for convert to date.

Exceptions

\salt\SaltException

Returns

\salt\SqlExprcurrent object

Add something before the SqlExpr

before(mixed|\salt\SqlExpr $arg) : \salt\SqlExpr

Parameters

$arg

mixed\salt\SqlExpr

converted with SqlExpr::value() if needed

Returns

\salt\SqlExprcurrent object

Add DISTINCT before SqlExpr

distinct() : \salt\SqlExpr

Returns

\salt\SqlExprcurrent object

Escape a name for SQL use with backquote

escapeName(string $name) : string
InheritedStatic

Parameters

$name

string

a SQL name : database, table, field, alias, etc...

Returns

stringSQL escaped text with backquote.

Create a new SqlExpr for a field

field(string $alias, \salt\Field $field) : \salt\SqlExpr
Static

Do not use this method, but Query::getField($fieldName) instead

Parameters

$alias

string

alias of the table of the field

$field

\salt\Field

field

Returns

\salt\SqlExpr

Retrieve all used table alias in SqlExpr

getAllUsedTableAlias() : array<mixed,string>

Returns

array<mixed,string>all table alias

Return all binds

getBinds(string $source = \salt\ClauseType::ALL) : array<mixed,mixed[]>
Inherited

Parameters

$source

string

(Optional) ClauseType or specific text for restrict returned binds to specified source

Returns

array<mixed,mixed[]>list of binds : array of array('value' => ..., 'type' => ...)

Retrieve the field name of a FIELD type SqlExpr

getFieldName() : string

Returns

stringthe field name if SqlExpr is a FIELD expr, NULL otherwise

Retrieve binds for pagination (LIMIT clause)

getPaginationBinds(\salt\Pagination $pagination) : array<mixed,mixed[]>
InheritedStatic

Parameters

$pagination

\salt\Pagination

the Pagination object

Returns

array<mixed,mixed[]>binds for the Pagination : array of bindName => array of ('value' => ..., 'type' => ...)

Get the type of the SqlExpr

getType() : integer

Returns

integerFieldType the type of the SqlExpr

Join multiple parameters with a separator

implode(string $separator, \salt\SqlExpr | mixed $args) : \salt\SqlExpr
Static

Parameters

$separator

string

text to use for join parameters

$args

\salt\SqlExprmixed

... $args list of parameters. Parameters that are not SqlExpr are converted with SqlExpr::value()

Returns

\salt\SqlExprcurrent object

Check binds are private

isPrivateBinds() : boolean
Inherited

Returns

booleanTRUE if privateBinds has been called

Add NOT before SqlExpr

not() : \salt\SqlExpr

Returns

\salt\SqlExprcurrent object

Add parenthesis around current SqlExpr

parenthesis() : \salt\SqlExpr

Returns

\salt\SqlExprcurrent objet

Increment or decrement a field

plus(integer $value = 1) : \salt\SqlExpr

Parameters

$value

integer

value to increment (if positive) or decrement (if negative)

Returns

\salt\SqlExprcurrent object

Set all binds values as private. They will be hidden in debug queries

privateBinds(boolean $privateBinds = TRUE) : \salt\SqlBindField
Inherited

Parameters

$privateBinds

boolean

(Optional, TRUE) hide binds values in debug queries

Returns

\salt\SqlBindFieldthe current object

Create a new SqlExpr for a query

query(\salt\BaseQuery $query) : \salt\SqlExpr
Static

Parameters

$query

\salt\BaseQuery

a query that return a compatible value with the usage of the SqlExpr.

Returns

\salt\SqlExpr

Use a template to format SqlExpr

template(string $template, \salt\SqlExpr | mixed $args = NULL) : \salt\SqlExpr

Parameters

$template

string

template with TEMPLATE_MAIN and TEMPLATE_PARAM if necessary

$args

\salt\SqlExprmixed

... $args list of parameters. Parameters that are not SqlExpr are converted with SqlExpr::value()

Returns

\salt\SqlExprcurrent object

Create a new SqlExpr for a static text

text(mixed $text) : \salt\SqlExpr
Static

Warning : $text HAVE TO BE escaped for sql usage, we recommand to DO NO USE user input in it.

  • Boolean is converted to 0/1
  • Number and text are used unchanged

Parameters

$text

mixed

text to transform in SqlExpr.

Returns

\salt\SqlExpr

Retrieve the SQL text

toSQL() : string
Inherited

Returns

stringthe memoized SQL text

Construct a tuple with parameters : (arg1, arg2, .

tuple(mixed|\salt\SqlExpr $args) : \salt\SqlExpr
Static

..)

Parameters

$args

mixed\salt\SqlExpr

...

Returns

\salt\SqlExprcurrent object

Create a new SqlExpr for a value

value(mixed $value) : \salt\SqlExpr
Static

Parameters

$value

mixed

value to transform in SqlExpr. Can be NULL

Returns

\salt\SqlExpr

create a new SqlExr. Use static function for that

__construct(integer $objectType, mixed $data) 

Parameters

$objectType

integer

type of SqlExpr : FUNC|VALUE|FIELD

$data

mixed

source of SqlExpr, can be a scalar value or an array

Add a bind

addBind(mixed $value, integer $type, string $source = \salt\ClauseType::ALL) : string
Inherited

Parameters

$value

mixed

value of bind

$type

integer

(FieldType) type of the field

$source

string

(Optional) ClauseType or other text : clause where the bind will be used, if we know it

Returns

stringthe bind name

Change in objet after calling this method are ignored

buildSQL() : string
inherited_from \salt\SqlBindField::buildSQL()

Returns

stringthe SQL text for SqlExpr

Build the SQL text that using the binds

buildSQL() : string
Inherited

Returns

stringthe SQL text

Link another SqlBindField to this bindFields.

linkBindsOf(\salt\SqlBindField $other, string $source = \salt\ClauseType::ALL, string $otherSource = \salt\ClauseType::ALL
Inherited

Parameters

$other

\salt\SqlBindField

the other SqlBindField to link

$source

string

clause usage of the SqlBindField if we known it

$otherSource

string

restrict link to this source in $other

Exceptions

\salt\SaltException if this SqlBindField is already resolved

Remove binds (local and linked) of the specified source.

removeBinds(string $source) 
Inherited

Parameters

$source

string

ClauseType of specific text

Convert an array of mixed elements in SqlExpr.

arrayToSqlExpr(array<mixed,mixed> $args) : array<mixed,\salt\SqlExpr>
Static

If element is not already an SqlExpr, it will be converted with SqlExpr::value

Parameters

$args

array<mixed,mixed>

all elements

Returns

array<mixed,\salt\SqlExpr>list of SqlExpr

Throw an exception if called after SqlBind have been resolved

checkNotResolved() 
Inherited

Exceptions

\salt\SaltException if called after toSQL()

Convert a SQL date to another format

getTemplateForConvertSqlDate(string $origin, string $destination) : string

Parameters

$origin

string

origin date format

$destination

string

destination date format

Returns

stringtemplate for conversion, with ? as placeholder

 Properties

 

bind unique number

$_salt_bindNumber : integer

Default

0
Static
 

bind pagination unique number

$_salt_bindPaginationNumber : integer

Default

0
Static
 

list of binds

$_salt_binds : array<mixed,mixed>

Default

array()
content
array of bindName => array(
					'value' => mixed // value of bind
					'type' => int FieldType // type of field
					'private' => boolean // if value is private data (like passwords)
			)
 

list of linked binds

$_salt_others : array<mixed,\salt\SqlBindField[][]>

Default

array()
content
array of (ClauseType (source) => array of ClauseType(dest) => array of SqlBindField)
 

TRUE if binds values are private (do not display in debug queries)

$_salt_privateBinds : boolean

Default

FALSE
 

list of local binds by source

$_salt_sources : array<mixed,string[]>

Default

array()
content
array of (ClauseType => array of bindName)
 

SQL text that use the binds

$_salt_text : string

Default

NULL
 

source of SqlExpr, can be a string, int, array, etc...

$data : mixed

Default

NULL
 

format of the parameter of the SqlExpr, when it's a DATE

$dateFormat : string

Default

NULL
 

type of SqlExpr : FUNC|VALUE|FIELD

$objectType : integer

Default

NULL
 

field to use when SqlExpr is in a SET clause

$setter : \salt\Field

Default

NULL
 

the template to apply

$template : array<mixed,mixed[]>

Default

array()
content array of array of (template, param1, param2, ...)
 

TRUE if the date SqlExpr have to be converted in TIMESTAMP, FALSE for MySQL DATE. Not used if the SqlExpr is not a date

$timestamp : boolean

Default

FALSE
 

type of the return of SqlExpr : FieldType::*

$type : integer

Default

NULL

 Constants

 

type of SqlExpr : a field

FIELD = 2 
 

type of SqlExpr : a function

FUNC = 0 
 

type of SqlExpr : a sub query

QUERY = 4 
 

template replaced by the main source of SqlExpr

TEMPLATE_MAIN = "\1:main\2" 
 

template replaced by each parameters in order

TEMPLATE_PARAM = "\3?\4" 
 

type of SqlExpr : a static text

TEXT = 3 
 

type of SqlExpr : a value

VALUE = 1