private static bool IsUDT(KnownDibixTypes dbx, Type parameterType) { if (dbx.Dibix_StructuredType.IsAssignableFrom(parameterType)) { return(true); } return(false); }
private static void GenerateDatabaseAccessHandlerFacade(KnownDibixTypes dbx, CodeCompileUnit ccu, Type databaseAccessorType) { //Console.WriteLine("" + databaseAccessorType.FullName); CodeNamespace ns = EnsureNamespace(ccu, databaseAccessorType); CodeTypeDeclaration typedecl = BuildDatabaseAccessHandlerFacadeType(dbx, databaseAccessorType); ns.Types.Add(typedecl); }
internal static KnownDibixTypes LoadKnownDibixTypes(Assembly asm_Dibix, AssemblyResolver assemblyResolver) { KnownDibixTypes dbx = new KnownDibixTypes(); dbx.Dibix_DatabaseAccessorAttribute = asm_Dibix.GetType("Dibix.DatabaseAccessorAttribute", true); dbx.Dibix_StructuredTypeAttribute = asm_Dibix.GetType("Dibix.StructuredTypeAttribute", true); dbx.Dibix_StructuredType = asm_Dibix.GetType("Dibix.StructuredType", true); return(dbx); }
private static CodeTypeDeclaration BuildDatabaseAccessHandlerFacadeType(KnownDibixTypes dbx, Type databaseAccessorType) { string typeName = databaseAccessorType.Name + "DatabaseAccessHandlerFacade"; // CommandHandlerFacade CodeTypeDeclaration typedecl = new CodeTypeDeclaration() { Name = typeName, IsClass = true, //Attributes = MemberAttributes.Final | MemberAttributes.Assembly,// dont use it for now! MemberAttributes.Public }; typedecl.TypeAttributes = (typedecl.TypeAttributes & ~TypeAttributes.VisibilityMask) | TypeAttributes.NestedAssembly; //typedecl.Comments.Add(new CodeCommentStatement("Generated (at:" + DateTime.UtcNow + ")", true)); typedecl.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(System.ComponentModel.DescriptionAttribute)), new CodeAttributeArgument(new_CodePrimitiveExpression("Generated (at:" + DateTime.UtcNow + ")")))); foreach (MethodInfo accessMethod in databaseAccessorType.GetMethods(BindingFlags.Public | BindingFlags.Static)) { ParameterInfo[] acessMethodParameters = accessMethod.GetParameters(); if (acessMethodParameters.Length == 0) { throw new StoreLakeSdkException("Access method signature not correct. Method:" + accessMethod.Name + ", Type:" + databaseAccessorType.FullName); } // first parameter must be of type 'IDatabaseAccessorFactory' if (acessMethodParameters[0].ParameterType.Name != "IDatabaseAccessorFactory") { throw new StoreLakeSdkException("Access method parameter signature not correct. Parameter:" + acessMethodParameters[0].Name + ", Method:" + accessMethod.Name + ", Type:" + databaseAccessorType.FullName); } CodeMemberMethod code_method = new CodeMemberMethod() { Name = accessMethod.Name, Attributes = MemberAttributes.Public }; typedecl.Members.Add(code_method); code_method.ReturnType = new CodeTypeReference(accessMethod.ReturnType); code_method.Statements.Add(new CodeThrowExceptionStatement(new CodeObjectCreateExpression(new CodeTypeReference(typeof(NotImplementedException))))); code_method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(DataSet), "db")); if (acessMethodParameters.Length > 1) { // second parameter can be 'long actioncontextid' for (int parameter_index = 1; parameter_index < acessMethodParameters.Length; parameter_index++) { ParameterInfo accessMethodParameter = acessMethodParameters[parameter_index]; Type parameterType = accessMethodParameter.ParameterType.IsByRef ? accessMethodParameter.ParameterType.GetElementType() : accessMethodParameter.ParameterType; CodeTypeReference parameterTypeRef; if (IsUDT(dbx, parameterType)) { //parameterType = typeof(IEnumerable<Microsoft.SqlServer.Server.SqlDataRecord>); parameterTypeRef = CreateUdtParameterType(parameterType); } else { string accessParameterTypeName = TypeNameAsText(parameterType); parameterTypeRef = new CodeTypeReference(parameterType); } CodeParameterDeclarationExpression prm_decl = new CodeParameterDeclarationExpression(parameterTypeRef, accessMethodParameter.Name); code_method.Parameters.Add(prm_decl); prm_decl.Direction = accessMethodParameter.IsOut ? FieldDirection.Out : (accessMethodParameter.ParameterType.IsByRef) ? FieldDirection.Ref : FieldDirection.In; if (accessMethodParameter.IsOut) { } } } } return(typedecl); }
internal static void GenerateAccessors(KnownDibixTypes dbx, AssemblyResolver assemblyResolver, DacPacRegistration dacpac, bool doGenerate, CompilerParameters comparam, CodeCompileUnit ccu, string inputdir) { string dacpacDllFileName = Path.GetFileName(dacpac.DacPacAssemblyFileName); string dacpacDllFullFileName = Path.Combine(inputdir, dacpacDllFileName); Console.WriteLine("Load '" + dacpacDllFullFileName + "'..."); if (!File.Exists(dacpacDllFullFileName)) { throw new StoreLakeSdkException("File could not be found:" + dacpacDllFullFileName); } Assembly asm = assemblyResolver.ResolveAssembyByLocation(dacpacDllFullFileName); if (doGenerate) { CollectIndirectAssemblies(assemblyResolver, dacpac, (string asm_location) => { AddReferencedAssemblies(assemblyResolver, comparam, asm_location); }); foreach (AssemblyName ref_asm_name in asm.GetReferencedAssemblies()) { if (ref_asm_name.FullName.StartsWith("mscorlib", StringComparison.OrdinalIgnoreCase) || ref_asm_name.FullName.StartsWith("System.", StringComparison.OrdinalIgnoreCase) || ref_asm_name.FullName.StartsWith("System,", StringComparison.OrdinalIgnoreCase) || ref_asm_name.FullName.StartsWith("Newtonsoft.", StringComparison.OrdinalIgnoreCase) ) { // ignore } else { Console.WriteLine(ref_asm_name.FullName); if (dacpac.referenced_assemblies.TryGetValue(ref_asm_name.FullName, out string ref_asm_location)) { } else { Assembly ref_asm = assemblyResolver.ResolveAssembyByName(ref_asm_name); ref_asm_location = ref_asm.Location; dacpac.referenced_assemblies.Add(ref_asm.GetName().FullName, ref_asm.Location); } AddReferencedAssemblies(assemblyResolver, comparam, ref_asm_location); } } AddReferencedAssemblies(assemblyResolver, comparam, asm.Location); dacpac.referenced_assemblies.Add(asm.GetName().FullName, asm.Location); ; AddReferencedAssemblies(assemblyResolver, comparam, assemblyResolver.CacheType(typeof(System.Xml.Linq.XElement)).Location); // System.Xml.Linq dbx.Dibix_DatabaseAccessorAttribute.ToString().GetHashCode(); Type databaseAccessorAttributeType = dbx.Dibix_DatabaseAccessorAttribute; foreach (Type type in asm.GetTypes()) { IList <CustomAttributeData> customAttributes = type.GetCustomAttributesData(); //Console.WriteLine("Type:" + type.FullName + " A:" + customAttributes.Count); //if (customAttributes.Count > 0 && type.FullName.EndsWith("Set")) //{ // Console.WriteLine(" A:" + customAttributes.Count); //} bool type_IsDefined__databaseAccessorAttributeType = false; bool type_IsDefined__dbx_Dibix_StructuredTypeAttribute = false; foreach (CustomAttributeData customAttribute in customAttributes) { if (customAttribute.AttributeType == databaseAccessorAttributeType) { type_IsDefined__databaseAccessorAttributeType = true; } if (customAttribute.AttributeType == dbx.Dibix_StructuredTypeAttribute) { type_IsDefined__dbx_Dibix_StructuredTypeAttribute = true; } } //if (type.IsDefined(databaseAccessorAttributeType)) //{ // GenerateDatabaseAccessHandlerFacade(dbx, ccu, type); //} //else if (type.IsDefined(dbx.Dibix_StructuredTypeAttribute)) //{ // GenerateStructureTypeRow(ccu, type); //} if (type_IsDefined__databaseAccessorAttributeType) { GenerateDatabaseAccessHandlerFacade(dbx, ccu, type); } else if (type_IsDefined__dbx_Dibix_StructuredTypeAttribute) { GenerateStructureTypeRow(ccu, type); } } } // doGenerate }