예제 #1
0
        /// <summary>
        /// Use an expression visitor to select which fields to update and construct the where expression, E.g:
        ///
        ///   var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor&gt;Person&lt;());
        ///   dbCmd.UpdateOnly(new Person { FirstName = "JJ" }, ev.Update(p => p.FirstName).Where(x => x.FirstName == "Jimi"));
        ///   UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("FirstName" = 'Jimi')
        ///
        ///   What's not in the update expression doesn't get updated. No where expression updates all rows. E.g:
        ///
        ///   dbCmd.UpdateOnly(new Person { FirstName = "JJ", LastName = "Hendo" }, ev.Update(p => p.FirstName));
        ///   UPDATE "Person" SET "FirstName" = 'JJ'
        /// </summary>
        public static int UpdateOnly <T>(this IDbCommand dbCmd, T model, SqlExpressionVisitor <T> onlyFields)
        {
            var fieldsToUpdate = onlyFields.UpdateFields.Count == 0
                ? onlyFields.GetAllFields()
                : onlyFields.UpdateFields;

            var sql = OrmLiteConfig.DialectProvider.ToUpdateRowStatement(model, fieldsToUpdate);

            if (!onlyFields.WhereExpression.IsNullOrEmpty())
            {
                sql += " " + onlyFields.WhereExpression;
            }
            return(dbCmd.ExecuteSql(sql));
        }
예제 #2
0
        public static int Update <T>(this IDbCommand dbCmd, T obj, SqlExpressionVisitor <T> expression)
            where T : new()
        {
            IList <string> uf;

            if (expression.UpdateFields.Count == 0)
            {
                uf = expression.GetAllFields();
            }
            else
            {
                uf = expression.UpdateFields;
            }

            string sql = OrmLiteConfig.DialectProvider.ToUpdateRowStatement(obj, uf);

            if (!expression.WhereExpression.IsNullOrEmpty())
            {
                sql = sql + expression.WhereExpression;
            }
            return(dbCmd.ExecuteSql(sql));
        }