Exemple #1
0
        public string GetDeleteSql <T>(Expression <Func <T, bool> > expression, out IDictionary <string, dynamic> param)
        {
            var context = new ExpressionVisitorContext(SqlGenerator);
            var visitor = new WhereExpressionVisitor(context);

            visitor.Visit(expression);
            var whereSql = visitor.Condition.ToSql(SqlGenerator);

            param = context.Parameters;
            var targetMapper = TypeMapperCache.GetTypeMapper(typeof(T));

            return(SqlGenerator.GetDeleteSql(targetMapper, null, whereSql));
        }
Exemple #2
0
        public string GetUpdateSql <T>(Expression <Func <T, bool> > expression, Expression <Func <T, object> > paramInput, object value, out IDictionary <string, dynamic> paramOuput)
        {
            var context      = new ExpressionVisitorContext(SqlGenerator);
            var whereVisitor = new WhereExpressionVisitor(context);

            whereVisitor.Visit(expression);
            var whereSql = whereVisitor.Condition.ToSql(SqlGenerator);

            var setVisitor = new MemberExpressionVisitor(context);

            setVisitor.Visit(paramInput);
            var setSql = string.Format("{0} = {1}", setVisitor.Column.ToSql(SqlGenerator), context.AddParameter(value));

            paramOuput = context.Parameters;
            return(SqlGenerator.GetUpdateSql(TypeMapperCache.GetTypeMapper(typeof(T)), setSql, whereSql));
        }
Exemple #3
0
        public string GetUpdateSql <T>(Expression <Func <T, bool> > expression, IDictionary <Expression <Func <T, object> >, object> paramInput, out IDictionary <string, dynamic> paramOuput)
        {
            var context      = new ExpressionVisitorContext(SqlGenerator);
            var whereVisitor = new WhereExpressionVisitor(context);

            whereVisitor.Visit(expression);
            var whereSql   = whereVisitor.Condition.ToSql(SqlGenerator);
            var setVisitor = new MemberExpressionVisitor(context);
            var setSql     = new StringBuilder();

            foreach (KeyValuePair <Expression <Func <T, object> >, object> item in paramInput)
            {
                setVisitor.Visit(item.Key);
                setSql.AppendFormat("{0} = {1},", setVisitor.Column.ToSql(SqlGenerator), context.AddParameter(item.Value));
            }
            paramOuput = context.Parameters;
            return(SqlGenerator.GetUpdateSql(TypeMapperCache.GetTypeMapper(typeof(T)), setSql.Remove(setSql.Length - 1, 1).ToString(), whereSql));
        }
Exemple #4
0
        public string GetUpdateSql <T>(IDictionary <string, object> source, IDictionary <string, object> condition)
        {
            var setSql = new StringBuilder();

            foreach (KeyValuePair <string, object> item in source)
            {
                setSql.AppendFormat("{0} = {1},", SqlGenerator.DecorateName(item.Key), SqlGenerator.DecorateParameter(item.Key));
            }

            var whereSql = new StringBuilder();

            foreach (KeyValuePair <string, object> item in condition)
            {
                whereSql.AppendFormat("{0} = {1} and", SqlGenerator.DecorateName(item.Key), SqlGenerator.DecorateParameter(item.Key));
            }

            return(SqlGenerator.GetUpdateSql(TypeMapperCache.GetTypeMapper(typeof(T)), setSql.Remove(setSql.Length - 1, 1).ToString(), whereSql.Remove(whereSql.Length - 3, 3).ToString()));
        }
Exemple #5
0
        public string GetInsertSqlWithIdentity <T>(object obj)
        {
            var targetMapper = TypeMapperCache.GetTypeMapper(typeof(T));
            var sourceMapper = TypeMapperCache.GetTypeMapper(obj);

            if (insertSqlWithIdentityCache.Any(i => i.Item1 == sourceMapper.Code && i.Item2 == targetMapper.Code))
            {
                return(insertSqlWithIdentityCache.Single(i => i.Item1 == sourceMapper.Code && i.Item2 == targetMapper.Code).Item3);
            }
            string sql = string.Empty;

            if (obj is T)
            {
                sql = SqlGenerator.GetInsertSqlWithIdentity(targetMapper);
            }
            else
            {
                sql = SqlGenerator.GetInsertSqlWithIdentity(RebuildMapper(sourceMapper, targetMapper));
            }
            insertSqlWithIdentityCache.Add(Tuple.Create(sourceMapper.Code, targetMapper.Code, sql));
            return(sql);
        }