Ejemplo n.º 1
0
        public DBMiddleware(RequestDelegate _next,
                            IInterfaceDAL interfaceDAL,
                            IFlowStepDAL flowStpeDAL,
                            IDbHelper dbHelp,
                            DBRouteValueDictionary _routeDict,
                            IConfiguration config)
        {
            this.next      = _next;
            this.dbHelp    = dbHelp;
            this.routeDict = _routeDict;

            SqlConnStr = config.GetConnectionString("SqlConnStr");

            //加载路由集合
            List <InterfaceModel> dtInterface = interfaceDAL.All();

            foreach (InterfaceModel model in dtInterface)
            {
                //加载步骤
                int              SN      = model.SN;
                string           Url     = model.Url;
                DBInterfaceModel dbInter = new DBInterfaceModel();
                dbInter.SerializeType = model.SerializeType;
                dbInter.Method        = model.Method;
                dbInter.Steps         = flowStpeDAL.Query(SN);
                dbInter.IsTransaction = model.IsTransaction;
                dbInter.ExecuteType   = model.ExecuteType;

                routeDict.Add(Url, dbInter);
            }
        }
Ejemplo n.º 2
0
        public myResponse UpLoad(int SN)
        {
            myResponse res = new myResponse();

            InterfaceModel model = new InterfaceModel();

            model.SN = SN;
            model    = (InterfaceModel)this.infterfaceDAL.Get(model);

            DBInterfaceModel dbInter = new DBInterfaceModel();

            dbInter.SerializeType = model.SerializeType;
            dbInter.Method        = model.Method;
            dbInter.IsTransaction = model.IsTransaction;
            dbInter.ExecuteType   = model.ExecuteType;

            //步骤
            dbInter.Steps = this.flowStepDAL.Query(SN);

            string url = model.Url;

            if (this.routeDict.ContainsKey(url))
            {
                this.routeDict[url] = dbInter;
            }
            else
            {
                this.routeDict.Add(url, dbInter);
            }
            return(res);
        }
Ejemplo n.º 3
0
        private async Task InvokeDB(HttpContext context)
        {
            this.context = context;

            string           path     = context.Request.Path.ToString();
            DBInterfaceModel response = this.routeDict[path];

            #region init

            SqlConnection connection = new SqlConnection(SqlConnStr);
            SqlCommand    cmd        = new SqlCommand();
            cmd.CommandTimeout = 0;
            cmd.Connection     = connection;
            SqlDataAdapter sqlDA = new SqlDataAdapter();
            sqlDA.SelectCommand = cmd;

            #endregion

            SqlTransaction tran = null;

            if (response.IsTransaction)
            {
                tran            = connection.BeginTransaction();
                cmd.Transaction = tran;
            }

            string excCmdText = ""; //存储异常的存储过程名

            DataResult returnValue = new DataResult();
            if (response.Steps.Count > 0)
            {
                #region 执行步骤
                foreach (FlowStepModel flow in response.Steps)
                {
                    switch (flow.CommandType)
                    {
                    case "Text":
                        cmd.CommandType = CommandType.Text;
                        break;

                    case "StoredProcedure":
                        cmd.CommandType = CommandType.StoredProcedure;
                        break;
                    }

                    if (excCmdText != "")
                    {
                        excCmdText += ",";
                    }
                    excCmdText += flow.CommandText;

                    if (flow.CommandText == "")
                    {
                        string msg1 = flow.StepName + " 没有数据库语句,请维护";
                        await InvokeException(msg1);

                        return;
                    }
                    cmd.CommandText = flow.CommandText;
                    cmd.Parameters.Clear();

                    if (response.Method.ToLower() == "get")
                    {
                        //cmd.Parameters.AddWithValue("SN", 1);
                        foreach (string key in context.Request.Query.Keys)
                        {
                            StringValues value  = new StringValues();
                            bool         value1 = context.Request.Query.TryGetValue(key, out value);
                            cmd.Parameters.AddWithValue(key, value[0]);
                        }
                    }
                    else if (response.Method.ToLower() == "post")
                    {
                        try
                        {
                            var reader          = new StreamReader(context.Request.Body);
                            var contentFromBody = reader.ReadToEnd();
                            if (contentFromBody != "")
                            {
                                Dictionary <string, object> dict = JsonHelper.DeserializeJSON <Dictionary <string, object> >(contentFromBody);
                                foreach (KeyValuePair <string, object> kv in dict)
                                {
                                    cmd.Parameters.AddWithValue(kv.Key, kv.Value);
                                }
                            }
                        }
                        catch (System.Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
                #endregion

                //执行语句 
                await ExecSql(response, tran, sqlDA, cmd, excCmdText);   
            }
            else
            {
                await context.Response.WriteAsync(path + "没有任何步骤,请维护", UTF8Encoding.UTF8);
            }
        }
Ejemplo n.º 4
0
        private async Task ExecSql(DBInterfaceModel response, SqlTransaction tran, SqlDataAdapter sqlDA, SqlCommand cmd, string excCmdText)
        {
            string json = "";

            try
            {
                XmlHelper xmlHelp = new XmlHelper();

                switch (response.ExecuteType)
                {
                case "Scalar":
                    object obj = cmd.ExecuteScalar();
                    //DataResult dataResult = new DataResult();
                    //dataResult.Result = obj;
                    if (response.SerializeType == "Xml")
                    {
                        json = xmlHelp.SerializeXML(obj);
                    }
                    else
                    {
                        json = JsonHelper.SerializeJSON(obj);
                    }
                    break;

                case "Int":
                    int iResult = cmd.ExecuteNonQuery();
                    //IntDataResult intdataResult = new IntDataResult();
                    //intdataResult.Result = iResult;
                    if (response.SerializeType == "Xml")
                    {
                        json = xmlHelp.SerializeXML(iResult);
                    }
                    else
                    {
                        json = JsonHelper.SerializeJSON(iResult);
                    }

                    break;

                case "DataSet":

                    DataSet ds = new DataSet();
                    sqlDA.Fill(ds);

                    if (response.SerializeType == "Xml")
                    {
                        XmlDSDataResult xmlResult = new XmlDSDataResult();
                        json = xmlHelp.SerializeXML(ds);
                    }
                    else
                    {
                        //DSDataResult dsDataResult = new DSDataResult();
                        //dsDataResult.Result = ds;
                        json = JsonHelper.SerializeJSON(ds);
                    }
                    break;
                }
            }
            catch (System.Exception ex)
            {
                string msg = excCmdText + ex.Message;
                await InvokeException(msg);

                return;
            }

            if (response.IsTransaction)
            {
                tran.Commit();
            }

            await context.Response.WriteAsync(json, UTF8Encoding.UTF8);
        }