Title: Query templates · Issue #112 · sqlc-dev/sqlc · GitHub
Open Graph Title: Query templates · Issue #112 · sqlc-dev/sqlc
X Title: Query templates · Issue #112 · sqlc-dev/sqlc
Description: A sqlc user asked how it would be possible to share a set of select expressions across queries. There isn't a way to do this in the SQL standard, so I came up with the following idea. A given query would be marked as a template. This que...
Open Graph Description: A sqlc user asked how it would be possible to share a set of select expressions across queries. There isn't a way to do this in the SQL standard, so I came up with the following idea. A given query...
X Description: A sqlc user asked how it would be possible to share a set of select expressions across queries. There isn't a way to do this in the SQL standard, so I came up with the following idea. A given q...
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/112
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Query templates","articleBody":"A sqlc user asked how it would be possible to share a set of select expressions across queries. There isn't a way to do this in the SQL standard, so I came up with the following idea. A given query would be marked as a template. This query must be a certain shape, specifically a select from a single table with no additional clauses. You could then refer to that template from subsequent queries to that same table.\r\n\r\nHere's an example using the setup from the README\r\n\r\n```sql\r\nCREATE TABLE authors (\r\n id BIGSERIAL PRIMARY KEY,\r\n name text NOT NULL,\r\n bio text\r\n);\r\n\r\n-- template: named_author\r\nSELECT id, lower(name) as fullname, coalese(bio, 'none') as bio_default, 1 as one\r\nFROM authors;\r\n\r\n-- name: GetAuthor :one\r\nSELECT sqlc.named_author.* FROM authors\r\nWHERE id = $1 LIMIT 1;\r\n\r\n-- name: CreateAuthor :one\r\nINSERT INTO authors (\r\n name, bio\r\n) VALUES (\r\n $1, $2\r\n)\r\nRETURNING sqlc.named_author.*;\r\n```\r\n\r\n```go\r\npackage db\r\n\r\nimport (\r\n\t\"context\"\r\n\t\"database/sql\"\r\n)\r\n\r\ntype NamedAuthor struct {\r\n\tID int64\r\n\tFullname string\r\n\tBioDefault string\r\n\tOne int64\r\n}\r\n\r\nconst createAuthor = `-- name: CreateAuthor :one\r\nINSERT INTO authors (\r\n name, bio\r\n) VALUES (\r\n $1, $2\r\n)\r\nRETURNING id, lower(name) as fullname, coalese(bio, 'none') as bio_default, 1 as one\r\n`\r\n\r\ntype CreateAuthorParams struct {\r\n\tName string\r\n\tBio sql.NullString\r\n}\r\n\r\nfunc (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (NamedAuthor, error) {\r\n\trow := q.db.QueryRowContext(ctx, createAuthor, arg.Name, arg.Bio)\r\n\tvar i NamedAuthor\r\n\terr := row.Scan(&i.ID, &i.Fullname, &i.BioDefault, &i.One)\r\n\treturn i, err\r\n}\r\n\r\nconst getAuthor = `-- name: GetAuthor :one\r\nSELECT id, lower(name) as fullname, coalese(bio, 'none') as bio_default, 1 as one\r\nFROM authors\r\nWHERE id = $1 LIMIT 1\r\n`\r\n\r\nfunc (q *Queries) GetAuthor(ctx context.Context, id int64) (NamedAuthor, error) {\r\n\trow := q.db.QueryRowContext(ctx, getAuthor, id)\r\n\tvar i NamedAuthor\r\n\terr := row.Scan(&i.ID, &i.Fullname, &i.BioDefault, &i.One)\r\n\treturn i, err\r\n}\r\n```","author":{"url":"https://github.com/kyleconroy","@type":"Person","name":"kyleconroy"},"datePublished":"2019-10-25T22:06:22Z","url":"https://github.com/sqlc-dev/sqlc/issues/112"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c9aa9a71-ae17-f4be-c810-f4a8c406e60e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF10:21AC3F:2825DBE:32C68B8:6B017E91 |
| html-safe-nonce | 1a715daf95b7e053d9d478b0e9665f4f4ebac1249df760cac4713a4222d7e2f7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjEwOjIxQUMzRjoyODI1REJFOjMyQzY4Qjg6NkIwMTdFOTEiLCJ2aXNpdG9yX2lkIjoiNTc5NDc0Nzk0NjQzMjkxNzkzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 754a239ae00e27d35b8dfccb68333ae8956925384f3a7e898f3efbbae4930e1b |
| hovercard-subject-tag | issue:512739169 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sqlc-dev/sqlc/112/issue_layout |
| twitter:image | https://opengraph.githubassets.com/609f341cec5c3b040cdd737214a804f97ed71e24df65fc2542719f0139af2c53/sqlc-dev/sqlc/issues/112 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/609f341cec5c3b040cdd737214a804f97ed71e24df65fc2542719f0139af2c53/sqlc-dev/sqlc/issues/112 |
| og:image:alt | A sqlc user asked how it would be possible to share a set of select expressions across queries. There isn't a way to do this in the SQL standard, so I came up with the following idea. A given query... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kyleconroy |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/sqlc-dev/sqlc git https://github.com/sqlc-dev/sqlc.git |
| octolytics-dimension-user_id | 136738596 |
| octolytics-dimension-user_login | sqlc-dev |
| octolytics-dimension-repository_id | 193160679 |
| octolytics-dimension-repository_nwo | sqlc-dev/sqlc |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 193160679 |
| octolytics-dimension-repository_network_root_nwo | sqlc-dev/sqlc |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width