1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 18:20:54 +00:00
Beego/orm
2013-09-22 19:20:40 +08:00
..
docs/zh orm all docs wiil update to beego.me 2013-08-22 18:35:35 +08:00
cmd_utils.go orm now can specify engine for mysql. add api SetMaxIdleConns/SetMaxOpenConns(go 1.2) 2013-09-16 09:50:32 +08:00
cmd.go orm Improve syncdb 2013-08-27 12:33:27 +08:00
db_alias.go orm RegisterDataBase remove default maxIdle/maxConn 2013-09-22 17:51:37 +08:00
db_mysql.go orm Improve syncdb 2013-08-27 12:33:27 +08:00
db_oracle.go orm add sqlite3 support, may be support postgres in next commit 2013-08-11 00:15:26 +08:00
db_postgres.go orm Improve syncdb 2013-08-27 12:33:27 +08:00
db_sqlite.go orm Improve syncdb 2013-08-27 12:33:27 +08:00
db_tables.go orm support use any numeric type set QuerySeter.Limit value 2013-09-13 18:06:44 +08:00
db_utils.go orm now can specify engine for mysql. add api SetMaxIdleConns/SetMaxOpenConns(go 1.2) 2013-09-16 09:50:32 +08:00
db.go fix #213 2013-09-22 19:20:40 +08:00
models_boot.go orm add uint uint32 uint64 auto increment support 2013-08-22 18:35:26 +08:00
models_fields.go orm support auto create db 2013-08-19 22:37:39 +08:00
models_info_f.go orm fix syncdb 2013-08-25 11:35:12 +08:00
models_info_m.go fix #213 2013-09-22 19:20:40 +08:00
models_test.go orm now can specify engine for mysql. add api SetMaxIdleConns/SetMaxOpenConns(go 1.2) 2013-09-16 09:50:32 +08:00
models_utils.go orm now can specify engine for mysql. add api SetMaxIdleConns/SetMaxOpenConns(go 1.2) 2013-09-16 09:50:32 +08:00
models.go orm 1. complete QueryRow/QueryRows api 2. QuerySeter.All support *[]Type and *[]*Type 2013-09-09 22:33:41 +08:00
orm_conds.go some fix / add test 2013-08-07 19:11:44 +08:00
orm_log.go orm add postgres support 2013-08-11 22:27:45 +08:00
orm_object.go orm fix when use uint as pk 2013-08-30 12:32:05 +08:00
orm_queryset.go orm support use any numeric type set QuerySeter.Limit value 2013-09-13 18:06:44 +08:00
orm_raw.go orm 1. complete QueryRow/QueryRows api 2. QuerySeter.All support *[]Type and *[]*Type 2013-09-09 22:33:41 +08:00
orm_test.go fix #213 2013-09-22 19:20:40 +08:00
orm.go orm.Read support specify condition fields, orm.Update and QuerySeter All/One support omit fields. 2013-09-12 19:04:39 +08:00
README.md orm all docs wiil update to beego.me 2013-08-22 18:35:35 +08:00
types.go orm support use any numeric type set QuerySeter.Limit value 2013-09-13 18:06:44 +08:00
utils.go orm support use any numeric type set QuerySeter.Limit value 2013-09-13 18:06:44 +08:00

beego orm

Build Status

A powerful orm framework for go.

It is heavily influenced by Django ORM, SQLAlchemy.

now, beta, unstable, may be changing some api make your app build failed.

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

TODO

  • some unrealized api
  • examples
  • docs