| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- //
- // OracleConnection.cs
- //
- // Part of the Mono class libraries at
- // mcs/class/System.Data.OracleClient/System.Data.OracleClient
- //
- // Assembly: System.Data.OracleClient.dll
- // Namespace: System.Data.OracleClient
- //
- // Authors:
- // Daniel Morgan <[email protected]>
- // Tim Coleman <[email protected]>
- //
- // Copyright (C) Daniel Morgan, 2002
- // Copyright (C) Tim Coleman, 2003
- //
- // Original source code for setting ConnectionString
- // by Tim Coleman <[email protected]>
- //
- // Copyright (C) Tim Coleman, 2002
- //
- // Licensed under the MIT/X11 License.
- //
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.Data;
- using System.Data.OracleClient.Oci;
- using System.Drawing.Design;
- using System.EnterpriseServices;
- using System.Text;
- namespace System.Data.OracleClient
- {
- internal struct OracleConnectionInfo
- {
- public string Username;
- public string Password;
- public string Database;
- }
- [DefaultEvent ("InfoMessage")]
- public sealed class OracleConnection : Component, ICloneable, IDbConnection
- {
- #region Fields
- OciGlue oci;
- ConnectionState state;
- OracleConnectionInfo conInfo;
- OracleTransaction transaction = null;
- string connectionString = "";
- OracleDataReader dataReader = null;
- #endregion // Fields
- #region Constructors
- public OracleConnection ()
- {
- state = ConnectionState.Closed;
- oci = new OciGlue ();
- }
- public OracleConnection (string connectionString)
- : this()
- {
- SetConnectionString (connectionString);
- }
- #endregion // Constructors
- #region Properties
- int IDbConnection.ConnectionTimeout {
- [MonoTODO]
- get { return -1; }
- }
- string IDbConnection.Database {
- [MonoTODO]
- get { return String.Empty; }
- }
- internal OracleDataReader DataReader {
- get { return dataReader; }
- set { dataReader = value; }
- }
- internal OciEnvironmentHandle Environment {
- get { return oci.Environment; }
- }
- internal OciErrorHandle ErrorHandle {
- get { return oci.ErrorHandle; }
- }
- internal OciServiceHandle ServiceContext {
- get { return oci.ServiceContext; }
- }
- [MonoTODO]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public string DataSource {
- get {
- return conInfo.Database;
- }
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public ConnectionState State {
- get { return state; }
- }
- [DefaultValue ("")]
- [RecommendedAsConfigurable (true)]
- [RefreshProperties (RefreshProperties.All)]
- [Editor ("Microsoft.VSDesigner.Data.Oracle.Design.OracleConnectionStringEditor, " + Consts.AssemblyMicrosoft_VSDesigner, typeof(UITypeEditor))]
- public string ConnectionString {
- get { return connectionString; }
- set { SetConnectionString (value); }
- }
- [MonoTODO]
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public string ServerVersion {
- get {
- if (this.State != ConnectionState.Open)
- throw new System.InvalidOperationException ("Invalid operation. The connection is closed.");
- return GetOracleVersion ();
- }
- }
- internal string GetOracleVersion ()
- {
- byte[] buffer = new Byte[256];
- uint bufflen = (uint) buffer.Length;
- IntPtr sh = oci.ServiceContext;
- IntPtr eh = oci.ErrorHandle;
- OciCalls.OCIServerVersion (sh,
- eh,
- ref buffer, bufflen,
- OciHandleType.Service);
-
- // Get length of returned string
- int rsize = 0;
- IntPtr env = oci.Environment;
- OciCalls.OCICharSetToUnicode (env, null, buffer, out rsize);
-
- // Get string
- StringBuilder ret = new StringBuilder(rsize);
- OciCalls.OCICharSetToUnicode (env, ret, buffer, out rsize);
- return ret.ToString ();
- }
- internal OciGlue Oci {
- get { return oci; }
- }
- internal OracleTransaction Transaction {
- get { return transaction; }
- set { transaction = value; }
- }
- #endregion // Properties
- #region Methods
- public OracleTransaction BeginTransaction ()
- {
- return BeginTransaction (IsolationLevel.ReadCommitted);
- }
- public OracleTransaction BeginTransaction (IsolationLevel il)
- {
- if (state == ConnectionState.Closed)
- throw new InvalidOperationException ("The connection is not open.");
- if (transaction != null)
- throw new InvalidOperationException ("OracleConnection does not support parallel transactions.");
- OciTransactionHandle transactionHandle = oci.CreateTransaction ();
- if (transactionHandle == null)
- throw new Exception("Error: Unable to start transaction");
- else {
- transactionHandle.Begin ();
- transaction = new OracleTransaction (this, il, transactionHandle);
- }
- return transaction;
- }
- [MonoTODO]
- void IDbConnection.ChangeDatabase (string databaseName)
- {
- throw new NotImplementedException ();
- }
- public OracleCommand CreateCommand ()
- {
- OracleCommand command = new OracleCommand ();
- command.Connection = this;
- return command;
- }
- [MonoTODO]
- object ICloneable.Clone ()
- {
- OracleConnection con = new OracleConnection ();
- con.ConnectionString = this.ConnectionString;
- if (this.State == ConnectionState.Open)
- con.Open ();
- // TODO: what other properties need to be cloned?
- return con;
- }
- IDbTransaction IDbConnection.BeginTransaction ()
- {
- return BeginTransaction ();
- }
- IDbTransaction IDbConnection.BeginTransaction (IsolationLevel iso)
- {
- return BeginTransaction (iso);
- }
- IDbCommand IDbConnection.CreateCommand ()
- {
- return CreateCommand ();
- }
- void IDisposable.Dispose ()
- {
- Dispose (true);
- GC.SuppressFinalize (this);
- }
- [MonoTODO]
- protected override void Dispose (bool disposing)
- {
- base.Dispose (disposing);
- }
- [MonoTODO]
- public void EnlistDistributedTransaction (ITransaction distributedTransaction)
- {
- throw new NotImplementedException ();
- }
- public void Open ()
- {
- oci.CreateConnection (conInfo);
- state = ConnectionState.Open;
- CreateStateChange (ConnectionState.Closed, ConnectionState.Open);
- }
- internal void CreateInfoMessage (OciErrorInfo info)
- {
- OracleInfoMessageEventArgs a = new OracleInfoMessageEventArgs (info);
- OnInfoMessage (a);
- }
- private void OnInfoMessage (OracleInfoMessageEventArgs e)
- {
- if (InfoMessage != null)
- InfoMessage (this, e);
- }
- internal void CreateStateChange (ConnectionState original, ConnectionState current)
- {
- StateChangeEventArgs a = new StateChangeEventArgs (original, current);
- OnStateChange (a);
- }
- private void OnStateChange (StateChangeEventArgs e)
- {
- if (StateChange != null)
- StateChange (this, e);
- }
- public void Close ()
- {
- if (transaction != null)
- transaction.Rollback ();
- oci.Disconnect ();
- state = ConnectionState.Closed;
- CreateStateChange (ConnectionState.Open, ConnectionState.Closed);
- }
- void SetConnectionString (string connectionString)
- {
- this.connectionString = connectionString;
- conInfo.Username = "";
- conInfo.Database = "";
- conInfo.Password = "";
- if (connectionString == String.Empty)
- return;
-
- connectionString += ";";
- NameValueCollection parameters = new NameValueCollection ();
- bool inQuote = false;
- bool inDQuote = false;
- string name = String.Empty;
- string value = String.Empty;
- StringBuilder sb = new StringBuilder ();
- foreach (char c in connectionString) {
- switch (c) {
- case '\'':
- inQuote = !inQuote;
- break;
- case '"' :
- inDQuote = !inDQuote;
- break;
- case ';' :
- if (!inDQuote && !inQuote) {
- if (name != String.Empty && name != null) {
- value = sb.ToString ();
- parameters [name.ToUpper ().Trim ()] = value.Trim ();
- }
- name = String.Empty;
- value = String.Empty;
- sb = new StringBuilder ();
- }
- else
- sb.Append (c);
- break;
- case '=' :
- if (!inDQuote && !inQuote) {
- name = sb.ToString ();
- sb = new StringBuilder ();
- }
- else
- sb.Append (c);
- break;
- default:
- sb.Append (c);
- break;
- }
- }
- SetProperties (parameters);
- }
- private void SetProperties (NameValueCollection parameters)
- {
- string value;
- foreach (string name in parameters) {
- value = parameters[name];
- switch (name) {
- case "DATA SOURCE" :
- case "SERVER" :
- conInfo.Database = value;
- break;
- case "PASSWORD" :
- case "PWD" :
- conInfo.Password = value;
- break;
- case "UID" :
- case "USER ID" :
- conInfo.Username = value;
- break;
- default:
- throw new ArgumentException("Connection parameter not supported: '" + name + "'");
- }
- }
- }
- #endregion // Methods
- public event OracleInfoMessageEventHandler InfoMessage;
- public event StateChangeEventHandler StateChange;
- }
- }
|