README
Database Kit in golang.
This package contains following sub packages:
- cue ... query builder
- ef ... execution facade
- dialect ... DBMS dialects
How to use?
go get
go get bitbucket.org/shu/dk
cue -- query builder
import "bitbucket.org/shu/dk/cue"
sel := cue.Select().From("users").Where(cue.Eq("id", 100))
query, args := sel.BuildSQL()
// db.QueryRow(query, args...).Scan(...)
ef -- execution facade
import "bitbucket.org/shu/dk/ef"
import "bitbucket.org/shu/dk/dialect"
type User struct {
ID int
UserName string `db:"name"`
Age int `db:"-"`
}
var u User
// query, args := cue.Select().From("users").Where(cue.Eq("id", 100)).Dialect(dialect.MySQL{}).BuildSQL()
query, args := "SELECT * FROM users WHERE id = ?", []interface{}[100]
if err := ef.FetchOne(db, &u, query, args...); err == nil {
// u.ID == 100, u.UserName == "name100", u.Age == 0
}
Fetch family:
- ef.FetchOne
- fetchies into struct, map[string]interface{}, interface{} (for unknown type value) and fetchable type.
- ef.FetchAll
- at once, fetchies into []struct, []map[string]interface{}.
- ef.FetchEach
- rows.Next, rows.Scan loop method.
- Struct, map[string]interface{}, interface{} (for unknown type value) and fetchable types are supported.
Exec family:
- ef.Exec
- ef.Insert
- ef.Update
- needs struct that has db-tagged fields (pk).
- ef.Create
- needs struct that has db-tagged fields.
`db` tag
You can specify columns related to fields in db tag.
Outlook:
type Table1 struct {
ID int `db:"id; type=serial; pk"`
Name string `db:"name; type=varchar; size=10; default='nanashi'"`
Age int `db:"age; type=integer; null"`
Hoge int `db:"-"`
}
In `db` tag, each element is separated by ; (semicolon).
First element is the column name. If this is - (hyphen), this field has no relationship to any columns.
The latter elements are:
- **type = **VALUE
- **size = **VALUE
- length, precision and scale expression in each DBMS notation
- **default = **VALUE
- default value in raw expression ('aaa' for character type, 10 for integer type)
- pk or primarykey
- if it is one of primary keys
- null
- NULL-able, otherwise, NOT NULL is applied
- serial or auto_increment
- (or type:serial-family)
- excluded from ef.Insert
What is this repository for?
- Simple
- Easy to use
- Speedy