Convert SQL Query Into Dynamic Query Using Query Coder Module

Query coder allows you to convert SQL queries into dynamic queries. The module offers a simple UI where you can paste in a SQL query, press submit and then you’re presented with a dynamic query code example.

In Drupal 7, you can query the database in two ways; using a static or a dynamic query.
Static queries are the simplest to write and the fastest from a performance stand point. A static query will suffice for general SELECT queries.

Dynamic queries on the other hand can be tricky and takes some getting used to. One benefit of using it is that other modules can modify the query by implementing hook_query_alter.

Getting Started

The module is fairly easy to setup, simply download and enable Query coder and Libraries API. The module also requires the PHP SQL Parser library. This library needs to be extracted and copied into the libraries directory.

How To Use

Once the module is installed, go to Configuration -> “Query coder”.

Fig 1.1

Simply paste in some SQL code and click on Submit.

Fig 1.1

As an example paste in the query below:

SELECT n.nid, n.title
FROM node n
WHERE n.type = 'article';

It’s important to note that when you write a SELECT query make sure your queries have aliases (FROM node n). Read the “Known issues” section on the project page.

After you click on Submit, the code below should be returned:

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title'));
$query->condition('n.type', 'article');
$result = $query->execute();

The module can also convert INSERT, UPDATE and DELETE queries into dynamic queries. If you need more example SQL queries, look in the query_coder.test file. In the file, you’ll find example SQL queries that work – one reason why tests are useful.

6 thoughts on “Convert SQL Query Into Dynamic Query Using Query Coder Module”

  1. Jose Alvarez de Lara

    Hi,

    I am using your example query,

    SELECT n.nid, n.title
    FROM node n
    WHERE n.type = ‘article’;

    and when I click on Submit button the result is as follows,

    Error, please download PHP-SQL-Parser library and unzip it to “sites/all/libraries” folder. Full path must be “sites/all/libraries/PHP-SQL-Parser/php-sql-parser.php”.

    and I have that librarie installed. What is going wrong?

    1. The only thing I can recommend is that you double check everything. Make sure Libraries module is installed and that the PHP-SQL-Parser library is in the right place.

  2. SELECT node.nid AS nid, node.title AS node_title, node.created AS node_created, ‘node’ AS field_data_vccnews_tags_node_entity_type
    FROM
    node
    WHERE (( (node.status = ‘1’) AND (node.type IN (‘vccfreenews’, ‘vccnews’)) AND (node.created > 1218088901) ))
    ORDER BY node_created ASC
    iam using this query and when I click on Submit button the result is as follows, Error, please download PHP-SQL-Parser library and unzip it to “sites/all/libraries” folder. Full path must be “sites/all/libraries/PHP-SQL-Parser/php-sql-parser.php”. and I have that librarie installed.
    whats the problem

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.