💥 A lightweight DSL & ORM which helps you to write SQL in Go.

Mentioned in Awesome Go go.dev Travis CI Go Report Card codecov MIT license last commit

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.

Features

  • Auto-generating DSL objects and model structs from the database so you don't need to manually keep things in sync
  • SQL DML (SELECT / INSERT / UPDATE / DELETE) with some advanced SQL query syntaxes
  • Many common errors could be detected at compile time
  • Your can use the features in your editor / IDE, such as autocompleting the fields and queries, or finding the usage of a field or a table
  • Context support
  • Transaction support
  • Interceptor support

Database Support Status

Database Status
MySQL stable
PostgreSQL experimental
SQLite experimental

Tutorial

Install and use sqlingo code generator

The first step is to generate code from the database. In order to generate code, sqlingo requires your tables are already created in the database.

$ go get -u github.com/lqs/sqlingo/sqlingo-gen-mysql
$ mkdir -p generated/sqlingo
$ sqlingo-gen-mysql root:123456@/database_name >generated/sqlingo/database_name.dsl.go

Write your application

Here's a demonstration of some simple & advanced usage of sqlingo.

package main

import (
    "github.com/lqs/sqlingo"
    . "./generated/sqlingo"
)

func main() {
    db, err := sqlingo.Open("mysql", "root:123456@/database_name")
    if err != nil {
        panic(err)
    }

    // a simple query
    var customers []*CustomerModel
    db.SelectFrom(Customer).
        Where(Customer.Id.In(1, 2)).
    	OrderBy(Customer.Name.Desc()).
        FetchAll(&customers)

    // query from multiple tables
    var customerId int64
    var orderId int64
    err = db.Select(Customer.Id, Order.Id).
        From(Customer, Order).
        Where(Customer.Id.Equals(Order.CustomerId), Order.Id.Equals(1)).
        FetchFirst(&customerId, &orderId)
    
    // subquery and count
    count, err := db.SelectFrom(Order)
        Where(Order.CustomerId.In(db.Select(Customer.Id).
            From(Customer).
            Where(Customer.Name.Equals("Customer One")))).
    	Count()
        
    // group-by with auto conversion to map
    var customerIdToOrderCount map[int64]int64
    err = db.Select(Order.CustomerId, f.Count(1)).
    	From(Order).
    	GroupBy(Order.CustomerId).
    	FetchAll(&customerIdToOrderCount)
    if err != nil {
    	println(err)
    }
    
    // insert some rows
    customer1 := &CustomerModel{name: "Customer One"}
    customer2 := &CustomerModel{name: "Customer Two"}
    _, err = db.InsertInto(Customer).
        Models(customer1, customer2).
        Execute()
    
    // insert with on-duplicate-key-update
    _, err = db.InsertInto(Customer).
    	Fields(Customer.Id, Customer.Name).
    	Values(42, "Universe").
    	OnDuplicateKeyUpdate().
    	Set(Customer.Name, Customer.Name.Concat(" 2")).
    	Execute()
}
Owner
Comments
  • 改进意见

    改进意见

    业务里有几个特殊地方,需要用到db.query,垮了几个表最终查询到数据并不是一个实际表,比如

    Table A
    akey  avalue
    
    Table B
    bkey  bvalue
    
    type Item struct {
    		Akey    string
    		AValue  string
    		BKey    string
    		BValue  string
    		Querytime time.Time
    	}
    	
    	var items []Item
    
    	rows, _ := db.Query(`select total.*, now() as querytime from (select akey, avalue,bkey, bvalue from table_a join table_b) total`)
    
    	for rows.Next() {
    		var i Item
    		rows.Scan(&i)
    		
    		items = append(items,i)
    	}
    	
    	//....
    

    这种情况很麻烦啊,又没办法用fetchAll 或者 fetchAllAsMap

    mysql sql return image

  • Null handling

    Null handling

    Trying out sqlingo this afternoon on a mysql database with lots of nulls and sqlingo complains:

    "read C:\users\ed\gostuff\sqlingotest\generated\sqlingo\jake2.dsl.go: unexpected NUL in input"

    Looks like a nice tool set. Is it not built to handle nulls?

  • I want to add a Cache-function, Currently like this, is there any other simplified way

    I want to add a Cache-function, Currently like this, is there any other simplified way

    I want to add a Cache-function, Currently like this, is there any other simplified way

    var foundCachedErr = errors.New("FoundCachedErr")
    
    	db.SetInterceptor(func(ctx context.Context, sql string, invoker sqlingo.InvokerFunc) error {
    		var v = ctx.Value("name").(string)
    
    		if cache.Instance().Exists(v) {
    			return foundCachedErr
    		}
    
    		return invoker(ctx, sql)
    	})
    
    	var member DtbMemberModel
    	var cacheName = "DtbMember:ID:1"
    
    	ctx := context.WithValue(context.Background(), "name", cacheName)
    
    	ok, err := db.SelectFrom(DtbMember).Where(
    		sqlingo.And(
    			DtbMember.Id.Equals(1),
    		),
    	).WithContext(ctx).FetchFirst(&member)
    
    	if errors.Is(err, foundCachedErr) {
    		cache.Instance().Get(cacheName)
    		// ....
    		// return
    	}
    
    	if err != nil {
    		panic(err)
    	}
    
    	// ....
    
    	spew.Dump(ok)
    	spew.Dump(member)
    
  • The ability to use this library in existing code

    The ability to use this library in existing code

    Hello. Please add the ability to use this library in existing code, without opening a connection to the database, but using the existing one.

    For example:

    var db *sql.DB // already existing connection
    sq := sqlingo.Use("mysql", db)
    

    If I understand correctly, this fix should suffice:

    func Use(driverName string, sqlDB *sql.DB) Database {
    	return &database{
    		dialect: getDialectFromDriverName(driverName),
    		db:      sqlDB,
    	}
    }
    
  • Error generated DSL

    Error generated DSL

    Errors:

    undefined: ufloat64
    

    Generated sample code:

    type SampleModel struct {
        Id             uint32
        DoubleValue    ufloat64
    }
    
    Field is not a type
    

    Generated sample code:

    type Field interface {
        Expression
        GetTable() Table
    }
    

    DB: MariaDB 10.x

  • error in generating code...

    error in generating code...

    D:\Projects\GoProjects*>sqlingo-gen-mysql root:flipped199@/lover-bind >model/user2.go Generating user panic: 4:14: expected ';', found '-'

    goroutine 1 [running]: main.main() C:/Users/Administrator/go/pkg/mod/github.com/lqs/[email protected]/sqlingo-gen-mysql/main.go:12 +0xf1

    D:\Projects\GoProjects*>

    ddl is ok.

  • BUG

    BUG

    var members []*DtbMemberModel
    
    	_, err = db.Select(DtbMember.GetFields()).From(tDtbMember).FetchAll(&members)
    
    	if err != nil {
    		panic(err)
    	}
    

    会报错 panic: Error 1241: Operand should contain 1 column(s),原因是生成的sql 带括号,导致报错,sql如下:

    SELECT (`id`, `username`, `password`, `api_token`, `remember_token`, `created_at`, `updated_at`, `organization_id`) FROM `dtb_member`
    

    使用下面的替换 select中字段可正常

    var fields []interface{}
    	
    	for _, name := range DtbMember.GetFields() {
    		fields = append(fields, name)
    	}
    
    db.Select(fields...)
    

    或者

    db.Select(DtbMember.GetFields()).From(tDtbMember) 替换成 db.SelectFrom
    
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

Mar 24, 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

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

Jan 8, 2023
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:

Dec 19, 2022
Golang Sequel ORM that support Enum, JSON, Spatial and many more
Golang Sequel ORM that support Enum, JSON, Spatial and many more

sqlike A golang SQL ORM which anti toxic query and focus on latest features. Installation go get github.com/si3nloong/sqlike Fully compatible with nat

Nov 21, 2022
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
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
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
SQL builder and query library for golang

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

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

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

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

Jan 6, 2023
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
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

Jan 4, 2023
LBADD: An experimental, distributed SQL database
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

Nov 29, 2022