-
Notifications
You must be signed in to change notification settings - Fork 34
classes_essentials_model_datasource.class
Provides access to a database. Use this to execute SQL statements directly when you need to do so.
SHORTCUT DataSource::getAffectedRowsCount DEPRECATED (2023/09) Use getAffectedRowsCount instead
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 $fieldColumn name -
mixed $valuesValues or null or false
DEPRECATED (2023/10) Typo in name, use BuildInConstraint instead.
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 $fieldColumn name -
mixed $valuesValues or null or false
DEPRECATED (2023/10) Typo in name, use BuildNotInConstraint instead.
SHORTCUT to DataSource::DLookUp but uses cache DEPRECATED (2023/09) Use CacheExecuteScalar instead
Same as ExecuteScalar, but uses the cache.
Definition: public function CacheExecuteScalar($sql, $prms, $lifetime=false)
Returns: mixed The first scalar
Parameters:
-
string $sqlSQL statement -
array $prmsArguments for $sql -
int $lifetimeLifetime in seconds
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 $sqlSQL statement -
array $prmsArguments for the query -
int $lifetimeOptional Lifetime in seconds
Closes this datasource.
Definition: public function Close()
Returns: void
Returns the database name.
Definition: public function Database()
Returns: string The name
SHORTCUT for DataSource::ExecuteScalar DEPRECATED (2023/09) Use ExecuteScalar instead
Returns the last errormessage, if any.
Definition: public function ErrorMsg()
Returns: string The last error or false
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 $valueArgument to be escaped
SHORTCUT DataSource::ExecuteSql DEPRECATED (2023/09) Use ExecuteSql instead
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 $sqlSQL statement -
array|mixed $prmsArguments for $sql
Executes an SQL statement.
Definition: public function ExecuteSql($sql, $parameter)
Returns: ResultSet The query result
Parameters:
-
string $sqlSQL statement -
mixed $parameterArguments
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 $nameAliasname for the datasource or (default) false to get the default datasource
Gets the amount of rows affected by the last query.
Definition: public function getAffectedRowsCount()
Returns: int Number of affected rows
Returns the DSN
Definition: public function GetDsn()
Returns: string The Dsn
SHORTCUT system_get_lock
SHORTCUT DataSource::ExecuteScalar DEPRECATED (2023/09) Use ExecuteScalar instead
Returns the database host.
Definition: public function Host()
Returns: string The host or false (for example sqlite has no host)
Returns the id of the last inserted row.
Definition: public function LastInsertId($table=null)
Returns: mixed The last insert id
Parameters:
-
string $tableThe table to get last insert id for
SHORTCUT ResultSet::LogDebug
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 $typeType of Model class to create -
array $fieldsData for the new model, keys must be columns, values will be assigned -
bool $as_newIf true treats created Model as new instead of as if it was loaded from database.
Return now how the database sees it.
Definition: public function Now($seconds_to_add)
Returns: string String representing now
Parameters:
-
int $seconds_to_addOffset to now in seconds, may be negative too.
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 $sqlSQL statement -
int $items_per_pageItems per page -
int $pagePage number (1-based!) -
array $parameterSQL arguments
Returns the database password.
Definition: public function Password()
Returns: string The password
Prepares a statement
Definition: public function Prepare($sql)
Returns: ResultSet Prepared statement
Parameters:
-
string $sqlSQL statement
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 $tablenameName of table to query
Quotes an argument
Definition: public function QuoteArgument($value)
Returns: string The quoted argument
Parameters:
-
string $valueThe argument to quote
Quotes a column name
Definition: public function QuoteColumnName($column)
Returns: string The quoted column name
Parameters:
-
string $columnThe column name to quote
Reconnects to the database. Note: This should only be nessesary in long running processes.
Definition: public function Reconnect()
Returns: void
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 $sqlThe statement to be registered -
mixed $expected_runtime_secondsExtepcted runtime in seconds
SHORTCUT system_release_lock
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 $dsThe default datasource or it's aliasname
Checks if a table exists.
Definition: public function TableExists($name)
Returns: bool true or false
Parameters:
-
string $nameName of table to check
Returns the table where a Model is stored.
Definition: public function TableForType($type)
Returns: string Table name
Parameters:
-
string $typeClassname of Model to check
Returns the database username.
Definition: public function Username()
Returns: string The username