Esempio n. 1
0
            protected override SqlStatement ProcessQuery(SqlStatement statement, EvaluationContext context)
            {
                if (statement.IsInsert() && statement.RequireInsertClause().Into !.Name == "Parent")
                {
                    var expr =
                        new QueryVisitor().Find(statement.RequireInsertClause(), e =>
                    {
                        if (e.ElementType == QueryElementType.SetExpression)
                        {
                            var se = (SqlSetExpression)e;
                            return(((SqlField)se.Column).Name == "ParentID");
                        }

                        return(false);
                    }) as SqlSetExpression;

                    if (expr != null && expr.Expression !.TryEvaluateExpression(context, out var expressionValue))
                    {
                        var value = ConvertTo <int> .From(expressionValue);

                        if (value == 555)
                        {
                            var tableName = "Parent1";
                            var dic       = new Dictionary <IQueryElement, IQueryElement>();

                            statement = ConvertVisitor.Convert(statement, (v, e) =>
                            {
                                if (e.ElementType == QueryElementType.SqlTable)
                                {
                                    var oldTable = (SqlTable)e;

                                    if (oldTable.Name == "Parent")
                                    {
                                        var newTable = new SqlTable(oldTable)
                                        {
                                            Name = tableName, PhysicalName = tableName
                                        };

                                        foreach (var field in oldTable.Fields)
                                        {
                                            dic.Add(field, newTable[field.Name] ?? throw new InvalidOperationException());
                                        }

                                        return(newTable);
                                    }
                                }

                                IQueryElement?ex;
                                return(dic.TryGetValue(e, out ex) ? ex : e);
                            });
                        }
                    }

                    return(statement);
                }

                return(statement);
            }
Esempio n. 2
0
            protected override SqlStatement ProcessQuery(SqlStatement statement)
            {
                if (statement.IsInsert() && statement.RequireInsertClause().Into !.Name == "Parent")
                {
                    var expr =
                        new QueryVisitor().Find(statement.RequireInsertClause(), e =>
                    {
                        if (e.ElementType == QueryElementType.SetExpression)
                        {
                            var se = (SqlSetExpression)e;
                            return(((SqlField)se.Column).Name == "ParentID");
                        }

                        return(false);
                    }) as SqlSetExpression;

                    if (expr != null)
                    {
                        var value = ConvertTo <int> .From(((IValueContainer)expr.Expression !).Value);

                        if (value == 555)
                        {
                            var tableName = "Parent1";
                            var dic       = new Dictionary <IQueryElement, IQueryElement>();

                            statement = new QueryVisitor().Convert(statement, e =>
                            {
                                if (e.ElementType == QueryElementType.SqlTable)
                                {
                                    var oldTable = (SqlTable)e;

                                    if (oldTable.Name == "Parent")
                                    {
                                        var newTable = new SqlTable(oldTable)
                                        {
                                            Name = tableName, PhysicalName = tableName
                                        };

                                        foreach (var field in oldTable.Fields.Values)
                                        {
                                            dic.Add(field, newTable.Fields[field.Name]);
                                        }

                                        return(newTable);
                                    }
                                }

                                IQueryElement ex;
                                return(dic.TryGetValue(e, out ex) ? ex : e);
                            });
                        }
                    }

                    return(statement);
                }

                return(statement);
            }
Esempio n. 3
0
        public override SqlStatement TransformStatement(SqlStatement statement)
        {
            if (statement.IsUpdate() || statement.IsInsert() || statement.IsDelete())
            {
                statement = ReplaceTakeSkipWithRowNum(statement, false);
            }

            switch (statement.QueryType)
            {
            case QueryType.Delete: statement = GetAlternativeDelete((SqlDeleteStatement)statement); break;

            case QueryType.Update: statement = GetAlternativeUpdate((SqlUpdateStatement)statement); break;
            }

            return(statement);
        }
Esempio n. 4
0
 void CorrectInsertParameters(SqlStatement statement)
 {
     //SlqCe do not support parameters in columns for insert
     //
     if (statement.IsInsert())
     {
         var query = statement.SelectQuery;
         if (query != null)
         {
             foreach (var column in query.Select.Columns)
             {
                 if (column.Expression is SqlParameter parameter)
                 {
                     parameter.IsQueryParameter = false;
                 }
             }
         }
     }
 }
Esempio n. 5
0
 protected override SqlStatement ProcessQuery(SqlStatement statement, EvaluationContext context)
 {
     if (statement.IsInsert() && statement.RequireInsertClause().Into !.TableName.Name == "Parent")
     {
         var expr =
             statement.RequireInsertClause().Find(static e =>
Esempio n. 6
0
 public static bool IsInsertWithIdentity(this SqlStatement statement)
 {
     return(statement.IsInsert() && ((SelectQuery)statement).Insert.WithIdentity);
 }
Esempio n. 7
0
            protected override SqlStatement ProcessQuery(SqlStatement statement)
            {
                #region Update

                if (statement.QueryType == QueryType.Update)
                {
                    var query  = (SelectQuery)statement;
                    var source = query.From.Tables[0].Source as SqlTable;
                    if (source == null)
                    {
                        return(statement);
                    }

                    var descriptor = MappingSchema.GetEntityDescriptor(source.ObjectType);
                    if (descriptor == null)
                    {
                        return(statement);
                    }

                    var rowVersion = descriptor.Columns.SingleOrDefault(c => c.MemberAccessor.GetAttribute <RowVersionAttribute>() != null);
                    if (rowVersion == null)
                    {
                        return(statement);
                    }

                    var newQuery = Clone(query);
                    source = newQuery.From.Tables[0].Source as SqlTable;
                    var field = source.Fields[rowVersion.ColumnName];

                    // get real value of RowVersion
                    var updateColumn = newQuery.Update.Items.FirstOrDefault(ui => ui.Column is SqlField && ((SqlField)ui.Column).Equals(field));
                    if (updateColumn == null)
                    {
                        updateColumn = new SelectQuery.SetExpression(field, field);
                        newQuery.Update.Items.Add(updateColumn);
                    }

                    updateColumn.Expression = new SqlBinaryExpression(typeof(int), field, "+", new SqlValue(1));

                    return(newQuery);
                }

                #endregion Update

                #region Insert

                else if (statement.IsInsert())
                {
                    var query      = (SelectQuery)statement;
                    var source     = query.Insert.Into;
                    var descriptor = MappingSchema.GetEntityDescriptor(source.ObjectType);
                    var rowVersion = descriptor.Columns.SingleOrDefault(c => c.MemberAccessor.GetAttribute <RowVersionAttribute>() != null);

                    if (rowVersion == null)
                    {
                        return(statement);
                    }


                    var newQuery = Clone(query);

                    var field = newQuery.Insert.Into[rowVersion.ColumnName];

                    var versionColumn = (from i in newQuery.Insert.Items
                                         let f = i.Column as SqlField
                                                 where f != null && f.PhysicalName == field.PhysicalName
                                                 select i).FirstOrDefault();

                    // if we do not try to insert version, lets suppose it should be done in database
                    if (versionColumn != null)
                    {
                        versionColumn.Expression = new SqlValue(1);
                        return(newQuery);
                    }
                }
                #endregion Insert

                return(statement);
            }