{"id":21149942,"url":"https://github.com/nicklaswallgren/sqltemplate","last_synced_at":"2025-07-09T09:31:20.523Z","repository":{"id":42448113,"uuid":"470042828","full_name":"NicklasWallgren/sqlTemplate","owner":"NicklasWallgren","description":"Template engine for writing dynamic SQL queries","archived":false,"fork":false,"pushed_at":"2024-04-30T14:41:45.000Z","size":28,"stargazers_count":7,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-02T09:20:33.746Z","etag":null,"topics":["go","sql","template"],"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/NicklasWallgren.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":"2022-03-15T07:04:37.000Z","updated_at":"2024-04-30T14:41:42.000Z","dependencies_parsed_at":"2024-06-19T13:34:02.938Z","dependency_job_id":"2599ac86-eb4f-4e75-92fc-1f9be26d8ef3","html_url":"https://github.com/NicklasWallgren/sqlTemplate","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/NicklasWallgren%2FsqlTemplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NicklasWallgren%2FsqlTemplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NicklasWallgren%2FsqlTemplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NicklasWallgren%2FsqlTemplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NicklasWallgren","download_url":"https://codeload.github.com/NicklasWallgren/sqlTemplate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225527242,"owners_count":17483516,"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","sql","template"],"created_at":"2024-11-20T09:48:13.221Z","updated_at":"2024-11-20T09:48:13.885Z","avatar_url":"https://github.com/NicklasWallgren.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SQL Template library\n\nA simple template engine for writing dynamic SQL queries.\n\n[![Build Status](https://github.com/NicklasWallgren/sqlTemplate/workflows/Test/badge.svg)](https://github.com/NicklasWallgren/sqlTemplate/actions?query=workflow%3ATest)\n[![Reviewdog](https://github.com/NicklasWallgren/sqlTemplate/workflows/reviewdog/badge.svg)](https://github.com/NicklasWallgren/sqlTemplate/actions?query=workflow%3Areviewdog)\n[![Go Report Card](https://goreportcard.com/badge/github.com/NicklasWallgren/sqlTemplate)](https://goreportcard.com/report/github.com/NicklasWallgren/sqlTemplate)\n[![GoDoc](https://godoc.org/github.com/NicklasWallgren/sqlTemplate?status.svg)](https://godoc.org/github.com/NicklasWallgren/sqlTemplate)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/cabd5fbbcde543ec959fb4a3581600ed)](https://app.codacy.com/gh/NicklasWallgren/sqlTemplate?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=NicklasWallgren/sqlTemplate\u0026utm_campaign=Badge_Grade)\n\nSometimes it can be hard to write comprehensible SQL queries with\ntools like SQL builders ([squirrel](https://github.com/Masterminds/squirrel)\nor [dbr](https://github.com/gocraft/dbr)), specially dynamic queries\nwith optional statements and joins.\nIt can be hard to see the overall cohesive structure of the queries,\nand the primary goal.\n\nThe main motivation of this library is to separate the SQL queries\nfrom the Go code, and to improve the readability of complex dynamic\nqueries.\n\nCheck out the API Documentation http://godoc.org/github.com/NicklasWallgren/sqlTemplate\n\n# Installation\nThe library can be installed through `go get`\n```bash\ngo get github.com/NicklasWallgren/sqlTemplate\n```\n\n# Supported versions\nWe support the latest major Go version, which are 1.19 at the moment.\n\n# Features and benefits\n- Separates SQL and Go code.\n- Keeps the templated query as close as possible to the actual SQL query.\n- Extensible template language with support for https://github.com/Masterminds/sprig\n- No third party dependencies\n- Support for embedded filesystem\n\n# API\n```go\n// Parse parses a sql template and returns the 'QueryTemplate'\nParse(namespace string, templateName string) (QueryTemplate, error)\n\n// ParseWithValuesFromMap parses a sql template with values from a map and returns the 'QueryTemplate'\nParseWithValuesFromMap(namespace string, templateName string, parameters map[string]interface{}) (QueryTemplate, error)\n\n// ParseWithValuesFromStruct parses a sql template with values from a struct and returns the 'QueryTemplate'\nParseWithValuesFromStruct(namespace string, templateName string, parameters interface{}) (QueryTemplate, error)\n\n// Register registers a new namespace by template filesystem and extension\nRegister(namespace string, filesystem fs.FS, extensions string) error\n```\n\n# Examples \n\n## Register a namespace and parse a template\n```go\n//go:embed queries/users/*.tsql\nvar fs embed.FS\n\nsqlt := sqlTemplate.NewQueryTemplateEngine()\nsqlt.Register(\"users\", fs, \".tsql\");\n\ncriteria := map[string]interface{}{\"Name\": \"Bill\", \"Order\": \"id\"}\ntmpl, _ := sqlt.ParseWithValuesFromMap(\"users\", \"findByName\", criteria)\n\nsql.QueryRowContext(context.Background(), tmpl.GetQuery(), tmpl.GetParams())\n\nfmt.Printf(\"query %v\\n\", tmpl.GetQuery())\nfmt.Printf(\"query parameters %v\\n\", tmpl.GetParams())\n```\n\n```sql\n-- File ./queries/users/users.tsql\n{{define \"findByName\"}}\n    SELECT *\n    FROM users\n    WHERE name={{bind .Name}}\n    {{if .Order}}ORDER BY {{.Order}}{{end}}\n{{end}}\n```\n\n## Unit tests\n\n```bash\ngo test -v -race ./pkg\n```\n\nFor benchmark :\n\n```bash\ngo test ./pkg -bench=.\n```\n\n### Code Guide\n\nWe use GitHub Actions to make sure the codebase is consistent\n(`golangci-lint run`) and continuously tested (`go test -v -race\n./pkg`). We try to keep comments at a maximum of 120 characters of\nlength and code at 120.\n\n## Contributing\n\nIf you find any problems or have suggestions about this library,\nplease submit an issue. Moreover, any pull request, code review and\nfeedback are welcome.\n\n## Contributors\n- [Nicklas Wallgren](https://github.com/NicklasWallgren)\n- [All Contributors][link-contributors]\n\n[link-contributors]: ../../contributors\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklaswallgren%2Fsqltemplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicklaswallgren%2Fsqltemplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklaswallgren%2Fsqltemplate/lists"}