OleDbConnection.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Events
  43. public event OleDbInfoMessageEventHandler InfoMessage;
  44. #endregion // Events
  45. #region Constructors
  46. public OleDbConnection() : this(null) {
  47. }
  48. public OleDbConnection(String connectionString) : base(connectionString) {
  49. }
  50. #endregion // Constructors
  51. #region Properties
  52. public String Provider {
  53. get {
  54. IDictionary conDict = ConnectionStringBuilder;
  55. string provider = (string)conDict["Provider"];
  56. if (provider == null || provider.Length == 0)
  57. throw ExceptionHelper.OleDbNoProviderSpecified();
  58. return provider;
  59. }
  60. }
  61. protected override IConnectionProvider GetConnectionProvider() {
  62. IDictionary conProviderDict = ConnectionStringDictionary.Parse(ConnectionString);
  63. string jdbcUrl = (string)conProviderDict["JdbcUrl"];
  64. if (jdbcUrl == null) {
  65. string provider = (string)conProviderDict["Provider"];
  66. if (provider != null)
  67. return GetConnectionProvider("Mainsoft.Data.Configuration/OleDbProviders", provider);
  68. }
  69. return new GenericProvider (conProviderDict);
  70. }
  71. #endregion // Properties
  72. #region Methods
  73. public new OleDbTransaction BeginTransaction(IsolationLevel level)
  74. {
  75. return new OleDbTransaction(level, this);
  76. }
  77. public new OleDbTransaction BeginTransaction()
  78. {
  79. return BeginTransaction(IsolationLevel.ReadCommitted);
  80. }
  81. public new OleDbCommand CreateCommand()
  82. {
  83. return new OleDbCommand(this);
  84. }
  85. protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
  86. return BeginTransaction();
  87. }
  88. protected override DbCommand CreateDbCommand() {
  89. return CreateCommand();
  90. }
  91. protected sealed override SystemException CreateException(SQLException e)
  92. {
  93. return new OleDbException(e,this);
  94. }
  95. protected sealed override SystemException CreateException(string message)
  96. {
  97. return new OleDbException(message, null, this);
  98. }
  99. [MonoTODO]
  100. public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
  101. {
  102. if (State != ConnectionState.Open)
  103. throw ExceptionHelper.ConnectionNotOpened("GetOleDbSchemaTable", State.ToString());
  104. try {
  105. string[] fixedRestrictions = new string[4];
  106. if (restrictions != null) {
  107. if (restrictions.Length > 4)
  108. throw new OleDbException("The parameter is incorrect", null, this);
  109. for (int i = 0, count = restrictions.Length; i < count; i ++) {
  110. if (restrictions[i] != null) {
  111. if (!(restrictions[i] is string))
  112. throw new OleDbException("The parameter is incorrect", null, this);
  113. fixedRestrictions[i] = (string)restrictions[i];
  114. }
  115. }
  116. }
  117. DataTable schemaTable = new DataTable("Tables");
  118. schemaTable.Columns.Add("TABLE_CATALOG");
  119. schemaTable.Columns.Add("TABLE_SCHEMA");
  120. schemaTable.Columns.Add("TABLE_NAME");
  121. schemaTable.Columns.Add("TABLE_TYPE");
  122. schemaTable.Columns.Add("TABLE_GUID");
  123. schemaTable.Columns.Add("DESCRIPTION");
  124. schemaTable.Columns.Add("TABLE_PROPID");
  125. schemaTable.Columns.Add("DATE_CREATED");
  126. schemaTable.Columns.Add("DATE_MODIFIED");
  127. java.sql.ResultSet tableRes = JdbcConnection.getMetaData().getTables(
  128. fixedRestrictions[0],
  129. fixedRestrictions[1],
  130. fixedRestrictions[2],
  131. new string[]{fixedRestrictions[3]});
  132. try {
  133. while(tableRes.next()) {
  134. DataRow row = schemaTable.NewRow();
  135. row["TABLE_CATALOG"] = tableRes.getString("TABLE_CAT");
  136. row["TABLE_SCHEMA"] = tableRes.getString("TABLE_SCHEM");
  137. row["TABLE_NAME"] = tableRes.getString("TABLE_NAME");
  138. row["TABLE_TYPE"] = tableRes.getString("TABLE_TYPE");
  139. row["DESCRIPTION"] = tableRes.getString("REMARKS");
  140. schemaTable.Rows.Add(row);
  141. }
  142. }
  143. finally {
  144. tableRes.close();
  145. }
  146. return schemaTable;
  147. }
  148. catch (SQLException e) {
  149. throw CreateException(e);
  150. }
  151. }
  152. public static void ReleaseObjectPool()
  153. {
  154. // since we're using connection pool from app servet, this is by design
  155. //throw new NotImplementedException();
  156. }
  157. protected internal sealed override void OnSqlWarning(SQLWarning warning)
  158. {
  159. OleDbErrorCollection col = new OleDbErrorCollection(warning, this);
  160. OnOleDbInfoMessage(new OleDbInfoMessageEventArgs(col));
  161. }
  162. private void OnOleDbInfoMessage (OleDbInfoMessageEventArgs value)
  163. {
  164. if (InfoMessage != null) {
  165. InfoMessage (this, value);
  166. }
  167. }
  168. #endregion // Methods
  169. }
  170. }