-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sub_select is not properly parenthesized under some conditions #79
Comments
Any update on this / any workarounds? |
Patches warmly welcomed! |
I can try and look into it whenever I next have some free time. It's just going to take me a long time to understand the internals of esqueleto, so I was hoping it would be a quick fix for someone who already understood the library. |
Opened #126 with a failing test case. |
In the interim, we're solving this in our work codebase with: -- | The extra parens are needed when the argument is a `SELECT` statement.
unsafeSqlFunctionWithExtraParens
:: UnsafeSqlFunctionArgument a
=> TLB.Builder
-> a
-> SqlExpr (Value b)
unsafeSqlFunctionWithExtraParens name' arg =
ERaw Never $ \info ->
let (argsTLB, argsVals) =
uncommas' $ map (\(ERaw _ f) -> f info) $ toArgList arg
in (name' <> parens (parens argsTLB), argsVals)
-- | If the argument is a 'sub_select' then you have to use this, otherwise it
-- goes boom.
sumSelect :: (Num a, Num b) => SqlExpr (Value a) -> SqlExpr (Value (Maybe b))
sumSelect = unsafeSqlFunctionWithExtraParens "SUM" |
I think this might be due to the parens field of makeWhere :: IdentInfo -> WhereClause -> (TLB.Builder, [PersistValue])
makeWhere _ NoWhere = mempty
makeWhere info (Where v) = first ("\nWHERE " <>) $ x info
where
x =
case v of
ERaw _ f -> f
EAliasedValue i _ -> aliasedValueIdentToRawSql i
EValueReference i i' -> valueReferenceToRawSql i i'
ECompositeKey _ -> throw (CompositeKeyError -> EsqueletoError
CompositeKeyErr MakeWhereError) |
@hdgarrood that sounds plausible |
Having looked a little bit more closely I think the issue I'm running into might be different from this one. |
It should be possible to avoid sub_select through the use of lateral joins (or just regular subquery joins if you don't need to correlate the subquery) EDIT: oh didn't notice that this issue is in the insertSelect. Don't think .Experimental works with that. |
Specifically the following fails with
syntax error at or near "SELECT"
, because the inner sub_select is not properly enclosed in parenthesis.This is with:
The text was updated successfully, but these errors were encountered: