OleDbConnection.cs 6.6 KB

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