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);
        }
Beispiel #2
0
        private static string BuildInsertQuery(IDbConnection connection, Type type)
        {
            var sqlBuilder = GetSqlBuilder(connection);
            var cacheKey   = new QueryCacheKey(QueryCacheType.Insert, sqlBuilder, type);

            if (!QueryCache.TryGetValue(cacheKey, out var sql))
            {
                var tableName   = Resolvers.Table(type, connection);
                var keyProperty = Resolvers.KeyProperty(type, out var isIdentity);

                var typeProperties = new List <PropertyInfo>();
                foreach (var typeProperty in Resolvers.Properties(type))
                {
                    if (typeProperty == keyProperty)
                    {
                        if (isIdentity)
                        {
                            // Skip key properties marked as an identity column.
                            continue;
                        }
                    }

                    if (typeProperty.GetSetMethod() != null)
                    {
                        typeProperties.Add(typeProperty);
                    }
                }

                var columnNames = typeProperties.Select(p => Resolvers.Column(p, sqlBuilder)).ToArray();
                var paramNames  = typeProperties.Select(p => "@" + p.Name).ToArray();

                sql = sqlBuilder.BuildInsert(tableName, columnNames, paramNames, keyProperty);

                QueryCache.TryAdd(cacheKey, sql);
            }

            return(sql);
        }
Beispiel #3
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;
        }
Beispiel #4
0
        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);
                var keyProperties = Resolvers.KeyProperties(type);

                // Use all properties which are settable.
                var typeProperties = Resolvers.Properties(type)
                                     .Except(keyProperties.Where(p => p.IsGenerated).Select(p => p.Property))
                                     .Where(p => p.GetSetMethod() != null)
                                     .ToArray();

                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);
        }
Beispiel #5
0
        private static string BuildUpdateQuery(IDbConnection connection, Type type)
        {
            var cacheKey = new QueryCacheKey(QueryCacheType.Update, connection, type);

            if (!QueryCache.TryGetValue(cacheKey, out var sql))
            {
                var tableName   = Resolvers.Table(type, connection);
                var keyProperty = Resolvers.KeyProperty(type);
                var builder     = GetSqlBuilder(connection);

                // Use all properties which are settable.
                var typeProperties = Resolvers.Properties(type)
                                     .Where(p => p != keyProperty)
                                     .Where(p => p.GetSetMethod() != null)
                                     .ToArray();

                var columnNames = typeProperties.Select(p => $"{Resolvers.Column(p, connection)} = {builder.PrefixParameter(p.Name)}").ToArray();
                sql = $"update {tableName} set {string.Join(", ", columnNames)} where {Resolvers.Column(keyProperty, connection)} = {builder.PrefixParameter(keyProperty.Name)}";

                QueryCache.TryAdd(cacheKey, sql);
            }

            return(sql);
        }