{"id":37080002,"url":"https://github.com/simonacca/dict2sql","last_synced_at":"2026-01-14T09:40:49.127Z","repository":{"id":62568081,"uuid":"363758940","full_name":"simonacca/dict2sql","owner":"simonacca","description":"express SQL as python data structures","archived":false,"fork":false,"pushed_at":"2025-07-13T14:44:25.000Z","size":484,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-06T10:02:57.683Z","etag":null,"topics":["database","orm","python","sql"],"latest_commit_sha":null,"homepage":"","language":"Python","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/simonacca.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":"2021-05-02T22:02:35.000Z","updated_at":"2025-07-13T14:44:29.000Z","dependencies_parsed_at":"2022-11-03T16:30:38.225Z","dependency_job_id":null,"html_url":"https://github.com/simonacca/dict2sql","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/simonacca/dict2sql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonacca%2Fdict2sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonacca%2Fdict2sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonacca%2Fdict2sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonacca%2Fdict2sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonacca","download_url":"https://codeload.github.com/simonacca/dict2sql/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonacca%2Fdict2sql/sbom","scorecard":{"id":824611,"data":{"date":"2025-08-11","repo":{"name":"github.com/simonacca/dict2sql","commit":"91384e97b1ff4b70deda999c232dead8f22dfafd"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.7,"checks":[{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":3,"reason":"4 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 3","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/python-publish.yml:1","Warn: no topLevel permission defined: .github/workflows/test.yaml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/python-publish.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/simonacca/dict2sql/python-publish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/python-publish.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/simonacca/dict2sql/python-publish.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/python-publish.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/simonacca/dict2sql/python-publish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yaml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/simonacca/dict2sql/test.yaml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yaml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/simonacca/dict2sql/test.yaml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yaml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/simonacca/dict2sql/test.yaml/main?enable=pin","Info:   0 out of   4 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   2 third-party GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-23T16:23:36.008Z","repository_id":62568081,"created_at":"2025-08-23T16:23:36.008Z","updated_at":"2025-08-23T16:23:36.008Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28416120,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T08:38:59.149Z","status":"ssl_error","status_checked_at":"2026-01-14T08:38:43.588Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":["database","orm","python","sql"],"created_at":"2026-01-14T09:40:48.332Z","updated_at":"2026-01-14T09:40:49.120Z","avatar_url":"https://github.com/simonacca.png","language":"Python","readme":"# dict2sql, the missing SQL API\n\nBuild SQL queries with Python dictionaries. Composable, safe and reusable.\n\nSee [API.md](API.md) for comprehensive documentation.\n\n# Installation\n\n```shell\npip install git+https://github.com/simonacca/dict2sql@v3.0.0\n\n```\n\n\n\n\n# Examples\n\n```python\nfrom dict2sql import to_sql\n\nquery = {\n    \n    \"Select\": [\"name\", \"height\", \"country\"],\n    \"From\": \"mountains\",\n    \"Where\": {\n        \"Op\": \"AND\",\n        \"Predicates\": [\n            {\"Op\": \"\u003e=\", \"Sx\": \"height\", \"Dx\": 3000},\n            {\"Op\": \"=\", \"Sx\": \"has_glacier\", \"Dx\": True}\n        ],\n    },\n    \"Limit\": 3\n}\n\nsql = to_sql(query)\nprint(sql)\n```\n\nproduces\n\n```sql\nSELECT \"name\" , \"height\" , \"country\" FROM \"mountains\" WHERE \"height\" \u003e= 3000 AND \"has_glacier\" = TRUE LIMIT 3\n```\n\n## INSERT Statement\n\n```python\nfrom dict2sql import to_sql\n\ninsert_query = {\n    \"StatementType\": \"INSERT\",\n    \"Table\": \"users\",\n    \"Insert\": {\n        \"name\": \"Alice Johnson\", \n        \"email\": \"alice@example.com\",\n        \"age\": 28,\n        \"active\": True\n    }\n}\n\nsql = to_sql(insert_query)\nprint(sql)\n```\n\nproduces\n\n```sql\nINSERT INTO \"users\" ( \"name\" , \"email\" , \"age\" , \"active\" ) VALUES ( 'Alice Johnson' , 'alice@example.com' , 28 , TRUE )\n```\n\n## UPDATE Statement\n\n```python\nfrom dict2sql import to_sql\n\nupdate_query = {\n    \"StatementType\": \"UPDATE\",\n    \"Table\": \"users\",\n    \"Values\": {\n        \"email\": \"alice.johnson@newdomain.com\",\n        \"last_login\": \"2024-01-15T10:30:00\"\n    },\n    \"Where\": {\n        \"Op\": \"=\",\n        \"Sx\": \"id\", \n        \"Dx\": 123\n    }\n}\n\nsql = to_sql(update_query)\nprint(sql)\n```\n\nproduces\n\n```sql\nUPDATE \"users\" SET \"email\" = 'alice.johnson@newdomain.com' , \"last_login\" = '2024-01-15T10:30:00' WHERE \"id\" = 123\n```\n\n## DELETE Statement\n\n```python\nfrom dict2sql import to_sql\n\ndelete_query = {\n    \"StatementType\": \"DELETE\",\n    \"Table\": \"users\",\n    \"Where\": {\n        \"Op\": \"AND\",\n        \"Predicates\": [\n            {\"Op\": \"=\", \"Sx\": \"active\", \"Dx\": False},\n            {\"Op\": \"\u003c\", \"Sx\": \"last_login\", \"Dx\": \"2023-01-01\"}\n        ]\n    }\n}\n\nsql = to_sql(delete_query)\nprint(sql)\n```\n\nproduces\n\n```sql\nDELETE FROM \"users\" WHERE \"active\" = FALSE AND \"last_login\" \u003c '2023-01-01'\n```\n\n## Advanced SELECT Examples\n\n### Aggregations and GROUP BY\n\n```python\nfrom dict2sql import to_sql\n\naggregation_query = {\n    \n    \"Select\": [\n        \"country\",\n        {\"Function\": \"COUNT\", \"Column\": \"*\", \"Alias\": \"mountain_count\"},\n        {\"Function\": \"AVG\", \"Column\": \"height\", \"Alias\": \"avg_height\"},\n        {\"Function\": \"MAX\", \"Column\": \"height\", \"Alias\": \"highest_peak\"}\n    ],\n    \"From\": \"mountains\",\n    \"Where\": {\n        \"Op\": \"\u003e=\",\n        \"Sx\": \"height\", \n        \"Dx\": 1000\n    },\n    \"GroupBy\": \"country\",\n    \"Having\": {\n        \"Op\": \"\u003e\",\n        \"Sx\": {\"Function\": \"COUNT\", \"Column\": \"*\"},\n        \"Dx\": 5\n    },\n    \"OrderBy\": {\"Column\": \"avg_height\", \"Direction\": \"DESC\"}\n}\n\nsql = to_sql(aggregation_query)\nprint(sql)\n```\n\nproduces\n\n```sql\nSELECT \"country\" , COUNT ( * ) AS \"mountain_count\" , AVG ( \"height\" ) AS \"avg_height\" , MAX ( \"height\" ) AS \"highest_peak\" FROM \"mountains\" WHERE \"height\" \u003e= 1000 GROUP BY \"country\" HAVING COUNT ( * ) \u003e 5 ORDER BY \"avg_height\" DESC\n```\n\n### JOINs and Subqueries\n\n```python\nfrom dict2sql import to_sql\n\njoin_query = {\n    \n    \"Select\": [\n        \"m.name\",\n        \"m.height\", \n        \"c.population\",\n        \"c.continent\"\n    ],\n    \"From\": {\n        \"Sx\": {\"TableName\": \"mountains\", \"Alias\": \"m\"},\n        \"Join\": \"INNER JOIN\",\n        \"Dx\": {\"TableName\": \"countries\", \"Alias\": \"c\"},\n        \"On\": {\"Op\": \"=\", \"Sx\": \"m.country_code\", \"Dx\": \"c.code\"}\n    },\n    \"Where\": {\n        \"Op\": \"AND\",\n        \"Predicates\": [\n            {\"Op\": \"\u003e\", \"Sx\": \"m.height\", \"Dx\": 8000},\n            {\"Op\": \"=\", \"Sx\": \"c.continent\", \"Dx\": \"Asia\"}\n        ]\n    },\n    \"OrderBy\": [\n        {\"Column\": \"m.height\", \"Direction\": \"DESC\"},\n        {\"Column\": \"c.population\", \"Direction\": \"ASC\"}\n    ]\n}\n\nsql = to_sql(join_query)\nprint(sql)\n```\n\nproduces\n\n```sql\nSELECT \"m\".\"name\" , \"m\".\"height\" , \"c\".\"population\" , \"c\".\"continent\" FROM \"mountains\" AS \"m\" INNER JOIN \"countries\" AS \"c\" ON \"m\".\"country_code\" = \"c\".\"code\" WHERE \"m\".\"height\" \u003e 8000 AND \"c\".\"continent\" = 'Asia' ORDER BY \"m\".\"height\" DESC , \"c\".\"population\" ASC\n```\n\n### Arithmetic Expressions\n\n```python\nfrom dict2sql import to_sql\n\narithmetic_query = {\n    \n    \"Select\": [\n        \"name\",\n        \"height\",\n        {\n            \"Left\": \"height\",\n            \"Op\": \"*\", \n            \"Right\": 3.28084,\n            \"Alias\": \"height_feet\"\n        },\n        {\n            \"Left\": {\n                \"Left\": \"height\",\n                \"Op\": \"-\",\n                \"Right\": \"base_elevation\"\n            },\n            \"Op\": \"/\",\n            \"Right\": \"height\",\n            \"Alias\": \"prominence_ratio\"\n        }\n    ],\n    \"From\": \"mountains\",\n    \"Where\": {\n        \"Op\": \"\u003e\",\n        \"Sx\": \"height\",\n        \"Dx\": 5000\n    },\n    \"OrderBy\": {\"Column\": \"prominence_ratio\", \"Direction\": \"DESC\"},\n    \"Limit\": 10\n}\n\nsql = to_sql(arithmetic_query)\nprint(sql)\n```\n\nproduces\n\n```sql\nSELECT \"name\" , \"height\" , \"height\" * 3.28084 AS \"height_feet\" , ( \"height\" - \"base_elevation\" ) / \"height\" AS \"prominence_ratio\" FROM \"mountains\" WHERE \"height\" \u003e 5000 ORDER BY \"prominence_ratio\" DESC LIMIT 10\n```\n\n# Notes\n\n\n## Rationale\n\nFor historical reasons in the world of relational databases interfaces usually consist of domain-specific languages (mostly dialects of SQL)\nrather than composition of data structures as it is common with modern APIs (for example JSON-based REST, protobuf).\nWhile a domain-specific language (DSL) is very well suited for interactive use, such as manually exploring a dataset, this approach has some limitations when trying to interface with a database programmatically (for example from a Python script).\n\nThis library brings a modern API to SQL databases, allowing the user to express queries as composition of basic python data structures: dicts, lists, strings, ints, floats and booleans.\n\nAmong the primary benefits of this approach is a superior ability to reuse code. All the usual python constructs and software engineering best practices are available to the query author to express queries using clean, maintainable code.\n\nQuery-as-data also means compatibility with Python's type hinting system, which translates to reduced query-correctness issues, improved error messages (at least with respect to some query engines), and a quicker development experience.\n\nNotably, this solution eliminates one major source of friction with traditional programming-language level handling of SQL: SQL injection and excaping. While solutions to this problem such as parametrized queries have been developed over time, they heavily favor safety at the expense of expressivity; it is usually forbidden to compose parametrized queries at runtime.\nHow is this accomplished? By having granular information about each component of a query, `dict2sql` is easily able to apply escaping where needed, resulting in safe queries.\n\nFinally, it should be noted that this library strictly tries to do *one* job well, namely *composing sql queries*. There is many related functionalities in this space which we explicitely avoid taking on, feeling that they are best left to other very mature libraries in the Python ecosystem. For example: connecting to the database and performing queries, parsing query return values.\n\n## Implementation details\nThis project at the moment targets ANSI SQL, with the ambition of soon targeting all major SQL dialects.\n\nTests are based on the [Chinhook Database](https://github.com/lerocha/chinook-database).\n\n\n## Contributing\n\nContributions and forks are welcome!\n\nIf you want to increment the current language to increase coverage of ANSI SQL, go right ahead.\n\nIf you plan to contribute major features such as support for a new dialect, it is recommended to start a PR early on in the development process to prevent duplicate work and ensure that it will be possible to merge the PR without any hiccups.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonacca%2Fdict2sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonacca%2Fdict2sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonacca%2Fdict2sql/lists"}