48

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.

CREATE TABLE `izend_registry` (
  `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
  1. function registry_get($name, $default=false) {
  2.     $sqlname=db_sql_arg($name, false);
  3.  
  4.     $tabregistry=db_prefix_table('registry');
  5.  
  6.     $sql="SELECT value FROM $tabregistry WHERE name=$sqlname";
  7.  
  8.     $r = db_query($sql);
  9.  
  10.     return $r ? unserialize($r[0]['value']) : $default;
  11. }

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
  1. function registry_set($name, $value) {
  2.     $sqlname=db_sql_arg($name, false);
  3.     $sqlvalue=db_sql_arg(serialize($value), true);
  4.  
  5.     $tabregistry=db_prefix_table('registry');
  6.  
  7.     $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)";
  8.  
  9.     $r = db_insert($sql);
  10.  
  11.     if ($r) {
  12.         return true;
  13.     }
  14.  
  15.     $sql="UPDATE $tabregistry SET name=$sqlname, value=$sqlvalue WHERE name=$sqlname";
  16.  
  17.     $r = db_update($sql);
  18.  
  19.     if ($r === false) {
  20.         return false;
  21.     }
  22.  
  23.     return true;
  24. }

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
  1. function registry_delete($name) {
  2.     $sqlname=db_sql_arg($name, false);
  3.  
  4.     $tabregistry=db_prefix_table('registry');
  5.  
  6.     $sql="DELETE FROM $tabregistry WHERE name=$sqlname";
  7.  
  8.     $r = db_delete($sql);
  9.  
  10.     if ($r === false) {
  11.         return false;
  12.     }
  13.  
  14.     return true;
  15. }

registry_delete executes an SQL request which deletes the variable $name from the table registry.

SEE ALSO

pdo, cron

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].