|
| 1 | +// Copyright 2017 the original author or authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using Microsoft.EntityFrameworkCore; |
| 16 | +using Microsoft.Extensions.Configuration; |
| 17 | +using Steeltoe.CloudFoundry.Connector.EFCore; |
| 18 | +using Steeltoe.CloudFoundry.Connector.Services; |
| 19 | +using System; |
| 20 | +using System.Reflection; |
| 21 | + |
| 22 | +namespace Steeltoe.CloudFoundry.Connector.Oracle.EFCore |
| 23 | +{ |
| 24 | + public static class OracleDbContextOptionsExtensions |
| 25 | + { |
| 26 | + public static DbContextOptionsBuilder UseOracle(this DbContextOptionsBuilder optionsBuilder, IConfiguration config, object oracleOptionsAction = null) |
| 27 | + { |
| 28 | + if (optionsBuilder == null) |
| 29 | + { |
| 30 | + throw new ArgumentNullException(nameof(optionsBuilder)); |
| 31 | + } |
| 32 | + |
| 33 | + if (config == null) |
| 34 | + { |
| 35 | + throw new ArgumentNullException(nameof(config)); |
| 36 | + } |
| 37 | + |
| 38 | + var connection = GetConnection(config); |
| 39 | + |
| 40 | + return DoUseOracle(optionsBuilder, connection, oracleOptionsAction); |
| 41 | + } |
| 42 | + |
| 43 | + public static DbContextOptionsBuilder UseOracle(this DbContextOptionsBuilder optionsBuilder, IConfiguration config, string serviceName, object oracleOptionsAction = null) |
| 44 | + { |
| 45 | + if (optionsBuilder == null) |
| 46 | + { |
| 47 | + throw new ArgumentNullException(nameof(optionsBuilder)); |
| 48 | + } |
| 49 | + |
| 50 | + if (config == null) |
| 51 | + { |
| 52 | + throw new ArgumentNullException(nameof(config)); |
| 53 | + } |
| 54 | + |
| 55 | + if (string.IsNullOrEmpty(serviceName)) |
| 56 | + { |
| 57 | + throw new ArgumentException(nameof(serviceName)); |
| 58 | + } |
| 59 | + |
| 60 | + var connection = GetConnection(config, serviceName); |
| 61 | + |
| 62 | + return DoUseOracle(optionsBuilder, connection, oracleOptionsAction); |
| 63 | + } |
| 64 | + |
| 65 | + public static DbContextOptionsBuilder<TContext> UseOracle<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder, IConfiguration config, object oracleOptionsAction = null) |
| 66 | + where TContext : DbContext |
| 67 | + { |
| 68 | + if (optionsBuilder == null) |
| 69 | + { |
| 70 | + throw new ArgumentNullException(nameof(optionsBuilder)); |
| 71 | + } |
| 72 | + |
| 73 | + if (config == null) |
| 74 | + { |
| 75 | + throw new ArgumentNullException(nameof(config)); |
| 76 | + } |
| 77 | + |
| 78 | + var connection = GetConnection(config); |
| 79 | + |
| 80 | + return DoUseOracle<TContext>(optionsBuilder, connection, oracleOptionsAction); |
| 81 | + } |
| 82 | + |
| 83 | + public static DbContextOptionsBuilder<TContext> UseOracle<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder, IConfiguration config, string serviceName, object oracleOptionsAction = null) |
| 84 | + where TContext : DbContext |
| 85 | + { |
| 86 | + if (optionsBuilder == null) |
| 87 | + { |
| 88 | + throw new ArgumentNullException(nameof(optionsBuilder)); |
| 89 | + } |
| 90 | + |
| 91 | + if (config == null) |
| 92 | + { |
| 93 | + throw new ArgumentNullException(nameof(config)); |
| 94 | + } |
| 95 | + |
| 96 | + if (string.IsNullOrEmpty(serviceName)) |
| 97 | + { |
| 98 | + throw new ArgumentException(nameof(serviceName)); |
| 99 | + } |
| 100 | + |
| 101 | + var connection = GetConnection(config, serviceName); |
| 102 | + |
| 103 | + return DoUseOracle<TContext>(optionsBuilder, connection, oracleOptionsAction); |
| 104 | + } |
| 105 | + |
| 106 | + private static DbContextOptionsBuilder DoUseOracle(DbContextOptionsBuilder optionsBuilder, object connection, object oracleOptionsAction) |
| 107 | + { |
| 108 | + Type extensionType = EntityFrameworkCoreTypeLocator.OracleDbContextOptionsType; |
| 109 | + |
| 110 | + MethodInfo useMethod = FindUseSqlMethod(extensionType, new Type[] { typeof(DbContextOptionsBuilder), typeof(string) }); |
| 111 | + if (extensionType == null) |
| 112 | + { |
| 113 | + throw new ConnectorException("Unable to find UseOracle extension, are you missing Oracle EntityFramework Core assembly"); |
| 114 | + } |
| 115 | + |
| 116 | + object result = ConnectorHelpers.Invoke(useMethod, null, new object[] { optionsBuilder, connection, oracleOptionsAction }); |
| 117 | + if (result == null) |
| 118 | + { |
| 119 | + throw new ConnectorException(string.Format("Failed to invoke UseOracle extension, connection: {0}", connection)); |
| 120 | + } |
| 121 | + |
| 122 | + return (DbContextOptionsBuilder)result; |
| 123 | + } |
| 124 | + |
| 125 | + private static DbContextOptionsBuilder<TContext> DoUseOracle<TContext>(DbContextOptionsBuilder<TContext> optionsBuilder, string connection, object oracleOptionsAction = null) |
| 126 | + where TContext : DbContext |
| 127 | + { |
| 128 | + return (DbContextOptionsBuilder<TContext>)DoUseOracle((DbContextOptionsBuilder)optionsBuilder, connection, oracleOptionsAction); |
| 129 | + } |
| 130 | + |
| 131 | + private static MethodInfo FindUseSqlMethod(Type type, Type[] parameterTypes) |
| 132 | + { |
| 133 | + var typeInfo = type.GetTypeInfo(); |
| 134 | + var declaredMethods = typeInfo.DeclaredMethods; |
| 135 | + |
| 136 | + foreach (MethodInfo ci in declaredMethods) |
| 137 | + { |
| 138 | + var parameters = ci.GetParameters(); |
| 139 | + if (parameters.Length == 3 && |
| 140 | + ci.Name.Equals("UseOracle", StringComparison.InvariantCultureIgnoreCase) && |
| 141 | + parameters[0].ParameterType.Equals(parameterTypes[0]) && |
| 142 | + parameters[1].ParameterType.Equals(parameterTypes[1]) && |
| 143 | + ci.IsPublic && ci.IsStatic) |
| 144 | + { |
| 145 | + return ci; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + private static string GetConnection(IConfiguration config, string serviceName = null) |
| 153 | + { |
| 154 | + OracleServiceInfo info = string.IsNullOrEmpty(serviceName) |
| 155 | + ? config.GetSingletonServiceInfo<OracleServiceInfo>() |
| 156 | + : config.GetRequiredServiceInfo<OracleServiceInfo>(serviceName); |
| 157 | + |
| 158 | + OracleProviderConnectorOptions oracleConfig = new OracleProviderConnectorOptions(config); |
| 159 | + |
| 160 | + OracleProviderConnectorFactory factory = new OracleProviderConnectorFactory(info, oracleConfig, null); |
| 161 | + |
| 162 | + return factory.CreateConnectionString(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments