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

Commit

Permalink
Updated READMEs for complex query
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Feb 7, 2021
1 parent 7f04d9e commit 8802656
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 88 deletions.
151 changes: 68 additions & 83 deletions README-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,88 +27,6 @@
$ go get github.com/teacat/rushia/v2
```

## 範例

廢話不多說,讓我們先來點有趣的。

```go
rushia.NewQuery("Users").Select()
// 等效於:SELECT * FROM Users

rushia.NewQuery("Users").Insert(rushia.H{
"Username": "YamiOdymel",
"Password": "test",
})
// 等效於:INSERT INTO Users (Username, Password) VALUES (?, ?)
```

然後再來點刺激的。

```go
jobHistories := rushia.NewQuery("JobHistories").
WhereBetween("DepartmentID", 50, 100).
Select("JobID")
jobs := rushia.NewQuery("Jobs").
WhereIn("JobID", jobHistories).
GroupBy("JobID").
Select("JobID", "AVG(MinSalary) AS MyAVG")
maxAverage := rushia.NewQuery(jobs).
As("SS").
Select("MAX(MyAVG)")
employees := rushia.NewQuery("Employees").
GroupBy("JobID").
Having("Avg(Salary)", "<", maxAverage).
Select("JobID", "AVG(Salary)")

// 等效於:
// SELECT JobID,
// AVG(Salary)
// FROM Employees
// HAVING AVG(Salary) < (SELECT MAX(MyAVG)
// FROM (SELECT JobID,
// AVG(MinSalary) AS MyAVG
// FROM Jobs
// WHERE JobID IN (SELECT JobID
// FROM JobHistories
// WHERE DepartmentID BETWEEN 50
// AND 100
// )
// GROUP BY JobID) AS SS)
// GROUP BY job_id;

agents := rushia.NewQuery("Agents").
WhereValue("Commission", "<", 0.12).
Select()
customers := rushia.NewQuery("Customers").
WhereValue("Grade", "=", 3).
WhereValue("CustomerCountry", "<>", "India").
WhereValue("OpeningAmount", "<", 7000).
WhereExists(agents).
Select("OutstandingAmount")
orders := rushia.NewQuery("Orders").
WhereValue("OrderAmount", ">", 2000).
WhereValue("OrderDate", "<", "01-SEP-08").
WhereValue("AdvanceAmount", "<", NewExpr("ANY (?)", customers)).
Select("OrderNum", "OrderDate", "OrderAmount", "AdvanceAmount")

// 等效於:
// SELECT OrderNum,
// OrderDate,
// OrderAmount,
// AdvanceAmount
// FROM Orders
// WHERE OrderAmount > 2000
// AND OrderDate < '01-SEP-08'
// AND AdvanceAmount < ANY (SELECT OutstandingAmount
// FROM Customers
// WHERE Grade = 3
// AND CustomerCountry <> 'India'
// AND OpeningAmount < 7000
// AND EXISTS (SELECT *
// FROM Agents
// WHERE Commission < 0.12));
```

## 使用方式

Rushia 的使用方式十分直覺與簡易,類似基本的 SQL 指令集但是更加地簡化了。
Expand Down Expand Up @@ -639,7 +557,74 @@ rushia.NewQuery("Users").SetQueryOption("LOW_PRIORITY", "IGNORE").Insert(data)
// Gives: INSERT LOW_PRIORITY IGNORE INTO Users ...
```

# 相關連結
## 複雜場景範例

```go
jobHistories := rushia.NewQuery("JobHistories").
WhereBetween("DepartmentID", 50, 100).
Select("JobID")
jobs := rushia.NewQuery("Jobs").
WhereIn("JobID", jobHistories).
GroupBy("JobID").
Select("JobID", "AVG(MinSalary) AS MyAVG")
maxAverage := rushia.NewQuery(jobs).
As("SS").
Select("MAX(MyAVG)")
employees := rushia.NewQuery("Employees").
GroupBy("JobID").
Having("Avg(Salary)", "<", maxAverage).
Select("JobID", "AVG(Salary)")

// 等效於:
// SELECT JobID,
// AVG(Salary)
// FROM Employees
// HAVING AVG(Salary) < (SELECT MAX(MyAVG)
// FROM (SELECT JobID,
// AVG(MinSalary) AS MyAVG
// FROM Jobs
// WHERE JobID IN (SELECT JobID
// FROM JobHistories
// WHERE DepartmentID BETWEEN 50
// AND 100
// )
// GROUP BY JobID) AS SS)
// GROUP BY job_id;

agents := rushia.NewQuery("Agents").
WhereValue("Commission", "<", 0.12).
Select()
customers := rushia.NewQuery("Customers").
WhereValue("Grade", "=", 3).
WhereValue("CustomerCountry", "<>", "India").
WhereValue("OpeningAmount", "<", 7000).
WhereExists(agents).
Select("OutstandingAmount")
orders := rushia.NewQuery("Orders").
WhereValue("OrderAmount", ">", 2000).
WhereValue("OrderDate", "<", "01-SEP-08").
WhereValue("AdvanceAmount", "<", NewExpr("ANY (?)", customers)).
Select("OrderNum", "OrderDate", "OrderAmount", "AdvanceAmount")

// 等效於:
// SELECT OrderNum,
// OrderDate,
// OrderAmount,
// AdvanceAmount
// FROM Orders
// WHERE OrderAmount > 2000
// AND OrderDate < '01-SEP-08'
// AND AdvanceAmount < ANY (SELECT OutstandingAmount
// FROM Customers
// WHERE Grade = 3
// AND CustomerCountry <> 'India'
// AND OpeningAmount < 7000
// AND EXISTS (SELECT *
// FROM Agents
// WHERE Commission < 0.12));
```

## 相關連結

這裡是 Rushia 受啟發,或是和資料庫有所關聯的連結。

Expand Down
73 changes: 68 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ Install the package via `go get` command.
$ go get github.com/teacat/rushia/v2
```

## NULL values

We suggest you to make all the columns in the database as non-nullable since Golang sucks at supporting NULL fields.

## Usage

Rushia is easy to use, it's kinda like a SQL query but simplized.
Expand Down Expand Up @@ -563,7 +559,74 @@ rushia.NewQuery("Users").SetQueryOption("LOW_PRIORITY", "IGNORE").Insert(data)
// Gives: INSERT LOW_PRIORITY IGNORE INTO Users ...
```

# References
## Complex query example

```go
jobHistories := rushia.NewQuery("JobHistories").
WhereBetween("DepartmentID", 50, 100).
Select("JobID")
jobs := rushia.NewQuery("Jobs").
WhereIn("JobID", jobHistories).
GroupBy("JobID").
Select("JobID", "AVG(MinSalary) AS MyAVG")
maxAverage := rushia.NewQuery(jobs).
As("SS").
Select("MAX(MyAVG)")
employees := rushia.NewQuery("Employees").
GroupBy("JobID").
Having("Avg(Salary)", "<", maxAverage).
Select("JobID", "AVG(Salary)")

// Equals:
// SELECT JobID,
// AVG(Salary)
// FROM Employees
// HAVING AVG(Salary) < (SELECT MAX(MyAVG)
// FROM (SELECT JobID,
// AVG(MinSalary) AS MyAVG
// FROM Jobs
// WHERE JobID IN (SELECT JobID
// FROM JobHistories
// WHERE DepartmentID BETWEEN 50
// AND 100
// )
// GROUP BY JobID) AS SS)
// GROUP BY job_id;

agents := rushia.NewQuery("Agents").
WhereValue("Commission", "<", 0.12).
Select()
customers := rushia.NewQuery("Customers").
WhereValue("Grade", "=", 3).
WhereValue("CustomerCountry", "<>", "India").
WhereValue("OpeningAmount", "<", 7000).
WhereExists(agents).
Select("OutstandingAmount")
orders := rushia.NewQuery("Orders").
WhereValue("OrderAmount", ">", 2000).
WhereValue("OrderDate", "<", "01-SEP-08").
WhereValue("AdvanceAmount", "<", NewExpr("ANY (?)", customers)).
Select("OrderNum", "OrderDate", "OrderAmount", "AdvanceAmount")

// Equals:
// SELECT OrderNum,
// OrderDate,
// OrderAmount,
// AdvanceAmount
// FROM Orders
// WHERE OrderAmount > 2000
// AND OrderDate < '01-SEP-08'
// AND AdvanceAmount < ANY (SELECT OutstandingAmount
// FROM Customers
// WHERE Grade = 3
// AND CustomerCountry <> 'India'
// AND OpeningAmount < 7000
// AND EXISTS (SELECT *
// FROM Agents
// WHERE Commission < 0.12));
```

## References

Let's see what inspired Rushia.

Expand Down

0 comments on commit 8802656

Please sign in to comment.