Create comprehensive documentation for future Claude Code instances working in this repository, including: - Development commands for testing, building, and code quality - Core architecture overview of the SQL query builder system - Directory structure and component explanations - Testing patterns and conventions - Key dependencies and their purposes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
313 lines
8.2 KiB
Go
313 lines
8.2 KiB
Go
package db_test
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
dbv2 "git.fsdpf.net/go/db"
|
|
_ "git.fsdpf.net/go/db/dialect/mysql"
|
|
)
|
|
|
|
func ExampleDelete() {
|
|
ds := dbv2.Delete("items")
|
|
|
|
sql, args, _ := ds.ToSQL()
|
|
fmt.Println(sql, args)
|
|
|
|
// Output:
|
|
// DELETE FROM "items" []
|
|
}
|
|
|
|
func ExampleDeleteDataset_Executor() {
|
|
_db := getDB()
|
|
|
|
de := _db.Delete("test_user").
|
|
Where(dbv2.Ex{"first_name": "Bob"}).
|
|
Executor()
|
|
if r, err := de.Exec(); err != nil {
|
|
fmt.Println(err.Error())
|
|
} else {
|
|
c, _ := r.RowsAffected()
|
|
fmt.Printf("Deleted %d users", c)
|
|
}
|
|
|
|
// Output:
|
|
// Deleted 1 users
|
|
}
|
|
|
|
func ExampleDeleteDataset_Executor_returning() {
|
|
_db := getDB()
|
|
|
|
de := _db.Delete("test_user").
|
|
Where(dbv2.C("last_name").Eq("Yukon")).
|
|
Returning(dbv2.C("id")).
|
|
Executor()
|
|
|
|
var ids []int64
|
|
if err := de.ScanVals(&ids); err != nil {
|
|
fmt.Println(err.Error())
|
|
} else {
|
|
fmt.Printf("Deleted users [ids:=%+v]", ids)
|
|
}
|
|
|
|
// Output:
|
|
// Deleted users [ids:=[1 2 3]]
|
|
}
|
|
|
|
func ExampleDeleteDataset_With() {
|
|
sql, _, _ := dbv2.Delete("test").
|
|
With("check_vals(val)", dbv2.From().Select(dbv2.L("123"))).
|
|
Where(dbv2.C("val").Eq(dbv2.From("check_vals").Select("val"))).
|
|
ToSQL()
|
|
fmt.Println(sql)
|
|
|
|
// Output:
|
|
// WITH check_vals(val) AS (SELECT 123) DELETE FROM "test" WHERE ("val" IN (SELECT "val" FROM "check_vals"))
|
|
}
|
|
|
|
func ExampleDeleteDataset_WithRecursive() {
|
|
sql, _, _ := dbv2.Delete("nums").
|
|
WithRecursive("nums(x)",
|
|
dbv2.From().Select(dbv2.L("1")).
|
|
UnionAll(dbv2.From("nums").
|
|
Select(dbv2.L("x+1")).Where(dbv2.C("x").Lt(5)))).
|
|
ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// WITH RECURSIVE nums(x) AS (SELECT 1 UNION ALL (SELECT x+1 FROM "nums" WHERE ("x" < 5))) DELETE FROM "nums"
|
|
}
|
|
|
|
func ExampleDeleteDataset_Where() {
|
|
// By default everything is anded together
|
|
sql, _, _ := dbv2.Delete("test").Where(dbv2.Ex{
|
|
"a": dbv2.Op{"gt": 10},
|
|
"b": dbv2.Op{"lt": 10},
|
|
"c": nil,
|
|
"d": []string{"a", "b", "c"},
|
|
}).ToSQL()
|
|
fmt.Println(sql)
|
|
// You can use ExOr to get ORed expressions together
|
|
sql, _, _ = dbv2.Delete("test").Where(dbv2.ExOr{
|
|
"a": dbv2.Op{"gt": 10},
|
|
"b": dbv2.Op{"lt": 10},
|
|
"c": nil,
|
|
"d": []string{"a", "b", "c"},
|
|
}).ToSQL()
|
|
fmt.Println(sql)
|
|
// You can use Or with Ex to Or multiple Ex maps together
|
|
sql, _, _ = dbv2.Delete("test").Where(
|
|
dbv2.Or(
|
|
dbv2.Ex{
|
|
"a": dbv2.Op{"gt": 10},
|
|
"b": dbv2.Op{"lt": 10},
|
|
},
|
|
dbv2.Ex{
|
|
"c": nil,
|
|
"d": []string{"a", "b", "c"},
|
|
},
|
|
),
|
|
).ToSQL()
|
|
fmt.Println(sql)
|
|
// By default everything is anded together
|
|
sql, _, _ = dbv2.Delete("test").Where(
|
|
dbv2.C("a").Gt(10),
|
|
dbv2.C("b").Lt(10),
|
|
dbv2.C("c").IsNull(),
|
|
dbv2.C("d").In("a", "b", "c"),
|
|
).ToSQL()
|
|
fmt.Println(sql)
|
|
// You can use a combination of Ors and Ands
|
|
sql, _, _ = dbv2.Delete("test").Where(
|
|
dbv2.Or(
|
|
dbv2.C("a").Gt(10),
|
|
dbv2.And(
|
|
dbv2.C("b").Lt(10),
|
|
dbv2.C("c").IsNull(),
|
|
),
|
|
),
|
|
).ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE FROM "test" WHERE (("a" > 10) AND ("b" < 10) AND ("c" IS NULL) AND ("d" IN ('a', 'b', 'c')))
|
|
// DELETE FROM "test" WHERE (("a" > 10) OR ("b" < 10) OR ("c" IS NULL) OR ("d" IN ('a', 'b', 'c')))
|
|
// DELETE FROM "test" WHERE ((("a" > 10) AND ("b" < 10)) OR (("c" IS NULL) AND ("d" IN ('a', 'b', 'c'))))
|
|
// DELETE FROM "test" WHERE (("a" > 10) AND ("b" < 10) AND ("c" IS NULL) AND ("d" IN ('a', 'b', 'c')))
|
|
// DELETE FROM "test" WHERE (("a" > 10) OR (("b" < 10) AND ("c" IS NULL)))
|
|
}
|
|
|
|
func ExampleDeleteDataset_Where_prepared() {
|
|
// By default everything is anded together
|
|
sql, args, _ := dbv2.Delete("test").Prepared(true).Where(dbv2.Ex{
|
|
"a": dbv2.Op{"gt": 10},
|
|
"b": dbv2.Op{"lt": 10},
|
|
"c": nil,
|
|
"d": []string{"a", "b", "c"},
|
|
}).ToSQL()
|
|
fmt.Println(sql, args)
|
|
// You can use ExOr to get ORed expressions together
|
|
sql, args, _ = dbv2.Delete("test").Prepared(true).Where(dbv2.ExOr{
|
|
"a": dbv2.Op{"gt": 10},
|
|
"b": dbv2.Op{"lt": 10},
|
|
"c": nil,
|
|
"d": []string{"a", "b", "c"},
|
|
}).ToSQL()
|
|
fmt.Println(sql, args)
|
|
// You can use Or with Ex to Or multiple Ex maps together
|
|
sql, args, _ = dbv2.Delete("test").Prepared(true).Where(
|
|
dbv2.Or(
|
|
dbv2.Ex{
|
|
"a": dbv2.Op{"gt": 10},
|
|
"b": dbv2.Op{"lt": 10},
|
|
},
|
|
dbv2.Ex{
|
|
"c": nil,
|
|
"d": []string{"a", "b", "c"},
|
|
},
|
|
),
|
|
).ToSQL()
|
|
fmt.Println(sql, args)
|
|
// By default everything is anded together
|
|
sql, args, _ = dbv2.Delete("test").Prepared(true).Where(
|
|
dbv2.C("a").Gt(10),
|
|
dbv2.C("b").Lt(10),
|
|
dbv2.C("c").IsNull(),
|
|
dbv2.C("d").In("a", "b", "c"),
|
|
).ToSQL()
|
|
fmt.Println(sql, args)
|
|
// You can use a combination of Ors and Ands
|
|
sql, args, _ = dbv2.Delete("test").Prepared(true).Where(
|
|
dbv2.Or(
|
|
dbv2.C("a").Gt(10),
|
|
dbv2.And(
|
|
dbv2.C("b").Lt(10),
|
|
dbv2.C("c").IsNull(),
|
|
),
|
|
),
|
|
).ToSQL()
|
|
fmt.Println(sql, args)
|
|
// Output:
|
|
// DELETE FROM "test" WHERE (("a" > ?) AND ("b" < ?) AND ("c" IS NULL) AND ("d" IN (?, ?, ?))) [10 10 a b c]
|
|
// DELETE FROM "test" WHERE (("a" > ?) OR ("b" < ?) OR ("c" IS NULL) OR ("d" IN (?, ?, ?))) [10 10 a b c]
|
|
// DELETE FROM "test" WHERE ((("a" > ?) AND ("b" < ?)) OR (("c" IS NULL) AND ("d" IN (?, ?, ?)))) [10 10 a b c]
|
|
// DELETE FROM "test" WHERE (("a" > ?) AND ("b" < ?) AND ("c" IS NULL) AND ("d" IN (?, ?, ?))) [10 10 a b c]
|
|
// DELETE FROM "test" WHERE (("a" > ?) OR (("b" < ?) AND ("c" IS NULL))) [10 10]
|
|
}
|
|
|
|
func ExampleDeleteDataset_ClearWhere() {
|
|
ds := dbv2.Delete("test").Where(
|
|
dbv2.Or(
|
|
dbv2.C("a").Gt(10),
|
|
dbv2.And(
|
|
dbv2.C("b").Lt(10),
|
|
dbv2.C("c").IsNull(),
|
|
),
|
|
),
|
|
)
|
|
sql, _, _ := ds.ClearWhere().ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE FROM "test"
|
|
}
|
|
|
|
func ExampleDeleteDataset_Limit() {
|
|
ds := dbv2.Dialect("mysql").Delete("test").Limit(10)
|
|
sql, _, _ := ds.ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE FROM `test` LIMIT 10
|
|
}
|
|
|
|
func ExampleDeleteDataset_LimitAll() {
|
|
// Using mysql dialect because it supports limit on delete
|
|
ds := dbv2.Dialect("mysql").Delete("test").LimitAll()
|
|
sql, _, _ := ds.ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE FROM `test` LIMIT ALL
|
|
}
|
|
|
|
func ExampleDeleteDataset_ClearLimit() {
|
|
// Using mysql dialect because it supports limit on delete
|
|
ds := dbv2.Dialect("mysql").Delete("test").Limit(10)
|
|
sql, _, _ := ds.ClearLimit().ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE `test` FROM `test`
|
|
}
|
|
|
|
func ExampleDeleteDataset_Order() {
|
|
// use mysql dialect because it supports order by on deletes
|
|
ds := dbv2.Dialect("mysql").Delete("test").Order(dbv2.C("a").Asc())
|
|
sql, _, _ := ds.ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE FROM `test` ORDER BY `a` ASC
|
|
}
|
|
|
|
func ExampleDeleteDataset_OrderAppend() {
|
|
// use mysql dialect because it supports order by on deletes
|
|
ds := dbv2.Dialect("mysql").Delete("test").Order(dbv2.C("a").Asc())
|
|
sql, _, _ := ds.OrderAppend(dbv2.C("b").Desc().NullsLast()).ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE FROM `test` ORDER BY `a` ASC, `b` DESC NULLS LAST
|
|
}
|
|
|
|
func ExampleDeleteDataset_OrderPrepend() {
|
|
// use mysql dialect because it supports order by on deletes
|
|
ds := dbv2.Dialect("mysql").Delete("test").Order(dbv2.C("a").Asc())
|
|
sql, _, _ := ds.OrderPrepend(dbv2.C("b").Desc().NullsLast()).ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE FROM `test` ORDER BY `b` DESC NULLS LAST, `a` ASC
|
|
}
|
|
|
|
func ExampleDeleteDataset_ClearOrder() {
|
|
ds := dbv2.Delete("test").Order(dbv2.C("a").Asc())
|
|
sql, _, _ := ds.ClearOrder().ToSQL()
|
|
fmt.Println(sql)
|
|
// Output:
|
|
// DELETE FROM "test"
|
|
}
|
|
|
|
func ExampleDeleteDataset_ToSQL() {
|
|
sql, args, _ := dbv2.Delete("items").ToSQL()
|
|
fmt.Println(sql, args)
|
|
|
|
sql, args, _ = dbv2.Delete("items").
|
|
Where(dbv2.Ex{"id": dbv2.Op{"gt": 10}}).
|
|
ToSQL()
|
|
fmt.Println(sql, args)
|
|
|
|
// Output:
|
|
// DELETE FROM "items" []
|
|
// DELETE FROM "items" WHERE ("id" > 10) []
|
|
}
|
|
|
|
func ExampleDeleteDataset_Prepared() {
|
|
sql, args, _ := dbv2.Delete("items").Prepared(true).ToSQL()
|
|
fmt.Println(sql, args)
|
|
|
|
sql, args, _ = dbv2.Delete("items").
|
|
Prepared(true).
|
|
Where(dbv2.Ex{"id": dbv2.Op{"gt": 10}}).
|
|
ToSQL()
|
|
fmt.Println(sql, args)
|
|
|
|
// Output:
|
|
// DELETE FROM "items" []
|
|
// DELETE FROM "items" WHERE ("id" > ?) [10]
|
|
}
|
|
|
|
func ExampleDeleteDataset_Returning() {
|
|
ds := dbv2.Delete("items")
|
|
sql, args, _ := ds.Returning("id").ToSQL()
|
|
fmt.Println(sql, args)
|
|
|
|
sql, args, _ = ds.Returning("id").Where(dbv2.C("id").IsNotNull()).ToSQL()
|
|
fmt.Println(sql, args)
|
|
|
|
// Output:
|
|
// DELETE FROM "items" RETURNING "id" []
|
|
// DELETE FROM "items" WHERE ("id" IS NOT NULL) RETURNING "id" []
|
|
}
|