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