/// <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(); }
private SqlString GenerateLockString() { ISessionFactoryImplementor factory = lockable.Factory; SqlUpdateBuilder update = new SqlUpdateBuilder(factory.Dialect, factory); update.SetTableName(lockable.RootTableName); update.SetIdentityColumn(lockable.RootTableIdentifierColumnNames, lockable.IdentifierType); update.SetVersionColumn(new string[] { lockable.VersionColumnName }, lockable.VersionType); update.AddColumns(new string[] { lockable.VersionColumnName }, null, lockable.VersionType); if (factory.Settings.IsCommentsEnabled) { update.SetComment(lockMode + " lock " + lockable.EntityName); } 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 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(); }