/// <summary> /// 通过当前数据库版本号判断是否需要执行本数据处理 /// </summary> /// <param name="current_db_version"></param> /// <returns></returns> public bool shouldPerformProcess(DBVersion current_db_version) { return(shouldPerformProcess(current_db_version.db_version)); }
/// <summary> /// 通过当前数据库版本号判断是否需要执行本描述文件升级 /// </summary> /// <param name="current_db_version"></param> /// <returns></returns> public bool shouldUpdate(DBVersion current_db_version) { return(shouldUpdate(current_db_version.db_version)); }
/// <summary> /// 更新数据库 /// </summary> /// <param name="connection">数据库连接</param> /// <param name="assembly_name">资源库路径, 例如 IMC.Test.DB</param> /// <param name="msg">错误信息</param> /// <returns>当前更新后的数据库版本号</returns> public DBVersion updateDB(DBConnection connection, string assembly_name, ref string msg) { DBVersion current_db_version = getCurrentDBVersion(connection); if (db_version.compare(current_db_version) != 1) {//当前最新版本号小于等于数据库版本号, 无需更新 msg = "当前数据库无需更新!"; return(db_version); } //获取所有数据库升级描述文件 Assembly assembly = null; try { assembly = Assembly.Load(assembly_name); } catch { msg = "无法获取更新描述文件!"; return(db_version); } Type[] types = assembly.GetTypes(); List <DBUpdateProfile> update_profiles = new List <DBUpdateProfile>(); List <DBDataProcess> data_processes = new List <DBDataProcess>(); //获取描述文件后按照版本号先后顺序排列 foreach (Type type in types) { try { if (type.BaseType == typeof(DBUpdateProfile)) {//数据库更新 DBUpdateProfile profile = Activator.CreateInstance(type) as DBUpdateProfile; //过滤掉之前更新过的记录 if (!profile.shouldUpdate(current_db_version)) { continue; } int index = -1; for (int i = 0; i < update_profiles.Count; i++) { if (update_profiles[i].start_db_version.compare(profile.start_db_version) == 1) { index = i; break; } } if (index >= 0) { update_profiles.Insert(index, profile); } else { update_profiles.Add(profile); } } else if (type.BaseType == typeof(DBDataProcess)) {//数据处理操作 DBDataProcess process = Activator.CreateInstance(type) as DBDataProcess; //过滤掉之前更新过的记录 if (!process.shouldPerformProcess(current_db_version)) { continue; } data_processes.Add(process); } } catch { } } //更新数据库 foreach (DBUpdateProfile profile in update_profiles) { List <string> sqls = profile.getUpdateSqlList(connection.db_type); foreach (var query in sqls) { DBQuery.query(query, connection); } //查找是否有相关的数据更新操作, 有则进行更新 foreach (DBDataProcess process in data_processes) { if (process.start_db_version.compare(profile.start_db_version) == 0) { process.processData(connection); } } } //更新版本号到数据库相关表(需继承后实现) msg = updateNewVersionInfoToDB(connection); return(db_version); }
/// <summary> /// 与其他数据库版本做比较, 相等返回0, 当前版本较小返回-1, 当前版本较大返回1, 格式有误返回-2 /// </summary> /// <param name="db_version">相比较的数据库版本实例</param> /// <returns>相等返回0, 当前版本较小返回-1, 当前版本较大返回1, 格式有误返回-2</returns> public int compare(DBVersion db_version) { return(compare(db_version.db_version)); }