Wednesday, 4 June 2014

Using mysqli in Xataface

Xataface now supports the new mysqli drivers.  Support has been added on GitHub and it will be incorporated into the next release.  For now (for upgrade compatibility), it will still use the default mysql driver, but you can explicitly tell it to use mysqli by adding the following to the [_database] section of your application's conf.ini file:

driver=mysqli

E.g.

[_database]
    host=localhost

    user=steve
    password=mypass
    name=dbname
    driver=mysqli

New Database Compatibility Layer


Adding support for mysqli whilst maintaining backward compatibility with the mysql driver was achieved by adding a new compatibility layer.  If you look through the changes to the Xataface source, you'll notice that all of the calls to mysql_xxx have been changed to xf_db_xxx.  E.g.

mysql_connect() was changed to xf_db_connect(), mysql_query() changed to xf_db_query(), etc..

These new functions are defined inside the xf/db/drivers directory.  Xataface loads the correct driver at the beginning of each request.

Automatic Conversion Tool


This change was made via this ANT build script that is now a part of the Xataface distribution.  It scours the entire source base looking for instances of the old mysql_xxx() functions, and it replaces them with the corresponding xf_db_xxx() function.

Example usage:

ant -f fix-deprecated-mysql-ant-task.xml

This will scour the current directory and all subdirectories.  If you want to run it on a particular base directory, you can use the -Dbasedir=PATH parameter.  E.g.

ant -Dbasedir=/Users/matt/files/myapp -f fix-deprecated-mysql-ant-task.xml

If you are using Xataface in an existing application that makes mysql_xxx() calls at any point, and you want to use the mysqli driver, then you'll need to update your application accordingly, as you cannot use both mysqli_xxx() functions and mysql_xxx() functions on the same database connection.

I'll be going through the existing Xataface modules in the next while to port them over to the new compatibility layer, but if you run into issues with a module before I get to it, you should be able to just run the ANT task on the module's directory and it will be updated automatically.

No comments:

Post a Comment