Go to file
Qishuai Liu ed36ef030f
Revert "reduce memory usage of building insert values"
This reverts commit 0363d4a459.
2025-01-06 14:09:25 +09:00
.github/workflows adjust workflow versions 2024-11-19 23:16:37 +09:00
generator feat: add time range notice. (there is a sql execute err when insert using time.Time{} due out of range) 2024-02-02 16:46:11 +08:00
sqlingo-gen fix import path 2020-07-28 18:23:54 +08:00
sqlingo-gen-mysql fix import path 2020-07-28 18:21:14 +08:00
sqlingo-gen-postgres fix import path 2020-07-28 18:21:14 +08:00
sqlingo-gen-sqlite3 fix gofmt issues 2020-12-28 22:20:17 +08:00
LICENSE initial commit 2018-11-18 22:11:43 +08:00
README.md Update README.md 2023-12-28 11:02:22 +09:00
array.go fix compilation error 2023-08-31 10:19:49 +09:00
builder.go refactor boolean expression 2024-11-23 22:40:56 +09:00
builder_test.go refactor boolean expression 2024-11-23 22:40:56 +09:00
case.go add documents on all exported functions and interfaces 2020-07-07 17:07:14 +08:00
case_test.go add test 2019-04-30 18:51:03 +08:00
common.go Revert "reduce memory usage of building insert values" 2025-01-06 14:09:25 +09:00
common_test.go more tests 2020-07-01 20:53:55 +08:00
cursor.go handle MySQL zero date 2024-09-06 17:23:12 +09:00
cursor_test.go handle MySQL zero date 2024-09-06 17:23:12 +09:00
database.go fix comments 2024-09-06 11:40:08 +09:00
database_test.go change duration to time.Duration in LoggerFunc 2024-07-23 23:42:12 +09:00
delete.go refactor boolean expression 2024-11-23 22:40:56 +09:00
delete_test.go add WithContext for insert / update / delete 2024-11-15 10:24:33 +09:00
dialect.go support for multiple dialects 2020-07-01 19:54:03 +08:00
dialect_test.go more tests 2020-07-01 20:53:55 +08:00
expression.go refactor boolean expression 2024-11-23 22:40:56 +09:00
expression_test.go refactor boolean expression 2024-11-23 22:40:56 +09:00
field.go Revert "feat: transaction support powerful more" 2024-07-25 18:10:54 +09:00
field_test.go automatically find tables from fields if "From" is not specified 2020-07-28 12:43:07 +08:00
function.go add more binary operators 2023-12-28 11:01:22 +09:00
function_test.go tests 2020-06-17 16:19:54 +08:00
geometry.go add basic geometry support for mysql spatial extension and postgis 2021-08-29 23:21:02 +08:00
geometry_test.go add basic geometry support for mysql spatial extension and postgis 2021-08-29 23:21:02 +08:00
insert.go Revert "reduce memory usage of building insert values" 2025-01-06 14:09:25 +09:00
insert_test.go add WithContext for insert / update / delete 2024-11-15 10:24:33 +09:00
interceptor.go add ChainInterceptors 2024-09-06 11:40:41 +09:00
interceptor_test.go add ChainInterceptors 2024-09-06 11:40:41 +09:00
logo.png initial commit 2018-11-18 22:11:43 +08:00
order.go add documents on all exported functions and interfaces 2020-07-07 17:07:14 +08:00
order_test.go fix gofmt 2020-06-18 12:29:34 +08:00
select.go refactor boolean expression 2024-11-23 22:40:56 +09:00
select_test.go support "range over function" in Go 1.22 2024-05-30 17:33:24 +09:00
table.go Revert "feat: transaction support powerful more" 2024-07-25 18:10:54 +09:00
table_test.go automatically find tables from fields if "From" is not specified 2020-07-28 12:43:07 +08:00
transaction.go Revert "feat: transaction support powerful more" 2024-07-25 18:10:54 +09:00
transaction_test.go Revert "feat: transaction support powerful more" 2024-07-25 18:10:54 +09:00
update.go refactor boolean expression 2024-11-23 22:40:56 +09:00
update_test.go refactor boolean expression 2024-11-23 22:40:56 +09:00
utils_test.go refactor boolean expression 2024-11-23 22:40:56 +09:00
value.go fix bool value 2020-07-29 16:47:22 +08:00
value_test.go fix bool value 2020-07-29 16:47:22 +08:00

README.md

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
  • Golang time.Time is supported now, but you can still use the string type by adding -timeAsString when generating the model

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 install github.com/lqs/sqlingo/sqlingo-gen-mysql@latest
$ 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()
}