Skip to content

Commit

Permalink
patched template and parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Aug 17, 2022
1 parent 8f17961 commit 0e266f2
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.8.2
v0.2.8.3
3 changes: 2 additions & 1 deletion cmd/ast/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func ExtractHint(text string) string {
}

func UnmarshalHint(hint string, dest interface{}) (string, error) {
hint = hint[3 : len(hint)-2]
hint = hint[2 : len(hint)-2]
hint = strings.TrimSpace(hint)

//TODO: replace with parsly
index := strings.LastIndex(hint, "}")
Expand Down
14 changes: 8 additions & 6 deletions cmd/ast/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ast

import (
"bytes"
"encoding/json"
"fmt"
"github.com/viant/datly/cmd/option"
"github.com/viant/parsly"
Expand All @@ -12,7 +11,7 @@ import (

var resetWords = []string{"AND", "OR", "WITH", "HAVING", "LIMIT", "OFFSET", "WHERE", "SELECT", "UNION", "ALL", "AS", "BETWEEN", "IN"}

func correctUntyped(SQL string, variables map[string]bool, meta *option.ViewMeta) {
func (d *paramTypeDetector) correctUntyped(SQL string, variables map[string]bool, meta *option.ViewMeta) {
var typer option.Typer
var untyped []*option.Parameter

Expand Down Expand Up @@ -53,11 +52,10 @@ outer:
continue
}

matched = cursor.MatchAfterOptional(whitespaceMatcher, commentBlockMatcher)
if matched.Code == commentBlockToken {
hint, ok := d.hints[paramName]
if ok {
parameter := &option.Parameter{}
commentContent := bytes.TrimSpace(bytes.Trim(matched.Bytes(cursor), "/**/"))
_ = json.Unmarshal(commentContent, parameter)
_, _ = UnmarshalHint(hint.Hint, parameter)
inherit(aParam, parameter)
}

Expand Down Expand Up @@ -101,4 +99,8 @@ func inherit(generated *option.Parameter, inlined *option.Parameter) {
if inlined.Id != "" {
generated.Id = inlined.Id
}

if inlined.Codec != "" {
generated.Codec = inlined.Codec
}
}
10 changes: 6 additions & 4 deletions cmd/ast/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ type paramTypeDetector struct {
paramTypes map[string]string
variables map[string]bool
viewMeta *option.ViewMeta
hints map[string]*option.ParameterHint
}

func newParamTypeDetector(route *option.Route, meta *option.ViewMeta) *paramTypeDetector {
func newParamTypeDetector(route *option.Route, meta *option.ViewMeta, hints option.ParameterHints) *paramTypeDetector {
uriParams := map[string]bool{}
paramTypes := map[string]string{}
if route != nil {
Expand All @@ -41,10 +42,11 @@ func newParamTypeDetector(route *option.Route, meta *option.ViewMeta) *paramType
paramTypes: paramTypes,
variables: map[string]bool{},
viewMeta: meta,
hints: hints.Index(),
}
}

func Parse(SQL string, route *option.Route) (*option.ViewMeta, error) {
func Parse(SQL string, route *option.Route, hints option.ParameterHints) (*option.ViewMeta, error) {
viewMeta := option.NewViewMeta()

block, err := parser.Parse([]byte(SQL))
Expand All @@ -65,12 +67,12 @@ func Parse(SQL string, route *option.Route) (*option.ViewMeta, error) {
cursor.Input[i] = ' '
}

detector := newParamTypeDetector(route, viewMeta)
detector := newParamTypeDetector(route, viewMeta, hints)
detector.implyDefaultParams(block.Statements(), true, nil, false)
viewMeta.SetVariables(detector.variables)

sqlNoVelty := removeVeltySyntax(string(from))
correctUntyped(sqlNoVelty, detector.variables, viewMeta)
detector.correctUntyped(sqlNoVelty, detector.variables, viewMeta)

viewMeta.Source = actualSource

Expand Down
6 changes: 5 additions & 1 deletion cmd/ast/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestParse(t *testing.T) {
description string
path string
uriParams map[string]bool
hints option.ParameterHints
}{
{
description: "basic",
Expand Down Expand Up @@ -53,6 +54,9 @@ func TestParse(t *testing.T) {
{
description: "param type hint",
path: "case008",
hints: option.ParameterHints{
{Parameter: "quantity", Hint: `/* {"DataType": "time.Time"} */`},
},
},
{
description: "uri params",
Expand All @@ -73,7 +77,7 @@ func TestParse(t *testing.T) {
continue
}

viewMeta, err := ast.Parse(string(inputData), &option.Route{URIParams: testcase.uriParams})
viewMeta, err := ast.Parse(string(inputData), &option.Route{URIParams: testcase.uriParams}, testcase.hints)
if !assert.Nil(t, err, testcase.description) {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ast/testdata/case008/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ AND ID = 10
#end

#if($Has.quantity)
AND quantity = $quantity /* {"DataType": "time.Time"} */
AND quantity = $quantity
AND quantity = $Unsafe.quantity
#end
76 changes: 38 additions & 38 deletions cmd/ast/testdata/case008/output.yaml
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
parameters:
- assumed: true
datatype: string
id: ID
kind: query
name: ID
required: false
- assumed: true
cardinality: Many
datatype: string
id: Columns
kind: query
multi: true
name: Columns
required: false
- datatype: time.Time
id: quantity
kind: query
name: quantity
required: false
- assumed: true
datatype: string
id: ID
kind: query
name: ID
required: false
- assumed: true
cardinality: Many
datatype: string
id: Columns
kind: query
multi: true
name: Columns
required: false
- datatype: time.Time
id: quantity
kind: query
name: quantity
required: false
source: |
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#set($valueSet = 123)
#if($Has.ID)
AND ID = 10
#end
#foreach($column in $Columns)
$column.Id
#end
#if($Has.quantity)
AND quantity = $quantity /* {"DataType": "time.Time"} */
AND quantity = $Unsafe.quantity
#end
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#set($valueSet = 123)
#if($Has.ID)
AND ID = 10
#end
#foreach($column in $Columns)
$column.Id
#end
#if($Has.quantity)
AND quantity = $quantity
AND quantity = $Unsafe.quantity
#end
14 changes: 13 additions & 1 deletion cmd/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func TestRun(t *testing.T) {
viewURL: "/v1/api/meta/view/dev/status",
dataMethod: http.MethodGet,
},
{
description: "AsInts codec",
URI: "case013_ints_codec",
args: []string{
"-N=eventTypes",
"-D=sqlite3",
"-A=/tmp/datly/generator/db.db",
"-X=testdata/case013_ints_codec/update.sql",
},
viewURL: "/v1/api/meta/view/dev/status",
dataMethod: http.MethodGet,
},
{
description: "set view param",
URI: "case012_set_view_param",
Expand All @@ -174,7 +186,7 @@ func TestRun(t *testing.T) {

loader := afs.New()
//for i, testCase := range testCases[len(testCases)-1:] {
for i, testCase := range testCases[:len(testCases)-1] {
for i, testCase := range testCases[11:12] {
mem.ResetSingleton()
gateway.ResetSingleton()
tests.LogHeader(fmt.Sprintf("Running testcase: %v\n", i))
Expand Down
1 change: 1 addition & 0 deletions cmd/option/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type (
DataType string `json:",omitempty" yaml:",omitempty"`
Repeated bool `json:",omitempty" yaml:",omitempty"`
ExpectReturned *int `json:",omitempty" yaml:",omitempty"`
Codec string `json:",omitempty" yaml:",omitempty"`
FullName string `json:"-" yaml:"-"`
Assumed bool `json:",omitempty" yaml:",omitempty"`
Typer Typer `json:",omitempty" yaml:",omitempty"`
Expand Down
8 changes: 7 additions & 1 deletion cmd/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,14 @@ func (s *serverBuilder) buildSQLSource(aView *view.View, table *option2.Table) e
}

func convertMetaParameter(param *option2.Parameter) *view.Parameter {
var aCodec *view.Codec
if param.Codec != "" {
aCodec = &view.Codec{Reference: shared.Reference{Ref: param.Codec}}
}

return &view.Parameter{
Name: param.Id,
Name: param.Id,
Codec: aCodec,
Schema: &view.Schema{
DataType: param.DataType,
Cardinality: param.Cardinality,
Expand Down
4 changes: 2 additions & 2 deletions cmd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *serverBuilder) updateRouteSQLMeta(ctx context.Context, route *option.Ro
}

func (s *serverBuilder) updateRouteInExecMode(ctx context.Context, route *option.Route, SQL string) error {
sqlExecModeView, err := ast.Parse(SQL, route)
sqlExecModeView, err := ast.Parse(SQL, route, route.ParameterHints)
if err != nil {
return err
}
Expand All @@ -73,7 +73,7 @@ func (s *serverBuilder) updateRouteInReadMode(route *option.Route, SQL string) e
rData := &option.ReadData{}
var err error

rData.Table, rData.DataViewParams, err = ParseSQLx(SQL, route)
rData.Table, rData.DataViewParams, err = ParseSQLx(SQL, route, route.ParameterHints)
if err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"strings"
)

func ParseSQLx(SQL string, routeOpt *option.Route) (*option.Table, map[string]*option.TableParam, error) {
func ParseSQLx(SQL string, routeOpt *option.Route, parameterHints option.ParameterHints) (*option.Table, map[string]*option.TableParam, error) {
aQuery, err := parser.ParseQuery(SQL)
if aQuery == nil {
return nil, nil, err
}

var tables = map[string]*option.Table{}
table, err := buildTable(aQuery.From.X, routeOpt)
table, err := buildTable(aQuery.From.X, routeOpt, parameterHints)
if err != nil {
return nil, nil, err
}
Expand All @@ -37,21 +37,21 @@ func ParseSQLx(SQL string, routeOpt *option.Route) (*option.Table, map[string]*o

if len(aQuery.Joins) > 0 {
for _, join := range aQuery.Joins {
if err := processJoin(join, tables, table.Columns, dataParameters, routeOpt); err != nil {
if err := processJoin(join, tables, table.Columns, dataParameters, routeOpt, parameterHints); err != nil {
return nil, nil, err
}
}
}
return table, dataParameters, nil
}

func buildTable(x node.Node, routeOpt *option.Route) (*option.Table, error) {
func buildTable(x node.Node, routeOpt *option.Route, parameterHints option.ParameterHints) (*option.Table, error) {
//var err error
table := &option.Table{}
switch actual := x.(type) {
case *expr.Raw:
table.SQL = strings.Trim(actual.Raw, "()")
if err := UpdateTableSettings(table, routeOpt); err != nil {
if err := UpdateTableSettings(table, routeOpt, parameterHints); err != nil {
return table, err
}

Expand All @@ -62,7 +62,7 @@ func buildTable(x node.Node, routeOpt *option.Route) (*option.Table, error) {
return table, nil
}

func UpdateTableSettings(table *option.Table, routeOpt *option.Route) error {
func UpdateTableSettings(table *option.Table, routeOpt *option.Route, parameterHints option.ParameterHints) error {
innerSQL, paramsExprs := ast.ExtractCondBlock(table.SQL)
innerQuery, err := parser.ParseQuery(innerSQL)
fmt.Printf("innerSQL %v %v\n", table.SQL, err)
Expand All @@ -85,7 +85,7 @@ func UpdateTableSettings(table *option.Table, routeOpt *option.Route) error {
table.InnerAlias = innerQuery.From.Alias
}

table.ViewMeta, err = ast.Parse(table.SQL, routeOpt)
table.ViewMeta, err = ast.Parse(table.SQL, routeOpt, parameterHints)
if err != nil {
return err
}
Expand Down Expand Up @@ -132,8 +132,8 @@ func appendParamExpr(x node.Node, op string, y node.Node, list *[]string) {
}
}

func processJoin(join *query.Join, tables map[string]*option.Table, outerColumn option.Columns, dataParameters map[string]*option.TableParam, routeOpt *option.Route) error {
relTable, err := buildTable(join.With, routeOpt)
func processJoin(join *query.Join, tables map[string]*option.Table, outerColumn option.Columns, dataParameters map[string]*option.TableParam, routeOpt *option.Route, parameterHints option.ParameterHints) error {
relTable, err := buildTable(join.With, routeOpt, parameterHints)
if err != nil {
return err
}
Expand Down
34 changes: 34 additions & 0 deletions cmd/testdata/case013_ints_codec/populate/event_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

[
{},
{
"id": 1,
"name" : "type 1",
"account_id": 33
},
{
"id": 11,
"name" : "type 2",
"account_id": 33
},
{
"id": 111,
"name" : "type 3",
"account_id": 36
},
{
"id": 4,
"name" : "type 4",
"account_id": 36
},
{
"id": 5,
"name" : "type 5",
"account_id": 37
},
{
"id": 2,
"name" : "type 6",
"account_id": 37
}
]
Loading

0 comments on commit 0e266f2

Please sign in to comment.