/PetaPoco/OracleProvider.cs

http://github.com/toptensoftware/PetaPoco · C# · 117 lines · 64 code · 16 blank · 37 comment · 10 complexity · 10d706f4f6e969da36cfcb102f343bc3 MD5 · raw file

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