/// <summary> /// 数据注册 /// </summary> public static void Register() { if (DateTime.MinValue == DataStorage.initialTime) { var configJson = new HTTP().GetGzip2(string.Format("http://aifuwu.wang/API.ashx?c=WangJun.DB.YunConfig&m=Load&p0={0}", YunConfig.CurrentGroupID), Encoding.UTF8); if (!string.IsNullOrWhiteSpace(configJson)) { var configDict = Convertor.FromJsonToDict2(configJson); ///加载当前的配置信息 foreach (var item in configDict) { if (item.Value.ToString().Contains("mongodb://")) { MongoDB.Register(item.Key, item.Value.ToString()); } else if (item.Value.ToString().Contains("Data Source=")) { SQLServer.Register(item.Key, item.Value.ToString()); } else if (item.Value.ToString().Contains("server=")) { MySQL.Register(item.Key, item.Value.ToString()); } } DataStorage.initialTime = DateTime.Now; LOGGER.Log("数据库组件配置信息初始化完毕 初始化为 " + YunConfig.CurrentGroupID); } } }
/// <summary> /// 获取一个实例 /// </summary> /// <param name="keyName"></param> /// <returns></returns> public static MongoDB GetInst(string keyName) { if (MongoDB.regDict.ContainsKey(keyName)) { var db = new MongoDB(); db.client = new MongoClient(MongoDB.regDict[keyName]); return(db); } return(null); }
/// <summary> /// 获取一个可操作性的实例 /// </summary> /// <param name="type"></param> /// <param name="exData"></param> /// <returns></returns> public static DataStorage GetInstance(DBType dbType, object exData = null) { DataStorage.Register(); DataStorage inst = new DataStorage(); if (DBType.MongoDB == dbType) { inst.mongo = MongoDB.GetInst("mongodb"); } else if (DBType.SQLServer == dbType) { inst.sqlserver = SQLServer.GetInstance("sqlserver"); } return(inst); }
/// <summary> /// /// </summary> /// <param name="keyName"></param> /// <param name="dbType">mongo,sqlserver,mysql,redis</param> /// <returns></returns> public static DataStorage GetInstance(string keyName = "140", string dbType = "mongo") { DataStorage.Register(); DataStorage inst = new DataStorage(); if ("mongo" == dbType) { inst.mongo = MongoDB.GetInst(keyName); } else if ("sqlserver" == dbType) { inst.sqlserver = SQLServer.GetInstance(keyName); inst.sqlserver.UpdateSysObject(true); } return(inst); }
/// <summary> /// 移动数据 /// </summary> /// <param name="sourceKeyName"></param> /// <param name="sourceDbName"></param> /// <param name="sourceCollectionName"></param> /// <param name="sourceFilter"></param> /// <param name="targetKeyName"></param> /// <param name="targetDbName"></param> /// <param name="targetCollectionName"></param> /// <param name="needDeleteSource"></param> public static void MoveCollection(MongoDB sourceInst, string sourceDbName, string sourceCollectionName, string sourceFilter, MongoDB targetInst, string targetDbName, string targetCollectionName, bool needDeleteSource = false) { if (null != sourceInst && null != targetInst) { sourceInst.EventTraverse += (object sender, EventArgs e) => { var ee = e as EventProcEventArgs; var dict = ee.Default as Dictionary <string, object>; var filter = string.Format("{{\"_id\":ObjectId('{0}')}}", dict["_id"]); targetInst.Save3(targetDbName, targetCollectionName, dict, filter); LOGGER.Log(string.Format("正在转移数据 {0}", dict["_id"])); ; if (true == needDeleteSource) { sourceInst.Delete(sourceDbName, sourceCollectionName, filter); } }; sourceInst.Find(sourceDbName, sourceCollectionName, sourceFilter); } }
/// <summary> /// 移动数据 /// </summary> /// <param name="sourceKeyName"></param> /// <param name="sourceDbName"></param> /// <param name="sourceCollectionName"></param> /// <param name="sourceFilter"></param> /// <param name="targetKeyName"></param> /// <param name="targetDbName"></param> /// <param name="targetCollectionName"></param> /// <param name="needDeleteSource"></param> public static void MoveCollection(string sourceKeyName, string sourceDbName, string sourceCollectionName, string sourceFilter, string targetKeyName, string targetDbName, string targetCollectionName, bool needDeleteSource = false) { var sourceInst = MongoDB.GetInst(sourceKeyName); var targetInst = MongoDB.GetInst(targetKeyName); var count = 0; sourceInst.EventTraverse += (object sender, EventArgs e) => { var ee = e as EventProcEventArgs; var dict = ee.Default as Dictionary <string, object>; var filter = string.Format("{{\"_id\":\"{0}\"}}", dict["_id"]); targetInst.Save2(targetDbName, targetCollectionName, filter, dict); Console.WriteLine("正在转移数据 {0} 已转移 {1}", dict["_id"], ++count); if (true == needDeleteSource) { sourceInst.Delete(sourceDbName, sourceCollectionName, filter); } }; sourceInst.Find(sourceDbName, sourceCollectionName, sourceFilter); }
/// <summary> /// 转移集合 /// </summary> /// <param name="sourceKeyName"></param> /// <param name="sourceDbName"></param> /// <param name="sourceCollectionName"></param> /// <param name="sourceFilter"></param> /// <param name="targetKeyName"></param> /// <param name="targetDbName"></param> /// <param name="targetCollectionName"></param> /// <param name="needDeleteSource"></param> public static void MoveCollection(DataStorage sourceInst, string sourceDbName, string sourceCollectionName, string sourceFilter, DataStorage targetInst, string targetDbName, string targetCollectionName, bool needDeleteSource = false) { MongoDB.MoveCollection(sourceInst.mongo, sourceDbName, sourceCollectionName, sourceFilter, targetInst.mongo, targetDbName, targetCollectionName, needDeleteSource); }
/// <summary> /// 转移集合 /// </summary> /// <param name="sourceKeyName"></param> /// <param name="sourceDbName"></param> /// <param name="sourceCollectionName"></param> /// <param name="sourceFilter"></param> /// <param name="targetKeyName"></param> /// <param name="targetDbName"></param> /// <param name="targetCollectionName"></param> /// <param name="needDeleteSource"></param> public static void MoveCollection(string sourceKeyName, string sourceDbName, string sourceCollectionName, string sourceFilter, string targetKeyName, string targetDbName, string targetCollectionName, bool needDeleteSource = false) { MongoDB.MoveCollection(sourceKeyName, sourceDbName, sourceCollectionName, sourceFilter, targetKeyName, targetDbName, targetCollectionName, needDeleteSource); }