예제 #1
0
        private bool HasColumnWithSameName(QueryElement queryElement, QueryColumn queryColumn)
        {
            var columns = queryElement.GetColumns();

            if (columns == null)
            {
                return(false);
            }

            return(columns.Any(c => c.Value == queryColumn.Value));
        }
예제 #2
0
        private string AddQueryElementColumns(QueryElement queryElement, bool useAlias = false)
        {
            var columns = queryElement.GetColumns();

            if (columns == null)
            {
                return("");
            }

            var sb = new StringBuilder();

            var last = columns.LastOrDefault();

            foreach (var column in columns)
            {
                if (column.Alias != null)
                {
                    sb.Append(column.Alias);
                    sb.Append('.');
                }
                else if (column.IsDbColumn)
                {
                    sb.Append(queryElement.Table.GetAlias());
                    sb.Append('.');
                }

                sb.Append(column.Value);

                if (column.As != null)
                {
                    sb.Append(" AS '");
                    sb.Append(column.As);
                    sb.Append('\'');
                }
                else if (useAlias)
                {
                    if (_query.QueryColumnAliasStrategy != QueryColumnAliasStrategy.EnableDuplicates)
                    {
                        if (_query.QueryColumnAliasStrategy == QueryColumnAliasStrategy.PrefixTableAliasAlways)
                        {
                            sb.Append(" AS '");
                            sb.Append(queryElement.Table.GetAlias());
                            sb.Append('_');
                            sb.Append(column.Value);
                            sb.Append('\'');
                        }
                        else if (_query.QueryColumnAliasStrategy == QueryColumnAliasStrategy.PrefixTableNameIfNeeded ||
                                 _query.QueryColumnAliasStrategy == QueryColumnAliasStrategy.PrefixTableAliasIfNeeded)
                        {
                            var hasColumnWithSameName = HasColumnWithSameName(_query, column);
                            if (!hasColumnWithSameName)
                            {
                                foreach (var qe in _query.QueryElements.Where(qe => qe != queryElement))
                                {
                                    hasColumnWithSameName = HasColumnWithSameName(qe, column);
                                    if (hasColumnWithSameName)
                                    {
                                        break;
                                    }
                                }
                            }

                            if (hasColumnWithSameName)
                            {
                                sb.Append(" AS '");
                                if (_query.QueryColumnAliasStrategy == QueryColumnAliasStrategy.PrefixTableNameIfNeeded)
                                {
                                    sb.Append(QueryHelper.GetSimplifiedSchemaAndTableName(queryElement.Table.SchemaAndTableName));
                                }
                                else // PrefixTableAliasIfNeeded
                                {
                                    sb.Append(queryElement.Table.GetAlias());
                                    sb.Append('_');
                                }

                                sb.Append(column.Value);
                                sb.Append('\'');
                            }
                        }
                        else if (_query.QueryColumnAliasStrategy == QueryColumnAliasStrategy.PrefixTableNameAlways)
                        {
                            sb.Append(" AS '");
                            sb.Append(QueryHelper.GetSimplifiedSchemaAndTableName(queryElement.Table.SchemaAndTableName));
                            sb.Append(column.Value);
                            sb.Append('\'');
                        }
                        else
                        {
#pragma warning disable IDE0071 // Simplify interpolation
                            throw new NotImplementedException($"Unhandled QueryColumnAliasStrategy {_query.QueryColumnAliasStrategy.ToString()}.");
#pragma warning restore IDE0071 // Simplify interpolation
                        }
                    }
                }

                if (column != last)
                {
                    sb.Append(", ");
                }
            }

            return(sb.ToString());
        }