Created
April 12, 2015 06:18
-
-
Save xavierzwirtz/f9f0c9a7e8951debc137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let translate expression = | |
//QueryTranslator().Translate(expression) | |
let rec mapFun e : ExpressionResult * list<string> option = | |
let map = map(mapFun) | |
let bin (e : BinaryExpression) text = | |
Some (["("] @ map(e.Left) @ [text] @ map(e.Right) @ [")"]) | |
let result = | |
match e with | |
| Call m -> | |
let argResult = m.Arguments.Item(0) |> map | |
let lambda = (stripQuotes (m.Arguments.Item(1))) :?> LambdaExpression | |
let bodyResult = lambda.Body |> map | |
Some (["SELECT * FROM ("] @ argResult @ [ ") AS T WHERE "] @ bodyResult) | |
| Not n -> | |
Some ([" NOT "] @ map(n.Operand)) | |
| And e -> bin e "AND" | |
| Or e -> bin e "OR" | |
| Equal e -> bin e "=" | |
| NotEqual e -> bin e "<>" | |
| LessThan e -> bin e "<" | |
| LessThanOrEqual e -> bin e "<=" | |
| GreaterThan e -> bin e ">" | |
| GreaterThanOrEqual e -> bin e ">=" | |
| Constant c -> | |
let q = | |
match c.Value with | |
| :? IQueryable as v -> Some v | |
| _ -> None | |
if q <> None then | |
Some ["SELECT * FROM "; q.Value.ElementType.Name] | |
else if c.Value = null then | |
Some ["NULL"] | |
else | |
let cv = | |
match System.Type.GetTypeCode(c.Value.GetType()) with | |
| System.TypeCode.Boolean -> if (c.Value :?> bool) then "1" else "0" | |
| System.TypeCode.String -> "'" + (c.Value :?> string) + "'" | |
| System.TypeCode.Object -> failwithf "The constant for '%A' is not supported" c.Value | |
| _ -> c.Value.ToString() | |
Some [cv] | |
| MemberAccess m -> | |
if m.Expression <> null && m.Expression.NodeType = ExpressionType.Parameter then | |
Some [m.Member.Name] | |
else | |
failwithf "The member '%s' is not supported" m.Member.Name | |
| _ -> None | |
if result.IsSome then | |
ExpressionResult.Skip, result | |
else | |
ExpressionResult.Recurse, result | |
let ls = | |
expression | |
|> map(mapFun) | |
ls | |
|> String.concat("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment