private AttributeMap GetAttributeMap(XmlReader reader, ClassMap clsMap, TableMap tblMap, int colIndex) { string attrName = reader.GetAttribute("name"); //类属性名 string attrColumn = reader.GetAttribute("column"); //属性对应列名 string attrKey = reader.GetAttribute("key"); //键类型 string attrReference = reader.GetAttribute("reference"); //引用父类属性 string dataType = reader.GetAttribute("type"); //数据类型 string autoIncrement = reader.GetAttribute("increment"); //是否自动增加 string timestamp = reader.GetAttribute("timestamp"); //时间戳 string autoValue = reader.GetAttribute("auto"); //是否自动值 AttributeMap attrMap = null; if (attrName != null) { ColumnMap colMap = null; //生成一个AttributeMap attrMap = new AttributeMap(attrName); if (attrColumn != null) { colMap = new ColumnMap(attrColumn, tblMap); if (attrKey != null) { //设置键类型 if (attrKey.ToUpper() == "PRIMARY") { colMap.KeyType = ColumnKeyTypes.PrimaryKey; } else if (attrKey.ToUpper() == "FOREIGN") { colMap.KeyType = ColumnKeyTypes.ForeignKey; } } if (dataType == null) { string s = "不能确定列" + clsMap.GetFullyQualifiedName(colMap.Name) + "的数据类型!"; throw new PlException(s, ErrorTypes.XmlError); } //确定数据列的数据类型 //Xml中所有数据类型为OleDbType中的枚举类型 //将其映射到 DbType switch (dataType.ToLower()) { case "boolean": colMap.Type = DbType.Boolean; attrMap.SqlValueStringType = clsMap.RelationalDatabase.SqlValueType(DbType.Boolean); break; //Map to Int64 case "bigint": colMap.Type = DbType.Int64; break; //Map to Binary case "binary": colMap.Type = DbType.Binary; attrMap.SqlValueStringType = SqlValueTypes.NotSupport; break; //Map to Decimal case "currency": colMap.Type = DbType.Currency; break; //Map to DateTime case "date": //对Access的数据库做特殊处理 if (clsMap.RelationalDatabase.Vendor == MsAccess.VENDOR_NAME) { colMap.Type = DbType.Object; attrMap.SqlValueStringType = SqlValueTypes.AccessDateString; } else if (clsMap.RelationalDatabase.Vendor == DatabaseVendor.Oracle) //如果是oracle的话,则会对日期使用to_date转换 { colMap.Type = DbType.DateTime; attrMap.SqlValueStringType = SqlValueTypes.OracleDate; } else { colMap.Type = DbType.DateTime; attrMap.SqlValueStringType = SqlValueTypes.SimpleQuotesString; } break; //Mapt to Date case "dbdate": colMap.Type = DbType.Date; attrMap.SqlValueStringType = SqlValueTypes.SimpleQuotesString; break; //Map to Decimal case "decimal": colMap.Type = DbType.Decimal; break; //Map to Double case "double": colMap.Type = DbType.Double; break; //Map to Guid case "guid": colMap.Type = DbType.Guid; attrMap.SqlValueStringType = SqlValueTypes.SimpleQuotesString; break; //Map to Object case "object": colMap.Type = DbType.Object; attrMap.SqlValueStringType = SqlValueTypes.NotSupport; break; //Map to Single case "single": colMap.Type = DbType.Single; break; //Map to Int16 case "smallint": colMap.Type = DbType.Int16; break; //Map to SBtype case "tinyint": colMap.Type = DbType.Byte; break; //Map to Int32 case "integer": colMap.Type = DbType.Int32; break; //Map to String case "varchar": case "string": colMap.Type = DbType.String; attrMap.SqlValueStringType = SqlValueTypes.String; break; default: throw new PlException("目前仍不支持的数据类型!", ErrorTypes.XmlError); } //该列不是自动增加 if (autoIncrement != null && autoIncrement.ToLower() == "true") { colMap.IsAutoValue = true; tblMap.AutoIdentityIndex = colIndex; clsMap.AutoIdentityAttribute = attrName; } //是不是timestamp if (timestamp != null && timestamp.ToLower() == "true") { tblMap.TimestampColumn = attrColumn; if (clsMap.RelationalDatabase.Vendor == DatabaseVendor.MySql) { tblMap.TimestampParameter = "?" + tblMap.TimestampColumn; } else { tblMap.TimestampParameter = "@" + tblMap.TimestampColumn; } //tblMap.TimestampParameter = "@" + tblMap.TimestampColumn ; clsMap.TimestampAttribute = attrMap; colMap.IsAutoValue = true; } //是不是自动值 if (autoValue != null) { colMap.IsAutoValue = true; } } // end of if (column!=null) attrMap.Column = colMap; if ((attrReference != null) && (clsMap.SuperClass != null)) { //若该属性来自父类,则它的Reference属性指向父类的AttributeMap AttributeMap r = clsMap.SuperClass.GetAttributeMap(attrReference, true); if (r != null) { attrMap.Reference = r; } } } return(attrMap); }
//Constructor public ColumnMap(string name, TableMap table) { this.m_Name = name; this._TableMap = table; }
//从Xml Node结点(Name=class)得到相应ClassMap private ClassMap GetClassMap(XmlNodeReader node) { string className = node.GetAttribute("name"); //类名 string tableName = node.GetAttribute("table"); //表名 string databaseName = node.GetAttribute("database"); //数据库名 string superClassName = node.GetAttribute("superclass"); //父类名 //string timeStamp = node.GetAttribute ("timeStamp"); //时间戳 string IsSaveToMemory = node.GetAttribute("IsSaveToMemory"); if (IsSaveToMemory == null) { IsSaveToMemory = "false"; } int c = 0; string err; ClassMap clsMap = null; if ((className != null) && (databaseName != null) && (tableName != null)) { RelationalDatabase r = (RelationalDatabase)this.DatabasePool[databaseName]; if (r == null) { err = "名为:" + databaseName + "的数据库映射信息未找到!"; throw new PlException(err, ErrorTypes.XmlError); } clsMap = new ClassMap(className, r); //从XML读取是否保存到内存值 clsMap.IsSaveToMemory = IsSaveToMemory.ToLower() == "true"?true:false; if (superClassName != null) { clsMap.SuperClass = (ClassMap)classMaps[superClassName]; if (clsMap.SuperClass == null) { err = "在superclass中引用 " + superClassName + "前,必须对该类映射!"; throw new PlException(err, ErrorTypes.XmlError); } clsMap.SelfClassAttributes = new ArrayList(); } else { //如果该类没有父类,则把SelfClassAttributes指向AttributesMapToTable clsMap.SelfClassAttributes = clsMap.AttributesMapToTable; } //生成一个DatabaseMap对象,并加如databaseMaps //若它已存在则直接引用 if (!databaseMaps.ContainsKey(databaseName)) { err = "在映射类:" + className + "时,名为:" + databaseName + "的数据库映射信息找不到!"; throw new PlException(err, ErrorTypes.XmlError); } // TableMap tblMap = new TableMap(tableName, (DatabaseMap)databaseMaps[databaseName]); int clsDep = node.Depth; while (node.Read() && node.Depth > clsDep) { if ((node.NodeType == XmlNodeType.Element) && (node.Name == "attribute")) { //若该Node是Element类型能 且 它的名字是attribute AttributeMap attrMap = GetAttributeMap(node, clsMap, tblMap, c); if (attrMap != null) { clsMap.AddAttributeMap(attrMap); } c++; } } } else { err = "ClassMap 缺少className,databaseName,tableName 这些必要属性!"; throw new PlException(err, ErrorTypes.XmlError); } //缺乏主键 if (clsMap.Table.PrimaryKeyIndex < 0) { err = "表 " + clsMap.Table.Name + " 未定义主键!"; throw new PlException(err, ErrorTypes.XmlError); } return(clsMap); }