DbProxy

Description

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.

Features list

Note: The module is built on top of the ParamsProxy module, therefore it inherits its security model, base functionality and limitations.

* this implies that the module is required for every single operation that is to be made on the database. This limitation is assumed.

How it works

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

XML format

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:

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.

MySQL Query Results Interpretation

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:

  1. Is the result FALSE and the last MySQL error was not an empty string? Re-throw the MySQL exception in PHP and exit.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

Known Limitations, Gotchas