1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-26 21:10:50 +00:00
Beego/orm
2017-04-24 21:36:07 +08:00
..
cmd_utils.go add go simple support 2017-03-17 20:22:20 +02:00
cmd.go add go simple support 2017-03-17 20:22:20 +02:00
db_alias.go add more oracle alias 2017-04-24 21:36:07 +08:00
db_mysql.go Fix #2263 2016-11-08 13:39:31 +08:00
db_oracle.go support oracle 2016-03-10 21:23:13 +08:00
db_postgres.go orm: add json & jsonb type support 2016-04-12 11:00:31 +08:00
db_sqlite.go Fix for IndexExists in SQLite driver, they added the "origin" and "partial" columns to the index_list pragma. 2017-03-28 12:38:27 +02:00
db_tables.go add go simple support 2017-03-17 20:22:20 +02: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 Fixed InsertOrUpdate bug 2017-04-21 19:57:04 +03:00
models_boot.go add go simple support 2017-03-17 20:22:20 +02:00
models_fields.go orm: fix golint 2016-04-12 11:00:31 +08:00
models_info_f.go add go simple support 2017-03-17 20:22:20 +02:00
models_info_m.go add go simple support 2017-03-17 20:22:20 +02:00
models_test.go Add strong relationship support to orm 2017-01-11 20:16:38 +02:00
models_utils.go simplfy the code 2016-08-31 22:47:31 +08:00
models.go optimize the ORM 2016-08-31 00:07:19 +08:00
orm_conds.go resolves #2291, introduces AndNotCond/OrNotCond to orm.Condition 2016-11-28 09:49:06 +01:00
orm_log.go orm log delete repetition time 2016-09-15 10:54:21 +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 Add GetCond func to querySet 2017-01-18 17:04:23 +08:00
orm_raw.go optimize the ORM 2016-08-31 00:07:19 +08:00
orm_test.go add go simple support 2017-03-17 20:22:20 +02:00
orm.go add go simple support 2017-03-17 20:22:20 +02: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 Transaction 2015-01-13 11:09:43 +08:00
types.go Add GetCond func to querySet 2017-01-18 17:04:23 +08:00
utils_test.go Modify func camelString to be more robust 2016-12-25 21:09:06 +08:00
utils.go Merge branch 'develop' into add-gosimple 2017-03-18 11:20:30 +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