private AddForeignKeyResult AddAllForeignKeys(DatabaseAgent agent, Assembly daoAssembly, string contextName) { List <DaoForeignKeyInfo> foreignKeys = new List <DaoForeignKeyInfo>(); foreach (Type type in daoAssembly.GetTypes()) { object[] tableAttributes = type.GetCustomAttributes(typeof(DaoTable), false); if (tableAttributes.Length == 1) { DaoTable tableAttribute = (DaoTable)tableAttributes[0]; ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes); DaoObject obj = (DaoObject)ctor.Invoke(null); if (obj.DataContextName.Equals(contextName)) { foreignKeys.AddRange(this.GetForeignKeys(obj, tableAttribute.TableName)); } } } AddForeignKeyResult retVal = AddForeignKeyResult.Success; foreach (DaoForeignKeyInfo foreignKey in foreignKeys) { if (this.AddForeignKey(foreignKey, agent) == AddForeignKeyResult.Error) { retVal = AddForeignKeyResult.Error; } } return(retVal); }
public CreateTableResult CreateDaoTable(DaoObject daoObject, DatabaseAgent agent) { DaoTable tableAttribute = (DaoTable)daoObject.GetType().GetCustomAttributes(typeof(DaoTable), true)[0]; Expect.IsNotNull(tableAttribute); string tableName = tableAttribute.TableName; string idColumnName = this.GetIdColumnName(daoObject); Dictionary <PropertyInfo, DaoColumn> columns = this.GetColumns(daoObject); StringBuilder sqlColumns = new StringBuilder(); bool first = true; foreach (PropertyInfo key in columns.Keys) { string dataType = this.TranslateToDataType(key); string special = ""; bool primaryKey = false; if (key.HasCustomAttributeOfType <DaoIdColumn>(true, true))// -- old -> key.Name.Equals(idColumnName)) { special += this.GetIdentitySpec(); } // -- added 2/1/11 else if (key.HasCustomAttributeOfType <DaoPrimaryKeyColumn>(true, true)) { special += this.GetPrimaryKeySpec(); primaryKey = true; } // -- end added if (!columns[key].AllowNulls && !primaryKey) { special += " NOT NULL"; } if (!first) { sqlColumns.Append(", "); } sqlColumns.AppendFormat("\"{0}\" {1} {2}", columns[key].ColumnName, dataType, special); first = false; } try { agent.ExecuteSql(string.Format(DatabaseAgent.CREATETABLEFORMAT, tableName, sqlColumns.ToString())); if (agent.DbType == DaoDbType.Firebird) { this.CreateFirebirdIdTable(tableName, idColumnName, agent); } } catch (Exception ex) { return(CatchException(ex, tableName));//CreateDaoTableResult.UnknownError; } return(CreateTableResult.Success); }