1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-14 07:00:56 +00:00
Beego/pkg/orm
2020-08-20 22:25:37 +08:00
..
filter Refactor orm filter 2020-08-18 14:31:06 +00:00
hints Refactor orm filter 2020-08-18 14:31:06 +00:00
cmd_utils.go Format code 2020-07-22 23:00:06 +08:00
cmd.go Minor grammar fixes 2020-08-05 17:50:05 +01:00
db_alias_test.go Refactor orm filter 2020-08-18 14:31:06 +00:00
db_alias.go Refactor orm filter 2020-08-18 14:31:06 +00:00
db_mysql.go Refactor orm filter 2020-08-18 14:31:06 +00:00
db_oracle.go Refactor orm filter 2020-08-18 14:31:06 +00:00
db_postgres.go Refactor orm filter 2020-08-18 14:31:06 +00:00
db_sqlite.go Refactor orm filter 2020-08-18 14:31:06 +00:00
db_tables.go Refactor orm filter 2020-08-18 14:31:06 +00:00
db_tidb.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
db_utils.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
db.go Refactor orm filter 2020-08-18 14:31:06 +00:00
do_nothing_orm_test.go Fix Tracing and prometheus bug 2020-08-11 03:23:38 +00:00
do_nothing_orm.go Refactor orm filter 2020-08-18 14:31:06 +00:00
filter_orm_decorator_test.go Add more methods to Configer 2020-08-19 22:09:05 +08:00
filter_orm_decorator.go Refactor orm filter 2020-08-18 14:31:06 +00:00
filter_test.go Refactor orm filter 2020-08-18 14:31:06 +00:00
filter.go Refactor orm filter 2020-08-18 14:31:06 +00:00
invocation.go Refactor orm filter 2020-08-18 14:31:06 +00:00
model_utils_test.go Fix Tracing and prometheus bug 2020-08-11 03:23:38 +00:00
models_boot.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
models_fields.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
models_info_f.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
models_info_m.go add tag interfaces and remove log.go 2020-07-28 06:28:51 +00:00
models_test.go Refactor orm filter 2020-08-18 14:31:06 +00:00
models_utils.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
models.go Orm filter support 2020-08-07 13:45:24 +00:00
orm_conds.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
orm_log.go specify index 2020-08-10 18:46:16 +08:00
orm_object.go refactor orm 2020-07-19 23:46:42 +08:00
orm_querym2m.go refactor orm 2020-07-19 23:46:42 +08:00
orm_queryset.go Refactor orm filter 2020-08-18 14:31:06 +00:00
orm_raw.go Fix issue 3886 2020-08-20 10:00:49 +08:00
orm_test.go Fix issue 3886 2020-08-20 10:00:49 +08:00
orm.go Refactor orm filter 2020-08-18 14:31:06 +00:00
qb_mysql.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
qb_tidb.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
qb.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
README.md Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
types.go Refactor orm filter 2020-08-18 14:31:06 +00:00
utils_test.go Move orm to pkg/orm 2020-07-15 10:05:11 +08:00
utils.go Move orm to pkg/orm 2020-07-15 10:05:11 +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)
	
	// create table
	orm.RunSyncdb("default", false, true)	
}

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