Takes an XML file, parses it and dynamically creates specific wrapper functions (aka stubs) that connect to a database to read/write data.
Note: currently, the only supported database is MySQL. There are currently no plans to support a different database type.
Note: The module is built on top of the ParamsProxy module, therefore it inherits its security model, base functionality and limitations.
Provides a way to effectively separate program logic from data exchange logic.
Built-in protection against SQL injections: every char of user provided information is first encoded*, and only then sent to database, effectively making futile any attempt of naïve SQL injection.
* this implies that the module is required for every single operation that is to be made on the database. This limitation is assumed.
The module uses the PHP5 overloading feature to create specific stubs on the fly based on settings in the provided XML file.
The following section describes in detail the inner operation of the module.
The class that uses module's abilities (from this point onward called client class) calls the relevant API method configure() on the unique instance of the module. Ex:
$dbProxy = new DbProxy(); $dbProxy->configure();
Note: The module must be called from within the execution context of another class. It will not work if called from the global scope.
This will attempt to locate and parse a corresponding XML database configuration file bound to the client class. Upon success, stubs data will be saved, based on the XML file's content.
The basics on configuration files and their location are covered in the ParamsProxy.html document.
The name of the data base configuration file is dbAccess.xml and is to be found at <configuration files folder>/<client class name>/dbAccess.xml.
The client class issues a call to a stub name. Ex:
$dbProxy = new DbProxy(); $dbProxy->getEmployees (“employees”, “sex”, “M”);
The module's class catches the call to a non-existing method in its magic “__call()” function and attempts to locate/match a corresponding stub for the client class.
Note: the module uses the PHP debug trace feature to determine the client class name.
If the match succeeds, the specific stub is run with arguments provided by the client class, and results of the database query are interpreted and passed back.
Note: “interpretation” of data base results is controlled on a per-stub basis, in the XML database configuration file.
Any failures in the above scenario, including SQL syntax errors will produce fatal errors, thus forcing the developer to finely tune SQL query templates and/or provided arguments.
There is no custom XML format imposed by the module. The module expects data in the default configuration file format, as defined by the ParamsProxy.html document.
The general form of a database configuration file follows (bold values must be given literally):
<config for="MyClass"> <!-- Connection data for class MyClass --> <param> <name>connection</name> <type>array</type> <value> <item key="dbName" type="string">MyDatabaseName</item> <item key="dbHost" type="string">127.0.0.1</item> <item key="dbUser" type="string">JonDoe</item> <item key="dbPassword" type="string">1234</item> </value> </param> <!-- Sample stub definition. --> <param> <name>getEmployees</name> <type>array</type> <value> <item key="queryParams"> MY_TABLE, MY_COLUMN, MY_VALUE </item> <item key="queryTemplate"> <![CDATA[ SELECT * FROM %MY_TABLE% WHERE `%COLUMN%` = "%MY_VALUE%"; ]]> </item> <item key="queryResultType" type="string">array</item> </value> </param> </config>
Although the module performs no syntax validation (as it is handled by the underlying ParamsProxy module, it will actively validate form and content-wise data passed in, and produce fatal errors in case of failure.
Following items are subject of validation:
parameters and function names must match /^[_a-zA-Z]+[_a-zA-Z0-9]*$/;
no parameter or function may be named “connection”, regardless of case;
there must be one and only one “connection” parameter;
there cannot be two parameters with the same name;
within the “connection”:
all items, with their given keys are required and mustn't be null or the empty string;
a formally valid host name or IP address is required; localhost is tolerated;
only the main Latin letters subset plus underscore is allowed for user name; the root user name is also forbidden;
only the printable chars of the main ASCII set are allowed for user password, with a few notable exceptions: the backslash, apostrophe, double quotes, dash, and back tick chars.
Note: this implies that the module cannot be used on machines that only provide root access to the MySQL server, or on some publicly accessible MySQL servers. This limitation is assumed.
The module itself requires no configuration.
The actual value(s) returned by the module to the calling function heavily depend on the queryResultType setting of a specific stub. Except for the “array” setting, there is a bit of heuristic involved to obtain a response of the expected type. The algorithm is outlined below:
Is the result FALSE and the last MySQL error was not an empty string? Re-throw the MySQL exception in PHP and exit.
Our caller is expecting a boolean; is the result FALSE? Return false. Is the result TRUE and we have had either affectedRows or numRows greater than 0? Return true. None of the above? Return false.
Our caller is expecting a string; is the result comprised of a single row having a single cell? Return it as a string. Is the result comprised of several rows, having one or several cells each? Concatenate all as a HTTP GET string and return it. None of the above? Return the empty string.
Our caller is expecting a number; is the result comprised of a single row having a single cell? Is that value numeric? Cast it to an int or float and return it. Or, get the largest value of either affectedRows or numRows and return it.
Our caller is expecting an array; there is nothing to assume/guess. Return the result as a multidimensional array.
Note: the module will only return number-indexed array (not associative, nor double-indexed). The rationale behind that is that the client code must have a good idea on the database structure, and returning the column names would be futile.
The queryResultType must be given as a string:
Ex: <item key="queryResultType" type="string">null</item>
will set the queryResultType to "null", while:
Ex: <item key="queryResultType">null</item>
will set it to "" (empty string).
The queryResultType can only be “string”, “boolean”, “number”, “array” and “null”. Unlike ParamsProxy, the module does not provide an “auto” type. If left unspecified – i.e., the entire <item key="queryResultType"...> node is missing – “array” is assumed.
You must escape all values, even if they're not reserved words:
Wrong: SELECT * FROM %MY_TABLE% WHERE %MY_COLUMN% = %MY_VALUE%;
Right: SELECT * FROM `%MY_TABLE%` WHERE `%MY_COLUMN%` = "%MY_VALUE%".
Don't place XML comments within the queryTemplate item node. Due to a limitation in the underlying ParamsProxy module, they will become part of the template (and usually cause a fatal error).
If a stub is sent an integer or float as a parameter, it will be passed to MySQL unencoded, so that arithmetic operations in queries can be achieved. Send the parameter as string if you intend to match it literally, within a larger string.
Despite the fact that PHP returns strings for all MySQL types except the NULL type, the module will actually return integer or floats for MySQL numeric types (provided they fit in the int or float ranges on the current host – otherwise, strings will be returned).
The module expects to be given UTF-8 strings.
Due to encoding, identifiers length has been truncated to half (32 chars instead of 64 for table names, etc.)