registry
The functions registry_get
, registry_set
and registry_delete
implement a permanent registry for saving variables with their values.
Variables are registered in the table registry
of the DB.
`name` VARCHAR(100) NOT NULL,
`value` longtext NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The values of the variables are serialized.
registry_get
SYNOPSIS
registry_get($name, $default=false)
DESCRIPTION
registry_get
returns the value of the variable previously registered with the name $name
.
If $name
isn't a registered variable, or in case of error, registry_get
returns the value specified by $default
.
CODE
- function registry_get($name, $default=false) {
- $sqlname=db_sql_arg($name, false);
- $tabregistry=db_prefix_table('registry');
- $sql="SELECT value FROM $tabregistry WHERE name=$sqlname";
- $r = db_query($sql);
- return $r ? unserialize($r[0]['value']) : $default;
- }
registry_get
executes an SQL request which extracts the value of the variable $name
from the table registry
, then unserializes the returned value before returning it.
Note how the functions db_sql_arg
and db_prefix_table
are used.
If the variable $name
doesn't exist, registry_get
returns the value given by $default
which is false
by default.
registry_set
SYNOPSIS
registry_set($name, $value)
DESCRIPTION
registry_set
registers the variable $name
with the value $value
.
If $name
is already registered, its value is replaced.
registry_set
returns true
, or false
in case of error.
CODE
- function registry_set($name, $value) {
- $sqlname=db_sql_arg($name, false);
- $sqlvalue=db_sql_arg(serialize($value), true);
- $tabregistry=db_prefix_table('registry');
- $sql="INSERT INTO $tabregistry (name, value) SELECT * FROM (SELECT $sqlname AS name, $sqlvalue AS value) s WHERE NOT EXISTS (SELECT name FROM $tabregistry WHERE name=$sqlname)";
- $r = db_insert($sql);
- if ($r) {
- return true;
- }
- $sql="UPDATE $tabregistry SET name=$sqlname, value=$sqlvalue WHERE name=$sqlname";
- $r = db_update($sql);
- if ($r === false) {
- return false;
- }
- return true;
- }
registry_set
executes an SQL request which adds the variable $name
with the serialized value of $value
in the table registry
.
If $name
is already registered in the table, the SQL request does an update of its value.
registry_delete
SYNOPSIS
registry_delete($name)
DESCRIPTION
registry_delete
deletes the variable previously registered with the name $name
.
registry_delete
returns true
, or false
in case of error.
CODE
- function registry_delete($name) {
- $sqlname=db_sql_arg($name, false);
- $tabregistry=db_prefix_table('registry');
- $sql="DELETE FROM $tabregistry WHERE name=$sqlname";
- $r = db_delete($sql);
- if ($r === false) {
- return false;
- }
- return true;
- }
registry_delete
executes an SQL request which deletes the variable $name
from the table registry
.
Comments