Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Gatsby Database Toolkit For Go (ORM, SQL Builder and SQLUtils)
Go Shell
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
sqlfragments
sqlutils
README.md
ROADMAP.md
baserecord.go
create.go
create_test.go
db.go
delete.go
executor.go
filler.go
filler_test.go
load.go
load_test.go
query.go
query_test.go
result.go
returning.go
run-benchmarks
select.go
select_test.go
staff.go
staff_test.go
test.go
types.go
update.go

README.md

Gatsby Database Toolkit For Go

Query Builder

Gatsby Query provides a general query object to build SQL for selecting, updating, deleting, inserting records.

Currently it supports 4 mode (CRUD) to build SQL.

To Build Select Query:

import "gatsby"

staffs := gatsby.NewQuery("staffs")
staffs.Select("id", "name")
staffs.WhereFromMap( gatsby.ArgMap{
    "name": "John"
})
sql := staffs.String()
args := staffs.Args()

To Build Insert Query:

import "gatsby"
query := gatsby.NewQuery("staffs")
query.Insert(map[string]interface{} {
    "name": "John",
})
sql := query.String()

To Build Update Query:

query := gatsby.NewQuery("staffs")
query.Update(map[string]interface{} {
    "name": "John",
})
query.WhereFromMap(map[string]interface{} {
    "id": 3,
})
sql := query.String()

SQLFragments

More flexible SQL Builder by fragments.

You can append query fragments then combine them into one SQL string by joining, and you can use the generated SQL in anywhere you want to combine with your own SQL statements.

SQLFragments filters these question marks into placeholders with number format, for example, the first ? will be $1 and the second ? will be $2.

import "gatsby"
frag := gatsby.NewFragment()
frag.AppendQuery("name = ?", "John")
frag.AppendQuery("phone = ?", "John")
sql := frag.Join("OR")         // generates name = $1 AND phone = $2
args := frag.Args()

BaseRecord

The BaseRecord provides a general CRUD operations on a struct type.

To define your model with Gatsby BaseRecord:

package app
import "github.com/c9s/gatsby"

type Staff struct {
    Id        int64     `json:"id" field:"id,primary,serial"`
    Name      string    `json:"name"`
    Gender    string    `json:"gender"`
    Phone     string    `json:"phone"`
    CellPhone string    `json:"cell_phone"`
    gatsby.BaseRecord
}

Then you can do CRUD operations on the struct object:

import "github.com/c9s/gatsby"
import _ "github.com/mattn/go-sqlite3"
import "database/sql"

db, err := sql.Open("sqlite3", "./foo.db")
gatsby.SetupConnection(db, gatsby.DriverSqlite)

staff := gatsby.NewRecord(&Staff{}).(*Staff)

res := staff.Load(10)   // load the record where primary key = 10

if res.Error != nil {
    // handle error here
}
if res.IsEmpty { 
    // handle empty record here
}

staff.Name = "John"

res := staff.Update()

res := staff.Delete()   // delete the record where primary key = 10

res := staff.Create()    // create another record
if res.Error != nil {

}
Something went wrong with that request. Please try again.