zhaolei
8 days ago 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Data.Common;
using System.Reflection;
 
namespace PetaPoco
{
    /* 
    Thanks to Adam Schroder (@schotime) for this.
    
    This extra file provides an implementation of DbProviderFactory for early versions of the Oracle
    drivers that don't include include it.  For later versions of Oracle, the standard OracleProviderFactory
    class should work fine
    
    Uses reflection to load Oracle.DataAccess assembly and in-turn create connections and commands
    
    Currently untested.
    
    Usage:   
        
            new PetaPoco.Database("<connstring>", new PetaPoco.OracleProvider())
    
    Or in your app/web config (be sure to change ASSEMBLYNAME to the name of your 
    assembly containing OracleProvider.cs)
    
        <connectionStrings>
            <add
                name="oracle"
                connectionString="WHATEVER"
                providerName="Oracle"
                />
        </connectionStrings>
 
        <system.data>
            <DbProviderFactories>
                <add name="PetaPoco Oracle Provider" invariant="Oracle" description="PetaPoco Oracle Provider" 
                                type="PetaPoco.OracleProvider, ASSEMBLYNAME" />
            </DbProviderFactories>
        </system.data>
 
     */
 
    public class OracleProvider : DbProviderFactory
    {
        private const string _assemblyName = "Oracle.DataAccess";
        private const string _connectionTypeName = "Oracle.DataAccess.Client.OracleConnection";
        private const string _commandTypeName = "Oracle.DataAccess.Client.OracleCommand";
        private static Type _connectionType;
        private static Type _commandType;
 
        // Required for DbProviderFactories.GetFactory() to work.
        public static OracleProvider Instance = new OracleProvider();
 
        public OracleProvider()
        {
            _connectionType = TypeFromAssembly(_connectionTypeName, _assemblyName);
            _commandType = TypeFromAssembly(_commandTypeName, _assemblyName);
            if (_connectionType == null)
                throw new InvalidOperationException("Can't find Connection type: " + _connectionTypeName);
        }
 
        public override DbConnection CreateConnection()
        {
            return (DbConnection) Activator.CreateInstance(_connectionType);
        }
 
        public override DbCommand CreateCommand()
        {
            DbCommand command = (DbCommand) Activator.CreateInstance(_commandType);
 
            var oracleCommandBindByName = _commandType.GetProperty("BindByName");
            oracleCommandBindByName.SetValue(command, true, null);
 
            return command;
        }
 
        public static Type TypeFromAssembly(string typeName, string assemblyName)
        {
            try
            {
                // Try to get the type from an already loaded assembly
                Type type = Type.GetType(typeName);
 
                if (type != null)
                {
                    return type;
                }
 
                if (assemblyName == null)
                {
                    // No assembly was specified for the type, so just fail
                    string message = "Could not load type " + typeName + ". Possible cause: no assembly name specified.";
                    throw new TypeLoadException(message);
                }
 
                Assembly assembly = Assembly.Load(assemblyName);
 
                if (assembly == null)
                {
                    throw new InvalidOperationException("Can't find assembly: " + assemblyName);
                }
 
                type = assembly.GetType(typeName);
 
                if (type == null)
                {
                    return null;
                }
 
                return type;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}