Go to file
lqs 263f7d02a5 add test 2019-04-30 18:51:03 +08:00
sqlingo-gen allow generate only specified tables 2019-01-08 18:33:11 +08:00
.travis.yml add codecov 2019-04-30 18:28:10 +08:00
LICENSE initial commit 2018-11-18 22:11:43 +08:00
README.md Update README.md 2019-04-30 18:31:35 +08:00
case.go add support for CASE statement 2019-02-25 20:01:11 +08:00
case_test.go add test 2019-04-30 18:51:03 +08:00
common.go extract functions 2019-03-15 23:59:24 +08:00
cursor.go fix parseBool 2019-03-08 18:43:31 +08:00
database.go add interceptor 2019-04-09 19:09:16 +08:00
delete.go clean up pointers 2018-11-30 20:14:52 +08:00
expression.go fix escape 2019-04-12 12:16:53 +08:00
expression_test.go add test 2019-04-30 18:16:32 +08:00
field.go optimize 2019-04-10 21:36:47 +08:00
function.go extract functions 2019-03-15 23:59:24 +08:00
insert.go fix insert bug 2018-12-04 15:38:33 +08:00
interceptor.go add interceptor 2019-04-09 19:09:16 +08:00
logo.png initial commit 2018-11-18 22:11:43 +08:00
order.go clean up pointers 2018-11-30 20:14:52 +08:00
select.go optimize 2019-04-10 21:36:47 +08:00
table.go clean up pointers 2018-11-30 20:14:52 +08:00
transaction.go make types match 2018-11-30 20:29:03 +08:00
update.go make types match 2018-11-30 20:29:03 +08:00

README.md

Travis CI Go Report Card codecov MIT license

sqlingo is a SQL DSL library in Go. It generates code from the database and lets you write SQL easily and correctly.

Tutorial

Install and use sqlingo code generator

In order to generate code, sqlingo requires your tables are already created in the database.

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

Start using sqlingo

Create main.go to use the generated code

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)
    }
    
    // insert some rows
    customer1 := &CustomerModel{name: "Customer One"}
    customer2 := &CustomerModel{name: "Customer Two"}
    db.InsertInto(Customer).
        Values(customer1, customer2).
        Execute()

    // do some queries
    var customers []*CustomerModel
    db.SelectFrom(Customer).
        Where(Customer.Id.In(1, 2)).
        FetchAll(&customers)

    // more examples
    var customerId int64
    var orderId int64
    db.Select(Customer.Id, Order.Id).
        From(Customer, Order).
        Where(Customer.Id.Equals(Order.CustomerId), Order.Id.Equals(1)).
        FetchFirst(&customerId, &orderId)
}