# DatabaseProvider methods

## clearCacheQuery( cacheKeyNamingOptions, overrideCacheProviderName ) <a href="#clearcachequery" id="clearcachequery"></a>

Clears the cache for the query identified by the given `cacheKeyNamingOptions`

* **`cacheKeyNamingOptions`** An optional hash array with the cache key naming options. [Cache::buildCacheKey](https://cherrycake.tin.cat/version-1.x-beta/core-modules/cache/cache-methods#buildcachekey)
* **`overrideCacheProviderName`** If specified, the cache provider with this name will be used instead of the one in the Database configuration key `cacheProviderName`.

**Returns:** True if the cache could be cleared successfully, false otherwise.

## execute( prepareResult, parameters ) <a href="#execute" id="execute"></a>

Executes a previously prepared query with the given parameters.

* **`prepareResult`** The prepare result as returned by the [DatabaseProvider::prepare](#prepare) method.
* **`parameters`** A hash array with the variables to be applied to the prepared result, in the same order as they are stated in the original SQL statement, where each item in the array is a hash array with the following keys:
  * **`type`** One of the available [database field type constants](https://cherrycake.tin.cat/version-1.x-beta/core-modules/database#constants), like `DATABASE_FIELD_TYPE_INTEGER`
  * **`value`** The value of the variable.

## prepare( sql ) <a href="#prepare" id="prepare"></a>

Prepares a query so it can be later executed as a prepared query with the [DatabaseProvider::execute](#execute) method.

* **`sql`** The SQL statement to prepare to be queried to the database, where all the variables are replaced by question marks.

**Returns:** A hash array with the following keys:

* **`sql`** The passed SQL statement.
* **`statement`** A provider-specific statement object if the query has been prepared correctly, false otherwise.

## prepareAndExecute( sql, parameters, setup ) <a href="#prepareandexecute" id="prepareandexecute"></a>

Prepares and executes a prepared query

* **`sql`** The prepared SQL statement.
* **`parameters`** A hash array with the variables to be applied to the prepared SQL statement, in the same order as they are stated in the SQL, with the same structure as in the [DatabaseProvider::execute](#execute-prepareresult-parameters) method.
* **`setup`** Optional hash array with options, same specification as [DatabaseResult::$setup](https://cherrycake.tin.cat/version-1.x-beta/reference/databaseresult/databaseresult-properties#usdsetup)

**Returns:** A [DatabaseResult](https://cherrycake.tin.cat/version-1.x-beta/reference/core-classes/databaseresult) object if the query was executed correctly, false otherwise.

## prepareAndExecuteCache( sql, parameters, cacheTtl, cacheKeyNamingOptions, overrideCacheProviderName, isStoreInCacheWhenNoResults, setup ) <a href="#prepareandexecutecache" id="prepareandexecutecache"></a>

Prepares and executes a prepared query implementing a caching mechanism. If the query results are stored in the cache, it retrieves them. If not in cache, it performs the query and stores the results in cache.

* **`sql`** The SQL statement.
* **`parameters`** A hash array with the variables to be applied to the prepared SQL statement, in the same order as they are stated in the SQL, with the same structure as in the [DatabaseProvider::execute](#execute-prepareresult-parameters) method.
* **`cacheTtl`** The TTL for the cache results. If not specified, the Database configuration key `cacheDefaultTtl` is used.
* **`cacheKeyNamingOptions`** An optional hash array with the cache key naming options. [Cache::buildCacheKey](https://cherrycake.tin.cat/version-1.x-beta/core-modules/cache/cache-methods#buildcachekey)
* **`overrideCacheProviderName`** If specified, the cache provider with this name will be used instead of the one in the Database configuration key `cacheProviderName`.
* **`isStoreInCacheWhenNoResults`** Whether to store results in the cache when the query returned no rows or not.
* **`setup`** Optional setup keys, as specified in [DatabaseResult::$setup](https://cherrycake.tin.cat/version-1.x-beta/reference/databaseresult/databaseresult-properties#usdsetup)

## query( query, setup ) <a href="#query" id="query"></a>

Performs a query to the database.

* **`query`** The SQL query string
* **`setup`** An optional hash array with setup options.

## queryCache( sql, cacheTtl, cacheKeyNamingOptions, overrideCacheProviderName, isStoreInCacheWhenNoResults, setup ) <a href="#querycache" id="querycache"></a>

Performs a query to the database implementing a caching mechanism. If the query results are stored in the cache, it retrieves them. If not in cache, it performs the query and stores the results in cache.

* **`query`** The SQL statement.
* **`cacheTtl`** The TTL for the cache results. If not specified, the Database configuration key `cacheDefaultTtl` is used.
* **`cacheKeyNamingOptions`** An optional hash array with the cache key naming options. [Cache::buildCacheKey](https://cherrycake.tin.cat/version-1.x-beta/core-modules/cache/cache-methods#buildcachekey)
* **`overrideCacheProviderName`** If specified, the cache provider with this name will be used instead of the one in the Database configuration key `cacheProviderName`.
* **`isStoreInCacheWhenNoResults`** Whether to store results in the cache when the query returned no rows or not.
* **`setup`** Optional setup keys, as specified in [DatabaseResult::$setup](https://cherrycake.tin.cat/version-1.x-beta/reference/databaseresult/databaseresult-properties#usdsetup).

**Returns:** A provider-specific [DatabaseResult](https://cherrycake.tin.cat/version-1.x-beta/reference/core-classes/databaseresult) object if the query was executed or retrieved from cache successfully, false otherwise.
