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 that updates rows by id (and version) /// </summary> /// <param name="includeProperty"></param> /// <returns>An array of SqlStrings</returns> protected virtual SqlString[ ] GenerateUpdateStrings( bool[ ] includeProperty ) { SqlString[ ] results = new SqlString[naturalOrderTableNames.Length]; for( int j = 0; j < naturalOrderTableNames.Length; j++ ) { SqlUpdateBuilder updateBuilder = new SqlUpdateBuilder( factory ) .SetTableName( naturalOrderTableNames[ j ] ) .SetIdentityColumn( naturalOrderTableKeyColumns[ j ], IdentifierType ); if( j == 0 && IsVersioned ) { updateBuilder.SetVersionColumn( new string[ ] {VersionColumnName}, VersionType ); } //TODO: figure out what the hasColumns variable comes into play for?? bool hasColumns = false; for( int i = 0; i < propertyColumnNames.Length; i++ ) { if( includeProperty[ i ] && naturalOrderPropertyTables[ i ] == j ) { updateBuilder.AddColumns( propertyColumnNames[ i ], PropertyTypes[ i ] ); hasColumns = hasColumns || propertyColumnNames[ i ].Length > 0; } } results[ j ] = hasColumns ? updateBuilder.ToSqlString() : null; } return results; }
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; }