Skip to content
SQLBrick generates golang function from your SQL statements.
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Build Status license

SQLBrick generates golang function from your SQL statements. It's not another orm library, but a tool to generate golang function and models from given SQL files. The generated source code is totally based on sqlx.

Why this

As metioned above, this is not an orm library. If you are looking for some orm library in go, this is not suitable for you. If you like to write SQL statements, but you don't want to write SQL function again and again, then this tool will help you to reduce workload.


go get -u

Add the following to your dependency if you are working with dep

required = [

  name = ""
  version = "0.1.0"


To use sqlbrick, put your SQL statements in .sql file. Typically the first statement creates a table. Each SQL file can include only one CREATE TABLE. The statement will be a little different from standard SQL statement, it uses {} as some simple syntax and ${} as placeholder. Other syntax in standard SQL can be used as usual such as comment --. Here's an example:

{define name CreateBook}
  "id"  serial NOT NULL PRIMARY KEY,
  uid int NOT NULL,
  name text NOT NULL,
  content varchar(255),
  create_time TIMESTAMP,
  price int NOT NULL
{end define}

{define name InsertOne}
INSERT INTO book (uid, name, content, create_time, price)
  VALUES (${uid}, ${name}, ${content}, ${create_time}, ${price});
{end define}

-- An example to show update price.
{define name UpdatePrice}
{if price > 0} price = ${price}, {end if}
{if content != ""} content = ${content}, {end if}
name = ${name} WHERE id = ${id};
{end define}

-- An example to show complex update.
-- Second line comment.
{define name ComplexUpdate}
UPDATE book SET price=(SELECT price FROM book, user WHERE
  WHERE book.price <= ${price} AND name = ${name};
{end define}

{define name SelectAll}
{end define}

-- An example to show SelectById.
{define name SelectById, mapper single}
SELECT * FROM book WHERE id = ${id} ORDER BY name ASC;
{end define}

{define name SelectByUid, mapper array}
SELECT * FROM book WHERE uid = ${uid} ORDER BY name ASC;
{end define}

-- An example to show DeleteById.
{define name DeleteById}
DELETE FROM book WHERE id = ${id};
{end define}

{define name TxInsert, tx true}
INSERT INTO book (uid, name, content, create_time, price)
  VALUES (${uid}, ${name}, ${content}, ${create_time}, ${price});
{end define}

{define name TxDeleteById, tx true}
DELETE FROM book WHERE id = ${id};
{end define}

Go to your work directory where contains .sql files, then run:

$ sqlbrick

Or you can specify the directory of sql files. For more usage, check

$ sqlbrick -help

You can also use SQLBrick with go generate, just add the following somewhere in your source code and then run go generate:

//go:generate sqlbrick -w your/sql/dir -o output/dir

From this sqlbrick will generate sqlbrick.go, book.go. You'll see all SQL function in book.go and Book model. At finally, you can use them in your code.

import (

func main() {
	db, err := sqlx.Connect("postgres", "postgres://user:pass@localhost/dbname?sslmode=disable")
	if err != nil {
	sqlBrick := models.NewSqlBrick(db)

	var books []Book
	err := sqlBrick.Book.SelectById(&books, "someid")
	if err != nil {
		log.Printf("error: %v", err)
	// your code here

The value in placeholder should keep the same with SQL field, or the same with your custom struct which tagged with db. For more detail, you can check the document of sqlx.



SQLBrick uses {define ...}...{end define} to define a SQL function:

{define name SelectById, mapper single}
{end define}

Definition has there parameters, name, mapper and tx, they must be split by ,.

  • The name is necessary to define the name of current SQL function.
  • The mapper is optional, default is array which means the result will map to an array. If you want to map to only one result, then use single. If you want to map to basic type, then use basicType(ps: this should be based on your query result).
  • The tx is optional, default is false. If set true, the SQL function will work in a transaction. Consuming the generated code like this:
if tx, err := sqlBrick.Begin(); err != nil {
	// your code here
} else {

For detail usage, check the examples in source code.


SQLBrick uses {if ...}...{end if} as condition to make dynamic queries.


MIT License

Copyright (C) 2018-present Anbillon Team

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
You can’t perform that action at this time.