{"id":20882264,"url":"https://github.com/girgias/query-builder","last_synced_at":"2025-05-12T18:30:49.865Z","repository":{"id":62510929,"uuid":"162196193","full_name":"Girgias/query-builder","owner":"Girgias","description":"Library to build valid SQL queries","archived":false,"fork":false,"pushed_at":"2019-02-10T15:31:46.000Z","size":55,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T08:51:25.292Z","etag":null,"topics":["database","php","query-builder","sql"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Girgias.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-17T22:13:36.000Z","updated_at":"2024-06-14T19:40:58.000Z","dependencies_parsed_at":"2022-11-02T12:46:59.118Z","dependency_job_id":null,"html_url":"https://github.com/Girgias/query-builder","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Girgias%2Fquery-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Girgias%2Fquery-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Girgias%2Fquery-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Girgias%2Fquery-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Girgias","download_url":"https://codeload.github.com/Girgias/query-builder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253797895,"owners_count":21965976,"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":["database","php","query-builder","sql"],"created_at":"2024-11-18T07:30:32.317Z","updated_at":"2025-05-12T18:30:49.558Z","avatar_url":"https://github.com/Girgias.png","language":"PHP","readme":"# SQL Query Builder\n[![Build Status](https://travis-ci.org/Girgias/query-builder.svg?branch=master)](https://travis-ci.org/Girgias/query-builder)\n[![Maintainability](https://api.codeclimate.com/v1/badges/e804486b68df4080cead/maintainability)](https://codeclimate.com/github/Girgias/query-builder/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/e804486b68df4080cead/test_coverage)](https://codeclimate.com/github/Girgias/query-builder/test_coverage)\n\nA fluent SQL Query Builder which **ONLY** builds a valid SQL query with the SQL \nclauses it has been asked to provide.\n\n## Installing\n\n```shell\ncomposer require girgias/query-builder\n```\n\n## Features\n\nThis Query Builder can build a variety of SQL queries which are database agnostic\nas it uses the ANSI standardized syntax.\n\nEvery sort of query has its own class which extends from the base ``Query`` class,\nThey all have the same constructor signature which requires the table name \non which to execute the query.\n\nTo build a SELECT query with table join use the ``SelectJoin`` class.\nAn example can be seen below on how to use this class.\n\nIt is possible to directly provide scalar values to WHERE clauses and while binding a field.\nIt is also possible to specify the named parameter against which the value will be bounded.\nIn case no named parameter has been provided a random one will be generated.\n\nTo retrieve the named parameters with their associated values use the ``getParameters``\nmethod which will return an associative array ``parameter =\u003e value``.\n\n### Examples\nA basic ``SELECT`` query:\n```php\n$query = (new \\Girgias\\QueryBuilder\\Select('demo'))\n    -\u003elimit(10, 20)\n    -\u003eorder('published_date')\n    -\u003egetQuery();\n```\nWill output:\n```sql\nSELECT * FROM demo ORDER BY published_date ASC LIMIT 10 OFFSET 20\n```\n\nA more complex ``SELECT`` query:\n```php\n$start = new \\DateTime('01/01/2016');\n$end = new \\DateTime('01/01/2017');\n$query = (new \\Girgias\\QueryBuilder\\Select('demo'))\n    -\u003eselect('title', 'slug')\n    -\u003eselectAs('name_author_post', 'author')\n    -\u003ewhereBetween('date_published', $start, $end)\n    -\u003eorder('date_published', 'DESC')\n    -\u003elimit(25)\n    -\u003egetQuery();\n```\n\nWill output:\n```sql\nSELECT title, slug, name_author_post AS author FROM demo WHERE date_published BETWEEN '2016-01-01 00:00:00' AND '2017-01-01 00:00:00' ORDER BY date_published DESC LIMIT 25\n```\n\nAn example with the ``whereOr`` method:\n```php\n$query = (new \\Girgias\\QueryBuilder\\Select('demo'))\n    -\u003ewhere('author', '=', 'Alice', 'author')\n    -\u003ewhereOr('editor', '=', 'Alice', 'editor')\n    -\u003egetQuery();\n```\n\nWill output:\n```sql\nSELECT * FROM demo WHERE (author = :author OR editor = :editor)\n```\n\n``UPDATE`` query example:\n```php\n$query = (new \\Girgias\\QueryBuilder\\Update('posts'))\n    -\u003ewhere('id', '=', 1, 'id')\n    -\u003ebindField('title', 'This is a title', 'title')\n    -\u003ebindField('content', 'Hello World', 'content')\n    -\u003ebindField('date_last_edited', (new \\DateTimeImmutable()), 'nowDate')\n    -\u003egetQuery();\n```\n\nWill output:\n```sql\nUPDATE posts SET title = :title, content = :content, date_last_edited = :now_date WHERE id = :id\n```\n\nA ``SELECT`` query with an ``INNER JOIN``:\n```php\n$query = (new \\Girgias\\QueryBuilder\\SelectJoin('comments', 'posts'))\n    -\u003etableAlias('co')\n    -\u003eselect('co.user', 'co.content', 'p.title')\n    -\u003ejoinTableAlias('p')\n    -\u003einnerJoin('post_id', 'id')\n    -\u003egetQuery();\n```\n\nWill output:\n```sql\nSELECT co.user, co.content, p.title FROM comments AS co INNER JOIN posts AS p ON comments.post_id = posts.id\n```\n\n## Future scope\n\nPossible features that will be added to this library\n\n* WHERE subqueries \n\n## Contributing\n\nIf you found an invalid SQL name which **DOESN'T** throw a Runtime exception\nor a valid SQL name which does please add a test case into the \n``tests/CheckSqlNamesTest.php`` file.\n\nIf you found an example where this library returns an invalid SQL query\nplease add (or fix) a test case in the relevant Query test case or if it's\na general error please use the ``tests/QueryTest`` file.\n\nIf a RunTime exception should be thrown please add a test in the relevant test file\nor if it is specific to ``SELECT`` Query please add a test in the\n``tests/SelectThrowExceptionsTest.php`` file.\n\nIf you'd like to contribute, please fork the repository and use a feature\nbranch. Pull requests are warmly welcome.\n\n### Notes\nWhen contributing please assure that Psalm runs without error\nand all unit tests pass.\nMoreover if you add functionality please add corresponding unit tests to cover\nat least 90% of your code and that these tests cover any edge cases if they exist.\n\n## Links\n\n- Repository: https://github.com/girgias/query-builder/\n- Issue tracker: https://github.com/girgias/query-builder/issues\n  - In case of sensitive bugs like security vulnerabilities, please contact\n    george.banyard@gmail.com directly instead of using the issue tracker.\n\n\n## Licensing\n\nThe code in this project is licensed under MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgirgias%2Fquery-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgirgias%2Fquery-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgirgias%2Fquery-builder/lists"}