Skip to content
项目

关于

a thin layer over Go's database/sql

数据

这些数字如何统计

实测星标增长

24小时
正在收集历史
7天
正在收集历史
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

DALí Logo   Go Reference

Database Abstraction Layer (í)

DALí is not exactly a database abstraction layer. It doesn't try to abstract the SQL in a way that the queries could run unchanged on any supported database. It rather abstracts just the placeholder manipulation and provides convenient ways for some common situations.

The main goal of this project is to provide a clean, compact API for communication with SQL databases.

Quickstart

package main

import (
	"fmt"
	"log"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"github.com/mibk/dali"
)

func main() {
	db, err := dali.Open("mysql", "root@/example?parseTime=true")
	if err != nil {
		log.Fatal(err)
	}

	q := db.Query(`INSERT INTO [group] ?values`, dali.Map{"name": "admins"})
	groupID, err := dali.LastInsertID(q.Exec())
	if err != nil {
		log.Fatal(err)
	}
	// INSERT INTO `group` (`name`) VALUES ('admins')

	users := []User{
		{0, "Peter", "[email protected]", groupID, time.Now()},
		{0, "Nick", "[email protected]", groupID, time.Now()},
	}
	_, err = db.Query(`INSERT INTO [user] ?values...`, users).Exec()
	if err != nil {
		log.Fatal(err)
	}
	// ?values... expands a slice of struct into multi insert
	// INSERT INTO `user` (`name`, `email`, `group_id`, `created`) VALUES
	//	('Peter', '[email protected]', 1, '2015-11-20 13:59:59'),
	//	('Nick', '[email protected]', 1, '2015-11-20 13:59:59')

	var u User
	q = db.Query(`SELECT * FROM ?ident WHERE group_id IN (?...) LIMIT 1`,
		"user", []int{1, 2, 5})
	fmt.Println(q) // dali.Query implements fmt.Stringer. It prints:
	// SELECT * FROM `user` WHERE group_id IN (1, 2, 5) LIMIT 1
	if err := q.One(&u); err != nil {
		log.Fatal(err)
	}
	fmt.Println(u)

	u.Email = "[email protected]"
	u.GroupID = 2
	_, err = db.Query(`UPDATE [user] ?set WHERE [id] = ?`,
		dali.Map{
			"email":    u.Email,
			"group_id": u.GroupID,
		}, 1).Exec()
	if err != nil {
		log.Fatal(err)
	}
	// UPDATE `user` SET `email` = '[email protected]', `group_id` = 2
	//	WHERE `id` = 1
}

type User struct {
	ID         int64     `db:",selectonly"` // omitted on INSERT, UPDATE, etc.
	Name       string    `db:"name"`
	Email      string    `db:"email"`
	GroupID    int64     `db:"group_id"`
	Registered time.Time `db:"created"`
}

Installation

$ go get github.com/mibk/dali

Checking queries with dalivet

dalivet is a go vet-style analyzer that catches dali mistakes before they reach the database. Run it on every module that uses dali:

$ go install github.com/mibk/dali/dalivet/cmd/dalivet@latest
$ go vet -vettool=$(which dalivet) ./...

It reports, among others:

  • query strings that are not constants (a string parameter passed on by a wrapper is fine),
  • placeholders whose count or argument types do not match,
  • destinations of One, All and ScanAllRows of the wrong type,
  • slices passed to ?... or ?values... with no visible length check,
  • unknown options in db struct tags, such as db:"id,readonly".

dali currently ignores an unknown tag option, so the field it was meant to change is still inserted. A future version will reject unknown options at run time; run dalivet now to find them.

A //dalivet:ignore comment silences a diagnostic on its line, or on the next line when the comment stands alone:

db.Query(`SELECT * FROM [user] WHERE [id] IN (?...)`, ids) //dalivet:ignore callers check the length

Caveats

DALí processes the query unaware of the actual SQL syntax. This means it is quite stupid on deciding whether the placeholder is inside a string literal.

conn.Query(`SELECT * FROM foo WHERE name = 'really?'`)
// This will return an error because it would try to replace the `?` with an argument
// that is missing.

To avoid this just use the whole string as a parameter.

conn.Query(`SELECT * FROM foo WHERE name = ?`, "really?")

DALí escapes values itself instead of sending them as bind parameters, so the escaping must match how the server parses string literals. It is safe for MySQL and MariaDB connections using a UTF-8 character set (utf8mb4, utf8mb3) in the default SQL mode. Avoid multi-byte character sets such as GBK, Big5 or SJIS, whose characters can contain the byte of a backslash and so swallow an escape. With NO_BACKSLASH_ESCAPES enabled the escaping stays safe, but values containing backslashes or control characters are stored mangled (\n arrives as the two characters \ and n).

Features

Identifier escaping

This feature comes from the need to fix the clumsy way of escaping identifiers in MySQL in Go's raw string literals. So instead of

sql := `SELECT `+"`where`"+`
	FROM location`

you can use

sql := `SELECT [where]
	FROM location

So there is one way to escape identifiers among all dialects.

Handy placeholders

Again, placeholder manipulation is the same for all dialects and besides that it also provides some additional placeholders. The complete list is:

?          primitive value or a value implementing driver.Valuer
?...       a slice of values which is going to be expanded (especially useful in
           IN clauses)
?values    expects either Map, or a struct as an argument. It derives column names
           from map keys or struct fields and constructs a VALUES clause (e.g.
           INSERT INTO user ?values)
?set       similar to ?values but used for SET clauses (e.g. UPDATE user SET ?set)
?values... expects a slice of structs as an argument which is expanded into multi
           INSERT clause
?ident     used for identifiers (column or table name)
?ident...  expands identifiers and separates them with a comma
?sql       inserts the parameter, a string or Marshaler, as is (meant for SQL parts)

Using the placeholders it is easy and quite expressive to write common SQL queries, but it is also possible to adjust these queries to a specific need (which is often not so easy when using query builders).

Note: only ?, ?ident, ?ident..., and ?sql are allowed in prepared statements (see the method Prepare for more information).

Profiling and other

Using the DB.SetMiddlewareFunc it is possible to do additional operations before and after execution of every query. This example logs every executed query:

var db *dali.DB // init db...

func init() {
	db.SetMiddlewareFunc(profile)
}

func profile(e dali.Execer) dali.Execer {
	return profiler{e}
}

type profiler struct {
	ex dali.Execer
}

func (p profiler) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
	log.Println(query, args)
	return p.ex.ExecContext(ctx, query, args...)
}

func (p profiler) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
	log.Println(query, args)
	return p.ex.QueryContext(ctx, query, args...)
}

func (p profiler) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row {
	log.Println(query, args)
	return p.ex.QueryRowContext(ctx, query, args...)
}

Faster performance

DALí interpolates all parameters before it gets to the database which has a huge performance benefit. This behaviour is taken from the gocraft/dbr library. See this for more information.

Supported dialects

Currently, only a MySQL dialect is implemented directly in this package (see dialect for more information). Nevertheless, supporting another dialect should be as easy as creating a new dialect implementing dialect.Escaper interface. The most common dialects will be implemented directly in the future.

Thanks

Ideas for building this library come mainly from these sources:

License

DALí is distributed under the MIT license found in the LICENSE file.

探索相关项目

加载中…