|
| 1 | +/** |
| 2 | + * Copyright 2018-2020 Aaron Sherber |
| 3 | +
|
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using Microsoft.FSharp.Core; |
| 18 | +using Microsoft.FSharp.Quotations; |
| 19 | +using PetaPoco; |
| 20 | +using PetaPoco.Core; |
| 21 | +using StaTypPocoQueries.Core; |
| 22 | +using System; |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.Linq; |
| 25 | +using System.Linq.Expressions; |
| 26 | +using System.Reflection; |
| 27 | +using System.Text; |
| 28 | +using System.Threading.Tasks; |
| 29 | + |
| 30 | +namespace StaTypPocoQueries.PetaPoco |
| 31 | +{ |
| 32 | + public class SqlTranslator |
| 33 | + { |
| 34 | + private readonly IDatabase _db; |
| 35 | + |
| 36 | + public SqlTranslator(IDatabase db) |
| 37 | + { |
| 38 | + _db = db; |
| 39 | + } |
| 40 | + |
| 41 | + public Sql Translate<T>(Expression<Func<T, bool>> query) |
| 42 | + { |
| 43 | + var translated = ExpressionToSql.Translate( |
| 44 | + quoter: new DatabaseQuoter(_db), |
| 45 | + conditions: query, |
| 46 | + includeWhere: true, |
| 47 | + customNameExtractor: ExtractColumnName, |
| 48 | + customParameterValueMap: InvokeValueConverter); |
| 49 | + |
| 50 | + return new Sql(translated.Item1, translated.Item2); |
| 51 | + } |
| 52 | + |
| 53 | + public Sql Translate<T>(FSharpExpr<FSharpFunc<T, bool>> query) |
| 54 | + { |
| 55 | + var translated = ExpressionToSql.Translate( |
| 56 | + quoter: new DatabaseQuoter(_db), |
| 57 | + conditions: query, |
| 58 | + includeWhere: true, |
| 59 | + customNameExtractor: FsExtractColumnName, |
| 60 | + customParameterValueMap: FsInvokeValueConverter); |
| 61 | + |
| 62 | + return new Sql(translated.Item1, translated.Item2); |
| 63 | + } |
| 64 | + |
| 65 | + private string ExtractColumnName(MemberInfo mi) |
| 66 | + { |
| 67 | + if (mi is PropertyInfo pi) |
| 68 | + { |
| 69 | + var pd = PocoData.ForType(mi.DeclaringType, _db.DefaultMapper); |
| 70 | + return pd.GetColumnName(pi.Name); |
| 71 | + } |
| 72 | + else |
| 73 | + return mi.Name; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private object InvokeValueConverter(PropertyInfo pi, object input) |
| 78 | + { |
| 79 | + var converter = _db.DefaultMapper.GetToDbConverter(pi); |
| 80 | + return converter == null ? input : converter(input); |
| 81 | + } |
| 82 | + |
| 83 | + private FSharpFunc<MemberInfo, string> FsExtractColumnName |
| 84 | + => ExpressionToSql.AsFsFunc<MemberInfo, string>(ExtractColumnName); |
| 85 | + |
| 86 | + // Helpful precis: https://codeblog.jonskeet.uk/2012/01/30/currying-vs-partial-function-application/ |
| 87 | + private FSharpFunc<PropertyInfo, FSharpFunc<object, object>> FsInvokeValueConverter |
| 88 | + => ExpressionToSql.AsFsFunc<PropertyInfo, FSharpFunc<object, object>>( |
| 89 | + pi => ExpressionToSql.AsFsFunc<object, object>( |
| 90 | + input => InvokeValueConverter(pi, input) |
| 91 | + ) |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments