Exemple #1
0
        private string AppendJoinToSelect(SqlQuery originalBuilder, params Expression <Func <TEntity, object> >[] includes)
        {
            var joinBuilder = new StringBuilder();

            var joinedProperties = new List <PropertyInfo>();

            foreach (var include in includes)
            {
                var propertyName = ExpressionHelper.GetFullPropertyName(include);
                var joinProperty = AllProperties.Concat(joinedProperties).First(x =>
                {
                    if (x.DeclaringType != null)
                    {
                        return(x.DeclaringType.FullName + "." + x.Name == propertyName);
                    }
                    return(false);
                });

                var tableName = GetTableNameOrAlias(joinProperty.DeclaringType);

                var attrJoin = joinProperty.GetCustomAttribute <JoinAttributeBase>();

                if (attrJoin == null)
                {
                    continue;
                }

                var joinString = "";
                if (attrJoin is LeftJoinAttribute)
                {
                    joinString = "LEFT JOIN";
                }
                else if (attrJoin is InnerJoinAttribute)
                {
                    joinString = "INNER JOIN";
                }
                else if (attrJoin is RightJoinAttribute)
                {
                    joinString = "RIGHT JOIN";
                }

                var joinType = joinProperty.PropertyType.IsGenericType() ? joinProperty.PropertyType.GenericTypeArguments[0] : joinProperty.PropertyType;
                joinedProperties.AddRange(joinType.GetProperties().Where(p => !p.GetCustomAttributes <NotMappedAttribute>().Any()));
                var properties = joinType.GetProperties().Where(ExpressionHelper.GetPrimitivePropertiesPredicate());
                var props      = properties.Where(p => !p.GetCustomAttributes <NotMappedAttribute>().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray();

                if (Config.UseQuotationMarks)
                {
                    switch (Config.SqlConnector)
                    {
                    case ESqlConnector.MSSQL:
                        tableName            = "[" + tableName + "]";
                        attrJoin.TableName   = "[" + attrJoin.TableName + "]";
                        attrJoin.Key         = "[" + attrJoin.Key + "]";
                        attrJoin.ExternalKey = "[" + attrJoin.ExternalKey + "]";
                        foreach (var prop in props)
                        {
                            prop.ColumnName = "[" + prop.ColumnName + "]";
                        }
                        break;

                    case ESqlConnector.MySQL:
                        tableName            = "`" + tableName + "`";
                        attrJoin.TableName   = "`" + attrJoin.TableName + "`";
                        attrJoin.Key         = "`" + attrJoin.Key + "`";
                        attrJoin.ExternalKey = "`" + attrJoin.ExternalKey + "`";
                        foreach (var prop in props)
                        {
                            prop.ColumnName = "`" + prop.ColumnName + "`";
                        }
                        break;

                    case ESqlConnector.PostgreSQL:
                        tableName            = "\"" + tableName + "\"";
                        attrJoin.TableName   = "\"" + attrJoin.TableName + "\"";
                        attrJoin.Key         = "\"" + attrJoin.Key + "\"";
                        attrJoin.ExternalKey = "\"" + attrJoin.ExternalKey + "\"";
                        foreach (var prop in props)
                        {
                            prop.ColumnName = "\"" + prop.ColumnName + "\"";
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(Config.SqlConnector));
                    }
                }

                originalBuilder.SqlBuilder.Append(", " + GetFieldsSelect(attrJoin.TableName, props));
                joinBuilder.Append(joinString + " " + attrJoin.TableName + " ON " + tableName + "." + attrJoin.Key + " = " + attrJoin.TableName + "." + attrJoin.ExternalKey + " ");
            }
            return(joinBuilder.ToString());
        }