Beispiel #1
0
        public override void Append(FilterConstraintAppendContext context)
        {
            var builder = context.Sql;

            // create...
            var table = SqlStatementParameter.TableifyArrayInternal(this.Field.DBType, this.Values);

            // build...
            builder.Append(context.Creator.Dialect.FormatColumnNameForSelect(this.Field, this.UseFullyQualifiedNames));

            if (this.Operator == SqlOperator.EqualTo)
            {
                // no-op...
            }
            else if (this.Operator == SqlOperator.NotEqualTo)
            {
                builder.Append("not ");
            }
            else
            {
                throw new NotSupportedException(string.Format("Cannot handle '{0}'.", this.Operator));
            }

            builder.Append(" in (select id from ");
            builder.Append(context.Creator.Dialect.FormatVariableNameForQueryText(((SqlFilter)context.Creator).ExtraParameters.Add(Field.DBType, table)));
            builder.Append(")");
        }
Beispiel #2
0
        /// <summary>
        /// Append the constraint for the extended table property name and key fields, used for updating and deleting
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="statement"></param>
        protected void AppendKeyFieldConstraints(StringBuilder builder, SqlStatement statement)
        {
            EntityField[] keyFields = GetKeyFields();
            if (keyFields.Length == 0)
            {
                throw new InvalidOperationException("Key fields are zero-length.");
            }

            // Walk each key field
            for (int index = 0; index < keyFields.Length; index++)
            {
                // param...
                SqlStatementParameter parameter = this.CreateParameterForField(keyFields[index]);
                statement.Parameters.Add(parameter);

                // add...
                if (index > 0)
                {
                    builder.Append(" ");
                    builder.Append(statement.Dialect.AndKeyword);
                    builder.Append(" ");
                }
                builder.Append(statement.Dialect.FormatNativeName(keyFields[index].NativeName));
                builder.Append("=");
                builder.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));
            }
        }
 /// <summary>
 /// Removes a SqlStatementParameter item to the collection.
 /// </summary>
 /// <param name="item">The item to remove.</param>
 public void Remove(SqlStatementParameter item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     List.Remove(item);
 }
 /// <summary>
 /// Inserts a SqlStatementParameter instance into the collection.
 /// </summary>
 /// <param name="item">The item to add.</param>
 public void Insert(int index, SqlStatementParameter item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     List.Insert(index, item);
 }
Beispiel #5
0
        /// <summary>
        /// Gets the hash of the statement.
        /// </summary>
        /// <returns></returns>
        public SqlStatementHash GetHash(bool includeParameterValues)
        {
            StringBuilder builder = new StringBuilder();

            // default...
            if (this.CommandType != System.Data.CommandType.Text)
            {
                builder.Append(this.CommandType);
            }

            // type...
            builder.Append("|");
            builder.Append(this.CommandText);

            // get...
            if (includeParameterValues)
            {
                var paramBuilder = new StringBuilder();
                for (int index = 0; index < this.Parameters.Count; index++)
                {
                    SqlStatementParameter parameter = this.Parameters[index];

                    // do we want parameter values? if we're caching results, we do, if we're caching the command, we do not...
                    if (index > 0)
                    {
                        paramBuilder.Append("|");
                    }
                    paramBuilder.Append(parameter.Value);
                }

                // erturn...
                return(new SqlStatementHash(builder.ToString(), paramBuilder.ToString()));
            }
            else
            {
                return(new SqlStatementHash(builder.ToString(), null));
            }

            // hash it...
            //byte[] bs = Encoding.Unicode.GetBytes(builder.ToString());

            //// return...
            //if(Md5 == null)
            //    throw new InvalidOperationException("Md5 is null.");
            //byte[] hash = Md5.ComputeHash(bs);

            //// again...
            //builder = new StringBuilder();
            //for(int index = 0; index < hash.Length; index++)
            //    builder.Append(hash[index].ToString("x2"));

            //// return...
            //string result = builder.ToString();
            //return result;

            // mbr - 2014-11-30 - changed...
            //return HashHelper.GetMd5HashOfStringAsBase64(builder.ToString());
        }
Beispiel #6
0
        private static void AddValueListXml(this ISqlStatementArrayBuilder sql, StringBuilder builder, IEnumerable values, DbType dbType)
        {
            string xml = null;

            using (var writer = new StringWriter())
            {
                var xmlWriter = new XmlTextWriter(writer);
                xmlWriter.WriteStartElement("a");
                foreach (var value in values)
                {
                    xmlWriter.WriteStartElement("b");
                    xmlWriter.WriteValue(value);
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();

                xml = writer.ToString();
            }

            var name  = sql.Parameters.GetUniqueParameterName();
            var alias = "x_" + name;
            var p     = new SqlStatementParameter(name, DbType.Xml, xml);

            sql.Parameters.Add(p);

            //SELECT x.node.value('@value', 'int')
            //    FROM @xml.nodes('/values/value') as x(node)

            builder.Append("select ");
            builder.Append(alias);
            builder.Append(".node.value('./node()[1]', '");
            if (dbType == DbType.Int32 || dbType == DbType.Int16 || dbType == DbType.Boolean)
            {
                builder.Append("int");
            }
            else if (dbType == DbType.Int64)
            {
                builder.Append("bigint");
            }
            else if (dbType == DbType.String)
            {
                builder.Append("nvarchar(max)");
            }
            else if (dbType == DbType.Guid)
            {
                builder.Append("uniqueidentider");
            }
            else
            {
                throw new NotSupportedException(string.Format("Cannot handle '{0}'.", dbType));
            }
            builder.Append("') id from ");
            builder.Append(sql.Dialect.FormatVariableNameForQueryText(p.Name));
            builder.Append(".nodes('/a/b') as ");
            builder.Append(alias);
            builder.Append("(node)");
        }
Beispiel #7
0
        private static void AddValueListTableType(this ISqlStatementArrayBuilder sql, StringBuilder builder, IEnumerable values, DbType dbType)
        {
            var name = sql.Parameters.GetUniqueParameterName();
            var p    = SqlStatementParameter.CreateArrayParameter(name, dbType, values);

            sql.Parameters.Add(p);

            builder.Append("select id from ");
            builder.Append(sql.Dialect.FormatVariableNameForQueryText(name));
        }
 /// <summary>
 /// Discovers if the given item is in the collection.
 /// </summary>
 /// <param name="item">The item to find.</param>
 /// <returns>Returns true if the given item is in the collection.</returns>
 public bool Contains(SqlStatementParameter item)
 {
     if (IndexOf(item) == -1)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
        /// <summary>
        /// Adds a SqlStatementParameter instance to the collection.
        /// </summary>
        /// <param name="item">The item to add.</param>
        public void Add(SqlStatementParameter item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (!(IndexOf(item.Name) == -1))
            {
                return;
            }

            List.Add(item);
        }
Beispiel #10
0
        /// <summary>
        /// Add a named parameter for the extended property name
        /// </summary>
        /// <param name="field"></param>
        /// <param name="statement"></param>
        /// <returns></returns>
        private string AddPropertyNameParameter(EntityField field, SqlStatement statement)
        {
            SqlStatementParameter propertyNameField = CreateParameterForField(field);

            if (statement.Parameters.Contains(propertyNameField.Name))
            {
                return(propertyNameField.Name);
            }

            // Add the property name parameter so we can make sure we are accessing the correct property name.
            statement.Parameters.Add(propertyNameField);

            return(propertyNameField.Name);
        }
Beispiel #11
0
        /// <summary>
        /// Add a named parameter for the extended fields
        /// </summary>
        /// <param name="field"></param>
        /// <param name="statement"></param>
        /// <returns></returns>
        internal static string AddNameParameter(EntityField field, SqlStatement statement)
        {
            string parameterName = string.Format("Extended{0}", field.NativeName);

            if (statement.Parameters.Contains(parameterName))
            {
                return(parameterName);
            }

            // Add the property name parameter so we can make sure we are accessing the correct property name.
            SqlStatementParameter propertyNameParameter = new SqlStatementParameter(parameterName, DbType.String, field.NativeName.Name);

            statement.Parameters.Add(propertyNameParameter);

            return(parameterName);
        }
Beispiel #12
0
        /// <summary>
        /// Append insert into extended table
        /// </summary>
        /// <param name="context"></param>
        /// <param name="extendedField"></param>
        /// <param name="builder"></param>
        /// <param name="statement"></param>
        protected void AppendInsertIntoExtendedTable(WorkUnitProcessingContext context, EntityField extendedField, StringBuilder builder, SqlStatement statement)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            EntityField[] keyFields = GetKeyFields();

            string propertyName          = AddPropertyNameParameter(extendedField, statement);
            string propertyNameParameter = FlatTableExtensibilityProvider.AddNameParameter(extendedField, statement);

            // Append the inssert statements
            builder.Append(statement.Dialect.InsertIntoKeyword);
            builder.Append(" ");
            // mbr - 08-12-2005 - changed...
//			builder.Append(statement.Dialect.FormatNativeName(ExtendedPropertySettings.GetExtendedNativeNameForEntityType(EntityType)));
            builder.Append(statement.Dialect.FormatNativeName(this.EntityType.NativeNameExtended));
            builder.Append(" (");
            builder.Append(statement.Dialect.FormatNativeName(FlatTableExtensibilityProvider.GetColumnNameForDbType(extendedField.DBType)));
            builder.Append(statement.Dialect.IdentifierSeparator);
            builder.Append(statement.Dialect.FormatNativeName(ExtendedPropertySettings.GetExtendedNativeNameForNameColumn()));

            for (int index = 0; index < keyFields.Length; index++)
            {
                builder.Append(statement.Dialect.IdentifierSeparator);
                builder.Append(statement.Dialect.FormatNativeName(keyFields[index].NativeName));
            }

            builder.Append(") ");
            builder.Append(statement.Dialect.ValuesKeyword);
            builder.Append(" (");

            // create the param...

            builder.Append(statement.Dialect.FormatVariableNameForQueryText(propertyName));
            builder.Append(statement.Dialect.IdentifierSeparator);
            builder.Append(statement.Dialect.FormatVariableNameForQueryText(propertyNameParameter));

            for (int index = 0; index < keyFields.Length; index++)
            {
                // create the param...
                SqlStatementParameter param = null;
                if (!statement.Parameters.Contains(keyFields[index].NativeName.Name))
                {
                    AddPropertyNameParameter(keyFields[index], statement);
                }

                // Get the parameter
                param = statement.Parameters[keyFields[index].Name];

                // We need to get the id of the field if it is auto generated from the context
                if (context.Bag.LastCreatedId != null)
                {
                    param.Value = context.Bag.LastCreatedId;
                }

                // add...
                builder.Append(statement.Dialect.IdentifierSeparator);
                builder.Append(statement.Dialect.FormatVariableNameForQueryText(param.Name));
            }
        }
Beispiel #13
0
        /// <summary>
        /// Appends the insert portion to the query.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="builder"></param>
        private void AppendInsertPortion(SqlStatement statement, StringBuilder builder)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            // create a block...
            TouchedValueCollection block = this.AddTouchedValueBlock();

            EntityField[] autoIncrementFields = GetAutoIncrementFields();
            if (autoIncrementFields.Length > 0 && statement.Dialect.LastInsertedIdMode == LastInsertedIdMode.Parameter)
            {
                statement.Parameters.Add(statement.Dialect.GetLastInsertedIdParameter());
            }

            // append...
            builder.Append(statement.Dialect.InsertIntoKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatNativeName(this.EntityType.NativeName));
            builder.Append(" (");
            EntityField[] fields = this.GetNonAutoIncrementFields();
            for (int index = 0; index < fields.Length; index++)
            {
                if (index > 0)
                {
                    builder.Append(statement.Dialect.IdentifierSeparator);
                }
                builder.Append(statement.Dialect.FormatNativeName(fields[index].NativeName));
            }
            builder.Append(") ");
            builder.Append(statement.Dialect.ValuesKeyword);
            builder.Append(" (");
            for (int index = 0; index < fields.Length; index++)
            {
                // create the param...
                SqlStatementParameter param = this.CreateParameterForField(fields[index]);
                statement.Parameters.Add(param);
                param.RelatedField = fields[index];

                // add...
                if (index > 0)
                {
                    builder.Append(statement.Dialect.IdentifierSeparator);
                }
                builder.Append(statement.Dialect.FormatVariableNameForQueryText(param.Name));

                // mbr - 2007-04-02 - touched value...
                block.Add(new TouchedValue(fields[index], param.Value));
            }
            builder.Append(")");
            if (autoIncrementFields.Length > 0 && statement.Dialect.LastInsertedIdMode == LastInsertedIdMode.Parameter)
            {
                builder.Append(statement.Dialect.AppendLastInsertedIdParameterAccess(GetAutoIncrementFields()));
            }
            builder.Append(statement.Dialect.StatementSeparator);
        }
Beispiel #14
0
        /// <summary>
        /// Processes the work unit.
        /// </summary>
        /// <param name="context"></param>
        public override void Process(WorkUnitProcessingContext context, ITimingBucket timings)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // check...
            if (EntityType == null)
            {
                throw new ArgumentNullException("EntityType");
            }
            if (context.Connection == null)
            {
                throw new InvalidOperationException("context.Connection is null.");
            }

            // insert...
            SqlStatement[] statements = null;
            using (timings.GetTimer("GetStatements"))
            {
                statements = this.GetStatements(context);
                if (statements == null)
                {
                    throw new InvalidOperationException("'statements' is null.");
                }
                if (statements.Length == 0)
                {
                    throw new InvalidOperationException("'statements' is zero-length.");
                }
            }

            // get...
            SqlStatement statement = statements[0];

            if (statement == null)
            {
                throw new InvalidOperationException("statement is null.");
            }

            // run it...
            object[] result = null;

            // mbr - 27-10-2005 - if we're sproc mode, get the result differently...
            if (context.Connection.SqlMode == SqlMode.AdHoc)
            {
                if (context.Connection.SupportsMultiStatementQueries)
                {
                    // debug?
                    using (timings.GetTimer("SingletonExecute"))
                        result = this.Execute(context, statement);
                }
                else
                {
                    using (timings.GetTimer("MultipleExecutes"))
                    {
                        // run...
                        context.Connection.ExecuteNonQuery(statement);

                        // id...
                        if (context.Dialect.LastInsertedIdMode == LastInsertedIdMode.Scalar)
                        {
                            statement = this.CreateSelectIdStatement(context);
                            if (statement == null)
                            {
                                throw new InvalidOperationException("statement is null.");
                            }

                            // run...
                            result = new object[] { context.Connection.ExecuteScalar(statement) };
                        }
                        else if (context.Dialect.LastInsertedIdMode == LastInsertedIdMode.Parameter)
                        {
                            EntityField[] autos = this.GetAutoIncrementFields();
                            if (autos == null)
                            {
                                throw new InvalidOperationException("'autos' is null.");
                            }
                            if (autos.Length == 0)
                            {
                                throw new InvalidOperationException("'autos' is zero-length.");
                            }

                            SqlStatementParameter parameter = statement.Parameters[context.Dialect.LastInsertedIdVariableName];
                            if (parameter == null)
                            {
                                throw new InvalidOperationException("parameter is null.");
                            }

                            // run...
                            result = new object[] { parameter.Value };
                        }
                        else
                        {
                            throw new NotSupportedException(string.Format("Cannot handle {0}.", context.Dialect.LastInsertedIdMode));
                        }
                    }
                }
            }
            // mbr - 10-10-2007 - removed.
//			else if(context.Connection.SqlMode == SqlMode.Sprocs)
//			{
//				// run it...
//				context.Connection.ExecuteNonQuery(statement);
//
//				// get the return parameter...
//				EntityField[] autoFields = this.EntityType.GetAutoIncrementFields();
//				if(autoFields == null)
//					throw new InvalidOperationException("autoFields is null.");
//				if(autoFields.Length == 1)
//				{
//					// find it...
//					SqlStatementParameter parameter = statement.Parameters[autoFields[0].NativeName.Name];
//					if(parameter == null)
//						throw new InvalidOperationException("parameter is null.");
//
//					// return...
//					result = new object[] { parameter.Value };
//				}
//				else if(autoFields.Length > 1)
//					throw new NotSupportedException(string.Format("Tables with '{0}' auto-increment fields are not supported.", autoFields.Length));
//			}
            else
            {
                throw new NotSupportedException(string.Format("Cannot handle '{0}'.", context.Connection.SqlMode));
            }

            // set...
            using (timings.GetTimer("Passback"))
            {
                if (result != null)
                {
                    context.Bag.LastCreatedId = result[0];
                }
            }
        }
Beispiel #15
0
        //public static string AppendSqlOperator(FilterConstraintAppendContext context, SqlFilter filter, string fieldName, SqlOperator sqlOperator, DbType dbType, object value)
        //{
        //    StringBuilder sql = new StringBuilder();
        //    SqlStatement statement = filter.GetStatement();
        //    AppendSqlOperator(sqlOperator,new EntityField(fieldName,fieldName,dbType,EntityFieldFlags.Normal,4000), sql,value,statement,context);

        //    foreach(SqlStatementParameter parameter in statement.Parameters)
        //        filter.ExtraParameters.Add(parameter);

        //    return sql.ToString();
        //}

        public static IEnumerable <SqlStatementParameter> AppendSqlOperator(SqlOperator sqlOperator, EntityField field, StringBuilder sql, object value, SqlStatement statement, FilterConstraintAppendContext context)
        {
            var results = new List <SqlStatementParameter>();

            // any mangling?
            bool addParameter = true;

            sql.Append(" ");
            switch (sqlOperator)
            {
            case SqlOperator.EqualTo:
            case SqlOperator.NotEqualTo:
            case SqlOperator.GreaterThan:
            case SqlOperator.GreaterThanOrEqualTo:
            case SqlOperator.LessThan:
            case SqlOperator.LessThanOrEqualTo:
                if (!(value is DBNull))
                {
                    sql.Append(statement.Dialect.GetOperatorKeyword(sqlOperator, field.DBType));
                }
                else
                {
                    sql.Append(statement.Dialect.IsKeyword);
                    sql.Append(" ");
                    if (sqlOperator == SqlOperator.NotEqualTo)
                    {
                        sql.Append(statement.Dialect.NotKeyword);
                        sql.Append(" ");
                    }
                    sql.Append(statement.Dialect.NullKeyword);

                    // no need for param...
                    addParameter = false;
                }
                break;

            case SqlOperator.StartsWith:
            case SqlOperator.NotStartsWith:
            case SqlOperator.EndsWith:
            case SqlOperator.NotEndsWidth:
            case SqlOperator.Contains:
            case SqlOperator.NotContains:

                // add...
                switch (sqlOperator)
                {
                case SqlOperator.NotStartsWith:
                case SqlOperator.NotEndsWidth:
                case SqlOperator.NotContains:
                    sql.Append(statement.Dialect.NotKeyword);
                    sql.Append(" ");
                    break;
                }
                sql.Append(statement.Dialect.LikeKeyword);

                // mangle...
                if (value is DBNull)
                {
                    throw new InvalidOperationException(string.Format(Cultures.Exceptions, "Cannot do a '{0}' query with a null value.", sqlOperator));
                }
                value = ConversionHelper.ToString(value, Cultures.System);

                // append...
                switch (sqlOperator)
                {
                case SqlOperator.StartsWith:
                case SqlOperator.NotStartsWith:
                    value = (string)value + "%";
                    break;

                case SqlOperator.EndsWith:
                case SqlOperator.NotEndsWidth:
                    value = "%" + (string)value;
                    break;

                case SqlOperator.Contains:
                case SqlOperator.NotContains:
                    value = "%" + (string)value + "%";
                    break;

                default:
                    throw ExceptionHelper.CreateCannotHandleException(sqlOperator);
                }

                // done...
                break;

            case SqlOperator.Between:
            case SqlOperator.NotBetween:

                // values...
                // mbr - 21-04-2006 - datetime[] is not an object[]...
                //object[] values = (object[])value;
                Array values = (Array)value;

                // create...
                if (sqlOperator == SqlOperator.NotBetween)
                {
                    sql.Append(statement.Dialect.NotKeyword);
                    sql.Append(" ");
                }
                sql.Append(statement.Dialect.BetweenKeyword);
                sql.Append(" ");

                // first...
                SqlStatementParameter parameter = statement.OriginalCreator.CreateParameter(field, values.GetValue(0), context.CreateConstraintName());
                statement.Parameters.Add(parameter);
                results.Add(parameter);
                sql.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));

                // and...
                sql.Append(" ");
                sql.Append(statement.Dialect.AndKeyword);
                sql.Append(" ");

                // second...
                parameter = statement.OriginalCreator.CreateParameter(field, values.GetValue(1), context.CreateConstraintName());
                statement.Parameters.Add(parameter);
                results.Add(parameter);
                sql.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));

                // don't do default...
                addParameter = false;
                break;

            case SqlOperator.BitwiseAnd:

                // add...
                SqlStatementParameter bitwiseParameter = statement.OriginalCreator.CreateParameter(field, value, context.CreateConstraintName());
                statement.Parameters.Add(bitwiseParameter);                         // Check if the parameter has already been added
                results.Add(bitwiseParameter);

                sql.Append(" ");
                sql.Append(statement.Dialect.AsKeyword);
                sql.Append(" ");
                sql.Append(statement.Dialect.GetColumnTypeNativeName(DbType.Int32, false, 0));
                sql.Append(")");
                sql.Append(" ");
                sql.Append(statement.Dialect.BitwiseAndKeyword);
                sql.Append(" ");
                sql.Append(statement.Dialect.FormatVariableNameForQueryText(bitwiseParameter.Name));
                sql.Append(")");
                sql.Append(statement.Dialect.GetOperatorKeyword(SqlOperator.EqualTo, field.DBType));
                sql.Append(statement.Dialect.FormatVariableNameForQueryText(bitwiseParameter.Name));

                addParameter = false;

                break;

            default:
                throw ExceptionHelper.CreateCannotHandleException(sqlOperator);
            }

            // value...
            if (addParameter == true)
            {
                // space...
                sql.Append(" ");

                // add...
                SqlStatementParameter parameter = statement.OriginalCreator.CreateParameter(field, value, context.CreateConstraintName());
                statement.Parameters.Add(parameter); // Check if the parameter has already been added
                results.Add(parameter);

                sql.Append(statement.Dialect.FormatVariableNameForQueryText(parameter));
            }

            return(results);
        }
Beispiel #16
0
        /// <summary>
        /// Append SQL to select the extended properties from the extended table for the entity
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="builder"></param>
        /// <param name="field"></param>
        public override void AddExtendedPropertyToSelectStatement(SqlStatementCreator creator, SqlStatement statement, StringBuilder builder,
                                                                  EntityField field)
        {
            if (creator == null)
            {
                throw new ArgumentNullException("creator");
            }
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }
            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            // check...
            if (creator.EntityType == null)
            {
                throw new InvalidOperationException("creator.EntityType is null.");
            }

            // add...
            builder.Append("(");
            builder.Append(statement.Dialect.SelectKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatNativeName(GetColumnNameForDbType(field.DBType)));
            builder.Append(" ");
            builder.Append(statement.Dialect.FromKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatNativeName(creator.EntityType.NativeNameExtended));
            builder.Append(" ");
            builder.Append(statement.Dialect.WhereKeyword);
            builder.Append(" ");

            // where...
            creator.AppendConstraints(statement, builder);

            // create the param...
            SqlStatementParameter parameter = new SqlStatementParameter(string.Format("Extended{0}", field.NativeName), DbType.String,
                                                                        field.NativeName.Name);

            // param...
            builder.Append(" ");
            builder.Append(statement.Dialect.AndKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatNativeName(new NativeName("Name")));
            builder.Append("=");
            builder.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));

            // details...
            builder.Append(") AS ");
            builder.Append(statement.Dialect.FormatNativeName(field.NativeName));

            // add the parameter...
            statement.Parameters.Add(parameter);
        }
 /// <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(SqlStatementParameter item)
 {
     return(List.IndexOf(item));
 }
        // mbr - 06-01-2006 - added.
        public void Add(string name, DbType dbType, object value, ParameterDirection direction)
        {
            SqlStatementParameter parameter = new SqlStatementParameter(name, dbType, value, direction);

            this.Add(parameter);
        }
Beispiel #19
0
        /// <summary>
        /// Creates an Delete statement.
        /// </summary>
        /// <returns></returns>
        public override SqlStatement[] GetStatements(WorkUnitProcessingContext context)
        {
            // check...
            if (EntityType == null)
            {
                throw new ArgumentNullException("EntityType");
            }

            // create...
            SqlStatement statement = new SqlStatement(this.EntityType, this.Dialect);

            statement.OriginalWorkUnit = this;

            // sql...
            StringBuilder builder = new StringBuilder();

            builder.Append(statement.Dialect.DeleteFromKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatNativeName(this.EntityType.NativeName));
            builder.Append(" ");
            builder.Append(statement.Dialect.WhereKeyword);
            builder.Append(" ");

            // mbr - 13-10-2005 - rejigged to support partitioning...
            StringBuilder constraints = new StringBuilder();

            // key...
            EntityField[] fields = this.GetKeyFields();
            if (fields.Length == 0)
            {
                throw new InvalidOperationException("Key fields are zero length.");
            }

            // walk...
            for (int index = 0; index < fields.Length; index++)
            {
                // param...
                SqlStatementParameter parameter = this.CreateParameterForField(fields[index]);
                statement.Parameters.Add(parameter);

                // add...
                if (index > 0)
                {
                    constraints.Append(" ");
                    constraints.Append(statement.Dialect.AndKeyword);
                    constraints.Append(" ");
                }
                constraints.Append(statement.Dialect.FormatNativeName(fields[index].NativeName));
                constraints.Append("=");
                constraints.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));
            }

            // mbr - 13-10-2005 - rejigged to handle partitioning...
            string useConstraints = constraints.ToString();

            //if (this.EntityType.SupportsPartitioning)
            //{
            //    // get the strategy....
            //    PartitioningStrategy strategy = this.EntityType.PartitioningStrategy;
            //    if (strategy == null)
            //        throw new InvalidOperationException("strategy is null.");

            //    // mbr - 04-09-2007 - for c7 - can skip...
            //    if (strategy.ConstrianDeleteQuery)
            //    {
            //        // get the partition SQL...  (yes, this is for read, not for write.  for write really means 'for insert'.)
            //        // mbr - 04-09-2007 - for 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 - zero length can be ok...
            //        if (useConstraints.Length == 0 && !(strategy.IsZeroLengthIdSetOk))
            //            throw new InvalidOperationException("'useConstraints' is zero-length.");
            //    }
            //}

            // append...
            builder.Append(useConstraints);

            // return...
            statement.CommandText = builder.ToString();
            return(new SqlStatement[] { statement });
        }
Beispiel #20
0
        /// <summary>
        /// Adds constraints to the search.
        /// </summary>
        /// <param name="statement"></param>
        /// <param name="builder"></param>
        protected internal override void AppendConstraints(SqlStatement statement, StringBuilder builder)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            // the sql we want is something like this...
            // (firstname like @term0 or firstname like @term1) or (lastname like @term0 or lastname like @term2)

            // check...
            if (Dialect == null)
            {
                throw new InvalidOperationException("Dialect is null.");
            }

            if (Terms == null)
            {
                throw new InvalidOperationException("'Terms' is null.");
            }
            if (Terms.Length == 0)
            {
                throw new InvalidOperationException("'Terms' is zero-length.");
            }

            // create parameters...
            for (int index = 0; index < this.Terms.Length; index++)
            {
                // term...
                string term = this.Dialect.AllWildcard + this.Terms[index] + this.Dialect.AllWildcard;

                // create...
                SqlStatementParameter parameter = new SqlStatementParameter("term" + index.ToString(), DbType.String, term);
                statement.Parameters.Add(parameter);
            }

            // mbr - 13-10-2005 - the creator now does this...
//			// where...
//			builder.Append(" ");
//			builder.Append(this.Dialect.WhereKeyword);
//			builder.Append(" ");

            // walk each field in the type...
            if (EntityType == null)
            {
                throw new InvalidOperationException("EntityType is null.");
            }
            bool first = true;

            builder.Append("(");
            foreach (EntityField field in this.EntityType.Fields)
            {
                if (this.IsCandidate(field))
                {
                    // first?
                    if (first == true)
                    {
                        first = false;
                    }
                    else
                    {
                        builder.Append(" ");
                        builder.Append(this.Dialect.OrKeyword);
                        builder.Append(" ");
                    }

                    // create...
                    builder.Append("(");
                    for (int index = 0; index < this.Terms.Length; index++)
                    {
                        if (index > 0)
                        {
                            builder.Append(" ");
                            builder.Append(this.Dialect.OrKeyword);
                            builder.Append(" ");
                        }

                        // add...
                        builder.Append(this.Dialect.FormatNativeName(field.NativeName));
                        builder.Append(" ");
                        builder.Append(this.Dialect.LikeKeyword);
                        builder.Append(" ");
                        builder.Append(this.Dialect.FormatVariableNameForQueryText(statement.Parameters[index].Name));
                    }
                    builder.Append(")");
                }
            }
            builder.Append(")");

            // did we add any?
            if (first)
            {
                throw new InvalidOperationException(string.Format(string.Format("No candidate fields were found on '{0}'.", this.EntityType)));
            }

            // extra constraints...
            if (this.Constraints.Count > 0)
            {
                // create a context...
                FilterConstraintAppendContext context = new FilterConstraintAppendContext(this, statement, 0);

                // walk...
                foreach (FilterConstraint constraint in this.Constraints)
                {
                    // add...
                    context.ResetBuilder();
                    constraint.Append(context);

                    // join...
                    builder.Append(" and ");
                    builder.Append(context.Sql.ToString());
                }
            }
        }
Beispiel #21
0
        /// <summary>
        /// Creates an Delete statement.
        /// </summary>
        /// <returns></returns>
        public override SqlStatement[] GetStatements(WorkUnitProcessingContext context)
        {
            // check...
            if (EntityType == null)
            {
                throw new ArgumentNullException("EntityType");
            }
            if (Dialect == null)
            {
                throw new InvalidOperationException("Dialect is null.");
            }

            // mbr - 25-04-2007 - added touched...
            TouchedValueCollection block = this.AddTouchedValueBlock();

            if (block == null)
            {
                throw new InvalidOperationException("block is null.");
            }

            // create...
            SqlStatement statement = new SqlStatement(this.EntityType, this.Dialect);

            statement.OriginalWorkUnit = this;

            // sql...
            StringBuilder builder = new StringBuilder();

            builder.Append(statement.Dialect.UpdateKeyword);
            builder.Append(" ");
            builder.Append(statement.Dialect.FormatNativeName(this.EntityType.NativeName));
            builder.Append(" ");
            builder.Append(statement.Dialect.SetKeyword);
            builder.Append(" ");

            // do the non-key fields...
            EntityField[] fields = this.GetNonAutoIncrementFields();
            if (fields.Length == 0)
            {
                throw new InvalidOperationException("Non-key fields are zero length.");
            }

            // walk...
            int fieldCount = 0;

            for (int index = 0; index < fields.Length; index++)
            {
                // create a parameter...
                SqlStatementParameter parameter = this.CreateParameterForField(fields[index]);
                statement.Parameters.Add(parameter);
                parameter.RelatedField = fields[index];

                // mbr - 25-04-2007 - added touched...
                object originalValue = null;
                if (this.Entity is Entity && ((Entity)this.Entity).GetOriginalValue(fields[index], ref originalValue))
                {
                    block.Add(new TouchedValue(fields[index], originalValue, parameter.Value));
                }

                // add...
                if (fieldCount > 0)
                {
                    builder.Append(",");
                }
                builder.Append(statement.Dialect.FormatNativeName(fields[index].NativeName));
                builder.Append("=");
                builder.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));

                fieldCount++;
            }

            // where...
            builder.Append(" ");
            builder.Append(statement.Dialect.WhereKeyword);
            builder.Append(" ");

            // key...
            fields = this.GetKeyFields();
            if (fields.Length == 0)
            {
                throw new InvalidOperationException("Key fields are zero length.");
            }

            // walk...
            StringBuilder constraints = new StringBuilder();

            for (int index = 0; index < fields.Length; index++)
            {
                // param...
                SqlStatementParameter parameter = this.CreateParameterForField(fields[index]);
                statement.Parameters.Add(parameter);
                parameter.RelatedField = fields[index];

                // add...
                if (index > 0)
                {
                    constraints.Append(" ");
                    constraints.Append(statement.Dialect.AndKeyword);
                    constraints.Append(" ");
                }
                constraints.Append(statement.Dialect.FormatNativeName(fields[index].NativeName));
                constraints.Append("=");
                constraints.Append(statement.Dialect.FormatVariableNameForQueryText(parameter.Name));
            }

            // mbr - 13-10-2005 - rejigged to handle partitioning...
            string useConstraints = constraints.ToString();

            //if(this.EntityType.SupportsPartitioning)
            //{
            //    // get the strategy....
            //    PartitioningStrategy strategy = this.EntityType.PartitioningStrategy;
            //    if(strategy == null)
            //        throw new InvalidOperationException("strategy is null.");

            //    // mbr - 04-09-2007 - for c7 - need to be able to skip the the constraint check...
            //    if(strategy.ConstrainUpdateQuery)
            //    {
            //        // get the partition SQL...  (yes, this is for read, not for write.  for write really means 'for insert'.)
            //        // mbr - 04-09-2007 - for 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 - zero-length can be ok.
            //        if(useConstraints.Length == 0 && !(strategy.IsZeroLengthIdSetOk))
            //            throw new InvalidOperationException("'useConstraints' is zero-length.");
            //    }
            //}

            // append...
            builder.Append(useConstraints);

            // return...
            statement.CommandText = builder.ToString();
            return(new SqlStatement[] { statement });
        }