Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Dec 4, 2017
1 parent d75e492 commit 4a4bac5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ results, err := engine.QueryInterface("select * from user")
results, err := engine.Where("a = 1").QueryInterface()
```

* `Execute` runs a SQL string, it returns `affected` and `error`
* `Exec` runs a SQL string, it returns `affected` and `error`

```Go
affected, err := engine.Exec("update user set age = ? where name = ?", age, name)
Expand All @@ -158,7 +158,7 @@ affected, err := engine.Insert(&user1, &users)
// INSERT INTO struct2 () values (),(),()
```

* Query one record from database
* `Get` query one record from database

```Go
has, err := engine.Get(&user)
Expand All @@ -180,7 +180,7 @@ has, err := engine.Where("id = ?", id).Cols(cols...).Get(&valuesSlice)
// SELECT col1, col2, col3 FROM user WHERE id = ?
```

* Check if one record exist on table
* `Exist` check if one record exist on table

```Go
has, err := testEngine.Exist(new(RecordExist))
Expand All @@ -199,7 +199,7 @@ has, err = testEngine.Table("record_exist").Where("name = ?", "test1").Exist()
// SELECT * FROM record_exist WHERE name = ? LIMIT 1
```

* Query multiple records from database, also you can use join and extends
* `Find` query multiple records from database, also you can use join and extends

```Go
var users []User
Expand All @@ -224,7 +224,7 @@ err := engine.Table("user").Select("user.*, detail.*").
// SELECT user.*, detail.* FROM user INNER JOIN detail WHERE user.name = ? limit 0 offset 10
```

* Query multiple records and record by record handle, there are two methods Iterate and Rows
* `Iterate` and `Rows` query multiple records and record by record handle, there are two methods Iterate and Rows

```Go
err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
Expand All @@ -233,6 +233,13 @@ err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
})
// SELECT * FROM user

err := engine.BufferSize(100).Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
user := bean.(*User)
return nil
})
// SELECT * FROM user Limit 0, 100
// SELECT * FROM user Limit 101, 100

rows, err := engine.Rows(&User{Name:name})
// SELECT * FROM user
defer rows.Close()
Expand All @@ -242,7 +249,7 @@ for rows.Next() {
}
```

* Update one or more records, default will update non-empty and non-zero fields except when you use Cols, AllCols and so on.
* `Update` update one or more records, default will update non-empty and non-zero fields except when you use Cols, AllCols and so on.

```Go
affected, err := engine.Id(1).Update(&user)
Expand All @@ -267,22 +274,22 @@ affected, err := engine.Id(1).AllCols().Update(&user)
// UPDATE user SET name=?,age=?,salt=?,passwd=?,updated=? Where id = ?
```

* Delete one or more records, Delete MUST have condition
* `Delete` delete one or more records, Delete MUST have condition

```Go
affected, err := engine.Where(...).Delete(&user)
// DELETE FROM user Where ...
affected, err := engine.Id(2).Delete(&user)
```

* Count records
* `Count` count records

```Go
counts, err := engine.Count(&user)
// SELECT count(*) AS total FROM user
```

* Sum functions
* `Sum` sum functions

```Go
agesFloat64, err := engine.Sum(&user, "age")
Expand Down
25 changes: 16 additions & 9 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ results, err := engine.Where("a = 1").QueryInterface()
affected, err := engine.Exec("update user set age = ? where name = ?", age, name)
```

* 插入一条或者多条记录
* `Insert` 插入一条或者多条记录

```Go
affected, err := engine.Insert(&user)
Expand All @@ -165,7 +165,7 @@ affected, err := engine.Insert(&user1, &users)
// INSERT INTO struct2 () values (),(),()
```

* 查询单条记录
* `Get` 查询单条记录

```Go
has, err := engine.Get(&user)
Expand All @@ -187,7 +187,7 @@ has, err := engine.Where("id = ?", id).Cols(cols...).Get(&valuesSlice)
// SELECT col1, col2, col3 FROM user WHERE id = ?
```

* 检测记录是否存在
* `Exist` 检测记录是否存在

```Go
has, err := testEngine.Exist(new(RecordExist))
Expand All @@ -206,7 +206,7 @@ has, err = testEngine.Table("record_exist").Where("name = ?", "test1").Exist()
// SELECT * FROM record_exist WHERE name = ? LIMIT 1
```

* 查询多条记录,当然可以使用Join和extends来组合使用
* `Find` 查询多条记录,当然可以使用Join和extends来组合使用

```Go
var users []User
Expand All @@ -231,7 +231,7 @@ err := engine.Table("user").Select("user.*, detail.*")
// SELECT user.*, detail.* FROM user INNER JOIN detail WHERE user.name = ? limit 0 offset 10
```

* 根据条件遍历数据库,可以有两种方式: Iterate and Rows
* `Iterate``Rows` 根据条件遍历数据库,可以有两种方式: Iterate and Rows

```Go
err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
Expand All @@ -240,6 +240,13 @@ err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
})
// SELECT * FROM user

err := engine.BufferSize(100).Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
user := bean.(*User)
return nil
})
// SELECT * FROM user Limit 0, 100
// SELECT * FROM user Limit 101, 100

rows, err := engine.Rows(&User{Name:name})
// SELECT * FROM user
defer rows.Close()
Expand All @@ -249,7 +256,7 @@ for rows.Next() {
}
```

* 更新数据,除非使用Cols,AllCols函数指明,默认只更新非空和非0的字段
* `Update` 更新数据,除非使用Cols,AllCols函数指明,默认只更新非空和非0的字段

```Go
affected, err := engine.Id(1).Update(&user)
Expand All @@ -274,21 +281,21 @@ affected, err := engine.Id(1).AllCols().Update(&user)
// UPDATE user SET name=?,age=?,salt=?,passwd=?,updated=? Where id = ?
```

* 删除记录,需要注意,删除必须至少有一个条件,否则会报错。要清空数据库可以用EmptyTable
* `Delete` 删除记录,需要注意,删除必须至少有一个条件,否则会报错。要清空数据库可以用EmptyTable

```Go
affected, err := engine.Where(...).Delete(&user)
// DELETE FROM user Where ...
```

* 获取记录条数
* `Count` 获取记录条数

```Go
counts, err := engine.Count(&user)
// SELECT count(*) AS total FROM user
```

* 求和函数
* `Sum` 求和函数

```Go
agesFloat64, err := engine.Sum(&user, "age")
Expand Down

0 comments on commit 4a4bac5

Please sign in to comment.