2 // System.Data.OleDb.OleDbConnection
\r
5 // Konstantin Triger <kostat@mainsoft.com>
\r
6 // Boris Kirzner <borisk@mainsoft.com>
\r
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
\r
12 // Permission is hereby granted, free of charge, to any person obtaining
\r
13 // a copy of this software and associated documentation files (the
\r
14 // "Software"), to deal in the Software without restriction, including
\r
15 // without limitation the rights to use, copy, modify, merge, publish,
\r
16 // distribute, sublicense, and/or sell copies of the Software, and to
\r
17 // permit persons to whom the Software is furnished to do so, subject to
\r
18 // the following conditions:
\r
20 // The above copyright notice and this permission notice shall be
\r
21 // included in all copies or substantial portions of the Software.
\r
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
35 using System.Data.Common;
\r
36 using System.Collections;
\r
37 using System.Data.ProviderBase;
\r
38 using System.Globalization;
\r
42 using System.Configuration;
\r
43 using Mainsoft.Data.Configuration;
\r
44 using Mainsoft.Data.Jdbc.Providers;
\r
46 namespace System.Data.OleDb
\r
48 public sealed class OleDbConnection : AbstractDBConnection {
\r
51 static readonly IList _providers = (IList) ConfigurationSettings.GetConfig("Mainsoft.Data.Configuration/OleDbProviders");
\r
57 public event OleDbInfoMessageEventHandler InfoMessage;
\r
58 public event StateChangeEventHandler StateChange;
\r
60 #endregion // Events
\r
62 #region Constructors
\r
64 public OleDbConnection() : this(null) {
\r
67 public OleDbConnection(String connectionString) : base(connectionString) {
\r
70 #endregion // Constructors
\r
74 public String Provider {
\r
76 IDictionary conDict = ConnectionStringBuilder;
\r
77 string provider = (string)conDict["Provider"];
\r
78 if (provider == null || provider.Length == 0)
\r
79 throw ExceptionHelper.OleDbNoProviderSpecified();
\r
85 protected override IConnectionProvider GetConnectionProvider() {
\r
86 IDictionary conProviderDict = ConnectionStringDictionary.Parse(ConnectionString);
\r
87 string providerName = (string)conProviderDict["Provider"];
\r
89 if (providerName != null)
\r
90 for (int i = 0; i < _providers.Count; i++) {
\r
91 IDictionary providerInfo = (IDictionary) _providers[i];
\r
93 string curProvider = (string)providerInfo["Provider"];
\r
94 if (String.Compare(providerName, 0, curProvider, 0, providerName.Length, true, CultureInfo.InvariantCulture) == 0) {
\r
95 string providerType = (string) providerInfo [ConfigurationConsts.ProviderType];
\r
96 if (providerType == null || providerType.Length == 0)
\r
97 return new GenericProvider (providerInfo);
\r
99 Type t = Type.GetType (providerType);
\r
100 return (IConnectionProvider) Activator.CreateInstance (t , new object[] {providerInfo});
\r
105 return new GenericProvider (conProviderDict);
\r
108 #endregion // Properties
\r
112 public new OleDbTransaction BeginTransaction(IsolationLevel level)
\r
114 return new OleDbTransaction(level, this);
\r
117 public new OleDbTransaction BeginTransaction()
\r
119 return BeginTransaction(IsolationLevel.ReadCommitted);
\r
122 public new OleDbCommand CreateCommand()
\r
124 return new OleDbCommand(this);
\r
127 protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
\r
128 return BeginTransaction();
\r
131 protected override DbCommand CreateDbCommand() {
\r
132 return CreateCommand();
\r
135 protected sealed override SystemException CreateException(SQLException e)
\r
137 return new OleDbException(e,this);
\r
140 protected sealed override SystemException CreateException(string message)
\r
142 return new OleDbException(message, null, this);
\r
145 public DataTable GetOleDbSchemaTable (Guid schema, object[] restrictions)
\r
147 DataTable schemaTable = new DataTable("Tables");
\r
148 schemaTable.Columns.Add("TABLE_CATALOG");
\r
149 schemaTable.Columns.Add("TABLE_SCHEMA");
\r
150 schemaTable.Columns.Add("TABLE_NAME");
\r
151 schemaTable.Columns.Add("TABLE_TYPE");
\r
152 schemaTable.Columns.Add("TABLE_GUID");
\r
153 schemaTable.Columns.Add("DESCRIPTION");
\r
154 schemaTable.Columns.Add("TABLE_PROPID");
\r
155 schemaTable.Columns.Add("DATE_CREATED");
\r
156 schemaTable.Columns.Add("DATE_MODIFIED");
\r
159 java.sql.Connection con = JdbcConnection;
\r
160 String catalog = con.getCatalog();
\r
162 DatabaseMetaData meta = con.getMetaData();
\r
163 java.sql.ResultSet schemaRes = meta.getSchemas();
\r
164 System.Collections.ArrayList schemas = new System.Collections.ArrayList();
\r
165 while(schemaRes.next()) {
\r
166 schemas.Add(schemaRes.getString(1));
\r
170 for(int i = 0; i < schemas.Count; i++) {
\r
171 java.sql.ResultSet tableRes = meta.getTables(catalog, schemas[i].ToString(), null, null);
\r
172 while(tableRes.next()) {
\r
173 DataRow row = schemaTable.NewRow();
\r
174 row["TABLE_CATALOG"] = catalog;
\r
175 row["TABLE_SCHEMA"] = schemas[i];
\r
176 row["TABLE_NAME"] = tableRes.getString("TABLE_NAME");
\r
177 row["TABLE_TYPE"] = tableRes.getString("TABLE_TYPE");
\r
178 row["DESCRIPTION"] = tableRes.getString("REMARKS");
\r
180 schemaTable.Rows.Add(row);
\r
184 return schemaTable;
\r
187 public static void ReleaseObjectPool()
\r
189 // since we're using connection pool from app servet, this is by design
\r
190 //throw new NotImplementedException();
\r
193 protected internal sealed override void OnSqlWarning(SQLWarning warning)
\r
195 OleDbErrorCollection col = new OleDbErrorCollection(warning, this);
\r
196 OnOleDbInfoMessage(new OleDbInfoMessageEventArgs(col));
\r
199 protected internal sealed override void OnStateChanged(ConnectionState orig, ConnectionState current)
\r
201 if(StateChange != null) {
\r
202 StateChange(this, new StateChangeEventArgs(orig, current));
\r
206 public override void Close()
\r
208 ConnectionState orig = State;
\r
210 ConnectionState current = State;
\r
211 if(current != orig) {
\r
212 OnStateChanged(orig, current);
\r
216 private void OnOleDbInfoMessage (OleDbInfoMessageEventArgs value)
\r
218 if (InfoMessage != null) {
\r
219 InfoMessage (this, value);
\r
223 #endregion // Methods
\r