# README

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()
}
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Functions
And creates an expression with AND operator.
Case initiates a CASE statement.
Concat creates an expression of CONCAT function.
Count creates an expression of COUNT aggregator.
DefaultLogger is sqlingo default logger, which print log to stderr and regard executing time gt 100ms as slow sql.
No description provided by the author
Function creates an expression of the call to specified function.
If creates an expression of IF function.
Length creates an expression of LENGTH function.
NewBooleanField creates a reference to a boolean field.
NewDateField creates a reference to a time.Time field.
NewNumberField creates a reference to a number field.
NewStringField creates a reference to a string field.
NewTable creates a reference to a table.
NewWellKnownBinaryField creates a reference to a geometry WKB field.
Open a database, similar to sql.Open.
Or creates an expression with OR operator.
Raw create a raw SQL statement.
No description provided by the author
No description provided by the author
Sum creates an expression of SUM aggregator.
No description provided by the author
Use an existing *sql.DB handle.
# Constants
SqlingoRuntimeVersion is the the runtime version of sqlingo.
# Structs
No description provided by the author
# Interfaces
Alias is the interface of an table/column alias.
No description provided by the author
ArrayField is the interface of a generated field of array type.
Assignment is an assignment statement.
BooleanExpression is the interface of an SQL expression with boolean value.
BooleanField is the interface of a generated field of boolean type.
CaseExpression indicates the status in a CASE statement.
CaseExpressionWithElse indicates the status in CASE ..
Cursor is the interface of a row cursor.
Database is the interface of a database with underlying sql.DB object.
No description provided by the author
No description provided by the author
Expression is the interface of an SQL expression.
Field is the interface of a generated field.
Model is the interface of generated model struct.
NumberExpression is the interface of an SQL expression with number value.
NumberField is the interface of a generated field of number type.
OrderBy indicates the ORDER BY column and the status of descending order.
Scanner is the interface that wraps the Scan method.
StringExpression is the interface of an SQL expression with string value.
StringField is the interface of a generated field of string type.
Table is the interface of a generated table.
Transaction is the interface of a transaction with underlying sql.Tx object.
UnknownExpression is the interface of an SQL expression with unknown value.
WellKnownBinaryExpression is the interface of an SQL expression with binary geometry (WKB) value.
WellKnownBinaryField is the interface of a generated field of binary geometry (WKB) type.
# Type aliases
InterceptorFunc is the function type of an interceptor.
InvokerFunc is the function type of the actual invoker.
No description provided by the author
WellKnownBinary is the type of geometry well-known binary (WKB) field.