Example #1
0
        /// <summary>
        /// Returns true if the extension row for the entity exists.
        /// </summary>
        /// <returns></returns>
        protected bool DoesExtendedRowExist(WorkUnitProcessingContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // values...
            EntityField[] keyFields = this.EntityType.GetKeyFields();
            this.AssertKeyFields(keyFields);

            // values...
            object[] values = this.EntityType.Storage.GetKeyValues(this.Entity);
            if (values == null)
            {
                throw new InvalidOperationException("values is null.");
            }
            if (keyFields.Length != values.Length)
            {
                throw new InvalidOperationException(string.Format("Length mismatch for 'keyFields' and 'values': {0} cf {1}.", keyFields.Length, values.Length));
            }

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

            // builder...
            builder.Append("SELECT ");
            for (int index = 0; index < keyFields.Length; index++)
            {
                if (index > 0)
                {
                    builder.Append(", ");
                }
                builder.Append(sql.Dialect.FormatColumnName(
                                   ColumnPerFieldExtensibilityProvider.MangleIdColumnName(keyFields[index].NativeName)));
            }

            // from...
            builder.Append(" FROM ");
            builder.Append(sql.Dialect.FormatTableName(this.EntityType.NativeNameExtended));

            // values...
            this.AppendWhereClause(sql, builder, keyFields, values);

            // set...
            sql.CommandText = builder.ToString();

            // result...
            bool result = context.Connection.ExecuteExists(sql);

            return(result);
        }
Example #2
0
        /// <summary>
        /// Appends a WHERE clause to a statement.
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="builder"></param>
        /// <param name="keyFields"></param>
        /// <param name="keyValues"></param>
        protected void AppendWhereClause(SqlStatement sql, StringBuilder builder, EntityField[] keyFields, object[] keyValues)
        {
            if (sql == null)
            {
                throw new ArgumentNullException("sql");
            }
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }
            if (keyFields == null)
            {
                throw new ArgumentNullException("keyFields");
            }
            if (keyFields.Length == 0)
            {
                throw new ArgumentOutOfRangeException("'keyFields' is zero-length.");
            }
            if (keyValues == null)
            {
                throw new ArgumentNullException("keyValues");
            }
            if (keyFields.Length != keyValues.Length)
            {
                throw new InvalidOperationException(string.Format("Length mismatch for 'keyFields' and 'keyValues': {0} cf {1}.", keyFields.Length, keyValues.Length));
            }

            // where...
            builder.Append(" WHERE ");
            for (int index = 0; index < keyFields.Length; index++)
            {
                if (index > 0)
                {
                    builder.Append(" AND ");
                }
                builder.Append(sql.Dialect.FormatColumnName(
                                   ColumnPerFieldExtensibilityProvider.MangleIdColumnName(keyFields[index].NativeName)));
                builder.Append("=");
                builder.Append(sql.Dialect.FormatVariableNameForQueryText(sql.Parameters.Add(keyFields[index].DBType, keyValues[index])));
            }
        }
Example #3
0
        /// <summary>
        /// Gets the statements to do an insert.
        /// </summary>
        /// <returns></returns>
        protected SqlStatement[] GetInsertStatements(WorkUnitProcessingContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

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

            // keys...
            EntityField[] keyFields = this.EntityType.GetKeyFields();
            AssertKeyFields(keyFields);

            // key values...
            object[] keyValues = this.EntityType.Storage.GetKeyValues(this.Entity);
            if (keyValues == null)
            {
                throw new InvalidOperationException("keyValues is null.");
            }
            if (keyFields.Length != keyValues.Length)
            {
                throw new InvalidOperationException(string.Format("Length mismatch for 'keyFields' and 'keyValues': {0} cf {1}.", keyFields.Length, keyValues.Length));
            }

            // fields...
            EntityField[] fields = this.GetFields();
            if (fields == null)
            {
                throw new InvalidOperationException("'fields' is null.");
            }
            if (fields.Length == 0)
            {
                throw new InvalidOperationException("'fields' is zero-length.");
            }

            // values...
            object[] values = this.GetValues();
            if (values == null)
            {
                throw new InvalidOperationException("values is null.");
            }
            if (fields.Length != values.Length)
            {
                throw new InvalidOperationException(string.Format("Length mismatch for 'fields' and 'values': {0} cf {1}.", fields.Length, values.Length));
            }

            // fields...
            ArrayList allFields = new ArrayList();

            allFields.AddRange(keyFields);
            allFields.AddRange(fields);

            // values...
            ArrayList allValues = new ArrayList();

            allValues.AddRange(keyValues);
            allValues.AddRange(values);

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

            // get...
            builder.Append(sql.Dialect.InsertIntoKeyword);
            builder.Append(" ");
            builder.Append(sql.Dialect.FormatTableName(this.EntityType.NativeNameExtended));
            builder.Append(" (");
            for (int index = 0; index < allFields.Count; index++)
            {
                if (index > 0)
                {
                    builder.Append(", ");
                }

                // field...
                EntityField field = (EntityField)allFields[index];

                // key?
                if (field.IsKey())
                {
                    builder.Append(sql.Dialect.FormatColumnName(ColumnPerFieldExtensibilityProvider.MangleIdColumnName(field.NativeName)));
                }
                else
                {
                    builder.Append(sql.Dialect.FormatColumnName(field.NativeName));
                }
            }
            builder.Append(") ");
            builder.Append(sql.Dialect.ValuesKeyword);
            builder.Append(" (");
            for (int index = 0; index < allFields.Count; index++)
            {
                if (index > 0)
                {
                    builder.Append(", ");
                }

                // value...
                object value = allValues[index];

                // mbr - 10-10-2007 - for c7 - before we wouldn't have had a reconciled value here, but now that the
                // reconciliation step has moved, we do.
                EntityField field = (EntityField)allFields[index];
//				if(field.IsKey())
//					value = context.Bag.LastCreatedId;
//				else if(value == null)
//					value = DBNull.Value;
                if (value == null)
                {
                    value = DBNull.Value;
                }

                // add...
                builder.Append(sql.Dialect.FormatVariableNameForQueryText(sql.Parameters.Add(field.DBType, value)));
            }
            builder.Append(")");

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