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”.
Simply paste in some SQL code and click on Submit.
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.
Nice Post thanks for sharing.
That looks like a fun module to play with. Thanks Ivan.
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?
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.
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
The module can’t find the library. There could be a number of reasons; the folder of the library is not correct or the folder has the wrong permissions.
Look in the project issue queue and see if others have the same problem, https://www.drupal.org/project/issues/query_coder?status=All&categories=All
You’ll need to debug this further.