{"id":13714369,"url":"https://github.com/keegancsmith/sqlf","last_synced_at":"2025-04-04T10:09:43.348Z","repository":{"id":41168836,"uuid":"58958942","full_name":"keegancsmith/sqlf","owner":"keegancsmith","description":"sqlf generates parameterized SQL statements in Go, sprintf style","archived":false,"fork":false,"pushed_at":"2023-08-18T06:45:50.000Z","size":17,"stargazers_count":127,"open_issues_count":1,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-28T09:09:22.410Z","etag":null,"topics":["go","golang","sprintf-style","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","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/keegancsmith.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}},"created_at":"2016-05-16T19:01:04.000Z","updated_at":"2024-07-09T09:49:29.000Z","dependencies_parsed_at":"2024-06-18T15:19:34.194Z","dependency_job_id":"2e466d57-4ea7-4746-b2b1-5547d16d7bd8","html_url":"https://github.com/keegancsmith/sqlf","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keegancsmith%2Fsqlf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keegancsmith%2Fsqlf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keegancsmith%2Fsqlf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keegancsmith%2Fsqlf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/keegancsmith","download_url":"https://codeload.github.com/keegancsmith/sqlf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247157283,"owners_count":20893220,"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":["go","golang","sprintf-style","sql"],"created_at":"2024-08-02T23:01:58.183Z","updated_at":"2025-04-04T10:09:43.320Z","avatar_url":"https://github.com/keegancsmith.png","language":"Go","funding_links":[],"categories":["Go","Repositories"],"sub_categories":[],"readme":"sqlf [![Build Status](https://travis-ci.org/keegancsmith/sqlf.svg?branch=master)](https://travis-ci.org/) [![GoDoc](https://godoc.org/github.com/keegancsmith/sqlf?status.svg)](https://godoc.org/github.com/keegancsmith/sqlf)\n======\n\nGenerate parameterized SQL statements in Go, sprintf Style.\n\n```go\nq := sqlf.Sprintf(\"SELECT * FROM users WHERE country = %s AND age \u003e %d\", \"US\", 27);\nrows, err := db.Query(q.Query(sqlf.SimpleBindVar), q.Args()...) // db is a database/sql.DB\n```\n\n`sqlf.Sprintf` does not return a string. It returns `*sqlf.Query` which has\nmethods for a parameterized SQL query and arguments. You then pass that to\n`db.Query`, `db.Exec`, etc. This is not like using `fmt.Sprintf`, which could\nexpose you to malformed SQL or SQL injection attacks.\n\n`sqlf.Query` can be passed as an argument to `sqlf.Sprintf`. It will \"flatten\"\nthe query string, while preserving the correct variable binding. This allows\nyou to easily compose and build SQL queries. See the below examples to find\nout more.\n\n```go\n// This is an example which shows off embedding SQL, which simplifies building\n// complicated SQL queries\nname := \"John\"\nage, offset := 27, 100\nwhere := sqlf.Sprintf(\"name=%s AND age=%d\", name, age)\nlimit := sqlf.Sprintf(\"%d OFFSET %d\", 10, offset)\nq := sqlf.Sprintf(\"SELECT name FROM users WHERE %s LIMIT %s\", where, limit)\nfmt.Println(q.Query(sqlf.PostgresBindVar))\nfmt.Println(q.Args())\n// Output: SELECT name FROM users WHERE name=$1 AND age=$2 LIMIT $3 OFFSET $4\n// [John 27 10 100]\n```\n\nAnother common task is joining conditionals with `AND` or `OR`. sqlf\nsimplifies this task with `sqlf.Join`:\n\n```go\n// Our inputs\nminQuantity := 100\nnameFilters := []string{\"apple\", \"orange\", \"coffee\"}\n\nvar conds []*sqlf.Query\nfor _, filter := range nameFilters {\n\tconds = append(conds, sqlf.Sprintf(\"name LIKE %s\", \"%\"+filter+\"%\"))\n}\nsubQuery := sqlf.Sprintf(\"SELECT product_id FROM order_item WHERE quantity \u003e %d\", minQuantity)\nq := sqlf.Sprintf(\"SELECT name FROM product WHERE id IN (%s) AND (%s)\", subQuery, sqlf.Join(conds, \"OR\"))\n\nfmt.Println(q.Query(sqlf.PostgresBindVar))\nfmt.Println(q.Args())\n// Output: SELECT name FROM product WHERE id IN (SELECT product_id FROM order_item WHERE quantity \u003e $1) AND (name LIKE $2 OR name LIKE $3 OR name LIKE $4)\n// [100 %apple% %orange% %coffee%]\n```\n\nSee https://godoc.org/github.com/keegancsmith/sqlf for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeegancsmith%2Fsqlf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeegancsmith%2Fsqlf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeegancsmith%2Fsqlf/lists"}