fatedier 0f1005ff61 using glide | 7 лет назад | |
---|---|---|
.. | ||
README.md | 7 лет назад | |
cmd.go | 7 лет назад | |
cmd_utils.go | 7 лет назад | |
db.go | 7 лет назад | |
db_alias.go | 7 лет назад | |
db_mysql.go | 7 лет назад | |
db_oracle.go | 7 лет назад | |
db_postgres.go | 7 лет назад | |
db_sqlite.go | 7 лет назад | |
db_tables.go | 7 лет назад | |
db_tidb.go | 7 лет назад | |
db_utils.go | 7 лет назад | |
models.go | 7 лет назад | |
models_boot.go | 7 лет назад | |
models_fields.go | 7 лет назад | |
models_info_f.go | 7 лет назад | |
models_info_m.go | 7 лет назад | |
models_test.go | 7 лет назад | |
models_utils.go | 7 лет назад | |
orm.go | 7 лет назад | |
orm_conds.go | 7 лет назад | |
orm_log.go | 7 лет назад | |
orm_object.go | 7 лет назад | |
orm_querym2m.go | 7 лет назад | |
orm_queryset.go | 7 лет назад | |
orm_raw.go | 7 лет назад | |
orm_test.go | 7 лет назад | |
qb.go | 7 лет назад | |
qb_mysql.go | 7 лет назад | |
qb_tidb.go | 7 лет назад | |
types.go | 7 лет назад | |
utils.go | 7 лет назад | |
utils_test.go | 7 лет назад |
A powerful orm framework for go.
It is heavily influenced by Django ORM, SQLAlchemy.
Support Database:
Passed all test, but need more feedback.
Features:
more features please read the docs
Install:
go get github.com/astaxie/beego/orm
package main
import (
"fmt"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql" // import your used driver
)
// Model Struct
type User struct {
Id int `orm:"auto"`
Name string `orm:"size(100)"`
}
func init() {
// register model
orm.RegisterModel(new(User))
// set default database
orm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8", 30)
}
func main() {
o := orm.NewOrm()
user := User{Name: "slene"}
// insert
id, err := o.Insert(&user)
// update
user.Name = "astaxie"
num, err := o.Update(&user)
// read one
u := User{Id: user.Id}
err = o.Read(&u)
// delete
num, err = o.Delete(&u)
}
type Post struct {
Id int `orm:"auto"`
Title string `orm:"size(100)"`
User *User `orm:"rel(fk)"`
}
var posts []*Post
qs := o.QueryTable("post")
num, err := qs.Filter("User__Name", "slene").All(&posts)
If you don't like ORM,use Raw SQL to query / mapping without ORM setting
var maps []Params
num, err := o.Raw("SELECT id FROM user WHERE name = ?", "slene").Values(&maps)
if num > 0 {
fmt.Println(maps[0]["id"])
}
o.Begin()
...
user := User{Name: "slene"}
id, err := o.Insert(&user)
if err == nil {
o.Commit()
} else {
o.Rollback()
}
In development env, you can simple use
func main() {
orm.Debug = true
...
enable log queries.
output include all queries, such as exec / prepare / transaction.
like this:
[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.4ms] - [INSERT INTO `user` (`name`) VALUES (?)] - `slene`
...
note: not recommend use this in product env.
more details and examples in docs and test