Skip to content

Commit dd12767

Browse files
committed
Add en/0.5.x.md syntax highlighting
1 parent 76e5d0f commit dd12767

File tree

4 files changed

+69
-68
lines changed

4 files changed

+69
-68
lines changed

en/05.3.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ The first driver is the only one that supports the `database/sql` interface stan
1515
## Samples
1616

1717
We create the following SQL:
18-
18+
```sql
1919
CREATE TABLE `userinfo` (
2020
`uid` INTEGER PRIMARY KEY AUTOINCREMENT,
2121
`username` VARCHAR(64) NULL,
2222
`departname` VARCHAR(64) NULL,
2323
`created` DATE NULL
2424
);
25-
25+
```
2626
An example:
27-
27+
```Go
2828
package main
2929

3030
import (
@@ -101,11 +101,11 @@ An example:
101101
panic(err)
102102
}
103103
}
104-
104+
```
105105
You may have noticed that the code is almost the same as in the previous section, and that we only changed the name of the registered driver and called `sql.Open` to connect to SQLite in a different way.
106106

107107
Note that sometimes you can't use the `for` statement because you don't have more than one row, then you can use the `if` statement
108-
108+
```Go
109109
if rows.Next() {
110110
err = rows.Scan(&uid, &username, &department, &created)
111111
checkErr(err)
@@ -114,14 +114,14 @@ Note that sometimes you can't use the `for` statement because you don't have mor
114114
fmt.Println(department)
115115
fmt.Println(created)
116116
}
117-
117+
```
118118
Also you have to do a `rows.Next()`, without using that you can't fetch data in the `Scan` function.
119119

120120
Transactions
121121
===============
122122

123123
The above example shows how you fetch data from the database, but when you want to write a web application then it will not only be necessary to fetch data from the db but it will also be required to write data into it. For that purpose, you should use transactions because for various reasons, such as having multiple go routines which access the database, the database might get locked. This is undesirable in your web application and the use of transactions is effective in ensuring your database activities either pass or fail completely depending on circumstances. It is clear that using transactions can prevent a lot of things from going wrong with the web app.
124-
124+
```Go
125125
trashSQL, err := database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
126126
if err != nil {
127127
fmt.Println(err)
@@ -137,7 +137,7 @@ The above example shows how you fetch data from the database, but when you want
137137
} else {
138138
tx.Commit()
139139
}
140-
140+
```
141141
As it is clear from the above block of code, you first prepare a statement, after which you execute it, depending on the output of that execution then you either roll it back or commit it.
142142

143143

en/05.4.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ I will use the first one in the examples that follow.
1515
## Samples
1616

1717
We create the following SQL:
18-
18+
```sql
1919
CREATE TABLE userinfo
2020
(
2121
uid serial NOT NULL,
@@ -25,9 +25,9 @@ We create the following SQL:
2525
CONSTRAINT userinfo_pkey PRIMARY KEY (uid)
2626
)
2727
WITH (OIDS=FALSE);
28-
28+
```
2929
An example:
30-
30+
```Go
3131
package main
3232

3333
import (
@@ -102,20 +102,20 @@ An example:
102102
panic(err)
103103
}
104104
}
105-
105+
```
106106
Note that PostgreSQL uses the `$1, $2` format instead of the `?` that MySQL uses, and it has a different DSN format in `sql.Open`.
107107
Another thing is that the PostgreSQL driver does not support `sql.Result.LastInsertId()`.
108108
So instead of this,
109-
109+
```Go
110110
stmt, err := db.Prepare("INSERT INTO userinfo(username,departname,created) VALUES($1,$2,$3);")
111111
res, err := stmt.Exec("astaxie", "研发部门", "2012-12-09")
112112
fmt.Println(res.LastInsertId())
113-
113+
```
114114
use `db.QueryRow()` and `.Scan()` to get the value for the last inserted id.
115-
115+
```Go
116116
err = db.QueryRow("INSERT INTO TABLE_NAME values($1) returning uid;", VALUE1").Scan(&lastInsertId)
117117
fmt.Println(lastInsertId)
118-
118+
```
119119
## Links
120120
121121
- [Directory](preface.md)

en/05.5.md

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,69 +32,69 @@ You can use `go get` to install beedb locally.
3232
## Initialization
3333

3434
First, you have to import all the necessary packages:
35-
35+
```Go
3636
import (
3737
"database/sql"
3838
"github.com/astaxie/beedb"
3939
_ "github.com/ziutek/mymysql/godrv"
4040
)
41-
41+
```
4242
Then you need to open a database connection and create a beedb object (MySQL in this example):
43-
43+
```Go
4444
db, err := sql.Open("mymysql", "test/xiemengjun/123456")
4545
if err != nil {
4646
panic(err)
4747
}
4848
orm := beedb.New(db)
49-
49+
```
5050
`beedb.New()` actually has two arguments. The first is the database object, 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.
5151

5252
Otherwise, this argument must be supplied. For instance, in the case of SQLServer:
53-
53+
```Go
5454
orm = beedb.New(db, "mssql")
55-
55+
```
5656
PostgreSQL:
57-
57+
```Go
5858
orm = beedb.New(db, "pg")
59-
59+
```
6060
beedb supports debugging. Use the following code to enable it:
61-
61+
```Go
6262
beedb.OnDebug=true
63-
63+
```
6464
Next, we have a struct for the `Userinfo` database table that we used in previous sections.
65-
65+
```Go
6666
type Userinfo struct {
6767
Uid int `PK` // if the primary key is not id, you need to add tag `PK` for your customized primary key.
6868
Username string
6969
Departname string
7070
Created time.Time
7171
}
72-
72+
```
7373
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.
7474

7575

7676
## Insert data
7777

7878
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.
79-
79+
```Go
8080
var saveone Userinfo
8181
saveone.Username = "Test Add User"
8282
saveone.Departname = "Test Add Departname"
8383
saveone.Created = time.Now()
8484
orm.Save(&saveone)
85-
85+
```
8686
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.
8787

8888
beedb provides another way of inserting data; this is via Go's map type.
89-
89+
```Go
9090
add := make(map[string]interface{})
9191
add["username"] = "astaxie"
9292
add["departname"] = "cloud develop"
9393
add["created"] = "2012-12-02"
9494
orm.SetTable("userinfo").Insert(add)
95-
95+
```
9696
Insert multiple data:
97-
97+
```Go
9898
addslice := make([]map[string]interface{}, 10)
9999
add:=make(map[string]interface{})
100100
add2:=make(map[string]interface{})
@@ -106,26 +106,26 @@ Insert multiple data:
106106
add2["created"] = "2012-12-02"
107107
addslice = append(addslice, add, add2)
108108
orm.SetTable("userinfo").InsertBatch(addslice)
109-
109+
```
110110
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.
111111

112112
The method `SetTable` tells the ORM we want to insert our data into the `userinfo` table.
113113

114114
## Update data
115115

116116
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.
117-
117+
```Go
118118
saveone.Username = "Update Username"
119119
saveone.Departname = "Update Departname"
120120
saveone.Created = time.Now()
121121
orm.Save(&saveone) // update
122-
122+
```
123123
Like before, you can also use map for updating data:
124-
124+
```Go
125125
t := make(map[string]interface{})
126126
t["username"] = "astaxie"
127127
orm.SetTable("userinfo").SetPK("uid").Where(2).Update(t)
128-
128+
```
129129
Let me explain some of the methods used above:
130130

131131
- `.SetPK()` tells the ORM that `uid` is the primary key records in the `userinfo` table.
@@ -137,54 +137,54 @@ Let me explain some of the methods used above:
137137
The beedb query interface is very flexible. Let's see some examples:
138138

139139
Example 1, query by primary key:
140-
140+
```Go
141141
var user Userinfo
142142
// Where accepts two arguments, supports integers
143143
orm.Where("uid=?", 27).Find(&user)
144-
144+
```
145145
Example 2:
146-
146+
```Go
147147
var user2 Userinfo
148148
orm.Where(3).Find(&user2) // short form that omits primary key
149-
149+
```
150150
Example 3, other query conditions:
151-
151+
```Go
152152
var user3 Userinfo
153153
// Where two arguments are accepted, with support for char type.
154154
orm.Where("name = ?", "john").Find(&user3)
155-
155+
```
156156
Example 4, more complex conditions:
157-
157+
```Go
158158
var user4 Userinfo
159159
// Where three arguments are accepted
160160
orm.Where("name = ? and age < ?", "john", 88).Find(&user4)
161-
161+
```
162162
Examples to get multiple records:
163163

164164
Example 1, gets 10 records with `id>3` that starts with position 20:
165-
165+
```Go
166166
var allusers []Userinfo
167167
err := orm.Where("id > ?", "3").Limit(10,20).FindAll(&allusers)
168-
168+
```
169169
Example 2, omits the second argument of limit, so it starts with 0 and gets 10 records:
170-
170+
```Go
171171
var tenusers []Userinfo
172172
err := orm.Where("id > ?", "3").Limit(10).FindAll(&tenusers)
173-
173+
```
174174
Example 3, gets all records:
175-
175+
```Go
176176
var everyone []Userinfo
177177
err := orm.OrderBy("uid desc,username asc").FindAll(&everyone)
178-
178+
```
179179
As you can see, the Limit method is for limiting the number of results.
180180

181181
- `.Limit()` supports two arguments: the number of results and the starting position. 0 is the default value of the starting position.
182182
- `.OrderBy()` is for ordering results. The argument is the order condition.
183183

184184
All the examples here are simply mapping records to structs. You can also just put the data into a map as follows:
185-
185+
```Go
186186
a, _ := orm.SetTable("userinfo").SetPK("uid").Where(2).Select("uid,username").FindMap()
187-
187+
```
188188
- `.Select()` tells beedb how many fields you want to get from the database table. If unspecified, all fields are returned by default.
189189
- `.FindMap()` returns the `[]map[string][]byte` type, so you need to convert to other types yourself.
190190

@@ -193,27 +193,27 @@ All the examples here are simply mapping records to structs. You can also just p
193193
beedb provides rich methods to delete data.
194194

195195
Example 1, delete a single record:
196-
196+
```Go
197197
// saveone is the one in above example.
198198
orm.Delete(&saveone)
199-
199+
```
200200
Example 2, delete multiple records:
201-
201+
```Go
202202
// alluser is the slice which gets multiple records.
203203
orm.DeleteAll(&alluser)
204-
204+
```
205205
Example 3, delete records by SQL:
206-
206+
```Go
207207
orm.SetTable("userinfo").Where("uid>?", 3).DeleteRow()
208-
208+
```
209209
## Association queries
210210

211211
beedb doesn't support joining between structs.
212212
However, since some applications need this feature, here is an implementation:
213-
213+
```Go
214214
a, _ := orm.SetTable("userinfo").Join("LEFT", "userdetail", "userinfo.uid=userdetail.uid")
215215
.Where("userinfo.uid=?", 1).Select("userinfo.uid,userinfo.username,userdetail.profile").FindMap()
216-
216+
```
217217
We see a new method called `.Join()` that has three arguments:
218218

219219
- The first argument: Type of Join; INNER, LEFT, OUTER, CROSS, etc.
@@ -223,9 +223,9 @@ We see a new method called `.Join()` that has three arguments:
223223
## Group By and Having
224224

225225
beedb also has an implementation of `group by` and `having`.
226-
226+
```Go
227227
a, _ := orm.SetTable("userinfo").GroupBy("username").Having("username='astaxie'").FindMap()
228-
228+
```
229229
- `.GroupBy()` indicates the field that is for group by.
230230
- `.Having()` indicates conditions of having.
231231

@@ -235,19 +235,20 @@ I have received a lot of feedback on beedb from many people all around the world
235235

236236
- Implement an interface design similar to `database/sql/driver` in order to facilitate CRUD operations.
237237
- Implement relational database associations like one to one, one to many and many to many. Here's a sample:
238-
238+
```Go
239239
type Profile struct {
240240
Nickname string
241241
Mobile string
242242
}
243+
243244
type Userinfo struct {
244245
Uid int
245246
PK_Username string
246247
Departname string
247248
Created time.Time
248249
Profile HasOne
249250
}
250-
251+
```
251252
- Auto-create tables and indexes.
252253
- Implement a connection pool using goroutines.
253254

en/05.6.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ I forked the last of these packages, fixed some bugs, and used it in my short UR
9898
- [https://github.com/astaxie/goredis](https://github.com/astaxie/goredis)
9999

100100
Let's see how to use the driver that I forked to operate on a database:
101-
101+
```Go
102102
package main
103103

104104
import (
@@ -129,7 +129,7 @@ Let's see how to use the driver that I forked to operate on a database:
129129
}
130130
client.Del("l")
131131
}
132-
132+
```
133133
We can see that it is quite easy to operate redis in Go, and it has high performance. It's client commands are almost the same as redis' built-in commands.
134134

135135
## mongoDB

0 commit comments

Comments
 (0)