public static string GetTableName(Type classType, DbOperateType type) { string strTableName = string.Empty; string strEntityName = string.Empty; strEntityName = classType.FullName; object[] attr = classType.GetCustomAttributes(false); if (attr.Length == 0) { return(strTableName); } object classAttr = attr[0]; if (classAttr is TableAttribute) { TableAttribute tableAttr = classAttr as TableAttribute; strTableName = tableAttr.Name; } if (string.IsNullOrEmpty(strTableName) && (type == DbOperateType.INSERT || type == DbOperateType.UPDATE || type == DbOperateType.DELETE)) { throw new Exception("实体类:" + strEntityName + "的属性配置[Table(name=\"tablename\")]错误或未配置"); } return(strTableName); }
public static string GetPrimaryKey(object attribute, DbOperateType type) { string strPrimary = string.Empty; IdAttribute attr = attribute as IdAttribute; if (type == DbOperateType.INSERT) { switch (attr.Strategy) { case GenerationType.INDENTITY: break; case GenerationType.SEQUENCE: strPrimary = System.Guid.NewGuid().ToString(); break; case GenerationType.TABLE: break; } } else { strPrimary = attr.Name; } return(strPrimary); }
//public static string GetTableName(Type classType, DbOperateType type) //{ // //string strTableName = string.Empty; // //string strEntityName = string.Empty; // //strEntityName = classType.FullName; // //object[] attr = classType.GetCustomAttributes(false); // //if (attr.Length == 0) return strTableName; // //foreach (object classAttr in attr) // //{ // // if (classAttr is TableAttribute) // // { // // TableAttribute tableAttr = classAttr as TableAttribute; // // strTableName = tableAttr.Name; // // } // //} // TableAttribute tableAttr = GetTableAttribute(classType, type); // //if (string.IsNullOrEmpty(strTableName) && (type == DbOperateType.INSERT || type == DbOperateType.UPDATE || type == DbOperateType.DELETE)) // //{ // // throw new Exception("实体类:" + strEntityName + "的属性配置[Table(name=\"tablename\")]错误或未配置"); // //} // return tableAttr.Name; //} public static TableAttribute GetTableAttribute(Type classType, DbOperateType type) { TableAttribute tableAttr = null; //string strTableName = string.Empty; string strEntityName = string.Empty; strEntityName = classType.FullName; object[] attr = classType.GetCustomAttributes(false); if (attr.Length == 0) { return(null); } foreach (object classAttr in attr) { if (classAttr is TableAttribute) { tableAttr = classAttr as TableAttribute; //atrTableName = tableAttr.Name; } } if (tableAttr == null && (type == DbOperateType.INSERT || type == DbOperateType.UPDATE || type == DbOperateType.DELETE)) { throw new Exception("实体类:" + strEntityName + "的属性配置[Table(name=\"tablename\")]错误或未配置"); } return(tableAttr); }
public static bool IsCaseColumn(PropertyInfo property, DbOperateType dbOperateType) { bool isBreak = false; object[] propertyAttrs = property.GetCustomAttributes(false); foreach (object propertyAttr in propertyAttrs) { if (EntityHelper.IsCaseColumn(propertyAttr, DbOperateType.SELECT)) { isBreak = true; break; } } return(isBreak); }
public static bool IsCaseColumn(object attribute, DbOperateType dbOperateType) { if (attribute is ColumnAttribute) { ColumnAttribute columnAttr = attribute as ColumnAttribute; if (columnAttr.Ignore) { return(true); } if (!columnAttr.IsInsert && dbOperateType == DbOperateType.INSERT) { return(true); } if (!columnAttr.IsUpdate && dbOperateType == DbOperateType.UPDATE) { return(true); } } return(false); }
public static TableInfo GetTableInfo(object entity, DbOperateType dbOpType, PropertyInfo[] properties) { bool breakForeach = false; string strPrimaryKey = string.Empty; TableInfo tableInfo = new TableInfo(); Type type = entity.GetType(); tableInfo.TableName = GetTableName(type, dbOpType); if (dbOpType == DbOperateType.COUNT) { return(tableInfo); } foreach (PropertyInfo property in properties) { object propvalue = null; string columnName = string.Empty; string propName = columnName = property.Name; propvalue = ReflectionHelper.GetPropertyValue(entity, property); object[] propertyAttrs = property.GetCustomAttributes(false); for (int i = 0; i < propertyAttrs.Length; i++) { object propertyAttr = propertyAttrs[i]; if (EntityHelper.IsCaseColumn(propertyAttr, dbOpType)) { breakForeach = true; break; } string tempVal = GetColumnName(propertyAttr); columnName = tempVal == string.Empty ? propName : tempVal; if (propertyAttr is IdAttribute) { if (dbOpType == DbOperateType.INSERT || dbOpType == DbOperateType.DELETE) { IdAttribute idAttr = propertyAttr as IdAttribute; tableInfo.Strategy = idAttr.Strategy; if (CommonUtils.IsNullOrEmpty(propvalue)) { strPrimaryKey = EntityHelper.GetPrimaryKey(propertyAttr, dbOpType); if (!string.IsNullOrEmpty(strPrimaryKey)) { propvalue = strPrimaryKey; } } } tableInfo.Id.Key = columnName; tableInfo.Id.Value = propvalue; tableInfo.PropToColumn.Put(propName, columnName); breakForeach = true; } } if (breakForeach && dbOpType == DbOperateType.DELETE) { break; } if (breakForeach) { breakForeach = false; continue; } tableInfo.Columns.Put(columnName, propvalue); tableInfo.PropToColumn.Put(propName, columnName); } return(tableInfo); }
public static TableInfo GetTableInfo(object entity, DbOperateType dbOpType) { // Type type = entity.GetType(); bool breakForeach = false; string strPrimaryKey = string.Empty; TableInfo tableInfo = new TableInfo(); PropertyInfo[] properties; if (!maps.ContainsKey(type)) { properties = type.GetProperties(); maps[type] = properties; for (int i = 0; i < properties.Length; i++) { CustomAttributes[properties[i]] = properties[i].GetCustomAttributes(false); } } TableAttribute tableAttr = GetTableAttribute(type, dbOpType); tableInfo.TableName = tableAttr.Name; tableInfo.NoAutomaticKey = tableAttr.NoAutomaticKey; //tableInfo.TableName = GetTableName(type, dbOpType); if (dbOpType == DbOperateType.COUNT) { return(tableInfo); } properties = maps[type]; // 遍历所有字段 foreach (PropertyInfo property in properties) { object propvalue = null; string columnName = string.Empty; string propName = columnName = property.Name; // 获得字段值 propvalue = ReflectionHelper.GetPropertyValue(entity, property); // 获得字段属性值列表 object[] propertyAttrs = CustomAttributes[property];// property.GetCustomAttributes(false); // 遍历字段属性列表 for (int i = 0; i < propertyAttrs.Length; i++) { // 当前字段属性 object propertyAttr = propertyAttrs[i]; // 根据操作类型确定是否应该跳过该字段 if (EntityHelper.IsCaseColumn(propertyAttr, dbOpType)) { breakForeach = true; break; } // 暂存字段名字 string tempVal = GetColumnName(propertyAttr); // 如果字段名为空则使用属性名, 否则使用字段名 columnName = tempVal == string.Empty ? propName : tempVal; // 关键字段 if (propertyAttr is IdAttribute) { // 插入或删除记录需要根据主键的值来确定是否收集该字段信息 if (dbOpType == DbOperateType.INSERT || dbOpType == DbOperateType.DELETE) { // 转换为关键字段类型 IdAttribute idAttr = propertyAttr as IdAttribute; // 主键值的填充方式(自增,GUID,设置) tableInfo.Strategy = idAttr.Strategy; // 如果字段值为空 if (CommonUtils.IsNullOrEmpty(propvalue)) { // 获得主键名字 strPrimaryKey = EntityHelper.GetPrimaryKey(propertyAttr, dbOpType); // 主键名字非空 if (string.IsNullOrEmpty(strPrimaryKey) == false) { // 主键名作为主键值 propvalue = strPrimaryKey; } } // 记录主键信息 tableInfo.Columns.Put(columnName, propvalue); tableInfo.PropToColumn.Put(propName, columnName); tableInfo.ColumnToProp.Put(columnName, propName); } else if (dbOpType != DbOperateType.UPDATE) {// 更新语句在任何情况下都不能更新关键字段 tableInfo.Columns.Put(columnName, propvalue); tableInfo.PropToColumn.Put(propName, columnName); tableInfo.ColumnToProp.Put(columnName, propName); } tableInfo.Id.Put(columnName, propvalue); breakForeach = true; } } // 删除记录只需要收集关键字段就行了 if (breakForeach && dbOpType == DbOperateType.DELETE) { break; } // if (breakForeach) { breakForeach = false; continue; } // 非关键字段 tableInfo.Columns.Put(columnName, propvalue); tableInfo.PropToColumn.Put(propName, columnName); tableInfo.ColumnToProp.Put(columnName, propName); } /*if (dbOpType == DbOperateType.UPDATE) * { * tableInfo.Columns.Put(tableInfo.Id.Key, tableInfo.Id.Value); * }*/ return(tableInfo); }
public static bool IsCaseColumn(object attribute, DbOperateType dbOperateType) { if (attribute is ColumnAttribute) { ColumnAttribute columnAttr = attribute as ColumnAttribute; if (columnAttr.Ignore) { return true; } if (!columnAttr.IsInsert && dbOperateType == DbOperateType.INSERT) { return true; } if (!columnAttr.IsUpdate && dbOperateType == DbOperateType.UPDATE) { return true; } } return false; }
public static TableInfo GetTableInfo(object entity, DbOperateType dbOpType) { bool breakForeach = false; string strPrimaryKey = string.Empty; TableInfo tableInfo = new TableInfo(); Type type = entity.GetType(); tableInfo.TableName = GetTableName(type); if (dbOpType == DbOperateType.COUNT) { return tableInfo; } PropertyInfo[] properties = ReflectionUtils.GetProperties(type); foreach (PropertyInfo property in properties) { object propvalue = null; string columnName = string.Empty; string propName = columnName = property.Name; propvalue = ReflectionUtils.GetPropertyValue(entity, property); object[] propertyAttrs = property.GetCustomAttributes(false); for (int i = 0; i < propertyAttrs.Length; i++) { object propertyAttr = propertyAttrs[i]; if (DbEntityUtils.IsCaseColumn(propertyAttr, dbOpType)) { breakForeach = true;break; } string tempVal = GetColumnName(propertyAttr); columnName = tempVal == string.Empty ? propName : tempVal; if (propertyAttr is IdAttribute) { if (dbOpType == DbOperateType.INSERT || dbOpType == DbOperateType.DELETE) { IdAttribute idAttr = propertyAttr as IdAttribute; tableInfo.Strategy = idAttr.Strategy; if (CommonUtils.IsNullOrEmpty(propvalue)) { strPrimaryKey = DbEntityUtils.GetPrimaryKey(propertyAttr, dbOpType); if (!string.IsNullOrEmpty(strPrimaryKey)) propvalue = strPrimaryKey; } } tableInfo.Id.Key = columnName; tableInfo.Id.Value = propvalue; tableInfo.PropToColumn.Put(propName, columnName); breakForeach = true; } } if (breakForeach && dbOpType == DbOperateType.DELETE) break; if (breakForeach) { breakForeach = false; continue; } tableInfo.Columns.Put(columnName, propvalue); tableInfo.PropToColumn.Put(propName, columnName); } return tableInfo; }
public static string GetPrimaryKey(object attribute, DbOperateType type) { string strPrimary = string.Empty; IdAttribute attr = attribute as IdAttribute; if (type == DbOperateType.INSERT) { switch (attr.Strategy) { case GenerationType.INDENTITY: break; case GenerationType.SEQUENCE: strPrimary = System.Guid.NewGuid().ToString(); break; case GenerationType.TABLE: break; } } else { strPrimary = attr.Name; } return strPrimary; }
private void OperateDb(DbOperateType operateType, int index) { var barFieldName = TbBarFieldName.Text.Trim(); if (barFieldName.Equals("")) { InfoBox.ErrorMsg("条码字段名不能为空,请首先配置条形码确定字段内容"); } else { if (_barInfoList != null) { if (operateType == DbOperateType.Insert) { if (_barInfoList.Any(tmp => tmp.BarFieldName.Equals(barFieldName))) { InfoBox.InfoMsg("条码字段名已存在"); return; } } else { var tmpIndex = 0; foreach (var tmp in _barInfoList) { if (tmp.BarFieldName.Equals(barFieldName) && tmpIndex != index) { InfoBox.InfoMsg("条码字段名已存在"); return; } tmpIndex++; } } var fieldType = BarFieldType.AutoIncrement; var fieldValue = ""; var iTmp = 0; if (int.TryParse(TbBarValueLen.Text, out iTmp) == false) { InfoBox.ErrorMsg("数值长度必须为数字"); return; } if (iTmp <= 0) { InfoBox.ErrorMsg("数值长度必须大于0"); return; } switch (CbBar.SelectedIndex) { case 0: fieldType = BarFieldType.AutoIncrement; fieldValue = "0".PadLeft(iTmp, '0'); break; case 1: fieldType = BarFieldType.Constant; fieldValue = TbBarFieldContent.Text.Trim(); break; case 2: fieldType = BarFieldType.Month; fieldValue = ""; break; case 3: fieldType = BarFieldType.Day; fieldValue = ""; break; case 4: fieldType = BarFieldType.Year; fieldValue = ""; break; default: break; } switch (operateType) { case DbOperateType.Insert: if (ChkEditable.IsChecked != null && !DbBar.AddBarInfo(_cable.CableName, barFieldName, fieldType, fieldValue, (bool)ChkEditable.IsChecked)) { InfoBox.ErrorMsg("添加条码字段失败"); return; } break; case DbOperateType.Update: var id = _barInfoList[index].Id; if (ChkEditable.IsChecked != null && !DbBar.UpdateBarInfo(id, _cable.CableName, barFieldName, fieldType, fieldValue, (bool)ChkEditable.IsChecked)) { InfoBox.ErrorMsg("修改条码字段失败"); return; } break; default: break; } if (!CpldControl.Bartend.BartendControl.LoadBartendDb(_cable.CableName)) { InfoBox.ErrorMsg("读取Bartend数据库文件失败"); return; } } } LoadBar(); }