{"id":20678903,"url":"https://github.com/phpgt/sqlbuilder","last_synced_at":"2026-04-24T04:33:30.311Z","repository":{"id":47542779,"uuid":"151145745","full_name":"phpgt/SqlBuilder","owner":"phpgt","description":"[in development] Object oriented representation of SQL queries.","archived":false,"fork":false,"pushed_at":"2023-09-21T21:59:16.000Z","size":146,"stargazers_count":0,"open_issues_count":7,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-07T12:08:36.855Z","etag":null,"topics":["database","database-queries","fluent","fluent-api","oop","phpgt","query-builder","sql"],"latest_commit_sha":null,"homepage":"https://www.php.gt/sqlbuilder","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/phpgt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-01T19:23:05.000Z","updated_at":"2022-07-01T10:29:49.000Z","dependencies_parsed_at":"2024-11-16T21:25:35.597Z","dependency_job_id":"fdb71eb8-e59e-4a7d-9be3-3221910370a2","html_url":"https://github.com/phpgt/SqlBuilder","commit_stats":{"total_commits":61,"total_committers":2,"mean_commits":30.5,"dds":"0.016393442622950838","last_synced_commit":"45fb3c2364ce16c65f4094b6783d816649d8f8cf"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpgt%2FSqlBuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpgt%2FSqlBuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpgt%2FSqlBuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpgt%2FSqlBuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpgt","download_url":"https://codeload.github.com/phpgt/SqlBuilder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242911339,"owners_count":20205476,"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","database-queries","fluent","fluent-api","oop","phpgt","query-builder","sql"],"created_at":"2024-11-16T21:22:59.752Z","updated_at":"2026-04-24T04:33:30.304Z","avatar_url":"https://github.com/phpgt.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Object oriented representation of SQL queries.\n==============================================\n\nThis library does not generate any SQL, instead it provides an object oriented representation of SQL queries, allowing the structure of queries to be defined by the developer whilst gaining the benefits of inheritance.\n\nWhen a PHP application reaches scale, it is often the database that is the performance bottleneck. When queries are generated on behalf of the developer, they are often difficult to optimise because control is lost. With SqlBuilder, the developer is always in control of the raw SQL.\n\n***\n\n\u003ca href=\"https://github.com/PhpGt/SqlBuilder/actions\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/sqlbuilder-build.svg\" alt=\"Build status\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://app.codacy.com/gh/PhpGt/SqlBuilder\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/sqlbuilder-quality.svg\" alt=\"Code quality\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://app.codecov.io/gh/PhpGt/SqlBuilder\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/sqlbuilder-coverage.svg\" alt=\"Code coverage\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/PhpGt/SqlBuilder\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/sqlbuilder-version.svg\" alt=\"Current version\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"http://www.php.gt/sqlbuilder\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/sqlbuilder-docs.svg\" alt=\"PHP.Gt/SqlBuilder documentation\" /\u003e\n\u003c/a\u003e\n\nExample usage: a class that represents a SELECT query\n-----------------------------------------------------\n\nImagine a typical database application with a `student` table used to store details of each student. A basic select might look something like this:\n\n```sql\nselect\n\tid,\n\tforename,\n\tsurname,\n\tdateOfBirth\nfrom\n\tstudent\n```\n\nThe above query will return a list of all students. The problem here is that when you come to need to select from the student table again, this time with some constraints such as an age range or ordered by surname, the whole query will need to be repeated and only a small portion of the original query will need to be changed.\n\nInstead, the following class can be used to _represent_ the above query:\n\n```php\nclass StudentSelect extends SelectQuery {\n\tpublic function select():array {\n\t\treturn [\n\t\t\t\"id\",\n\t\t\t\"forename\",\n\t\t\t\"surname\",\n\t\t\t\"dateOfBirth\",\n\t\t];\n\t}\n\t\n\tpublic function from():array {\n\t\treturn [\n\t\t\t\"student\",\n\t\t];\n\t}\n}\n```\n\nThe `__toString` method of the above class will produce identical SQL to the original query.\n\nNow, to write another query that returns students of a certain age:\n\n```php\nclass StudentSelectByAge extends StudentSelect {\n\tpublic function where():array {\n\t\treturn [\n\t\t\t\"year(now()) - year(dateOfBirth) = :age\",\n\t\t];\n\t}\n}\n```\n\nExample usage: A fluent class that _builds_ a SELECT query (coming in v2 release)\n---------------------------------------------------------------------------------\n\nAs you can see in the example above, `SqlQuery` functions always return an array of expressions. The `SqlBuilder` classes have the same methods (`select`, `from`, `where`, etc.) but take the expressions as parameters, acting as a **[fluent interface][fluent]**. \n\nTo create the same query as in the example above with fluent syntax:\n\n```php\n$selectQuery = new SelectBuilder(\n\t\"id\",\n\t\"forename\",\n\t\"surname\",\n\t\"dateOfBirth\"\n)-\u003efrom(\n\t\"student\"\n)-\u003ewhere(\n\t\"year(now()) - year(dateOfBirth) = :age\"\n);\n```\n\nThis is particularly useful for when there is a base query, say `StudentSelect`, and your code only requires a single additional condition. Rather than having to create a separate class for this single usage, it can be called inline:\n\n```php\n// Start by using a base StudentSelect, then add a single inline condition to it.\n$studentSelect = new StudentSelect();\n\n$selectQuery = new SelectBuilder($studentSelect)\n-\u003ewhere(\n\t\"year(now()) - year(dateOfBirth) = :age\"\n);\n```\n\n\nConditionals\n------------\n\nExpressions within `where` and `having` clauses can be connected with logical operators, which are often combined. To avoid logical errors, `Condition` objects are used to specify the precedence of logic.\n\nFor example, to select students with a specific age _and_ gender:\n\n```php\nclass StudentSelectByAge extends StudentSelect {\n\tpublic function where():array {\n\t\treturn [\n\t\t\tnew AndCondition(\"year(now()) - year(dateOfBirth) = :age\"),\n\t\t\tnew AndCondition(\"gender = :gender\"),\n\t\t];\n\t}\n}\n```\n\nSubqueries\n----------\n\n`SqlQuery` objects have a `__toString()` function, and `SqlBuilder` results create `SqlQuery` instances. Because of this, they can be used in place of any other expression within a Query or Builder.\n\nLimitations of plain SQL\n------------------------\n\nThe only tools provided by plain SQL that can be used to write [DRY code][dry] are [views][view] and [stored procedures][stored-procedure], both of which have their own set of limitations when writing clean and maintainable code.\n\nThe solution provided by this library is to break down an SQL query into its different sections, represented by a PHP class which can be extended by other classes, while still retaining the plain SQL that is being represented.\n\nSQL compatibility\n-----------------\n\nThis library does not provide any SQL processing capabilities by design. Any driver-specific SQL used will not be compatible with other drivers. This allows the developer to fully utilise their SQL driver of choice, rather than generating _generic_ SQL.\n\n[dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself\n[view]: https://en.wikipedia.org/wiki/View_(SQL)\n[stored-procedure]: https://en.wikipedia.org/wiki/Stored_procedure\n[fluent]: https://en.wikipedia.org/wiki/Fluent_interface\n\n# Proudly sponsored by\n\n[JetBrains Open Source sponsorship program](https://www.jetbrains.com/community/opensource/)\n\n[![JetBrains logo.](https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.svg)](https://www.jetbrains.com/community/opensource/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpgt%2Fsqlbuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpgt%2Fsqlbuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpgt%2Fsqlbuilder/lists"}