OleDbConnection.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // System.Data.OleDb.OleDbConnection
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Data;
  31. using System.Data.Common;
  32. using System.Collections;
  33. using System.Data.ProviderBase;
  34. using System.Globalization;
  35. using java.sql;
  36. using System.Configuration;
  37. using Mainsoft.Data.Configuration;
  38. using Mainsoft.Data.Jdbc.Providers;
  39. namespace System.Data.OleDb
  40. {
  41. public sealed class OleDbConnection : AbstractDBConnection {
  42. #region Fields
  43. static readonly IList _providers = (IList) ConfigurationSettings.GetConfig("Mainsoft.Data.Configuration/OleDbProviders");
  44. #endregion //Fields
  45. #region Events
  46. public event OleDbInfoMessageEventHandler InfoMessage;
  47. public event StateChangeEventHandler StateChange;
  48. #endregion // Events
  49. #region Constructors
  50. public OleDbConnection() : this(null) {
  51. }
  52. public OleDbConnection(String connectionString) : base(connectionString) {
  53. }
  54. #endregion // Constructors
  55. #region Properties
  56. public String Provider {
  57. get {
  58. IDictionary conDict = ConnectionStringBuilder;
  59. string provider = (string)conDict["Provider"];
  60. if (provider == null || provider.Length == 0)
  61. throw ExceptionHelper.OleDbNoProviderSpecified();
  62. return provider;
  63. }
  64. }
  65. protected override IConnectionProvider GetConnectionProvider() {
  66. IDictionary conProviderDict = ConnectionStringDictionary.Parse(ConnectionString);
  67. string providerName = (string)conProviderDict["Provider"];
  68. if (providerName != null)
  69. for (int i = 0; i < _providers.Count; i++) {
  70. IDictionary providerInfo = (IDictionary) _providers[i];
  71. string curProvider = (string)providerInfo["Provider"];
  72. if (String.Compare(providerName, 0, curProvider, 0, providerName.Length, true, CultureInfo.InvariantCulture) == 0) {
  73. string providerType = (string) providerInfo [ConfigurationConsts.ProviderType];
  74. if (providerType == null || providerType.Length == 0)
  75. return new GenericProvider (providerInfo);
  76. else {
  77. Type t = Type.GetType (providerType);
  78. return (IConnectionProvider) Activator.CreateInstance (t , new object[] {providerInfo});
  79. }
  80. }
  81. }
  82. return new GenericProvider (conProviderDict);
  83. }
  84. #endregion // Properties
  85. #region Methods
  86. public new OleDbTransaction BeginTransaction(IsolationLevel level)
  87. {
  88. return new OleDbTransaction(level, this);
  89. }
  90. public new OleDbTransaction BeginTransaction()
  91. {
  92. return BeginTransaction(IsolationLevel.ReadCommitted);
  93. }
  94. public new OleDbCommand CreateCommand()
  95. {
  96. return new OleDbCommand(this);
  97. }
  98. protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
  99. return BeginTransaction();
  100. }
  101. protected override DbCommand CreateDbCommand() {
  102. return CreateCommand();
  103. }
  104. protected sealed override SystemException CreateException(SQLException e)
  105. {
  106. return new OleDbException(e,this);
  107. }
  108. protected sealed override SystemException CreateException(string message)
  109. {
  110. return new OleDbException(message, null, this);
  111. }
  112. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  113. {
  114. DataTable schemaTable = new DataTable("Tables");
  115. schemaTable.Columns.Add("TABLE_CATALOG");
  116. schemaTable.Columns.Add("TABLE_SCHEMA");
  117. schemaTable.Columns.Add("TABLE_NAME");
  118. schemaTable.Columns.Add("TABLE_TYPE");
  119. schemaTable.Columns.Add("TABLE_GUID");
  120. schemaTable.Columns.Add("DESCRIPTION");
  121. schemaTable.Columns.Add("TABLE_PROPID");
  122. schemaTable.Columns.Add("DATE_CREATED");
  123. schemaTable.Columns.Add("DATE_MODIFIED");
  124. java.sql.Connection con = JdbcConnection;
  125. String catalog = con.getCatalog();
  126. DatabaseMetaData meta = con.getMetaData();
  127. java.sql.ResultSet schemaRes = meta.getSchemas();
  128. System.Collections.ArrayList schemas = new System.Collections.ArrayList();
  129. while(schemaRes.next()) {
  130. schemas.Add(schemaRes.getString(1));
  131. }
  132. schemaRes.close();
  133. for(int i = 0; i < schemas.Count; i++) {
  134. java.sql.ResultSet tableRes = meta.getTables(catalog, schemas[i].ToString(), null, null);
  135. while(tableRes.next()) {
  136. DataRow row = schemaTable.NewRow();
  137. row["TABLE_CATALOG"] = catalog;
  138. row["TABLE_SCHEMA"] = schemas[i];
  139. row["TABLE_NAME"] = tableRes.getString("TABLE_NAME");
  140. row["TABLE_TYPE"] = tableRes.getString("TABLE_TYPE");
  141. row["DESCRIPTION"] = tableRes.getString("REMARKS");
  142. schemaTable.Rows.Add(row);
  143. }
  144. tableRes.close();
  145. }
  146. return schemaTable;
  147. }
  148. public static void ReleaseObjectPool()
  149. {
  150. // since we're using connection pool from app servet, this is by design
  151. //throw new NotImplementedException();
  152. }
  153. protected internal sealed override void OnSqlWarning(SQLWarning warning)
  154. {
  155. OleDbErrorCollection col = new OleDbErrorCollection(warning, this);
  156. OnOleDbInfoMessage(new OleDbInfoMessageEventArgs(col));
  157. }
  158. protected internal sealed override void OnStateChanged(ConnectionState orig, ConnectionState current)
  159. {
  160. if(StateChange != null) {
  161. StateChange(this, new StateChangeEventArgs(orig, current));
  162. }
  163. }
  164. public override void Close()
  165. {
  166. ConnectionState orig = State;
  167. base.Close();
  168. ConnectionState current = State;
  169. if(current != orig) {
  170. OnStateChanged(orig, current);
  171. }
  172. }
  173. private void OnOleDbInfoMessage (OleDbInfoMessageEventArgs value)
  174. {
  175. if (InfoMessage != null) {
  176. InfoMessage (this, value);
  177. }
  178. }
  179. #endregion // Methods
  180. }
  181. }