Exemplo n.º 1
0
        internal void AddWhereClause <T>(IList <Expression <Func <T, bool> > > whereClauses, StringBuilder sql, AutoNamingDynamicParameters parameters, ref FetchNode rootNode)
        {
            var whereClauseWriter = new WhereClauseWriter(this.Dialect, this.Configuration);
            var result            = whereClauseWriter.GenerateSql(whereClauses, rootNode, parameters);

            if (result.Sql.Length > 0)
            {
                sql.Append(result.Sql);
            }

            rootNode = result.FetchTree;
        }
Exemplo n.º 2
0
        private void AppendPredicates <T>(IEnumerable <Expression <Func <T, bool> > > predicates, StringBuilder sql, DynamicParameters parameters)
        {
            var predicateArray = predicates as Expression <Func <T, bool> >[] ?? predicates.ToArray();

            if (!predicateArray.Any())
            {
                return;
            }

            var whereClauseWriter = new WhereClauseWriter(this.Dialect, this.Configuration);
            var whereResult       = whereClauseWriter.GenerateSql(predicateArray, null);

            if (whereResult.FetchTree != null && whereResult.FetchTree.Children.Any())
            {
                throw new NotImplementedException("Dashing does not currently support where clause across tables in a delete");
            }

            sql.Append(whereResult.Sql);
            parameters.AddDynamicParams(whereResult.Parameters);
        }
Exemplo n.º 3
0
        public SqlWriterResult GenerateBulkSql <T>(Action <T> updateAction, IEnumerable <Expression <Func <T, bool> > > predicates) where T : class, new()
        {
            var sql        = new StringBuilder();
            var parameters = new DynamicParameters();
            var map        = this.Configuration.GetMap <T>();

            // run the update
            var entity = new T();

            updateAction(entity);

            // find the set properties
            var setLogger = (ISetLogger)entity;
            var setProps  = setLogger.GetSetProperties();

            if (!setProps.Any())
            {
                return(new SqlWriterResult(string.Empty, parameters));
            }

            sql.Append("update ");
            this.Dialect.AppendQuotedTableName(sql, map);
            sql.Append(" set ");

            foreach (var updatedProp in setProps)
            {
                var column = map.Columns[updatedProp];
                this.Dialect.AppendQuotedName(sql, column.DbName);
                var paramName     = "@" + updatedProp;
                var propertyValue = map.GetColumnValue(entity, column);
                if (propertyValue == null)
                {
                    parameters.Add(paramName, null);
                }
                else
                {
                    parameters.Add(paramName, this.GetValueOrPrimaryKey(column, propertyValue));
                }

                sql.Append(" = ");
                sql.Append(paramName);
                sql.Append(", ");
            }

            sql.Remove(sql.Length - 2, 2);

            if (predicates != null && predicates.Any())
            {
                var whereClauseWriter = new WhereClauseWriter(this.Dialect, this.Configuration);
                var whereResult       = whereClauseWriter.GenerateSql(predicates, null);
                if (whereResult.FetchTree != null && whereResult.FetchTree.Children.Any())
                {
                    throw new NotImplementedException("Dashing does not currently support where clause across tables in an update");
                }

                parameters.AddDynamicParams(whereResult.Parameters);
                sql.Append(whereResult.Sql);
            }

            return(new SqlWriterResult(sql.ToString(), parameters));
        }