{"id":21727061,"url":"https://github.com/pgjones/sql-tstring","last_synced_at":"2025-04-12T23:37:30.550Z","repository":{"id":255402915,"uuid":"849297174","full_name":"pgjones/sql-tstring","owner":"pgjones","description":"SQL-tString allows for f-string like construction of sql queries ","archived":false,"fork":false,"pushed_at":"2024-12-03T08:11:56.000Z","size":78,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T23:37:10.181Z","etag":null,"topics":["quart"],"latest_commit_sha":null,"homepage":"","language":"Python","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/pgjones.png","metadata":{"files":{"readme":"README.rst","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-29T10:46:40.000Z","updated_at":"2025-04-11T08:15:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"51731cf2-e424-4588-8b8a-5ec0cb1a2eab","html_url":"https://github.com/pgjones/sql-tstring","commit_stats":null,"previous_names":["pgjones/sql-string","pgjones/sql-tstring"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgjones%2Fsql-tstring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgjones%2Fsql-tstring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgjones%2Fsql-tstring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgjones%2Fsql-tstring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pgjones","download_url":"https://codeload.github.com/pgjones/sql-tstring/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647255,"owners_count":21139081,"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":["quart"],"created_at":"2024-11-26T03:43:34.619Z","updated_at":"2025-04-12T23:37:30.531Z","avatar_url":"https://github.com/pgjones.png","language":"Python","readme":"SQL-tString\n===========\n\n|Build Status| |pypi| |python| |license|\n\nSQL-tString allows for f-string like construction of sql queries\nwithout allowing for SQL injection. The basic usage is as follows,\n\n.. code-block:: python\n\n    from sql_tstring import sql\n\n    a = 1\n\n    query, values = sql(\n        \"\"\"SELECT a, b, c\n             FROM tbl\n            WHERE a = {a}\"\"\",\n        locals(),\n    )\n\nThe ``query`` is a ``str`` and ``values`` a ``list[Any]``, both are\nthen typically passed to a DB connection. Note the parameters can only\nbe identifiers that identify variables (in the above example in the\nlocals()) e.g. ``{a - 1}`` is not valid.\n\nSQL-tString will convert parameters to SQL placeholders where\nappropriate. In other locations SQL-tString will allow pre defined\ncolumn or table names to be used,\n\n.. code-block:: python\n\n    from sql_tstring import sql, sql_context\n\n    col = \"a\"\n    table = \"tbl\"\n\n    with sql_context(columns={\"a\"}, tables={\"tbl\"}):\n        query, values = sql(\n            \"SELECT {col} FROM {table}\",\n            locals(),\n        )\n\nIf the value of ``col`` or ``table`` does not match the valid values\ngiven to the ``sql_context`` function an error will be raised.\n\nRewriting values\n----------------\n\nSQL-tString will also remove parameters if they are set to the special\nvalue of ``Absent`` (or ``RewritingValue.Absent``). This is most\nuseful for optional updates, or conditionals,\n\n.. code-block:: python\n\n    from sql_tstring import Absent, sql\n\n    a = Absent\n    b = Absent\n\n    query, values = sql(\n        \"\"\"UPDATE tbl\n              SET a = {a},\n                  b = 1\n            WHERE b = {b}\"\"\",\n        locals(),\n    )\n\nAs both ``a`` and ``b`` are ``Absent`` the above ``query`` will be\n``UPDATE tbl SET b =1``.\n\nIn addition for conditionals the values ``IsNull`` (or\n``RewritingValue.IS_NULL``) and ``IsNotNull`` (or\n``RewritingValue.IS_NOT_NULL``) can be used to rewrite the conditional\nas expected. This is useful as ``x = NULL`` is always false in SQL.\n\nt-string (PEP 750)\n------------------\n\nIf, hopefully, `PEP 750 \u003chttps://peps.python.org/pep-0750/\u003e`_ is\naccepted the usage of this library will change to,\n\n.. code-block:: python\n\n    from sql_tstring import sql\n\n    a = 1\n\n    query, values = sql(\n        t\"\"\"SELECT a, b, c\n              FROM tbl\n             WHERE a = {a}\"\"\",\n    )\n\n.. |Build Status| image:: https://github.com/pgjones/sql-tstring/actions/workflows/ci.yml/badge.svg\n   :target: https://github.com/pgjones/sql-tstring/commits/main\n\n.. |pypi| image:: https://img.shields.io/pypi/v/sql-tstring.svg\n   :target: https://pypi.python.org/pypi/Sql-Tstring/\n\n.. |python| image:: https://img.shields.io/pypi/pyversions/sql-tstring.svg\n   :target: https://pypi.python.org/pypi/Sql-Tstring/\n\n.. |license| image:: https://img.shields.io/badge/license-MIT-blue.svg\n   :target: https://github.com/pgjones/sql-tstring/blob/main/LICENSE\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgjones%2Fsql-tstring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpgjones%2Fsql-tstring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgjones%2Fsql-tstring/lists"}