René's URL Explorer Experiment


Title: Type annotations for query parameters · Issue #2800 · sqlc-dev/sqlc · GitHub

Open Graph Title: Type annotations for query parameters · Issue #2800 · sqlc-dev/sqlc

X Title: Type annotations for query parameters · Issue #2800 · sqlc-dev/sqlc

Description: I propose adding type annotations for query parameters. Users will no longer need to cast parameters to the desired type. This proposal builds on Andrew's query annotation work for vet. Unified syntax We added the @sqlc-vet-disable annot...

Open Graph Description: I propose adding type annotations for query parameters. Users will no longer need to cast parameters to the desired type. This proposal builds on Andrew's query annotation work for vet. Unified syn...

X Description: I propose adding type annotations for query parameters. Users will no longer need to cast parameters to the desired type. This proposal builds on Andrew's query annotation work for vet. Unified...

Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/2800

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type annotations for query parameters","articleBody":"I propose adding type annotations for query parameters. Users will no longer need to cast parameters to the desired type. This proposal builds on Andrew's query annotation\r\nwork for `vet`.\r\n\r\n## Unified syntax\r\n\r\nWe added the `@sqlc-vet-disable` annotation to disable vet rules on a per-query\r\nbasis. We can extend this syntax to support other per-query configuration\r\noptions.\r\n\r\nThe current syntax for query name and command are different, so we'll\r\nstandardize on the `@` prefix. This will be the new, preferred syntax for name\r\nand command.\r\n\r\n```sql\r\n-- name: GetAuthor :one\r\nSELECT * FROM authors\r\nWHERE id = $1 LIMIT 1;\r\n\r\n-- becomes\r\n\r\n-- @name GetAuthor :one\r\nSELECT * FROM authors\r\nWHERE id = $1 LIMIT 1;\r\n```\r\n\r\nThe existing syntax will continue to work, but it will be an error to use both\r\nannotations on a single query.\r\n\r\n```sql\r\n-- INVALID!\r\n-- name: GetAuthor :one\r\n-- @name GetAuthor :one\r\nSELECT * FROM authors\r\nWHERE id = $1 LIMIT 1;\r\n```\r\n\r\n## Query command\r\n\r\nToday, queries must have a name and a command. With the new syntax, the\r\ncommand option will default to `exec`.\r\n\r\n```sql\r\n-- These two annotations are the same\r\n\r\n-- @name DeleteAuthor :exec\r\nDELETE FROM authors\r\nWHERE id = $1;\r\n\r\n-- @name DeleteAuthor\r\nDELETE FROM authors\r\nWHERE id = $1;\r\n```\r\n\r\n### Validation\r\n\r\nsqlc will delegate command validation to codegen plugins, allowing plugins to implement new commands without having\r\nto merge anything into sqlc.\r\n\r\nIf you want to still validate those, you can simulate the current behavior by\r\nusing this vet rule.\r\n\r\n```yaml\r\nrules:\r\n  - name: validate-command\r\n    rule: |\r\n      !(query.cmd in [\"exec\", \"one\", \"many\", \"execrows\", \"execlastid\", \"execresult\", \"batchone\", \"batchmany\", \"batchexec\"])\r\n```\r\n\r\n### Type annotations for query parameters\r\n\r\nThe `@param` annotation supports passing type information without having to use\r\na cast. \r\n\r\n```sql\r\n-- @param name type\r\n```\r\n\r\nCasts are required today when sqlc infers an incorrect type, but these\r\ncasts are passed down to the engine itself, possibly hurting performance. \r\nFor example, this cast is required to get sqlc working correctly, but isn't needed at runtime.\r\n\r\n```sql\r\n-- @name ListAuthorsByIDs :many\r\nSELECT * FROM authors\r\nWHERE id = ANY(@ids::bigint[]);\r\n```\r\nHere's what it looks like with the new syntax. The type annotation is\r\nengine-specific and is the same that you'd pass to CAST or CREATE TABLE.\r\n\r\n```sql\r\n-- @name ListAuthorsByIDs :many\r\n-- @param ids BIGINT[]\r\nSELECT * FROM authors\r\nWHERE id = ANY(@ids);\r\n```\r\n\r\nIf a parameter has a type annotation, that will be used instead of inferring the\r\ntype from the query.\r\n\r\n### NULL values\r\n\r\nsqlc will infer the nullability of parameters by default. You can force a parameter to be nullable using the `?` operator, or not null using the `!` operator.\r\n\r\n```sql\r\n-- @name CreateAuthor :one\r\n-- @param name! TEXT -- force NOT NULL\r\n-- @param bio? TEXT -- force NULL\r\nINSERT INTO authors (\r\n  name, bio\r\n) VALUES (\r\n  @name, @bio\r\n)\r\nRETURNING *;\r\n```\r\n\r\n### Positional parameters\r\n\r\nIf your parameters do not have a given name, you can refer to them by number to add a type annotation and nullability.\r\n\r\nFor PostgreSQL:\r\n\r\n```sql\r\n-- @name CreateAuthor :one\r\n-- @param 1? TEXT\r\n-- @param 2? TEXT\r\nINSERT INTO authors (\r\n  name, bio\r\n) VALUES (\r\n  $1, $2\r\n)\r\nRETURNING *;\r\n```\r\n\r\nAnd for MySQL or SQLite:\r\n\r\n```sql\r\n-- @name CreateAuthor :one\r\n-- @param 1? TEXT\r\n-- @param 2? TEXT\r\nINSERT INTO authors (\r\n  name, bio\r\n) VALUES (\r\n  ?1, ?2\r\n);\r\n```\r\n\r\n## sqlc.arg / sqlc.narg\r\n\r\nUsing the proposed annotation syntax allows you to replace `sqlc.arg()` and `sqlc.narg()`.\r\n\r\nFor example this query with `sqlc.arg()` and `sqlc.narg()`\r\n\r\n```sql\r\n-- name: UpdateAuthor :one\r\nUPDATE author\r\nSET\r\n name = coalesce(sqlc.narg('name')::text, name),\r\n bio = coalesce(sqlc.narg('bio')::text, bio)\r\nWHERE id = sqlc.arg('id')::bigint\r\nRETURNING *;\r\n```\r\n\r\nis equivalent to this one without\r\n\r\n```sql\r\n-- name: UpdateAuthor :one\r\n-- @param name? TEXT\r\n-- @param bio? TEXT\r\n-- @param id! BIGINT\r\nUPDATE author\r\nSET\r\n name = coalesce(@name, name),\r\n bio = coalesce(@bio, bio)\r\nWHERE id = @id\r\nRETURNING *;\r\n```\r\n\r\n`sqlc.arg` and `sqlc.narg` will continue to work, but will likely be deprecated in favor of the `@foo` syntax.\r\nYou can use `sqlc.arg()` with the new `@param` annotation syntax (to avoid explicit casts), but not `sqlc.narg()`. This constraint\r\nis intended to eliminate confusion about precedence of nullability directives.\r\n\r\nSo for example this will work\r\n\r\n```sql\r\n-- name: UpdateAuthor :one\r\n-- @param id! BIGINT\r\nUPDATE author\r\nSET\r\n name = coalesce(sqlc.narg('name')::text, name),\r\n bio = coalesce(sqlc.narg('bio')::text, bio)\r\nWHERE id = sqlc.arg('id')\r\nRETURNING *;\r\n```\r\n\r\nbut this won't\r\n\r\n```sql\r\n-- name: UpdateAuthor :one\r\n-- @param name TEXT\r\n-- @param bio TEXT\r\nUPDATE author\r\nSET\r\n name = coalesce(sqlc.narg('name'), name),\r\n bio = coalesce(sqlc.narg('bio'), bio)\r\nWHERE id = sqlc.arg('id')::bigint\r\nRETURNING *;\r\n```\r\n\r\nTo make this work, switch `sqlc.narg` to `sqlc.arg` and add a `?` to the param annotation.\r\n\r\n```sql\r\n-- name: UpdateAuthor :one\r\n-- @param name? TEXT\r\n-- @param bio? TEXT\r\nUPDATE author\r\nSET\r\n name = coalesce(sqlc.arg('name'), name),\r\n bio = coalesce(sqlc.arg('bio'), bio)\r\nWHERE id = sqlc.arg('id')::bigint\r\nRETURNING *;\r\n```\r\n\r\n## Why comments?\r\n\r\nWe're using comments instead of `sqlc.*` functions to avoid engine-specific parsing issues. For example, we've run into issues with the MySQL parser not support functions in certain parameter locations.\r\n\r\n## Full example\r\n\r\nThis is the normal example from the playground using the new syntax.\r\n\r\n```sql\r\n-- @name GetAuthor :one\r\n-- @param id! BIGINT\r\nSELECT * FROM authors\r\nWHERE id = @id LIMIT 1;\r\n\r\n-- @name ListAuthors :many\r\nSELECT * FROM authors\r\nORDER BY name;\r\n\r\n-- @name CreateAuthor :one\r\n-- @param name! TEXT\r\n-- @param bio TEXT\r\nINSERT INTO authors (\r\n  name, bio\r\n) VALUES (\r\n  @name, @bio\r\n)\r\nRETURNING *;\r\n\r\n-- @name GetAuthor\r\n-- @param id! BIGINT\r\nDELETE FROM authors\r\nWHERE id = @id;\r\n```\r\n\r\n## Future\r\nThe plan is to use similar annotations to support type annotations for output columns and values, Go type overrides for parameters and outputs, and JSON unmarshal / marshal hints.","author":{"url":"https://github.com/kyleconroy","@type":"Person","name":"kyleconroy"},"datePublished":"2023-10-03T15:26:45.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":11},"url":"https://github.com/2800/sqlc/issues/2800"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:8bfada1b-4b07-28be-e4e0-d335e52c7abe
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id96CA:B97C3:A98541C:EDCA445:6A50ED5D
html-safe-nonce464a281ce94833321a490360c92c3f99a550e041c94162de9eb9d3366213eb70
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NkNBOkI5N0MzOkE5ODU0MUM6RURDQTQ0NTo2QTUwRUQ1RCIsInZpc2l0b3JfaWQiOiIxNDM2MjUxODIzMDczNzE3NTk3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac289d49d982b861acad559a2544c456de8791bd8cf1d8635cd21a28bd7c4ad78a
hovercard-subject-tagissue:1924394815
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sqlc-dev/sqlc/2800/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c5b4c5f5792e2f2313c098f1803bce947d1fed0cc2c12f1e03f141d04d93ad01/sqlc-dev/sqlc/issues/2800
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c5b4c5f5792e2f2313c098f1803bce947d1fed0cc2c12f1e03f141d04d93ad01/sqlc-dev/sqlc/issues/2800
og:image:altI propose adding type annotations for query parameters. Users will no longer need to cast parameters to the desired type. This proposal builds on Andrew's query annotation work for vet. Unified syn...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamekyleconroy
hostnamegithub.com
expected-hostnamegithub.com
None5266e58c17a510c403505cc811606465e90a881d2007ee7df1c4800d5c659838
turbo-cache-controlno-preview
go-importgithub.com/sqlc-dev/sqlc git https://github.com/sqlc-dev/sqlc.git
octolytics-dimension-user_id136738596
octolytics-dimension-user_loginsqlc-dev
octolytics-dimension-repository_id193160679
octolytics-dimension-repository_nwosqlc-dev/sqlc
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id193160679
octolytics-dimension-repository_network_root_nwosqlc-dev/sqlc
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release5ec60191a48933536a90c8a19f47142fcd0d4739
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/sqlc-dev/sqlc/issues/2800#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsqlc-dev%2Fsqlc%2Fissues%2F2800
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsqlc-dev%2Fsqlc%2Fissues%2F2800
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=sqlc-dev%2Fsqlc
Reloadhttps://github.com/sqlc-dev/sqlc/issues/2800
Reloadhttps://github.com/sqlc-dev/sqlc/issues/2800
Reloadhttps://github.com/sqlc-dev/sqlc/issues/2800
Please reload this pagehttps://github.com/sqlc-dev/sqlc/issues/2800
sqlc-dev https://github.com/sqlc-dev
sqlchttps://github.com/sqlc-dev/sqlc
Notifications https://github.com/login?return_to=%2Fsqlc-dev%2Fsqlc
Fork 1.1k https://github.com/login?return_to=%2Fsqlc-dev%2Fsqlc
Star 18k https://github.com/login?return_to=%2Fsqlc-dev%2Fsqlc
Code https://github.com/sqlc-dev/sqlc
Issues 597 https://github.com/sqlc-dev/sqlc/issues
Pull requests 106 https://github.com/sqlc-dev/sqlc/pulls
Discussions https://github.com/sqlc-dev/sqlc/discussions
Actions https://github.com/sqlc-dev/sqlc/actions
Projects https://github.com/sqlc-dev/sqlc/projects
Security and quality 0 https://github.com/sqlc-dev/sqlc/security
Insights https://github.com/sqlc-dev/sqlc/pulse
Code https://github.com/sqlc-dev/sqlc
Issues https://github.com/sqlc-dev/sqlc/issues
Pull requests https://github.com/sqlc-dev/sqlc/pulls
Discussions https://github.com/sqlc-dev/sqlc/discussions
Actions https://github.com/sqlc-dev/sqlc/actions
Projects https://github.com/sqlc-dev/sqlc/projects
Security and quality https://github.com/sqlc-dev/sqlc/security
Insights https://github.com/sqlc-dev/sqlc/pulse
Type annotations for query parametershttps://github.com/sqlc-dev/sqlc/issues/2800#top
proposalhttps://github.com/sqlc-dev/sqlc/issues?q=state%3Aopen%20label%3A%22proposal%22
https://github.com/kyleconroy
kyleconroyhttps://github.com/kyleconroy
on Oct 3, 2023https://github.com/sqlc-dev/sqlc/issues/2800#issue-1924394815
proposalhttps://github.com/sqlc-dev/sqlc/issues?q=state%3Aopen%20label%3A%22proposal%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.