private void EnsureConsistency(Type type, IClassMap classMap) { IContext ctx = this.Context; if (ctx.ReadConsistency.Equals(ConsistencyMode.Pessimistic) || ctx.WriteConsistency.Equals(ConsistencyMode.Pessimistic)) { ISourceMap sourceMap = classMap.GetSourceMap(); if (sourceMap != null) { if (sourceMap.PersistenceType.Equals(PersistenceType.ObjectRelational) || sourceMap.PersistenceType.Equals(PersistenceType.Default)) { ITransaction tx = ctx.GetTransaction(ctx.GetDataSource(sourceMap).GetConnection()); if (tx == null) { if (ctx.WriteConsistency.Equals(ConsistencyMode.Pessimistic)) { throw new WriteConsistencyException( string.Format("A write consistency exception has occurred. An object of type {0} was created outside of a transaction. This is not permitted in a context using Pessimistic WriteConsistency.", type), null); } throw new ReadConsistencyException( string.Format("A read consistency exception has occurred. An object of type {0} was created outside of a transaction. This is not permitted in a context using Pessimistic ReadConsistency.", type), null); } } } } }
private void EnsureReadConsistency(object obj, string propertyName) { IContext ctx = this.Context; if (ctx.ReadConsistency.Equals(ConsistencyMode.Pessimistic)) { IIdentityHelper identityHelper = obj as IIdentityHelper; if (identityHelper == null) { throw new NPersistException(string.Format("Object of type {0} does not implement IIdentityHelper", obj.GetType())); } IClassMap classMap = ctx.DomainMap.MustGetClassMap(obj.GetType()); ISourceMap sourceMap = classMap.GetSourceMap(); if (sourceMap != null) { if (sourceMap.PersistenceType.Equals(PersistenceType.ObjectRelational) || sourceMap.PersistenceType.Equals(PersistenceType.Default)) { ITransaction tx = ctx.GetTransaction(ctx.GetDataSource(sourceMap).GetConnection()); if (tx == null) { throw new ReadConsistencyException( string.Format("A read consistency exception has occurred. The property {0} for the object of type {1} and with identity {2} was loaded or initialized with a value outside of a transaction. This is not permitted in a context using Pessimistic ReadConsistency.", propertyName, obj.GetType(), ctx.ObjectManager.GetObjectIdentity(obj)), obj); } Guid txGuid = identityHelper.GetTransactionGuid(); if (!(tx.Guid.Equals(txGuid))) { throw new ReadConsistencyException( string.Format("A read consistency exception has occurred. The property {0} for the object of type {1} and with identity {2} has already been loaded or initialized inside a transactions with Guid {3} and was now loaded or initialized again under another transaction with Guid {4}. This is not permitted in a context using Pessimistic ReadConsistency.", propertyName, obj.GetType(), ctx.ObjectManager.GetObjectIdentity(obj), txGuid, tx.Guid), txGuid, tx.Guid, obj); } } } } }
private bool HasCount(ref int count) { IContext context = interceptable.GetInterceptor().Context; PropertyStatus propStatus = context.ObjectManager.GetPropertyStatus(interceptable, propertyName); if (propStatus != PropertyStatus.NotLoaded) { return(false); } IInverseHelper inverseHelper = interceptable as IInverseHelper; if (inverseHelper == null) { return(false); } ITransaction tx = null; ConsistencyMode readConsistency = context.ReadConsistency; if (readConsistency == ConsistencyMode.Pessimistic) { IClassMap classMap = context.DomainMap.MustGetClassMap(interceptable.GetType()); ISourceMap sourceMap = classMap.GetSourceMap(); if (sourceMap != null) { if (sourceMap.PersistenceType.Equals(PersistenceType.ObjectRelational) || sourceMap.PersistenceType.Equals(PersistenceType.Default)) { tx = context.GetTransaction(context.GetDataSource(sourceMap).GetConnection()); if (tx == null) { return(false); } } } } if (inverseHelper.HasCount(propertyName, tx)) { count = inverseHelper.GetCount(propertyName, tx); return(true); } return(false); }
private void SetTransactionGuid(object obj) { IIdentityHelper identityHelper = obj as IIdentityHelper; if (identityHelper == null) { throw new NPersistException(string.Format("Object of type {0} does not implement IIdentityHelper", obj.GetType())); } IContext ctx = this.Context; IClassMap classMap = ctx.DomainMap.MustGetClassMap(obj.GetType()); ISourceMap sourceMap = classMap.GetSourceMap(); if (sourceMap != null) { if (sourceMap.PersistenceType.Equals(PersistenceType.ObjectRelational) || sourceMap.PersistenceType.Equals(PersistenceType.Default)) { IDataSource ds = ctx.GetDataSource(sourceMap); if (ds != null && ds.HasConnection()) { ITransaction tx = ctx.GetTransaction(ds.GetConnection()); //This may throw if ReadConsistency is Pessimistic and a tx guid has already been set... if (tx != null) { identityHelper.SetTransactionGuid(tx.Guid); } else { identityHelper.SetTransactionGuid(Guid.Empty); } } else { identityHelper.SetTransactionGuid(Guid.Empty); } } } }
protected virtual void CreateTypeColumnForClass(IClassMap classMap, IDomainMap targetDomainMap, bool generateMappings) { ITableMap tableMap = null; ISourceMap sourceMap = null; ISourceMap addToSourceMap = null; IClassMap classMapClone = null; IColumnMap columnMap = null; string tableName = ""; string name = ""; sourceMap = classMap.GetSourceMap(); if (sourceMap == null) { sourceMap = classMap.DomainMap.GetSourceMap(); if (sourceMap == null) { throw new Exception("No default data source specified for domain model! Can't add table for class!"); } } addToSourceMap = targetDomainMap.GetSourceMap(sourceMap.Name); if (addToSourceMap == null) { addToSourceMap = (ISourceMap) sourceMap.Clone(); addToSourceMap.DomainMap = targetDomainMap; } if (classMap.Table.Length > 0) { tableName = classMap.Table; } else { tableName = GetTableNameForClass(classMap); } tableMap = addToSourceMap.GetTableMap(tableName); if (tableMap == null) { tableMap = new TableMap(); tableMap.Name = tableName; tableMap.SourceMap = addToSourceMap; } if (classMap.TypeColumn.Length > 0) { name = classMap.TypeColumn; } else { name = GetTypeColumnNameForClass(classMap); } columnMap = tableMap.GetColumnMap(name); if (columnMap == null) { columnMap = new ColumnMap(); columnMap.Name = name; columnMap.TableMap = tableMap; } columnMap.DataType = DbType.AnsiStringFixedLength; columnMap.Length = 1; columnMap.Precision = 1; columnMap.Scale = 0; columnMap.IsPrimaryKey = true; columnMap.AllowNulls = false; if (generateMappings & classMap.TypeColumn.Length < 1) { classMapClone = targetDomainMap.GetClassMap(classMap.Name); if (classMapClone == null) { classMapClone = (IClassMap) classMap.Clone(); classMapClone.DomainMap = targetDomainMap; } classMapClone.TypeColumn = name; } }
protected virtual void CreateTableForClass(IClassMap classMap, IDomainMap targetDomainMap, bool generateMappings) { ITableMap tableMap = null; ISourceMap addToSourceMap = null; IClassMap classMapClone = null; string name = ""; ISourceMap sourceMap = classMap.GetSourceMap(); if (sourceMap == null) { sourceMap = classMap.DomainMap.GetSourceMap(); if (sourceMap == null) { throw new Exception("No default data source specified for domain model! Can't add table for class!"); } } addToSourceMap = targetDomainMap.GetSourceMap(sourceMap.Name); if (addToSourceMap == null) { addToSourceMap = (ISourceMap) sourceMap.Clone(); addToSourceMap.DomainMap = targetDomainMap; } if (classMap.Table.Length > 0) { name = classMap.Table; } else { name = GetTableNameForClass(classMap); } tableMap = addToSourceMap.GetTableMap(name); if (tableMap == null) { tableMap = new TableMap(); tableMap.Name = name; tableMap.SourceMap = addToSourceMap; } if (generateMappings & classMap.Table.Length < 1) { classMapClone = targetDomainMap.GetClassMap(classMap.Name); if (classMapClone == null) { classMapClone = (IClassMap) classMap.Clone(); classMapClone.DomainMap = targetDomainMap; } classMapClone.Table = tableMap.Name; if (!(addToSourceMap.Name == targetDomainMap.Source)) { classMapClone.Source = addToSourceMap.Name; } classMapClone.DomainMap = targetDomainMap; } }
private void RunQuery(bool eval) { string query = GetQuery(); if (query == "") { MessageBox.Show("You must enter a query first!"); return; } try { NPathQuery npath = new NPathQuery(query); IContext context = GetContext(); IClassMap classMap = context.NPathEngine.GetRootClassMap(query, context.DomainMap); Type type = context.AssemblyManager.MustGetTypeFromClassMap(classMap); if (type == null) { throw new Exception("Could not find type for classMap " + classMap.Name); } npath.Context = context; npath.PrimaryType = type; NPathQueryType npathQueryType = context.NPathEngine.GetNPathQueryType(query); string sql = npath.ToSql(); textSql.Text = sql; if (!(eval)) { if (npathQueryType == NPathQueryType.SelectObjects) { DataTable sqlResult = context.SqlExecutor.ExecuteDataTable(sql, context.GetDataSource(classMap.GetSourceMap()), npath.Parameters); gridRows.DataSource = sqlResult; IList result = context.GetObjectsByQuery(npath); DataTable table = GetDataTable(result, context); gridObjects.DataSource = table; } if (npathQueryType == NPathQueryType.SelectScalar) { DataTable sqlResult = context.SqlExecutor.ExecuteDataTable(sql, context.GetDataSource(classMap.GetSourceMap())); gridRows.DataSource = sqlResult; object result = context.ExecuteScalar(npath); DataTable table = GetScalarDataTable(result); gridObjects.DataSource = table; } if (npathQueryType == NPathQueryType.SelectTable) { DataTable sqlResult = context.SqlExecutor.ExecuteDataTable(sql, context.GetDataSource(classMap.GetSourceMap())); gridRows.DataSource = sqlResult; DataTable result = context.GetDataTable(npath); gridObjects.DataSource = result; } } context.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void EnsureWriteConsistency(object value) { IContext ctx = this.Context; if (ctx.WriteConsistency.Equals(ConsistencyMode.Pessimistic)) { IIdentityHelper identityHelper = Interceptable as IIdentityHelper; if (identityHelper == null) { throw new NPersistException(string.Format("Object of type {0} does not implement IIdentityHelper", Interceptable.GetType())); } IClassMap classMap = ClassMap; ISourceMap sourceMap = classMap.GetSourceMap(); if (sourceMap != null) { if (sourceMap.PersistenceType.Equals(PersistenceType.ObjectRelational) || sourceMap.PersistenceType.Equals(PersistenceType.Default)) { ITransaction tx = ctx.GetTransaction(ctx.GetDataSource(sourceMap).GetConnection()); if (tx == null) { throw new WriteConsistencyException( string.Format("A write consistency exception has occurred. The property {0} for the object of type {1} and with identity {2} was updated with a value outside of a transaction. This is not permitted in a context using Pessimistic WriteConsistency.", PropertyName, Interceptable.GetType(), ctx.ObjectManager.GetObjectIdentity(Interceptable)), Interceptable); } Guid txGuid = identityHelper.GetTransactionGuid(); if (!(tx.Guid.Equals(txGuid))) { throw new WriteConsistencyException( string.Format("A write consistency exception has occurred. The property {0} for the object of type {1} and with identity {2} was loaded or initialized inside a transactions with Guid {3} and was now updated with a value under another transaction with Guid {4}. This is not permitted in a context using Pessimistic WriteConsistency.", PropertyName, Interceptable.GetType(), ctx.ObjectManager.GetObjectIdentity(Interceptable), txGuid, tx.Guid), txGuid, tx.Guid, Interceptable); } if (value != null) { IPropertyMap propertyMap = PropertyMap; if (propertyMap.ReferenceType != ReferenceType.None) { IIdentityHelper valueIdentityHelper = value as IIdentityHelper; if (valueIdentityHelper == null) { throw new NPersistException(string.Format("Object of type {0} does not implement IIdentityHelper", value.GetType())); } Guid valueTxGuid = valueIdentityHelper.GetTransactionGuid(); if (!(tx.Guid.Equals(valueTxGuid))) { throw new WriteConsistencyException( string.Format("A write consistency exception has occurred. The property {0} for the object of type {1} and with identity {2} was loaded or initialized inside a transactions with Guid {3} and was now updated with a reference to an object that was loaded under another transaction with Guid {4}. This is not permitted in a context using Pessimistic WriteConsistency.", PropertyName, Interceptable.GetType(), ctx.ObjectManager.GetObjectIdentity(Interceptable), tx.Guid, valueTxGuid), tx.Guid, valueTxGuid, Interceptable); } } } } } } }