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 introspection queries to discover the types and relationships contained within a schema, and applying a standard set of base (or customized) Go templates against the discovered relationships.

Currently, xo can generate types for tables, enums, stored procedures, and custom SQL queries for PostgreSQL, MySQL, Oracle, Microsoft SQL Server, and SQLite3 databases.

NOTE: While the code generated by xo is production quality, it is not the goal, nor the intention for xo to be a "silver bullet," nor to completely eliminate the manual authoring of SQL / Go code.

Database Feature Support

The following is a matrix of the feature support for each database:

PostgreSQL MySQL Oracle Microsoft SQL Server SQLite
Models
Primary Keys
Foreign Keys
Indexes
Stored Procs
ENUM types
Custom types

Installation

Install goimports dependency (if not already installed):

$ go get -u golang.org/x/tools/cmd/goimports

Then, install in the usual Go way:

$ go get -u github.com/xo/xo

# install with oracle support (see notes below)
$ go get -tags oracle -u github.com/xo/xo

NOTE: Go 1.6+ is needed for installing xo from source, as it makes use of the trim template syntax in Go templates, which is not compatible with previous versions of Go. However, code generated by xo should compile with Go 1.3+.

Quickstart

The following is a quick overview of using xo on the command-line:

# change to project directory
$ cd $GOPATH/src/path/to/project

# make an output directory
$ mkdir -p models

# generate code for a postgres schema
$ xo pgsql://user:pass@host/dbname -o models

# generate code for a Microsoft SQL schema using a custom template directory (see notes below)
$ mkdir -p mssqlmodels
$ xo mysql://user:pass@host/dbname -o mssqlmodels --template-path /path/to/custom/templates

# generate code for a custom SQL query for postgres
$ xo pgsql://user:pass@host/dbname -N -M -B -T AuthorResult -o models/ << ENDSQL
SELECT
  a.name::varchar AS name,
  b.type::integer AS my_type
FROM authors a
  INNER JOIN authortypes b ON a.id = b.author_id
WHERE
  a.id = %%authorID int%%
LIMIT %%limit int%%
ENDSQL

# build generated code
$ go build ./models/
$ go build ./mssqlmodels/

# do standard go install
$ go install ./models/
$ go install ./mssqlmodels/

Command Line Options

The following are xo's command-line arguments and options:

$ xo --help
usage: xo [--verbose] [--schema SCHEMA] [--out OUT] [--append] [--suffix SUFFIX] [--single-file] [--package PACKAGE] [--custom-type-package CUSTOM-TYPE-PACKAGE] [--int32-type INT32-TYPE] [--uint32-type UINT32-TYPE] [--ignore-fields IGNORE-FIELDS] [--fk-mode FK-MODE] [--use-index-names] [--use-reversed-enum-const-names] [--query-mode] [--query QUERY] [--query-type QUERY-TYPE] [--query-func QUERY-FUNC] [--query-only-one] [--query-trim] [--query-strip] [--query-interpolate] [--query-type-comment QUERY-TYPE-COMMENT] [--query-func-comment QUERY-FUNC-COMMENT] [--query-delimiter QUERY-DELIMITER] [--query-fields QUERY-FIELDS] [--escape-all] [--escape-schema] [--escape-table] [--escape-column] [--enable-postgres-oids] [--name-conflict-suffix NAME-CONFLICT-SUFFIX] [--template-path TEMPLATE-PATH] DSN

positional arguments:
  dsn                    data source name

options:
  --verbose, -v          toggle verbose
  --schema SCHEMA, -s SCHEMA
                         schema name to generate Go types for
  --out OUT, -o OUT      output path or file name
  --append, -a           append to existing files
  --suffix SUFFIX, -f SUFFIX
                         output file suffix [default: .xo.go]
  --single-file          toggle single file output
  --package PACKAGE, -p PACKAGE
                         package name used in generated Go code
  --custom-type-package CUSTOM-TYPE-PACKAGE, -C CUSTOM-TYPE-PACKAGE
                         Go package name to use for custom or unknown types
  --int32-type INT32-TYPE, -i INT32-TYPE
                         Go type to assign to integers [default: int]
  --uint32-type UINT32-TYPE, -u UINT32-TYPE
                         Go type to assign to unsigned integers [default: uint]
  --ignore-fields IGNORE-FIELDS
                         fields to exclude from the generated Go code types
  --fk-mode FK-MODE, -k FK-MODE
                         sets mode for naming foreign key funcs in generated Go code [values: <smart|parent|field|key>] [default: smart]
  --use-index-names, -j
                         use index names as defined in schema for generated Go code
  --use-reversed-enum-const-names, -R
                         use reversed enum names for generated consts in Go code
  --query-mode, -N       enable query mode
  --query QUERY, -Q QUERY
                         query to generate Go type and func from
  --query-type QUERY-TYPE, -T QUERY-TYPE
                         query's generated Go type
  --query-func QUERY-FUNC, -F QUERY-FUNC
                         query's generated Go func name
  --query-only-one, -1   toggle query's generated Go func to return only one result
  --query-trim, -M       toggle trimming of query whitespace in generated Go code
  --query-strip, -B      toggle stripping type casts from query in generated Go code
  --query-interpolate, -I
                         toggle query interpolation in generated Go code
  --query-type-comment QUERY-TYPE-COMMENT
                         comment for query's generated Go type
  --query-func-comment QUERY-FUNC-COMMENT
                         comment for query's generated Go func
  --query-delimiter QUERY-DELIMITER, -D QUERY-DELIMITER
                         delimiter for query's embedded Go parameters [default: %%]
  --query-fields QUERY-FIELDS, -Z QUERY-FIELDS
                         comma separated list of field names to scan query's results to the query's associated Go type
  --escape-all, -X       escape all names in SQL queries
  --escape-schema, -z    escape schema name in SQL queries
  --escape-table, -y     escape table names in SQL queries
  --escape-column, -x    escape column names in SQL queries
  --enable-postgres-oids
                         enable postgres oids
  --name-conflict-suffix NAME-CONFLICT-SUFFIX, -w NAME-CONFLICT-SUFFIX
                         suffix to append when a name conflicts with a Go variable [default: Val]
  --template-path TEMPLATE-PATH
                         user supplied template path
  --help, -h             display this help and exit

About Base Templates

xo provides a set of generic "base" templates for each of the supported databases, but it is understood these templates are not suitable for every organization or every schema out there. As such, you can author your own custom templates, or modify the base templates available in the xo source tree, and use those with xo by a passing a directory path via the --template-path flag.

For non-trivial schemas, custom templates are the most practical, common, and best way to use xo (see below quickstart and related example).

Custom Template Quickstart

The following is a quick overview of copying the base templates contained in the xo project's templates/ directory, editing to suit, and using with xo:

# change to working project directory
$ cd $GOPATH/src/path/to/my/project

# create a template directory
$ mkdir -p templates

# copy xo templates for postgres
$ cp "$GOPATH/src/github.com/xo/xo/templates/*" templates/

# remove xo binary data
$ rm templates/*.go

# edit base postgres templates
$ vi templates/postgres.*.tpl.go

# use with xo
$ xo pgsql://user:pass@host/db -o models --template-path templates

See the Custom Template example below for more information on adapting the base templates in the xo source tree for use within your own project.

Storing Project Templates

Ideally, the custom templates for your project/schema should be stored within your project, and used in conjunction with a build pipeline such as go generate:

# add to custom xo command to go generate:
$ tee -a gen.go << ENDGO
package mypackage

//go:generate xo pgsql://user:pass@host/db -o models --template-path templates
ENDGO

# run go generate
$ go generate

# add custom templates and gen.go to project
$ git add templates gen.go && git commit -m 'Adding custom xo templates for models'

Note that xo only needs the templates for your specific database. You can safely delete the templates for the other databases -- make sure, however, that your templates are not symlinks to another database's templates before deleting.

Template Language/Syntax

xo templates are standard Go text templates. Please see the documentation for Go's standard text/template package for information concerning the syntax, logic, and variable use within Go templates.

Template Context and File Layout

The contexts (ie, the . identifier in templates) made available to custom templates are instances of xo/internal/$TYPE (see below table on $TYPE available $TYPEs), and are defined in internal/types.go.

Each database, $DBNAME, has its own set of templates for $TYPE and are available in the templates/ directory as templates/$DBNAME.$TYPE.go.tpl:

Template File $TYPE Description
templates/$DBNAME.type.go.tpl Type Template for schema tables/views/queries
templates/$DBNAME.enum.go.tpl Enum Template for schema enum definitions
templates/$DBNAME.proc.go.tpl Proc Template for stored procedures/functions ("routines")
templates/$DBNAME.foreignkey.go.tpl ForeignKey Template for foreign keys relationships
templates/$DBNAME.index.go.tpl Index Template for schema indexes
templates/$DBNAME.querytype.go.tpl QueryType Template for a custom query's generated type
templates/$DBNAME.query.go.tpl Query Template for custom query execution
templates/xo_db.go.tpl ArgType Package level template generated once per package
templates/xo_package.go.tpl ArgType File header template generated once per file

For example, PostgreSQL has templates/postgres.foreignkey.go.tpl which defines the template used by xo for PostgreSQL's foreign keys. This template will be called once for every foreign key relationship that xo finds in a PostgreSQL schema, and each time the template will be passed a different internal.ForeignKey instance, populated fields for Name, Schema, etc., which are then available in the templates/postgres.foreignkey.go.tpl as template variables, and used similar to the following: {{ .Name }}, {{ .Schema }}, etc.

Since some of the templates are identical for the supported databases, the templates are not duplicated, but are instead symlinks in the xo source tree. For example, templates/oracle.querytype.go.tpl is a symlink to templates/postgres.querytype.go.tpl.

Template Helpers

There is a set of well defined template helpers in internal/funcs.go that can assist with writing templated Go code / SQL. Please review how the base templates/ make use of helpers, and/or see the inline documentation for the respective helper func definitions.

Packing Templates

The base xo templates are bin packed so that they are always available to the built xo binary using go-bindata (via the tpl.sh script) and need to be regenerated/included in any changeset when submitting any template changes to the xo project.

If you would like to distribute your own binary version of xo with the included templates, simply modify the templates in the xo source tree, run tpl.sh, and build as you normally would.

Alternatively, you can simply do the following:

$ go generate && go build

Examples

Example: End-to-End

Please see the booktest example for a full end-to-end example for each supported database, showcasing how to use a database schema with xo, and the resulting code generated by xo.

Additionally, please see the pokedex example for a demonstration of running xo against a large schema. Please note that this example is a work in progress, and does not yet work properly with Microsoft SQL Server and Oracle databases, and has no documentation (for now) -- however it works very similarly to the booktest end-to-end example.

Example: Ignoring Fields

Sometimes you may wish to have the database manage the values of columns instead of having them managed by code generated by xo. As such, when you need xo to ignore fields for a database schema, you can use the --ignore-fields flag. For example, a common use case is to define a table with created_at and/or modified_at timestamps, where the database is responsible for setting column values on INSERT and UPDATE, respectively.

Consider the following PostgreSQL schema where a users table has a created_at and modified_at field, where created_at has a default value of now() and where modified_at is updated by a trigger on UPDATE:

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name text NOT NULL DEFAULT '' UNIQUE,
  created_at timestamptz default now(),
  modified_at timestamptz default now(),
);

CREATE OR REPLACE FUNCTION update_modified_column() RETURNS TRIGGER AS $$
BEGIN
    NEW.modfified_at = now();
    RETURN NEW;
END;
$$ language 'plpgsql';

CREATE TRIGGER update_users_modtime BEFORE UPDATE ON users FROM EACH ROW EXECUTE PROCEDURE update_modified_column();

We can ensure that these columns are managed by PostgreSQL and not by the Go code generated with xo by passing the --ignore-fields option:

# ignore special fields
$ xo pgsql://user:pass@host/db -o models --ignore-fields created_at modified_at

Example: Custom Template -- adding a GetMostRecent lookup for all tables

Often, a schema has a common layout/pattern, such as every table having a created_at and modified_at field (as in the PostgreSQL schema in the previous example). It is then a common use-case to have a GetMostRecent lookup for each table type, retrieving the most recently modified rows for each table (up to some limit, N).

To accomplish this with xo, we will need to create our own set of custom templates, and then add a GetMostRecent lookup to the $DBTYPE.type.go.tpl template.

First, we create a copy of the base xo templates:

$ cd $GOPATH/src/path/to/project

$ mkdir -p templates

$ cp $GOPATH/src/github.com/xo/xo/templates/* templates/

We can now modify the templates to suit our specific schema, adding lookups, helpers, or anything else necessary for our schema.

To add a GetMostRecent lookup, we edit our copy of the postgres.type.go.tpl template:

$ vi templates/postgres.type.go.tpl

And add the following templated GetMostRecent func at the end of the file:

// GetMostRecent{{ .Name }} returns n most recent rows from '{{ .Schema }}.{{ .Table.TableName }}',
// ordered by "created_at" in descending order.
func GetMostRecent{{ .Name }}(db XODB, n int) ([]*{{ .Name }}, error) {
    const sqlstr = `SELECT ` +
        `{{ colnames .Fields "created_at" "modified_at" }}` +
        `FROM {{ $table }} ` +
        `ORDER BY created_at DESC LIMIT $1`

    q, err := db.Query(sqlstr, n)
    if err != nil {
        return nil, err
    }
    defer q.Close()

    // load results
    var res []*{{ .Name }}
    for q.Next() {
        {{ $short }} := {{ .Name }}{}

        // scan
        err = q.Scan({{ fieldnames .Fields (print "&" $short) }})
        if err != nil {
            return nil, err
        }

        res = append(res, &{{ $short }})
    }

    return res, nil
}

We can then use the templates in conjunction with xo to generate our "model" code:

$ xo pgsql://user:pass@localhost/dbname -o models --template-path templates/

There will now be a GetMostRecentUsers func defined in models/user.xo.go, which can be used as follows:

db, err := dburl.Open("pgsql//user:pass@localhost/dbname")
if err != nil { /* ... */ }

// retrieve 15 most recent items
mostRecentUsers, err := models.GetMostRecentUsers(db, 15)
if err != nil { /* ... */ }
for _, user := range users {
    log.Printf("got user: %+v", user)
}

Using SQL Drivers

Please note that the base xo templates do not import any SQL drivers. It is left for the user of xo's generated code to import the actual drivers. For reference, these are the expected drivers to use with the code generated by xo:

Database (driver) Package
Microsoft SQL Server (mssql) github.com/denisenkom/go-mssqldb
MySQL (mysql) github.com/go-sql-driver/mysql
Oracle (ora) gopkg.in/rana/ora.v4
PostgreSQL (postgres) github.com/lib/pq
SQLite3 (sqlite3) github.com/mattn/go-sqlite3

Additionally, please see below for usage notes on specific SQL database drivers.

MySQL (mysql)

If your schema or custom query contains table or column names that need to be escaped using any of the --escape-* options, you must pass the sql_mode=ansi option to the MySQL driver:

$ xo --escape-all 'mysql://user:pass@host/?parseTime=true&sql_mode=ansi' -o models

And when opening a database connection:

db, err := dburl.Open("mysql://user:pass@host/?parseTime=true&sql_mode=ansi")

Additionally, when working with date/time column types in MySQL, one should pass the parseTime=true option to the MySQL driver:

$ xo 'mysql://user:pass@host/dbname?parseTime=true' -o models

And when opening a database connection:

db, err := dburl.Open("mysql://user:pass@host/dbname?parseTime=true")

Oracle (ora)

Oracle support is disabled by default as the Go Oracle driver used by xo needs the Oracle instantclient libs to be installed/known by pkg-config. If you have already installed rana's Oracle driver according to the installation instructions, you can simply pass -tags oracle to go get, go install or go build to enable Oracle support:

$ go get -tags oracle -u github.com/xo/xo

Installing Oracle instantclient on Debian/Ubuntu

On Ubuntu/Debian, you may download the instantclient RPMs here.

You should then be able to do the following:

# install alien, if not already installed
$ sudo aptitude install alien

# install the instantclient RPMs
$ sudo alien -i oracle-instantclient-12.1-basic-*.rpm
$ sudo alien -i oracle-instantclient-12.1-devel-*.rpm
$ sudo alien -i oracle-instantclient-12.1-sqlplus-*.rpm

# get xo
$ go get -u github.com/xo/xo

# copy oci8.pc from xo/contrib to system pkg-config directory
$ sudo cp $GOPATH/src/github.com/xo/xo/contrib/oci8.pc /usr/lib/pkgconfig/

# install rana's ora driver
$ go get -u gopkg.in/rana/ora.v4

# assuming the above succeeded, install xo with oracle support enabled
$ go install -tags oracle github.com/xo/xo

Contrib Scripts and Oracle Docker Image

It's of note that there are additional scripts available in the contrib directory that can help when working with Oracle databases and xo.

For reference, the xo developers use the sath89/oracle-12c Docker image for testing xo's Oracle database support.

SQLite3 (sqlite3)

While not required, one should specify the loc=auto option when using xo with a SQLite3 database:

$ xo 'file:mydatabase.sqlite3?loc=auto' -o models

And when opening a database connection:

db, err := dburl.Open("file:mydatabase.sqlite3?loc=auto")

About Primary Keys

For row inserts xo determines whether the primary key is automatically generated by the DB or must be provided by the application for the table row being inserted. For example a table that has a primary key that is also a foreign key to another table, or a table that has multiple primary keys in a many-to-many link table, it is desired that the application provide the primary key(s) for the insert rather than the DB.

xo will query the schema to determine if the database provides an automatic primary key and if the table does not provide one then it will require that the application provide the primary key for the object passed to the the Insert method. Below is information on how the logic works for each database type to determine if the DB automatically provides the PK.

MySQL Auto PK Logic

  • Checks for an autoincrement row in the information_schema for the table in question.

PostgreSQL Auto PK Logic

  • Checks for a sequence that is owned by the table in question.

SQLite Auto PK Logic

  • Checks the SQL that is used to generate the table contains the AUTOINCREMENT keyword.
  • Checks that the table was created with the primary key type of INTEGER.

If either of the above conditions are satisfied then the PK is determined to be automatically provided by the DB. For the case of integer PK's when you want to override that the PK be manually provided then you can define the key type as INT instead of INTEGER, for example as in the following many-to-many link table:

  CREATE TABLE "SiteContacts" (
  "ContactId"	INT NOT NULL,
  "SiteId"	INT NOT NULL,
  PRIMARY KEY(ContactId,SiteId),
  FOREIGN KEY("ContactId") REFERENCES "Contacts" ( "ContactId" ),
  FOREIGN KEY("SiteId") REFERENCES "Sites" ( "SiteId" )
)

SQL Server Auto PK Logic

  • Checks for an identity associated with one of the columns for the table in question.

Oracle Auto PK Logic

There is currently no method provided for Oracle as there is no programmatic way to query for which sequences are associated with tables. All PK's will be assumed to be provided by the database.

About xo: Design, Origin, Philosophy, and History

xo can likely get you 99% "of the way there" on medium or large database schemas and 100% of the way there for small or trivial database schemas. In short, xo is a great launching point for developing standardized packages for standard database abstractions/relationships, and xo's most common use-case is indeed in a code generation pipeline, ala stringer.

Design

xo is NOT designed to be an ORM or to generate an ORM. Instead, xo is designed to vastly reduce the overhead/redundancy of (re-)writing types and funcs for common database queries/relationships in Go -- it is not meant to be a "silver bullet".

History

xo was originally developed while migrating a large application written in PHP to Go. The schema in use in the original app, while well designed, had become inconsistent over multiple iterations/generations, mainly due to different naming styles adopted by various developers/database admins over the preceding years. Additionally, some components had been written in different languages (Ruby, Java) and had also accumulated significant drift from the original application and accompanying schema. Simultaneously, a large amount of growth meant that the PHP/Ruby code could no longer efficiently serve the traffic volumes.

In late 2014/early 2015, a decision was made to unify and strip out certain backend services and to fully isolate the API from the original application, allowing the various components to instead speak to a common API layer instead of directly to the database, and to build that service layer in Go.

However, unraveling the old PHP/Ruby/Java code became a large headache, as the code, the database, and the API, all had significant drift -- thus, underlying function names, fields, and API methods no longer coincided with the actual database schema, and were named differently in each language. As such, after a round of standardizing names, dropping cruft, and adding a small number of relationship changes to the schema, the various codebases were fixed to match the schema changes. After that was determined to be a success, the next target was to rewrite the backend services in Go.

In order to keep a similar and consistent workflow for the developers, the previous code generator (written in PHP and Twig templates) was modified to generate Go code. Additionally, at this time, but tangential to the story, the API definitions were ported from JSON to Protobuf to make use of its code generation abilities as well.

xo is the open source version of that code generation tool, and is the fruits of those development efforts. It is hoped that others will be able to use and expand xo to support other databases -- SQL or otherwise -- and that xo can become a common tool in any Go developer's toolbox.

Goals

Part of xo's goals is to avoid writing an ORM, or an ORM-like in Go, and to instead generate static, type-safe, fast, and idiomatic Go code across languages and databases. Additionally, the xo developers are of the opinion that relational databases should have proper, well-designed relationships and all the related definitions should reside within the database schema itself: ie, a "self-documenting" schema. xo is an end to that pursuit.

Related Projects

  • dburl - a Go package providing a standard, URL style mechanism for parsing and opening database connection URLs
  • usql - a universal command-line interface for SQL databases

Other Projects

The following projects work with similar concepts as xo:

Go Generators

Go ORM-likes

TODO

  • Completely refactor / fix code, templates, and other issues (PRIORITY #1)
  • Add (finish) stored proc support for Oracle + Microsoft SQL Server
  • Unit tests / code coverage / continuous builds for binary package releases
  • Move database introspection to separate package for reuse by other Go packages
  • Overhaul/standardize type parsing
  • Finish support for --{incl, excl}[ude] types
  • Write/publish template set for protobuf
  • Add support for generating models for other languages
  • Finish many-to-many and link table support
  • Finish example and code for generated *Slice types (also, only generate for the databases its needed for)
  • Add example for many-to-many relationships and link tables
  • Add support for supplying a file (ie, *.sql) for query generation
  • Add support for full text types (tsvector, tsquery on PostgreSQL)
  • Finish COMMENT support for PostgreSQL/MySQL and update templates accordingly.
  • Add support for JSON types (json, jsonb on PostgreSQL, json on MySQL)
  • Add support for GIN index queries (PostgreSQL)
Owner
XO
Various Cross-Platform language, database, and platform tools
XO
Comments
  • README: fieldnames template function is mentioned in example but it does not exist anymore

    README: fieldnames template function is mentioned in example but it does not exist anymore

    In https://github.com/xo/xo#example-custom-template----adding-a-getmostrecent-lookup-for-all-tables-go there is a line

            if err := rows.Scan({{ fieldnames $type.Fields (print "&" $short) }}); err != nil {
    

    the fieldnames template function is commented out and can't be used though.

  • postgres TEXT[] type mapped to []sql.NullString, insert fails

    postgres TEXT[] type mapped to []sql.NullString, insert fails

    I have a field in my postgreSQL table that is of type TEXT[]. Xo generates a model for it where the field is of type []sql.NullString.

    When I do an insert operation on the model, I get the following error: sql: converting Exec argument $5 type: unsupported type []sql.NullString, a slice of struct

    It doesn't make sense to have a string array that has NULL elements. If the type pq.StringArrayis used instead for the field, it works.

  • Fails to generate <table>ByID methods for some tables

    Fails to generate ByID methods for some tables

    Let me start by saying that I'm thrilled with the work being done on this utility. I see XO as being an integral part of bootstrapping web applications written in GO, and I'd love to see how it continues to grow. Unfortunately I'm having a problem. I've got a schema for MySQL 5.5.46 (which I've attached), which had fallen victim to Issue #3 I went through and removed indexes until XO was able to run completely. The generated code had a couple issues. First, some of the (table)ByID methods were defined like this:

    func ArtistByID(db XODB, iD int64, iD int64, iD int, iD int64, iD int64, iD int64, iD int64, iD int64)(*Artist, error) {...}
    

    There were also a couple of tables which didn't have a (table)ByID method generated.

    I haven't really dug into this, I will when I have a chance tomorrow. db_init.txt

  • error: Error 1242: Subquery returns more than 1 row

    error: Error 1242: Subquery returns more than 1 row

    I get the error described in the header when I try to run xo against my MySQL schema. Is there any flags/env vars to print debug info, e.g. so I could finding out which query that crashes it?

    I found: http://stackoverflow.com/questions/12597620/1242-subquery-returns-more-than-1-row-mysql which might be related to the issue.

  • [Wish] Support REFERENCES through ALTER TABLE please

    [Wish] Support REFERENCES through ALTER TABLE please

    There are two ways REFERENCES are defined, one is within CREATE TABLE itself, the other is after CREATE TABLE, via ALTER TABLE.

    I'm wishing REFERENCES through ALTER TABLE will be supported some time, as both the MS-Sql and the db schema designing tools that I'm using are doing it this way.

    Here is an example:

    example
    CREATE TABLE "Customer" (
        "CustomerID" int   NOT NULL,
        "Name" string   NOT NULL,
        "Address1" string   NOT NULL,
        "Address2" string   NULL,
        "Address3" string   NULL,
        CONSTRAINT "pk_Customer" PRIMARY KEY (
            "CustomerID"
         )
    );
    
    CREATE TABLE "Order" (
        "OrderID" int   NOT NULL,
        "CustomerID" int   NOT NULL,
        "TotalAmount" money   NOT NULL,
        "OrderStatusID" int   NOT NULL,
        CONSTRAINT "pk_Order" PRIMARY KEY (
            "OrderID"
         )
    );
    
    CREATE TABLE "OrderLine" (
        "OrderLineID" int   NOT NULL,
        "OrderID" int   NOT NULL,
        "ProductID" int   NOT NULL,
        "Quantity" int   NOT NULL,
        CONSTRAINT "pk_OrderLine" PRIMARY KEY (
            "OrderLineID"
         )
    );
    
    -- Table documentation comment 1 (try the PDF/RTF export)
    -- Table documentation comment 2
    CREATE TABLE "Product" (
        "ProductID" int   NOT NULL,
        -- Field documentation comment 1
        -- Field documentation comment 2
        -- Field documentation comment 3
        "Name" varchar(200)   NOT NULL,
        "Price" money   NOT NULL,
        CONSTRAINT "pk_Product" PRIMARY KEY (
            "ProductID"
         ),
        CONSTRAINT "uc_Product_Name" UNIQUE (
            "Name"
        )
    );
    
    CREATE TABLE "OrderStatus" (
        "OrderStatusID" int   NOT NULL,
        "Name" string   NOT NULL,
        CONSTRAINT "pk_OrderStatus" PRIMARY KEY (
            "OrderStatusID"
         ),
        CONSTRAINT "uc_OrderStatus_Name" UNIQUE (
            "Name"
        )
    );
    
    ALTER TABLE "Order" ADD CONSTRAINT "fk_Order_CustomerID" FOREIGN KEY("CustomerID")
    REFERENCES "Customer" ("CustomerID");
    
    ALTER TABLE "Order" ADD CONSTRAINT "fk_Order_OrderStatusID" FOREIGN KEY("OrderStatusID")
    REFERENCES "OrderStatus" ("OrderStatusID");
    
    ALTER TABLE "OrderLine" ADD CONSTRAINT "fk_OrderLine_OrderID" FOREIGN KEY("OrderID")
    REFERENCES "Order" ("OrderID");
    
    ALTER TABLE "OrderLine" ADD CONSTRAINT "fk_OrderLine_ProductID" FOREIGN KEY("ProductID")
    REFERENCES "Product" ("ProductID");
    
    CREATE INDEX "idx_Customer_Name"
    ON "Customer" ("Name");
    

    The generated .dot digraph will be missing the relations between them, which is supposed to be:

    image

  • Handle multiple primary keys colums in sqlite.

    Handle multiple primary keys colums in sqlite.

    Handle specially the fact that the pk column can have values larger than 1 for multiple PK's and should be scanned as an integer rather than a bool.

    Fixes #42

    Actually I see that a previous PR was addressing this same specific issue in Sqlite. While it doesn't address the more general problem of multiple it does prevent the program from exiting in Sqlite and paves the way for it to at least work when the other problem is fixed.

  • error: sql: Scan error on column index 0: unsupported driver -> Scan pair: <nil> -> *string

    error: sql: Scan error on column index 0: unsupported driver -> Scan pair: -> *string

    Using MSSQL Driver from https://github.com/denisenkom/go-mssqldb.git

    Getting this trying this from CLI.

    xo mssql://user:pass@host:1433/db -o models -N -M -B -T Match << ENDSQL select * from match ; ENDSQL

    error: sql: Scan error on column index 0: unsupported driver -> Scan pair: -> *string

    Is there another driver I should be using?

    Thanks.

  • errors when a column name matches a go reserved keyword

    errors when a column name matches a go reserved keyword

    If your database table has a field named "type" or some other go reserved keyword and it's part of some index, then xo will name some local variables "type" which collides with the reserved keyword.

    Some examples:

    // ProductsByParentIDType retrieves a row from 'phx.products' as a Product.
    //
    // Generated from index 'products_parent_id_type'.
    func ProductsByParentIDType(db XODB, parentID sql.NullInt64, type sql.NullString) ([]*Product, error) {
    
    // ProductsBySkuIDType retrieves a row from 'phx.products' as a Product.
    //
    // Generated from index 'ku_id_type'.
    func ProductsBySkuIDType(db XODB, sku sql.NullString, id int, type sql.NullString) ([]*Product, error) {
    

    Renaming the variable from "type" to some other name ("t", for example) solves the problem.

  • Escape names in MySQL using backtick.

    Escape names in MySQL using backtick.

    The default identifier quote character for MySQL is the backtick (`) https://dev.mysql.com/doc/refman/5.7/en/identifiers.html instead of the double quote (") used in Postgres https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html

    Add a MyEscape function that uses backticks, tests and update the mysql template files to use double quotes for strings instead of backticks.

  • documenting with graph

    documenting with graph

    the xo developers are of the opinion that relational databases should have proper, well-designed relationships and all the related definitions should reside within the database schema itself -- call it "self-documenting" schema. xo is an end to that pursuit

    Totally agree. However, I hope thing will not ends there, but bring the documenting to the next level -- using E-R diagrams. Since xo has already had a clear view of the table, PK, FK and their relationships, dumping out such relationships should not be too difficult.

    And I hope the Dot format from GraphViz is one of the output format. It can be more beautiful than people normally think -- check out https://github.com/BurntSushi/erd for example.

    thanks

  • Single Field Tables Fail

    Single Field Tables Fail

    I know this is nitpicking because it makes no sense to have a table with nothing in it but a primary key, but I ran into this odd bug during some testing:

    screen shot 2017-04-09 at 10 53 48 am

    screen shot 2017-04-09 at 10 51 17 am

    screen_shot_2017-04-09_at_10_51_06_am

    Obviously the workaround is to make sure one includes at least one meaningful field besides the primary key or any that are being ignored. I don't know if this could even be considered a bug other than the fact that it is conceivable to have tables with nothing but primary keys that link together others.

    Thanks again for this great tool.

  • Procedure code generation may have issues

    Procedure code generation may have issues

    first thanks for the wonderful library. I got an issue with the code generated for a procedure.

    xo generated following code for my procedure. My DB is PostgreSQL 14

    Procedure

    create function update_updated_at_column() returns trigger
        language plpgsql
    as
    $$
    BEGIN
        NEW.updated_at= now();
    RETURN NEW;
    END;
    $$;
    
    alter function update_updated_at_column() owner to abhishek;
    
    
    

    Generated go code:

    package xodb
    
    // Code generated by xo. DO NOT EDIT.
    
    import (
    	"context"
    )
    
    // UpdateUpdatedAtColumn calls the stored function 'public.update_updated_at_column() trigger' on db.
    func UpdateUpdatedAtColumn(ctx context.Context, db DB) (Trigger, error) {
    	// call public.update_updated_at_column
    	const sqlstr = `SELECT * FROM public.update_updated_at_column()`
    	// run
    	var r0 Trigger
    	logf(sqlstr)
    	if err := db.QueryRowContext(ctx, sqlstr).Scan(&r0); err != nil {
    		return Trigger{}, logerror(err)
    	}
    	return r0, nil
    }
    

    If you see the Trigger structure is not generated and the code fails to compile. I think it is a bug in code generation but can not figure out in the templates. Please help.

    Thanks & Regards, Abhi

  • #374 provide a comment of the column into structure used in Templates

    #374 provide a comment of the column into structure used in Templates

    Hello, here's my attempt to implement the issue described there: https://github.com/xo/xo/issues/374

    I've made changes only for PostgreSQL and MySQL

    Short list of the changes:

    1. gen.sh - enrich SQL query to get table's column's comment from pg_description table
    2. types/types.go - added Comment field to the Field struct
    3. cmd/schema.go - passed new value from query result to xo.Field
    4. models/column.xo.go - generated stuff
    5. templates/go/go.go - changes in the standard templates
  • [Bug] array type is not generated correctly for integer array in case of postgres

    [Bug] array type is not generated correctly for integer array in case of postgres

    For postgres table that contains integer array type e.g. "related-number" in example given below

    CREATE TABLE IF NOT EXISTS test( id BIGSERIAL PRIMARY KEY, number integer, title text, label_name text[] not null default '{}', related_number int[] not null default '{}', );

    Corresponding golang struct will generate pq.GenericArray type for related_number this will fail while scanning if related_number field is empy(not to be confused with null)

    Right mapping should be "pq.Int32Array" This is happening because pqArrMapping in postgres.go is incorrect

    This can be fixed either in xo by correcting pqArrMapping or by overriding go.go teamplate like this

    // custom override loader.PostgresGoType var pqArrMapping = map[string]string{ "bool": "pq.BoolArray", "[]byte": "pq.ByteArray", "float64": "pq.Float64Array", "float32": "pq.Float32Array", "int64": "pq.Int64Array", "int32": "pq.Int32Array", "string": "pq.StringArray", "int": "pq.Int32Array", // default: "pq.GenericArray" }

    // PQPostgresGoType parses a type into a Go type based on the databate type definition. // // For array types, it returns the equivalent as defined in github.com/lib/pq. func PQPostgresGoType(d xo.Type, schema, itype, _ string) (string, string, error) { goType, zero, err := loader.PostgresGoType(d, schema, itype) if err != nil { return "", "", err } if d.IsArray { arrType, ok := pqArrMapping[goType] goType, zero = "pq.GenericArray", "pg.GenericArray{}" // is of type struct { A any }; can't be nil if ok { goType, zero = arrType, "nil" } } return goType, zero, nil }

  • Could you add the Comment of each Field of migration table to generated xo files ?

    Could you add the Comment of each Field of migration table to generated xo files ?

    Could you add the Comment of each Field of migration table to generated xo files?

    I appreciate your valuable efforts.

    I am not good at writing English and sorry if my English is wrong. If you couldn't understand what I mean, please let me know.

    I would like to talk about this file: https://github.com/xo/xo/blob/master/templates/go/go.go

    The Field structure has Comment like bellow, but the Comment is always empty.

    // Field is a field template.
    type Field struct {
    	GoName     string
    	SQLName    string
    	Type       string
    	Zero       string
    	IsPrimary  bool
    	IsSequence bool
    	Comment    string
    }
    

    I guess this is caused by method named "convertField" (bellow). As you can see, returned Field's Comment is empty because xo.Field structure doesn't have Comment. (if it had Comment, "Comment: f.Comment" could be added to the returned Field)

    func convertField(ctx context.Context, tf transformFunc, f xo.Field) (Field, error) {
    	typ, zero, err := goType(ctx, f.Type)
    	if err != nil {
    		return Field{}, err
    	}
    	return Field{
    		Type:       typ,
    		GoName:     tf(f.Name),
    		SQLName:    f.Name,
    		Zero:       zero,
    		IsPrimary:  f.IsPrimary,
    		IsSequence: f.IsSequence,
    	}, nil
    }
    
  • xo schema scan is failing.

    xo schema scan is failing.

    idm · master± ⟩ xo schema "mysql://[email protected]/users_dev?charset=utf8&parseTime=True&loc=Local" -o models/

    error: sql: Scan error on column index 5, name "proc_def": converting NULL to string is unsupported

    This is from mysql:8 . How to mitigate this?

  • How to generate postgres functions under schema other than public

    How to generate postgres functions under schema other than public

    I am using following command to generate go code

    xo schema -s pg://postgres:postgres@localhost:5432 -o ./

    This generates go code for all the tables but it does not generates any functions is there some additional option that needs to be given for generating functions when using non public schema?

  • 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

    Nov 17, 2022
    Bulk query SQLite database over the network

    SQLiteQueryServer Bulk query SQLite database over the network. Way faster than SQLiteProxy!

    May 20, 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

    Jan 5, 2023
    Generate type safe Go from SQL

    sqlc: A SQL Compiler sqlc generates type-safe code from SQL. Here's how it works: You write queries in SQL.

    Dec 31, 2022
    Type safe SQL builder with code generation and automatic query result data mapping
    Type safe SQL builder with code generation and automatic query result data mapping

    Jet Jet is a complete solution for efficient and high performance database access, consisting of type-safe SQL builder with code generation and automa

    Jan 6, 2023
    Go database query builder library for PostgreSQL

    buildsqlx Go Database query builder library Installation Selects, Ordering, Limit & Offset GroupBy / Having Where, AndWhere, OrWhere clauses WhereIn /

    Dec 23, 2022
    igor is an abstraction layer for PostgreSQL with a gorm like syntax.

    igor igor is an abstraction layer for PostgreSQL, written in Go. Igor syntax is (almost) compatible with GORM. When to use igor You should use igor wh

    Jan 1, 2023
    An early PostgreSQL implementation in Go

    gosql An early PostgreSQL implementation in Go. Example $ git clone [email protected]:eatonphil/gosql $ cd gosql $ go run cmd/main.go Welcome to gosql. #

    Dec 27, 2022
    sqlc implements a Dynamic Query Builder for SQLC and more specifically MySQL queries.

    sqlc-go-builder sqlc implements a Dynamic Query Builder for SQLC and more specifically MySQL queries. It implements a parser using vitess-go-sqlparser

    May 9, 2023
    Generate a Go ORM tailored to your database schema.
    Generate a Go ORM tailored to your database schema.

    SQLBoiler is a tool to generate a Go ORM tailored to your database schema. It is a "database-first" ORM as opposed to "code-first" (like gorm/gorp). T

    Jan 9, 2023
    SQL builder and query library for golang

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

    Dec 30, 2022
    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

    Dec 19, 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.

    Dec 22, 2022
    Additions to Go's database/sql for super fast performance and convenience. (fork of gocraft/dbr)

    dbr (fork of gocraft/dbr) provides additions to Go's database/sql for super fast performance and convenience. Getting Started // create a connection (

    Dec 31, 2022
    This is a SQL Finder, Admin panel finder, and http analyzer that goes basses off requests. Made from 100% golang

    This is a SQL Finder, Admin panel finder that goes basses off requests. Made from 100% golang

    May 19, 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

    Dec 27, 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

    Dec 26, 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

    Dec 12, 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

    Dec 31, 2022