yaacov / tree-search-language

Tree Search Language (TSL) is a wonderful search langauge.
Apache License 2.0
60 stars 11 forks source link

Use TSL to generate TSL strings? #23

Open cben opened 4 years ago

cben commented 4 years ago

One drawback of exposing an API accepting a pseudo-SQL query strings (parsed by TSL) is that it's tricky for clients to generate those strings with correct value quoting... We have some ad-hoc string-building code and I'm looking at more principled generation from something like an AST. In Go.

Is this something you'd like like TSL to support — round trip from AST to a TSL string that parses to same AST? :tanabata_tree: -> :abc: -> :tanabata_tree:

yaacov commented 4 years ago

Interesting :+1:

are you suggesting a builder that will help building tsl-tree from code, instead of parsing tsl-phrases ?

e.g. something to automate this ?

right := Node{Func: StringOp, Left: "Moki"}
left := Node{Func: StringOp, Left: "Not Moki"}

tslTreeRoot := Node(Func: EqOp, Right: right, Left: left}
cben commented 4 years ago

I'm suggesting a walker [or a flag on existing sql walker] that from the example you give above, would build the self-contained string "Moki" = "Not Moki". My understading is that currently it builds ? = ? plus the args "Moki", "Not Moki", right?

yaacov commented 4 years ago

My understading is that currently it builds ? = ? plus the args "Moki", "Not Moki", right?

yes,

so filing in the args in a string with '?' place holders sounds like for each arg do strings.Replace of ? ?

cben commented 4 years ago

Interestingly, squirrel.Expr may provide the infrastructure to "smuggle" literals into squirrel: https://github.com/Masterminds/squirrel/blob/master/expr.go#L31-L82 If any of the args supports .ToSql(), it inlines the result in place of the ?.

yaacov commented 4 years ago

sql walker return Sqlizer so it should supports .ToSql() out of the box ... !?

cben commented 4 years ago

OTOH, I don't understand how this now works:

    case tsl.StringOp:
        s = sq.Expr(n.Left.(string))

Isn't sq.Expr() supposed to take an SQL fragment (with possible placeholders)? Here you're feeding it the string as-is? Playing with the tests, but I'm not managing to break it the way I expected :+1:, so I guess it's time to call it a night :)