OleDbConnection.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. for (int i = 0; i < _providers.Count; i++) {
  69. IDictionary providerInfo = (IDictionary) _providers[i];
  70. string curProvider = (string)providerInfo["Provider"];
  71. if (String.Compare(providerName, curProvider, true, CultureInfo.InvariantCulture) == 0) {
  72. string providerType = (string) providerInfo [ConfigurationConsts.ProviderType];
  73. if (providerType == null || providerType.Length == 0)
  74. return new GenericProvider (providerInfo);
  75. else {
  76. Type t = Type.GetType (providerType);
  77. return (IConnectionProvider) Activator.CreateInstance (t , new object[] {providerInfo});
  78. }
  79. }
  80. }
  81. return new GenericProvider (conProviderDict);
  82. }
  83. #endregion // Properties
  84. #region Methods
  85. public new OleDbTransaction BeginTransaction(IsolationLevel level)
  86. {
  87. return new OleDbTransaction(level, this);
  88. }
  89. public new OleDbTransaction BeginTransaction()
  90. {
  91. return BeginTransaction(IsolationLevel.ReadCommitted);
  92. }
  93. public new OleDbCommand CreateCommand()
  94. {
  95. return new OleDbCommand(this);
  96. }
  97. protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
  98. return BeginTransaction();
  99. }
  100. protected override DbCommand CreateDbCommand() {
  101. return CreateCommand();
  102. }
  103. protected sealed override SystemException CreateException(SQLException e)
  104. {
  105. return new OleDbException(e,this);
  106. }
  107. protected sealed override SystemException CreateException(string message)
  108. {
  109. return new OleDbException(message, null, this);
  110. }
  111. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  112. {
  113. DataTable schemaTable = new DataTable("Tables");
  114. schemaTable.Columns.Add("TABLE_CATALOG");
  115. schemaTable.Columns.Add("TABLE_SCHEMA");
  116. schemaTable.Columns.Add("TABLE_NAME");
  117. schemaTable.Columns.Add("TABLE_TYPE");
  118. schemaTable.Columns.Add("TABLE_GUID");
  119. schemaTable.Columns.Add("DESCRIPTION");
  120. schemaTable.Columns.Add("TABLE_PROPID");
  121. schemaTable.Columns.Add("DATE_CREATED");
  122. schemaTable.Columns.Add("DATE_MODIFIED");
  123. java.sql.Connection con = JdbcConnection;
  124. String catalog = con.getCatalog();
  125. DatabaseMetaData meta = con.getMetaData();
  126. ResultSet schemaRes = meta.getSchemas();
  127. System.Collections.ArrayList schemas = new System.Collections.ArrayList();
  128. while(schemaRes.next()) {
  129. schemas.Add(schemaRes.getString(1));
  130. }
  131. schemaRes.close();
  132. for(int i = 0; i < schemas.Count; i++) {
  133. ResultSet tableRes = meta.getTables(catalog, schemas[i].ToString(), null, null);
  134. while(tableRes.next()) {
  135. DataRow row = schemaTable.NewRow();
  136. row["TABLE_CATALOG"] = catalog;
  137. row["TABLE_SCHEMA"] = schemas[i];
  138. row["TABLE_NAME"] = tableRes.getString("TABLE_NAME");
  139. row["TABLE_TYPE"] = tableRes.getString("TABLE_TYPE");
  140. row["DESCRIPTION"] = tableRes.getString("REMARKS");
  141. schemaTable.Rows.Add(row);
  142. }
  143. tableRes.close();
  144. }
  145. return schemaTable;
  146. }
  147. public static void ReleaseObjectPool()
  148. {
  149. // since we're using connection pool from app servet, this is by design
  150. //throw new NotImplementedException();
  151. }
  152. protected internal sealed override void OnSqlWarning(SQLWarning warning)
  153. {
  154. OleDbErrorCollection col = new OleDbErrorCollection(warning, this);
  155. OnOleDbInfoMessage(new OleDbInfoMessageEventArgs(col));
  156. }
  157. protected internal sealed override void OnStateChanged(ConnectionState orig, ConnectionState current)
  158. {
  159. if(StateChange != null) {
  160. StateChange(this, new StateChangeEventArgs(orig, current));
  161. }
  162. }
  163. public override void Close()
  164. {
  165. ConnectionState orig = State;
  166. base.Close();
  167. ConnectionState current = State;
  168. if(current != orig) {
  169. OnStateChanged(orig, current);
  170. }
  171. }
  172. private void OnOleDbInfoMessage (OleDbInfoMessageEventArgs value)
  173. {
  174. if (InfoMessage != null) {
  175. InfoMessage (this, value);
  176. }
  177. }
  178. #endregion // Methods
  179. }
  180. }