private string AppendJoinToSelect(SqlQuery originalBuilder, params Expression <Func <TEntity, object> >[] includes) { var joinBuilder = new StringBuilder(); foreach (var include in includes) { var joinProperty = AllProperties.First(q => q.Name == ExpressionHelper.GetPropertyName(include)); var declaringType = joinProperty.DeclaringType.GetTypeInfo(); var tableAttribute = declaringType.GetCustomAttribute <TableAttribute>(); var tableName = tableAttribute != null ? tableAttribute.Name : declaringType.Name; 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; var properties = joinType.FindClassProperties().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 = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "[", "]"); 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 = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "`", "`"); 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 = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "\"", "\""); 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)); } } else { attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema); } originalBuilder.SqlBuilder.Append(", " + GetFieldsSelect(attrJoin.TableName, props)); joinBuilder.Append(joinString + " " + attrJoin.TableName + " ON " + tableName + "." + attrJoin.Key + " = " + attrJoin.TableName + "." + attrJoin.ExternalKey + " "); } return(joinBuilder.ToString()); }
private string AppendJoinToSelect(SqlQuery originalBuilder, params Expression <Func <TEntity, object> >[] includes) { var joinBuilder = new StringBuilder(); foreach (var include in includes) { var joinProperty = AllProperties.First(q => q.Name == ExpressionHelper.GetPropertyName(include)); var declaringType = joinProperty.DeclaringType.GetTypeInfo(); var tableAttribute = declaringType.GetCustomAttribute <TableAttribute>(); var tableName = tableAttribute != null ? tableAttribute.Name : declaringType.Name; 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; var properties = joinType.FindClassProperties().Where(ExpressionHelper.GetPrimitivePropertiesPredicate()); var props = properties.Where(p => !p.GetCustomAttributes <NotMappedAttribute>().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray(); if (Config.UseQuotationMarks) { switch (Config.SqlProvider) { case SqlProvider.MSSQL: tableName = "[" + tableName + "]"; attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "[", "]"); attrJoin.Key = "[" + attrJoin.Key + "]"; attrJoin.ExternalKey = "[" + attrJoin.ExternalKey + "]"; attrJoin.TableAlias = "[" + attrJoin.TableAlias + "]"; foreach (var prop in props) { prop.ColumnName = "[" + prop.ColumnName + "]"; } break; case SqlProvider.MySQL: tableName = "`" + tableName + "`"; attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "`", "`"); attrJoin.Key = "`" + attrJoin.Key + "`"; attrJoin.ExternalKey = "`" + attrJoin.ExternalKey + "`"; attrJoin.TableAlias = "`" + attrJoin.TableAlias + "`"; foreach (var prop in props) { prop.ColumnName = "`" + prop.ColumnName + "`"; } break; case SqlProvider.PostgreSQL: tableName = "\"" + tableName + "\""; attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "\"", "\""); attrJoin.Key = "\"" + attrJoin.Key + "\""; attrJoin.ExternalKey = "\"" + attrJoin.ExternalKey + "\""; attrJoin.TableAlias = "\"" + attrJoin.TableAlias + "\""; foreach (var prop in props) { prop.ColumnName = "\"" + prop.ColumnName + "\""; } break; default: throw new ArgumentOutOfRangeException(nameof(Config.SqlProvider)); } } else { attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema); } originalBuilder.SqlBuilder.Append($", {GetFieldsSelect(attrJoin.TableAlias, props)}"); joinBuilder.Append( $"{joinString} {attrJoin.TableName} AS {attrJoin.TableAlias} ON {tableName}.{attrJoin.Key} = {attrJoin.TableAlias}.{attrJoin.ExternalKey} "); if (LogicalDelete && props.Any(s => s.PropertyName == LogicalDeleteProperty.Name)) { bool isDateTime = LogicalDeleteProperty.PropertyType.IsDateTime(); joinBuilder.AppendFormat(" AND {0}.{1} {2} {3} ", attrJoin.TableAlias, LogicalDeletePropertyMetadata.ColumnName, isDateTime ? "IS" : "!=", isDateTime ? "NULL" : GetLogicalDeleteValue()); } } return(joinBuilder.ToString()); }
private string AppendJoinToSelect(SqlQuery originalBuilder, params Expression <Func <TEntity, object> >[] includes) { var joinBuilder = new StringBuilder(); foreach (var include in includes) { var joinProperty = AllProperties.First(q => q.Name == ExpressionHelper.GetPropertyName(include)); var attrJoin = joinProperty.GetCustomAttribute <JoinAttributeBase>(); if (attrJoin == null) { continue; } var declaringType = joinProperty.DeclaringType.GetTypeInfo(); var tableAttribute = declaringType.GetCustomAttribute <TableAttribute>(); var tableName = tableAttribute != null ? tableAttribute.Name : declaringType.Name; var joinString = ""; switch (attrJoin) { case LeftJoinAttribute _: joinString = "LEFT JOIN"; break; case InnerJoinAttribute _: joinString = "INNER JOIN"; break; case RightJoinAttribute _ when Config.SqlProvider == SqlProvider.SQLite: throw new NotSupportedException("SQLite doesn't support RIGHT JOIN"); case RightJoinAttribute _: joinString = "RIGHT JOIN"; break; case CrossJoinAttribute _: joinString = "CROSS JOIN"; break; } var joinType = joinProperty.PropertyType.IsGenericType ? joinProperty.PropertyType.GenericTypeArguments[0] : joinProperty.PropertyType; var properties = joinType.FindClassProperties().Where(ExpressionHelper.GetPrimitivePropertiesPredicate()); var props = properties.Where(p => !p.GetCustomAttributes <NotMappedAttribute>().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray(); if (Config.UseQuotationMarks) { switch (Config.SqlProvider) { case SqlProvider.MSSQL: tableName = "[" + tableName + "]"; attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "[", "]"); attrJoin.Key = "[" + attrJoin.Key + "]"; attrJoin.ExternalKey = "[" + attrJoin.ExternalKey + "]"; attrJoin.TableAlias = "[" + attrJoin.TableAlias + "]"; foreach (var prop in props) { prop.ColumnName = "[" + prop.ColumnName + "]"; } break; case SqlProvider.MySQL: tableName = "`" + tableName + "`"; attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "`", "`"); attrJoin.Key = "`" + attrJoin.Key + "`"; attrJoin.ExternalKey = "`" + attrJoin.ExternalKey + "`"; attrJoin.TableAlias = "`" + attrJoin.TableAlias + "`"; foreach (var prop in props) { prop.ColumnName = "`" + prop.ColumnName + "`"; } break; case SqlProvider.SQLite: break; case SqlProvider.PostgreSQL: tableName = "\"" + tableName + "\""; attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema, "\"", "\""); attrJoin.Key = "\"" + attrJoin.Key + "\""; attrJoin.ExternalKey = "\"" + attrJoin.ExternalKey + "\""; attrJoin.TableAlias = "\"" + attrJoin.TableAlias + "\""; foreach (var prop in props) { prop.ColumnName = "\"" + prop.ColumnName + "\""; } break; default: throw new ArgumentOutOfRangeException(nameof(Config.SqlProvider)); } } else { attrJoin.TableName = GetTableNameWithSchemaPrefix(attrJoin.TableName, attrJoin.TableSchema); } originalBuilder.SqlBuilder.Append($", {GetFieldsSelect(attrJoin.TableAlias, props)}"); joinBuilder.Append( attrJoin is CrossJoinAttribute ? $"{joinString} {attrJoin.TableName} AS {attrJoin.TableAlias}" : $"{joinString} {attrJoin.TableName} AS {attrJoin.TableAlias} ON {tableName}.{attrJoin.Key} = {attrJoin.TableAlias}.{attrJoin.ExternalKey} "); } return(joinBuilder.ToString()); }