Esempio n. 1
0
        /// <summary>
        /// Returns the Statement for the <see cref="SelectDbOperation"/>.
        /// </summary>
        /// <returns>A string containing the select statement</returns>
        public override string GetStatement()
        {
            // Make sure it's ok to run the statement
            this.PreStatementValidation();

            // For performance issues, use the String Builder Class
            StringBuilder sbStatement = new StringBuilder();

            #region Select

            // Display Fields
            StringBuilder sbSelect = new StringBuilder("SELECT ");

            if (Distinct)
            {
                sbSelect.Append("DISTINCT ");
            }

            // Check all Non-Aggregate Fields
            if (SelectFields.Count == 0)
            {
                // Select All Fields
                sbSelect.Append('*');
                sbSelect.AppendLine();
            }
            else
            {
                // For formatting purposes, add a line right after the select
                sbSelect.AppendLine();

                // Select Specific Fields
                sbSelect.AppendLine(String.Join(",\r\n", from field in SelectFields
                                                select String.Concat(String.Empty.PadLeft(7, ' '),
                                                                     field.Type == FieldTypes.Display ?
                                                                     Dialect.FormatField((DisplayField)field) : Dialect.FormatField((AggregateField)field))));
            }

            #endregion Select

            #region Select Aggregates

            //// Check all Aggregate Fields
            //if (SelectFields.Count(field => field.Type == FieldTypes.Aggregate) > 0)
            //{
            //    sbSelect.AppendLine(String.Join(",\r\n", from field in SelectFields
            //                                             where field.Type == FieldTypes.Aggregate
            //                                             select String.Concat(String.Empty.PadLeft(7, ' '), Dialect.FormatField((AggregateField)field))));
            //}

            #endregion Select Aggregates

            #region From

            // From Statement
            sbSelect.Append("  FROM ");

            if (base.Tables == null)
            {
                throw new NullReferenceException($"Tables are not defined for the {nameof(SelectDbOperation)}.");
            }
            if (base.Tables.Count == 0)
            {
                throw new Exception($"There are no tables assigned for the {nameof(SelectDbOperation)}.");
            }

            if (base.Tables.Count == 1)
            {
                // Append Single Table
                sbSelect.AppendLine(base.Dialect.FormatTable(base.Tables[0]));
            }
            else
            {
                // Append Multiple Tables Separated by a Comma
                sbSelect.AppendLine();
                sbSelect.AppendLine(String.Join(",\r\n", from tbl in base.Tables
                                                select String.Concat(String.Empty.PadLeft(7, ' '), Dialect.FormatTable(tbl))));
            }

            #endregion From

            #region Joins

            //TODO: Implement SelectDbOperation -> Joins

            #endregion Joins

            #region Where

            //TODO: Implement SelectDbOperation -> Where

            // Add Filters, if needed
            if (Where?.Count > 0)
            {
                sbSelect.AppendLine(" WHERE ");
            }

            #endregion Where

            #region Group By

            // Group By Fields
            if (GroupBy)
            {
                sbSelect.AppendLine("GROUP BY");

                if (SelectFields?.Count(field => field.Type == FieldTypes.Display) > 0)
                {
                    // Add all display fields but make sure to not have the alternate name on it
                    sbSelect.AppendLine(String.Join(",\r\n", from field in SelectFields
                                                    where field.Type == FieldTypes.Display
                                                    select String.Concat(String.Empty.PadLeft(9, ' '), Dialect.FormatField(new DisplayField(field.Name, field.TableAlias)))));
                }
            }

            #endregion Group By

            #region Having

            //TODO: Implement SelectDbOperation -> Having

            #endregion Having

            // Return the Final Statement
            return(sbSelect.ToString());
        }