コード例 #1
0
        private static int UpdateByExpression <T>(TempEntry <T> entry)
        {
            string        paramPrefix = "p_";
            List <string> properties  = entry.ModifiedProperties.Any() ? entry.ModifiedProperties : typeof(T).GetProperties().Select(p => p.Name).ToList();

            if (properties == null || !properties.Any())
            {
                throw new Exception("No update fields.");
            }

            List <string>       columnList = new List <string>();
            List <SqlParameter> paramList  = new List <SqlParameter>();

            FormatSqlParmeter <T>(entry.Data, properties, ref columnList, ref paramList, paramPrefix);

            // Where表达式解析器
            WhereExpressionResolver <T> whereResolver = new WhereExpressionResolver <T>();

            whereResolver.Resolve(entry.UpdateCondition as LambdaExpression);

            string sql        = $"UPDATE {typeof(T).TableName()} SET {string.Join(",", columnList.Select(c => $"{c} = @{paramPrefix}{c}"))} WHERE {whereResolver.SQL}";
            var    parameters = paramList.Concat(whereResolver.SqlParameters.ToList());

            return(DatabaseOperator.ExecuteNonQuery(DatabaseOperator.connection, sql, parameters.ToArray()));
        }
コード例 #2
0
ファイル: DbDeleteProvider.cs プロジェクト: JH013/ORMDemo
        public static int Execute <T>(List <IQueryable <T> > queryables)
        {
            foreach (var queryable in queryables)
            {
                var expression = queryable.Expression;

                // Where表达式解析器
                WhereExpressionResolver <T> whereResolver = new WhereExpressionResolver <T>();
                MethodCallExpression        methodCall    = expression as MethodCallExpression;
                while (methodCall != null)
                {
                    var method = methodCall.Arguments[0];
                    var lambda = methodCall.Arguments[1];
                    var right  = (lambda as UnaryExpression).Operand as LambdaExpression;

                    whereResolver.Resolve(right);

                    methodCall = method as MethodCallExpression;
                }

                string sql = $"DELETE FROM {typeof(T).TableName()} WHERE {whereResolver.SQL}";
                return(DatabaseOperator.ExecuteNonQuery(DatabaseOperator.connection, sql, whereResolver.SqlParameters));
            }

            return(0);
        }
コード例 #3
0
ファイル: DbInsertProvider.cs プロジェクト: JH013/ORMDemo
        private static int SingleInsert <T>(T data)
        {
            string              tableName  = typeof(T).TableName();     // 表名(默认取类名)
            List <string>       columnList = new List <string>();       // 列名列表
            List <SqlParameter> paramList  = new List <SqlParameter>(); // 参数列表

            FormatSqlParmeter <T>(data, ref columnList, ref paramList);
            string sqlText = $"INSERT INTO {tableName}({string.Join(",", columnList.Select(c => $"[{c}]"))})VALUES({string.Join(",", columnList.Select(c => $"@{c}"))})";

            return(DatabaseOperator.ExecuteNonQuery(DatabaseOperator.connection, sqlText, paramList.ToArray()));
        }
コード例 #4
0
        private static int UpdateByPrimaryKey <T>(TempEntry <T> entry)
        {
            var primaryKeyProp = entry.Data.PrimaryKey();

            if (primaryKeyProp == null)
            {
                throw new Exception("No primary key.");
            }

            List <string> properties = typeof(T).GetProperties().Where(p => !string.Equals(p.Name, primaryKeyProp.Name)).Select(p => p.Name).ToList();

            if (properties == null || !properties.Any())
            {
                throw new Exception("No update fields.");
            }

            List <string>       columnList = new List <string>();
            List <SqlParameter> paramList  = new List <SqlParameter>();

            FormatSqlParmeter <T>(entry.Data, properties, ref columnList, ref paramList, "");
            string       primaryKeyColumn = primaryKeyProp.ColumnName();
            string       sql          = $"UPDATE {typeof(T).TableName()} SET {string.Join(",", columnList.Select(c => $"{c} = @{c}"))} WHERE {primaryKeyColumn} = @{primaryKeyColumn}";
            SqlParameter parameter    = new SqlParameter($"{primaryKeyColumn}", primaryKeyProp.GetValue(entry.Data) ?? DBNull.Value);
            var          dataTypeAttr = primaryKeyProp.Attr <DataTypeAttribute>();

            parameter.SqlDbType = dataTypeAttr != null ? dataTypeAttr.DbType : Util.GetSqlDataType(primaryKeyProp);
            var stringLengthAttr = primaryKeyProp.Attr <StringLengthAttribute>();

            if (stringLengthAttr != null)
            {
                parameter.Size = stringLengthAttr.Length;
            }

            paramList.Add(parameter);
            return(DatabaseOperator.ExecuteNonQuery(DatabaseOperator.connection, sql, paramList.ToArray()));
        }