Generate type safe Go from SQL

Overview

sqlc: A SQL Compiler

go Go Report Card

sqlc generates type-safe code from SQL. Here's how it works:

  1. You write queries in SQL.
  2. You run sqlc to generate code with type-safe interfaces to those queries.
  3. You write application code that calls the generated code.

Check out an interactive example to see it in action.

Overview

Sponsors

sqlc development is funded by our generous sponsors, including the following companies:

If you use sqlc at your company, please consider becoming a sponsor today.

Sponsors receive priority support via the sqlc Slack organization.

Acknowledgements

sqlc was inspired by PugSQL and HugSQL.

Issues
  • Add support for pgx

    Add support for pgx

    Get out the trumpets and ready the 21-gun salute, lib/pq is deprecated (#470). This means it's time to support it's successor, pgx.

    This will be the main tracking issue for pgx. It supersedes #28, as I have no intention of adding support for any additional PostgreSQL drivers beyond pgx.

    enhancement :books: postgresql :wrench: golang 
    opened by kyleconroy 26
  • Kotlin prototype

    Kotlin prototype

    Updated TODO:

    • [x] New config format https://github.com/kyleconroy/sqlc/issues/302
    • [x] Adds CI with tests for authors https://github.com/mightyguava/sqlc/pull/2
    • [x] Tests for booktest and ondeck https://github.com/mightyguava/sqlc/pull/4
    • [x] move ktgen.go to kotlin/gen.go https://github.com/mightyguava/sqlc/pull/3
    • [x] Rewrite $1 to ? in query parsing instead of with a HACK in code generation https://github.com/mightyguava/sqlc/pull/5
    • [x] Get some Kotlin folks to review generated code https://github.com/mightyguava/sqlc/pull/6
    • [ ] Support MySQL (probably needs to wait for experimental MySQL parser to stabilize a bit)

    I wanted to learn more about how sqlc works, so I took a stab at https://github.com/kyleconroy/sqlc/issues/287 anyways to see how hard it would be.

    Here's a prototype implementation for Kotlin. It's pretty rough around the edges but tests pass, and I was able to generate and compile all of the existing examples in Kotlin. authors has tests written in Kotlin to confirm that the generated code works. I haven't gotten to the rest. The sqlc codebase is pretty easy to work in! I'm impressed.

    I basically just copied gen.go into ktgen.go and renamed all the functions. It should really be its own package, but that can come if this PR actually has a chance of getting merged 😄While the code follows the same patterns as the Go generator, there were enough changes that I'm not sure much code reuse is possible.

    I split out the generated examples and a working gradle project because it would pollute this PR too much. They can be viewed here: https://github.com/mightyguava/sqlc/pull/1

    Things that work:

    • Primitives
    • NULL primitives
    • Generating schema, param, and return classes
    • one/many/exec queries

    Things that compile but I haven't tested:

    • Enums
    • Dates
    • NULLS for these types
    • Arrays
    • execRow queries

    Things that don't work:

    • Type overrides - there's no fancy Scan() interface in JDBC to support arbitrary types, so this'll require generating code to call Adapter classes for individual types. Not hard, probably, just didn't seem worthwhile exploring in a prototype
    • JSON tags - no such thing in Java/Kotlin
    • MySQL - that part of the codebase seems to be iterating rapidly, so I decided to just look at Postgres for now.

    Things that work differently:

    • Numbered parameters - JDBC seems to only support ? as placeholders. I had to write a HACK to convert things like $1 to ?. This is buggy and terrible. It also means that you can't use $1 multiple times in the same query to reference the same value. If the AST was available in the generator part then I can probably figure out how to do something better.
    • I only generate a single QueriesImpl.kt for the queries instead one for each sql file. This is because you can't have a class span multiple files in Kotlin.
    • Prepared statements - According to both JDBC and Postgres JDBC Driver docs, prepared statements are implicitly cached, so the EmitPreparedQueries option is likely unneeded here.

    Things to think about:

    • The code is in Kotlin, meaning Java projects can use it, but they need to have the Kotlin compiler. An alternative is to have sqlc just generate a self-contained module that will produce a jar for the project to depend on. I could have generated Java instead, but that would have resulted in at least 5X more generated LOC of mostly boilerplate (though I guess technically everything sqlc generates is boilerplate). I also just don't like writing Java.
    • Remove the interface option, make it required: Unlike Go, interfaces are basically mandatory in the JVM if you want to do proper unit testing. You can't just create interfaces for arbitrary classes.
    • I haven't actually spent that much time working with JDBC before, so some of the generated code could be questionable. More tests will help tell!

    Please take a look! While I'd love for this code to get merged, I understand if you don't want to take on the additional maintenance burden that another language would add. Though, I wouldn't mind helping to maintain it.

    opened by mightyguava 23
  • Fix complex packages import paths

    Fix complex packages import paths

    EDIT Please skip directly to this reply as none of what I made matters anymore since I found another import path case which fails with this current PR.

    See #462 and #255

    Attempting to fix the parsing of complex package names by leveraging regex capabilities to capture the relevant parts.

    After smashing my head for an hour I asked for help on Reddit, details of the regex expression can be found here, credits for the regex go to /u/Lee_Dailey.

    I've also added the relevant test entries which used to fail and now they pass.


    Notes

    I've left the original code as commented out for reference, it should be removed if the PR is to be accepted to avoid polluting the code with unnecessary lines.

    The regex expression can be easily batch tested here to ensure it satisfies package import names, I am not aware of any other strange import path other than gopkg.in/guregu/null.v3/zero. To nitpick the issue we should find other packages with complex import paths. I can't guarantee that this will work with every package ever.

    Two assumptions on fully qualified names have been made to build the regex:

    1. A type name is always in the form of pkgName.TypeName
    2. The import path ends with the rightmost forward slash

    This led to formulate these two regex:

    • To match a package type: match everything backwards from the end of the string to the first encountered forward slash /
    • To match a package name: match everything forward from the start of the string till the last dot found
    opened by maxiride 17
  • Update configuration structure to support multiple languages

    Update configuration structure to support multiple languages

    The current version of the configuration file can't support multiple languages. Packages have Go-specific settings that do not apply to other languages.

    enhancement 
    opened by kyleconroy 15
  • RFC: nullability (and other metadata) for input arguments / parameters to a query

    RFC: nullability (and other metadata) for input arguments / parameters to a query

    What do you want to change?

    Kudos Before I start, I wanted to give a huge kudos for sqlc - I love it. As a backend developer who knows SQL very well, I fight every single ORM I have used trying to do any SQL more complicated than CRUD. Thank you for creating this.

    Background I use sqlc alongside Postgres, as sort of a foreign function interface to SQL. I manage a PostgreSQL database with about 100 different tables and working in Typescript and Go to support every stage of solar operations for Bright. We do not currently use sqlc at Bright - but I do use it personally, but I am evaluating it Bright as well.

    The types of problems I work on are not always CRUD-y (though sqlc works really well if they are). And the challenge I am facing is that SQL is a little less-expressive than the target languages in some cases, and therefore using sqlc as a FFI is a little clunky. A quick glance through the some open issues, shows that "nullability" is one of those deficiencies. sqlc does its best to always "do the right thing" and that has gotten it very far indeed. But I think a simple change can give developers full control, and allow for sqlc to keep getting smarter (better interred mappings / types).

    Say for example, instead of finding a really good GeoJSON library in my target language, I wanted to rely on PostGIS.

    - name: WKBToGeoJSON :one
    select ST_AsGeoJSON(ST_GeomFromWKB(sqlc.arg('wkb_bytes'))::text as output;
    

    I would love for this Go code to be generated:

    func (q *Queries) WKBToGeoJSON(ctx context.Context, db DBTX, wkbBytes []byte) (string, error) {
        // ...
    }
    

    Or perhaps a more common use case, partial updates

    -- name: EditThing :exec
    update thing
    set
      name = coalesce(sqlc.arg('name'), name),
      description = coalesce(sqlc.arg('description'), description),
      status = coalesce(sqlc.arg('status'), status)
    where thing.id = sqlc.arg(id)
    
    -- name, description and status are all nullable arguments, id is not.
    

    Proposal I think sqlc should expose a way (like overrides) in the query itself to control aspects of the generated code in the target languages.

    Allow sqlc.arg(name) to accept more variadic arguments (similar to jsonb_build_object) to build up string key, and json value pairs of "configuration" for that arguments. I think sqlc itself should not really know too much about the semantics of these key/value pairs, and just pass them along to the code generators in the target language.

    -- name: EditThing :exec
    update thing
    set
      name = coalesce(sqlc.arg('new_name', 'nullable', true, 'type', 'string'), name),
      description = coalesce(sqlc.arg('description', nullable, true), description),
      status = coalesce(sqlc.arg('status', 'nullable', true), status)
    where thing.id = sqlc.arg(id)
    

    This also allows the developer to control the API interface much better, even if the database is a little wacky. Imagine if there was a column name that was nullable for historical reasons, but any new insert to name it should be non null. sqlc should support that use case too.

    I am not a super-polyglot, but if there were a C++ backend for sqlc, you probably wouldn't just want nullable but also maybe an option for the type of pointer that should be generated raw pointer? std::shared_ptr? std::unique_ptr? boost::? std::optional ?

    -- name: GetThingForCpp :one
    select
      id,
      first_name,
      last_name,
      sqlc.output(concat(first_name, ' ', last_name), 'nullable', false, 'ref_counted', true) as display_name
    where ....
    

    This is very similar to overrides, but on a query-by-query basis, and expressed directly in SQL instead of away from the code in a config file.

    Implementation I got pretty far along in the implementation, and I'd be happy to push it all the way through - but I hit a big snag at the SQL rewriting. Rewriting the AST is no big deal (and I had the code working for that). But the way in which sqlc rewrites the original source seems to be engine independent, and a little hacky. Performing the source rewrites with an AST "deparse" (like in pg_query_go) would solve this with almost no problem. Or if the AST has more source location info in it (not just a start location, but also an end location), that might work too. But this seemed like a bigger refactor, and I wanted to get feedback on it first.

    I'd love to know your thoughts @kyleconroy and from the general sqlc community too. I'd be happy to help and make this a reality.

    What database engines need to be changed?

    PostgreSQL, MySQL

    What programming language backends need to be changed?

    No response

    enhancement triage 
    opened by skabbes 14
  • Failing to install sqlc v1.5.0 / latest

    Failing to install sqlc v1.5.0 / latest

    Issue

    go install github.com/kyleconroy/sqlc/cmd/sqlc seems to be failing due to dolphin changes

    How to reproduce

    As there are some C dependencies in sqlc, we have the following target on our Makefile:

    install-generators:
    	# more tools...
    	tmp_dir=sqlc-$(shell date +'%s') && mkdir -p /tmp/$$tmp_dir && cd /tmp/$$tmp_dir && go mod init tmp && GO11MODULE=on go install github.com/kyleconroy/sqlc/cmd/sqlc && cd - # it has C dependencies that would be a headache using go mod vendor
    

    But now it crashes with the following error:

    go: creating new go.mod: module tmp
    go: finding module for package github.com/kyleconroy/sqlc/cmd/sqlc
    go: found github.com/kyleconroy/sqlc/cmd/sqlc in github.com/kyleconroy/sqlc v1.5.0
    # github.com/kyleconroy/sqlc/internal/engine/dolphin
    /Users/brunosilva/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/engine/dolphin/convert.go:197:13: n.Schema undefined (type *"github.com/pingcap/parser/ast".FuncCallExpr has no field or method Schema)
    /Users/brunosilva/go/pkg/mod/github.com/kyleconroy/[email protected]/internal/engine/dolphin/convert.go:216:14: n.Offset undefined (type *"github.com/pingcap/parser/ast".FuncCallExpr has no field or method Offset)
    

    Solution for now:

    We've changed the script to download a specific version for now

    install-generators:
    	# more tools...
    	tmp_dir=sqlc-$(shell date +'%s') && mkdir -p /tmp/$$tmp_dir && cd /tmp/$$tmp_dir && go mod init tmp && go mod edit -require github.com/kyleconroy/[email protected] && go install github.com/kyleconroy/sqlc/cmd/sqlc && cd - # it has C dependencies that would be a headache using go mod vendor
    

    EDIT: Above solution as a bash/shell script (above one is for Makefile):

    tmp_dir=sqlc-$(date +'%s') && \
    mkdir -p /tmp/$tmp_dir && \
    cd /tmp/$tmp_dir && \
    go mod init tmp && \
    go mod edit -replace github.com/pingcap/parser=github.com/kyleconroy/[email protected] && \
    go get github.com/kyleconroy/sqlc/cmd/[email protected] && \
    cd - # it has C dependencies that would be a headache using go mod vendor
    
    bug documentation upstream 
    opened by brunoluiz 13
  • syntax error at or near

    syntax error at or near "$"

    Copying the 25 line sample config exactly as shown, pasting it into a file, and running it with sqlc compile or sqlc generate leaves me with the following errors:

    test.sql:9:15: syntax error at or near "$"
    test.sql:8:1: failed to parse query leading comment
    test.sql:19:5: syntax error at or near "$"
    test.sql:20:1: syntax error at or near "2"
    test.sql:25:14: syntax error at or near "$"
    

    I tried with an sqlc init YAML file and the JSON file design in https://github.com/kyleconroy/sqlc/tree/master/examples/authors.

    sqlc version: v1.2.1-devel

    documentation :books: mysql 
    opened by Xeoncross 13
  • feat: Add embedded structs based on result match

    feat: Add embedded structs based on result match

    Identifies every column with a returned struct for queries.

    This is a breaking change. I'm very open to comments, ideas, or reasons why this shouldn't even happen. To prevent breakage we could move this under a configuration option, and eventually migrate to default in a major version.

    -- schema.sql
    CREATE TABLE users (id text, email text);
    CREATE TABLE usernames (user_id text, name text);
    
    -- query.sql
    -- name: GetUsers :many
    SELECT * FROM users;
    
    -- name: GetUsersWithUsername :many
    SELECT users.*, usernames.name FROM users JOIN usernames ON users.id = usernames.user_id;
    

    Before:

    type GetUsersWithUsernameRow struct {
    	ID    sql.NullString `db:"id"`
    	Email sql.NullString `db:"email"`
    	Name  sql.NullString `db:"name"`
    }
    

    After:

    type GetUsersWithUsernameRow struct {
    	User
    	Name sql.NullString `db:"name"`
    }
    
    opened by kylecarbs 10
  • Add support for custom struct tags

    Add support for custom struct tags

    When working with structs, struct tags are used to add additional information to a field. This is for example very common when using validation packages. But currently there is no way to add additional struct tags to take advantage of this.

    I think it would be a great idea to add a comment syntax just like the -- name:NameForQuery for table columns.

    A possible option to express these custom struct tags would be to write create table statements like this:

    CREATE TABLE user (
      id   BIGSERIAL PRIMARY KEY,
    
      -- tags:`validate:"min=10,max=0"`
      username text NOT NULL,
    
      -- tags:`validate:"gt=18"`
      age  integer NOT NULL,
    
      -- tags:`validate:"url"`
      website text NOT NULL
    );
    

    or alternatively:

    CREATE TABLE user (
      id   BIGSERIAL PRIMARY KEY,
      username text NOT NULL,     -- tags:`validate:"min=10,max=0"`
      age integer NOT NULL,       -- tags:`validate:"gt=18"`
      website text NOT NULL       -- tags:`validate:"url"`
    );
    
    enhancement 
    opened by philippta 10
  • Parameters not recognized in jsonpath expressions

    Parameters not recognized in jsonpath expressions

    Version

    1.13.0

    What happened?

    When using jsonpath (@?) operations in postgres, sqlc doesn't recognize any of the forms for query input arguments.

    input:

    CREATE TABLE entries (
      id integer not null,
      entry jsonb not null,
      primary key (id)
    );
    
    -- name: GetEntry :one
    select * from entries
      where entry @? '$.a[*] ? (@.b == "@expr")'
      or entry @? '$.a[*] ? (@.b == "sqlc.arg(expr)")'
      or entry @? '$.a[*] ? (@.b == "$1")'
      limit 1;
    

    output:

    // Code generated by sqlc. DO NOT EDIT.
    // versions:
    //   sqlc v1.13.0
    // source: query.sql
    
    package db
    
    import (
    	"context"
    )
    
    const getEntry = `-- name: GetEntry :one
    select id, entry from entries
      where entry @? '$.a[*] ? (@.b == "@expr")'
      or entry @? '$.a[*] ? (@.b == "sqlc.arg(expr)")'
      or entry @? '$.a[*] ? (@.b == "$1")'
      limit 1
    `
    
    func (q *Queries) GetEntry(ctx context.Context) (Entry, error) {
    	row := q.db.QueryRowContext(ctx, getEntry)
    	var i Entry
    	err := row.Scan(&i.ID, &i.Entry)
    	return i, err
    }
    

    I'd expect GetEntry to take an argument (presumably of type json.RawMessage).

    Relevant log output

    n/a
    

    Database schema

    CREATE TABLE entries (
      id integer not null,
      entry jsonb not null,
      primary key (id)
    );
    

    SQL queries

    -- name: GetEntry :one
    select * from entries
      where entry @? '$.a[*] ? (@.b == "@expr")'
      or entry @? '$.a[*] ? (@.b == "sqlc.arg(expr)")'
      or entry @? '$.a[*] ? (@.b == "$1")'
      limit 1;
    

    Configuration

    {
      "version": 1,
      "packages": [
        {
          "name": "models",
          "path": "internal/models/",
          "engine": "postgresql",
          "schema": "internal/models/migrations/",
          "queries": "internal/models/",
          "sql_package": "pgx/v4",
          "emit_exported_queries": true
        }
      ],
      "overrides": [
        {
          "column": "entries.Entry",
          "go_type": "github.com/stephen/etc/entry.Entry"
        }
      ]
    }
    

    Playground URL

    https://play.sqlc.dev/p/5578358bb9d81fe7b82e1c6d21e4cb7e4609caeaa35beba19ef2559b708726d2

    What operating system are you using?

    macOS

    What database engines are you using?

    PostgreSQL

    What type of code are you generating?

    Go

    bug triage 
    opened by stephen 9
  • Order by clause: dynamic parameters ignored?

    Order by clause: dynamic parameters ignored?

    Hi, All!

    OS - Debian Linux 10 DB - PostgreSQL 13.2.

    W'd like to create dynamic parameter(s) for the simple query:

    -- name: PersonsGet :many
    select *
      from persons
     order by @sql_order::text
     limit @sql_limit offset @sql_offset;
    
    

    @sql_limit and @sql_offset works perfectly

    but @sql_order with dynamic parameters seems doesn't work... and always ignored...

    For example:

    • order by [id]
    • order by [id desc]
    • order by [name, created_at desc]

    Generated code below:

    
    const personsGet = `-- name: PersonsGet :many
    select id, salut, first_name, last_name, birthday, gender, status, created_at, updated_at
      from persons
     order by $1::text
     limit $3 offset $2
    `
    
    type PersonsGetParams struct {
    	SqlOrder  string `json:"sql_order"`
    	SqlOffset int32  `json:"sql_offset"`
    	SqlLimit  int32  `json:"sql_limit"`
    }
    
    func (q *Queries) PersonsGet(ctx context.Context, arg PersonsGetParams) ([]Persons, error) {
    	rows, err := q.query(ctx, q.personsGetStmt, personsGet, arg.SqlOrder, arg.SqlOffset, arg.SqlLimit)
    	if err != nil {
    		return nil, err
    	}
    	defer rows.Close()
    	items := []Persons{}
    	for rows.Next() {
    		var i Persons
    		if err := rows.Scan(
    			&i.ID,
    			&i.Salut,
    			&i.FirstName,
    			&i.LastName,
    			&i.Birthday,
    			&i.Gender,
    			&i.Status,
    			&i.CreatedAt,
    			&i.UpdatedAt,
    		); err != nil {
    			return nil, err
    		}
    		items = append(items, i)
    	}
    	if err := rows.Close(); err != nil {
    		return nil, err
    	}
    	if err := rows.Err(); err != nil {
    		return nil, err
    	}
    	return items, nil
    }
    
    

    Any ideas why? Any help would be appreciated!

    :books: postgresql 
    opened by basilex 9
  • Type overrides not working with nullable varchar in mysql

    Type overrides not working with nullable varchar in mysql

    Version

    1.14.0

    What happened?

    Type overrides not working with nullable varchar in mysql i have three varchar(255) fields with DEFAULT NULL. but for some reason types override does not work on those fields.

    Relevant log output

    No response

    Database schema

    this is the table, exported from adminer.
    
    CREATE TABLE `murid` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `nama` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `nis` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `rfid` varchar(255) NOT NULL,
      `role` tinyint(4) NOT NULL DEFAULT 3,
      `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `kelas` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
      `sekolah_id` int(11) NOT NULL,
      `hp_siswa` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `hp_orangtua` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `hp_pengurus` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
      `created_at` datetime NOT NULL DEFAULT current_timestamp(),
      `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
      PRIMARY KEY (`id`),
      UNIQUE KEY `nis` (`nis`),
      UNIQUE KEY `rfid` (`rfid`),
      KEY `sekolah_id` (`sekolah_id`),
      CONSTRAINT `murid_ibfk_1` FOREIGN KEY (`sekolah_id`) REFERENCES `sekolah` (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    

    SQL queries

    No response

    Configuration

    version: "2"
    sql:
    - schema: "schema.sql"
      queries: "query.sql"
      engine: "mysql"
      gen:
        go:
          package: "model"
          out: "."
          emit_db_tags: true
          emit_json_tags: true
          emit_methods_with_db_argument: true
          overrides:
            - db_type: "text"
              go_type: "smkti.pc/absensiNext/pkg/types.NullString"
              nullable: true
    

    Playground URL

    No response

    What operating system are you using?

    Linux

    What database engines are you using?

    MySQL

    What type of code are you generating?

    Go

    bug triage 
    opened by bokunodev 0
  • Support sqlite stdlib functions

    Support sqlite stdlib functions

    Add SQLite function catalogs listed in below pages.

    • https://www.sqlite.org/lang_aggfunc.html
    • https://www.sqlite.org/lang_mathfunc.html
    • https://www.sqlite.org/lang_corefunc.html

    Task

    • [x] Move existing builtins end to end test to builtins/postgresql
    • [x] Add function catalogs for SQLite
      • [x] Aggregate functions
      • [x] Mathematical functions
      • [x] Scalar functions
    • [x] Add end to end test

    Remarks

    Due to current SQLite parser limitation, we can't get correct type of arguments of function, so we can't overload function based on argument type like below.

    SELECT max(1); => integer
    SELECT max(1.0); => real
    SELECT max('text'); => text
    

    So arguments and return types of these functions like max are defined as any type due to this limitation.

    opened by hakobera 0
  • Code generation for a nullable type so that enums can work with sqlc.narg

    Code generation for a nullable type so that enums can work with sqlc.narg

    What do you want to change?

    As noted by #1683, when a table have a nullable column that has a enum type, or when a param of a enum type can be nullable, sqlc is enable to properly generate code that handles this situation.

    So, my proposition is to create a new boolean flag in the configuration file that indicates when a nullable type should be created for the enums declared in the schema. This way, old code generated by sqlc won't be changed by the introduction of this new feature.

    I will start working on a draft PR for this feature this weekend.

    What database engines need to be changed?

    PostgreSQL

    What programming language backends need to be changed?

    Go

    enhancement triage 
    opened by eullerpereira94 0
  • sqlite - use []byte for blobs

    sqlite - use []byte for blobs

    To keep the API consistent with mysql

    case "blob", "binary", "varbinary", "tinyblob", "mediumblob", "longblob":
        return "[]byte"
    

    and postgres

    case "bytea", "blob", "pg_catalog.bytea":
        return "[]byte"
    

    I suggest sqlc uses []byte instead of []uint8. If the field is nullable, it is pointles to have a *[]uint8 (or *[]byte) because slices are already pointers, and a slice can be nil

    opened by javiercbk 0
  • AVG should be nullable

    AVG should be nullable

    Version

    1.14.0

    What happened?

    I have the following query:

    SELECT AVG(rating)
    FROM product_ratings
    WHERE product_id = 1234;
    

    This generates the following go code:

    var avg string
    err := row.Scan(&avg)
    

    Notice how it uses a string for avg. I would like it to use an int, so I cast it like so:

    SELECT AVG(rating)::int
    FROM product_ratings
    WHERE product_id = 1234;
    

    Which generates the following code:

    var column_1 int32
    err := row.Scan(&column_1)
    

    Now, my issue is that the product_ratings table might not always have entries for all products so if the AVG(rating) statement returns NULL for a specific product, I get the following error:

    sql: Scan error on column index 0, name "avg": converting NULL to int32 is unsupported
    

    Is there a way to force sqlc to generate sql.NullInt32 instead of just int32?

    Relevant log output

    sql: Scan error on column index 0, name "avg": converting NULL to int32 is unsupported
    

    Database schema

    CREATE TABLE product_ratings
    (
      rating      smallint NOT NULL CHECK ( score <= 5 AND score >= 1 ),
      product_id  int      NOT NULL REFERENCES products (id)
    );
    
    CREATE TABLE products
    (
       id    serial PRIMARY KEY,
       name  VARCHAR(255)  NOT NULL
    );
    

    SQL queries

    SELECT AVG(rating)::int
    FROM product_ratings
    WHERE product_id = 1234;
    

    Configuration

    version: "1"
    packages:
      - name: "sql"
        path: "./internal/store/sql"
        queries: "./internal/store/sql/queries/"
        schema: "./internal/store/sql/migrations/"
        engine: "postgresql"
        emit_db_tags: true
        emit_prepared_queries: true
        emit_interface: false
        emit_exact_table_names: false
        emit_empty_slices: true
        emit_json_tags: true
        json_tags_case_style: "camel"
    

    Playground URL

    No response

    What operating system are you using?

    macOS

    What database engines are you using?

    PostgreSQL

    What type of code are you generating?

    Go

    bug triage 
    opened by odannyc 1
Releases(v1.14.0)
  • v1.14.0(Jun 10, 2022)

    What's Changed

    • fix(bundler): Only close multipart writer once by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1528
    • Adding LastInsertId for Go by @Xeoncross in https://github.com/kyleconroy/sqlc/pull/1484
    • build(deps): bump github.com/lib/pq from 1.10.4 to 1.10.5 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1534
    • build(deps): bump actions/setup-java from 2 to 3 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1542
    • build(deps): bump actions/setup-go from 2 to 3 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1543
    • build(deps): bump golang from 1.18.0 to 1.18.1 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1547
    • Added update query to postgresql docs by @airscholar in https://github.com/kyleconroy/sqlc/pull/1535
    • feat: run sqlc with docker on windows cmd by @ParvinEyvazov in https://github.com/kyleconroy/sqlc/pull/1557
    • Pydantic models config field by @danicc097 in https://github.com/kyleconroy/sqlc/pull/1538
    • build(deps): bump github.com/jackc/pgx/v4 from 4.15.0 to 4.16.0 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1562
    • Prepend an underscore to a StructName if it starts with a digit. by @avalonbits in https://github.com/kyleconroy/sqlc/pull/1554
    • fix typo in feature request bug template by @josharian in https://github.com/kyleconroy/sqlc/pull/1568
    • feat: Add JSON "codegen" output by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1565
    • build(deps): bump github.com/google/go-cmp from 0.5.7 to 0.5.8 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1573
    • UPDATED: sqlc.narg() - allow user-specified nullability by @skabbes in https://github.com/kyleconroy/sqlc/pull/1536
    • docs: add documentation for sqlc.narg() by @skabbes in https://github.com/kyleconroy/sqlc/pull/1580
    • docs: Add some more example code for running transactions by @Jille in https://github.com/kyleconroy/sqlc/pull/1583
    • build(deps): bump github.com/jackc/pgx/v4 from 4.16.0 to 4.16.1 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1597
    • Add 1.13.0 to Issue Template by @danielbprice in https://github.com/kyleconroy/sqlc/pull/1592
    • Add support for CHANGE COLUMN in MySQL by @dansimau in https://github.com/kyleconroy/sqlc/pull/1605
    • build(deps): bump golang from 1.18.1 to 1.18.2 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1609
    • docs: fix examples by @orisano in https://github.com/kyleconroy/sqlc/pull/1608
    • Fix panic when calling cmd.Do subsequently by @jlisthood in https://github.com/kyleconroy/sqlc/pull/1614
    • build(deps): bump github.com/lib/pq from 1.10.5 to 1.10.6 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1627
    • doc: typo fix in overrides config example by @reddec in https://github.com/kyleconroy/sqlc/pull/1619
    • [FIX] copyfrom imports by @positiveblue in https://github.com/kyleconroy/sqlc/pull/1626
    • internal/codegen: add Enum.Valid and AllEnumValues by @josharian in https://github.com/kyleconroy/sqlc/pull/1613
    • Refactor to Package Catalog to Improve Readability by @jakoguta in https://github.com/kyleconroy/sqlc/pull/1595
    • Validate sqlc function arguments by @ryan-berger in https://github.com/kyleconroy/sqlc/pull/1633
    • Bug fix to index relation creation bug by @jakoguta in https://github.com/kyleconroy/sqlc/pull/1640
    • docs: Add documentation for version 2 of the configuration file by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1657
    • build(deps): bump golang from 1.18.2 to 1.18.3 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1658
    • docs: Re-org the config page. Add JSON docs by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1659
    • build(deps): bump actions/setup-python from 3 to 4 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1666
    • add support for custom Go struct tags by @josharian in https://github.com/kyleconroy/sqlc/pull/1569
    • fix: fixed typo sql.narg in doc by @akhilmhdh in https://github.com/kyleconroy/sqlc/pull/1668
    • feat: Process-based codegen plugins by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1578
    • cmd/sqlc: Bump version to v1.14.0 by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1670

    New Contributors

    • @Xeoncross made their first contribution in https://github.com/kyleconroy/sqlc/pull/1484
    • @airscholar made their first contribution in https://github.com/kyleconroy/sqlc/pull/1535
    • @ParvinEyvazov made their first contribution in https://github.com/kyleconroy/sqlc/pull/1557
    • @danicc097 made their first contribution in https://github.com/kyleconroy/sqlc/pull/1538
    • @avalonbits made their first contribution in https://github.com/kyleconroy/sqlc/pull/1554
    • @josharian made their first contribution in https://github.com/kyleconroy/sqlc/pull/1568
    • @skabbes made their first contribution in https://github.com/kyleconroy/sqlc/pull/1536
    • @danielbprice made their first contribution in https://github.com/kyleconroy/sqlc/pull/1592
    • @jlisthood made their first contribution in https://github.com/kyleconroy/sqlc/pull/1614
    • @reddec made their first contribution in https://github.com/kyleconroy/sqlc/pull/1619
    • @positiveblue made their first contribution in https://github.com/kyleconroy/sqlc/pull/1626
    • @jakoguta made their first contribution in https://github.com/kyleconroy/sqlc/pull/1595
    • @ryan-berger made their first contribution in https://github.com/kyleconroy/sqlc/pull/1633
    • @akhilmhdh made their first contribution in https://github.com/kyleconroy/sqlc/pull/1668

    Full Changelog: https://github.com/kyleconroy/sqlc/compare/v1.13.0...v1.14.0

    Source code(tar.gz)
    Source code(zip)
    sqlc_1.14.0_darwin_amd64.tar.gz(8.50 MB)
    sqlc_1.14.0_darwin_amd64.zip(8.44 MB)
    sqlc_1.14.0_linux_amd64.tar.gz(7.81 MB)
    sqlc_1.14.0_linux_amd64.zip(7.75 MB)
    sqlc_1.14.0_windows_amd64.tar.gz(6.86 MB)
    sqlc_1.14.0_windows_amd64.zip(6.79 MB)
  • v1.13.0(Apr 3, 2022)

    What's Changed

    • Define code generation interface via Protocol Buffers by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1406
    • Refactor Kotlin codegen package to use plugin types by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1416
    • fix(postgresql): Remove extra newline with db argument by @rliebz in https://github.com/kyleconroy/sqlc/pull/1417
    • build(deps): bump github.com/jackc/pgx/v4 from 4.14.1 to 4.15.0 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1421
    • docs: Specify which db_type should be used in overrides by @egtann in https://github.com/kyleconroy/sqlc/pull/1427
    • fix copyfrom methods on interface not matching generated methods by @alexdulin in https://github.com/kyleconroy/sqlc/pull/1424
    • build(deps): bump golang from 1.17.6 to 1.17.7 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1431
    • docs: Add more documentation for go_type by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1432
    • Start filling out SQLite support by @PadraigK in https://github.com/kyleconroy/sqlc/pull/1410
    • Support codespace development by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1434
    • chore: fix extra newline in comments for copyfrom by @alexdulin in https://github.com/kyleconroy/sqlc/pull/1438
    • feat(pgx): Add support for batch operations by @jrperritt in https://github.com/kyleconroy/sqlc/pull/1437
    • SQLite: Adds a few more tests, fixes DROP TABLE by @PadraigK in https://github.com/kyleconroy/sqlc/pull/1443
    • Add support for delete statements by @PadraigK in https://github.com/kyleconroy/sqlc/pull/1447
    • feat(sqlite) Adds support for column aliases and function arguments by @PadraigK in https://github.com/kyleconroy/sqlc/pull/1455
    • feat(codegen): Insert comments in interfaces by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1458
    • build(deps): bump actions/setup-python from 2 to 3 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1461
    • Refactor go codegen to plugin types by @stephen in https://github.com/kyleconroy/sqlc/pull/1460
    • feat(sdk): Add the plugin SDK package by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1463
    • build(deps): bump actions/checkout from 2.4.0 to 3 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1464
    • refactor(cmd): Simplify codegen selection logic by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1466
    • chore: Generate marshal/unmarshal with vtprotobuf by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1467
    • Remove data_type from column proto by @stephen in https://github.com/kyleconroy/sqlc/pull/1468
    • docs: Add new logo by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1471
    • docs: Add hosted services to privacy / data guide by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1475
    • build(deps): bump golang from 1.17.7 to 1.17.8 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1476
    • docs: Add Plausible analytics by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1477
    • build(deps): bump github.com/spf13/cobra from 1.3.0 to 1.4.0 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1483
    • fix(compiler): Fix left join nullability with table aliases by @timstudd in https://github.com/kyleconroy/sqlc/pull/1491
    • build(endtoend): Don't run tests in testdata by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1492
    • build(deps): bump golang from 1.17.8 to 1.18.0 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1493
    • feat: Upload projects by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1436
    • Add a new issue template by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1497
    • docs: Add more examples to updating rows by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1499
    • config: Add basic fuzzing for config / overrides by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1500
    • build(deps): bump google.golang.org/protobuf from 1.27.1 to 1.28.0 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1504
    • Added postgres CALL support by @terrycain in https://github.com/kyleconroy/sqlc/pull/1495
    • Add version number to Python code comments by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1511
    • feat: Add sqlc version to generated Kotlin code by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1512
    • feat: Add sqlc version to generated Go code by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1513
    • feat: Pass sqlc version in codegen request by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1514
    • add materialized view support by @ludusrusso in https://github.com/kyleconroy/sqlc/pull/1509
    • fix: Regenerate testdata for CREATE TABLE AS by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1516
    • feat(python): Graduate Python support to beta by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1520
    • docs: Include scope in changelog format by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1521
    • release(cmd/sqlc): Bump to v1.13.0 by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1523

    New Contributors

    • @rliebz made their first contribution in https://github.com/kyleconroy/sqlc/pull/1417
    • @alexdulin made their first contribution in https://github.com/kyleconroy/sqlc/pull/1424
    • @jrperritt made their first contribution in https://github.com/kyleconroy/sqlc/pull/1437
    • @stephen made their first contribution in https://github.com/kyleconroy/sqlc/pull/1460
    • @terrycain made their first contribution in https://github.com/kyleconroy/sqlc/pull/1495
    • @ludusrusso made their first contribution in https://github.com/kyleconroy/sqlc/pull/1509

    Full Changelog: https://github.com/kyleconroy/sqlc/compare/v1.12.0...v1.13.0

    Source code(tar.gz)
    Source code(zip)
    sqlc_1.13.0_darwin_amd64.tar.gz(8.19 MB)
    sqlc_1.13.0_darwin_amd64.zip(8.14 MB)
    sqlc_1.13.0_linux_amd64.tar.gz(7.47 MB)
    sqlc_1.13.0_linux_amd64.zip(7.40 MB)
    sqlc_1.13.0_windows_amd64.tar.gz(6.56 MB)
    sqlc_1.13.0_windows_amd64.zip(6.50 MB)
  • v1.12.0(Feb 6, 2022)

    What's Changed

    • fix(sqlite): Update ANTLR v4 go.mod entry by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1336
    • fix(compiler): Check delete statements for CTEs by @wabain in https://github.com/kyleconroy/sqlc/pull/1329
    • First pass at AST-based codegen by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1333
    • Postgres Inherits by @timwmillard in https://github.com/kyleconroy/sqlc/pull/1339
    • chore(python): Add tests for :exec{result,rows} by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1344
    • Generate queries class by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1338
    • chore(python): Delete template-based codegen by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1345
    • fix(compiler): Fix validation of GROUP BY on field aliases by @timstudd in https://github.com/kyleconroy/sqlc/pull/1348
    • handling of dollar signs in function names by @Nlossae in https://github.com/kyleconroy/sqlc/pull/1359
    • Add support for CREATE TABLE a ( LIKE b ) for Postgres by @Jille in https://github.com/kyleconroy/sqlc/pull/1355
    • Update error message by @Threpio in https://github.com/kyleconroy/sqlc/pull/1366
    • feat(postgres): Add support for sql.NullInt16 by @egtann in https://github.com/kyleconroy/sqlc/pull/1376
    • Implement support for pgx's CopyFrom by @Jille in https://github.com/kyleconroy/sqlc/pull/1352
    • Fixed typo in select.md by @Hirochon in https://github.com/kyleconroy/sqlc/pull/1378
    • fix(copyfrom): Fix imports when non-copyfrom queries needed imports that copyfrom queries didn't by @Jille in https://github.com/kyleconroy/sqlc/pull/1386
    • build: Format all Go code by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1387
    • fix(postgresql): Remove extra comment newline by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1395
    • Migrate SQLite Grammar to maintained version by @PadraigK in https://github.com/kyleconroy/sqlc/pull/1397
    • Fix return type for subqueries in MySQL (#1383) by @dansimau in https://github.com/kyleconroy/sqlc/pull/1404
    • fix: Enable strict function checking by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1405
    • Bug: ALTER TABLE SET SCHEMA by @timwmillard in https://github.com/kyleconroy/sqlc/pull/1409
    • cmd/sqlc: Bump version to v1.12.0 by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1415

    New Contributors

    • @Nlossae made their first contribution in https://github.com/kyleconroy/sqlc/pull/1359
    • @Jille made their first contribution in https://github.com/kyleconroy/sqlc/pull/1355
    • @Threpio made their first contribution in https://github.com/kyleconroy/sqlc/pull/1366
    • @egtann made their first contribution in https://github.com/kyleconroy/sqlc/pull/1376
    • @Hirochon made their first contribution in https://github.com/kyleconroy/sqlc/pull/1378
    • @PadraigK made their first contribution in https://github.com/kyleconroy/sqlc/pull/1397
    • @dansimau made their first contribution in https://github.com/kyleconroy/sqlc/pull/1404

    Full Changelog: https://github.com/kyleconroy/sqlc/compare/v1.11.0...v1.12.0

    Source code(tar.gz)
    Source code(zip)
    sqlc_1.12.0_darwin_amd64.tar.gz(7.60 MB)
    sqlc_1.12.0_darwin_amd64.zip(7.55 MB)
    sqlc_1.12.0_linux_amd64.tar.gz(6.93 MB)
    sqlc_1.12.0_linux_amd64.zip(6.87 MB)
    sqlc_1.12.0_windows_amd64.tar.gz(6.01 MB)
    sqlc_1.12.0_windows_amd64.zip(5.96 MB)
  • v1.11.0(Nov 24, 2021)

    Bug Fixes

    • Update incorrect signatures (#1180)
    • Correct aggregate func sig (#1182)
    • Jsonb_build_object (#1211)
    • Case-insensitive identifiers (#1216)
    • Incorrect handling of meta (#1228)
    • Detect invalid INSERT expression (#1231)
    • Respect alias name for coalesce (#1232)
    • Mark nullable when casting NULL (#1233)
    • Support nullable fields in joins for MySQL engine (#1249)
    • Fix between expression handling of table references (#1268)
    • Support nullable fields in joins on same table (#1270)
    • Fix missing binds in ORDER BY (#1273)
    • Set RV for TargetList items on updates (#1252)
    • Fix MySQL parser for query without trailing semicolon (#1282)
    • Validate table alias references (#1283)
    • Add support for MySQL ON DUPLICATE KEY UPDATE (#1286)
    • Support references to columns in joined tables in UPDATE statements (#1289)
    • Add validation for GROUP BY clause column references (#1285)
    • Prevent variable redeclaration in single param conflict (#1298)
    • Use common params struct field for same named params (#1296)

    Documentation

    • Replace deprecated go get with go install (#1181)
    • Fix package name referenced in tutorial (#1202)
    • Add environment variables (#1264)
    • Add go.17+ install instructions (#1280)
    • Warn about golang-migrate file order (#1302)

    Features

    • Instrument compiler via runtime/trace (#1258)
    • Add MySQL support for BETWEEN arguments (#1265)

    Refactor

    • Move from io/ioutil to io and os package (#1164)

    Styling

    • Apply gofmt to sample code (#1261)

    New Contributors

    • @Juneezee made their first contribution in https://github.com/kyleconroy/sqlc/pull/1164
    • @ovadbar made their first contribution in https://github.com/kyleconroy/sqlc/pull/1072
    • @l4u made their first contribution in https://github.com/kyleconroy/sqlc/pull/1199
    • @oliverpool made their first contribution in https://github.com/kyleconroy/sqlc/pull/1204
    • @cqsd made their first contribution in https://github.com/kyleconroy/sqlc/pull/1209
    • @snakeclown made their first contribution in https://github.com/kyleconroy/sqlc/pull/1248
    • @timstudd made their first contribution in https://github.com/kyleconroy/sqlc/pull/1249
    • @orisano made their first contribution in https://github.com/kyleconroy/sqlc/pull/1261
    • @andreasgan made their first contribution in https://github.com/kyleconroy/sqlc/pull/1252
    • @lopezator made their first contribution in https://github.com/kyleconroy/sqlc/pull/1280
    • @danielmmetz made their first contribution in https://github.com/kyleconroy/sqlc/pull/1279
    • @wabain made their first contribution in https://github.com/kyleconroy/sqlc/pull/1301
    • @remLse made their first contribution in https://github.com/kyleconroy/sqlc/pull/1302

    Full Changelog: https://github.com/kyleconroy/sqlc/compare/v1.10.0...v1.11.0

    Source code(tar.gz)
    Source code(zip)
    sqlc_1.11.0_darwin_amd64.tar.gz(7.38 MB)
    sqlc_1.11.0_darwin_amd64.zip(7.32 MB)
    sqlc_1.11.0_linux_amd64.tar.gz(6.74 MB)
    sqlc_1.11.0_linux_amd64.zip(6.69 MB)
    sqlc_1.11.0_windows_amd64.tar.gz(5.39 MB)
    sqlc_1.11.0_windows_amd64.zip(5.35 MB)
  • v1.10.0(Sep 7, 2021)

    Release notes coming soon!

    What's Changed

    • Fix typos in howto/select generated code sample by @naisuuuu in https://github.com/kyleconroy/sqlc/pull/1125
    • Remove MYSQL_USER and MYSQL_PASSWORD from docker-compose.yaml by @robsonpeixoto in https://github.com/kyleconroy/sqlc/pull/1123
    • build(deps): bump golang from 1.16.7 to 1.17.0 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1129
    • ci: Upgrade Go to 1.17 by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1130
    • getting-started.md: fix typo by @LLLeon in https://github.com/kyleconroy/sqlc/pull/1133
    • Add RStudio to list of sponsors by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1131
    • endtoend: Run go test in CI by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1134
    • golang: Output NullUUID when necessary by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1137
    • codegen/golang: Consolidate import logic by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1139
    • codgen/golang: Use p[gq]type for network address types by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1142
    • When using pgx, map numeric types to pgtype.Numeric by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1143
    • Handle null PostgreSQL JSON values by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1145
    • codegen/golang: Add pgx support for range types by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1146
    • First attempt at a template for bugs by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1151
    • Re-order form, add configuration file section by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1153
    • codegen/golang: Use pgtype for hstore when using pgx by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1156
    • build(deps): bump github.com/lib/pq from 1.10.2 to 1.10.3 by @dependabot in https://github.com/kyleconroy/sqlc/pull/1160
    • docs: Fix invalid language support table by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1161
    • engine/mysql: Add support for LIKE by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1162
    • docs: Add a getting started guide for MySQL by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1163
    • cmd/sqlc: Bump version to v1.10.0 by @kyleconroy in https://github.com/kyleconroy/sqlc/pull/1165

    New Contributors

    • @naisuuuu made their first contribution in https://github.com/kyleconroy/sqlc/pull/1125
    • @robsonpeixoto made their first contribution in https://github.com/kyleconroy/sqlc/pull/1123
    • @LLLeon made their first contribution in https://github.com/kyleconroy/sqlc/pull/1133

    Full Changelog: https://github.com/kyleconroy/sqlc/compare/v1.9.0...v1.10.0

    Source code(tar.gz)
    Source code(zip)
    sqlc_1.10.0_darwin_amd64.tar.gz(7.24 MB)
    sqlc_1.10.0_darwin_amd64.zip(7.19 MB)
    sqlc_1.10.0_linux_amd64.tar.gz(6.63 MB)
    sqlc_1.10.0_linux_amd64.zip(6.57 MB)
    sqlc_1.10.0_windows_amd64.tar.gz(5.28 MB)
    sqlc_1.10.0_windows_amd64.zip(5.23 MB)
  • v1.9.0(Aug 13, 2021)

  • v1.8.0(May 3, 2021)

    Core

    • cmd: Fix sqlc init (#959)

    MySQL

    • Generate correct code for booleans
    • Calling mysql LAST_INSERT_ID(expr) function (#977)

    PostgreSQL

    • Support PostgreSQL 12 and 13 features by upgrading to pg_query_go/v2 (#949)
    • Fix ALTER TYPE to update column types (#973)

    Python

    • Add experimental Python support behind the --experimental flag
    Source code(tar.gz)
    Source code(zip)
    sqlc-v1.8.0-darwin-amd64.tar.gz(7.02 MB)
    sqlc-v1.8.0-darwin-amd64.zip(6.96 MB)
    sqlc-v1.8.0-linux-amd64.tar.gz(6.42 MB)
    sqlc-v1.8.0-linux-amd64.zip(6.35 MB)
    sqlc-v1.8.0-windows-amd64.tar.gz(5.07 MB)
    sqlc-v1.8.0-windows-amd64.zip(5.02 MB)
  • v1.7.0(Mar 1, 2021)

    Changelog

    Core

    • Initial Windows Support (#886)
    • UNION support (#896)
    • Publish documentation at https://docs.sqlc.dev
    • cmd: Allow config file location to be specified (#863)
    • Exit with non-zero error code when parsing fails (#870)

    MySQL

    • Compile tinyint(1) to bool (#873)
    • Support joins in update statements (#883)
    Source code(tar.gz)
    Source code(zip)
    sqlc-v1.7.0-darwin-amd64.tar.gz(11.35 MB)
    sqlc-v1.7.0-darwin-amd64.zip(11.31 MB)
    sqlc-v1.7.0-linux-amd64.tar.gz(10.18 MB)
    sqlc-v1.7.0-linux-amd64.zip(10.13 MB)
    sqlc-v1.7.0-windows-amd64.tar.gz(8.47 MB)
    sqlc-v1.7.0-windows-amd64.zip(8.44 MB)
  • v1.6.0(Nov 23, 2020)

    Notes

    The new MySQL engine, code-named Dolphin, is now default. The old engine has been removed. Your generated code will be different, but should be easy to adapt. Please open an issue if your code is now broken. If you were using the mysql:beta engine, you'll need to change that to mysql.

    Changelog

    Core

    • Add option to return emit db tags for struct fields (#656)
    • Add support for variadic stored procedures and functions (#798)
    • Fix issue installing sqlc via go get (#744)
    • Add support for nontrivial import paths for overrides (#785)

    Go

    • You can now specify the go_type import path of an override using an object instead of a string.
    overrides:
      - db_type: "uuid"
        go_type:
          import: "gopkg.in/guregu/null.v3"
          package: "null"
          type: "Bool"
    

    MySQL

    • Add function definitions for all built-in MySQL functions (#796)
    • Upgrade to the latest version of pingcap/parser (#744)
    Source code(tar.gz)
    Source code(zip)
    sqlc-v1.6.0-darwin-amd64.tar.gz(12.69 MB)
    sqlc-v1.6.0-darwin-amd64.zip(12.49 MB)
    sqlc-v1.6.0-linux-amd64.tar.gz(10.93 MB)
    sqlc-v1.6.0-linux-amd64.zip(10.83 MB)
  • v1.5.0(Aug 5, 2020)

    Notes

    The new MySQL engine, code-named Dolphin, has finally landed. To try it out, use mysql:beta for the engine value in sqlc.json. This uses the same compiler infrastructure as the PostgreSQL engine, giving it feature parity and making it easier to maintain.

    The old engine is official deprecated and will be placed behind the mysql:deprecated name in v1.6.0. The engine will be completely removed in v1.7.0. For more information, see the MySQL roadmap.

    On the PostgreSQL side of things, I've used some magic to generate function signatures for all built-in functions. This also includes many of the most popular PostgreSQL extensions. You will need to make sure to have CREATE EXTENSION ... in your schema to get this functionality.

    Changelog

    Core

    • Add option to return empty slices (instead of nil slices) in :many queries
    • Use "nullable" instead of "null" in configuration file (#571)
    • Add debugging support via SQLCDEBUG (#573)
    • Support calling functions with defaults (#635)

    PostgreSQL

    • Add support for the money type (#552)
    • Add support for 'cidr' and 'interval' types (#601)
    • Generate all functions in pg_catalog (#550)

    MySQL

    • Add the mysql:beta engine, which will be the default next release
    Source code(tar.gz)
    Source code(zip)
    sqlc-v1.5.0-darwin-amd64.tar.gz(15.16 MB)
    sqlc-v1.5.0-darwin-amd64.zip(15.24 MB)
    sqlc-v1.5.0-linux-amd64.tar.gz(13.28 MB)
    sqlc-v1.5.0-linux-amd64.zip(13.35 MB)
  • v1.4.0(Jun 17, 2020)

    Notes

    This release includes a complete refactor of the compiler internals. The compiler now uses a database-agnostic SQL AST. The PostgreSQL backend uses this new code path by default. I've tested the new path extensively, but you still may run into bugs. If you do, you can use the old code path by setting the the following environment variable: SQLC_EXPERIMENTAL_PARSER=off. The old code path will be removed in v1.5.0, so please report any bugs you run into.

    A new :execresult query command has been added. The generated methods will return (sql.Result, error), the same as DB.ExecContext.

    Changelog

    Core

    • Post the compiler to the new, database-agnostic SQL AST
    • Add support for dbmate migrations (#511)
    • Apply rename rules to enum constants (#523)
    • Add the :execresult query annotation (#542)

    PostgreSQL

    • Support columns from subselect statements (#489)
    • Temporary fix for typecast function parameters (#530)
    • Support functions with table parameters (#541)
    Source code(tar.gz)
    Source code(zip)
    sqlc-v1.4.0-darwin-amd64.tar.gz(15.53 MB)
    sqlc-v1.4.0-darwin-amd64.zip(15.47 MB)
    sqlc-v1.4.0-linux-amd64.tar.gz(13.61 MB)
  • v1.3.0(May 12, 2020)

    Core

    • Exclude golang-migrate .down.sql files in dirs + single files (https://github.com/kyleconroy/sqlc/pull/445)
    • Add EmitExactTableNames config option (https://github.com/kyleconroy/sqlc/pull/474)

    PostgreSQL

    • Add PostgreSQL support for TRUNCATE (https://github.com/kyleconroy/sqlc/pull/448)
    • Emit json.RawMessage for JSON columns (https://github.com/kyleconroy/sqlc/pull/461)
    • Fix panic walking CreateTableAsStmt (https://github.com/kyleconroy/sqlc/475)
    Source code(tar.gz)
    Source code(zip)
    sqlc-v1.3.0-darwin-amd64.tar.gz(7.49 MB)
    sqlc-v1.3.0-darwin-amd64.zip(7.46 MB)
    sqlc-v1.3.0-linux-amd64.tar.gz(6.76 MB)
  • v1.2.0(Apr 7, 2020)

    Core

    • Publish an official Docker container (https://github.com/kyleconroy/sqlc/pull/422)
    • Configuration values for schema and queries can now be a list of paths (https://github.com/kyleconroy/sqlc/pull/426)
    • Rename rules correctly apply to table names (https://github.com/kyleconroy/sqlc/pull/435)

    SQLite

    • Add support for ALTER TABLE DDL (https://github.com/kyleconroy/sqlc/pull/414)

    PostgreSQL

    • Generate correct type for SELECT EXISTS (https://github.com/kyleconroy/sqlc/pull/411)
    • Add support for ALTER TYPE RENAME / ADD VALUE (https://github.com/kyleconroy/sqlc/pull/433)
    • Properly quote reserved keywords when expanding SELECT * (https://github.com/kyleconroy/sqlc/pull/436)
    Source code(tar.gz)
    Source code(zip)
    sqlc-v1.2.0-darwin-amd64.tar.gz(7.46 MB)
    sqlc-v1.2.0-darwin-amd64.zip(7.43 MB)
    sqlc-v1.2.0-linux-amd64.tar.gz(6.76 MB)
  • v1.1.0(Mar 17, 2020)

    Core

    • Allow sqlc.arg('argname') form for named params (https://github.com/kyleconroy/sqlc/pull/351)
    • Apply changes to the ValuesList slice (#372)

    PostgreSQL

    • Add support for create-if-not-exists for 'create schema' (https://github.com/kyleconroy/sqlc/pull/362)
    • Add support for the ltree extension (#385)

    Go

    • Support Postgres macaddr type in Go (https://github.com/kyleconroy/sqlc/pull/358)
    • Scan enums when using pgx (#386)
    • Fix column comment (#392

    Kotlin

    • Add Query class to support timeout and cancellation (#368)
    Source code(tar.gz)
    Source code(zip)
    sqlc-v1.1.0-darwin-amd64.tar.gz(7.46 MB)
    sqlc-v1.1.0-darwin-amd64.zip(7.42 MB)
    sqlc-v1.1.0-linux-amd64.tar.gz(6.75 MB)
  • v1.0.0(Feb 18, 2020)

Type safe SQL query builder and struct mapper for Go

sq (Structured Query) ?? ?? sq is a code-generated, type safe query builder and struct mapper for Go. ?? ?? Documentation • Reference • Examples This

null 162 Jun 24, 2022
Command line tool to generate idiomatic Go code for SQL databases supporting PostgreSQL, MySQL, SQLite, Oracle, and Microsoft SQL Server

About xo xo is a command-line tool to generate Go code based on a database schema or a custom query. xo works by using database metadata and SQL intro

XO 3.1k Jun 20, 2022
Go fearless SQL. Sqlvet performs static analysis on raw SQL queries in your Go code base.

Sqlvet Sqlvet performs static analysis on raw SQL queries in your Go code base to surface potential runtime errors at build time. Feature highlights:

QP Hou 446 Jun 19, 2022
Query git repositories with SQL. Generate reports, perform status checks, analyze codebases. 🔍 📊

askgit askgit is a command-line tool for running SQL queries on git repositories. It's meant for ad-hoc querying of git repositories on disk through a

Augmentable 3k Jun 23, 2022
A Golang library for using SQL.

dotsql A Golang library for using SQL. It is not an ORM, it is not a query builder. Dotsql is a library that helps you keep sql files in one place and

Gustavo Chaín 634 Jun 15, 2022
a golang library for sql builder

Gendry gendry is a Go library that helps you operate database. Based on go-sql-driver/mysql, it provides a series of simple but useful tools to prepar

DiDi 1.4k Jun 22, 2022
Database Abstraction Layer (dbal) for Go. Support SQL builder and get result easily (now only support mysql)

godbal Database Abstraction Layer (dbal) for go (now only support mysql) Motivation I wanted a DBAL that No ORM、No Reflect、Concurrency Save, support S

徐佳军 53 Jan 23, 2022
SQL builder and query library for golang

__ _ ___ __ _ _ _ / _` |/ _ \ / _` | | | | | (_| | (_) | (_| | |_| | \__, |\___/ \__, |\__,_| |___/ |_| goqu is an expressive SQL bu

Doug Martin 1.6k Jun 24, 2022
SQL query builder for Go

GoSQL Query builder with some handy utility functions. Documentation For full documentation see the pkg.go.dev or GitBook. Examples // Open database a

Travis Harmon 23 Apr 25, 2022
A Go (golang) package that enhances the standard database/sql package by providing powerful data retrieval methods as well as DB-agnostic query building capabilities.

ozzo-dbx Summary Description Requirements Installation Supported Databases Getting Started Connecting to Database Executing Queries Binding Parameters

Ozzo Framework 551 Jun 3, 2022
Write your SQL queries in raw files with all benefits of modern IDEs, use them in an easy way inside your application with all the profit of compile time constants

About qry is a general purpose library for storing your raw database queries in .sql files with all benefits of modern IDEs, instead of strings and co

Sergey Treinis 21 Apr 25, 2022
Fast SQL query builder for Go

sqlf A fast SQL query builder for Go. sqlf statement builder provides a way to: Combine SQL statements from fragments of raw SQL and arguments that ma

Vlad Glushchuk 70 Jun 24, 2022
💥 A lightweight DSL & ORM which helps you to write SQL in Go.

sqlingo is a SQL DSL (a.k.a. SQL Builder or ORM) library in Go. It generates code from the database and lets you write SQL queries in an elegant way.

Qishuai Liu 244 Jun 13, 2022
Fluent SQL generation for golang

sqrl - fat-free version of squirrel - fluent SQL generator for Go Non thread safe fork of squirrel. The same handy fluffy helper, but with extra lette

Ivan Kirichenko 243 Jun 12, 2022
Fluent SQL generation for golang

Squirrel is "complete". Bug fixes will still be merged (slowly). Bug reports are welcome, but I will not necessarily respond to them. If another fork

null 4.9k Jun 25, 2022
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.

GraphJin - Build APIs in 5 minutes GraphJin gives you a high performance GraphQL API without you having to write any code. GraphQL is automagically co

Vikram Rangnekar 2.2k Jun 30, 2022
golang orm and sql builder

gosql gosql is a easy ORM library for Golang. Style: var userList []UserModel err := db.FetchAll(&userList, gosql.Columns("id","name"), gosql.

RushTeam 160 May 12, 2022
Analyzer: helps uncover bugs by reporting a diagnostic for mistakes of *sql.Rows usage.

sqlrows sqlrows is a static code analyzer which helps uncover bugs by reporting a diagnostic for mistakes of sql.Rows usage. Install You can get sqlro

GoStaticAnalysis 86 Mar 24, 2022
LBADD: An experimental, distributed SQL database

LBADD Let's build a distributed database. LBADD is an experimental distributed SQL database, written in Go. The goal of this project is to build a dat

Tom Arrell 379 May 22, 2022