Recommend this page to a friend! |
Classes of Christian Vigh | PHP Database Variable Store | README.md | Download |
|
DownloadINTRODUCTIONWe all wanted to be able to store and retrieve application parameters to and from a database table dedicated to this sole purpose. If you're as lazy as I am, you probably won't want to multiply SQL queries throughout your code but rather access your parameters as if they were grouped together in an array or object, just to keep your code clean and readable. And if you're running in a multitasking environment, you'll probably want your parameters to be accurate each time you retrieve their value ; and that all the modifications you made become instantly available to all other processes. This is what the DbVariableStore class is aimed at, although this is yet-another-variable-store-table-access class : to provide you with an easy way to create variable store tables, and to easily create, update, access or delete them ; and also provide the syntactic sugar that will make you forget that you are manipulating values that are stored in an underlying SQL table. Note that this class uses MYSQL. A BASIC SAMPLEPerhaps the most basic sample code would be the following : it creates a variable store, then defines the 'RunMode' variable to 'production' ; note that the variable is created as a string value, but you will discover later in the following sections that several other types are available :
Note that step 3) could also be written as :
Or even :
You can even store complex objects :
which will display :
CLASS VERSATILITYthe DbVariableStore has to be seen as some kind of dictionary holding key/value pairs (the keys being the variable names). As such, provisions has been made so that you can access or set variable values using several ways :
(in the above example, the variable having index 0 is supposed to be 'RunMode'). Of course, this last access method is provided here only for consistency and completeness ; but keep in mind that this may not be the fastest one (or even the most readable one). IMPLEMENTATIONThe variable store table has the following structure :
Where \\table\_name\\and \\length\\ are parameters specified to the DbVariableStore class constructor. The meaning of the various columns is described below :
Note that in the current version, the creation\_time and update\_time columns are updated when needed, but cannot be accessed using the class methods. DOCUMENTATIONOVERVIEWVariable names are case-insensitive, can contain any kind of characters and must be unique within a variable store (the indexes of the variable store table do not allow for multiple variables with the same name). Each variable has a type, which is type String by default (a string can hold unprocessed data of any length). Additional types are available, including one which allows you to store/retrieve serialized values without caring for conversion aspects. You can have a look at the Variable Types section for more explanation on variable types. APIConstructorThe class constructor has the following signature :
Where the arguments are the following :
Note that instanciations specifying an already existing variable store table will not recreate it. You have to manually execute the DROP TABLE SQL statement if you really want to recreate your variable store. Defining a variableTo define a variable, use the Define() method :
This defines a variable $name and assigns the value $value to it. By default, all variable values are considered as strings, unless you specify a different type through the $type parameter. You can have a look at the Variable Types section for more information on the supported variable types. If the variable already exists, its value will be updated, unless the type you specified differs from its underlying type stored in the database table ; in this case, an exception will be thrown. This method returns true if a new variable has been created, or false if an existing variable value was updated. Note that there are other ways to define/update variables :
Integer indexes start from zero. You cannot create new variables using the integer-indexed array syntax. However you can create new variables using the object property and the associative array syntaxes ; the created values will be of type String (see the Variable types section for an explanation on variable types). Checking for variable existenceYou can check for variable existence using the following method :
This will return true if the variable $name exists, and false otherwise. Deleting a variableUse the following method to delete a variable :
This will return true if the variable did already exist and was deleted, and false otherwise. Retrieving variable valuesRetrieving a variable value can be done in several ways :
Integer indexes start from zero. Retrieving variable namesUse the following method to retrieve the names of the variables defined in your variable store :
The result is an array holding the variable names defined in the variable store. Items are always sorted in ascending order. If a pattern is specified, then variable names will be filtered against this value, which can be any expression supported by the SQL LIKE operator. VARIABLE TYPESVariables can be assigned a type, which is defined by one of the DbVariableStore::TYPE\_xxx constants. Variable types allow for automatic on-the-fly conversions when storing/retrieving values. All the variables you create using the object property or associative array access methods will have the DbVariableStore::TYPE\_STRING type, as in the following example :
You have to use the Define() method to specify a type other than the default one, as in :
The following types are available :
The default type for all new values. Variable values of this type will be stored and retrieved as is, without any interpretation.
Integer value. Boolean and floating-point values will be converted to an integer ; any other kind of value will generate an exception.
Any other kind of value will generate an exception.
A floating-point, boolean or integer value, which will be converted to a floating-point value. Any other kind of value will generate an exception.
The value will be stored using the following date() format : Y/m/d H:i:s. The value supplied when defining the variable can either be a Unix timestamp or a string that can be understood by the strtotime() function. Any other kind of value will generate an exception.
The value will be stored using the following date() format : Y/m/d. The value supplied when defining the variable can either be a Unix timestamp or a string that can be understood by the strtotime() function. Any other kind of value will generate an exception.
The value will be stored using the following date() format : H:i:s. The value supplied when defining the variable can either be a Unix timestamp or a string that can be understood by the strtotime() function. Any other kind of value will generate an exception.
The value will be stored using as a Unix timestamp, which is the number of seconds elapsed since January 1st, 1970 at 0h. The value supplied when defining the variable can either be a Unix timestamp or a string that can be understood by the strtotime() function. Any other kind of value will generate an exception.
The supplied value will be serialized before storing it into the variable store, and unserialized when retrieving it. It can be used to hold any kind of complex structures such as objects or arrays. DERIVINGDerived classes may overload defined methods, especially the one that creates the underlying variable store table, Create ; below are the actual contents of the Create() method :
ADDING NEW DATA TYPESAlthough the defined types should answer most of your needs, there may still be cases where you might want to implement your own one(s). To do this, you must either modify the source code of the DbVariableStore class or inherit from it, then :
|