Skip to content

Commit

Permalink
added insert example, fixes typos in README
Browse files Browse the repository at this point in the history
  • Loading branch information
profe-ajedrez committed Nov 4, 2024
1 parent e833900 commit 4ff7e62
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 63 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.out
.idea
.vscode
cover

# Test binary, built with `go test -c`
*.test
Expand Down
83 changes: 20 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
```

Expand All @@ -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)
```

Expand All @@ -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).
Expand All @@ -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").
Expand All @@ -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").
Expand All @@ -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'").
Expand All @@ -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").
Expand All @@ -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
```
Expand All @@ -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").
Expand All @@ -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)").
Expand All @@ -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").
Expand All @@ -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()
Expand All @@ -218,7 +220,7 @@ query, params := Insert().
* insert select

```go
query, params := nsert().
query, params := v2.Insert().
Into("courses").
ColSelect("name, location, gid",
Select().
Expand All @@ -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()
Expand All @@ -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
```
15 changes: 15 additions & 0 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,36 @@ func Delete() *DeleteStm {
return d
}

// From sets the target table for the delete stament
//
// # Example
//
// s := Delete()
// From("client")
func (dst *DeleteStm) From(source string) *DeleteStm {
dst.add(fromS, "FROM", source)
return dst
}

// Where adds a condition to filter the query
func (dst *DeleteStm) Where(cond string, p ...any) *DeleteStm {
dst.where(cond, p...)
return dst
}

// Y adds an AND conector to the stament where is called. Its helpful when used with In()
func (dst *DeleteStm) Y() *DeleteStm {
dst.clause("AND", "")
return dst
}

// And adds a condition to the query connecting with an AND operator
func (dst *DeleteStm) And(expr string, p ...any) *DeleteStm {
dst.clause("AND", expr, p...)
return dst
}

// AndIf adds a condition to the query connecting with an AND operator only when cond parameter is true
func (dst *DeleteStm) AndIf(cond bool, expr string, p ...any) *DeleteStm {
if cond {
dst.clause("AND", expr, p...)
Expand All @@ -55,18 +65,21 @@ func (dst *DeleteStm) OrIf(cond bool, expr string, p ...any) *DeleteStm {
return dst
}

// Like adds a LIKE clause to the query after the las clause added
func (dst *DeleteStm) Like(expr string, p ...any) *DeleteStm {
dst.clause("LIKE", expr, p...)
return dst
}

// LikeIf adds a LIKE clause to the query after the las clause added, when cond is true
func (dst *DeleteStm) LikeIf(cond bool, expr string, p ...any) *DeleteStm {
if cond {
dst.Like(expr, p...)
}
return dst
}

// In adds a IN clause to the query after the las clause added
func (dst *DeleteStm) In(value, expr string, p ...any) *DeleteStm {
dst.clause(value+" IN ("+expr+")", "", p...)
return dst
Expand Down Expand Up @@ -94,11 +107,13 @@ func (dst *DeleteStm) OrderBy(expr string, p ...any) *DeleteStm {
return dst
}

// Limit adds a LIMIT clause to the query
func (dst *DeleteStm) Limit(limit int) *DeleteStm {
dst.add(limitS, "LIMIT", "?", limit)
return dst
}

// Clause adds a custom clause to the query in the position were is invoked
func (dst *DeleteStm) Clause(clause, expr string, p ...any) *DeleteStm {
dst.add(dst.lastPos, clause, expr, p...)
return dst
Expand Down
31 changes: 31 additions & 0 deletions examples/delete/delete.go
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']
}
50 changes: 50 additions & 0 deletions examples/insert/insert.go
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: []
}
11 changes: 11 additions & 0 deletions obreron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,17 @@ func updateTestCases() (tcs []struct {
Set("b.mapx = g.latitude, b.mapy = g.longitude").
Where("(b.mapx = '' or b.mapx = 0)").And("g.latitude > 0").ClauseIf(true, "AND", "3 = 3"),
},
{
tc: Update("items").
Set("items.retail = items.retail * 0.9").
Set("a = 2").
Where("discounted.markup >= 1.3").
And("colX").
Like("'%ago%'"),
name: "",
expected: "UPDATE items SET items.retail = items.retail * 0.9, a = 2 WHERE discounted.markup >= 1.3 AND colX LIKE '%ago%'",
expectedParams: []any{},
},
}...)
return tcs
}
Expand Down
Loading

0 comments on commit 4ff7e62

Please sign in to comment.