https://github.com/cuigh/gsd
Simple and fluent SQL database access framework for Go
https://github.com/cuigh/gsd
Last synced: 7 months ago
JSON representation
Simple and fluent SQL database access framework for Go
- Host: GitHub
- URL: https://github.com/cuigh/gsd
- Owner: cuigh
- License: apache-2.0
- Created: 2014-11-11T10:11:45.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-11-27T09:27:11.000Z (almost 8 years ago)
- Last Synced: 2025-01-23T10:25:52.410Z (9 months ago)
- Language: Go
- Size: 21.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# gsd
> [DEPRECATED] This package now is a part of [auxo](https://github.com/cuigh/auxo), see [auxo/gsd](https://github.com/cuigh/auxo/tree/master/db/gsd).
gsd is a Simple, fluent SQL data access framework. It supports various types of database, like mysql/mssql/sqlite etc.
## Install
To install gsd, just use `go get` command.
```
$ go get github.com/cuigh/gsd
```## Usage
gsd's API is very simular to native SQL sytax.
## Configure
For now, gsd only supports initializing databases from config file. There is a sample config file in the package(database.sql.conf):
```
```
You must set [ConfigPath] before you go to next step:```
gsd.ConfigPath = "./database.sql.conf"
```
Now you can open a database:```
db, err := Open("Test")
......
```
### INSERT```
v := gsd.InsertValues{
"ID": 10,
"NAME": "Clothes",
"COUNT": 0,
"ENTER_USER": 1,
"ENTER_TIME": time.Now(),
}
r, err := db.Insert("Category").Values(v).Result()
```
### DELETE```
f := gsd.F().Add("ID", 10)
r, err := db.Delete("Category").Where(f).Result()
```
### UPDATE```
f := gsd.F().Add("ID", 2)
v := gsd.UpdateValues{
"ENTER_TIME": gsd.UV(time.Now()),
"COUNT": gsd.UVT(gsd.UPDATE_INC, 1),
}
r, err := db.Update("Category").Set(v).Where(f).Result()
```
### SELECT```
type Category struct {
ID int32
Name string
Count int32
EnterUser int32 `gsd:"ENTER_USER"`
EnterTime time.Time `gsd:"ENTER_TIME"`
}t := gsd.T("Category")
f := gsd.F().AddT("ID", gsd.FILTER_GT, 0)
r := db.Select(t1.C("ID", "NAME", "ENTER_USER", "ENTER_TIME")).From(t).Where(f).Limit(0, 3).Rows()
objs := []*Category{}
if err := r.All(&objs); err != nil {
log.Fatal(err)
}
```
### TRANSACTION```
err := db.Transact(func(tx Transaction) error {
obj := Category{}
err := tx.Execute("SELECT ID, NAME, ENTER_TIME FROM Category WHERE ID=?", 2).Row().ScanObj(&obj)
if err != nil{
return err
}f := gsd.F().Add("ID", 3)
v := gsd.UpdateValues{
"Name": gsd.UV(obj.Name),
}
_, err = tx.Update("Category").Set(v).Where(f).Result()
return err
})
if err != nil {
log.Fatal(err)
}
```