1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-20 17:20:17 +00:00
Files
cache
config
context
grace
httplib
logs
migration
orm
README.md
cmd.go
cmd_utils.go
db.go
db_alias.go
db_mysql.go
db_oracle.go
db_postgres.go
db_sqlite.go
db_tables.go
db_tidb.go
db_utils.go
models.go
models_boot.go
models_fields.go
models_info_f.go
models_info_m.go
models_test.go
models_utils.go
orm.go
orm_conds.go
orm_log.go
orm_object.go
orm_querym2m.go
orm_queryset.go
orm_raw.go
orm_test.go
qb.go
qb_mysql.go
qb_tidb.go
types.go
utils.go
plugins
session
swagger
testing
toolbox
utils
validation
.gitignore
.travis.yml
CONTRIBUTING.md
LICENSE
README.md
admin.go
adminui.go
app.go
beego.go
config.go
config_test.go
controller.go
controller_test.go
doc.go
docs.go
error.go
filter.go
filter_test.go
flash.go
flash_test.go
hooks.go
log.go
mime.go
namespace.go
namespace_test.go
parser.go
router.go
router_test.go
staticfile.go
staticfile_test.go
template.go
template_test.go
templatefunc.go
templatefunc_test.go
tree.go
tree_test.go
Beego/orm
astaxie 441f795a1a Merge pull request from thanhtranjs/develop
Add GroupBy to QuerySeter
2016-02-02 12:44:49 +08:00
..
2016-01-15 08:43:02 +08:00
2015-09-12 21:46:43 +08:00
2015-09-17 23:47:26 +08:00
2015-09-12 21:46:43 +08:00
2014-08-18 16:41:43 +08:00
2015-09-12 21:46:43 +08:00
2015-09-12 21:46:43 +08:00
2015-09-17 17:04:23 +08:00
2015-09-12 21:46:43 +08:00
2015-09-28 14:07:35 +02:00
2015-09-12 21:46:43 +08:00
2015-09-12 21:46:43 +08:00
2016-01-17 18:35:11 +08:00
2015-09-12 21:46:43 +08:00
2016-01-17 23:48:17 +08:00
2015-09-12 21:46:43 +08:00
2014-08-18 16:41:43 +08:00
2016-01-18 00:18:21 +08:00
2015-09-12 21:46:43 +08:00
2015-09-12 21:46:43 +08:00
2016-01-15 14:36:45 +08:00
2015-09-12 21:46:43 +08:00
2016-01-17 23:48:17 +08:00
2015-09-17 23:00:05 +08:00
2015-09-17 17:04:23 +08:00
2015-01-13 11:09:43 +08:00
2015-09-17 23:47:26 +08:00

beego orm

Build Status

A powerful orm framework for go.

It is heavily influenced by Django ORM, SQLAlchemy.

Support Database:

Passed all test, but need more feedback.

Features:

  • full go type support
  • easy for usage, simple CRUD operation
  • auto join with relation table
  • cross DataBase compatible query
  • Raw SQL query / mapper without orm model
  • full test keep stable and strong

more features please read the docs

Install:

go get github.com/astaxie/beego/orm

Changelog

  • 2013-08-19: support table auto create
  • 2013-08-13: update test for database types
  • 2013-08-13: go type support, such as int8, uint8, byte, rune
  • 2013-08-13: date / datetime timezone support very well

Quick Start

Simple Usage

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)	
}

Next with relation

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)

Use Raw sql

If you don't like ORMuse 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"])
}

Transaction

o.Begin()
...
user := User{Name: "slene"}
id, err := o.Insert(&user)
if err == nil {
	o.Commit()
} else {
	o.Rollback()
}

Debug Log Queries

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.

Docs

more details and examples in docs and test

documents