/// <summary> /// Bind AbsentEvent Tables dropdown /// </summary> private void BindAbsentEventTables() { // get a list of absent event tables IDictionary <string, string> absentEventTables = _dataEntryController.GetAbsentEventTables(); TableNameField.DataSource = absentEventTables; TableNameField.DataBind(); }
/// <summary> /// 根据OQL内部的实体类,将查询结果直接映射到这些实体类实例对象上去 /// </summary> /// <param name="entitys">OQL内部的实体类</param> /// <returns></returns> private IEnumerable <object[]> MapMoreEntity(EntityBase[] entitys) { if (this.Values == null) { int rowsCount = this.Execute(); if (rowsCount <= 0) { yield break; } } if (this.Values != null && this.fieldNames != null) { if (this.Values.Count == 0) { yield break; } TableNameField[] fieldInfo = new TableNameField[this.OQL.selectedFieldInfo.Count]; //查找字段匹配情况 //entity.PropertyNames 存储的仅仅是查询出来的列名称 for (int i = 0; i < this.OQL.selectedFieldInfo.Count; i++) { TableNameField tnf = this.OQL.selectedFieldInfo[i]; tnf.Index = tnf.Entity.GetPropertyFieldNameIndex(tnf.Field); fieldInfo[i] = tnf; } foreach (object[] itemValues in this.Values) { for (int m = 0; m < fieldInfo.Length; m++) { //将容器的值赋值给实体的值元素 EntityBase entity = fieldInfo[m].Entity; int index = fieldInfo[m].Index; entity.PropertyValues[index] = itemValues[m]; } yield return(itemValues); } } else { throw new Exception("EntityContainer 错误,执行查询没有返回任何行。"); } }
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 } }
/// <summary> /// 将数据从查询结果容器中映射到实体中 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public IEnumerable <T> Map <T>() where T : EntityBase, new() { #region 旧方法代码,注释 /* * if (this.Values == null) * { * int rowsCount = this.Execute(); * if (rowsCount <= 0) * yield break; * * } * if (this.Values != null && this.fieldNames != null) * { * if (this.Values.Count == 0) * yield break; * * Dictionary<string, int> dictNameIndex = new Dictionary<string, int>(); * T entity = new T(); * string tabeName = entity.TableName; * //查找字段匹配情况 * //entity.PropertyNames 存储的仅仅是查询出来的列名称,由于有连表查询, * //如果要映射到指定的实体,还得检查当前列对应的表名称 * if (this.OQL.sql_fields.Contains("[" + tabeName + "]")) * { * //是连表查询 * for (int i = 0; i < this.fieldNames.Length; i++) * { * for (int j = 0; j < entity.PropertyNames.Length; j++) * { * string cmpString = "[" + tabeName + "].[" + entity.PropertyNames[j] + "]"; * if (this.OQL.sql_fields.Contains(cmpString)) * { * dictNameIndex[this.fieldNames[i]] = i; * break; * } * } * * * } * } * else * { * for (int i = 0; i < this.fieldNames.Length; i++) * { * for (int j = 0; j < entity.PropertyNames.Length; j++) * { * if (this.fieldNames[i] == entity.PropertyNames[j]) * { * dictNameIndex[this.fieldNames[i]] = i; * break; * } * } * } * } * * //没有匹配的,提前结束 * if (dictNameIndex.Count == 0) * yield break; * * int length = entity.PropertyValues.Length; * foreach (object[] itemValues in this.Values) * { * for (int m = 0; m < length; m++) * { * //将容器的值赋值给实体的值元素 * string key = entity.PropertyNames[m]; * if (dictNameIndex.ContainsKey(key)) * entity.PropertyValues[m] = itemValues[dictNameIndex[key]]; * } * yield return entity; * //创建一个新实例 * entity = new T (); * } * } * else * { * throw new Exception("EntityContainer 错误,执行查询没有返回任何行。"); * } */ #endregion if (this.Values == null) { //可能执行本方法之前,并没有调用过 OQL的Select方法指定要查询的实体类属性,需要模拟调用一次。 if (this.OQL.selectedFieldInfo.Count == 0) { int fieldCount = 0; foreach (EntityBase entity in this.OQL.GetAllUsedEntity()) { for (int i = 0; i < entity.PropertyNames.Length; i++) { object value = entity[i];//模拟调用 fieldCount++; } } this.OQL.Select(new object[fieldCount]); } int rowsCount = this.Execute(); if (rowsCount <= 0) { yield break; } } if (this.Values != null && this.fieldNames != null) { if (this.Values.Count == 0) { yield break; } TableNameField[] fieldInfo = new TableNameField[this.OQL.selectedFieldInfo.Count]; //查找字段匹配情况 //entity.PropertyNames 存储的仅仅是查询出来的列名称 for (int i = 0; i < this.OQL.selectedFieldInfo.Count; i++) { TableNameField tnf = this.OQL.selectedFieldInfo[i]; tnf.Index = tnf.Entity.GetPropertyFieldNameIndex(tnf.Field); fieldInfo[i] = tnf; } foreach (object[] itemValues in this.Values) { EntityBase itemEntity = null; Type entityType = typeof(T); for (int m = 0; m < fieldInfo.Length; m++) { //将容器的值赋值给实体的值元素 EntityBase entity = fieldInfo[m].Entity; if (entity.GetType() == entityType) { itemEntity = entity; int index = fieldInfo[m].Index; entity.PropertyValues[index] = itemValues[m]; } } if (itemEntity == null) { throw new Exception("EntityContainer 错误,查询没有包含当前指定的实体类类型,请检查OQL语句。"); } T resultEntity = (T)itemEntity.Clone(); yield return(resultEntity); } } else { throw new Exception("EntityContainer 错误,执行查询没有返回任何行。"); } }
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.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]); } } } else if (oql.EntityMap == PWMIS.Common.EntityMapType.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 db.ExecuteDataReader(sql, CommandType.StoredProcedure, paras); return(si); } else { sql = oql.ToString(); Parameters = oql.Parameters; } if (oql.PageEnable && (!single || oql.PageWithAllRecordCount <= 0)) { switch (db.CurrentDBMSType) { case PWMIS.Common.DBMSType.Access: case PWMIS.Common.DBMSType.SqlServer: case PWMIS.Common.DBMSType.SqlServerCe: //如果含有Order By 子句,则不能使用主键分页 if (oql.haveJoinOpt || sql.IndexOf("order by", StringComparison.OrdinalIgnoreCase) > 0) { 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; default: sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(db.CurrentDBMSType, sql, "", oql.PageSize, oql.PageNumber, oql.PageWithAllRecordCount); break; } } SqlInfo result = new SqlInfo(sql, Parameters); result.CommandType = CommandType.Text; result.TableName = oql.sql_table; return(result); }