{"id":28906654,"url":"https://github.com/belchior/sql_query_builder","last_synced_at":"2026-02-21T15:07:27.144Z","repository":{"id":43645664,"uuid":"484279333","full_name":"belchior/sql_query_builder","owner":"belchior","description":"Write SQL queries in a simple and composable way","archived":false,"fork":false,"pushed_at":"2026-02-18T18:58:01.000Z","size":375,"stargazers_count":72,"open_issues_count":3,"forks_count":8,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-18T22:43:34.500Z","etag":null,"topics":["mysql","postgresql","query-builder","sql","sqlite"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/sql_query_builder","language":"Rust","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/belchior.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-04-22T03:11:46.000Z","updated_at":"2026-01-27T17:58:31.000Z","dependencies_parsed_at":"2024-04-16T11:34:24.118Z","dependency_job_id":"1bfef62b-2c71-4572-bbb2-aaae0949a30b","html_url":"https://github.com/belchior/sql_query_builder","commit_stats":{"total_commits":67,"total_committers":3,"mean_commits":"22.333333333333332","dds":0.02985074626865669,"last_synced_commit":"9fdbc39bacd27c263f770d4df097a19dd5378759"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"purl":"pkg:github/belchior/sql_query_builder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belchior%2Fsql_query_builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belchior%2Fsql_query_builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belchior%2Fsql_query_builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belchior%2Fsql_query_builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/belchior","download_url":"https://codeload.github.com/belchior/sql_query_builder/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belchior%2Fsql_query_builder/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29684107,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-21T14:31:22.911Z","status":"ssl_error","status_checked_at":"2026-02-21T14:31:22.570Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["mysql","postgresql","query-builder","sql","sqlite"],"created_at":"2025-06-21T15:08:17.572Z","updated_at":"2026-02-21T15:07:27.137Z","avatar_url":"https://github.com/belchior.png","language":"Rust","readme":"Write SQL queries in a simple and composable way.\n\nThe main goal is to find the best balance between write idiomatic SQL queries and manage scenarios\nof complex query composition mixed with conditional clauses.\n\n\n## Quick Start\n\n```rust\nuse sql_query_builder as sql;\n\nlet mut select = sql::Select::new()\n  .select(\"id, login\")\n  .from(\"users\")\n  .where_clause(\"login = $1\");\n\nlet is_admin = true;\n\nif is_admin {\n  select = select.where_clause(\"is_admin = true\");\n}\n\nlet query = select.as_string();\n\nprintln!(\"{query}\");\n```\n\nOutput\n\n```sql\nSELECT id, login FROM users WHERE login = $1 AND is_admin = true\n```\n\n\n## Feature Flags\n\nSQL Query Builder comes with the following optional features:\n- `postgresql` enable Postgres syntax\n- `sqlite` enable SQLite syntax\n- `mysql` enable MySQL syntax\n\nYou can enable features like\n\n```toml\n# Cargo.toml\n\nsql_query_builder = { version = \"2.x.x\", features = [\"postgresql\"] }\n```\n\n\n## How it's works\n\nIn a simplified way, the lib has an API to allows you to write dynamic queries in a style\nsimilar to queries written in pure SQL and the result is a code idiomatic to both Rust and SQL.\nAdditionally, the library will not try to understand what you write in the parameters and in some\nways this is good as it removes a lot of verbosity to generate a SQL query, in contrast,\ndebugging tends to be more difficult and silly errors can appear, the library has a\n[debug()](https://docs.rs/sql_query_builder/latest/sql_query_builder/struct.Select.html#method.debug)\nmethod which had a good output to minimize the effort of debugging complex queries.\n\nMore technically, consecutive calls to the same clause will accumulates values respecting the order\nof the calls, the two select produce the same SQL query.\n\n```rust\nuse sql_query_builder as sql;\n\nlet select = sql::Select::new()\n  .select(\"id, login\");\n\nlet select = sql::Select::new()\n  .select(\"id\")\n  .select(\"login\");\n```\n\nMethods like `limit` and `offset` will override the previous value, the two select is equivalent\n\n```rust\n# #[cfg(any(feature = \"postgresql\", feature = \"sqlite\"))]\n# {\nuse sql_query_builder as sql;\n\nlet select = sql::Select::new()\n  .limit(\"1000\")\n  .limit(\"123\");\n\nlet select = sql::Select::new()\n  .limit(\"123\");\n# }\n```\n\nThe library ignores the order between clauses so the two selects will produce the same query\n\n```rust\nuse sql_query_builder as sql;\n\nlet select = sql::Select::new()\n  .select(\"id, login\")\n  .from(\"users\")\n  .where_clause(\"login = $1\");\n\nlet select = sql::Select::new()\n  .from(\"users\")\n  .where_clause(\"login = $1\")\n  .select(\"id, login\");\n```\n\nYou can conditionally add a clause mutating the select\n\n```rust\nuse sql_query_builder as sql;\n\nlet mut select = sql::Select::new()\n  .select(\"id, login\")\n  .from(\"users\")\n  .where_clause(\"login = $1\");\n\nlet should_includes_address = true;\n\nif should_includes_address {\n  select = select.inner_join(\"addresses on user.login = addresses.owner_login\");\n}\n```\n\n\n## Composition\n\nComposition is very welcome to write complex queries, this feature makes the library shine\n\n```rust\nuse sql_query_builder as sql;\n\nfn project(select: sql::Select) -\u003e sql::Select {\n  select\n    .select(\"u.id, u.name as user_name, u.login\")\n    .select(\"a.name as addresses_name\")\n    .select(\"o.name as product_name\")\n}\n\nfn relations(select: sql::Select) -\u003e sql::Select {\n  select\n    .from(\"users u\")\n    .inner_join(\"addresses a ON a.user_login = u.login\")\n    .inner_join(\"orders o ON o.user_login = u.login\")\n}\n\nfn conditions(select: sql::Select) -\u003e sql::Select {\n  select\n    .where_clause(\"u.login = $1\")\n    .where_clause(\"o.id = $2\")\n}\n\nfn as_string(select: sql::Select) -\u003e String {\n  select.as_string()\n}\n\nlet query = Some(sql::Select::new())\n  .map(project)\n  .map(relations)\n  .map(conditions)\n  .map(as_string)\n  .unwrap();\n\nprintln!(\"{query}\");\n```\n\nOutput (indented for readability)\n\n```sql\nSELECT u.id, u.name as user_name, u.login, a.name as addresses_name, o.name as product_name\nFROM users u\nINNER JOIN addresses a ON a.user_login = u.login\nINNER JOIN orders o ON o.user_login = u.login\nWHERE u.login = $1 AND o.id = $2\n```\n\n\n## Raw queries\n\nYou can use the raw method to reach some edge cases that are hard to rewrite into the Select syntax.\nThe `select.raw()` method will put any SQL you define on top of the output\n\n```rust\nuse sql_query_builder as sql;\n\nlet raw_query = \"\\\n  select u.id as user_id, addr.* \\\n  from users u \\\n  inner join addresses addr on u.login = addr.owner_login\\\n\";\nlet select = sql::Select::new()\n  .raw(raw_query)\n  .where_clause(\"login = $1\");\n```\n\nTo a more precisely use case your can use the `select.raw_before()` and `select.raw_after()`\n\n```rust\nuse sql_query_builder as sql;\n\nlet raw_query = \"\\\n  from users u \\\n  inner join addresses addr on u.login = addr.owner_login\\\n\";\nlet select = sql::Select::new()\n  .select(\"u.id as user_id, addr.*\")\n  .raw_before(sql::SelectClause::Where, raw_query)\n  .where_clause(\"login = $1\");\n```\n\n```rust\nuse sql_query_builder as sql;\n\nlet raw_query = \"\\\n  from users u \\\n  inner join addresses addr on u.login = addr.owner_login\\\n\";\nlet select = sql::Select::new()\n  .select(\"u.id as user_id, addr.*\")\n  .raw_after(sql::SelectClause::Select, raw_query)\n  .where_clause(\"login = $1\");\n```\n\n## Debugging queries\n\nSometimes it's more ease just print de current state of the query builder, to do so adds the `.debug()` method anywhere in the builder.\nIn the example below, the where clause will not be printed because the debug was added before the clause\n\n```rust\nuse sql_query_builder as sql;\n\nlet mut select = sql::Select::new()\n  .select(\"id, login\")\n  .from(\"users\")\n  .debug()\n  .where_clause(\"login = $1\");\n```\n\nPrints to the standard output\n\n```sql\n-- ------------------------------------------------------------------------------\nSELECT id, login\nFROM users\n-- ------------------------------------------------------------------------------\n```\n\nSee the [documentation](https://docs.rs/sql_query_builder/) for more builders like [Insert](https://docs.rs/sql_query_builder/latest/sql_query_builder/struct.Insert.html), [Update](https://docs.rs/sql_query_builder/latest/sql_query_builder/struct.Update.html) and [Delete](https://docs.rs/sql_query_builder/latest/sql_query_builder/struct.Delete.html)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbelchior%2Fsql_query_builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbelchior%2Fsql_query_builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbelchior%2Fsql_query_builder/lists"}