Skip to content

classes_essentials_model_datasource.class

Daniel Spors edited this page Mar 11, 2024 · 4 revisions

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.

Affected_Rows

SHORTCUT DataSource::getAffectedRowsCount DEPRECATED (2023/09) Use getAffectedRowsCount instead

BuildInConstraint

Helper to create a valid 'column IN(0,1,2)' string. If given value is not an array or if it is empty will return '(0=1)' as valid SQL string.

Definition: public function BuildInConstraint($field, $values)

Returns: string Valid constraint string

Parameters:

  • string $field Column name

  • mixed $values Values or null or false

BuildInContraint

DEPRECATED (2023/10) Typo in name, use BuildInConstraint instead.

BuildNotInConstraint

Helper to create a valid 'column NOT IN(0,1,2)' string.

Definition: public function BuildNotInConstraint($field, $values)

Returns: string Valid constraint string

Parameters:

  • string $field Column name

  • mixed $values Values or null or false

BuildNotInContraint

DEPRECATED (2023/10) Typo in name, use BuildNotInConstraint instead.

CacheDLookUp

SHORTCUT to DataSource::DLookUp but uses cache DEPRECATED (2023/09) Use CacheExecuteScalar instead

CacheExecuteScalar

Same as ExecuteScalar, but uses the cache.

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

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=false)

Returns: ResultSet The ResultSet

Parameters:

  • string $sql SQL statement

  • array $prms Arguments for the query

  • int $lifetime Optional Lifetime in seconds

Close

Closes this datasource.

Definition: public function Close()

Returns: void

Database

Returns the database name.

Definition: public function Database()

Returns: string The name

DLookUp

SHORTCUT for DataSource::ExecuteScalar DEPRECATED (2023/09) Use ExecuteScalar instead

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 DEPRECATED (2023/09) Use ExecuteSql instead

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|mixed $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

  • mixed $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

getLock

SHORTCUT system_get_lock

GetOne

SHORTCUT DataSource::ExecuteScalar DEPRECATED (2023/09) Use ExecuteScalar instead

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: mixed The last insert id

Parameters:

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

LogLastStatement

SHORTCUT ResultSet::LogDebug

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

QuoteColumnName

Quotes a column name

Definition: public function QuoteColumnName($column)

Returns: string The quoted column name

Parameters:

  • string $column The column name to quote

Reconnect

Reconnects to the database. Note: This should only be nessesary in long running processes.

Definition: public function Reconnect()

Returns: void

RegisterSlowQuery

Registers a query as known to be slow. It will be logged only if the runtime exceeds the given expected runtime.

Definition: public static function RegisterSlowQuery($sql, $expected_runtime_seconds)

Returns: void

Parameters:

  • mixed $sql The statement to be registered

  • mixed $expected_runtime_seconds Extepcted runtime in seconds

releaseLock

SHORTCUT system_release_lock

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

Clone this wiki locally