classes_essentials_model_datasource.class

Daniel Spors edited this page May 9, 2014 · 4 revisions
Clone this wiki locally

Classes in file essentials/model/datasource.class.php

class DataSource

Provides access to a database.
Use this to execute SQL statements directly when you need to do so.

Namespace: ScavixWDF\Model

Affected_Rows

SHORTCUT DataSource::getAffectedRowsCount

CacheDLookUp

SHORTCUT to DataSource::DLookUp but uses cache

CacheExecuteScalar

Same as ExecuteScalar, but uses the cache.

Definition: public function CacheExecuteScalar($sql, $prms, $lifetime)

Returns: mixed The first scalar

Parameters:

  • string $sql SQL statement

  • array $prms Arguments for $sql

  • int $lifetime Lifetime in seconds

CacheExecuteSql

Executes a statement and caches the result. Of course returns the cached result if called again and cached result is still alive.

Definition: public function CacheExecuteSql($sql, $prms, $lifetime)

Returns: ResultSet The ResultSet

Parameters:

  • string $sql SQL statement

  • array $prms Arguments for the query

  • int $lifetime Lifetime in seconds

Database

Returns the database name.

Definition: public function Database()

Returns: string The name

DLookUp

SHORTCUT for DataSource::ExecuteScalar

ErrorMsg

Returns the last errormessage, if any.

Definition: public function ErrorMsg()

Returns: string The last error or false

EscapeArgument

Escapes an argument The result will not contain escaping chars, but only perform an 'inner escaping'.
This is basically substr($this->Quote,1,-1)

Definition: public function EscapeArgument($value)

Returns: string escaped argument

Parameters:

  • string $value Argument to be escaped

Execute

SHORTCUT DataSource::ExecuteSql

ExecuteScalar

Executes a query and returns the first column of the first row.

Definition: public function ExecuteScalar($sql, $prms)

Returns: mixed The first scalar

Parameters:

  • string $sql SQL statement

  • array $prms Arguments for $sql

ExecuteSql

Executes an SQL statement.

Definition: public function ExecuteSql($sql, $parameter)

Returns: ResultSet The query result

Parameters:

  • string $sql SQL statement

  • array $parameter Arguments

Get

Returns a DataSource by name.
You may use this as alternative for Model::$DefaultDatasource by ignoring the $name parameter;

$a = Model::$DefaultDatasource; 
// is the same as   
$b = DataSource::Get(); 

Definition: public static function Get($name=false)

Returns: DataSource The requested datasource

Parameters:

  • string $name Aliasname for the datasource or (default) false to get the default datasource

getAffectedRowsCount

Gets the amount of rows affected by the last query.

Definition: public function getAffectedRowsCount()

Returns: int Number of affected rows

GetDsn

Returns the DSN

Definition: public function GetDsn()

Returns: string The Dsn

GetOne

SHORTCUT DataSource::ExecuteScalar

Host

Returns the database host.

Definition: public function Host()

Returns: string The host or false (for example sqlite has no host)

LastInsertId

Returns the id of the last inserted row.

Definition: public function LastInsertId($table=null)

Returns: int The last insert id

Parameters:

  • string $table The table to get last insert id for

ModelFromArray

Creates a typed Model from an array of data values.
Not nice, but fast.

Definition: public function ModelFromArray($type, $fields, $as_new=false)

Returns: Model The created model

Parameters:

  • string $type Type of Model class to create

  • array $fields Data for the new model, keys must be columns, values will be assigned

  • bool $as_new If true treats created Model as new instead of as if it was loaded from database.

Now

Return now how the database sees it.

Definition: public function Now($seconds_to_add)

Returns: string String representing now

Parameters:

  • int $seconds_to_add Offset to now in seconds, may be negative too.

PageExecute

Executes a pages query. This will add LIMIT stuff to the statement.

Definition: public function PageExecute($sql, $items_per_page, $page, $parameter)

Returns: ResultSet The query result

Parameters:

  • string $sql SQL statement

  • int $items_per_page Items per page

  • int $page Page number (1-based!)

  • array $parameter SQL arguments

Password

Returns the database password.

Definition: public function Password()

Returns: string The password

Prepare

Prepares a statement

Definition: public function Prepare($sql)

Returns: ResultSet Prepared statement

Parameters:

  • string $sql SQL statement

Query

Entry point for anonymous queries.
If you dont want to write a Model class for a table you can use this method to create an anonymous query:

$entries = $dataSource->Query('my_bog_entries')->youngerThan('created',1,'month');  

Definition: public function Query($tablename)

Returns: CommonModel The query as CommonModel

Parameters:

  • string $tablename Name of table to query

QuoteArgument

Quotes an argument

Definition: public function QuoteArgument($value)

Returns: string The quoted argument

Parameters:

  • string $value The argument to quote

SetDefault

Sets the default datasource.
This is nicer alternative to setting Model::$DefaultDatasource manually.

$ds = Datasource::SetDefault('system'); 
// or   
$ds = model_datasource('system');   
Datasource::SetDefault($ds);    
// or   
$ds = Model::$DefaultDatasource = model_datasource('system');   

Definition: public static function SetDefault($ds)

Returns: DataSource The newly set default DataSource object

Parameters:

  • mixed $ds The default datasource or it's aliasname

TableExists

Checks if a table exists.

Definition: public function TableExists($name)

Returns: bool true or false

Parameters:

  • string $name Name of table to check

TableForType

Returns the table where a Model is stored.

Definition: public function TableForType($type)

Returns: string Table name

Parameters:

  • string $type Classname of Model to check

Username

Returns the database username.

Definition: public function Username()

Returns: string The username