/// <summary>
		/// Generate the SQL UPDATE that updates a foreign key to a value
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateInsertRowString()
		{
			SqlUpdateBuilder update = new SqlUpdateBuilder(Factory);
			update.SetTableName(qualifiedTableName)
				.AddColumns(KeyColumnNames, KeyType)
				.SetIdentityColumn(ElementColumnNames, ElementType);
			if (HasIndex)
			{
				update.AddColumns(IndexColumnNames, IndexType);
			}
			//identifier collections not supported for 1-to-many 

			return update.ToSqlCommandInfo();
		}
		/// <summary>
		/// Generate the SQL UPDATE that updates all the foreign keys to null
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateDeleteString()
		{
			SqlUpdateBuilder update = new SqlUpdateBuilder(Factory.Dialect, Factory)
				.SetTableName(qualifiedTableName)
				.AddColumns(KeyColumnNames, "null")
				.SetIdentityColumn(KeyColumnNames, KeyType);
			if (HasIndex)
				update.AddColumns(IndexColumnNames, "null");

			if (HasWhere)
				update.AddWhereFragment(sqlWhereString);

			if (Factory.Settings.IsCommentsEnabled)
				update.SetComment("delete one-to-many " + Role);

			return update.ToSqlCommandInfo();
		}
		/// <summary>
		/// Generate the SQL UPDATE that updates all the foreign keys to null
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateDeleteString()
		{
			SqlUpdateBuilder update = new SqlUpdateBuilder(Factory)
				.SetTableName(qualifiedTableName)
				.AddColumns(KeyColumnNames, "null")
				.SetIdentityColumn(KeyColumnNames, KeyType);
			if (HasIndex)
			{
				update.AddColumns(IndexColumnNames, "null");
			}
			if (HasWhere)
			{
				update.AddWhereFragment(sqlWhereString);
			}

			return update.ToSqlCommandInfo();
		}
		public void UpdateStringSqlTest()
		{
			Configuration cfg = new Configuration();
			ISessionFactory factory = cfg.BuildSessionFactory();

			ISessionFactoryImplementor factoryImpl = (ISessionFactoryImplementor) factory;
			SqlUpdateBuilder update = new SqlUpdateBuilder(factoryImpl.Dialect, factoryImpl);

			update.SetTableName("test_update_builder");

			update.AddColumns(new string[] {"intColumn"}, NHibernateUtil.Int32);
			update.AddColumns(new string[] {"longColumn"}, NHibernateUtil.Int64);
			update.AddColumn("literalColumn", false, (ILiteralType) NHibernateUtil.Boolean);
			update.AddColumn("stringColumn", 5.ToString());

			update.SetIdentityColumn(new string[] {"decimalColumn"}, NHibernateUtil.Decimal);
			update.SetVersionColumn(new string[] {"versionColumn"}, (IVersionType) NHibernateUtil.Int32);

			update.AddWhereFragment("a=b");
			SqlCommandInfo sqlCommand = update.ToSqlCommandInfo();

			Assert.AreEqual(CommandType.Text, sqlCommand.CommandType);
            string falseString = factoryImpl.Dialect.ToBooleanValueString(false);
            string expectedSql =
                "UPDATE test_update_builder SET intColumn = ?, longColumn = ?, literalColumn = " + falseString + ", stringColumn = 5 WHERE decimalColumn = ? AND versionColumn = ? AND a=b";
			Assert.AreEqual(expectedSql, sqlCommand.Text.ToString(), "SQL String");

			SqlType[] actualParameterTypes = sqlCommand.ParameterTypes;
			Assert.AreEqual(4, actualParameterTypes.Length, "Four parameters");

			SqlType[] expectedParameterTypes = new SqlType[]
				{
					SqlTypeFactory.Int32,
					SqlTypeFactory.Int64,
					SqlTypeFactory.Decimal,
					SqlTypeFactory.Int32
				};

			Assert.AreEqual(expectedParameterTypes[0], actualParameterTypes[0], "firstParam Type");
			Assert.AreEqual(expectedParameterTypes[1], actualParameterTypes[1], "secondParam Type");
			Assert.AreEqual(expectedParameterTypes[2], actualParameterTypes[2], "thirdParam Type");
			Assert.AreEqual(expectedParameterTypes[3], actualParameterTypes[3], "fourthParam Type");
		}
		/// <summary> Generate the SQL that updates a row by id (and version)</summary>
		protected internal SqlCommandInfo GenerateUpdateString(bool[] includeProperty, int j, object[] oldFields, bool useRowId)
		{
			SqlUpdateBuilder updateBuilder = new SqlUpdateBuilder(Factory.Dialect, Factory)
				.SetTableName(GetTableName(j));

			// select the correct row by either pk or rowid
			if (useRowId)
				updateBuilder.SetIdentityColumn(new string[] { rowIdName }, NHibernateUtil.Int32); //TODO: eventually, rowIdName[j]
			else
				updateBuilder.SetIdentityColumn(GetKeyColumns(j), IdentifierType);

			bool hasColumns = false;
			for (int i = 0; i < entityMetamodel.PropertySpan; i++)
			{
				if (includeProperty[i] && IsPropertyOfTable(i, j))
				{
					// this is a property of the table, which we are updating
					updateBuilder.AddColumns(GetPropertyColumnNames(i), propertyColumnUpdateable[i], PropertyTypes[i]);
					hasColumns = hasColumns || GetPropertyColumnSpan(i) > 0;
				}
			}

			if (j == 0 && IsVersioned && entityMetamodel.OptimisticLockMode == Versioning.OptimisticLock.Version)
			{
				// this is the root (versioned) table, and we are using version-based
				// optimistic locking;  if we are not updating the version, also don't
				// check it (unless this is a "generated" version column)!
				if (CheckVersion(includeProperty))
				{
					updateBuilder.SetVersionColumn(new string[] { VersionColumnName }, VersionType);
					hasColumns = true;
				}
			}
			else if (entityMetamodel.OptimisticLockMode > Versioning.OptimisticLock.Version && oldFields != null)
			{
				// we are using "all" or "dirty" property-based optimistic locking
				bool[] includeInWhere =
					OptimisticLockMode == Versioning.OptimisticLock.All
						? PropertyUpdateability
						: includeProperty; //optimistic-lock="dirty", include all properties we are updating this time

				bool[] versionability = PropertyVersionability;
				IType[] types = PropertyTypes;

				for (int i = 0; i < entityMetamodel.PropertySpan; i++)
				{
					bool include = includeInWhere[i] && IsPropertyOfTable(i, j) && versionability[i];
					if (include)
					{
						// this property belongs to the table, and it is not specifically
						// excluded from optimistic locking by optimistic-lock="false"
						string[] _propertyColumnNames = GetPropertyColumnNames(i);
						bool[] propertyNullness = types[i].ToColumnNullness(oldFields[i], Factory);
						SqlType[] sqlt = types[i].SqlTypes(Factory);
						for (int k = 0; k < propertyNullness.Length; k++)
						{
							if (propertyNullness[k])
							{
								updateBuilder.AddWhereFragment(_propertyColumnNames[k], sqlt[k], " = ");
							}
							else
							{
								updateBuilder.AddWhereFragment(_propertyColumnNames[k] + " is null");
							}
						}
					}
				}
			}

			if (Factory.Settings.IsCommentsEnabled)
			{
				updateBuilder.SetComment("update " + EntityName);
			}

			return hasColumns ? updateBuilder.ToSqlCommandInfo() : null;
		}
		private SqlCommandInfo GenerateVersionIncrementUpdateString()
		{
			SqlUpdateBuilder update = new SqlUpdateBuilder(Factory.Dialect, Factory);
			update.SetTableName(GetTableName(0));
			if (Factory.Settings.IsCommentsEnabled)
			{
				update.SetComment("forced version increment");
			}
			update.AddColumn(VersionColumnName, VersionType);
			update.SetIdentityColumn(IdentifierColumnNames, IdentifierType);
			update.SetVersionColumn(new string[] { VersionColumnName }, VersionType);
			return update.ToSqlCommandInfo();
		}
		/// <summary>
		/// Generate the SQL UPDATE that updates a foreign key to a value
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateInsertRowString()
		{
			var update = new SqlUpdateBuilder(Factory.Dialect, Factory);

			update.SetTableName(qualifiedTableName)
				.AddColumns(KeyColumnNames, KeyType);

			if (HasIndex && !indexContainsFormula)
				update.AddColumns(IndexColumnNames, IndexType);

			//identifier collections not supported for 1-to-many 
			if (Factory.Settings.IsCommentsEnabled)
				update.SetComment("create one-to-many row " + Role);

			update.SetIdentityColumn(ElementColumnNames, ElementType);

			return update.ToSqlCommandInfo();
		}
		/// <summary>
		/// Generate the SQL UPDATE that updates a particular row's foreign
		/// key to null
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateDeleteRowString()
		{
			var update = new SqlUpdateBuilder(Factory.Dialect, Factory);
			update.SetTableName(qualifiedTableName)
				.AddColumns(KeyColumnNames, "null");

			if (HasIndex && !indexContainsFormula)
				update.AddColumns(IndexColumnNames, "null");

			if (Factory.Settings.IsCommentsEnabled)
				update.SetComment("delete one-to-many row " + Role);

			//use a combination of foreign key columns and pk columns, since
			//the ordering of removal and addition is not guaranteed when
			//a child moves from one parent to another
			update
				.AddWhereFragment(KeyColumnNames, KeyType, " = ")
				.AddWhereFragment(ElementColumnNames, ElementType, " = ");

			return update.ToSqlCommandInfo();
		}
		/// <summary>
		/// Generate the SQL UPDATE that updates a row
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateUpdateRowString()
		{
			SqlUpdateBuilder update = new SqlUpdateBuilder(Factory.Dialect, Factory)
				.SetTableName(qualifiedTableName)
				.AddColumns(ElementColumnNames, elementColumnIsSettable, ElementType);
			if (hasIdentifier)
			{
				update.AddWhereFragment(new string[] { IdentifierColumnName }, IdentifierType, " = ");
			}
			else if (HasIndex && !indexContainsFormula)
			{
				update.AddWhereFragment(KeyColumnNames, KeyType, " = ")
					.AddWhereFragment(IndexColumnNames, IndexType, " = ");
			}
			else
			{
				string[] cnames = ArrayHelper.Join(KeyColumnNames, ElementColumnNames, elementColumnIsInPrimaryKey);
				SqlType[] ctypes = ArrayHelper.Join(KeyType.SqlTypes(Factory), ElementType.SqlTypes(Factory), elementColumnIsInPrimaryKey);
				update.AddWhereFragment(cnames, ctypes, " = ");
			}

			if (Factory.Settings.IsCommentsEnabled)
				update.SetComment("update collection row " + Role);

			return update.ToSqlCommandInfo();
		}
		protected virtual SqlCommandInfo GenerateUpdateString(bool[] includeProperty, int j, object[] oldFields)
		{
			SqlUpdateBuilder updateBuilder = new SqlUpdateBuilder(Factory)
				.SetTableName(GetTableName(j))
				.SetIdentityColumn(GetKeyColumns(j), IdentifierType);

			bool hasColumns = false;
			for (int i = 0; i < entityMetamodel.PropertySpan; i++)
			{
				if (includeProperty[i] && IsPropertyOfTable(i, j))
				{
					updateBuilder.AddColumns(GetPropertyColumnNames(i), propertyColumnUpdateable[i], PropertyTypes[i]);
					hasColumns = hasColumns || GetPropertyColumnSpan(i) > 0;
				}
			}

			if (j == 0 && IsVersioned && entityMetamodel.OptimisticLockMode == OptimisticLockMode.Version)
			{
				updateBuilder.SetVersionColumn(new string[] {VersionColumnName}, VersionType);
				hasColumns = true;
			}
			else if (entityMetamodel.OptimisticLockMode > OptimisticLockMode.Version && oldFields != null)
			{
				bool[] versionability = PropertyVersionability;
				bool[] includeInWhere =
					OptimisticLockMode == OptimisticLockMode.All
						? PropertyUpdateability
						: includeProperty;

				for (int i = 0; i < entityMetamodel.PropertySpan; i++)
				{
					bool include = includeInWhere[i] &&
					               IsPropertyOfTable(i, j) &&
					               versionability[i];
					if (include)
					{
						string[] propertyColumnNames = GetPropertyColumnNames(i);
						if (PropertyTypes[i].IsDatabaseNull(oldFields[i]))
						{
							foreach (string column in propertyColumnNames)
							{
								updateBuilder.AddWhereFragment(column + " is null");
							}
						}
						else
						{
							updateBuilder.AddWhereFragment(propertyColumnNames, PropertyTypes[i], "=");
						}
					}
				}
			}

			return hasColumns ? updateBuilder.ToSqlCommandInfo() : null;
		}
		/// <summary>
		/// Generate the SQL UPDATE that updates a row
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateUpdateRowString()
		{
			SqlUpdateBuilder update = new SqlUpdateBuilder(Factory)
				.SetTableName(qualifiedTableName)
				.AddColumns(ElementColumnNames, elementColumnIsSettable, ElementType);
			if (hasIdentifier)
			{
				update.AddWhereFragment(new string[] {IdentifierColumnName}, IdentifierType, " = ");
			}
			else if (HasIndex /* && !indexContainsFormula */)
			{
				update.AddWhereFragment(KeyColumnNames, KeyType, " = ")
					.AddWhereFragment(IndexColumnNames, IndexType, " = ");
			}
			else
			{
				update.AddWhereFragment(KeyColumnNames, KeyType, " = ")
					.AddWhereFragment(ElementColumnNames, ElementType, " = ");
			}

			return update.ToSqlCommandInfo();
		}
		/// <summary>
		/// Generate the SQL UPDATE that updates a particular row's foreign
		/// key to null
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateDeleteRowString()
		{
			SqlUpdateBuilder update = new SqlUpdateBuilder(Factory);
			update.SetTableName(qualifiedTableName)
				.AddColumns(KeyColumnNames, "null");

			if (HasIndex /* && TODO H3: !indexContainsFormula */)
			{
				update.AddColumns(IndexColumnNames, "null");
			}

			update.AddWhereFragment(KeyColumnNames, KeyType, " = ")
				.AddWhereFragment(ElementColumnNames, ElementType, " = ");

			return update.ToSqlCommandInfo();
		}
		/// <summary>
		/// Generate the SQL UPDATE that updates all the foreign keys to null
		/// </summary>
		/// <returns></returns>
		protected override SqlCommandInfo GenerateDeleteString()
		{
			var update = new SqlUpdateBuilder(Factory.Dialect, Factory)
				.SetTableName(qualifiedTableName)
				.AddColumns(JoinColumnNames, "null");

			if (CollectionType.UseLHSPrimaryKey)
			{
				update.SetIdentityColumn(KeyColumnNames, KeyType);
			}
			else
			{
				var ownerPersister = (IOuterJoinLoadable)OwnerEntityPersister;
				update.SetJoin(ownerPersister.TableName, KeyColumnNames, KeyType, JoinColumnNames, ownerPersister.GetPropertyColumnNames(CollectionType.LHSPropertyName));
			}

			if (HasIndex)
				update.AddColumns(IndexColumnNames, "null");

			if (HasWhere)
				update.AddWhereFragment(sqlWhereString);

			if (Factory.Settings.IsCommentsEnabled)
				update.SetComment("delete one-to-many " + Role);

			return update.ToSqlCommandInfo();
		}