public MultiTableUpdateExecutor(IStatement statement) : base(statement, log) { if (!Factory.Dialect.SupportsTemporaryTables) { throw new HibernateException("cannot perform multi-table updates using dialect not supporting temp tables"); } var updateStatement = (UpdateStatement) statement; FromElement fromElement = updateStatement.FromClause.GetFromElement(); string bulkTargetAlias = fromElement.TableAlias; persister = fromElement.Queryable; idInsertSelect = GenerateIdInsertSelect(persister, bulkTargetAlias, updateStatement.WhereClause); log.Debug("Generated ID-INSERT-SELECT SQL (multi-table update) : " + idInsertSelect); string[] tableNames = persister.ConstraintOrderedTableNameClosure; string[][] columnNames = persister.ConstraintOrderedTableKeyColumnClosure; string idSubselect = GenerateIdSubselect(persister); IList<AssignmentSpecification> assignmentSpecifications = Walker.AssignmentSpecifications; updates = new SqlString[tableNames.Length]; hqlParameters = new IParameterSpecification[tableNames.Length][]; for (int tableIndex = 0; tableIndex < tableNames.Length; tableIndex++) { bool affected = false; var parameterList = new List<IParameterSpecification>(); SqlUpdateBuilder update = new SqlUpdateBuilder(Factory.Dialect, Factory).SetTableName(tableNames[tableIndex]) .SetWhere( string.Format("({0}) IN ({1})", StringHelper.Join(", ", columnNames[tableIndex]), idSubselect)); if (Factory.Settings.IsCommentsEnabled) { update.SetComment("bulk update"); } foreach (var specification in assignmentSpecifications) { if (specification.AffectsTable(tableNames[tableIndex])) { affected = true; update.AppendAssignmentFragment(specification.SqlAssignmentFragment); if (specification.Parameters != null) { for (int paramIndex = 0; paramIndex < specification.Parameters.Length; paramIndex++) { parameterList.Add(specification.Parameters[paramIndex]); } } } } if (affected) { updates[tableIndex] = update.ToSqlString(); hqlParameters[tableIndex] = parameterList.ToArray(); } } }
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(); }
/// <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 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(); }
/// <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(); }