/// <summary> /// 构造一个DALBase 实例 /// </summary> /// <param name="dbName">数据库名称</param> /// <param name="tableName">表名称</param> public DALBase(string dbName, string tableName, DBrwType dbrwType = DBrwType.Write, bool logenable = false) { this._dbName = dbName.ToLower(); this._tableName = tableName.ToLower(); this._dbrwType = dbrwType; this._allColumns = SchemaManager.GetAllColumns(this._dbName, this._tableName); this._primaryColumn = SchemaManager.GetPrimaryColumn(this._dbName, this._tableName); this.LogEnable = logenable; }
///// <summary> ///// 所有主键列信息 ///// </summary> //public Dictionary<string, Column> PrimaryColumns //{ // get // { // return _allColumns; // } //} /// <summary> /// 创建一个DALBase 实例 /// </summary> /// <param name="dbName">数据库名称</param> /// <param name="tableName">表名称</param> /// <returns></returns> public static DALBase Create(string dbName, string tableName, DBrwType dbrwType = DBrwType.Write) { //if (ifforbatch == true)//批量操作的时候,不取缓存的值(因为有不同的实例参数) // return new DALBase(dbName, tableName, dbrwType); string key = string.Format("{0}.{1}.{2}", dbName, tableName, (int)dbrwType).ToLower(); if (!_dic.ContainsKey(key)) { lock (_lock) { if (!_dic.ContainsKey(key)) { DALBase dal = new DALBase(dbName, tableName, dbrwType); _dic.Add(key, dal); } } } return(_dic[key]); }
///// <summary> ///// 暂时放到conn.xml 中保存,未来可放入数据库 ///// </summary> ///// <returns></returns> //private static List<ConnectionConfig> GetConnConfig() //{ // List<ConnectionConfig> list = new List<ConnectionConfig>(); // string filePath = AppDomain.CurrentDomain.BaseDirectory;//web程序默认在根目录,非WEB程序默认在bin\debug // if (!filePath.EndsWith(@"\"))//非WEB程序默认不含\结束, // { // filePath = filePath + @"\"; // } // filePath = filePath + "conn.xml"; // try // { // var sqlDoc = new XmlDocument(); // sqlDoc.Load(filePath); // var sqlNodes = sqlDoc.DocumentElement.SelectNodes("//Root/DBConfig"); // foreach (XmlNode element in sqlNodes) // { // ConnectionConfig connConfig = new ConnectionConfig(); // connConfig.ShardID = Convert.ToInt32(element.Attributes["ShardID"].Value); // connConfig.DBName = element.Attributes["DBName"].Value; // connConfig.ReadWriteType = Convert.ToInt32(element.Attributes["ReadWriteType"].Value); // connConfig.Status = Convert.ToInt32(element.Attributes["Status"].Value); // connConfig.ConnectionString = element.InnerText; // if (connConfig.Status == 1)//状态开启 // { // list.Add(connConfig); // } // } // } // catch // { // throw new Exception("conn.xml 初始化失败!"); // } // return list; //} //private void init() //{ // //List<ConnectionConfig> connList = ConnectionConfig.GetConnectionConfig(); // //根据配置组装放入静态字典 // //foreach (ConnectionConfig config in connList) // //{ // // if (config.ReadWriteType == 1 || config.ReadWriteType == 3) // // { // // //分区 - 数据库名称 - 读写 例如:1_test_1 // // string key = string.Format("{0}_{1}_1", config.ShardID, config.DBName).ToLower(); // // List<string> list; // // if (!_dicConn.ContainsKey(key)) // // { // // list = new List<string>(); // // _dicConn.Add(key, list); // // } // // else // // { // // list = _dicConn[key]; // // } // // list.Add(config.ConnectionString); // // } // // if (config.ReadWriteType == 2 || config.ReadWriteType == 3) // // { // // //分区 - 数据库名称 - 读写 例如:1_test_2 // // string key = string.Format("{0}_{1}_2", config.ShardID, config.DBName).ToLower(); // // List<string> list; // // if (!_dicConn.ContainsKey(key)) // // { // // list = new List<string>(); // // _dicConn.Add(key, list); // // } // // else // // { // // list = _dicConn[key]; // // } // // list.Add(config.ConnectionString); // // } // //} //} ///// <summary> ///// 单库根据库名、读写状态返回合理的连接字符串(来自memcached) ///// </summary> ///// <param name="dbName">数据库名称</param> ///// <param name="dbrwType">读写状态</param> ///// <returns></returns> //public static string GetConnectionString(string dbName, DBrwType dbrwType) //{ // //单库始终1号分区 // //根据库名、读写状态返回合理的连接字符串(来自memcached) // return GetConnectionString(dbName, dbrwType, 0); //} /// <summary> /// 分布式库根据库名、读写状态、分区ID返回合理的连接字符串(来自memcached) /// </summary> /// <param name="dbName">数据库名称</param> /// <param name="dbrwType">读写状态</param> /// <param name="HostID">分区ID</param> /// <returns></returns> public static ConnectionConfig GetConnectionString(string dbName, DBrwType dbrwType, long shardID = 1) { //int shardID = GetShardID(dbName, HostID); string key = string.Format("{0}_{1}_{2}", shardID, dbName, (int)dbrwType).ToLower(); if (_hashConn.ContainsKey(key) == false)//没有合适的读写字符串 { throw new Exception(string.Format("no matched sql connection:{0}", key)); } else { List <ConnectionConfig> conns = (List <ConnectionConfig>)_hashConn[key]; if (conns.Count == 1) { return(conns[0]); } else//超过1个时,随机选择一个读取 { int i = _random.Next() % conns.Count; return(conns[i]); } } }
/// <summary> /// 构造一个DALBase 实例 /// </summary> /// <param name="dbName">数据库名称</param> /// <param name="tableName">表名称</param> public DALBase(string dbName, DBrwType dbrwType = DBrwType.Write, bool logenable = false) { this._dbName = dbName.ToLower(); this._dbrwType = dbrwType; this.LogEnable = logenable; }
///// <summary> ///// 替换带#号参数 ///// </summary> //private void ReplaceSQL() //{ // if (this.ReplaceParameters != null) // { // foreach (KeyValuePair<string, object> pair in this.ReplaceParameters) // { // this.CmdText = this.CmdText.Replace(pair.Key, pair.Value.ToString()); // } // } //} //private void SetRwType() //{ // switch (FunctionName) // { // case "ExecuteNonQuery": // case "ExecuteScalarWrite": // this._DBrwType = DBrwType.Write; // break; // case "ExecuteDataSet": // case "ExecuteScalarRead": // case "Exists": // case "GetData": // case "GetDataList": // case "GetDicListByGroup": // this._DBrwType = DBrwType.Read; // break; // default: // throw new Exception("读写类型未设置"); // break; // } //} /// <summary> /// dbhelper 包装器 /// </summary> /// <param name="dbName">数据库名称</param> /// <param name="tableName">表名称</param> /// <param name="sqlName">sql名称(不存在时传空)</param> /// <param name="parameters">参数(默认null)</param> /// <param name="cmdType">执行类型(默认text)</param> public DbHelperWrapper(DBrwType dbrwType, string dbName, string tableName, string sqlName, HashObject parameters = null, SqlType sqlType = SqlType.SqlName, CommandType cmdType = CommandType.Text) { this.DbName = dbName; this.TableName = tableName; this.SqlType = sqlType; this.SQLName = sqlName; this.CmdType = cmdType; this.LogEnable = true;//默认开启日志记录 this.ReplaceParameters = new HashObject(); //拷贝参数 this.Parameters = new HashObject(); if (parameters != null) { foreach (KeyValuePair <string, object> pair in parameters) { if (pair.Key.IndexOf('#') == 0)//替换参数 { this.ReplaceParameters.Add(pair.Key, pair.Value); } else { this.Parameters.Add(pair.Key, pair.Value); } } } //获取SQL,补充默认参数 if (this.SqlType == SqlType.SqlName) { SqlObj sqlobj = SchemaManager.GetDefaultSQL(this.DbName, this.TableName, SQLName); this.CmdText = sqlobj.SqlText; if (sqlobj.DBrwType != DBrwType.None)//检查SQL上是否包含设置 { this._DBrwType = sqlobj.DBrwType; } else { this._DBrwType = dbrwType;//否则取dalbase 传递过来的读写类型 } foreach (KeyValuePair <string, object> pair in sqlobj.DefaultParameters) { if (!this.ReplaceParameters.ContainsKey(pair.Key)) { this.ReplaceParameters.Add(pair.Key, pair.Value); } } } else { this.CmdText = SQLName; this._DBrwType = dbrwType;//取dalbase 传递过来的读写类型 } //#替换SQL foreach (KeyValuePair <string, object> pair in this.ReplaceParameters) { this.CmdText = this.CmdText.Replace(pair.Key, pair.Value.ToString()); } //计算分区 this.ShardID = ShardManager.GetShardID(this.DbName, this.Parameters); }