Skip to content

Commit

Permalink
added logger
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Aug 15, 2022
1 parent e4c7859 commit c167c06
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 45 deletions.
2 changes: 0 additions & 2 deletions executor/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package executor
import (
"context"
"database/sql"
"fmt"
"github.com/viant/datly/shared"
"sync"
)
Expand Down Expand Up @@ -59,7 +58,6 @@ func (e *Executor) execData(ctx context.Context, wg *sync.WaitGroup, tx *sql.Tx,
}

func (e *Executor) executeStatement(ctx context.Context, tx *sql.Tx, stmt *SQLStatment) error {
fmt.Printf("executing: %v %v\n", stmt.SQL, stmt.Args)
_, err := tx.ExecContext(ctx, stmt.SQL, stmt.Args...)
return err
}
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ require (
github.com/viant/scy v0.2.1-0.20220812183656-68209588b73e
github.com/viant/sqlx v0.0.0-20220811174716-1b6dd6d967a2
github.com/viant/toolbox v0.34.6-0.20220630003140-fb2bf82657c1
github.com/viant/velty v0.1.1-0.20220815170357-0518dfc48b9d
github.com/viant/velty v0.1.1-0.20220815184724-5837b86c2a72
github.com/viant/xunsafe v0.8.1-0.20220609224231-1d3e1fcf7bb6
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb
google.golang.org/api v0.84.0
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1
)

replace github.com/viant/sqlx v0.0.0-20220811162450-fda00ea81681 => /Users/awitas/go/src/github.com/viant/sqlx
9 changes: 5 additions & 4 deletions logger/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/viant/velty/est/op"
"github.com/viant/xunsafe"
"reflect"
"strings"
)

var stringType = reflect.TypeOf("")
Expand All @@ -17,7 +18,7 @@ func (p Printer) Discover(aFunc interface{}) (func(operands []*op.Operand, state
switch actual := aFunc.(type) {
case func(_ Printer, args ...interface{}) string:
return func(operands []*op.Operand, state *est.State) (interface{}, error) {
return actual(p, p.asInterfaces(operands, state)), nil
return actual(p, p.asInterfaces(operands[1:], state)), nil
}, stringType, true

case func(_ Printer, message string, args ...interface{}) string:
Expand All @@ -26,8 +27,8 @@ func (p Printer) Discover(aFunc interface{}) (func(operands []*op.Operand, state
return nil, fmt.Errorf("expected to get 1 or more arguments but got %v", len(operands))
}

format := *(*string)(operands[0].Exec(state))
args := p.asInterfaces(operands[1:], state)
format := *(*string)(operands[1].Exec(state))
args := p.asInterfaces(operands[2:], state)

return actual(p, format, args...), nil
}, stringType, true
Expand Down Expand Up @@ -55,6 +56,6 @@ func (p Printer) Println(args ...interface{}) string {
}

func (p Printer) Printf(format string, args ...interface{}) string {
fmt.Printf(format, args...)
fmt.Printf(strings.ReplaceAll(format, "\\n", "\n"), args...)
return ""
}
13 changes: 12 additions & 1 deletion router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ func TestRouter(t *testing.T) {
visitors: map[string]codec.LifecycleVisitor{},
afterInsertUri: "/api/events?_criteria=Quantity=40",
afterInsertMethod: http.MethodGet,
requestBody: `{"ID": [1,10,103], "Quantity": 40}`,
afterInsertExpected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":40,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":40,"UserId":2},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":40,"UserId":3}]`,
requestBody: `{"ID": [1,10,103], "Quantity": 40}`,
},
{
description: "extract values from Request Body",
Expand Down Expand Up @@ -684,6 +684,17 @@ func TestRouter(t *testing.T) {
requestBody: `[1,10,103]`,
expected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":33.23432374000549,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":21.957962334156036,"UserId":2},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":5.084940046072006,"UserId":3}]`,
},
{
description: "executor with param slice",
resourceURI: "035_logger",
uri: "/api/events",
method: http.MethodPost,
visitors: map[string]codec.LifecycleVisitor{},
requestBody: `{"ID": [1,10,103], "Quantity": 0}`,
afterInsertUri: "/api/events",
afterInsertMethod: http.MethodGet,
afterInsertExpected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":0,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":0,"UserId":2},{"Id":100,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":5.084940046072006,"UserId":3},{"Id":101,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":5.084940046072006,"UserId":3},{"Id":102,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":5.084940046072006,"UserId":3},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":0,"UserId":3}]`,
},
}

//for i, tCase := range testcases[len(testcases)-1:] {
Expand Down
4 changes: 1 addition & 3 deletions router/testdata/031_multiple_execs/resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ Routes:
$errors.RegisterError("invalid status")
#else
#set($index = 0)
#foreach($ID in $Unsafe.Body.ID)
UPDATE events SET quantity = $criteria.Add($index, $Unsafe.Body.Quantity) WHERE id = $criteria.Add($index, $ID);
#set($index = $index+1)
UPDATE events SET quantity = $criteria.Add(0, $Unsafe.Body.Quantity) WHERE id = $criteria.Add(0, $ID);
#end
#end
Expand Down
45 changes: 45 additions & 0 deletions router/testdata/035_logger/populate/events.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{},
{
"id": 1,
"event_type_id": 2,
"quantity": 33.23432374000549,
"timestamp": "2019-03-11 02:20:33",
"user_id": 1
},
{
"id": 10,
"event_type_id": 11,
"quantity": 21.957962334156036,
"timestamp": "2019-03-15 12:07:33",
"user_id": 2
},
{
"id": 100,
"event_type_id": 111,
"quantity": 5.084940046072006,
"timestamp": "2019-04-10 05:15:33",
"user_id": 3
},
{
"id": 101,
"event_type_id": 111,
"quantity": 5.084940046072006,
"timestamp": "2019-04-10 05:15:33",
"user_id": 3
},
{
"id": 102,
"event_type_id": 111,
"quantity": 5.084940046072006,
"timestamp": "2019-04-10 05:15:33",
"user_id": 3
},
{
"id": 103,
"event_type_id": 111,
"quantity": 5.084940046072006,
"timestamp": "2019-04-10 05:15:33",
"user_id": 3
}
]
47 changes: 47 additions & 0 deletions router/testdata/035_logger/resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Routes:
- URI: "/api/events"
Method: POST
Service: Executor

View:
Ref: events_ref
Name: events
Mode: SQLExec
Template:
Parameters:
- Name: Body
In:
Kind: body
Schema:
DataType: Body
Source: '
#foreach($ID in $Unsafe.Body.ID)
UPDATE events SET quantity = $Unsafe.Body.Quantity WHERE id = $ID;
$logger.Printf("executing update stmt with params: [%v,%v]\n", $Unsafe.Body.Quantity, $ID)
#end
'
- URI: "/api/events"
Method: GET
View:
Ref: events_ref
Name: events

Resource:
Views:
- Name: events_ref
Connector:
Ref: db
Table: events
Selector:
Constraints:
Criteria: true
Filterable: [ '*' ]

Types:
- Name: Body
DataType: 'struct {ID []int; Quantity float64 }'

Connectors:
- Name: db
Driver: sqlite3
DSN: "./testdata/db/db.db"
44 changes: 12 additions & 32 deletions view/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (

type CriteriaSanitizer struct {
Columns ColumnIndex
ParamsGroup [][]interface{}
ParamsGroup []interface{}
Mock bool
GroupCounter int
PlaceholderCounter int
sliceIndex map[reflect.Type]*xunsafe.Slice
}
Expand All @@ -21,6 +20,10 @@ func (p *CriteriaSanitizer) AsBinding(value interface{}) string {
return p.Add(0, value)
}

func (p *CriteriaSanitizer) AppendBinding(value interface{}) string {
return p.Add(0, value)
}

func (p *CriteriaSanitizer) AsColumn(columnName string) (string, error) {
lookup, err := p.Columns.Lookup(columnName)
if err != nil {
Expand All @@ -30,7 +33,7 @@ func (p *CriteriaSanitizer) AsColumn(columnName string) (string, error) {
return lookup.Name, nil
}

func (p *CriteriaSanitizer) Add(at int, value interface{}) string {
func (p *CriteriaSanitizer) Add(_ int, value interface{}) string {
if value == nil {
return ""
}
Expand All @@ -40,8 +43,7 @@ func (p *CriteriaSanitizer) Add(at int, value interface{}) string {
return ""
}

p.growIfNeeded(at)
p.ParamsGroup[at] = append(p.ParamsGroup[at], valueCopy...)
p.ParamsGroup = append(p.ParamsGroup, valueCopy...)
return expanded
}

Expand Down Expand Up @@ -85,25 +87,8 @@ func (p *CriteriaSanitizer) copyAndExpandSlice(valueType reflect.Type, valuePtr
}
}

func (p *CriteriaSanitizer) growIfNeeded(at int) {
if len(p.ParamsGroup) > at {
return
}

newParams := make([][]interface{}, at+1)
for i, group := range p.ParamsGroup {
newParams[i] = append(newParams[i], group...)
}

p.ParamsGroup = newParams
}

func (p *CriteriaSanitizer) At(i int) []interface{} {
if len(p.ParamsGroup) <= i {
return []interface{}{}
}

return p.ParamsGroup[i]
func (p *CriteriaSanitizer) At(_ int) []interface{} {
return p.ParamsGroup
}

func (p *CriteriaSanitizer) Next() (interface{}, error) {
Expand All @@ -112,18 +97,13 @@ func (p *CriteriaSanitizer) Next() (interface{}, error) {
}

for {
if p.GroupCounter >= len(p.ParamsGroup) {
return nil, fmt.Errorf("not found next binding variable")
}

if p.PlaceholderCounter < len(p.ParamsGroup[p.GroupCounter]) {
if p.PlaceholderCounter < len(p.ParamsGroup) {
index := p.PlaceholderCounter
p.PlaceholderCounter++
return p.ParamsGroup[p.GroupCounter][index], nil
return p.ParamsGroup[index], nil
}

p.GroupCounter++
p.PlaceholderCounter = 0
return nil, fmt.Errorf("expected to got binding parameter, but noone was found")
}
}

Expand Down

0 comments on commit c167c06

Please sign in to comment.