DAL provides a Database Access Layer in Go for MySQL.
- Go 1.10 or higher
- MySQL(4.1+) or MariaDB
This package can be installed with the go get command.
go get github.com/viemacs/dal
Examples can be found in the test file.
A model should be initialized first with connection options.
- Initialize a Model of MySQL.
model := Model{
DriverName: "mysql",
DataSourceName: "user:password@tcp(hostIP)/database",
}
- Write values to database.
type T struct {
ID int `field:"id"`
}
values := []T{{1}}
if err := model.Write("tableName", values); err != nil {
dealWithError(err)
}
- Read records from database.
if err := model.Read("tablename", []string{"id"}, "", T{}); err != nil {
dealWithError(err)
}
for _, v := range model.Records {
handleRecord(v.(T))
}
- Get version of database.
if info := model.DBInfo(); len(info) != 1 {
dealWithError(err)
}
- supports nested structs; does not support array/slice/map as struct fields
- supports single query; does not support multiple result sets
Program with fail with panic if the connection to database cannot be established. The caller of this module can recover properly.
- check type of parameter
values
in Write() Read
function skips passing query fields, or match query results with fields of struct.- Support values of nested struct in
Read
function.
Exec
accepts normal interface, but currently values are all printed into strings.
- sumRowsAffected: are records in database changed before tx.commit() ?
- Values of nested struct with duplicated fields are not supported yet. During writing, the duplicated fields will generate duplicated fields in SQL. During reading, only the first one of the duplicated fields will be filled.