{"id":23763486,"url":"https://github.com/querysorcery/sqlcommenter","last_synced_at":"2025-10-28T07:08:18.676Z","repository":{"id":168719437,"uuid":"644513915","full_name":"QuerySorcery/sqlcommenter","owner":"QuerySorcery","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-13T18:49:35.000Z","size":21,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-13T19:43:02.408Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","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/QuerySorcery.png","metadata":{"files":{"readme":"README.md","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":"2023-05-23T17:15:05.000Z","updated_at":"2024-12-13T18:49:39.000Z","dependencies_parsed_at":"2024-12-13T19:41:53.555Z","dependency_job_id":"b373abaf-7ff5-4bbf-b5bb-0ad18deb3a65","html_url":"https://github.com/QuerySorcery/sqlcommenter","commit_stats":null,"previous_names":["dkuku/sqlcommenter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuerySorcery%2Fsqlcommenter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuerySorcery%2Fsqlcommenter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuerySorcery%2Fsqlcommenter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuerySorcery%2Fsqlcommenter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/QuerySorcery","download_url":"https://codeload.github.com/QuerySorcery/sqlcommenter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232032116,"owners_count":18462972,"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":[],"created_at":"2024-12-31T22:12:42.778Z","updated_at":"2025-10-28T07:08:18.571Z","avatar_url":"https://github.com/QuerySorcery.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Installation\n\nThe package can be installed by adding `sqlcommenter` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:sqlcommenter, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\n\u003c!-- MDOC !--\u003e\n\n# Sqlcommenter\n\nDisclaimer: Currently using comments disables prepared statements and may increase cpu load on\nyour database.\nElixir implementation of [sqlcommenter](https://google.github.io/sqlcommenter/) escaping.\nAttach SQL comments to correlate user code in ORMs and SQL drivers with SQL statements.\n\n## Usage\n  Add these callbacks to your Repo module, adjust to your needs.\n  The stacktrace option is only required when you plan to include use the extract_repo_caller\n  function.  This function also accepts the __MODULE__ as the second argument to exclude it\n  from the stacktrace.\n\n The sqlcommenter option is used for adding static data to the comment like the running app,\n team, owner etc.  Any dynamic data can be added in the prepare_query function. Besides the\n caller it can also add the trace and span.\n\nThe generated comment needs to be added as comment option - this will be passed to the adapter.\n\n```elixir\ndef default_options(_operation) do\n  [stacktrace: true, prepare: :unnamed, sqlcommenter: [team: \"sqlcomm\", app: \"sqlcomm\"]]\nend\n\ndef prepare_query(_operation, query, opts) do\n  sqlcommennter_defaults = Keyword.get(opts, :sqlcommenter)\n  caller = Sqlcommenter.extract_repo_caller(opts, __MODULE__)\n  comment = Sqlcommenter.to_str([caller: caller] ++ sqlcommenter_defaults)\n\n  {query, [comment: comment] ++ opts}\nend\n```\n\nNow your postgres logs should look will return a log line like this:\n\n```\n2024-10-27 21:43:04.331 GMT,\"postgres\",\"sqlcomm_test\",416336,\"127.0.0.1:53348\",671eb3e8.65a50,6, \"SELECT\",2024-10-27 21:43:04 GMT,15/54,61620,LOG,00000,\"execute \u003cunnamed\u003e: SELECT u0.\"\"id\"\",\nu0.\"\"active\"\", u0.\"\"name\"\", u0.\"\"inserted_at\"\", u0.\"\"updated_at\"\" FROM \"\"users\"\" AS\nu0/*app='sqlcomm',caller='Elixir.SqlcommTest.test%20insert%20user%2F1',team='sqlcomm'*/\"\n,,,,,,,,,\"\",\"client backend\",,0\n```\n\nAlternatively, when you're concerned about performance and your options are mostly static\nyou can also omit the Sqlcommenter logic and write your own custom function.\nJust remember that according the sqlcommenter specs the keys must be sorted,\nand the values properly escaped check [specification](https://google.github.io/sqlcommenter/spec/) and sqlcommenter source code.\n\n```elixir\ndefmodule SqlEEx do\n   require EEx\n\n  EEx.function_from_string(\n    :def,\n    :to_comment,\n    \"app:'sqlcomm',caller:'\u003c%= caller %\u003e'team:'sqlcomm'\",\n    [:caller]\n  )\nend\n```\n\nAnd then use it in your repo:\n```elixir\ndef prepare_query(_operation, query, opts) do\n  caller = Sqlcommenter.extract_repo_caller(opts, __MODULE__)\n  SqlEEx.to_comment(caller: caller)\n  {query, [comment: comment] ++ opts}\nend\n```\n\nDocumentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)\nand published on [HexDocs](https://hexdocs.pm). Once published, the docs can\nbe found at \u003chttps://hexdocs.pm/sqlcommenter\u003e.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquerysorcery%2Fsqlcommenter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquerysorcery%2Fsqlcommenter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquerysorcery%2Fsqlcommenter/lists"}