|
| 1 | +What is QueryWritter? |
| 2 | +===================== |
| 3 | + |
| 4 | +QueryWritter is a PHP library that parses SQL queries, transforms those into an object representation, stores them in a |
| 5 | +dependency injection container, and returns them as string. It is a [Mouf plugin](http://mouf-php.com). |
| 6 | + |
| 7 | +Ok, but why would I use QueryWritter? |
| 8 | +------------------------------------- |
| 9 | + |
| 10 | +Because it is **the most effecient way to deal with queries that can have a variable number of parameters**! |
| 11 | +Think about a typical datagrid with a bunch of filter (for instance a list of products filtered by name, company, price, ...). |
| 12 | +If you have the very common idea to generate the SQL query using no PHP library, your code will look like this: |
| 13 | + |
| 14 | +<div class="alert"><strong>You should not do this!</strong></div> |
| 15 | + |
| 16 | +```php |
| 17 | +// People usually write queries like this: |
| 18 | +$sql = "SELECT * FROM products p JOIN companies c ON p.company_id = c.id WHERE 1=1 "; |
| 19 | +// They keep testing for parameters, and concatenating strings.... |
| 20 | +if (isset($params['name'])) { |
| 21 | + $sql .= "AND p.name LIKE '".addslashes($params['name'])."%'"; |
| 22 | +} |
| 23 | +if (isset($params['company'])) { |
| 24 | + $sql .= "AND c.name LIKE '".addslashes($params['company'])."%'"; |
| 25 | +} |
| 26 | +// And so on... for each parameter, we have a "if" statement |
| 27 | +``` |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +Concatenating SQL queries is dangerous (especially if you forget to protect parameters). |
| 32 | +You can always use parameterized SQL queries, but you will still have to concatenate the filters. |
| 33 | + |
| 34 | +To avoid concatenating strings, frameworks and libraries have used different strategies. Building a full ORM (like |
| 35 | +Doctrine or Propel) is a good idea, but it makes writing complex queries even more complex. Other frameworks like |
| 36 | +Zend are building queries using function calls. These are valid strategies, but you are no more typing SQL queries |
| 37 | +directly, and let's face it, it is always useful to use a query directly. |
| 38 | + |
| 39 | +This is where QueryWritter becomes helpful. |
| 40 | + |
| 41 | +How does it work? |
| 42 | +----------------- |
| 43 | +// TODO: schema... or even better... video! |
| 44 | + |
| 45 | +###1- Write your query |
| 46 | +You start by writing your query, **in plain SQL**. No ORM, no special query language (DQL or HQL anyone?), just plain and simple SQL. |
| 47 | +This is cool because everybody knows SQL. In your query, you put absolutely all the parameters you can imagine. |
| 48 | + |
| 49 | +###2- Store your query in Mouf |
| 50 | +In Mouf UI, go to **DB** > **SQL queries** > **Create SQL query**. |
| 51 | +Here, you can **copy and paste your query**. Since this is Mouf, every query is an "instance", and you have to pick |
| 52 | +a name for your query. |
| 53 | + |
| 54 | +Behind the scenes, QueryWritter will parse your query and make sure every piece of the query (each table, each column, each filter...) is transformed |
| 55 | +into an object. But you really don't have to care about that right now. |
| 56 | + |
| 57 | +###3- Test your query |
| 58 | +Right from Mouf UI, you can test your query! And lo and behold! Because the query was parsed, **QueryWritter will dynamically |
| 59 | +add parts of the query depending on the parameters you decide to use**. |
| 60 | + |
| 61 | +###4- Use it in your code |
| 62 | +If you are not a Mouf user (if you are using Drupal, Symfony, Zend Framework...), you can directly use the query by fetching the instance from Mouf and calling the <code>toSql</code> method, passing |
| 63 | +parameters in... parameter :) |
| 64 | + |
| 65 | +``` |
| 66 | +$mySelect = Mouf::getMySelectStatement(); |
| 67 | +$sql = $mySelect->toSql(array("status"=>1, "search"=>"hello")); |
| 68 | +``` |
| 69 | + |
| 70 | +If you are a Mouf user, you can even directly run the query using the **QueryResult** class that executes |
| 71 | +the query directly. Or even better, use the **Evolugrid** module, and display your query result in an HTML |
| 72 | +datagrid, directly! |
0 commit comments