{"id":20269247,"url":"https://github.com/wroge/superbasic","last_synced_at":"2025-04-11T04:01:42.326Z","repository":{"id":48937267,"uuid":"517095481","full_name":"wroge/superbasic","owner":"wroge","description":"The superbasic SQL-Builder.","archived":false,"fork":false,"pushed_at":"2024-03-08T13:19:18.000Z","size":161,"stargazers_count":39,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T02:01:38.365Z","etag":null,"topics":["no-orm","query","sql","sql-builder","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/wroge.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}},"created_at":"2022-07-23T15:32:43.000Z","updated_at":"2025-03-15T23:21:29.000Z","dependencies_parsed_at":"2024-03-08T14:45:05.162Z","dependency_job_id":null,"html_url":"https://github.com/wroge/superbasic","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wroge%2Fsuperbasic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wroge%2Fsuperbasic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wroge%2Fsuperbasic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wroge%2Fsuperbasic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wroge","download_url":"https://codeload.github.com/wroge/superbasic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248339256,"owners_count":21087214,"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":["no-orm","query","sql","sql-builder","template"],"created_at":"2024-11-14T12:24:09.395Z","updated_at":"2025-04-11T04:01:42.297Z","avatar_url":"https://github.com/wroge.png","language":"Go","readme":"Take a look at [github.com/wroge/esquel](https://github.com/wroge/esquel). \n\n\u003cbr\u003e\n\n# The superbasic SQL-Builder\n\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white)](https://pkg.go.dev/github.com/wroge/superbasic)\n[![Go Report Card](https://goreportcard.com/badge/github.com/wroge/superbasic)](https://goreportcard.com/report/github.com/wroge/superbasic)\n![golangci-lint](https://github.com/wroge/superbasic/workflows/golangci-lint/badge.svg)\n[![codecov](https://codecov.io/gh/wroge/superbasic/branch/main/graph/badge.svg?token=SBSedMOGHR)](https://codecov.io/gh/wroge/superbasic)\n[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/wroge/superbasic.svg?style=social)](https://github.com/wroge/superbasic/tags)\n\n```superbasic.Compile``` compiles expressions into an SQL template and thus offers an alternative to conventional query builders.\n\n- ```Compile``` replaces placeholders ```?``` with expressions.\n- ```Join``` joins expressions by a separator.\n\n```go\ncreate := superbasic.Compile(\"CREATE TABLE presidents (\\n\\t?\\n)\",\n\tsuperbasic.Join(\",\\n\\t\",\n\t\tsuperbasic.SQL(\"nr SERIAL PRIMARY KEY\"),\n\t\tsuperbasic.SQL(\"first TEXT NOT NULL\"),\n\t\tsuperbasic.SQL(\"last TEXT NOT NULL\"),\n\t),\n)\n\nfmt.Println(create.ToSQL())\n// CREATE TABLE presidents (\n//\tnr SERIAL PRIMARY KEY,\n//\tfirst TEXT NOT NULL,\n//\tlast TEXT NOT NULL\n// )\n```\n\n- ```Map``` is a generic mapper function particularly helpful in the context of Join.\n- ```Finalize``` replaces ```?``` placeholders with a string that can contain a positional part by ```%d```.\n\n```go\npresidents := []President{\n\t{\"George\", \"Washington\"},\n\t{\"John\", \"Adams\"},\n}\n\ninsert := superbasic.Join(\" \",\n\tsuperbasic.SQL(\"INSERT INTO presidents (first, last)\"),\n\tsuperbasic.Compile(\"VALUES ?\",\n\t\tsuperbasic.Join(\", \",\n\t\t\tsuperbasic.Map(presidents,\n\t\t\t\tfunc(_ int, president President) superbasic.Expression {\n\t\t\t\t\treturn superbasic.Values{president.First, president.Last}\n\t\t\t\t})...,\n\t\t),\n\t),\n\tsuperbasic.SQL(\"RETURNING nr\"),\n)\n\nfmt.Println(superbasic.Finalize(\"$%d\", insert))\n// INSERT INTO presidents (first, last) VALUES ($1, $2), ($3, $4) RETURNING nr [George Washington John Adams]\n```\n\n- ```If``` condition is true, return expression, else skip.\n- ```Switch``` returns an expression matching a value.\n\n```go\ndialect := \"sqlite\"\ncontains := \"Joe\"\n\nquery := superbasic.Join(\" \", superbasic.SQL(\"SELECT * FROM presidents\"),\n\tsuperbasic.If(contains != \"\", superbasic.Compile(\"WHERE ?\",\n\t\tsuperbasic.Switch(dialect,\n\t\t\tsuperbasic.Case(\"postgres\", superbasic.SQL(\"POSITION(? IN presidents.first) \u003e 0\", contains)),\n\t\t\tsuperbasic.Case(\"sqlite\", superbasic.SQL(\"INSTR(presidents.first, ?) \u003e 0\", contains)),\n\t\t))))\n\nfmt.Println(superbasic.Finalize(\"?\", query))\n// SELECT * FROM presidents WHERE INSTR(presidents.first, ?) \u003e 0 [Joe] \u003cnil\u003e\n```\n\nTo scan rows to types, i recommend [wroge/scan](https://github.com/wroge/scan).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwroge%2Fsuperbasic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwroge%2Fsuperbasic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwroge%2Fsuperbasic/lists"}