[feat] add raw sql call

This commit is contained in:
Anton Nesterov 2024-08-16 20:10:34 +02:00
parent e5152b2bc5
commit fa9d105e57
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
8 changed files with 33 additions and 10 deletions

View file

@ -80,6 +80,10 @@ export default class Builder<I extends abstract new (...args: any) => any> {
} }
return instance; return instance;
} }
Raw(sql: string, ...values: unknown[]): Builder<I> {
this.methodCalls.set("Raw", [{s: sql, v: values}]);
return this;
}
In(table: string): Builder<I> { In(table: string): Builder<I> {
this.methodCalls.set("In", [table]); this.methodCalls.set("In", [table]);
return this; return this;

View file

@ -22,7 +22,7 @@ interface Row {
} }
export const METHODS = export const METHODS =
"In|Find|Select|Fields|Join|Group|Sort|Limit|Offset|Delete|Insert|Set|Update|OnConflict|DoUpdate|DoNothing|Tx".split( "Raw|In|Find|Select|Fields|Join|Group|Sort|Limit|Offset|Delete|Insert|Set|Update|OnConflict|DoUpdate|DoNothing|Tx".split(
"|", "|",
); );

View file

@ -59,3 +59,16 @@ test("Query format", async () => {
} }
expect(true).toBe(true); expect(true).toBe(true);
}); });
test("Query raw", async () => {
const dal = new DAL(options);
const rows = await dal
.Raw("SELECT * FROM test WHERE id = 1")
.As(DTO)
.Query();
for (const row of rows) {
expect(row.id).toBeDefined();
expect(row.age).toBeUndefined();
}
expect(true).toBe(true);
});

View file

@ -12,7 +12,7 @@ replace l12.xyz/dal/adapter v0.0.0 => ../../../pkg/adapter
replace l12.xyz/dal/utils v0.0.0 => ../../../pkg/utils replace l12.xyz/dal/utils v0.0.0 => ../../../pkg/utils
require l12.xyz/dal/proto v0.0.0 // indirect
replace l12.xyz/dal/proto v0.0.0 => ../../../pkg/proto replace l12.xyz/dal/proto v0.0.0 => ../../../pkg/proto
@ -28,4 +28,5 @@ require (
l12.xyz/dal/builder v0.0.0 // indirect l12.xyz/dal/builder v0.0.0 // indirect
l12.xyz/dal/filters v0.0.0 // indirect l12.xyz/dal/filters v0.0.0 // indirect
l12.xyz/dal/utils v0.0.0 // indirect l12.xyz/dal/utils v0.0.0 // indirect
l12.xyz/dal/proto v0.0.0 // indirect
) )

View file

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"sync"
"syscall" "syscall"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -24,12 +23,12 @@ func mock(adapter adapter.DBAdapter) {
} }
func main() { func main() {
defer os.Remove("test.sqlite") c := make(chan os.Signal, 1)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() { go func() {
<-c <-c
os.Remove("test.sqlite") os.Remove("test.sqlite")
os.Exit(1)
}() }()
db := adapter.DBAdapter{ db := adapter.DBAdapter{
Type: "sqlite3", Type: "sqlite3",
@ -40,7 +39,4 @@ func main() {
mux.Handle("/", queryHandler) mux.Handle("/", queryHandler)
fmt.Println("Server running on port 8111") fmt.Println("Server running on port 8111")
http.ListenAndServe(":8111", mux) http.ListenAndServe(":8111", mux)
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
} }

View file

@ -7,7 +7,7 @@ import (
const ( const (
BUILDER_VERSION = "0.0.1" BUILDER_VERSION = "0.0.1"
BUILDER_CLIENT_METHODS = "In|Find|Select|Fields|Join|Group|Sort|Limit|Offset|Delete|Insert|Set|Update|OnConflict|DoUpdate|DoNothing" BUILDER_CLIENT_METHODS = "Raw|In|Find|Select|Fields|Join|Group|Sort|Limit|Offset|Delete|Insert|Set|Update|OnConflict|DoUpdate|DoNothing"
BUILDER_SERVER_METHODS = "Sql" BUILDER_SERVER_METHODS = "Sql"
) )
@ -18,6 +18,7 @@ type Builder struct {
Dialect Dialect Dialect Dialect
LastQuery Find LastQuery Find
Transaction bool Transaction bool
RawSql RawSql
} }
type SQLParts struct { type SQLParts struct {
@ -47,6 +48,12 @@ func New(dialect Dialect) *Builder {
} }
} }
func (b *Builder) Raw(sql map[string]interface{}) *Builder {
b.Parts.Operation = "RAW"
b.RawSql = sql
return b
}
func (b *Builder) In(table string) *Builder { func (b *Builder) In(table string) *Builder {
b.TableName, b.TableAlias = getTableAlias(table) b.TableName, b.TableAlias = getTableAlias(table)
b.Parts.FromExp = table b.Parts.FromExp = table
@ -184,6 +191,8 @@ func (b *Builder) Tx() *Builder {
func (b *Builder) Sql() (string, []interface{}) { func (b *Builder) Sql() (string, []interface{}) {
operation := b.Parts.Operation operation := b.Parts.Operation
switch { switch {
case operation == "RAW":
return b.RawSql["s"].(string), b.RawSql["v"].([]interface{})
case operation == "SELECT" || operation == "SELECT DISTINCT": case operation == "SELECT" || operation == "SELECT DISTINCT":
return unspace(strings.Join([]string{ return unspace(strings.Join([]string{
b.Parts.Operation, b.Parts.Operation,

View file

@ -5,6 +5,7 @@ import (
filters "l12.xyz/dal/filters" filters "l12.xyz/dal/filters"
) )
type RawSql = map[string]interface{}
type CommonDialect = adapter.CommonDialect type CommonDialect = adapter.CommonDialect
type Map = map[string]interface{} type Map = map[string]interface{}
type Fields = Map type Fields = Map

View file

@ -53,7 +53,6 @@ func (q *Request) Parse(dialect adapter.Dialect) (adapter.Query, error) {
for i, arg := range cmd.Args { for i, arg := range cmd.Args {
args[i] = reflect.ValueOf(arg) args[i] = reflect.ValueOf(arg)
} }
fmt.Print(exec, cmd.Method, args)
method.Call(args) method.Call(args)
} }
expr, data := b.Sql() expr, data := b.Sql()