/// <summary> /// /// </summary> static string OnConflictDoUpdate(OnConflictDoUpdateClause doUpdate, ParamMode paramMode, SqlParamDic paramDic, string origTableName) { var b = new StringBuilder(); b.AppendLine("DO UPDATE"); b.AppendLine("SET"); var exprAlias = new[] { new SqlFromList.ExprStrRawSql(doUpdate.Set.Parameters[0], "EXCLUDED"), new SqlFromList.ExprStrRawSql(doUpdate.Set.Parameters[1], $"\"{origTableName}\""), }; var setSql = SqlUpdate.SetToSql(doUpdate.Set.Body, paramMode, paramDic, exprAlias); b.Append(SqlSelect.TabStr(setSql)); if (doUpdate.Where != null) { b.AppendLine(); var pars = new SqlExprParams(null, null, false, "", new SqlFromList.ExprStrRawSql[0], paramMode, paramDic); var whereSql = SqlExpression.ExprToSql(doUpdate.Where.Body, pars, true); b.Append(whereSql); } return(b.ToString()); }
static void ConvertWhere(StringBuilder b, DeleteClause clause, ParamMode paramMode, SqlParamDic paramDic) { var whereParms = clause.Where.Parameters; var tableParam = whereParms[0]; var usingParam = whereParms[1]; var replace = new SqlFromList.ExprStrRawSql[0]; var pars = new SqlExprParams(tableParam, null, false, null, replace, paramMode, paramDic); var whereSql = SqlExpression.ExprToSql(clause.Where.Body, pars, true); b.Append("WHERE "); b.Append(whereSql); }
/// <summary> /// Convierte la cláusura ON CONFLICT /// </summary> static string OnConflict(OnConflictClause onConf, ParamMode paramMode, SqlParamDic paramDic, string tableName) { var b = new StringBuilder(); //fromAlias es null ya que en la expresion de indice de ON CONFLICT no se permiten //los identificadores de las tablas, sólo se permiten los nombres tal cual de las columnas var pars = new SqlExprParams(null, null, false, null, new SqlFromList.ExprStrRawSql[0], paramMode, paramDic); var indexExpr = onConf .IndexExpressions .Select(x => SqlExpression.ExprToSql(x.Body, pars.ReplaceSelectParams(x.Parameters[0], null), true)) ; b.Append("ON CONFLICT "); if (indexExpr.Any()) { b.Append("("); b.Append(string.Join(", ", indexExpr)); b.Append(") "); } if (onConf.Where != null) { var whereSql = SqlExpression.ExprToSql(onConf.Where.Body, pars.ReplaceSelectParams(onConf.Where.Parameters[0], null), true); b.Append("WHERE "); b.Append(whereSql); } if (onConf.DoUpdate == null) { //Si DoUpdate es null se considera que es DO NOTHING b.Append("DO NOTHING"); } else { b.Append(OnConflictDoUpdate(onConf.DoUpdate, paramMode, paramDic, tableName)); } return(b.ToString()); }
/// <summary> /// La regla que se aplica a las llamadas Atom(Raw(x)), lo que hace es convertir las llamadas ToSql del Atom(Raw(x)) /// </summary> public static IEnumerable <RewriteRule> AtomRawRule(SqlExprParams pars) { var deferredToSqlRule = RewriteRule.Create( "deferredToSql", (RewriteTypes.C1 x) => SqlFunctions.ToSql <RewriteTypes.C1>(x), null, null, (match, expr, visit) => Expression.Call( typeof(SqlExpression), nameof(SqlExpression.ExprToSql), new Type[0], Expression.Constant(match.Args[0]), Expression.Constant(pars), Expression.Constant(true))); var toSqlRule = RewriteRule.Create( "toSql", () => RewriteSpecial.Call <string>(typeof(SqlFunctions), nameof(ToSql)), null, null, (match, expr, visit) => Expression.Constant(SqlExpression.ExprToSql(((MethodCallExpression)expr).Arguments[0], pars, true))); var toSelectBodySqlRule = RewriteRule.Create( "toSelectBodySqlRule", () => RewriteSpecial.Call <string>(typeof(SqlFunctions), nameof(ToSelectBodySql)), null, null, (match, expr, visit) => Expression.Constant( SqlSelect.SelectExprToStr(SqlSelect.SelectBodyToStr(((MethodCallExpression)expr).Arguments[0], pars).Values) ) ); var windowToSqlRule = RewriteRule.Create( "windowToSql", (ISqlWindow a) => WindowToSql(a), null, null, (match, expr, visit) => Expression.Constant(SqlCalls.WindowToSql(match.Args[0])) ); var toSqlRules = new[] { toSelectBodySqlRule, toSqlRule, windowToSqlRule }; Func <Expression, Expression> applySqlRule = (Expression ex) => new RewriteVisitor(toSqlRules, ExcludeFromRewrite).Visit(ex); var atomRawRule = RewriteRule.Create( "executeAtomRaw", (string x) => RewriteSpecial.Atom(Sql.Raw <RewriteTypes.C1>(RewriteSpecial.NotConstant(x))), null, null, (match, expr, visit) => { var arg = match.Args[0]; var applySql = applySqlRule(arg); if (applySql == arg) { return(expr); } var type = match.Types[typeof(RewriteTypes.C1)]; var value = ExprEval.EvalExpr <string>(applySql).Value; var ret = Expression.Call(typeof(RewriteSpecial), nameof(RewriteSpecial.Atom), new[] { type }, Expression.Call(typeof(Sql), nameof(Sql.Raw), new[] { type }, Expression.Constant(value)) ); return(ret); }); return(new[] { atomRawRule }); }