Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Added NewAlias
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Mar 30, 2021
1 parent 14b9757 commit f5f9110
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,14 @@ rushia.NewQuery("Users").WhereValue("Username", "=", "YamiOdymel").Exists()

### 表格別名

有些時候需要幫表格建立別名(或建立子指令時),透過 `As` 就能做到
`As` 能夠替目前的查詢語句賦予表格別名,通常會應用在子查詢。若是在表格加入(JOIN)或是一般場景,則可以使用 `NewAlias`

```go
rushia.NewQuery("Users").As("U").Select()
// 等效於:SELECT * FROM Users AS U
rushia.NewQuery(NewQuery("Users").Select()).As("Result").WhereValue("Username", "=", "YamiOdymel").Select())
// 等效於:SELECT * FROM (SELECT * FROM Users) AS Result WHERE Username = ?

rushia.NewQuery(rushia.NewAlias("UserFriendRelationships", "relations")).Where("relations.ID", "=", 5).Select()
// 等效於: SELECT * FROM UserFriendRelationships AS relations WHERE ID = ?
```

### 執行生指令
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,14 @@ rushia.NewQuery("Users").WhereValue("Username", "=", "YamiOdymel").Exists()

### Table alias

When creating a sub query or table joins, you might need `As` to assign an alias to a table.
`As` assign an alias to the query, it's useful if you are creating a sub query. In a joining or common scenario, use `NewAlias` instead.

```go
rushia.NewQuery("Users").As("U").Select()
// Equals: SELECT * FROM Users AS U
rushia.NewQuery(NewQuery("Users").Select()).As("Result").WhereValue("Username", "=", "YamiOdymel").Select())
// Equals: SELECT * FROM (SELECT * FROM Users) AS Result WHERE Username = ?

rushia.NewQuery(rushia.NewAlias("UserFriendRelationships", "relations")).Where("relations.ID", "=", 5).Select()
// Equals: SELECT * FROM UserFriendRelationships AS relations WHERE ID = ?
```

### Raw Query
Expand Down
10 changes: 10 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,16 @@ func TestRawWhere(t *testing.T) {
assertParams(assert, []interface{}{"YamiOdymel"}, params)
}

//=======================================================
// As
//=======================================================

func TestAs(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery(NewAlias("Products", "p")).Select())
assertEqual(assert, "SELECT * FROM Products AS p", query)
}

//=======================================================
// Distinct
//=======================================================
Expand Down
6 changes: 6 additions & 0 deletions rushia.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rushia

import (
"errors"
"fmt"
"reflect"
)

Expand Down Expand Up @@ -195,6 +196,11 @@ func NewExpr(query string, params ...interface{}) *Expr {
}
}

// NewAlias creates an alias for a table.
func NewAlias(table string, alias string) string {
return fmt.Sprintf("%s AS %s", table, alias)
}

// Build builds the Query.
func Build(q *Query) (query string, params []interface{}) {
query += q.padSpace(q.buildQuery())
Expand Down

0 comments on commit f5f9110

Please sign in to comment.