Beispiel #1
0
        internal static SqlInfo GetSqlInfoFromOQL(OQL oql, AdoHelper db, Type factEntityType, bool single)
        {
            string sql = "";
            Dictionary <string, TableNameField> Parameters = null;

            if (oql.EntityMap == PWMIS.Common.EntityMapType.Table ||
                oql.EntityMap == PWMIS.Common.EntityMapType.View)
            {
                #region OQL2SqlInfo
                if (oql.PageEnable && (!single || oql.PageWithAllRecordCount <= 0))
                {
                    sql = GetOQLPageSql(oql, db);
                }
                else
                {
                    sql = oql.ToString();
                }

                SqlInfo result = new SqlInfo(sql, oql.Parameters);
                result.CommandType = CommandType.Text;
                result.TableName   = oql.GetEntityTableName();
                return(result);

                #endregion
            }
            else if (oql.EntityMap == PWMIS.Common.EntityMapType.SqlMap)
            {
                #region SQLMAP
                //处理用户查询映射的实体类
                if (CommonUtil.CacheEntityMapSql == null)
                {
                    CommonUtil.CacheEntityMapSql = new Dictionary <string, string>();
                }
                if (!CommonUtil.CacheEntityMapSql.ContainsKey(oql.sql_table))
                {
                    string tempView = GetMapSql(factEntityType);
                    CommonUtil.CacheEntityMapSql.Add(oql.sql_table, tempView);
                }
                sql = oql.GetMapSQL(CommonUtil.CacheEntityMapSql[oql.sql_table]);


                //如果用户本身没有初始化参数对象,则这里声明一个 edit at 2012.11.16
                Parameters = new Dictionary <string, TableNameField>();
                if (oql.InitParameters != null)
                {
                    foreach (string name in oql.InitParameters.Keys)
                    {
                        TableNameField tnf = new TableNameField();
                        tnf.FieldValue = oql.InitParameters[name];
                        Parameters.Add(name, tnf);
                    }
                }

                if (oql.Parameters != null && oql.Parameters.Count > 0)
                {
                    foreach (string name in oql.Parameters.Keys)
                    {
                        Parameters.Add(name, oql.Parameters[name]);
                    }
                }

                //这里可能需要特别处理分页 --修改 @芜湖-大枕头 2016.12.15
                if (oql.PageEnable && (!single || oql.PageWithAllRecordCount <= 0))
                {
                    sql = GetOQLPageSql(oql, db);
                }
                else
                {
                    sql = oql.ToString();
                }
                SqlInfo si = new SqlInfo(sql, Parameters);
                si.CommandType = CommandType.Text;
                return(si);

                #endregion
            }
            else
            {
                #region StoredProcedure
                string script = "";
                if (CommonUtil.CacheEntityMapSql == null)
                {
                    CommonUtil.CacheEntityMapSql = new Dictionary <string, string>();
                }
                //获取SQL-MAP脚本
                if (CommonUtil.CacheEntityMapSql.ContainsKey(oql.sql_table))
                {
                    script = CommonUtil.CacheEntityMapSql[oql.sql_table];
                }
                else
                {
                    script = GetMapSql(factEntityType);
                    CommonUtil.CacheEntityMapSql.Add(oql.sql_table, script);
                }
                //对SQL-MAP格式的参数进行解析
                SqlMap.SqlMapper mapper = new PWMIS.DataMap.SqlMap.SqlMapper();
                mapper.DataBase = db;
                //解析存储过程名称
                sql = mapper.FindWords(mapper.GetScriptInfo(script), 0, 255); //由于是存储过程,需要特殊处理,调用 FindWords方法
                //解析参数
                IDataParameter[] paras = mapper.GetParameters(script);
                if (oql.InitParameters != null && oql.InitParameters.Count > 0)
                {
                    Parameters = new Dictionary <string, TableNameField>();
                    try
                    {
                        foreach (IDataParameter para in paras)
                        {
                            string key = para.ParameterName.TrimStart(db.GetParameterChar.ToCharArray());
                            para.Value = oql.InitParameters[key];
                            Parameters.Add(key, new TableNameField()
                            {
                                FieldValue = para
                            });
                        }
                    }
                    catch (KeyNotFoundException exKey)
                    {
                        throw new KeyNotFoundException("'存储过程实体类'的初始化参数中没有找到指定的参数名,请检查参数定义和设置。", exKey);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    if (paras.Length > 0)
                    {
                        throw new Exception("当前'存储过程实体类'需要提供初始化参数,请设置OQL对象的InitParameters属性");
                    }
                }

                SqlInfo si = new SqlInfo(sql, Parameters);
                si.CommandType = CommandType.StoredProcedure;
                return(si);

                #endregion
            }
        }