Пример #1
0
        /// <summary>
        /// 根据EntityMapSql的全名称 "名称空间名字.SQL名字" 获取映射的SQL语句
        /// </summary>
        /// <param name="fullName">EntityMapSql的全名称,格式: "名称空间名字.SQL名字"</param>
        /// <param name="entityType">根据当前实体类所在程序集,获取其中的嵌入式EntityMapSql 文件</param>
        /// <returns>映射的SQL语句</returns>
        public static string GetMapSql(string fullName, Type entityType)
        {
            //string[] arrTemp=fullName .Split ('.');
            //if (arrTemp.Length != 2)
            //    throw new Exception("EntityMapSql的全名称格式错误,正确的格式应该: 名称空间名字.SQL名字");

            //string resourceName = "EntitySqlMap.config";
            //string xmlConfig=   CommonUtil.GetAssemblyResource(entityType, resourceName);

            //XmlDocument doc = new XmlDocument();
            //doc.LoadXml(xmlConfig);
            //XmlNode SqlText = default(XmlNode);
            //XmlElement root = doc.DocumentElement;
            //string objPath = "/configuration/Namespace[@name='" + arrTemp[0] + "']/Map[@name='" + arrTemp[1] + "']/Sql";
            //SqlText = root.SelectSingleNode(objPath);
            //if ((SqlText != null) && SqlText.HasChildNodes)
            //{
            //    return SqlText.InnerText;
            //}
            //return "";
            return(EntityQueryAnonymous.GetMapSql(fullName, entityType));
        }
Пример #2
0
        private IDataReader ExecuteDataReader(OQL oql, AdoHelper db)
        {
            string sql = "";

            sql = oql.ToString();

            //处理实体类分页 2010.6.20
            if (oql.PageEnable)
            {
                //处理分页统前的记录数量统计问题 感谢网友 @成都-小兵 发现此问题
                if (oql.PageWithAllRecordCount == 0)
                {
                    object oValue = EntityQueryAnonymous.ExecuteOQLCount(oql, db);
                    oql.PageWithAllRecordCount = CommonUtil.ChangeType <int>(oValue);
                }

                switch (db.CurrentDBMSType)
                {
                case PWMIS.Common.DBMSType.Access:
                case PWMIS.Common.DBMSType.SqlServer:
                case PWMIS.Common.DBMSType.SqlServerCe:
                    if (oql.haveJoinOpt)
                    {
                        if (oql.PageNumber <= 1)     //仅限定记录条数
                        {
                            sql = "Select Top " + oql.PageSize + " " + sql.Trim().Substring("SELECT ".Length);
                        }
                        else     //必须采用复杂分页方案
                        {
                            //edit at 2012.10.2 oql.PageWithAllRecordCount
                            sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.SqlServer, sql, "", oql.PageSize, oql.PageNumber, oql.PageWithAllRecordCount);
                        }
                    }
                    else
                    {
                        //单表查询的情况
                        if (oql.PageOrderDesc)
                        {
                            sql = PWMIS.Common.SQLPage.GetDescPageSQLbyPrimaryKey(oql.PageNumber, oql.PageSize, oql.sql_fields, oql.sql_table, oql.PageField, oql.sql_condition);
                        }
                        else
                        {
                            sql = PWMIS.Common.SQLPage.GetAscPageSQLbyPrimaryKey(oql.PageNumber, oql.PageSize, oql.sql_fields, oql.sql_table, oql.PageField, oql.sql_condition);
                        }
                    }

                    break;

                case PWMIS.Common.DBMSType.Oracle:
                    sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.Oracle, sql, "", oql.PageSize, oql.PageNumber, 999);
                    break;

                case PWMIS.Common.DBMSType.MySql:
                    sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.MySql, sql, "", oql.PageSize, oql.PageNumber, 999);
                    break;

                case PWMIS.Common.DBMSType.PostgreSQL:
                    sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.PostgreSQL, sql, "", oql.PageSize, oql.PageNumber, 999);
                    break;

                default:
                    throw new Exception("实体类分页错误:不支持此种类型的数据库分页。");
                }
            }

            IDataReader reader = null;

            if (oql.Parameters != null && oql.Parameters.Count > 0)
            {
                int fieldCount         = oql.Parameters.Count;
                IDataParameter[] paras = EntityQueryAnonymous.GetParameters(oql.Parameters, db);
                //int index = 0;

                //foreach (string name in oql.Parameters.Keys)
                //{
                //    paras[index] = db.GetParameter(name, oql.Parameters[name]);
                //    index++;
                //}
                reader = db.ExecuteDataReader(sql, CommandType.Text, paras);
            }
            else
            {
                reader = db.ExecuteDataReader(sql);
            }
            return(reader);
        }
Пример #3
0
        private IDataReader ExecuteDataReader(OQL oql, AdoHelper db)
        {
            string sql = null;

            //处理实体类分页 2010.6.20
            if (oql.PageEnable)
            {
                //处理分页统前的记录数量统计问题 感谢网友 @成都-小兵 发现此问题
                if (oql.PageWithAllRecordCount == 0)
                {
                    //网友 if-else 发现以下问题,备注:
                    //这个函数在只有where而没有排序的时候,生成的统计语句的sql字符串的不对,会生成这种sql
                    //发现没有排序,且pagenumber为1的时候,就直接返回了这种select top这个字符串,自然就查不到count了
                    //其次就是有分页的时候,把字段名转大写了
                    //都在SQLpage.cs
                    object oValue = EntityQueryAnonymous.ExecuteOQLCount(oql, db);
                    oql.PageWithAllRecordCount = CommonUtil.ChangeType <int>(oValue);
                    if (oql.PageWithAllRecordCount == 0)
                    {
                        return(null);
                    }
                }

                #region  面代码已经重构

                /*
                 *
                 * switch (db.CurrentDBMSType)
                 * {
                 *  case PWMIS.Common.DBMSType.Access:
                 *  case PWMIS.Common.DBMSType.SqlServer:
                 *  case PWMIS.Common.DBMSType.SqlServerCe:
                 *      if (oql.haveJoinOpt)
                 *      {
                 *          if (oql.PageNumber <= 1) //仅限定记录条数
                 *          {
                 *              sql = "Select Top " + oql.PageSize + " " + sql.Trim().Substring("SELECT ".Length);
                 *
                 *          }
                 *          else //必须采用复杂分页方案
                 *          {
                 *              //edit at 2012.10.2 oql.PageWithAllRecordCount
                 *              sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.SqlServer, sql, "", oql.PageSize, oql.PageNumber, oql.PageWithAllRecordCount);
                 *
                 *          }
                 *      }
                 *      else
                 *      {
                 *          //单表查询的情况
                 *          if (oql.PageOrderDesc)
                 *              sql = PWMIS.Common.SQLPage.GetDescPageSQLbyPrimaryKey(oql.PageNumber, oql.PageSize, oql.sql_fields, oql.sql_table, oql.PageField, oql.sql_condition);
                 *          else
                 *              sql = PWMIS.Common.SQLPage.GetAscPageSQLbyPrimaryKey(oql.PageNumber, oql.PageSize, oql.sql_fields, oql.sql_table, oql.PageField, oql.sql_condition);
                 *      }
                 *
                 *      break;
                 *  case PWMIS.Common.DBMSType.Oracle:
                 *      sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.Oracle, sql, "", oql.PageSize, oql.PageNumber, 999);
                 *      break;
                 *  case PWMIS.Common.DBMSType.MySql:
                 *      sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.MySql, sql, "", oql.PageSize, oql.PageNumber, 999);
                 *      break;
                 *  case PWMIS.Common.DBMSType.PostgreSQL:
                 *      sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.PostgreSQL, sql, "", oql.PageSize, oql.PageNumber, 999);
                 *      break;
                 *  default:
                 *      throw new Exception("实体类分页错误:不支持此种类型的数据库分页。");
                 *
                 * }
                 */
                #endregion

                sql = EntityQueryAnonymous.GetOQLPageSql(oql, db);
                //分页SQL为空,可能分页页码超出可用范围,感谢 @深圳-有事Q我 发现此bug
                if (sql == string.Empty)
                {
                    return(null);
                }
            }
            else
            {
                sql = oql.ToString();
            }

            IDataReader reader = null;
            if (oql.Parameters != null && oql.Parameters.Count > 0)
            {
                int fieldCount         = oql.Parameters.Count;
                IDataParameter[] paras = EntityQueryAnonymous.GetParameters(oql.Parameters, db);
                //int index = 0;

                //foreach (string name in oql.Parameters.Keys)
                //{
                //    paras[index] = db.GetParameter(name, oql.Parameters[name]);
                //    index++;
                //}
                reader = db.ExecuteDataReader(sql, CommandType.Text, paras);
            }
            else
            {
                reader = db.ExecuteDataReader(sql);
            }
            return(reader);
        }
Пример #4
0
        /// <summary>
        /// 根据实体查询表达式对象,和当前数据库操作对象,查询实体对象集合
        /// </summary>
        /// <param name="oql">实体查询表达式对象</param>
        /// <param name="db">数据库操作对象</param>
        /// <returns>实体对象集合</returns>
        public static List <T> QueryList(OQL oql, AdoHelper db)
        {
            //string sql = "";
            ////处理用户查询映射的实体类
            //if (oql.EntityMap == PWMIS.Common.EntityMapType.SqlMap)
            //{
            //    if (CommonUtil.CacheEntityMapSql == null)
            //        CommonUtil.CacheEntityMapSql = new Dictionary<string, string>();
            //    if (CommonUtil.CacheEntityMapSql.ContainsKey(oql.sql_table))
            //        sql = CommonUtil.CacheEntityMapSql[oql.sql_table];
            //    else
            //    {
            //        sql = oql.GetMapSQL(GetMapSql(oql.sql_table, typeof(T)));
            //        CommonUtil.CacheEntityMapSql.Add(oql.sql_table, sql);
            //    }

            //}
            //else
            //    sql = oql.ToString();

            ////处理实体类分页 2010.6.20
            //if (oql.PageEnable)
            //{
            //    if (db is SqlServer || db is Access)
            //    {
            //        if (oql.PageOrderDesc)
            //            sql = PWMIS.Common.SQLPage.GetDescPageSQLbyPrimaryKey(oql.PageNumber, oql.PageSize, oql.sql_fields, oql.sql_table, oql.PageField, oql.sql_condition);
            //        else
            //            sql = PWMIS.Common.SQLPage.GetAscPageSQLbyPrimaryKey(oql.PageNumber, oql.PageSize, oql.sql_fields, oql.sql_table, oql.PageField, oql.sql_condition);
            //    }
            //    else if (db is Oracle)
            //    {
            //        sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.Oracle, sql, "", oql.PageSize, oql.PageNumber, 999);
            //    }
            //    else
            //    {
            //        throw new Exception("实体类分页错误:不支持此种类型的数据库分页。");
            //    }
            //}

            //IDataReader reader = null;
            //if (oql.Parameters != null && oql.Parameters.Count > 0)
            //{
            //    int fieldCount = oql.Parameters.Count;
            //    IDataParameter[] paras = new IDataParameter[fieldCount];
            //    int index = 0;

            //    foreach (string name in oql.Parameters.Keys)
            //    {
            //        paras[index] = db.GetParameter(name, oql.Parameters[name]);
            //        index++;
            //    }
            //    reader = db.ExecuteDataReader(sql, CommandType.Text, paras);
            //}
            //else
            //{
            //    reader = db.ExecuteDataReader(sql);
            //}
            IDataReader reader = EntityQueryAnonymous.ExecuteDataReader(oql, db, typeof(T));

            return(QueryList(reader));
        }