Fix typo in 05.1.md and tidy up grammar in 05.5.md
This commit is contained in:
@@ -36,7 +36,7 @@ We always see the following code when we use third-party drivers:
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
Here the underscore `_` can be quite confusing for many beginners, but this is a great feature in Go. We already know that this identifier is for discarding values from function returns, and also that you must use all packages that you've imported in your code in Go. So when the underscore is used with import, it means that you need to execute the init() function of that package without directly using it, which exactly fits the use-case for registering database drivers.
|
||||
Here the underscore (also known as a 'blank') `_` can be quite confusing for many beginners, but this is a great feature in Go. We already know that this identifier is for discarding values from function returns, and also that you must use all packages that you've imported in your code in Go. So when the blank is used with import, it means that you need to execute the init() function of that package without directly using it, which exactly fits the use-case for registering database drivers.
|
||||
|
||||
## driver.Driver
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
# 5.5 Develop ORM based on beedb
|
||||
|
||||
( ***Project beedb is no longer maintained, but code still there*** )
|
||||
( ***Project beedb is no longer maintained, but the code s still there*** )
|
||||
|
||||
beedb is a ORM, "Object/Relational Mapping", that developed in Go by me.
|
||||
It uses Go style to operate database, implemented mapping struct to database records. It's a lightweight Go ORM framework, the purpose of developing this ORM is to help people learn how to write ORM, and finds a good balance between functions and performance.
|
||||
beedb is an ORM ( object-relational mapper ) developed in Go, by me.
|
||||
It uses idiomatic Go to operate on databases, implementing struct to database mapping and acts as a lightweight Go ORM framework. The purpose of developing this ORM is not only to help people learn how to write an ORM, but also to find a good balance between functionality and performance when it comes to data persistence.
|
||||
|
||||
beedb is an open source project, and supports basic functions of ORM, but doesn't support associated query.
|
||||
beedb is an open source project that supports basic ORM functionality, but doesn't support association queries.
|
||||
|
||||
Because beedb support `database/sql` interface standards, so any driver that supports this interface can be used in beedb, I've tested following drivers:
|
||||
Because beedb supports `database/sql` interface standards, any driver that implements this interface can be used with beedb. I've tested the following drivers:
|
||||
|
||||
Mysql: [github.com/ziutek/mymysql/godrv](github.com/ziutek/mymysql/godrv)
|
||||
|
||||
@@ -23,13 +23,13 @@ ODBC: [bitbucket.org/miquella/mgodbc](bitbucket.org/miquella/mgodbc)
|
||||
|
||||
## Installation
|
||||
|
||||
You can use `go get` to install beedb in your computer.
|
||||
You can use `go get` to install beedb locally.
|
||||
|
||||
go get github.com/astaxie/beedb
|
||||
|
||||
## Initialization
|
||||
|
||||
First, you have to import all corresponding packages as follows:
|
||||
First, you have to import all the necessary packages:
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
@@ -37,7 +37,7 @@ First, you have to import all corresponding packages as follows:
|
||||
_ "github.com/ziutek/mymysql/godrv"
|
||||
)
|
||||
|
||||
Then you need to open a database connection and create a beedb object(MySQL in this example):
|
||||
Then you need to open a database connection and create a beedb object (MySQL in this example):
|
||||
|
||||
db, err := sql.Open("mymysql", "test/xiemengjun/123456")
|
||||
if err != nil {
|
||||
@@ -45,9 +45,9 @@ Then you need to open a database connection and create a beedb object(MySQL in t
|
||||
}
|
||||
orm := beedb.New(db)
|
||||
|
||||
`beedb.New()` actually has two arguments, the first one is for standard requirement, and the second one is for indicating database engine, but if you are using MySQL/SQLite, you can just skip the second one.
|
||||
`beedb.New()` actually has two arguments. The first is the the database opject, and the second is for indicating which database engine you're using. If you're using MySQL/SQLite, you can just skip the second argument.
|
||||
|
||||
Otherwise, you have to initialize, like SQLServer:
|
||||
Otherwise, this argument must be supplied. For instance, in the case of SQLServer:
|
||||
|
||||
orm = beedb.New(db, "mssql")
|
||||
|
||||
@@ -55,11 +55,11 @@ PostgreSQL:
|
||||
|
||||
orm = beedb.New(db, "pg")
|
||||
|
||||
beedb supports debug, and use following code to enable:
|
||||
beedb supports debugging. Use the following code to enable it:
|
||||
|
||||
beedb.OnDebug=true
|
||||
|
||||
Now we have a struct for the database table `Userinfo` that we used in previous sections.
|
||||
Next, we have a struct for the `Userinfo` database table that we used in previous sections.
|
||||
|
||||
type Userinfo struct {
|
||||
Uid int `PK` // if the primary key is not id, you need to add tag `PK` for your customized primary key.
|
||||
@@ -68,12 +68,12 @@ Now we have a struct for the database table `Userinfo` that we used in previous
|
||||
Created time.Time
|
||||
}
|
||||
|
||||
Be aware that beedb auto-convert your camel style name to underline and lower case letter. For example, we have `UserInfo` as the struct name, and it will be `user_info` in database, same rule for field name.
|
||||
Be aware that beedb auto-converts camelcase names to lower snake case. For example, if we have `UserInfo` as the struct name, beedb will convert it to `user_info` in the database. The same rule applies to struct field names.
|
||||
Camel
|
||||
|
||||
## Insert data
|
||||
|
||||
The following example shows you how to use beedb to save a struct instead of SQL command, and use Save method to apply change.
|
||||
The following example shows you how to use beedb to save a struct, instead of using raw SQL commands. We use the beedb Save method to apply the change.
|
||||
|
||||
var saveone Userinfo
|
||||
saveone.Username = "Test Add User"
|
||||
@@ -81,9 +81,9 @@ The following example shows you how to use beedb to save a struct instead of SQL
|
||||
saveone.Created = time.Now()
|
||||
orm.Save(&saveone)
|
||||
|
||||
And you can check `saveone.Uid` after inserted, its value is self-increase ID, Save method did this job for you.
|
||||
You can check `saveone.Uid` after the record is inserted; its value is a self-incremented ID, which the Save method takes care of for you.
|
||||
|
||||
beedb provides another way to insert data, which is using map.
|
||||
beedb provides another way of inserting data; this is via Go's map type.
|
||||
|
||||
add := make(map[string]interface{})
|
||||
add["username"] = "astaxie"
|
||||
@@ -102,37 +102,37 @@ Insert multiple data:
|
||||
add2["username"] = "astaxie2"
|
||||
add2["departname"] = "cloud develop2"
|
||||
add2["created"] = "2012-12-02"
|
||||
addslice =append(addslice, add, add2)
|
||||
addslice = append(addslice, add, add2)
|
||||
orm.SetTable("userinfo").InsertBatch(addslice)
|
||||
|
||||
The way I showed you above is like chain query, you should be familiar if you know jquery. It returns original ORM object after calls, and continue to do other jobs.
|
||||
The method shown above is similar to a chained query, which you should be familiar with if you've ever used jquery. It returns the original ORM object after calls, then continues doing other jobs.
|
||||
|
||||
The method `SetTable` tells ORM we want to insert our data to table `userinfo`.
|
||||
The method `SetTable` tells the ORM we want to insert our data into the `userinfo` table.
|
||||
|
||||
## Update data
|
||||
|
||||
Continue above example to show how to update data. Now we have primary key value of saveone(Uid), so beedb executes update operation instead of inserting new record.
|
||||
Let's continue working with the above example to see how to update data. Now that we have the primary key of saveone(Uid), beedb executes an update operation instead of inserting a new record.
|
||||
|
||||
saveone.Username = "Update Username"
|
||||
saveone.Departname = "Update Departname"
|
||||
saveone.Created = time.Now()
|
||||
orm.Save(&saveone) // update
|
||||
|
||||
You can use map for updating data also:
|
||||
Like before, you can use map for updating data also:
|
||||
|
||||
t := make(map[string]interface{})
|
||||
t["username"] = "astaxie"
|
||||
orm.SetTable("userinfo").SetPK("uid").Where(2).Update(t)
|
||||
|
||||
Let me explain some methods we used above:
|
||||
Let me explain some of the methods used above:
|
||||
|
||||
- `.SetPK()` tells ORM `uid` is the primary key of table `userinfo`.
|
||||
- `.Where()` sets conditions, supports multiple arguments, if the first argument is a integer, it's a short form of `Where("<primary key>=?", <value>)`.
|
||||
- `.Update()` method accepts map and update to database.
|
||||
- `.SetPK()` tells the ORM that `uid` is the primary key records in the `userinfo` table.
|
||||
- `.Where()` sets conditions and supports multiple arguments. If the first argument is an integer, it's a short form for `Where("<primary key>=?", <value>)`.
|
||||
- `.Update()` method accepts a map and updates the database.
|
||||
|
||||
## Query data
|
||||
|
||||
beedb query interface is very flexible, let's see some examples:
|
||||
The beedb query interface is very flexible. Let's see some examples:
|
||||
|
||||
Example 1, query by primary key:
|
||||
|
||||
@@ -159,7 +159,7 @@ Example 4, more complex conditions:
|
||||
|
||||
Examples to get multiple records:
|
||||
|
||||
Example 1, gets 10 records that `id>3` and starts with position 20:
|
||||
Example 1, gets 10 records with `id>3` that starts with position 20:
|
||||
|
||||
var allusers []Userinfo
|
||||
err := orm.Where("id > ?", "3").Limit(10,20).FindAll(&allusers)
|
||||
@@ -174,17 +174,17 @@ Example 3, gets all records:
|
||||
var everyone []Userinfo
|
||||
err := orm.OrderBy("uid desc,username asc").FindAll(&everyone)
|
||||
|
||||
As you can see, the Limit method is for limiting number of results.
|
||||
As you can see, the Limit method is for limiting the number of results.
|
||||
|
||||
- `.Limit()` supports two arguments, which are number of results and start position. 0 is the default value of start position.
|
||||
- `.OrderBy()` is for ordering results, the arguments is the order condition.
|
||||
- `.Limit()` supports two arguments: the number of results and the starting position. 0 is the default value of the starting position.
|
||||
- `.OrderBy()` is for ordering results. The argument is the order condition.
|
||||
|
||||
All examples that you see is mapping records to structs, and you can also just put data into map as follows:
|
||||
All the examples here are simply mapping records to structs. You can also just put the data into a map as follows:
|
||||
|
||||
a, _ := orm.SetTable("userinfo").SetPK("uid").Where(2).Select("uid,username").FindMap()
|
||||
|
||||
- `.Select()` tells beedb how many fields you want to get from database table, returns all fields as default.
|
||||
- `.FindMap()` returns type `[]map[string][]byte`, so you need to convert to other types by yourself.
|
||||
- `.Select()` tells beedb how many fields you want to get from the database table. If unspecified, all fields are returned by default.
|
||||
- `.FindMap()` returns the `[]map[string][]byte` type, so you need to convert to other types yourself.
|
||||
|
||||
## Delete data
|
||||
|
||||
@@ -204,15 +204,15 @@ Example 3, delete records by SQL:
|
||||
|
||||
orm.SetTable("userinfo").Where("uid>?", 3).DeleteRow()
|
||||
|
||||
## Associated query
|
||||
## Association queries
|
||||
|
||||
beedb doesn't support joining between structs.
|
||||
However since some applications need this feature, here is a implementation:
|
||||
However, since some applications need this feature, here is an implementation:
|
||||
|
||||
a, _ := orm.SetTable("userinfo").Join("LEFT", "userdetail", "userinfo.uid=userdetail.uid")
|
||||
.Where("userinfo.uid=?", 1).Select("userinfo.uid,userinfo.username,userdetail.profile").FindMap()
|
||||
|
||||
We see a new method called `.Join()`, it has three arguments:
|
||||
We see a new method called `.Join()` that has three arguments:
|
||||
|
||||
- The first argument: Type of Join; INNER, LEFT, OUTER, CROSS, etc.
|
||||
- The second argument: the table you want to join with.
|
||||
@@ -220,19 +220,19 @@ We see a new method called `.Join()`, it has three arguments:
|
||||
|
||||
## Group By and Having
|
||||
|
||||
beedb also has a implementation of `group by` and `having`.
|
||||
beedb also has an implementation of `group by` and `having`.
|
||||
|
||||
a, _ := orm.SetTable("userinfo").GroupBy("username").Having("username='astaxie'").FindMap()
|
||||
|
||||
- `.GroupBy()` indicates field that is for group by.
|
||||
- `.GroupBy()` indicates the field that is for group by.
|
||||
- `.Having()` indicates conditions of having.
|
||||
|
||||
## Future
|
||||
|
||||
I have received many feedback from many people from all around world, and I'm thinking about reconfiguration in following aspects:
|
||||
I have received a lot of feedback on beedb from many people all around world, and I'm thinking about reconfiguring the following aspects:
|
||||
|
||||
- Implement interface design like `database/sql/driver` in order to implement corresponding CRUD operations.
|
||||
- Implement relational database design, one to one, one to many, many to many, here are some samples:
|
||||
- Implement an interface design similar to `database/sql/driver` in order to facilitate CRUD operations.
|
||||
- Implement relational database associations like one to one, one to many and many to many. Here's a sample:
|
||||
|
||||
type Profile struct {
|
||||
Nickname string
|
||||
@@ -246,8 +246,8 @@ I have received many feedback from many people from all around world, and I'm th
|
||||
Profile HasOne
|
||||
}
|
||||
|
||||
- Auto-create table and index.
|
||||
- Implement connection pool through goroutine.
|
||||
- Auto-create tables and indexes.
|
||||
- Implement a connection pool using goroutines.
|
||||
|
||||
## Links
|
||||
|
||||
|
||||
Reference in New Issue
Block a user