コード例 #1
0
        internal static string BuildGetByIds(IDbConnection connection, Type type, object[] ids, out DynamicParameters parameters)
        {
            var sqlBuilder = GetSqlBuilder(connection);
            var cacheKey   = new QueryCacheKey(QueryCacheType.GetByMultipleIds, sqlBuilder, type);

            if (!QueryCache.TryGetValue(cacheKey, out var sql))
            {
                var tableName      = Resolvers.Table(type, sqlBuilder);
                var keyProperties  = Resolvers.KeyProperties(type);
                var keyColumnNames = keyProperties.Select(p => Resolvers.Column(p.Property, sqlBuilder)).ToArray();
                if (keyColumnNames.Length != ids.Length)
                {
                    throw new InvalidOperationException($"Number of key columns ({keyColumnNames.Length}) of type {type.Name} does not match with the number of specified IDs ({ids.Length}).");
                }

                var sb = new StringBuilder("select * from ").Append(tableName).Append(" where");
                var i  = 0;
                foreach (var keyColumnName in keyColumnNames)
                {
                    if (i != 0)
                    {
                        sb.Append(" and");
                    }

                    sb.Append(" ").Append(keyColumnName).Append($" = {sqlBuilder.PrefixParameter("Id")}").Append(i);
                    i++;
                }

                sql = sb.ToString();
                QueryCache.TryAdd(cacheKey, sql);
            }

            parameters = new DynamicParameters();
            for (var i = 0; i < ids.Length; i++)
            {
                parameters.Add("Id" + i, ids[i]);
            }

            return(sql);
        }
コード例 #2
0
        internal static string BuildGetById(ISqlBuilder sqlBuilder, Type type, object id, out DynamicParameters parameters)
        {
            var cacheKey = new QueryCacheKey(QueryCacheType.Get, sqlBuilder, type);

            if (!QueryCache.TryGetValue(cacheKey, out var sql))
            {
                var tableName     = Resolvers.Table(type, sqlBuilder);
                var keyProperties = Resolvers.KeyProperties(type);
                if (keyProperties.Length > 1)
                {
                    throw new InvalidOperationException($"Entity {type.Name} contains more than one key property." +
                                                        "Use the Get<T> overload which supports passing multiple IDs.");
                }
                var keyColumnName = Resolvers.Column(keyProperties[0].Property, sqlBuilder);

                sql = $"select * from {tableName} where {keyColumnName} = {sqlBuilder.PrefixParameter("Id")}";
                QueryCache.TryAdd(cacheKey, sql);
            }

            parameters = new DynamicParameters();
            parameters.Add("Id", id);

            return(sql);
        }
コード例 #3
0
ファイル: Update.cs プロジェクト: xpwdm2020/AAFrameWork
        internal static string BuildUpdateQuery(ISqlBuilder sqlBuilder, Type type)
        {
            var cacheKey = new QueryCacheKey(QueryCacheType.Update, sqlBuilder, type);

            if (!QueryCache.TryGetValue(cacheKey, out var sql))
            {
                var tableName = Resolvers.Table(type, sqlBuilder);

                // Use all non-key and non-generated properties for updates
                var keyProperties  = Resolvers.KeyProperties(type);
                var typeProperties = Resolvers.Properties(type)
                                     .Where(x => !x.IsGenerated)
                                     .Select(x => x.Property)
                                     .Except(keyProperties.Where(p => p.IsGenerated).Select(p => p.Property));

                var columnNames  = typeProperties.Select(p => $"{Resolvers.Column(p, sqlBuilder)} = {sqlBuilder.PrefixParameter(p.Name)}").ToArray();
                var whereClauses = keyProperties.Select(p => $"{Resolvers.Column(p.Property, sqlBuilder)} = {sqlBuilder.PrefixParameter(p.Property.Name)}");
                sql = $"update {tableName} set {string.Join(", ", columnNames)} where {string.Join(" and ", whereClauses)}";

                QueryCache.TryAdd(cacheKey, sql);
            }

            return(sql);
        }