Modules

Kohana_Database_Expression

This class is a transparent base class for Database_Expression and should not be accessed directly.

Database expressions can be used to add unescaped SQL fragments to a Database_Query_Builder object.

For example, you can use an expression to generate a column alias:

// SELECT CONCAT(first_name, last_name) AS full_name
$query = DB::select([DB::expr('CONCAT(first_name, last_name)'), 'full_name']);

More examples are available on the Query Builder page

package
Kohana/Database
category
Base
author
Kohana Team
copyright
© 2009 Kohana Team
license
https://kohana.top/license

Class declared in MODPATH/database/classes/Kohana/Database/Expression.php on line 20.

Properties

protected $_parameters

Default value:
NULL

protected $_value

Default value:
NULL

Methods

public __construct( string $value [, array $parameters = array(0) ] ) (defined in Kohana_Database_Expression)

Sets the expression string.

$expression = new Database_Expression('COUNT(users.id)');

Parameters

  • string $value required - Raw SQL expression string
  • array $parameters = array(0) - Unquoted parameter values

Return Values

  • void

Source Code

public function __construct($value, $parameters = [])
{
    // Set the expression string
    $this->_value = $value;
    $this->_parameters = $parameters;
}

public __toString( ) (defined in Kohana_Database_Expression)

Return the value of the expression as a string.

echo $expression;

Tags

Return Values

  • string

Source Code

public function __toString()
{
    return $this->value();
}

public bind( string $param , mixed & $var ) (defined in Kohana_Database_Expression)

Bind a variable to a parameter.

Parameters

  • string $param required - Parameter key to replace
  • byref mixed $var required - Variable to use

Return Values

  • $this

Source Code

public function bind($param, & $var)
{
    $this->_parameters[$param] = & $var;

    return $this;
}

public compile( [ mixed $db = NULL ] ) (defined in Kohana_Database_Expression)

Compile the SQL expression and return it. Replaces any parameters with their given values.

Parameters

  • mixed $db = NULL - Database instance or name of instance

Return Values

  • string

Source Code

public function compile($db = null)
{
    if (!is_object($db)) {
        // Get the database instance
        $db = Database::instance($db);
    }

    $value = $this->value();

    if (!empty($this->_parameters)) {
        // Quote all of the parameter values
        $params = array_map([$db, 'quote'], $this->_parameters);

        // Replace the values in the expression
        $value = strtr($value, $params);
    }

    return $value;
}

public param( string $param , mixed $value ) (defined in Kohana_Database_Expression)

Set the value of a parameter.

Parameters

  • string $param required - Parameter key to replace
  • mixed $value required - Value to use

Return Values

  • $this

Source Code

public function param($param, $value)
{
    $this->_parameters[$param] = $value;

    return $this;
}

public parameters( array $params ) (defined in Kohana_Database_Expression)

Add multiple parameter values.

Parameters

  • array $params required - List of parameter values

Return Values

  • $this

Source Code

public function parameters(array $params)
{
    $this->_parameters = $params + $this->_parameters;

    return $this;
}

public value( ) (defined in Kohana_Database_Expression)

Get the expression value as a string.

$sql = $expression->value();

Return Values

  • string

Source Code

public function value()
{
    return (string) $this->_value;
}