Revert “reduce memory usage of building insert values” This reverts commit 0363d4a4595ca81890836c0b7286e185941deba9.
Revert “reduce memory usage of building insert values”
This reverts commit 0363d4a4595ca81890836c0b7286e185941deba9.
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.
-timeAsString
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
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() }
A lightweight DSL & ORM which helps you to write SQL in Go.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
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
-timeAsString
when generating the modelDatabase Support Status
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.
Write your application
Here’s a demonstration of some simple & advanced usage of sqlingo.