diff --git a/dal/Builder.ts b/dal/Builder.ts index 1916a03..3c15786 100644 --- a/dal/Builder.ts +++ b/dal/Builder.ts @@ -80,6 +80,10 @@ export default class Builder any> { } return instance; } + Raw(sql: string, ...values: unknown[]): Builder { + this.methodCalls.set("Raw", [{s: sql, v: values}]); + return this; + } In(table: string): Builder { this.methodCalls.set("In", [table]); return this; diff --git a/dal/Protocol.ts b/dal/Protocol.ts index 4623504..4360f3a 100644 --- a/dal/Protocol.ts +++ b/dal/Protocol.ts @@ -22,7 +22,7 @@ interface Row { } 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( "|", ); diff --git a/dal/__test__/builder.test.ts b/dal/__test__/builder.test.ts index e1c4e68..ee84c22 100644 --- a/dal/__test__/builder.test.ts +++ b/dal/__test__/builder.test.ts @@ -59,3 +59,16 @@ test("Query format", async () => { } 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); +}); diff --git a/dal/__test__/srv/go.mod b/dal/__test__/srv/go.mod index 449cd56..c7c559a 100644 --- a/dal/__test__/srv/go.mod +++ b/dal/__test__/srv/go.mod @@ -12,7 +12,7 @@ replace l12.xyz/dal/adapter v0.0.0 => ../../../pkg/adapter 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 @@ -28,4 +28,5 @@ require ( l12.xyz/dal/builder v0.0.0 // indirect l12.xyz/dal/filters v0.0.0 // indirect l12.xyz/dal/utils v0.0.0 // indirect + l12.xyz/dal/proto v0.0.0 // indirect ) diff --git a/dal/__test__/srv/main.go b/dal/__test__/srv/main.go index 6fc9683..6657134 100644 --- a/dal/__test__/srv/main.go +++ b/dal/__test__/srv/main.go @@ -5,7 +5,6 @@ import ( "net/http" "os" "os/signal" - "sync" "syscall" _ "github.com/mattn/go-sqlite3" @@ -24,12 +23,12 @@ func mock(adapter adapter.DBAdapter) { } func main() { - defer os.Remove("test.sqlite") - c := make(chan os.Signal) + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c os.Remove("test.sqlite") + os.Exit(1) }() db := adapter.DBAdapter{ Type: "sqlite3", @@ -40,7 +39,4 @@ func main() { mux.Handle("/", queryHandler) fmt.Println("Server running on port 8111") http.ListenAndServe(":8111", mux) - wg := sync.WaitGroup{} - wg.Add(1) - wg.Wait() } diff --git a/pkg/builder/Builder.go b/pkg/builder/Builder.go index 65da304..1cffef4 100644 --- a/pkg/builder/Builder.go +++ b/pkg/builder/Builder.go @@ -7,7 +7,7 @@ import ( const ( 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" ) @@ -18,6 +18,7 @@ type Builder struct { Dialect Dialect LastQuery Find Transaction bool + RawSql RawSql } 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 { b.TableName, b.TableAlias = getTableAlias(table) b.Parts.FromExp = table @@ -184,6 +191,8 @@ func (b *Builder) Tx() *Builder { func (b *Builder) Sql() (string, []interface{}) { operation := b.Parts.Operation switch { + case operation == "RAW": + return b.RawSql["s"].(string), b.RawSql["v"].([]interface{}) case operation == "SELECT" || operation == "SELECT DISTINCT": return unspace(strings.Join([]string{ b.Parts.Operation, diff --git a/pkg/builder/types.go b/pkg/builder/types.go index 5f9ecbd..765a989 100644 --- a/pkg/builder/types.go +++ b/pkg/builder/types.go @@ -5,6 +5,7 @@ import ( filters "l12.xyz/dal/filters" ) +type RawSql = map[string]interface{} type CommonDialect = adapter.CommonDialect type Map = map[string]interface{} type Fields = Map diff --git a/pkg/proto/request.go b/pkg/proto/request.go index 9f0e238..4203fa9 100644 --- a/pkg/proto/request.go +++ b/pkg/proto/request.go @@ -53,7 +53,6 @@ func (q *Request) Parse(dialect adapter.Dialect) (adapter.Query, error) { for i, arg := range cmd.Args { args[i] = reflect.ValueOf(arg) } - fmt.Print(exec, cmd.Method, args) method.Call(args) } expr, data := b.Sql()