/// <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 SqlString GenerateUpdateString(bool[] includeProperty, object[] oldFields) { SqlUpdateBuilder builder = GenerateUpdate(includeProperty); if (OptimisticLockMode > OptimisticLockMode.Version && oldFields != null) { bool[] includeInWhere = OptimisticLockMode == OptimisticLockMode.All ? PropertyUpdateability : includeProperty; for (int i = 0; i < HydrateSpan; i++) { if (includeInWhere[i]) { if (oldFields[i] == null) { foreach (string column in propertyColumnNames[i]) { builder.AddWhereFragment(column + " is null"); } } else { builder.AddWhereFragment(propertyColumnNames[i], PropertyTypes[i], "="); } } } } return(builder.ToSqlString()); }
/// <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.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 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()); }
/// <summary> /// Generate the SQL UPDATE that updates a row /// </summary> /// <returns></returns> protected override SqlString GenerateUpdateRowString() { SqlUpdateBuilder update = new SqlUpdateBuilder(factory) .SetTableName(qualifiedTableName) .AddColumns(ElementColumnNames, ElementType); if (hasIdentifier) { update.AddWhereFragment(rowSelectColumnNames, rowSelectType, " = "); } else { update.AddWhereFragment(KeyColumnNames, KeyType, " = ") .AddWhereFragment(rowSelectColumnNames, rowSelectType, " = "); } return(update.ToSqlString()); }
/// <summary> /// Generate the SQL UPDATE that updates all the foreign keys to null /// </summary> /// <returns></returns> protected override SqlString 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.ToSqlString()); }
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 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()); }