{"id":18929484,"url":"https://github.com/thecodingmachine/database.querywriter","last_synced_at":"2025-04-15T15:31:03.002Z","repository":{"id":6341093,"uuid":"7577138","full_name":"thecodingmachine/database.querywriter","owner":"thecodingmachine","description":"This package contains classes useful to generate SQL statements such as SELECT queries, etc...","archived":false,"fork":false,"pushed_at":"2018-10-23T15:04:21.000Z","size":194,"stargazers_count":0,"open_issues_count":0,"forks_count":3,"subscribers_count":8,"default_branch":"4.0","last_synced_at":"2025-04-11T18:59:46.804Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-01-12T15:56:55.000Z","updated_at":"2018-10-23T15:01:58.000Z","dependencies_parsed_at":"2022-09-09T03:01:04.172Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/database.querywriter","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fdatabase.querywriter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fdatabase.querywriter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fdatabase.querywriter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fdatabase.querywriter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/database.querywriter/tar.gz/refs/heads/4.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249097851,"owners_count":21212362,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:32:57.799Z","updated_at":"2025-04-15T15:31:02.767Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"What is QueryWriter?\n=====================\n\nQueryWritter is a PHP library that parses SQL queries, transforms those into an object representation, stores them in a \ndependency injection container, and returns them as string. It is a [Mouf plugin](http://mouf-php.com).\n\nIt is heavily based on **mouf/magic-query**. Actually, magic-query's code was extracted from query-writer. \n\nOk, but why would I use QueryWritter?\n-------------------------------------\n\nBecause it is **the most effecient way to deal with queries that can have a variable number of parameters**!\nThink about a typical datagrid with a bunch of filter (for instance a list of products filtered by name, company, price, ...).\nIf you have the very common idea to generate the SQL query using no PHP library, your code will look like this:\n\n\u003cdiv class=\"alert\"\u003e\u003cstrong\u003eYou should not do this!\u003c/strong\u003e\u003c/div\u003e\n\n```php\n// People usually write queries like this:\n$sql = \"SELECT * FROM products p JOIN companies c ON p.company_id = c.id WHERE 1=1 \";\n// They keep testing for parameters, and concatenating strings....\nif (isset($params['name'])) {\n\t$sql .= \"AND p.name LIKE '\".addslashes($params['name']).\"%'\";\n}\nif (isset($params['company'])) {\n\t$sql .= \"AND c.name LIKE '\".addslashes($params['company']).\"%'\";\n}\n// And so on... for each parameter, we have a \"if\" statement\n```\n\n\n\nConcatenating SQL queries is dangerous (especially if you forget to protect parameters).\nYou can always use parameterized SQL queries, but you will still have to concatenate the filters.\n\nTo avoid concatenating strings, frameworks and libraries have used different strategies. Building a full ORM (like\nDoctrine or Propel) is a good idea, but it makes writing complex queries even more complex. Other frameworks like\nZend are building queries using function calls. These are valid strategies, but you are no more typing SQL queries\ndirectly, and let's face it, it is always useful to use a query directly.\n\nThis is where QueryWritter becomes helpful.\n\nHow does it work?\n-----------------\n// TODO: schema... or even better... video!\n\n###1- Write your query\nYou start by writing your query, **in plain SQL**. No ORM, no special query language (DQL or HQL anyone?), just plain and simple SQL.\nThis is cool because everybody knows SQL. In your query, you put absolutely all the parameters you can imagine.\n\n###2- Store your query in Mouf\nIn Mouf UI, go to **DB** \u003e **SQL queries** \u003e **Create SQL query**.\nHere, you can **copy and paste your query**. Since this is Mouf, every query is an \"instance\", and you have to pick\na name for your query. \n\nBehind the scenes, QueryWritter will parse your query and make sure every piece of the query (each table, each column, each filter...) is transformed \ninto an object. But you really don't have to care about that right now.\n\n###3- Test your query\nRight from Mouf UI, you can test your query! And lo and behold! Because the query was parsed, **QueryWritter will dynamically \nadd parts of the query depending on the parameters you decide to use**.\n\n###4- Use it in your code\nIf 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 \u003ccode\u003etoSql\u003c/code\u003e method, passing \nparameters in... parameter :)\n\n```\n$mySelect = Mouf::getMySelectStatement();\n$sql = $mySelect-\u003etoSql(array(\"status\"=\u003e1, \"search\"=\u003e\"hello\"));\n```\n\nIf you are a Mouf user, you can even directly run the query using the **QueryResult** class that executes\nthe query directly. Or even better, use the **Evolugrid** module, and display your query result in an HTML\ndatagrid, directly!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fdatabase.querywriter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Fdatabase.querywriter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fdatabase.querywriter/lists"}