• Stars
    star
    711
  • Rank 63,679 (Top 2 %)
  • Language
    Go
  • Created over 12 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

beedb is a go ORM,support database/sql interface,pq/mysql/sqlite

Beedb

IMPORTANT: Beedb is being deprecated in favor of Beego.orm

Beedb is an ORM for Go. It lets you map Go structs to tables in a database. It's intended to be very lightweight, doing very little beyond what you really want. For example, when fetching data, instead of re-inventing a query syntax, we just delegate your query to the underlying database, so you can write the "where" clause of your SQL statements directly. This allows you to have more flexibility while giving you a convenience layer. But beedb also has some smart defaults, for those times when complex queries aren't necessary.

Right now, it interfaces with Mysql/SQLite/PostgreSQL/DB2/MS ADODB/ODBC/Oracle. The goal however is to add support for other databases in the future, including maybe MongoDb or NoSQL?

Relationship-support is not implemented, for this we will recommend you to use Beego.orm.

All in all, it's not entirely ready for advanced use yet, but it's getting there.

Drivers for Go's sql package which support database/sql includes:

Mysql:github.com/ziutek/mymysql/godrv[*]

Mysql:github.com/Go-SQL-Driver/MySQL[*]

PostgreSQL:github.com/bmizerany/pq[*]

SQLite:github.com/mattn/go-sqlite3[*]

DB2: bitbucket.org/phiggins/go-db2-cli

MS ADODB: github.com/mattn/go-adodb[*]

ODBC: bitbucket.org/miquella/mgodbc[*]

Oracle: github.com/mattn/go-oci8

Drivers marked with a [*] are tested with Beedb

API Interface

wiki/API-Interface

Installing Beedb

go get github.com/astaxie/beedb

How do we use it?

Open a database link(may be will support ConnectionPool in the future)

db, err := sql.Open("mymysql", "test/xiemengjun/123456")
if err != nil {
	panic(err)
}
orm := beedb.New(db)

with PostgreSQL,

orm := beedb.New(db, "pg")

Open Debug log, turn on the debug

beedb.OnDebug=true

Model a struct after a table in the db

type Userinfo struct {
	Uid		int	`beedb:"PK" sql:"UID" tname:"USER_INFO"` //if the table's PrimaryKey is not "Id", use this tag
	Username	string `sql:"USERNAME"`
	Departname	string `sql:"DEPARTNAME"`
	Created		time.Time `sql:"CREATED"`
}

###Caution The structs Name 'UserInfo' will turn into the table name 'USER_INFO', as defined by the tname tag. If the key 'UserName' will turn into the select colum 'USERNAME' because of the sql tag.

Create an object and save it

var saveone Userinfo
saveone.Username = "Test Add User"
saveone.Departname = "Test Add Departname"
saveone.Created = time.Now()
orm.Save(&saveone)

Saving new and existing objects

saveone.Username = "Update Username"
saveone.Departname = "Update Departname"
saveone.Created = time.Now()
orm.Save(&saveone)  //now saveone has the primarykey value it will update

Fetch a single object

var user Userinfo
orm.Where("uid=?", 27).Find(&user)

var user2 Userinfo
orm.Where(3).Find(&user2) // this is shorthand for the version above

var user3 Userinfo
orm.Where("name = ?", "john").Find(&user3) // more complex query

var user4 Userinfo
orm.Where("name = ? and age < ?", "john", 88).Find(&user4) // even more complex

Fetch multiple objects

var allusers []Userinfo
err := orm.Where("id > ?", "3").Limit(10,20).FindAll(&allusers) //Get id>3 limit 10 offset 20

var tenusers []Userinfo
err := orm.Where("id > ?", "3").Limit(10).FindAll(&tenusers) //Get id>3 limit 10  if omit offset the default is 0

var everyone []Userinfo
err := orm.FindAll(&everyone)

Find result as Map

//Original SQL Backinfo resultsSlice []map[string][]byte
//default PrimaryKey id
a, _ := orm.SetTable("userinfo").SetPK("uid").Where(2).Select("uid,username").FindMap()

Update with Map

t := make(map[string]interface{})
var j interface{}
j = "astaxie"
t["username"] = j
//update one
orm.SetTable("userinfo").SetPK("uid").Where(2).Update(t)

Update batch with Map

orm.SetTable("userinfo").Where("uid>?", 3).Update(t)

Insert data with Map

add := make(map[string]interface{})
j = "astaxie"
add["username"] = j
j = "cloud develop"
add["departname"] = j
j = "2012-12-02"
add["created"] = j
orm.SetTable("userinfo").Insert(add)

Insert batch with map

addslice := make([]map[string]interface{})
add:=make(map[string]interface{})
add2:=make(map[string]interface{})
j = "astaxie"
add["username"] = j
j = "cloud develop"
add["departname"] = j
j = "2012-12-02"
add["created"] = j
j = "astaxie2"
add2["username"] = j
j = "cloud develop2"
add2["departname"] = j
j = "2012-12-02"
add2["created"] = j
addslice =append(addslice, add, add2)
orm.SetTable("userinfo").Insert(addslice)

Join Table

a, _ := orm.SetTable("userinfo").Join("LEFT", "userdeatail", "userinfo.uid=userdeatail.uid").Where("userinfo.uid=?", 1).Select("userinfo.uid,userinfo.username,userdeatail.profile").FindMap()

Group By And Having

a, _ := orm.SetTable("userinfo").GroupBy("username").Having("username='astaxie'").FindMap()

Nesting Models (inline)

type SQLModel struct {
	Id       int       `beedb:"PK" sql:"id"`
	Created  time.Time `sql:"created"`
	Modified time.Time `sql:"modified"`
}
type User struct {
	SQLModel `sql:",inline"`
	Name     string `sql:"name" tname:"fn_group"`
	Auth     int    `sql:"auth"`
}
// the SQL table has the columns: id, name, auth, created, modified
// They are marshalled and unmarshalled automatically because of the inline keyword

LICENSE

BSD License http://creativecommons.org/licenses/BSD/

More Repositories

1

build-web-application-with-golang

A golang ebook intro how to build a web with golang
Go
43,226
star
2

go-best-practice

Trying to complete over 100 projects in various categories in golang.
Go
3,449
star
3

bat

Go implement CLI, cURL-like tool for humans
Go
2,551
star
4

gopkg

example for the go pkg's function
Go
1,968
star
5

NPWG_zh

Network programming with Go 中文翻译版本
HTML
583
star
6

go-system-programming

Go System Programming
Go
360
star
7

godoc

godoc chm pdf
262
star
8

weixin

基于beego开发的微信应用
Go
208
star
9

beeku

go libs
Go
97
star
10

session

sessionmanager is a golang session manager. It can use session for many providers
Go
68
star
11

Go-with-Unix-system

Go与Unix的底层实现
65
star
12

go-i18n

collect locale data for use in golang
Go
65
star
13

bee

Bee is a tool for managing beego framework.
Go
54
star
14

goorm

goorm is a mysql ORM tools,use mymysql
Go
47
star
15

flatmap

flatmap transform nested map to flatten map
Go
42
star
16

beegae

beego for GAE
Go
38
star
17

beepkg

an app base on beego
JavaScript
27
star
18

docs

beego's new docs
Go
27
star
19

gotraining-1

Comprehensive training material for learning the language.
Go
16
star
20

beco

beco a gateway for microservices
Go
10
star
21

docker-registry

A beego version docker-registry.
Go
6
star
22

gojob

gojob is a richly featured, open source job scheduling library that can be integrated within go application - from the smallest stand-alone application to the largest system.
6
star
23

bbchat

golang chat
4
star
24

ezjson

access the json elements easily
4
star
25

ngbee

ngbee is a tools for quickly generate cms system from github
Go
4
star
26

Web-Application-Development-with-Beego-and-Go

Web Application Development with Beego and Go
4
star
27

goimg

A light and high performance image storage and processing system written by Go
3
star
28

beecms

beecms is a cms based on beego which designed by REST API.
3
star
29

beegogae

an example base on the beego's clone for GAE
Go
2
star
30

astaxie

about me
2
star
31

yo

yo is command to generator dockerfile by golang
1
star
32

astaxie.github.io

astaxie.me
JavaScript
1
star
33

slide

all the shared slide
Go
1
star