/// <summary> /// Retrieves all the columns for the table /// </summary> /// <param name="assemblyFilePath">The assembly file to search</param> public void GetColumns(string assemblyFilePath) { // Get primary key for table GetPrimaryKey(assemblyFilePath); Type type = AssemblySearch.GetTypeFromAssembly(assemblyFilePath, FullClassNamespace); //Search fields foreach (var field in TypeSearch.GetFieldsWithScriptPropertyAttribute(type) ?? new FieldInfo[0]) { AddColumn(ColumnDefinition.CreateFromFieldInfo(field)); } //Search properties foreach (var property in TypeSearch.GetPropertiesWithScriptPropertyAttribute(type) ?? new PropertyInfo[0]) { AddColumn(ColumnDefinition.CreateFromPropertyInfo(property)); } #if DEBUG foreach (ColumnDefinition column in Columns) { Console.WriteLine($"Table: {Name} - Column: {column.Name}, {column.Type}, PK:{column.PrimaryKey}"); } #endif }
/// <summary> /// Retrieves the primary key for the table /// </summary> /// <param name="assemblyFilePath">The assembly file to search</param> /// <remarks>Assumes only 1 primary key defined on class</remarks> public void GetPrimaryKey(string assemblyFilePath) { Type type = AssemblySearch.GetTypeFromAssembly(assemblyFilePath, FullClassNamespace); object primaryKey = TypeSearch.GetPrimaryKeyFromType(type); //if null, there is no primary key if (primaryKey != null) { try { PropertyInfo property = (PropertyInfo)primaryKey; AddColumn(ColumnDefinition.CreateFromPropertyInfo(property, true)); } catch (InvalidCastException) { //If not a PropertyInfo type or null, it must be FieldInfo. Otherwise, allow exceptions. FieldInfo field = (FieldInfo)primaryKey; AddColumn(ColumnDefinition.CreateFromFieldInfo(field, true)); } } }
/// <summary> /// Retrieves all foreigns keys for the table /// </summary> /// <param name="assemblyFilePath">The assembly file to search</param> /// <remarks><see cref="CompleteForeignKeyDefinitions"/> should be called after this function.</remarks> public void GetForeignKeys(string assemblyFilePath) { Type type = AssemblySearch.GetTypeFromAssembly(assemblyFilePath, FullClassNamespace); //Search fields foreach (var field in TypeSearch.GetFieldsWithScriptForeignKeyAttribute(type) ?? new FieldInfo[0]) { AddForeignKey(ForeignKeyDefinition.CreateFromFieldInfo(field, Name)); } //Search properties foreach (var property in TypeSearch.GetPropertiesWithScriptForeignKeyAttribute(type) ?? new PropertyInfo[0]) { AddForeignKey(ForeignKeyDefinition.CreateFromPropertyInfo(property, Name)); } #if DEBUG foreach (ForeignKeyDefinition fk in ForeignKeys) { Console.WriteLine($"Table: {Name} - Foreign Key: {fk.ColumnName}, {fk.ForeignTable}"); } #endif }