https://github.com/viemacs/dal
https://github.com/viemacs/dal
database-access
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/viemacs/dal
- Owner: viemacs
- License: mit
- Created: 2020-02-16T12:33:24.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-03T14:10:38.000Z (over 5 years ago)
- Last Synced: 2024-06-20T14:19:26.276Z (almost 2 years ago)
- Topics: database-access
- Language: Go
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
DAL
===
DAL provides a Database Access Layer in Go for MySQL.
## Requirements
- Go 1.10 or higher
- MySQL(4.1+) or MariaDB
## Installation
This package can be installed with the go get command.
``` go
go get github.com/viemacs/dal
```
## API Reference
Examples can be found in the test file.
A model should be initialized first with connection options.
- Initialize a Model of MySQL.
``` go
model := Model{
DriverName: "mysql",
DataSourceName: "user:password@tcp(hostIP)/database",
}
```
- Write values to database.
``` go
type T struct {
ID int `field:"id"`
}
values := []T{{1}}
if err := model.Write("tableName", values); err != nil {
dealWithError(err)
}
```
- Read records from database.
``` go
if err := model.Read("tablename", []string{"id"}, "", T{}); err != nil {
dealWithError(err)
}
for _, v := range model.Records {
handleRecord(v.(T))
}
```
- Get version of database.
``` go
if info := model.DBInfo(); len(info) != 1 {
dealWithError(err)
}
```
## Features
- 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.
## Todo
1. check type of parameter `values` in Write()
2. `Read` function skips passing query fields, or match query results with fields of struct.
3. Support values of nested struct in `Read` function.
## Issue
1. `Exec` accepts normal interface, but currently values are all printed into strings.
## TBConfirm
1. sumRowsAffected: are records in database changed before tx.commit() ?
## Notice
1. 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.