public virtual void UpdateAutoIncIdProperty(IObjectManager om, object obj, IClassMap classMap, object newId) { IContext ctx = m_SqlEngineManager.Context; string prevId; string autoPropName; IPropertyMap autoProp; object autoID = null; prevId = om.GetObjectIdentity(obj); autoProp = classMap.GetAutoIncreasingIdentityPropertyMap(); autoPropName = autoProp.Name; PropertyInfo propInfo = obj.GetType().GetProperty(autoProp.Name); if (propInfo.PropertyType == typeof(System.Int64)) { autoID = Convert.ToInt64(newId); } else if (propInfo.PropertyType == typeof(System.Int16)) { autoID = Convert.ToInt16(newId); } else if (propInfo.PropertyType == typeof(System.Double)) { autoID = Convert.ToDouble(newId); } else if (propInfo.PropertyType == typeof(System.Decimal)) { autoID = Convert.ToDecimal(newId); } else { autoID = Convert.ToInt32(newId); } om.SetPropertyValue(obj, autoPropName, autoID); //om.SetOriginalPropertyValue(obj, autoPropName, autoID); om.SetNullValueStatus(obj, autoPropName, false); ctx.IdentityMap.UpdateIdentity(obj, prevId); }
public override void InsertObject(object obj, IList stillDirty) { IList parameters = new ArrayList(); ArrayList propertyNames = new ArrayList(); IPropertyMap propertyMap; ArrayList nonPrimaryPropertyMaps = new ArrayList(); ArrayList collectionPropertyMaps = new ArrayList(); ArrayList sqlStatements = new ArrayList(); int rowsAffected; object autoID; IPropertyMap autoProp; IColumnMap autoPropCol; string autoPropName; string prevId; bool originalKeepOpen; IContext ctx = SqlEngineManager.Context; ObjectCancelEventArgs e = new ObjectCancelEventArgs(obj); ctx.EventManager.OnInsertingObject(this, e); if (e.Cancel) { return; } IObjectManager om = ctx.ObjectManager; IListManager lm = ctx.ListManager; IClassMap classMap = ctx.DomainMap.MustGetClassMap(obj.GetType()); string sqlSelect = ""; string sql; if (classMap.HasSingleIdAutoIncreaser()) { sql = GetInsertStatement(obj, propertyNames, stillDirty, nonPrimaryPropertyMaps, collectionPropertyMaps, ref sqlSelect, parameters); } else { sql = GetInsertStatement(obj, propertyNames, stillDirty, nonPrimaryPropertyMaps, collectionPropertyMaps, parameters); } IDataSource ds = ctx.DataSourceManager.GetDataSource(obj); if (classMap.HasSingleIdAutoIncreaser()) { originalKeepOpen = ds.KeepConnectionOpen; ds.KeepConnectionOpen = true; rowsAffected = ctx.SqlExecutor.ExecuteNonQuery(sql, ds, parameters); if (!(rowsAffected == 1)) { ds.KeepConnectionOpen = originalKeepOpen; throw new NPersistException("An exception occurred when inserting the row for a new object into the data source. Exactly one row should have been affected by the insert operation. Instead " + rowsAffected + " rows were affected!"); // do not localize } object[,] result = (object[, ])ctx.SqlExecutor.ExecuteArray(sqlSelect, ds, parameters); if (Util.IsArray(result)) { prevId = om.GetObjectIdentity(obj); autoProp = classMap.GetAutoIncreasingIdentityPropertyMap(); autoPropName = autoProp.Name; autoPropCol = autoProp.GetColumnMap(); if (autoPropCol.DataType == DbType.Int64) { autoID = Convert.ToInt64(result[0, 0]); } else if (autoPropCol.DataType == DbType.Int16) { autoID = Convert.ToInt16(result[0, 0]); } else { autoID = Convert.ToInt32(result[0, 0]); } om.SetPropertyValue(obj, autoPropName, autoID); //om.SetOriginalPropertyValue(obj, autoPropName, autoID); om.SetNullValueStatus(obj, autoPropName, false); ctx.IdentityMap.UpdateIdentity(obj, prevId); } else { ds.KeepConnectionOpen = originalKeepOpen; throw new FailedFetchingDbGeneratedValueException("Could not find auto-increasing ID for new object!"); // do not localize } ds.KeepConnectionOpen = originalKeepOpen; } else { rowsAffected = ctx.SqlExecutor.ExecuteNonQuery(sql, ds, parameters); if (!(rowsAffected == 1)) { throw new RowNotInsertedException("A new row was not inserted in the data source for a new object."); // do not localize } } // foreach (string propName in propertyNames) // { // om.SetOriginalPropertyValue(obj, propName, om.GetPropertyValue(obj, propName)); // } InsertNonPrimaryProperties(obj, nonPrimaryPropertyMaps, stillDirty); foreach (IPropertyMap iPpropertyMap in collectionPropertyMaps) { propertyMap = iPpropertyMap; sqlStatements.Clear(); GetInsertCollectionPropertyStatements(obj, propertyMap, sqlStatements, stillDirty); ds = ctx.DataSourceManager.GetDataSource(obj, propertyMap.Name); foreach (SqlStatementAndDbParameters sqlStatementAndDbParameters in sqlStatements) { sql = sqlStatementAndDbParameters.SqlStatement; rowsAffected = ctx.SqlExecutor.ExecuteNonQuery(sql, ds, sqlStatementAndDbParameters.DbParameters); if (!(rowsAffected == 1)) { throw new RowNotInsertedException("A new row was not inserted in the data source for a collection property of a new object."); // do not localize } } } ctx.InverseManager.NotifyCreate(obj); ObjectEventArgs e2 = new ObjectEventArgs(obj); ctx.EventManager.OnInsertedObject(this, e2); }