Ejemplo n.º 1
0
 /// <summary>
 /// Returns the index of the item in the collection.
 /// </summary>
 /// <param name="item">The item to find.</param>
 /// <returns>The index of the item, or -1 if it is not found.</returns>
 public int IndexOf(SqlJoin item)
 {
     return(List.IndexOf(item));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a statement.
        /// </summary>
        /// <returns></returns>
        public override SqlStatement GetStatement()
        {
            if (EntityType == null)
            {
                throw new ArgumentNullException("EntityType");
            }

            // mbr - 16-10-2005 - changed so that inheriting classes can override default field inclusion behaviour...
            EntityField[] fields = this.ResolveFieldsForSelect();
            if (fields == null)
            {
                throw new ArgumentNullException("fields");
            }
            if (fields.Length == 0)
            {
                throw new InvalidOperationException("No fields selected.");
            }

            // create a statement...
            if (Dialect == null)
            {
                throw new ArgumentNullException("Dialect");
            }
            SqlStatement statement = new SqlStatement(this.EntityType, this.Dialect);

            if (this.TimeoutSeconds > 0)
            {
                statement.TimeoutSeconds = this.TimeoutSeconds;
            }

            // set...
            statement.SetOriginalCreator(this);

            // check...
            if (statement.SelectMap == null)
            {
                throw new ArgumentNullException("statement.SelectMap");
            }

            // set up the basic select text...
            StringBuilder builder = new StringBuilder();

            builder.Append(statement.Dialect.SelectKeyword);

            // mbr - 2010-03-03 - added...
            if (this.Distinct)
            {
                builder.Append(" ");
                builder.Append(statement.Dialect.DistinctKeyword);
            }

            // top...
            if (this.Top > 0 && statement.Dialect.TopAtTop)
            {
                builder.Append(" ");
                builder.Append(statement.Dialect.FormatTop(this.Top));
            }

            // fields...
            builder.Append(" ");
            for (int index = 0; index < fields.Length; index++)
            {
                // add...
                if (index > 0)
                {
                    builder.Append(statement.Dialect.IdentifierSeparator);
                }

                // field...
                EntityField field = fields[index];
                if (field.IsExtendedProperty)
                {
                    // mbr - 25-09-2007 - provider...
                    //					AppendExtendedPropertyStatement(statement, builder, field);
                    if (Database.ExtensibilityProvider == null)
                    {
                        throw new InvalidOperationException("Database.ExtensibilityProvider is null.");
                    }
                    Database.ExtensibilityProvider.AddExtendedPropertyToSelectStatement(this, statement, builder, field);
                }
                else
                {
                    // mbr - 2008-09-11 - changed for MySQL support...
                    //					builder.Append(statement.Dialect.FormatColumnName(field, this.UseFullyQualifiedNames));
                    builder.Append(statement.Dialect.FormatColumnNameForSelect(field, this.UseFullyQualifiedNames));

                    // mbr - 10-10-2007 - case 875 - do nothing if we don't have a join...
                    if (field.EntityType == this.EntityType)
                    {
                        statement.SelectMap.MapFields.Add(new SelectMapField(field, index));
                    }
                    else
                    {
                        // find it!
                        SqlJoin found = null;
                        foreach (SqlJoin join in this.Joins)
                        {
                            if (join.TargetEntityType == field.EntityType)
                            {
                                found = join;
                                break;
                            }
                        }

                        // found?
                        if (found == null)
                        {
                            throw new InvalidOperationException(string.Format("Could not find a join for field '{0}'.", field));
                        }

                        // set...
                        statement.SelectMap.MapFields.Add(new SelectMapField(field, index, found));
                    }
                }
            }

            this.AppendIndent(builder);
            builder.Append(statement.Dialect.FromKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatTableName(this.EntityType.NativeName));

            if (this.HasForcedIndex)
            {
                builder.Append(" ");
                builder.Append(statement.Dialect.GetForceIndexDirective(this.ForcedIndex));
            }

            // mbr - 25-09-2007 - joins...
            this.AppendJoins(statement, builder);

            // get the contraints...
            StringBuilder constraints = new StringBuilder();

            this.AppendConstraints(statement, constraints);

            // trim...
            string useConstraints = constraints.ToString().Trim();

            //// mbr - 13-10-2005 - rejigged to handle partitioning...
            //// mbr - 08-03-2006 - added supports...
            // mbr - 2014-11-30 - removes partitioning...
            //if(!(this.IgnorePartitioning) && this.EntityType.SupportsPartitioning)
            //{
            //    // get the strategy....
            //    PartitioningStrategy strategy = this.EntityType.PartitioningStrategy;
            //    if(strategy == null)
            //        throw new InvalidOperationException("strategy is null.");

            //    // get the partition SQL...
            //    // mbr - 04-09-2007 - c7 - removed 'forReading'.
            //    //				useConstraints = strategy.RebuildConstraints(statement, useConstraints, true);
            //    useConstraints = strategy.RebuildConstraints(statement, useConstraints);

            //    // we have to get something back...
            //    if(useConstraints == null)
            //        throw new InvalidOperationException("'useConstraints' is null.");

            //    // mbr - 04-09-2007 - for c7 - a zero-length string might be ok...
            //    if(useConstraints.Length == 0 && !(strategy.IsZeroLengthIdSetOk))
            //        throw new InvalidOperationException("'useConstraints' is zero-length.");
            //}

            // mbr - 2010-03-10 - added a method to allow us to change the statement before it is executed...
            if (Database.StatementCallback != null)
            {
                useConstraints = Database.StatementCallback.ReplaceContraints(statement, this, useConstraints);
            }

            // do we have constraints?
            if (useConstraints.Length > 0)
            {
                // just add the constraints...
                this.AppendIndent(builder);
                builder.Append(this.Dialect.WhereKeyword);
                builder.Append(" ");
                builder.Append(useConstraints);
            }

            // mbr - 2010-03-08 - if not distinct...
            ///if (this.SortOrder.Count > 0)
            //if ((!(this.Distinct) || this.ForceSortOnDistinct) && this.SortOrder.Count > 0)
            // mbr - 2016-08-17 -- no idea why this was like this...
            if (this.SortOrder.Count > 0)
            {
                // append...
                this.AppendIndent(builder);
                builder.Append(statement.Dialect.OrderByKeyword);
                builder.Append(" ");

                // add...
                for (int index = 0; index < this.SortOrder.Count; index++)
                {
                    if (index > 0)
                    {
                        builder.Append(statement.Dialect.IdentifierSeparator);
                    }

                    // spec...
                    SortSpecification specification = this.SortOrder[index];

                    // mbr - 10-10-2007 - case 875 - fixed to qualify...
                    //					builder.Append(statement.Dialect.FormatNativeName(specification.Field.NativeName));

                    if (specification.Field != null)
                    {
                        builder.Append(statement.Dialect.FormatColumnName(specification.Field, this.UseFullyQualifiedNames));
                    }
                    else
                    {
                        builder.Append(specification.FreeSort);
                    }

                    // direction?
                    builder.Append(" ");
                    builder.Append(statement.Dialect.GetSortDirectionKeyword(specification.Direction));
                }
            }

            // top...
            if (this.Top > 0 && !(statement.Dialect.TopAtTop))
            {
                this.AppendIndent(builder);
                builder.Append(statement.Dialect.FormatTop(this.Top));
            }

            if (this.SignalRecompile)
            {
                builder.Append(" OPTION(RECOMPILE)");
            }

            // setup...
            statement.CommandText = builder.ToString();
            statement.CommandType = CommandType.Text;

            // extras...
            statement.Parameters.AddRange(this.ExtraParameters);

            // return...
            return(statement);
        }