public string GetTableNameWithSchema(string table, RedShiftSchemasEnum schema) { PropertyInfo tablePropertyInfo = this.GetType().GetProperty(table); if (tablePropertyInfo == null) { throw new ArgumentException("Invalid table"); } bool schemaExist = tablePropertyInfo.GetCustomAttribute <RedShiftSchemasAttribute>().Schemas.Contains(schema); if (schemaExist) { return(schema.ToString() + '.' + tablePropertyInfo.GetCustomAttribute <RedShiftTableNameAttribute>().Name.ToString()); } else { throw new ArgumentException("Redshift schema don't exist"); } }
/// <summary> /// This method is useful when you want to ADD JOIN clause with existing table. Automatic gets selector parameter expression name as alias. /// </summary> /// <typeparam name="TSource">Must be class, must implement IAmazonRedshiftTableTranslator inerface and must have public constructor</typeparam> /// <typeparam name="T1">First Table (already joined), not joined table</typeparam> /// <typeparam name="T2">Second table, which we want to join</typeparam> /// <param name="query"></param> /// <param name="schema">Schema from RedShiftSchemasEnum.</param> /// <param name="selector">Table selector. You MUST select one table only.</param> /// <param name="conditionExpression">ON clause.</param> /// <param name="leftJoin">left or inner join (default is inner join).</param> /// <returns>modified query as string builder</returns> public static StringBuilder AddJoinClause <TSource, T1, T2>(this StringBuilder query, RedShiftSchemasEnum schema, Expression <Func <TSource, object> > selector, Expression <Func <T1, T2, bool> > conditionExpression, bool leftJoin = false) where TSource : class, IAmazonRedshiftTableTranslator, new() where T1 : class where T2 : class { TSource tableTranslator = new TSource(); string alias = GetParameterExpressionNameAsAlias(selector); IEnumerable <MemberInfo> members = GetExpressionMembers(selector, "Invalid FROM clause"); if (!members.Any() || members.Count() > 1) { throw new ArgumentException("Invalid FROM clause"); } query.AppendFormat(" {0} join ", leftJoin ? "left" : "inner"); query.AppendFormat(" {0} {1} ", tableTranslator.GetTableNameWithSchema(members.First().Name, schema), alias); query.AppendFormat("ON {0} ", GetQueryTranslator().Translate(conditionExpression, useExpressionSelectorAsAlias: true)); return(query); }
/// <summary> /// With this method you can add from clause from existing table. /// </summary> /// <typeparam name="TSource">Must be class, must implement IAmazonRedshiftTableTranslator inerface and must have public constructor.</typeparam> /// <param name="query"></param> /// <param name="schema">Schema from RedShiftSchemasEnum.</param> /// <param name="selector">You MUST select one table only.</param> /// <param name="useExpressionSelectorAsAlias">(use/don't use) alias - example: alias.property expression will be translated to alias.property or just property.</param> /// <returns>modified query as string builder</returns> public static StringBuilder AddFromClause <TSource>(this StringBuilder query, RedShiftSchemasEnum schema, Expression <Func <TSource, object> > selector, bool useExpressionSelectorAsAlias = false) where TSource : class, IAmazonRedshiftTableTranslator, new() { TSource tableTranslator = new TSource(); string alias = (useExpressionSelectorAsAlias == true) ? GetParameterExpressionNameAsAlias(selector) : string.Empty; IEnumerable <MemberInfo> members = GetExpressionMembers(selector, "Invalid FROM clause"); if (!members.Any() || members.Count() > 1) { throw new ArgumentException("Invalid FROM clause"); } return(query.AppendFormat(" FROM {0} {1} ", tableTranslator.GetTableNameWithSchema(members.First().Name, schema), alias)); }