public static void SchemaManagerSetFKShouldAddFK() { SchemaManager mgr = new SchemaManager(); SchemaDefinition schema = mgr.SetSchema("test"); string tableName = "Referencee"; mgr.AddTable(tableName); mgr.AddColumn(tableName, new Column("PutColumnName", DataTypes.Boolean)); string refering = "Referencer"; mgr.AddTable(refering); mgr.AddColumn(refering, new Column("fk", DataTypes.Long)); int initCount = schema.ForeignKeys.Length; mgr.SetForeignKey(tableName, refering, "fk"); Expect.AreEqual(initCount + 1, schema.ForeignKeys.Length); Table referenced = mgr.GetTable(tableName); Table referencer = mgr.GetTable(refering); Expect.AreEqual(schema.ForeignKeys.Length, referenced.ReferencingForeignKeys.Length); Expect.AreEqual(0, referencer.ReferencingForeignKeys.Length); DeleteSchema(schema); }
protected internal virtual void WriteDaoSchema(TypeSchema typeSchema, SchemaManager schemaManager, List <KeyColumn> missingKeyColumns = null, List <ForeignKeyColumn> missingForeignKeyColumns = null, ITypeTableNameProvider tableNameProvider = null) { AddSchemaTables(typeSchema, schemaManager, tableNameProvider); HashSet <TypeFk> foreignKeyTypes = typeSchema.ForeignKeys; HashSet <TypeXref> xrefTypes = typeSchema.Xrefs; // accounting for missing columns // loop primary keys and fks separately to ensure // missing keys get recorded prior to trying to add the // fks foreach (TypeFk foreignKey in foreignKeyTypes) { TypeSchemaPropertyInfo keyInfo = foreignKey.PrimaryKeyProperty as TypeSchemaPropertyInfo; if (keyInfo != null) { KeyColumn key = keyInfo.ToKeyColumn(); schemaManager.AddColumn(key.TableName, key); schemaManager.SetKeyColumn(key.TableName, key.Name); if (missingKeyColumns != null) { missingKeyColumns.Add(key); } } } foreach (TypeFk foreignKey in foreignKeyTypes) { TypeSchemaPropertyInfo fkInfo = foreignKey.ForeignKeyProperty as TypeSchemaPropertyInfo; if (fkInfo != null) { PropertyInfo keyProperty = GetKeyProperty(foreignKey.PrimaryKeyType, missingKeyColumns); string referencedKeyName = keyProperty.Name; ForeignKeyColumn fk = fkInfo.ToForeignKeyColumn(tableNameProvider); fk.AllowNull = true; schemaManager.AddColumn(fk.TableName, fk); schemaManager.SetForeignKey(fk.ReferencedTable, fk.TableName, fk.Name, referencedKeyName); if (missingForeignKeyColumns != null) { missingForeignKeyColumns.Add(fk); } } } // /end - accounting for missing columns foreach (TypeFk foreignKey in foreignKeyTypes) { schemaManager.SetForeignKey( GetTableNameForType(foreignKey.PrimaryKeyType, tableNameProvider), GetTableNameForType(foreignKey.ForeignKeyType, tableNameProvider), foreignKey.ForeignKeyProperty.Name); } foreach (TypeXref xref in xrefTypes) { schemaManager.SetXref(GetTableNameForType(xref.Left, tableNameProvider), GetTableNameForType(xref.Right, tableNameProvider)); } }
private static SchemaManager GetTestSchemaManager() { SchemaManager mgr = new SchemaManager(); mgr.AddTable("Person"); mgr.AddColumn("Person", "Id", DataTypes.Long); mgr.AddColumn("Person", "Name", DataTypes.String); mgr.AddTable("Employee"); mgr.AddColumn("Employee", "Id", DataTypes.Long); mgr.AddColumn("Employee", "Salary", DataTypes.Decimal); SchemaResult result = mgr.SetForeignKey("Person", "Employee", "Id", "Id"); Expect.IsTrue(result.Success, $"Message: {result.Message}\r\nException: {result.ExceptionMessage}"); return(mgr); }
public void SchemaMgrSetPropertyShouldSetPropertyNameOfColumn() { string tableName = "Test"; string columnName = "ColumnName"; string propertyName = "PropertyName"; FileInfo schemFile = new FileInfo(".\\{0}.json"._Format(MethodBase.GetCurrentMethod().Name)); SchemaDefinition def = new SchemaDefinition(); def.ToJsonFile(schemFile); SchemaManager mgr = new SchemaManager(schemFile); mgr.AddTable(tableName); mgr.AddColumn(tableName, columnName); Table testTable = mgr.GetTable(tableName); Column column = testTable.Columns[0]; Expect.AreEqual(1, testTable.Columns.Length); Expect.AreEqual(columnName, column.Name); Expect.AreEqual(column.Name, column.PropertyName); mgr.SetColumnPropertyName(tableName, columnName, propertyName); testTable = mgr.GetTable(tableName); column = testTable.Columns[0]; Expect.AreEqual(1, testTable.Columns.Length); Expect.AreEqual(columnName, column.Name); Expect.AreEqual(propertyName, column.PropertyName); }
private static SchemaDefinition CreateTestTables(string targetTable, string referencingTable, SchemaManager mgr) { SchemaDefinition schema = mgr.SetSchema("fkTest"); DeleteSchema(schema); schema = mgr.SetSchema("fkTest"); mgr.AddTable(targetTable); mgr.AddColumn(targetTable, new Column("PutColumnName", DataTypes.ULong)); mgr.AddTable(referencingTable); mgr.AddColumn(referencingTable, new Column("PutColumnName", DataTypes.ULong)); mgr.SetForeignKey(targetTable, referencingTable, "fk"); return(schema); }
protected override void AddSchemaTables(TypeSchema typeSchema, SchemaManager schemaManager, ITypeTableNameProvider tableNameProvider = null) { tableNameProvider = tableNameProvider ?? new EchoTypeTableNameProvider(); foreach (Type topType in typeSchema.Tables) { TypeInheritanceDescriptor inheritance = new TypeInheritanceDescriptor(topType); Type inheritFrom = null; inheritance.Chain.BackwardsEach(typeTable => { string tableName = typeTable.GetTableName(tableNameProvider); schemaManager.AddTable(tableName); schemaManager.ExecutePreColumnAugmentations(tableName); typeTable.PropertyColumns.Each(pc => { AddPropertyColumn(schemaManager, typeSchema.DefaultDataTypeBehavior, tableName, pc.PropertyInfo); }); schemaManager.ExecutePostColumnAugmentations(tableName); schemaManager.AddColumn(tableName, "Id", DataTypes.Long); if (inheritFrom != null) { schemaManager.SetForeignKey(tableNameProvider.GetTableName(inheritFrom), tableName, "Id", "Id"); } else { schemaManager.SetKeyColumn(tableName, "Id"); } inheritFrom = typeTable.Type; }); } }
public static void SchemaMgrAddColumnShouldSuccess() { string tableName = "tableOne"; SchemaManager mgr = new SchemaManager(); SchemaDefinition s = mgr.SetSchema("test"); SchemaResult r = mgr.AddTable(tableName); r = mgr.AddColumn(tableName, new Column("ColumnOne", DataTypes.Long, false)); Expect.IsTrue(r.Success); }
protected static void AddSchemaColumn(SchemaManager schemaManager, string tableName, PropertyInfo property, DataTypes dataType) { if (!property.IsEnumerable() || property.PropertyType == typeof(string)) { Column columnToAdd = new Column(property.Name.LettersOnly(), dataType); schemaManager.AddColumn(tableName, columnToAdd); if (property.HasCustomAttributeOfType <KeyAttribute>() || property.Name.Equals("Id")) { schemaManager.SetKeyColumn(tableName, columnToAdd.Name); } } }
public static void SchemaManagerAddColumnShouldAddColumn() { SchemaManager mgr = new SchemaManager(); SchemaDefinition schema = mgr.SetSchema("test"); string tableName = "Babboons"; mgr.AddTable(tableName); mgr.AddColumn(tableName, new Column("PutColumnName", DataTypes.Boolean)); Table table = mgr.GetTable(tableName); Expect.IsTrue(table.Columns.Length == 1); DeleteSchema(schema); }