Open cben opened 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}
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?
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 ?
?
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 ?
.
sql walker return Sqlizer
so it should supports .ToSql()
out of the box ... !?
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 :)
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.
I first looked at squirrel but turns out it can't generate a self-contained SQL string. It only emits
?
placeholders, and returns a separte array of values, assuming you have a DB interface that safely takes separate template + args. This is good design but our API doesn't support that :grin:, it takes a single TSL string.So my next idea was using TSL itself to generate a TSL string! :bulb: Ironically, currently sql walker uses squirrel as well, emitting
?
and separate values :grin: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: