Database query wrapper. See Parameterized Statements for usage and examples.
Class declared in MODPATH/database/classes/database/query.php on line 3.
$_as_objectlink to this
$_force_executelink to this
$_lifetimelink to this
$_object_paramslink to this
$_parameterslink to this
$_sqllink to this
$_typelink to thisCreates a new SQL query of the specified type.
integer
$type
required - Query type: Database::SELECT, Database::INSERT, etcstring
$sql
required - Query stringvoid
public
function
__construct(
$type
,
$sql
)
{
$this
->_type =
$type
;
$this
->_sql =
$sql
;
}
Return the SQL query string.
string
final
public
function
__toString()
{
try
{
// Return the SQL string
return
$this
->compile(Database::instance());
}
catch
(Exception
$e
)
{
return
Kohana_Exception::text(
$e
);
}
}
Returns results as associative arrays
$this
public
function
as_assoc()
{
$this
->_as_object = FALSE;
$this
->_object_params =
array
();
return
$this
;
}
Returns results as objects
string
$class
= bool TRUE - Classname or TRUE for stdClassarray
$params
= NULL - $params$this
public
function
as_object(
$class
= TRUE,
array
$params
= NULL)
{
$this
->_as_object =
$class
;
if
(
$params
)
{
// Add object parameters
$this
->_object_params =
$params
;
}
return
$this
;
}
Bind a variable to a parameter in the query.
string
$param
required - Parameter key to replacebyref mixed
$var
required - Variable to use$this
public
function
bind(
$param
, &
$var
)
{
// Bind a value to a variable
$this
->_parameters[
$param
] =&
$var
;
return
$this
;
}
Enables the query to be cached for a specified amount of time.
integer
$lifetime
= NULL - Number of seconds to cache, 0 deletes it from the cacheboolean
$force
= bool FALSE - Whether or not to execute the query during a cache hit$this
public
function
cached(
$lifetime
= NULL,
$force
= FALSE)
{
if
(
$lifetime
=== NULL)
{
// Use the global setting
$lifetime
= Kohana::
$cache_life
;
}
$this
->_force_execute =
$force
;
$this
->_lifetime =
$lifetime
;
return
$this
;
}
Compile the SQL query and return it. Replaces any parameters with their given values.
object
$db
required - Database instancestring
public
function
compile(Database
$db
)
{
// Import the SQL locally
$sql
=
$this
->_sql;
if
( !
empty
(
$this
->_parameters))
{
// Quote all of the values
$values
=
array_map
(
array
(
$db
,
'quote'
),
$this
->_parameters);
// Replace the values in the SQL
$sql
=
strtr
(
$sql
,
$values
);
}
return
$sql
;
}
Execute the current query on the given database.
mixed
$db
= NULL - Database instance or name of instancestring
$as_object
= NULL - Result object classname, TRUE for stdClass or FALSE for arrayarray
$object_params
= NULL - Result object constructor argumentsobject
- Database_Result for SELECT queriesmixed
- The insert id for INSERT queriesinteger
- Number of affected rows for all other queries
public
function
execute(
$db
= NULL,
$as_object
= NULL,
$object_params
= NULL)
{
if
( !
is_object
(
$db
))
{
// Get the database instance
$db
= Database::instance(
$db
);
}
if
(
$as_object
=== NULL)
{
$as_object
=
$this
->_as_object;
}
if
(
$object_params
=== NULL)
{
$object_params
=
$this
->_object_params;
}
// Compile the SQL query
$sql
=
$this
->compile(
$db
);
if
(
$this
->_lifetime !== NULL AND
$this
->_type === Database::SELECT)
{
// Set the cache key based on the database instance name and SQL
$cache_key
=
'Database::query("'
.
$db
.
'", "'
.
$sql
.
'")'
;
// Read the cache first to delete a possible hit with lifetime <= 0
if
((
$result
= Kohana::cache(
$cache_key
, NULL,
$this
->_lifetime)) !== NULL
AND !
$this
->_force_execute)
{
// Return a cached result
return
new
Database_Result_Cached(
$result
,
$sql
,
$as_object
,
$object_params
);
}
}
// Execute the query
$result
=
$db
->query(
$this
->_type,
$sql
,
$as_object
,
$object_params
);
if
(isset(
$cache_key
) AND
$this
->_lifetime > 0)
{
// Cache the result array
Kohana::cache(
$cache_key
,
$result
->as_array(),
$this
->_lifetime);
}
return
$result
;
}
Set the value of a parameter in the query.
string
$param
required - Parameter key to replacemixed
$value
required - Value to use$this
public
function
param(
$param
,
$value
)
{
// Add or overload a new parameter
$this
->_parameters[
$param
] =
$value
;
return
$this
;
}
Add multiple parameters to the query.
array
$params
required - List of parameters$this
public
function
parameters(
array
$params
)
{
// Merge the new parameters in
$this
->_parameters =
$params
+
$this
->_parameters;
return
$this
;
}
Get the type of the query.
integer
public
function
type()
{
return
$this
->_type;
}