Пример #1
0
        ///// <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);
        }
Пример #2
0
        static SchemaManager()
        {
            //从XML加载

            //读取SQL文件,默认在根目录下面的sql 文件夹中 (此文件夹禁止外部访问)
            //string filePath = AppDomain.CurrentDomain.BaseDirectory;//web程序默认在根目录,非WEB程序默认在bin\debug
            //if (!filePath.EndsWith(@"\"))//非WEB程序默认不含\结束,
            //{
            //    filePath = filePath + @"\";
            //}
            //if (filePath.Contains("bin"))
            //{
            //    filePath = filePath.Substring(0, filePath.Length - 10);//默认根目录,去掉bin\debug

            //}
            //filePath = filePath + DefaultSQLFilePath;
            //var sqlFiles = Directory.GetFiles(filePath, "*.xml", SearchOption.AllDirectories);
            List <string> sqlFiles = GetRNames();

            foreach (var sqlFile in sqlFiles)
            {
                var sqlDoc = new XmlDocument();
                try
                {
                    sqlDoc.LoadXml(sqlFile);
                }
                catch
                {
                    throw new Exception(sqlFile + "格式错误!");
                }
                XmlNode tableNode = sqlDoc.DocumentElement;

                //读取每个xml里面的SQL语句,放入SQL字典,名称为DBName.TableName.SqlName
                var    columnNodes = sqlDoc.DocumentElement.SelectNodes("//Table/COLUMN");
                string dbname      = tableNode.Attributes["DBName"].Value;
                string tablename   = tableNode.Attributes["TableName"].Value;
                string keyForTable = string.Format("{0}.{1}", dbname, tablename).ToLower();

                if (columnNodes != null)
                {
                    foreach (XmlNode columnElement in columnNodes)
                    {
                        Column column = new Column();
                        column.ColumnName = columnElement.Attributes["Name"].Value;
                        column.Type       = columnElement.Attributes["Type"].Value;
                        column.Default    = columnElement.Attributes["Default"].Value;
                        column.IsPrimary  = Convert.ToBoolean(columnElement.Attributes["IsPrimary"].Value);
                        if (column.IsPrimary)
                        {
                            if (_primaryColumnsDic.ContainsKey(keyForTable))
                            {
                                throw new Exception(string.Format("不支持多主键:{0}", keyForTable));
                            }
                            _primaryColumnsDic.Add(keyForTable, column.ColumnName);
                        }
                        if (!_allColumnsDic.ContainsKey(keyForTable))
                        {
                            _allColumnsDic.Add(keyForTable, new Dictionary <string, Column>());
                        }
                        _allColumnsDic[keyForTable].Add(column.ColumnName, column);
                    }
                }

                //读取每个xml里面的SQL语句,放入SQL字典,名称为DBName.TableName.SqlName
                var sqlNodes = sqlDoc.DocumentElement.SelectNodes("//Table/SQL");
                if (sqlNodes != null)
                {
                    foreach (XmlNode sqlElement in sqlNodes)
                    {
                        SqlObj sqlObj  = new SqlObj();
                        string key     = string.Format("{0}.{1}.{2}", dbname, tablename, sqlElement.Attributes["SqlName"].Value).ToLower();
                        string sqltext = sqlElement.InnerText.Trim();
                        sqlObj.Name    = key;
                        sqlObj.SqlText = sqltext;
                        if (sqlElement.Attributes["dbrwtype"] != null)//SQL本身包含
                        {
                            sqlObj.DBrwType = (DBrwType)Enum.Parse(typeof(DBrwType), sqlElement.Attributes["dbrwtype"].Value);
                        }
                        else if (tableNode.Attributes["dbrwtype"] != null)//table上包含定义
                        {
                            sqlObj.DBrwType = (DBrwType)Enum.Parse(typeof(DBrwType), tableNode.Attributes["dbrwtype"].Value);
                        }
                        else
                        {
                            sqlObj.DBrwType = DBrwType.None;//默认未设置
                        }

                        XmlNodeList defaultParameterNodes = sqlElement.SelectNodes("defaultParameters/item");
                        if (defaultParameterNodes != null)//包含默认参数
                        {
                            HashObject defaultParameters = new HashObject();
                            foreach (XmlNode item in defaultParameterNodes)
                            {
                                defaultParameters.Add(item.Attributes["name"].Value, item.Attributes["value"].Value);
                            }
                            sqlObj.DefaultParameters = defaultParameters;
                        }


                        bool isoverride = sqlElement.Attributes["isoverride"] == null ? false : true;
                        if (_sqlDic.ContainsKey(key))
                        {
                            if (isoverride)
                            {
                                _sqlDic.Remove(key);
                                _sqlDic.Add(key, sqlObj);
                            }
                        }
                        else
                        {
                            _sqlDic.Add(key, sqlObj);
                        }
                    }
                }
            }
        }