/// <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> /// Updates validations for the property /// </summary> /// <param name="model">Property model</param> public override void VisitProperty(PropertyModel model) { AptifyTableMetadata table = this.mappings.GetTableMetadata(model.Property.ReflectedType); string columnName = model.PropertyAtt.Column ?? model.Property.Name; AptifyColumnMetadata column; if (table.Columns.TryGetValue(columnName, out column)) { if (model.PropertyAtt.Column != null) { table.Columns.Remove(column.Name); table.Columns[model.Property.Name] = column; } model.PropertyAtt.NotNull = !column.Nullable; } else if (String.IsNullOrEmpty(model.PropertyAtt.Table)) { // If the table property is set, this is a joined table column string error = String.Format("Could not map {0} to entity {1}. Column not defined in Aptify entity.", columnName, model.Property.ReflectedType); throw new InvalidConstraintException(error); } base.VisitProperty(model); }
/// <summary> /// Maps the parent/child relationship /// </summary> /// <param name="model">Model that contains the relationship</param> public override void VisitBelongsTo(BelongsToModel model) { if (model.BelongsToAtt.Column == null) { string message = String.Format("BelongsTo missing column parameter in class {0} and property {1}", model.Property.DeclaringType.FullName, model.Property); throw new ArgumentException(message); } Type child = model.Property.ReflectedType; // Get the table and column that is mapped AptifyTableMetadata table = this.mappings.GetTableMetadata(child); AptifyColumnMetadata column; if (!table.Columns.TryGetValue(model.BelongsToAtt.Column, out column)) { throw new InvalidOperationException( String.Format("Could not find column {0} in {1}", model.BelongsToAtt.Column, model.Property.DeclaringType.FullName)); } // Remove the old name table.Columns.Remove(column.Name); // Reinsert with the updated property name table.Columns[model.Property.Name] = column; base.VisitBelongsTo(model); }
/// <summary> /// Creates a child entity /// </summary> /// <param name="parent">Parent entity</param> /// <param name="server">Aptify server connection</param> /// <param name="store">Entity storage</param> private AptifyEntity(AptifyEntity parent, AptifyServer server, EntityStore store) { if (server == null) { throw new ArgumentNullException("server"); } if (store == null) { throw new ArgumentNullException("store"); } this.parent = parent; this.server = server; this.store = store; // Get the entity that is mapped to this model instance this.table = server.Tables.GetTableMetadata(store.EntityObject); // Get the aptify generic entity and load its contents this.genericEntity = GetGenericEntity(server, parent, store, this.table.Entity); UpdateEntityContent( ); // Load any child items if (store.Persister.HasCascades) { this.children = LoadChildEntities( ).ToArray( ); } }
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); } } } }
private AptifyEntity(AptifyDataFieldBase field, AptifyServer server, EntityStore store) { if (!field.EmbeddedObjectExists) { throw new InvalidOperationException(field.Name + " is not an embedded object"); } this.server = server; this.store = store; // Get the entity that is mapped to this model instance this.table = server.Tables.GetTableMetadata(store.EntityObject); this.genericEntity = field.EmbeddedObject; UpdateEntityContent( ); // Load any child items if (store.Persister.HasCascades) { this.children = LoadChildEntities( ).ToArray( ); } //this.store.MarkAsPersisted( 0 ); }
internal void Add(Type type, AptifyTableMetadata table) { this.tables.Add(type, table); }