public void AddManyRelation(TableDefinition def) { if (!_hasRelation.Contains(def)) { _hasRelation.Add(def); } }
public ActiveRecordDescriptor Build(TableDefinition tableDef, BuildContext context) { ActiveRecordDescriptor desc = ObtainDescriptor(tableDef); // ClassName desc.ClassName = _namingService.CreateClassName( tableDef.Name ); // Plain fields desc.Properties.AddRange( _plainFieldsService.InferProperties(tableDef) ); // Relations desc.PropertiesRelations.AddRange( _relationsService.InferRelations(desc, tableDef, context) ); // Operations // TODO! context.RemovePendent(desc); Build(context); return desc; }
public void HasÁndBelongsToMany() { InitKernel(); IRelationshipBuilder relService = ObtainService(); DatabaseDefinition dbdef = new DatabaseDefinition("alias"); TableDefinition compTable = new TableDefinition("companies", dbdef ); compTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) ); compTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) ); TableDefinition peopleTable = new TableDefinition("people", dbdef ); peopleTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) ); peopleTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) ); TableDefinition assocTable = new TableDefinition("companiespeople", dbdef ); assocTable.AddColumn( new ColumnDefinition("comp_id", true, true, true, false, OleDbType.Integer) ); assocTable.AddColumn( new ColumnDefinition("person_id", true, true, false, false, OleDbType.Integer) ); ActiveRecordDescriptor desc = new ActiveRecordDescriptor("Company"); ActiveRecordDescriptor targetDesc = new ActiveRecordDescriptor("Person"); RelationshipInfo info = new RelationshipInfo( AssociationEnum.HasAndBelongsToMany, desc, targetDesc ); info.AssociationTable = assocTable; info.ParentCol = new ColumnDefinition("comp_id", false, true, false, true, OleDbType.Integer); info.ChildCol = new ColumnDefinition("person_id", false, true, false, true, OleDbType.Integer); ActiveRecordPropertyRelationDescriptor propDesc = relService.Build( info ); Assert.IsNotNull(propDesc as ActiveRecordHasAndBelongsToManyDescriptor); Assert.IsNotNull(propDesc); Assert.AreEqual( "People", propDesc.PropertyName ); Assert.AreEqual( targetDesc, propDesc.TargetType ); Assert.AreEqual( "person_id", propDesc.ColumnName ); Assert.AreEqual( "comp_id", (propDesc as ActiveRecordHasAndBelongsToManyDescriptor).ColumnKey); }
public void BlogPlainFields() { Kernel.AddComponent( "plainfields", typeof(IPlainFieldInferenceService), typeof(PlainFieldInferenceService) ); Kernel.AddComponent( "nameService", typeof(INamingService), typeof(NamingService) ); Kernel.AddComponent( "typeinf", typeof(ITypeInferenceService), typeof(TypeInferenceService) ); IPlainFieldInferenceService plainService = Kernel[ typeof(IPlainFieldInferenceService) ] as IPlainFieldInferenceService; TableDefinition table = new TableDefinition("blogs", new DatabaseDefinition("alias") ); table.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) ); table.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) ); table.AddColumn( new ColumnDefinition("authorid", false, true, false, false, OleDbType.VarChar) ); ActiveRecordPropertyDescriptor[] descs = plainService.InferProperties( table ); Assert.IsNotNull(descs); Assert.AreEqual( 2, descs.Length ); ActiveRecordPropertyDescriptor desc1 = descs[0]; ActiveRecordPropertyDescriptor desc2 = descs[1]; Assert.AreEqual( "id", desc1.ColumnName ); Assert.AreEqual( "Integer", desc1.ColumnTypeName ); Assert.AreEqual( "Id", desc1.PropertyName ); Assert.AreEqual( typeof(int), desc1.PropertyType ); Assert.AreEqual( "name", desc2.ColumnName ); Assert.AreEqual( "VarChar", desc2.ColumnTypeName ); Assert.AreEqual( "Name", desc2.PropertyName ); Assert.AreEqual( typeof(String), desc2.PropertyType ); }
public override void Activated(IDictionary context) { TableDefinition table = context["selectedtable"] as TableDefinition; if (table != _oldTable) { _oldTable = table; IPlainFieldInferenceService fieldInference = ServiceRegistry.Instance[ typeof(IPlainFieldInferenceService) ] as IPlainFieldInferenceService; ActiveRecordPropertyDescriptor[] properties = fieldInference.InferProperties( table ); ActiveRecordDescriptor ar = context["ardesc"] as ActiveRecordDescriptor; ar.Properties.Clear(); ar.Properties.AddRange(properties); listView1.Items.Clear(); foreach(ActiveRecordPropertyDescriptor desc in properties) { ListViewItem item = listView1.Items.Add( desc.PropertyName ); item.Tag = desc; item.Checked = desc.Generate; item.SubItems.Add( desc.PropertyType.Name ); item.SubItems.Add( desc.ColumnName ); item.SubItems.Add( desc.ColumnTypeName ); } } }
public ActiveRecordPropertyDescriptor[] InferProperties(TableDefinition tableDef) { ArrayList list = new ArrayList(); foreach(ColumnDefinition col in tableDef.Columns) { if (col.ForeignKey) continue; String propertyName = _namingService.CreatePropertyName(col.Name); Type propertyType = _typeInfService.ConvertOleType(col.Type); String colType = col.Type.ToString(); String colName = col.Name; ActiveRecordFieldDescriptor field = null; if (col.PrimaryKey) { field = new ActiveRecordPrimaryKeyDescriptor( colName, colType, propertyName, propertyType, "Native"); } else { field = new ActiveRecordFieldDescriptor( colName, colType, propertyName, propertyType, col.Nullable); } list.Add(field); } return (ActiveRecordFieldDescriptor[]) list.ToArray( typeof(ActiveRecordFieldDescriptor) ); }
public ActiveRecordDescriptor(TableDefinition table) { _table = table; // if (_table.RelatedDescriptor != null) // { // throw new ArgumentException("Table has an ARDescriptor already"); // } _table.RelatedDescriptor = this; }
private ActiveRecordDescriptor ObtainDescriptor(TableDefinition tableDef) { if (tableDef.RelatedDescriptor != null) { return tableDef.RelatedDescriptor; } else { return new ActiveRecordDescriptor(tableDef); } }
private void BuildBlogPostsStructure(out TableDefinition blogTable, out TableDefinition postTable) { DatabaseDefinition dbdef = new DatabaseDefinition("alias"); blogTable = new TableDefinition("blogs", dbdef ); blogTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) ); blogTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) ); postTable = new TableDefinition("posts", dbdef ); postTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) ); postTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) ); postTable.AddColumn( new ColumnDefinition("blog_id", false, true, false, false, OleDbType.VarChar, blogTable) ); blogTable.AddManyRelation(postTable); }
public ActiveRecordPropertyRelationDescriptor[] InferRelations(ActiveRecordDescriptor desc, TableDefinition tableDef, BuildContext context) { if (desc == null) throw new ArgumentNullException("desc"); if (tableDef == null) throw new ArgumentNullException("tableDef"); if (context == null) throw new ArgumentNullException("context"); // if (tableDef.RelatedDescriptor != null && tableDef.RelatedDescriptor != desc) { // throw new ArgumentException("Different descriptors"); // } ArrayList list = new ArrayList(); CreateHasManyRelations(desc, tableDef, list, context); CreateBelongsToRelations(desc, tableDef, list, context); return (ActiveRecordPropertyRelationDescriptor[]) list.ToArray( typeof(ActiveRecordPropertyRelationDescriptor) ); }
public override void Activated(IDictionary context) { TableDefinition table = context["selectedtable"] as TableDefinition; if (table != _oldTable) { _oldTable = table; ActiveRecordDescriptor ar = context["ardesc"] as ActiveRecordDescriptor; listView1.Items.Clear(); BuildContext buildCtx = context["buildcontext"] as BuildContext; IRelationshipInferenceService relationInference = ServiceRegistry.Instance[ typeof(IRelationshipInferenceService) ] as IRelationshipInferenceService; ActiveRecordPropertyRelationDescriptor[] properties = relationInference.InferRelations( ar, table, buildCtx ); ar.PropertiesRelations.Clear(); ar.PropertiesRelations.AddRange(properties); foreach(ActiveRecordPropertyRelationDescriptor property in properties) { ListViewItem item = listView1.Items.Add( property.PropertyName ); item.Tag = property; item.Checked = property.Generate; if (property.TargetType != null) { item.SubItems.Add( property.TargetType.ClassName != null ? property.TargetType.ClassName : "<Pendent>" ); } else { throw new ApplicationException("Information missing"); } item.SubItems.Add( property.RelationType ); item.SubItems.Add( property.ColumnName ); } } }
private void CreateBelongsToRelations(ActiveRecordDescriptor desc, TableDefinition tableDef, IList list, BuildContext context) { foreach(ColumnDefinition col in tableDef.Columns) { if (col.RelatedTable != null) { bool pendentNecessary = false; String propertyName = _namingService.CreateClassName(col.RelatedTable.Name); ActiveRecordDescriptor targetType = null; if (col.RelatedTable.RelatedDescriptor == null && col.RelatedTable != tableDef) { col.RelatedTable.RelatedDescriptor = new ActiveRecordDescriptor(col.RelatedTable); pendentNecessary = true; } else if (col.RelatedTable == tableDef) { targetType = desc; } if (targetType == null) { targetType = col.RelatedTable.RelatedDescriptor; } ActiveRecordBelongsToDescriptor belongsTo = new ActiveRecordBelongsToDescriptor(col.Name, propertyName, targetType); list.Add(belongsTo); if (pendentNecessary) { context.AddPendentDescriptor(belongsTo, col.RelatedTable.RelatedDescriptor); } } } }
public void Add(TableDefinition def) { BaseAdd(def.Name, def); }
private void PopulateColumnsInListBox(ListBox cols, TableDefinition table) { cols.Items.Clear(); cols.ValueMember = "Name"; foreach(ColumnDefinition col in table.Columns) { cols.Items.Add(col); } }
public override void Activated(System.Collections.IDictionary context) { TableDefinition table = context["selectedtable"] as TableDefinition; if (table != _oldTable) { _oldTable = table; INamingService naming = ServiceRegistry.Instance[ typeof(INamingService) ] as INamingService; className.Text = naming.CreateClassName(table.Name); } }
public ColumnDefinition(String name, bool primaryKey, bool foreignKey, bool unique, bool nullable, OleDbType type, TableDefinition relatedTable) : this(name, primaryKey, foreignKey, unique, nullable, type) { _relatedTable = relatedTable; }
public TableDefinition AddTable(TableDefinition table) { _tables.Add(table); return table; }
private void CreateHasManyRelations(ActiveRecordDescriptor desc, TableDefinition tableDef, IList list, BuildContext context) { foreach(TableDefinition fkTable in tableDef.TablesReferencedByHasRelation) { String propertyName = _namingService.CreateRelationName(fkTable.Name); ActiveRecordDescriptor targetType = null; String colName = null; bool pendentNecessary = false; ColumnDefinition selectedCol = null; foreach(ColumnDefinition col in fkTable.Columns) { if (col.RelatedTable == tableDef) { colName = col.Name; if (col.RelatedTable.RelatedDescriptor == null && col.RelatedTable != fkTable) { col.RelatedTable.RelatedDescriptor = new ActiveRecordDescriptor(fkTable); pendentNecessary = true; } else if (col.RelatedTable == tableDef) { targetType = desc; } if (targetType == null) { targetType = col.RelatedTable.RelatedDescriptor; } selectedCol = col; break; } } // Just to protect ourselves from awkward conditions if (colName == null) continue; ActiveRecordHasManyDescriptor hasMany = new ActiveRecordHasManyDescriptor(colName, propertyName, targetType); if (pendentNecessary) { context.AddPendentDescriptor(hasMany, selectedCol.RelatedTable.RelatedDescriptor); } list.Add(hasMany); } }
public void SelfReference() { InitKernel(); IRelationshipInferenceService relService = ObtainService(); DatabaseDefinition dbdef = new DatabaseDefinition("alias"); TableDefinition categoryTable = new TableDefinition("categories", dbdef ); categoryTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) ); categoryTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) ); categoryTable.AddColumn( new ColumnDefinition("parent_id", false, true, false, false, OleDbType.Integer, categoryTable) ); categoryTable.AddManyRelation(categoryTable); BuildContext context = new BuildContext(); ActiveRecordDescriptor arDesc = new ActiveRecordDescriptor(); ActiveRecordPropertyDescriptor[] descs = relService.InferRelations( arDesc, categoryTable, context ); Assert.IsFalse(context.HasPendents); Assert.IsNotNull(descs); Assert.AreEqual( 2, descs.Length ); ActiveRecordHasManyDescriptor desc1 = descs[0] as ActiveRecordHasManyDescriptor; Assert.IsNotNull(desc1); Assert.IsNotNull(desc1.TargetType); Assert.IsNotNull(desc1.PropertyType); Assert.AreEqual( "Categories", desc1.PropertyName ); Assert.AreEqual( "parent_id", desc1.ColumnName ); Assert.AreEqual( typeof(IList), desc1.PropertyType ); ActiveRecordBelongsToDescriptor desc2 = descs[1] as ActiveRecordBelongsToDescriptor; Assert.IsNotNull(desc2); Assert.IsNotNull(desc2.TargetType); Assert.IsNull(desc2.PropertyType); Assert.AreEqual( "Category", desc2.PropertyName ); Assert.AreEqual( "parent_id", desc2.ColumnName ); }