|
| 1 | +using System; |
| 2 | +using System.Data; |
| 3 | +using System.Reflection; |
| 4 | +using NHibernate.AdoNet; |
| 5 | +using NHibernate.Engine.Query; |
| 6 | +using NHibernate.SqlTypes; |
| 7 | +using NHibernate.Util; |
| 8 | + |
| 9 | +namespace NHibernate.Driver |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// A NHibernate Driver for using the Oracle.ManagedDataAccess DataProvider |
| 13 | + /// </summary> |
| 14 | + public class OracleManagedDataClientDriver : ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider |
| 15 | + { |
| 16 | + private const string driverAssemblyName = "Oracle.ManagedDataAccess"; |
| 17 | + private const string connectionTypeName = "Oracle.ManagedDataAccess.Client.OracleConnection"; |
| 18 | + private const string commandTypeName = "Oracle.ManagedDataAccess.Client.OracleCommand"; |
| 19 | + private static readonly SqlType GuidSqlType = new SqlType(DbType.Binary, 16); |
| 20 | + private readonly PropertyInfo oracleCommandBindByName; |
| 21 | + private readonly PropertyInfo oracleDbType; |
| 22 | + private readonly object oracleDbTypeRefCursor; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes a new instance of <see cref="OracleDataClientDriver"/>. |
| 26 | + /// </summary> |
| 27 | + /// <exception cref="HibernateException"> |
| 28 | + /// Thrown when the <c>Oracle.ManagedDataAccess</c> assembly can not be loaded. |
| 29 | + /// </exception> |
| 30 | + public OracleManagedDataClientDriver() |
| 31 | + : base( |
| 32 | + "Oracle.ManagedDataAccess.Client", |
| 33 | + driverAssemblyName, |
| 34 | + connectionTypeName, |
| 35 | + commandTypeName) |
| 36 | + { |
| 37 | + var oracleCommandType = ReflectHelper.TypeFromAssembly("Oracle.ManagedDataAccess.Client.OracleCommand", driverAssemblyName, false); |
| 38 | + oracleCommandBindByName = oracleCommandType.GetProperty("BindByName"); |
| 39 | + |
| 40 | + var parameterType = ReflectHelper.TypeFromAssembly("Oracle.ManagedDataAccess.Client.OracleParameter", driverAssemblyName, false); |
| 41 | + oracleDbType = parameterType.GetProperty("OracleDbType"); |
| 42 | + |
| 43 | + var oracleDbTypeEnum = ReflectHelper.TypeFromAssembly("Oracle.ManagedDataAccess.Client.OracleDbType", driverAssemblyName, false); |
| 44 | + oracleDbTypeRefCursor = Enum.Parse(oracleDbTypeEnum, "RefCursor"); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary></summary> |
| 48 | + public override string NamedPrefix |
| 49 | + { |
| 50 | + get { return ":"; } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary></summary> |
| 54 | + public override bool UseNamedPrefixInParameter |
| 55 | + { |
| 56 | + get { return true; } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary></summary> |
| 60 | + public override bool UseNamedPrefixInSql |
| 61 | + { |
| 62 | + get { return true; } |
| 63 | + } |
| 64 | + |
| 65 | + /// <remarks> |
| 66 | + /// This adds logic to ensure that a DbType.Boolean parameter is not created since |
| 67 | + /// ODP.NET doesn't support it. |
| 68 | + /// </remarks> |
| 69 | + protected override void InitializeParameter(IDbDataParameter dbParam, string name, SqlType sqlType) |
| 70 | + { |
| 71 | + // if the parameter coming in contains a boolean then we need to convert it |
| 72 | + // to another type since ODP.NET doesn't support DbType.Boolean |
| 73 | + switch (sqlType.DbType) |
| 74 | + { |
| 75 | + case DbType.Boolean: |
| 76 | + base.InitializeParameter(dbParam, name, SqlTypeFactory.Int16); |
| 77 | + break; |
| 78 | + case DbType.Guid: |
| 79 | + base.InitializeParameter(dbParam, name, GuidSqlType); |
| 80 | + break; |
| 81 | + default: |
| 82 | + base.InitializeParameter(dbParam, name, sqlType); |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + protected override void OnBeforePrepare(IDbCommand command) |
| 88 | + { |
| 89 | + base.OnBeforePrepare(command); |
| 90 | + |
| 91 | + // need to explicitly turn on named parameter binding |
| 92 | + // http://tgaw.wordpress.com/2006/03/03/ora-01722-with-odp-and-command-parameters/ |
| 93 | + oracleCommandBindByName.SetValue(command, true, null); |
| 94 | + |
| 95 | + var detail = CallableParser.Parse(command.CommandText); |
| 96 | + |
| 97 | + if (!detail.IsCallable) |
| 98 | + return; |
| 99 | + |
| 100 | + command.CommandType = CommandType.StoredProcedure; |
| 101 | + command.CommandText = detail.FunctionName; |
| 102 | + oracleCommandBindByName.SetValue(command, false, null); |
| 103 | + |
| 104 | + var outCursor = command.CreateParameter(); |
| 105 | + oracleDbType.SetValue(outCursor, oracleDbTypeRefCursor, null); |
| 106 | + |
| 107 | + outCursor.Direction = detail.HasReturn ? ParameterDirection.ReturnValue : ParameterDirection.Output; |
| 108 | + |
| 109 | + command.Parameters.Insert(0, outCursor); |
| 110 | + } |
| 111 | + |
| 112 | + System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass |
| 113 | + { |
| 114 | + get { return typeof (OracleDataClientBatchingBatcherFactory); } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments