{"id":30029072,"url":"https://github.com/kaatinga/baranka","last_synced_at":"2026-04-10T00:56:25.111Z","repository":{"id":307182252,"uuid":"1028577908","full_name":"kaatinga/baranka","owner":"kaatinga","description":"A simple and flexible helper for preparing SQL queries with positional parameters for SQL databases like PostgreSQL, MySQL or SQLite","archived":false,"fork":false,"pushed_at":"2025-07-29T20:41:28.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-29T22:59:53.715Z","etag":null,"topics":["mariadb","mysql","postgresql","query-builder","spanner","sql","sqlite"],"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/kaatinga.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"zenodo":null}},"created_at":"2025-07-29T18:30:18.000Z","updated_at":"2025-07-29T20:41:32.000Z","dependencies_parsed_at":"2025-07-29T22:59:54.958Z","dependency_job_id":"88bf5065-0ecf-480d-af58-aaf977c18814","html_url":"https://github.com/kaatinga/baranka","commit_stats":null,"previous_names":["kaatinga/baranka"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/kaatinga/baranka","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaatinga%2Fbaranka","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaatinga%2Fbaranka/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaatinga%2Fbaranka/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaatinga%2Fbaranka/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaatinga","download_url":"https://codeload.github.com/kaatinga/baranka/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaatinga%2Fbaranka/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269118237,"owners_count":24362988,"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","status":"online","status_checked_at":"2025-08-06T02:00:09.910Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["mariadb","mysql","postgresql","query-builder","spanner","sql","sqlite"],"created_at":"2025-08-06T17:00:52.914Z","updated_at":"2025-12-30T21:49:43.148Z","avatar_url":"https://github.com/kaatinga.png","language":"Go","readme":"# baranka\n\nA simple and flexible helper for preparing SQL queries with positional parameters for SQL databases like PostgreSQL, MySQL, or SQLite.\n\n## Features\n\n- Generates SQL value blocks with correct placeholders (`$1`, `$2`, ... or `?`)\n- Supports custom value templates (e.g., `(%s)`)\n- Collects arguments for use with database drivers\n- Supports SQL expressions with placeholders and arguments\n- Easily extensible with options\n\n## Installation\n\n```sh\ngo get github.com/kaatinga/baranka\n```\n\n## Usage\n\n### Basic Example\n\n```go\nimport \"github.com/kaatinga/baranka\"\n\nb := baranka.New()\nb.Add(1, \"foo\")\nb.Add(2, \"bar\")\n\nquery := \"INSERT INTO my_table (id, name) VALUES \" + b.Values()\nargs := b.Args()\n// Use query and args with your database/sql driver\n```\n\n### Using Options\n\nYou can customize the placeholder format and value template:\n\n```go\nb := baranka.New(\n    baranka.WithIncludeTemplate(\"(%s)\"),\n    baranka.WithPlaceholderFormat(baranka.PlaceholderFormatQuestionMark),\n)\n\nb.Add(1, \"foo\")\nb.Add(2, \"bar\")\n\nquery := \"INSERT INTO my_table (id, name) VALUES \" + b.Values()\n// VALUES (?,?), (?,?)\nargs := b.Args()\n// [1, \"foo\", 2, \"bar\"]\n```\n\n### Using Expressions\n\nYou can embed SQL expressions with their own placeholders and arguments using the `Expression` type.\n\n#### How to use `Expression`\n\n- The `Expression` type allows you to inject SQL fragments with their own arguments.\n- The `template` field must be a valid `fmt.Sprintf`-style template string, using `%s` for each argument you want to substitute with a placeholder.\n- The number of `%s` in the template **must match** the number of elements in the `args` slice.\n- Placeholders (`$1`, `$2`, `?`, etc.) will be substituted for each `%s` in the template, and the corresponding values will be appended to the argument list.\n\n#### Example\n\n```go\nb := baranka.New(baranka.WithPlaceholderFormat(baranka.PlaceholderFormatDollar))\n\nb.Add(\n    baranka.Expression{\n        template: \"POINT(%s %s)\", // two %s for two arguments\n        args:     []any{10.1, 20.2},\n    },\n)\nb.Add(\n    baranka.Expression{\n        template: \"POINT(%s %s)\",\n        args:     []any{11.1, 21.2},\n    },\n)\n\nquery := \"INSERT INTO points (geom) VALUES \" + b.Values()\n// Resulting VALUES: (POINT($1 $2)), (POINT($3 $4))\nargs := b.Args()\n// [10.1, 20.2, 11.1, 21.2]\n```\n\n**Note:**  \nIf you provide a template with more or fewer `%s` than arguments, the resulting SQL will be invalid.\n\n## API\n\n### `New(opts ...option) *Baranka`\n\nCreates a new Baranka helper with optional configuration.\n\n### `(*Baranka) Add(args ...any)`\n\nAdds a new block of values and collects arguments. Supports `Expression` for SQL fragments.\n\n### `(*Baranka) Args() []any`\n\nReturns the collected arguments in order.\n\n### `(*Baranka) Values() string`\n\nReturns the SQL value blocks as a string, e.g. `($1,$2),\\n($3,$4)` or `(?,?)`.\n\n### Options\n\n- `WithIncludeTemplate(template string)`  \n  Sets the template for value blocks (default: `(%s)`).\n\n- `WithPlaceholderFormat(format PlaceholderFormat)`  \n  Sets the placeholder format:  \n  - `PlaceholderFormatDollar` (default, for PostgreSQL: `$1`, `$2`, ...)  \n  - `PlaceholderFormatQuestionMark` (for MySQL/SQLite: `?`)\n\n- `WithBlocksLength(length int)`  \n  Pre-allocates the argument slice for performance.\n\n## Example: Bulk Insert\n\n```go\nb := baranka.New(\n    baranka.WithIncludeTemplate(\"(%s)\"),\n    baranka.WithPlaceholderFormat(baranka.PlaceholderFormatDollar),\n)\n\nfor _, row := range rows {\n    b.Add(row.ID, row.Name)\n}\n\nquery := \"INSERT INTO users (id, name) VALUES \" + b.Values()\nargs := b.Args()\n// db.Exec(query, args...)\n```\n\n## Testing\n\nRun tests with:\n\n```sh\ngo test ./...\n```\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE.md](LICENSE.md) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaatinga%2Fbaranka","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaatinga%2Fbaranka","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaatinga%2Fbaranka/lists"}