private static EntityMetadataCollection LoadAptifyEntityInformation(IDbConnection connection) { var entities = new EntityMetadataCollection( ); var parentIDs = new List <int>( ); using (IDbCommand command = connection.CreateCommand( )) { command.CommandText = Queries.GetAllEntities; command.CommandType = CommandType.Text; using (IDataReader reader = command.ExecuteReader( )) { while (reader.Read( )) { var entity = new AptifyEntityMetadata( ( int )reader["ID"], ( string )reader["Name"]); entities.Add(entity); parentIDs.Add(reader.GetInt32(1)); } } MapParents(entities, parentIDs); } return(entities); }
public override void VisitHasMany(HasManyModel model) { Type childType = model.Property.PropertyType.GetGenericArguments( ).SingleOrDefault( ); string tableName = model.ContainingTypeModel.ActiveRecordAtt.Table; if (childType == null) { throw new HasManyMappingException("Property must have only one generic argument", model); } AptifyEntityMetadata parentEntity = this.entities.FirstOrDefault(e => e.Tables.Any(t => t.Name == tableName)); AptifyEntityMetadata childEntity = this.mappings.GetTableMetadata(childType).Entity; // Can't map the table yet if (childEntity == null) { return; } if (childEntity.Parent == parentEntity) { parentEntity.AddChild(childEntity, model.Property.Name); } base.VisitHasMany(model); }
/// <summary> /// Maps the ActiveRecord class to the table information from Aptify /// </summary> /// <param name="model">ActiveRecord class model</param> public override void VisitModel(ActiveRecordModel model) { if (model.ActiveRecordAtt == null) { return; } // Find the Aptify entity that maps to this ActiveRecord model AptifyEntityMetadata entity = this.entities.FirstOrDefault( e => e.Tables.Any(t => t.Name == model.ActiveRecordAtt.Table)); if (entity == null) { // The mapping of the table name is incorrect throw new InvalidOperationException( string.Format("'{0}' is mapped to table '{1}', which is not in an entity in Aptify", model.Type.FullName, model.ActiveRecordAtt.Table)); } // Find the table within the entity AptifyTableMetadata table = entity.Tables.First(t => t.Name == model.ActiveRecordAtt.Table); // Insert this mapping into the mappings this.mappings.Add(model.Type, table); base.VisitModel(model); }
/// <summary> /// Gets a child entity from the server /// </summary> /// <param name="parent">Parent entity</param> /// <param name="entityMetadata">Metadata for the entity</param> /// <param name="store">The NHibernate storage about the updated object</param> /// <returns>A new generic entity that maps to this table and store</returns> internal AptifyGenericEntityBase GetEntity(AptifyEntity parent, AptifyEntityMetadata entityMetadata, EntityStore store) { Log.DebugFormat("Creating child aptify entity for {0}", entityMetadata.Name); AptifySubTypeBase subType = parent.GenericEntity.SubTypes[entityMetadata.Name]; // This is not a child entity within Aptify if (subType == null) { return(GetEntity(entityMetadata, store)); } switch (store.Status) { case EntityStatus.Clean: case EntityStatus.Dirty: Log.DebugFormat("Loading subentity for {0}", entityMetadata.Name); return(subType.Find("id", store.Id)); case EntityStatus.New: Log.DebugFormat("Loading new subentity for {0}", entityMetadata.Name); return(subType.Add( )); default: throw new InvalidOperationException("Unknown entity status " + store.Status); } }
/// <summary> /// Gets the generic entity from the server /// </summary> /// <param name="entityMetadata">Entity metadata for loading</param> /// <param name="store">The NHibernate storage about the updated object</param> /// <returns>A new generic entity that maps to this table and store</returns> public AptifyGenericEntityBase GetEntity(AptifyEntityMetadata entityMetadata, EntityStore store) { Log.DebugFormat("Creating aptify entity for {0}", entityMetadata.Name); this.application.UserCredentials.DefaultTransactionID = String.Empty; var entityInfo = this.application.get_Entity(entityMetadata.Id); return(entityInfo.GetEntityObject(store.Id)); }
/// <summary> /// Loads an aptify entity /// </summary> private static AptifyGenericEntityBase GetGenericEntity(AptifyServer server, AptifyEntity parent, EntityStore store, AptifyEntityMetadata metadata) { // Clean entities don't need to be loaded if (store.Status == EntityStatus.Clean && parent != null) { return(null); } Log.DebugFormat("Loading entity: '{0}'", metadata.Name); if (parent != null) { return(server.GetEntity(parent, metadata, store)); } return(server.GetEntity(metadata, store)); }
private static void LoadAptifyTableMetadata(IDbConnection connection, EntityMetadataCollection entities) { using (IDbCommand command = connection.CreateCommand( )) { command.CommandText = Queries.GetAllColumns; command.CommandType = CommandType.Text; using (IDataReader reader = command.ExecuteReader( )) { while (reader.Read( )) { // Load the required fields to simplify the latter code var entityId = ( int )reader["EntityID"]; AptifyEntityMetadata parent = entities.GetByName(reader["LinkedEntity"] as string); var tableName = ( string )reader["BaseTable"]; var columnName = ( string )reader["Name"]; var nullable = ( bool )reader["SQLAllowNull"]; var embedded = (( string )reader["LinkType"] ?? String.Empty).Trim( ) == "Embedded"; AptifyEntityMetadata entity = entities.GetById(entityId); if (entity == null) { throw new InvalidOperationException("No entity found with ID " + entityId); } AptifyTableMetadata table = entity.Tables.FirstOrDefault(t => t.Name == tableName); // This is the first time the table was found if (table == null) { // Add the table to the entity table = new AptifyTableMetadata(entity, tableName); entity.AddTable(table); } var column = new AptifyColumnMetadata(columnName, nullable, parent, embedded); table.AddColumn(column); } } } }
/// <summary> /// Creates an entity child mapping /// </summary> /// <param name="entity">Child entity</param> /// <param name="aptifyName">Column name used in the Aptify entity</param> /// <param name="propertyName">Property name used in the ActiveRecord model</param> internal AptifyChildEntity(AptifyEntityMetadata entity, string aptifyName, string propertyName) { if (entity == null) { throw new ArgumentNullException("entity"); } if (String.IsNullOrEmpty(aptifyName)) { throw new ArgumentException("aptifyName cannot be null or empty string"); } if (String.IsNullOrEmpty(propertyName)) { throw new ArgumentException("propertyName cannot be null or empty string"); } this.entity = entity; this.aptifyName = aptifyName; this.propertyName = propertyName; }