dbmodel
Description
dbmodel is simple database modeling.
dbmodel does not import database driver in production code. You must import database driver in your code.
Databases
- PostgreSQL: higher 8.4
- MySQL: not supported yet
- Oracle: not supported yet
- SQL Server: not supported yet
Usage
Install and use in your code.
Example (PostgreSQL):
package main
import (
"fmt"
"github.com/pinzolo/dbmodel"
)
func main() {
// Create DataSouce
ds := dbmodel.NewDataSource("postgres", "9.4", "localhost", 5432, "postgres", "", "sample", map[string]string{"sslmode": "disable"})
// Create Client
client := dbmodel.NewClient(ds)
// Connect to Database.
client.Connect()
// You must close connection.
defer client.Disconnect()
// AllTables returns all table in sample schema.
// dbmodel.RequireAll is built in option.
// When dbmodel.RequireAll is given, client loads all metadata of table.(columns, indices, constraints, foreign keys, referenced keys)
tables, err := client.AllTables("sample", dbmodel.RequireAll)
if err != nil {
fmt.Println(err)
return
}
for _, tbl := range tables {
for _, col := range tbl.Columns() {
fmt.Printf("%#v", col)
}
for _, idx := range tbl.Indices() {
fmt.Printf("%#v", idx)
}
for _, cns := range tbl.Constraints() {
fmt.Printf("%#v", cns)
}
for _, fk := range tbl.ForeignKeys() {
fmt.Printf("%#v", fk)
}
for _, rk := range tbl.ReferencedKeys() {
fmt.Printf("%#v", rk)
}
}
// You can load single table users.
// You need only columns, you can use dbmodel.RequireNone
table, err := client.Table("sample", "users", dbmodel.RequireNone)
fmt.Printf("%#v", table)
if err != nil {
fmt.Println(err)
return
}
// You can load table names.
// Returned *dbmodel.Table contains only table name and comment.
// As well as using client.TableNames("sample", "users"), you can get tables that contains "users" in its name.
tables, err = client.AllTableNames("sample")
if err != nil {
fmt.Println(err)
return
}
}
Install
To install, use go get
:
$ go get github.com/pinzolo/dbmodel
Contribution
- Fork (https://github.com/pinzolo/dbmodel/fork)
- Create a feature branch
- Commit your changes
- Rebase your local changes against the master branch
- Run test suite with the
go test ./...
command and confirm that it passes - Run
gofmt -s
- Create a new Pull Request