Skip to content

Commit

Permalink
fixed readme
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Dec 14, 2019
1 parent 640e74e commit 4c4e1bf
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,22 @@ func main() {
var users []struct {
ID, Name string
}
q.All(&users)
err := q.All(&users)

// fetch a single row into a struct
var user struct {
ID, Name string
}
q.One(&user)
err = q.One(&user)

// fetch a single row into a string map
data := dbx.NullStringMap{}
q.One(data)
err = q.One(data)

// fetch row by row
rows2, _ := q.Rows()
for rows2.Next() {
rows2.ScanStruct(&user)
_ = rows2.ScanStruct(&user)
// rows.ScanMap(data)
// rows.Scan(&id, &name)
}
Expand Down Expand Up @@ -154,11 +154,11 @@ func main() {
var users []struct {
ID, Name string
}
q.All(&users)
err := q.All(&users)

// build an INSERT query
// INSERT INTO `users` (`name`) VALUES ('James')
db.Insert("users", dbx.Params{
err = db.Insert("users", dbx.Params{
"name": "James",
}).Execute()
}
Expand Down Expand Up @@ -241,7 +241,7 @@ err = q.Row(&id, &name)
// populate data row by row
rows, _ := q.Rows()
for rows.Next() {
rows.ScanMap(&row)
_ = rows.ScanMap(&row)
}
```

Expand Down Expand Up @@ -303,7 +303,7 @@ parameters. For example,
```go
q := db.NewQuery("SELECT id, name FROM users WHERE id={:id}")
q.Bind(dbx.Params{"id": 100})
q.One(&user)
err := q.One(&user)
```

The above example will select the user record whose `id` is 100. The method `Query.Bind()` binds a set
Expand All @@ -318,10 +318,10 @@ q.Prepare()
defer q.Close()

q.Bind(dbx.Params{"id": 100})
q.One(&user)
err := q.One(&user)

q.Bind(dbx.Params{"id": 200})
q.One(&user)
err = q.One(&user)

// ...
```
Expand All @@ -334,7 +334,7 @@ can associate a context with a query and use the context to cancel the query whi

```go
q := db.NewQuery("SELECT id, name FROM users")
rows := q.WithContext(ctx).All()
err := q.WithContext(ctx).All(&users)
```


Expand All @@ -351,7 +351,7 @@ the corresponding query building methods. For example,

```go
db, _ := dbx.Open("mysql", "user:pass@/example")
db.Select("id", "name").
err := db.Select("id", "name").
From("users").
Where(dbx.HashExp{"id": 100}).
One(&user)
Expand Down Expand Up @@ -435,16 +435,16 @@ Such queries can be built by calling the corresponding methods of `DB`. For exam
db, _ := dbx.Open("mysql", "user:pass@/example")

// INSERT INTO `users` (`name`, `email`) VALUES ({:p0}, {:p1})
db.Insert("users", dbx.Params{
err := db.Insert("users", dbx.Params{
"name": "James",
"email": "[email protected]",
}).Execute()

// UPDATE `users` SET `status`={:p0} WHERE `id`={:p1}
db.Update("users", dbx.Params{"status": 1}, dbx.HashExp{"id": 100}).Execute()
err = db.Update("users", dbx.Params{"status": 1}, dbx.HashExp{"id": 100}).Execute()

// DELETE FROM `users` WHERE `status`={:p0}
db.Delete("users", dbx.HashExp{"status": 2}).Execute()
err = db.Delete("users", dbx.HashExp{"status": 2}).Execute()
```

When building data manipulation queries, remember to call `Execute()` at the end to execute the queries.
Expand All @@ -462,7 +462,7 @@ q := db.CreateTable("users", map[string]string{
"id": "int primary key",
"name": "varchar(255)",
})
q.Execute()
err := q.Execute()
```

## CRUD Operations
Expand Down Expand Up @@ -523,11 +523,11 @@ You can also exclude certain fields from being inserted by calling `Exclude()` b
db, _ := dbx.Open("mysql", "user:pass@/example")

// insert only Name and Email fields
db.Model(&customer).Insert("Name", "Email")
err := db.Model(&customer).Insert("Name", "Email")
// insert all public fields except Status
db.Model(&customer).Exclude("Status").Insert()
err = db.Model(&customer).Exclude("Status").Insert()
// insert only Name
db.Model(&customer).Exclude("Status").Insert("Name", "Status")
err = db.Model(&customer).Exclude("Status").Insert("Name", "Status")
```

### Read
Expand All @@ -539,10 +539,10 @@ db, _ := dbx.Open("mysql", "user:pass@/example")

var customer Customer
// SELECT * FROM customer WHERE id=100
db.Select().Model(100, &customer)
err := db.Select().Model(100, &customer)

// SELECT name, email FROM customer WHERE status=1 AND id=100
db.Select("name", "email").Where(dbx.HashExp{"status": 1}).Model(100, &customer)
err = db.Select("name", "email").Where(dbx.HashExp{"status": 1}).Model(100, &customer)
```

Note that `SelectQuery.Model()` does not support composite primary keys. You should use `SelectQuery.One()` in this case.
Expand All @@ -554,7 +554,7 @@ db, _ := dbx.Open("mysql", "user:pass@/example")
var orderItem OrderItem

// SELECT * FROM order_item WHERE order_id=100 AND item_id=20
db.Select().Where(dbx.HashExp{"order_id": 100, "item_id": 20}).One(&orderItem)
err := db.Select().Where(dbx.HashExp{"order_id": 100, "item_id": 20}).One(&orderItem)
```

In the above queries, we do not call `From()` to specify which table to select data from. This is because the select
Expand All @@ -576,11 +576,11 @@ be updated and which cannot in the same way as described for the `Insert()` meth
db, _ := dbx.Open("mysql", "user:pass@/example")

// update all public fields of customer
db.Model(&customer).Update()
err := db.Model(&customer).Update()
// update only Status
db.Model(&customer).Update("Status")
err = db.Model(&customer).Update("Status")
// update all public fields except Status
db.Model(&customer).Exclude("Status").Update()
err = db.Model(&customer).Exclude("Status").Update()
```

Note that the `Update()` method assumes that the primary keys are immutable. It uses the primary key value of the model
Expand All @@ -595,7 +595,7 @@ specified by the model. If the model does not have a primary key, an error will
```go
db, _ := dbx.Open("mysql", "user:pass@/example")

db.Model(&customer).Delete()
err := db.Model(&customer).Delete()
```

### Null Handling
Expand Down

0 comments on commit 4c4e1bf

Please sign in to comment.