2
0

OracleConnection.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. //
  2. // OracleConnection.cs
  3. //
  4. // Part of the Mono class libraries at
  5. // mcs/class/System.Data.OracleClient/System.Data.OracleClient
  6. //
  7. // Assembly: System.Data.OracleClient.dll
  8. // Namespace: System.Data.OracleClient
  9. //
  10. // Authors:
  11. // Daniel Morgan <[email protected]>
  12. // Tim Coleman <[email protected]>
  13. // Hubert FONGARNAND <[email protected]>
  14. // Marek Safar <[email protected]>
  15. //
  16. // Copyright (C) Daniel Morgan, 2002, 2005, 2006
  17. // Copyright (C) Tim Coleman, 2003
  18. // Copyright (C) Hubert FONGARNAND, 2005
  19. //
  20. // Original source code for setting ConnectionString
  21. // by Tim Coleman <[email protected]>
  22. //
  23. // Copyright (C) Tim Coleman, 2002
  24. //
  25. // Licensed under the MIT/X11 License.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Specialized;
  30. using System.ComponentModel;
  31. using System.Data;
  32. using System.Data.OracleClient.Oci;
  33. using System.Drawing.Design;
  34. using System.EnterpriseServices;
  35. using System.Globalization;
  36. using System.Text;
  37. namespace System.Data.OracleClient
  38. {
  39. internal struct OracleConnectionInfo
  40. {
  41. internal string Username;
  42. internal string Password;
  43. internal string Database;
  44. internal string ConnectionString;
  45. internal OciCredentialType CredentialType;
  46. }
  47. [DefaultEvent ("InfoMessage")]
  48. public sealed class OracleConnection :
  49. #if NET_2_0
  50. Common.DbConnection, ICloneable
  51. #else
  52. Component, ICloneable, IDbConnection
  53. #endif
  54. {
  55. #region Fields
  56. OciGlue oci;
  57. ConnectionState state;
  58. OracleConnectionInfo conInfo;
  59. OracleTransaction transaction;
  60. string connectionString = String.Empty;
  61. string parsedConnectionString;
  62. OracleDataReader dataReader;
  63. bool pooling = true;
  64. static OracleConnectionPoolManager pools = new OracleConnectionPoolManager ();
  65. OracleConnectionPool pool;
  66. int minPoolSize;
  67. int maxPoolSize = 100;
  68. byte persistSecurityInfo = 1;
  69. bool disposed;
  70. IFormatProvider format_info;
  71. #endregion // Fields
  72. #region Constructors
  73. public OracleConnection ()
  74. {
  75. state = ConnectionState.Closed;
  76. }
  77. public OracleConnection (string connectionString)
  78. : this()
  79. {
  80. SetConnectionString (connectionString, false);
  81. }
  82. #endregion // Constructors
  83. #region Properties
  84. #if NET_2_0
  85. [MonoTODO ("Currently not respected.")]
  86. public override int ConnectionTimeout {
  87. get { return 0; }
  88. }
  89. #else
  90. [MonoTODO ("Currently not respected.")]
  91. int IDbConnection.ConnectionTimeout {
  92. get { return 0; }
  93. }
  94. #endif
  95. #if NET_2_0
  96. [Browsable (false)]
  97. [EditorBrowsable (EditorBrowsableState.Never)]
  98. public override string Database {
  99. #else
  100. string IDbConnection.Database {
  101. #endif
  102. [MonoTODO]
  103. get { return String.Empty; }
  104. }
  105. internal OracleDataReader DataReader {
  106. get { return dataReader; }
  107. set { dataReader = value; }
  108. }
  109. internal OciEnvironmentHandle Environment {
  110. get { return oci.Environment; }
  111. }
  112. internal OciErrorHandle ErrorHandle {
  113. get { return oci.ErrorHandle; }
  114. }
  115. internal OciServiceHandle ServiceContext {
  116. get { return oci.ServiceContext; }
  117. }
  118. internal OciSessionHandle Session {
  119. get { return oci.SessionHandle; }
  120. }
  121. #if NET_2_0
  122. [Browsable (false)]
  123. #endif
  124. [MonoTODO]
  125. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  126. public
  127. #if NET_2_0
  128. override
  129. #endif
  130. string DataSource {
  131. get {
  132. return conInfo.Database;
  133. }
  134. }
  135. [Browsable (false)]
  136. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  137. public
  138. #if NET_2_0
  139. override
  140. #endif
  141. ConnectionState State {
  142. get { return state; }
  143. }
  144. [DefaultValue ("")]
  145. #if NET_2_0
  146. [SettingsBindableAttribute (true)]
  147. #else
  148. [RecommendedAsConfigurable (true)]
  149. #endif
  150. [RefreshProperties (RefreshProperties.All)]
  151. [Editor ("Microsoft.VSDesigner.Data.Oracle.Design.OracleConnectionStringEditor, " + Consts.AssemblyMicrosoft_VSDesigner, typeof(UITypeEditor))]
  152. public
  153. #if NET_2_0
  154. override
  155. #endif
  156. string ConnectionString {
  157. get {
  158. if (parsedConnectionString == null)
  159. return string.Empty;
  160. return parsedConnectionString;
  161. }
  162. set {
  163. SetConnectionString (value, false);
  164. }
  165. }
  166. [MonoTODO]
  167. [Browsable (false)]
  168. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  169. public
  170. #if NET_2_0
  171. override
  172. #endif
  173. string ServerVersion {
  174. get {
  175. if (this.State != ConnectionState.Open)
  176. throw new System.InvalidOperationException ("Invalid operation. The connection is closed.");
  177. return GetOracleVersion ();
  178. }
  179. }
  180. internal string GetOracleVersion ()
  181. {
  182. byte[] buffer = new Byte[256];
  183. uint bufflen = (uint) buffer.Length;
  184. IntPtr sh = oci.ServiceContext;
  185. IntPtr eh = oci.ErrorHandle;
  186. OciCalls.OCIServerVersion (sh, eh, ref buffer, bufflen, OciHandleType.Service);
  187. // Get length of returned string
  188. int rsize = 0;
  189. IntPtr env = oci.Environment;
  190. OciCalls.OCICharSetToUnicode (env, null, buffer, out rsize);
  191. // Get string
  192. StringBuilder ret = new StringBuilder(rsize);
  193. OciCalls.OCICharSetToUnicode (env, ret, buffer, out rsize);
  194. return ret.ToString ();
  195. }
  196. internal OciGlue Oci {
  197. get { return oci; }
  198. }
  199. internal OracleTransaction Transaction {
  200. get { return transaction; }
  201. set { transaction = value; }
  202. }
  203. #endregion // Properties
  204. #region Methods
  205. public
  206. #if NET_2_0
  207. new
  208. #endif
  209. OracleTransaction BeginTransaction ()
  210. {
  211. return BeginTransaction (IsolationLevel.ReadCommitted);
  212. }
  213. public
  214. #if NET_2_0
  215. new
  216. #endif
  217. OracleTransaction BeginTransaction (IsolationLevel il)
  218. {
  219. if (state == ConnectionState.Closed)
  220. throw new InvalidOperationException ("The connection is not open.");
  221. if (transaction != null)
  222. throw new InvalidOperationException ("OracleConnection does not support parallel transactions.");
  223. OciTransactionHandle transactionHandle = oci.CreateTransaction ();
  224. if (transactionHandle == null)
  225. throw new Exception("Error: Unable to start transaction");
  226. else {
  227. transactionHandle.Begin ();
  228. transaction = new OracleTransaction (this, il, transactionHandle);
  229. }
  230. return transaction;
  231. }
  232. [MonoTODO]
  233. #if NET_2_0
  234. public override void ChangeDatabase (string value)
  235. #else
  236. void IDbConnection.ChangeDatabase (string value)
  237. #endif
  238. {
  239. throw new NotImplementedException ();
  240. }
  241. public
  242. #if NET_2_0
  243. new
  244. #endif
  245. OracleCommand CreateCommand ()
  246. {
  247. OracleCommand command = new OracleCommand ();
  248. command.Connection = this;
  249. return command;
  250. }
  251. [MonoTODO]
  252. object ICloneable.Clone ()
  253. {
  254. OracleConnection con = new OracleConnection ();
  255. con.SetConnectionString (connectionString, true);
  256. // TODO: what other properties need to be cloned?
  257. return con;
  258. }
  259. #if !NET_2_0
  260. IDbTransaction IDbConnection.BeginTransaction ()
  261. {
  262. return BeginTransaction ();
  263. }
  264. IDbTransaction IDbConnection.BeginTransaction (IsolationLevel il)
  265. {
  266. return BeginTransaction (il);
  267. }
  268. IDbCommand IDbConnection.CreateCommand ()
  269. {
  270. return CreateCommand ();
  271. }
  272. #endif
  273. [MonoTODO]
  274. protected override void Dispose (bool disposing)
  275. {
  276. if (!disposed) {
  277. if (State == ConnectionState.Open)
  278. Close ();
  279. dataReader = null;
  280. transaction = null;
  281. oci = null;
  282. pool = null;
  283. conInfo.Username = string.Empty;
  284. conInfo.Database = string.Empty;
  285. conInfo.Password = string.Empty;
  286. connectionString = null;
  287. parsedConnectionString = null;
  288. base.Dispose (disposing);
  289. disposed = true;
  290. }
  291. }
  292. [MonoTODO]
  293. public void EnlistDistributedTransaction (ITransaction distributedTransaction)
  294. {
  295. throw new NotImplementedException ();
  296. }
  297. // Get NLS_DATE_FORMAT string from Oracle server
  298. internal string GetSessionDateFormat ()
  299. {
  300. // 23 is 22 plus 1 for NUL terminated character
  301. // a DATE format has a max size of 22
  302. return GetNlsInfo (Session, 23, OciNlsServiceType.DATEFORMAT);
  303. }
  304. // Get NLS Info
  305. //
  306. // handle = OciEnvironmentHandle or OciSessionHandle
  307. // bufflen = Length of byte buffer to allocate to retrieve the NLS info
  308. // item = OciNlsServiceType enum value
  309. //
  310. // if unsure how much you need, use OciNlsServiceType.MAXBUFSZ
  311. internal string GetNlsInfo (OciHandle handle, uint bufflen, OciNlsServiceType item)
  312. {
  313. byte[] buffer = new Byte[bufflen];
  314. OciCalls.OCINlsGetInfo (handle, ErrorHandle,
  315. ref buffer, bufflen, (ushort) item);
  316. // Get length of returned string
  317. int rsize = 0;
  318. OciCalls.OCICharSetToUnicode (Environment, null, buffer, out rsize);
  319. // Get string
  320. StringBuilder ret = new StringBuilder (rsize);
  321. OciCalls.OCICharSetToUnicode (Environment, ret, buffer, out rsize);
  322. return ret.ToString ();
  323. }
  324. // An instance of IFormatProvider for locale - independent IFormattable.ToString () in Bind ()
  325. [MonoTODO("Handle other culture-specific informations, restrict buffer sizes")]
  326. internal IFormatProvider SessionFormatProvider {
  327. get {
  328. if (format_info == null && state == ConnectionState.Open) {
  329. NumberFormatInfo numberFormatInfo = new NumberFormatInfo ();
  330. numberFormatInfo.NumberGroupSeparator
  331. = GetNlsInfo (Session, (uint)OciNlsServiceType.MAXBUFSZ, OciNlsServiceType.GROUP);
  332. numberFormatInfo.NumberDecimalSeparator
  333. = GetNlsInfo (Session, (uint)OciNlsServiceType.MAXBUFSZ, OciNlsServiceType.DECIMAL);
  334. numberFormatInfo.CurrencyGroupSeparator
  335. = GetNlsInfo (Session, (uint)OciNlsServiceType.MAXBUFSZ, OciNlsServiceType.MONGROUP);
  336. numberFormatInfo.CurrencyDecimalSeparator
  337. = GetNlsInfo (Session, (uint)OciNlsServiceType.MAXBUFSZ, OciNlsServiceType.MONDECIMAL);
  338. format_info = numberFormatInfo;
  339. }
  340. return format_info;
  341. }
  342. }
  343. public
  344. #if NET_2_0
  345. override
  346. #endif
  347. void Open ()
  348. {
  349. if (State == ConnectionState.Open)
  350. return;
  351. PersistSecurityInfo ();
  352. if (!pooling) {
  353. oci = new OciGlue ();
  354. oci.CreateConnection (conInfo);
  355. } else {
  356. pool = pools.GetConnectionPool (conInfo, minPoolSize, maxPoolSize);
  357. oci = pool.GetConnection ();
  358. }
  359. state = ConnectionState.Open;
  360. CreateStateChange (ConnectionState.Closed, ConnectionState.Open);
  361. }
  362. internal void CreateInfoMessage (OciErrorInfo info)
  363. {
  364. OracleInfoMessageEventArgs a = new OracleInfoMessageEventArgs (info);
  365. OnInfoMessage (a);
  366. }
  367. private void OnInfoMessage (OracleInfoMessageEventArgs e)
  368. {
  369. if (InfoMessage != null)
  370. InfoMessage (this, e);
  371. }
  372. internal void CreateStateChange (ConnectionState original, ConnectionState current)
  373. {
  374. StateChangeEventArgs a = new StateChangeEventArgs (original, current);
  375. OnStateChange (a);
  376. }
  377. #if !NET_2_0
  378. private void OnStateChange (StateChangeEventArgs e)
  379. {
  380. if (StateChange != null)
  381. StateChange (this, e);
  382. }
  383. #endif
  384. public
  385. #if NET_2_0
  386. override
  387. #endif
  388. void Close ()
  389. {
  390. if (transaction != null)
  391. transaction.Rollback ();
  392. if (!pooling)
  393. oci.Disconnect ();
  394. else if (pool != null)
  395. pool.ReleaseConnection (oci);
  396. state = ConnectionState.Closed;
  397. CreateStateChange (ConnectionState.Open, ConnectionState.Closed);
  398. }
  399. #if NET_2_0
  400. protected override Common.DbTransaction BeginDbTransaction (IsolationLevel isolationLevel)
  401. {
  402. return BeginTransaction (isolationLevel);
  403. }
  404. protected override Common.DbCommand CreateDbCommand ()
  405. {
  406. return CreateCommand ();
  407. }
  408. #endif
  409. private void PersistSecurityInfo ()
  410. {
  411. // persistSecurityInfo:
  412. // 0 = true/yes
  413. // 1 = false/no (have not parsed out password yet)
  414. // 2 = like 1, but have parsed out password
  415. if (persistSecurityInfo == 0 || persistSecurityInfo == 2)
  416. return;
  417. persistSecurityInfo = 2;
  418. if (connectionString == null || connectionString.Length == 0)
  419. return;
  420. string conString = connectionString + ";";
  421. bool inQuote = false;
  422. bool inDQuote = false;
  423. int inParen = 0;
  424. string name = String.Empty;
  425. StringBuilder sb = new StringBuilder ();
  426. int nStart = 0;
  427. int nFinish = 0;
  428. int i = -1;
  429. foreach (char c in conString) {
  430. i ++;
  431. switch (c) {
  432. case '\'':
  433. inQuote = !inQuote;
  434. break;
  435. case '"' :
  436. inDQuote = !inDQuote;
  437. break;
  438. case '(':
  439. inParen++;
  440. sb.Append (c);
  441. break;
  442. case ')':
  443. inParen--;
  444. sb.Append (c);
  445. break;
  446. case ';' :
  447. if (!inDQuote && !inQuote) {
  448. if (name != String.Empty && name != null) {
  449. name = name.ToUpper ().Trim ();
  450. if (name.Equals ("PASSWORD") || name.Equals ("PWD")) {
  451. nFinish = i;
  452. string part1 = String.Empty;
  453. string part3 = String.Empty;
  454. sb = new StringBuilder ();
  455. if (nStart > 0) {
  456. part1 = conString.Substring (0, nStart);
  457. if (part1[part1.Length - 1] == ';')
  458. part1 = part1.Substring (0, part1.Length - 1);
  459. sb.Append (part1);
  460. }
  461. if (!part1.Equals (String.Empty))
  462. sb.Append (';');
  463. if (conString.Length - nFinish - 1 > 0) {
  464. part3 = conString.Substring (nFinish, conString.Length - nFinish);
  465. if (part3[0] == ';')
  466. part3 = part3.Substring(1, part3.Length - 1);
  467. sb.Append (part3);
  468. }
  469. parsedConnectionString = sb.ToString ();
  470. return;
  471. }
  472. }
  473. name = String.Empty;
  474. sb = new StringBuilder ();
  475. nStart = i;
  476. nFinish = i;
  477. }
  478. else
  479. sb.Append (c);
  480. break;
  481. case '=' :
  482. if (!inDQuote && !inQuote && inParen == 0) {
  483. name = sb.ToString ();
  484. sb = new StringBuilder ();
  485. }
  486. else
  487. sb.Append (c);
  488. break;
  489. default:
  490. sb.Append (c);
  491. break;
  492. }
  493. }
  494. }
  495. internal void SetConnectionString (string connectionString, bool persistSecurity)
  496. {
  497. persistSecurityInfo = 1;
  498. conInfo.Username = string.Empty;
  499. conInfo.Database = string.Empty;
  500. conInfo.Password = string.Empty;
  501. conInfo.CredentialType = OciCredentialType.RDBMS;
  502. if (connectionString == null || connectionString.Length == 0) {
  503. this.connectionString = connectionString;
  504. this.parsedConnectionString = connectionString;
  505. return;
  506. }
  507. this.connectionString = String.Copy (connectionString);
  508. this.parsedConnectionString = this.connectionString;
  509. connectionString += ";";
  510. NameValueCollection parameters = new NameValueCollection ();
  511. bool inQuote = false;
  512. bool inDQuote = false;
  513. int inParen = 0;
  514. string name = String.Empty;
  515. string value = String.Empty;
  516. StringBuilder sb = new StringBuilder ();
  517. foreach (char c in connectionString) {
  518. switch (c) {
  519. case '\'':
  520. inQuote = !inQuote;
  521. break;
  522. case '"' :
  523. inDQuote = !inDQuote;
  524. break;
  525. case '(':
  526. inParen++;
  527. sb.Append (c);
  528. break;
  529. case ')':
  530. inParen--;
  531. sb.Append (c);
  532. break;
  533. case ';' :
  534. if (!inDQuote && !inQuote) {
  535. if (name != String.Empty && name != null) {
  536. name = name.ToUpper ().Trim ();
  537. value = sb.ToString ().Trim ();
  538. parameters [name] = value;
  539. }
  540. name = String.Empty;
  541. value = String.Empty;
  542. sb = new StringBuilder ();
  543. }
  544. else
  545. sb.Append (c);
  546. break;
  547. case '=' :
  548. if (!inDQuote && !inQuote && inParen == 0) {
  549. name = sb.ToString ();
  550. sb = new StringBuilder ();
  551. }
  552. else
  553. sb.Append (c);
  554. break;
  555. default:
  556. sb.Append (c);
  557. break;
  558. }
  559. }
  560. SetProperties (parameters);
  561. conInfo.ConnectionString = this.connectionString;
  562. if (persistSecurity)
  563. PersistSecurityInfo ();
  564. }
  565. private void SetProperties (NameValueCollection parameters)
  566. {
  567. string value;
  568. foreach (string name in parameters) {
  569. value = parameters[name];
  570. switch (name) {
  571. case "UNICODE":
  572. break;
  573. case "ENLIST":
  574. break;
  575. case "CONNECTION LIFETIME":
  576. // TODO:
  577. break;
  578. case "INTEGRATED SECURITY":
  579. if (!ConvertToBoolean ("integrated security", value))
  580. conInfo.CredentialType = OciCredentialType.RDBMS;
  581. else
  582. conInfo.CredentialType = OciCredentialType.External;
  583. break;
  584. case "PERSIST SECURITY INFO":
  585. if (!ConvertToBoolean ("persist security info", value))
  586. persistSecurityInfo = 1;
  587. else
  588. persistSecurityInfo = 0;
  589. break;
  590. case "MIN POOL SIZE":
  591. minPoolSize = int.Parse (value);
  592. break;
  593. case "MAX POOL SIZE":
  594. maxPoolSize = int.Parse (value);
  595. break;
  596. case "DATA SOURCE" :
  597. case "SERVER" :
  598. conInfo.Database = value;
  599. break;
  600. case "PASSWORD" :
  601. case "PWD" :
  602. conInfo.Password = value;
  603. break;
  604. case "UID" :
  605. case "USER ID" :
  606. conInfo.Username = value;
  607. break;
  608. case "POOLING" :
  609. pooling = ConvertToBoolean("pooling", value);
  610. break;
  611. default:
  612. throw new ArgumentException("Connection parameter not supported: '" + name + "'");
  613. }
  614. }
  615. }
  616. private bool ConvertToBoolean(string key, string value)
  617. {
  618. string upperValue = value.ToUpper();
  619. if (upperValue == "TRUE" || upperValue == "YES") {
  620. return true;
  621. } else if (upperValue == "FALSE" || upperValue == "NO") {
  622. return false;
  623. }
  624. throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
  625. "Invalid value \"{0}\" for key '{1}'.", value, key));
  626. }
  627. #endregion // Methods
  628. public event OracleInfoMessageEventHandler InfoMessage;
  629. #if !NET_2_0
  630. public event StateChangeEventHandler StateChange;
  631. #endif
  632. #if NET_2_0
  633. public override DataTable GetSchema ()
  634. {
  635. throw new NotImplementedException("GetSchema()");
  636. }
  637. public override DataTable GetSchema (String collectionName)
  638. {
  639. throw new NotImplementedException("GetSchema (String collectionName)");
  640. }
  641. public override DataTable GetSchema (String collectionName, string [] restrictionValues)
  642. {
  643. throw new NotImplementedException("GetSchema (String collectionName, string [] restrictionValues)");
  644. }
  645. #endif
  646. }
  647. }