2024-08-11 19:49:19 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2024-08-20 19:18:15 +00:00
|
|
|
"github.com/nesterow/dal/pkg/adapter"
|
|
|
|
"github.com/nesterow/dal/pkg/builder"
|
2024-08-20 17:34:01 +00:00
|
|
|
|
2024-08-11 19:49:19 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuilderBasic(t *testing.T) {
|
|
|
|
a := adapter.DBAdapter{Type: "sqlite3"}
|
|
|
|
db, err := a.Open("file::memory:?cache=shared")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open db: %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
_, err = db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name BLOB)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create table: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-08-15 15:52:21 +00:00
|
|
|
inserts := []builder.Map{
|
2024-08-11 19:49:19 +00:00
|
|
|
{"name": "a"},
|
|
|
|
{"name": 'b'},
|
2024-08-15 15:52:21 +00:00
|
|
|
}
|
|
|
|
insert, values := builder.New(adapter.CommonDialect{}).In("test t").Insert(inserts...).Sql()
|
|
|
|
|
2024-08-11 19:49:19 +00:00
|
|
|
_, err = db.Exec(insert, values...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to insert data: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-08-15 07:06:11 +00:00
|
|
|
expr, values := builder.New(adapter.CommonDialect{}).In("test t").Find(builder.Find{"name": builder.Is{"$in": []interface{}{"a", 98}}}).Sql()
|
2024-08-11 19:49:19 +00:00
|
|
|
fmt.Println(expr)
|
|
|
|
rows, err := a.Query(adapter.Query{
|
|
|
|
Db: "file::memory:?cache=shared",
|
|
|
|
Expression: expr,
|
2024-08-12 18:21:34 +00:00
|
|
|
Data: values,
|
2024-08-11 19:49:19 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to query data: %v", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
|
|
|
var name string
|
|
|
|
err = rows.Scan(&id, &name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to scan row: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("id: %d, name: %s\n", id, name)
|
|
|
|
}
|
|
|
|
}
|
2024-08-15 15:52:21 +00:00
|
|
|
|
|
|
|
func TestBuilderSet(t *testing.T) {
|
|
|
|
a := adapter.DBAdapter{Type: "sqlite3"}
|
|
|
|
db, err := a.Open("file::memory:?cache=shared")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open db: %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
_, err = db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name BLOB)")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create table: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
inserts := []builder.Map{
|
|
|
|
{"name": "a"},
|
|
|
|
{"name": 'b'},
|
|
|
|
}
|
|
|
|
insert, values := builder.New(adapter.CommonDialect{}).In("test t").Insert(inserts...).Sql()
|
|
|
|
|
|
|
|
_, err = db.Exec(insert, values...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to insert data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := builder.New(adapter.CommonDialect{}).In("test t")
|
|
|
|
b.Find(builder.Find{"id": builder.Is{"$eq": 2}})
|
|
|
|
b.Set(builder.Map{"name": "c"})
|
|
|
|
b.Tx()
|
|
|
|
expr, values := b.Sql()
|
|
|
|
fmt.Println(expr, values)
|
|
|
|
_, err = a.Exec(adapter.Query{
|
|
|
|
Db: "file::memory:?cache=shared",
|
|
|
|
Expression: expr,
|
|
|
|
Data: values,
|
|
|
|
Transaction: b.Transaction,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to query data: %v", err)
|
|
|
|
}
|
|
|
|
b = builder.New(adapter.CommonDialect{}).In("test t")
|
|
|
|
b.Find(builder.Find{})
|
|
|
|
expr, values = b.Sql()
|
|
|
|
fmt.Println(expr, values)
|
|
|
|
rows, _ := a.Query(adapter.Query{
|
|
|
|
Db: "file::memory:?cache=shared",
|
|
|
|
Expression: expr,
|
|
|
|
Data: values,
|
|
|
|
Transaction: b.Transaction,
|
|
|
|
})
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
|
|
|
var name string
|
|
|
|
err = rows.Scan(&id, &name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to scan row: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("id: %d, name: %s\n", id, name)
|
|
|
|
}
|
|
|
|
}
|