private static void Generate(OracleTypeObject type, TextWriter writer) { var owner = type.FullyQualifiedName.NormalizedOwner.Trim('"'); var name = type.FullyQualifiedName.NormalizedName.Trim('"'); writer.WriteLine($"[OracleCustomTypeMapping(\"{owner}.{name}\")]"); writer.WriteLine($"public class {owner}_{name} : CustomTypeBase<{owner}_{name}>"); writer.WriteLine("{"); foreach (var attribute in type.Attributes) { var attributeName = attribute.Name.Trim('"'); writer.WriteLine($" [OracleObjectMapping(\"{attributeName}\")]"); writer.WriteLine($" public {OracleToNetTypeMapper.MapOracleTypeToNetType(attribute.DataType.FullyQualifiedName).Name} {attributeName};"); } writer.WriteLine(); writer.WriteLine(" public override void FromCustomObject(OracleConnection connection, IntPtr pointerUdt)"); writer.WriteLine(" {"); foreach (var attribute in type.Attributes) { var attributeName = attribute.Name.Trim('"'); writer.WriteLine($" OracleUdt.SetValue(connection, pointerUdt, \"{attributeName}\", {attributeName});"); } writer.WriteLine(" }"); writer.WriteLine(); writer.WriteLine(" public override void ToCustomObject(OracleConnection connection, IntPtr pointerUdt)"); writer.WriteLine(" {"); foreach (var attribute in type.Attributes) { var attributeName = attribute.Name.Trim('"'); writer.WriteLine($" {attributeName} = ({OracleToNetTypeMapper.MapOracleTypeToNetType(attribute.DataType.FullyQualifiedName).Name})OracleUdt.GetValue(connection, pointerUdt, \"{attributeName}\");"); } writer.WriteLine(" }"); writer.WriteLine("}"); writer.WriteLine(); }
private static OracleSchemaObject MapSchemaType(IDataRecord reader) { var owner = QualifyStringObject(reader["OWNER"]); OracleTypeBase schemaType; var typeType = (string)reader["TYPECODE"]; switch (typeType) { case OracleTypeBase.TypeCodeXml: schemaType = new OracleTypeObject().WithTypeCode(OracleTypeBase.TypeCodeXml); break; case OracleTypeBase.TypeCodeAnyData: schemaType = new OracleTypeObject().WithTypeCode(OracleTypeBase.TypeCodeAnyData); break; case OracleTypeBase.TypeCodeObject: schemaType = new OracleTypeObject(); break; case OracleTypeBase.TypeCodeCollection: schemaType = new OracleTypeCollection(); break; default: if (!String.IsNullOrEmpty(owner)) { throw new NotSupportedException($"Type '{typeType}' is not supported. "); } schemaType = new OracleTypeObject().WithTypeCode(typeType); break; } schemaType.FullyQualifiedName = OracleObjectIdentifier.Create(owner, QualifyStringObject(reader["TYPE_NAME"])); return(schemaType); }
private static void CreateOracleObjectType(ModuleBuilder customTypeModuleBuilder, OracleTypeObject objectType, IDictionary <string, Type> customTypes) { var fullyQualifiedObjectTypeName = objectType.FullyQualifiedName.ToString().Replace("\"", null); var customTypeClassName = $"{DynamicAssemblyNameBase}.ObjectTypes.{MakeValidMemberName(fullyQualifiedObjectTypeName)}"; var customTypeBuilder = customTypeModuleBuilder.DefineType(customTypeClassName, TypeAttributes.Public | TypeAttributes.Class); customTypeBuilder.SetParent(typeof(OracleCustomTypeBase <>).MakeGenericType(customTypeBuilder)); AddOracleCustomTypeMappingAttribute(customTypeBuilder, fullyQualifiedObjectTypeName); AddConstructor(customTypeBuilder, typeof(object).GetConstructor(Type.EmptyTypes)); ImplementAbstractStringValueProperty(customTypeBuilder, "DataTypeName", fullyQualifiedObjectTypeName, MethodAttributes.Public); var fields = new Dictionary <string, FieldInfo>(); foreach (var objectAttribute in objectType.Attributes) { var targetType = OracleToNetTypeMapper.MapOracleTypeToNetType(objectAttribute.DataType.FullyQualifiedName); var fieldName = MakeValidMemberName(objectAttribute.Name); var fieldBuilder = customTypeBuilder.DefineField(fieldName, targetType, FieldAttributes.Public); var attributeType = typeof(OracleObjectMappingAttribute); var attribute = new CustomAttributeBuilder(attributeType.GetConstructor(new[] { typeof(string) }), new[] { objectAttribute.Name.Replace("\"", null) }); fieldBuilder.SetCustomAttribute(attribute); fields.Add(fieldName, fieldBuilder); } var ilGenerator = BuildOracleCustomTypeInterfaceMethod(customTypeBuilder, "FromCustomObject"); foreach (var field in fields) { ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.Ldarg_2); ilGenerator.Emit(OpCodes.Ldstr, field.Key); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldfld, field.Value); if (field.Value.FieldType.IsValueType) { ilGenerator.Emit(OpCodes.Box, field.Value.FieldType); } ilGenerator.Emit(OpCodes.Call, typeof(OracleUdt).GetMethod("SetValue", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(OracleConnection), typeof(IntPtr), typeof(string), typeof(object) }, null)); } ilGenerator.Emit(OpCodes.Ret); ilGenerator = BuildOracleCustomTypeInterfaceMethod(customTypeBuilder, "ToCustomObject");; foreach (var field in fields) { ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.Ldarg_2); ilGenerator.Emit(OpCodes.Ldstr, field.Key); ilGenerator.Emit(OpCodes.Call, typeof(OracleUdt).GetMethod("GetValue", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(OracleConnection), typeof(IntPtr), typeof(string) }, null)); if (field.Value.FieldType.IsValueType) { ilGenerator.Emit(OpCodes.Unbox_Any, field.Value.FieldType); } ilGenerator.Emit(OpCodes.Stfld, field.Value); } ilGenerator.Emit(OpCodes.Ret); var customType = customTypeBuilder.CreateType(); customTypes.Add(fullyQualifiedObjectTypeName, customType); /*var schemaName = objectType.FullyQualifiedName.Owner.Trim('"'); * var typeName = objectType.FullyQualifiedName.Name.Trim('"'); * var mappingTableKey = String.Format("schemaName='{0}' typeName='{1}'", schemaName, typeName); * var mappingTableValue = * new NameValueCollection * { * { "schemaName", schemaName }, * { "typeName", typeName }, * { "factoryName", customType.FullName } * }; * * mappingTable.Add(mappingTableKey, mappingTableValue);*/ }