-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added insert example, fixes typos in README
- Loading branch information
1 parent
e833900
commit 4ff7e62
Showing
7 changed files
with
168 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
*.out | ||
.idea | ||
.vscode | ||
cover | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,11 +26,13 @@ $ go get github.com/profe-ajedrez/obreron/v2 | |
|
||
## Use | ||
|
||
You could see the [examples](examples/) directory | ||
|
||
Import package | ||
|
||
```go | ||
import ( | ||
obreron "github.com/profe-ajedrez/obreron/v2" | ||
v2 "github.com/profe-ajedrez/obreron/v2" | ||
) | ||
``` | ||
|
||
|
@@ -40,7 +42,7 @@ import ( | |
|
||
```go | ||
// Produces SELECT a1, a2, a3 FROM client | ||
query, _ := obreron.Select().Col("a1, a2, a3").From("client").Build() | ||
query, _ := v2.Select().Col("a1, a2, a3").From("client").Build() | ||
r, error := db.Query(query) | ||
``` | ||
|
||
|
@@ -49,7 +51,7 @@ r, error := db.Query(query) | |
```go | ||
// Produces SELECT a1, a2, ? AS diez, colIf1, colIf2, ? AS zero, a3, ? AS cien FROM client c JOIN addresses a ON a.id_cliente = a.id_cliente JOIN phones p ON p.id_cliente = c.id_cliente JOIN mailes m ON m.id_cliente = m.id_cliente AND c.estado_cliente = ? LEFT JOIN left_joined lj ON lj.a1 = c.a1 WHERE a1 = ? AND a2 = ? AND a3 = 10 AND a16 = ? | ||
// with params = []any{10, 0, 100, 0, "'last name'", 1000.54, 75} | ||
query, params := obreron.Select(). | ||
query, params := v2.Select(). | ||
Where("a1 = ?", "'last name'"). | ||
Col("a1, a2, ? AS diez", 10). | ||
Col(`colIf1, colIf2, ? AS zero`, 0). | ||
|
@@ -76,7 +78,7 @@ Sometimes we need to check for a condition to build dynamic sql | |
This example adds the column `name` to the query only if the variable `shouldAddName` is true. | ||
|
||
```go | ||
query, _ := obreron.Select(). | ||
query, _ := v2.Select(). | ||
Col("a1, a2, a3"). | ||
ColIf(shouldAddName, "name") | ||
From("client"). | ||
|
@@ -89,7 +91,7 @@ query, _ := obreron.Select(). | |
This also can be applied to joins. | ||
|
||
```go | ||
query, _ := obreron.Select(). | ||
query, _ := v2.Select(). | ||
Col("*"). | ||
From("client c"). | ||
Join("addresses a").On("a.client_id = c.client_id"). | ||
|
@@ -103,7 +105,7 @@ query, _ := obreron.Select(). | |
And boolean connectors | ||
|
||
```go | ||
query, _ := obreron.Select(). | ||
query, _ := v2.Select(). | ||
Col("*"). | ||
From("client c"). | ||
Where("c.status = 0").AndIf(shouldFilterByCountry, "country = 'CL'"). | ||
|
@@ -118,7 +120,7 @@ query, _ := obreron.Select(). | |
You can add params to almost any clause | ||
|
||
```go | ||
query, params := obreron.Select(). | ||
query, params := v2.Select(). | ||
Col("name, mail, ? AS max_credit", 1000000). | ||
From("client c"). | ||
Where("c.status = 0").And("country = ?", "CL"). | ||
|
@@ -131,26 +133,26 @@ query, params := obreron.Select(). | |
* Simple delete | ||
|
||
```go | ||
query, _ := obreron.Delete().From("client").Build() | ||
query, _ := v2.Delete().From("client").Build() | ||
// Produces "DELETE FROM client" | ||
``` | ||
|
||
* Simple del where | ||
|
||
```go | ||
query, _ := obreron.Delete().From("client").Where("client_id = 100").Build() | ||
query, _ := v2.Delete().From("client").Where("client_id = 100").Build() | ||
// Produces "DELETE FROM client WHERE client_id = 100" | ||
``` | ||
|
||
* Like with Select you can use parameters and conditionals with Delete | ||
|
||
```go | ||
query, params := obreron.Delete().From("client").Where("client_id = ?", 100).Build() | ||
query, params := v2.Delete().From("client").Where("client_id = ?", 100).Build() | ||
// Produces "DELETE FROM client WHERE client_id = ?" | ||
``` | ||
|
||
```go | ||
query, params := obreron.Delete().From("client").Where("1=1").AndIf(filterByClient, "client_id = ?", 100).Build() | ||
query, params := v2.Delete().From("client").Where("1=1").AndIf(filterByClient, "client_id = ?", 100).Build() | ||
// Produces "DELETE FROM client WHERE 1=1" when filterByClient is false | ||
// Produces "DELETE FROM client WHERE 1=1 AND client_id = ?" when filterByClient is true | ||
``` | ||
|
@@ -161,14 +163,14 @@ query, params := obreron.Delete().From("client").Where("1=1").AndIf(filterByClie | |
* Simple update | ||
|
||
```go | ||
query, _ := obreron.Update("client").Set("status = 0").Build() | ||
query, _ := v2.Update("client").Set("status = 0").Build() | ||
// Produces UPDATE client SET status = 0 | ||
``` | ||
|
||
* Update/where/order/limit | ||
|
||
```go | ||
query, _ := obreron.Update("client"). | ||
query, _ := v2.Update("client"). | ||
Set("status = 0"). | ||
Where("status = ?", 1). | ||
OrderBy("ciudad"). | ||
|
@@ -179,7 +181,7 @@ query, _ := obreron.Update("client"). | |
* You can use obreron to build an update/join query | ||
|
||
```go | ||
query, _ := obreron.Update("business AS b"). | ||
query, _ := v2.Update("business AS b"). | ||
Join("business_geocode AS g").On("b.business_id = g.business_id"). | ||
Set("b.mapx = g.latitude, b.mapy = g.longitude"). | ||
Where("(b.mapx = '' or b.mapx = 0)"). | ||
|
@@ -192,7 +194,7 @@ Build() | |
* You can use obreron to build an update/select query | ||
|
||
```go | ||
query, _ := obreron.Update("items"). | ||
query, _ := v2.Update("items"). | ||
ColSelect(Select().Col("id, retail / wholesale AS markup, quantity").From("items"), "discounted"). | ||
Set("items.retail = items.retail * 0.9"). | ||
Where("discounted.markup >= 1.3"). | ||
|
@@ -207,7 +209,7 @@ query, _ := obreron.Update("items"). | |
* Simple insert | ||
|
||
```go | ||
query, params := Insert(). | ||
query, params := Iv2.nsert(). | ||
Into("client"). | ||
Col("name, value", "'some name'", "'[email protected]'"). | ||
Build() | ||
|
@@ -218,7 +220,7 @@ query, params := Insert(). | |
* insert select | ||
|
||
```go | ||
query, params := nsert(). | ||
query, params := v2.Insert(). | ||
Into("courses"). | ||
ColSelect("name, location, gid", | ||
Select(). | ||
|
@@ -235,7 +237,7 @@ query, params := nsert(). | |
You can add others clauses using the `Clause` method | ||
|
||
```go | ||
query, params := Insert().Clause("IGNORE", "") | ||
query, params := v2.Insert().Clause("IGNORE", "") | ||
Into("client"). | ||
Col("name, value", "'some name'", "'[email protected]'"). | ||
Build() | ||
|
@@ -246,48 +248,3 @@ query, params := Insert().Clause("IGNORE", "") | |
The `Clause` method always will inject the clause after the last uses building command | ||
|
||
|
||
|
||
## How fast is it? | ||
|
||
```bash | ||
goos: linux | ||
goarch: amd64 | ||
pkg: github.com/profe-ajedrez/obreron/v2 | ||
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz | ||
BenchmarkSelect/columns_-_from-8 18056312 114.1 ns/op 22 B/op 0 allocs/op | ||
BenchmarkSelect/columns_-_from#01-8 23938047 129.7 ns/op 22 B/op 0 allocs/op | ||
BenchmarkSelect/columns_params_-_from-8 15589848 141.3 ns/op 18 B/op 0 allocs/op | ||
BenchmarkSelect/columns_params_-_columns_params_-_from-8 22873436 135.7 ns/op 17 B/op 0 allocs/op | ||
BenchmarkSelect/columns_params_-_columns_params_-_Col_If_-_from-8 17448961 185.9 ns/op 30 B/op 0 allocs/op | ||
BenchmarkSelect/columns_params_-_columns_params_-_Col_If_-_from_-_where-8 22747176 206.5 ns/op 25 B/op 0 allocs/op | ||
BenchmarkSelect/columns_params_-_Col_If_-_columns_params_-_Col_If_-_from_-_join_if_-where-8 19259132 142.5 ns/op 27 B/op 0 allocs/op | ||
BenchmarkSelect/columns_params_-_columns_params_-_Col_If_-_from_-_where#01-8 17754853 131.1 ns/op 19 B/op 0 allocs/op | ||
BenchmarkSelect/columns_params_-_columns_params_-_Col_If_-_from_-_where#02-8 19057185 139.1 ns/op 28 B/op 0 allocs/op | ||
BenchmarkSelect/columns_params_-_columns_params_-_Col_If_-_from_-_where_shuffled-8 20544037 120.0 ns/op 26 B/op 0 allocs/op | ||
BenchmarkSelect/complex_query_shuffled-8 19129396 125.9 ns/op 21 B/op 0 allocs/op | ||
BenchmarkSelect/complex_query_badly_shuffled-8 14647981 300.2 ns/op 18 B/op 0 allocs/op | ||
BenchmarkSelect/columns_-_where_in-8 18710425 233.0 ns/op 28 B/op 0 allocs/op | ||
BenchmarkSelect/columns_-_where_in#01-8 11065960 182.1 ns/op 24 B/op 0 allocs/op | ||
BenchmarkSelect/columns_-_where_like-8 20629710 126.2 ns/op 19 B/op 0 allocs/op | ||
BenchmarkDelete/simple_del-8 25337035 123.7 ns/op 21 B/op 0 allocs/op | ||
BenchmarkDelete/simple_del_where-8 13867886 152.5 ns/op 25 B/op 0 allocs/op | ||
BenchmarkDelete/del_where_conditions-8 20007450 107.5 ns/op 26 B/op 0 allocs/op | ||
BenchmarkDelete/del_where_conditions_limit-8 21412128 111.8 ns/op 22 B/op 0 allocs/op | ||
BenchmarkDelete/del_where_conditions_limit_--_shuffled-8 21919268 128.5 ns/op 24 B/op 0 allocs/op | ||
BenchmarkDelete/simple_del_where_quick-8 22608212 145.3 ns/op 23 B/op 0 allocs/op | ||
BenchmarkDelete/simple_del_where_ignore-8 17356704 198.3 ns/op 17 B/op 0 allocs/op | ||
BenchmarkDelete/simple_del_where_partition-8 18770984 155.7 ns/op 21 B/op 0 allocs/op | ||
BenchmarkDelete/simple_del_where_order_by_limit-8 11128137 200.4 ns/op 21 B/op 0 allocs/op | ||
BenchmarkUpdate/update_simple-8 13399810 195.9 ns/op 22 B/op 0 allocs/op | ||
BenchmarkUpdate/update_where-8 29478886 136.1 ns/op 18 B/op 0 allocs/op | ||
BenchmarkUpdate/update_where_order_limit-8 18497040 311.7 ns/op 26 B/op 0 allocs/op | ||
BenchmarkUpdate/update_where_and_order_limit-8 16168311 174.5 ns/op 20 B/op 0 allocs/op | ||
BenchmarkUpdate/update_select-8 12885265 294.2 ns/op 20 B/op 0 allocs/op | ||
BenchmarkUpdate/update_join-8 17899226 113.7 ns/op 16 B/op 0 allocs/op | ||
BenchmarkInsert/simple_insert-8 4280902 571.5 ns/op 86 B/op 1 allocs/op | ||
BenchmarkInsert/simple_insert_params-8 8915893 380.1 ns/op 86 B/op 1 allocs/op | ||
BenchmarkInsert/simple_insert_params_shuffled-8 10031875 288.6 ns/op 86 B/op 1 allocs/op | ||
BenchmarkInsert/simple_insert_params_select-8 7867750 411.0 ns/op 86 B/op 1 allocs/op | ||
PASS | ||
ok | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
v2 "github.com/profe-ajedrez/obreron/v2" | ||
) | ||
|
||
func main() { | ||
// Simple delete | ||
fmt.Println("Simple Delete") | ||
q, p := v2.Delete().From("client").Build() | ||
fmt.Printf("query: %s \nparams: %v\n\n", q, p) | ||
// OUTPUT: | ||
// query: DELETE FROM client | ||
// params: [] | ||
|
||
// Delete with conditions | ||
fmt.Println("Delete With conditions") | ||
q, p = v2.Delete().From("client"). | ||
Where("client_id = 100"). | ||
And("estado_cliente = 0"). | ||
Y().In("regime_cliente", "'G01','G02', ?", "'G03'"). | ||
And("a"). | ||
Build() | ||
|
||
fmt.Printf("query: %s \nparams: %v\n\n", q, p) | ||
// OUTPUT: | ||
// query: DELETE FROM client WHERE client_id = 100 AND estado_cliente = 0 AND regime_cliente IN ('G01','G02', ?) AND a | ||
// params: ['G03'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
v2 "github.com/profe-ajedrez/obreron/v2" | ||
) | ||
|
||
func main() { | ||
// Simple Insert | ||
fmt.Println("Simple Insert") | ||
q, p := v2.Insert(). | ||
Into("client"). | ||
Col("name, mail", "some name", "[email protected]"). | ||
Build() | ||
fmt.Printf("query: %s \nparams: %v\n\n", q, p) | ||
// OUTPUT: | ||
// qquery: INSERT INTO client ( name, mail ) VALUES ( ?,? ) | ||
// nparams: [some name [email protected]] | ||
|
||
// Conditional insert | ||
fmt.Println("Conditional Insert") | ||
name := "" | ||
mail := "[email protected]" | ||
|
||
q, p = v2.Insert(). | ||
Into("client"). | ||
ColIf(len(name) > 0, "name", name). | ||
ColIf(len(mail) > 0, "mail", mail). | ||
Build() | ||
fmt.Printf("query: %s \nparams: %v\n\n", q, p) | ||
// OUTPUT: | ||
// query: INSERT INTO client ( mail ) VALUES ( ? ) | ||
// params: [[email protected]] | ||
|
||
// Insert Select | ||
fmt.Println("Insert Select") | ||
q, p = v2.Insert(). | ||
Into("courses"). | ||
ColSelect("name, location, gid", | ||
v2.Select(). | ||
Col("name, location, 1"). | ||
From("courses"). | ||
Where("cid = 2"), | ||
).Build() | ||
fmt.Printf("query: %s \nparams: %v\n", q, p) | ||
// OUTPUT | ||
// query: INSERT INTO courses ( name, location, gid ) SELECT name, location, 1 FROM courses WHERE cid = 2 | ||
// params: [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.