1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-01 01:20:56 +00:00
Beego/orm
BaoyangChai a0ca3d61d6 update
2019-05-08 23:11:57 +08:00
..
cmd_utils.go add field comment on create table 2018-06-06 12:33:28 +08:00
cmd.go add go simple support 2017-03-17 20:22:20 +02:00
db_alias.go Update for MySQL timezone detection bug 2017-10-26 14:32:42 +02:00
db_mysql.go Proposal to #2952 2017-11-30 18:12:49 +08:00
db_oracle.go Proposal to #2952 2017-11-30 18:12:49 +08:00
db_postgres.go Proposal to #2952 2017-11-30 18:12:49 +08:00
db_sqlite.go Proposal to #2952 2017-11-30 18:12:49 +08:00
db_tables.go orm: support filter raw sql 2018-11-05 03:47:21 +08:00
db_tidb.go Orm: Support TiDB 2015-09-17 17:04:23 +08:00
db_utils.go Add strong relationship support to orm 2017-01-11 20:16:38 +02:00
db.go update 2019-05-08 23:11:57 +08:00
models_boot.go Merge branch 'master' into master 2017-10-17 04:30:59 -05:00
models_fields.go fix orm fields SetRaw function error judge problem 2017-12-20 14:53:00 +08:00
models_info_f.go FixOrmDescrptionTag 2018-12-06 10:49:50 +08:00
models_info_m.go fix the issue #2995 2017-12-22 16:21:23 +08:00
models_test.go fix go1.10.3 orm test failed 2018-07-20 22:45:44 +08:00
models_utils.go fix format 2018-12-06 10:57:32 +08:00
models.go misc: fix typos 2017-10-17 17:27:03 +08:00
orm_conds.go orm: support filter raw sql 2018-11-05 03:47:21 +08:00
orm_log.go Update orm_log.go 2019-01-14 15:14:11 +08:00
orm_object.go orm: fix miss pk when pk is negative 2016-03-17 21:41:35 +08:00
orm_querym2m.go add go simple support 2017-03-17 20:22:20 +02:00
orm_queryset.go Merge pull request #3403 from nlimpid/develop 2018-11-20 15:44:25 +08:00
orm_raw.go add support for pointer fields of structs to method QueryRows() 2019-02-01 15:27:10 +08:00
orm_test.go little fix 2019-02-01 15:39:40 +08:00
orm.go remove the 1000-row limit for ORM result set 2019-04-27 16:58:00 +08:00
qb_mysql.go Add support "SELECT FOR UPDATE" to orm. Resolve issue #2157 2016-09-12 20:07:30 +00:00
qb_tidb.go Add support "SELECT FOR UPDATE" to orm. Resolve issue #2157 2016-09-12 20:07:30 +00:00
qb.go Add support "SELECT FOR UPDATE" to orm. Resolve issue #2157 2016-09-12 20:07:30 +00:00
README.md Fix the quick start section of the orm/README.md 2017-08-11 11:34:18 +08:00
types.go Nil check 2019-01-18 16:51:40 -08:00
utils_test.go add different column name parse strategy 2018-11-20 22:47:56 +08:00
utils.go add different column name parse strategy 2018-11-20 22:47:56 +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