[feat] add raw sql call
This commit is contained in:
parent
e5152b2bc5
commit
fa9d105e57
|
@ -80,6 +80,10 @@ export default class Builder<I extends abstract new (...args: any) => any> {
|
|||
}
|
||||
return instance;
|
||||
}
|
||||
Raw(sql: string, ...values: unknown[]): Builder<I> {
|
||||
this.methodCalls.set("Raw", [{s: sql, v: values}]);
|
||||
return this;
|
||||
}
|
||||
In(table: string): Builder<I> {
|
||||
this.methodCalls.set("In", [table]);
|
||||
return this;
|
||||
|
|
|
@ -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(
|
||||
"|",
|
||||
);
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue