AbstractDBConnection.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. //
  2. // System.Data.Common.AbstractDBConnection
  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.ProviderBase;
  32. using System.Data.Configuration;
  33. using System.Configuration;
  34. using System.Collections;
  35. using System.Collections.Specialized;
  36. using System.Text;
  37. using System.Text.RegularExpressions;
  38. using Mainsoft.Data.Jdbc.Providers;
  39. using System.Data.Common;
  40. using java.sql;
  41. using javax.sql;
  42. using javax.naming;
  43. using Mainsoft.Data.Configuration;
  44. namespace System.Data.ProviderBase
  45. {
  46. public abstract class AbstractDBConnection : DbConnection, ICloneable
  47. {
  48. #region ObjectNamesHelper
  49. private sealed class ObjectNamesHelper
  50. {
  51. //static readonly Regex NameOrder = new Regex(@"^\s*((\[(?<NAME>(\s*[^\[\]\s])+)\s*\])|(?<NAME>(\w|!|\#|\$)+(\s*(\w|!|\#|\$)+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  52. static readonly Regex NameOrder = new Regex(@"^((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  53. //static readonly Regex SchemaNameOrder = new Regex(@"^\s*((\[(?<SCHEMA>(\s*[^\[\]\s])+)\s*\])|(?<SCHEMA>(\w|!|\#|\$)*(\s*(\w|!|\#|\$)+)*))\s*\.\s*((\[(?<NAME>(\s*[^\[\]\s])+)\s*\])|(?<NAME>(\w|!|\#|\$)+(\s*(\w|!|\#|\$)+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  54. static readonly Regex SchemaNameOrder = new Regex(@"^((\[(?<SCHEMA>[^\]]+)\])|(?<SCHEMA>[^\.\[\]]+))\s*\.\s*((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  55. //static readonly Regex CatalogSchemaNameOrder = new Regex(@"^\s*((\[\s*(?<CATALOG>(\s*[^\[\]\s])+)\s*\])|(?<CATALOG>(\w|!|\#|\$)*(\s*(\w|!|\#|\$)+)*))\s*\.\s*((\[(?<SCHEMA>(\s*[^\[\]\s])+)\s*\])|(?<SCHEMA>(\w|!|\#|\$)*(\s*(\w|!|\#|\$)+)*))\s*\.\s*((\[(?<NAME>(\s*[^\[\]\s])+)\s*\])|(?<NAME>(\w|!|\#|\$)+(\s*(\w|!|\#|\$)+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  56. //static readonly Regex CatalogSchemaNameOrder = new Regex(@"^\s*((\[\s*(?<CATALOG>(\s*[^\]\s])+)\s*\])|(?<CATALOG>([^\.\s])*(\s*([^\.\s])+)*))\s*\.\s*((\[(?<SCHEMA>(\s*[^\]\s])+)\s*\])|(?<SCHEMA>([^\.\s])*(\s*([^\.\s])+)*))\s*\.\s*((\[(?<NAME>(\s*[^\]\s])+)\s*\])|(?<NAME>([^\.\s])+(\s*([^\.\s])+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  57. static readonly Regex CatalogSchemaNameOrder = new Regex(@"^((\[(?<CATALOG>[^\]]+)\])|(?<CATALOG>[^\.\[\]]+))\s*\.\s*((\[(?<SCHEMA>[^\]]+)\])|(?<SCHEMA>[^\.\[\]]+))\s*\.\s*((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  58. //static readonly Regex CatalogNameOrder = new Regex(@"^\s*((\[(?<CATALOG>(\s*[^\[\]\s])+)\s*\])|(?<CATALOG>(\w|!|\#|\$)*(\s*(\w|!|\#|\$)+)*))\s*\.\s*((\[(?<NAME>(\s*[^\[\]\s])+)\s*\])|(?<NAME>(\w|!|\#|\$)+(\s*(\w|!|\#|\$)+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  59. //static readonly Regex CatalogNameOrder = new Regex(@"^\s*((\[(?<CATALOG>(\s*[^\]\s])+)\s*\])|(?<CATALOG>([^\.\s])*(\s*([^\.\s])+)*))\s*\.\s*((\[(?<NAME>(\s*[^\]\s])+)\s*\])|(?<NAME>([^\.\s])+(\s*([^\.\s])+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  60. static readonly Regex CatalogNameOrder = new Regex(@"^((\[(?<CATALOG>[^\]]+)\])|(?<CATALOG>[^\.\[\]]+))\s*\.\s*((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  61. //static readonly Regex SchemaCatalogNameOrder = new Regex(@"^\s*((\[\s*(?<SCHEMA>(\s*[^\[\]\s])+)\s*\])|(?<SCHEMA>(\w|!|\#|\$)*(\s*(\w|!|\#|\$)+)*))\s*\.\s*((\[(?<CATALOG>(\s*[^\[\]\s])+)\s*\])|(?<CATALOG>(\w|!|\#|\$)*(\s*(\w|!|\#|\$)+)*))\s*\.\s*((\[(?<NAME>(\s*[^\[\]\s])+)\s*\])|(?<NAME>(\w|!|\#|\$)+(\s*(\w|!|\#|\$)+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  62. //static readonly Regex SchemaCatalogNameOrder = new Regex(@"^\s*((\[\s*(?<SCHEMA>(\s*[^\]\s])+)\s*\])|(?<SCHEMA>([^\.\s])*(\s*([^\.\s])+)*))\s*\.\s*((\[(?<CATALOG>(\s*[^\]\s])+)\s*\])|(?<CATALOG>([^\.\s])*(\s*([^\.\s])+)*))\s*\.\s*((\[(?<NAME>(\s*[^\]\s])+)\s*\])|(?<NAME>([^\.\s])+(\s*([^\.\s])+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  63. static readonly Regex SchemaCatalogNameOrder = new Regex(@"^((\[(?<SCHEMA>[^\]]+)\])|(?<SCHEMA>[^\.\[\]]+))\s*\.\s*((\[(?<CATALOG>[^\]]+)\])|(?<CATALOG>[^\.\[\]]+))\s*\.\s*((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
  64. internal static ObjectNameResolver[] GetSyntaxPatterns(AbstractDBConnection connection)
  65. {
  66. ArrayList collection = new ArrayList();
  67. collection.Add(new ObjectNameResolver(NameOrder));
  68. ObjectNameResolversCollection basic = (ObjectNameResolversCollection)ConfigurationSettings.GetConfig("system.data/objectnameresolution");
  69. DatabaseMetaData metaData = connection.JdbcConnection.getMetaData();
  70. string productName = metaData.getDatabaseProductName();
  71. foreach(ObjectNameResolver nameResolver in basic) {
  72. if (productName.IndexOf(nameResolver.DbName) != -1) {
  73. collection.Add(nameResolver);
  74. }
  75. }
  76. //defaults
  77. if (metaData.isCatalogAtStart()) {
  78. collection.Add(new ObjectNameResolver(SchemaNameOrder));
  79. collection.Add(new ObjectNameResolver(CatalogNameOrder));
  80. collection.Add(new ObjectNameResolver(CatalogSchemaNameOrder));
  81. collection.Add(new ObjectNameResolver(SchemaCatalogNameOrder));
  82. }
  83. else {
  84. collection.Add(new ObjectNameResolver(CatalogNameOrder));
  85. collection.Add(new ObjectNameResolver(SchemaNameOrder));
  86. collection.Add(new ObjectNameResolver(SchemaCatalogNameOrder));
  87. collection.Add(new ObjectNameResolver(CatalogSchemaNameOrder));
  88. }
  89. return (ObjectNameResolver[])collection.ToArray(typeof(ObjectNameResolver));
  90. }
  91. }
  92. #endregion // ObjectNamesHelper
  93. #region Fields
  94. private const int DEFAULT_TIMEOUT = 15;
  95. private java.sql.Connection _jdbcConnnection;
  96. private ConnectionState _internalState;
  97. private object _internalStateSync = new object();
  98. private string _connectionString = String.Empty;
  99. IConnectionStringDictionary _connectionStringBuilder;
  100. IConnectionProvider _connectionProvider;
  101. private ArrayList _referencedObjects = new ArrayList();
  102. private ObjectNameResolver[] _syntaxPatterns;
  103. #endregion // Fields
  104. #region Constructors
  105. public AbstractDBConnection(string connectionString)
  106. {
  107. _connectionString = connectionString;
  108. }
  109. #endregion // Constructors
  110. #region Properties
  111. public override String ConnectionString
  112. {
  113. get { return _connectionString; }
  114. set {
  115. if (IsOpened) {
  116. throw ExceptionHelper.NotAllowedWhileConnectionOpen("ConnectionString",_internalState);
  117. }
  118. _connectionString = value;
  119. _connectionProvider = null;
  120. _connectionStringBuilder = null;
  121. }
  122. }
  123. public override int ConnectionTimeout
  124. {
  125. get {
  126. string timeoutStr = (string)ConnectionStringBuilder["TIMEOUT"];
  127. if (timeoutStr != null && timeoutStr.Length > 0) {
  128. try {
  129. return Convert.ToInt32(timeoutStr);
  130. }
  131. catch(FormatException) {
  132. throw ExceptionHelper.InvalidValueForKey("connect timeout");
  133. }
  134. catch (OverflowException) {
  135. throw ExceptionHelper.InvalidValueForKey("connect timeout");
  136. }
  137. }
  138. return DEFAULT_TIMEOUT;
  139. }
  140. }
  141. public override String Database
  142. {
  143. get {
  144. if ((State & ConnectionState.Open) != 0)
  145. return JdbcConnection.getCatalog();
  146. return (string)ConnectionStringBuilder["CATALOG"];
  147. }
  148. }
  149. public override ConnectionState State
  150. {
  151. get {
  152. try {
  153. if ((JdbcConnection == null) || JdbcConnection.isClosed()) {
  154. // jdbc connection not initialized or closed
  155. if (_internalState == ConnectionState.Closed ) {
  156. return ConnectionState.Closed;
  157. }
  158. }
  159. else {
  160. // jdbc connection is opened
  161. if ((_internalState & ConnectionState.Open) != 0) {
  162. return ConnectionState.Open;
  163. }
  164. }
  165. return ConnectionState.Broken;
  166. }
  167. catch (SQLException) {
  168. return ConnectionState.Broken;
  169. }
  170. }
  171. }
  172. internal bool IsExecuting
  173. {
  174. get {
  175. return ((_internalState & ConnectionState.Executing) != 0);
  176. }
  177. set {
  178. lock(_internalStateSync) {
  179. // to switch to executing, the connection must be in opened
  180. if (value) {
  181. if (_internalState != ConnectionState.Open) {
  182. if (IsFetching) {
  183. throw ExceptionHelper.OpenedReaderExists();
  184. }
  185. throw ExceptionHelper.OpenConnectionRequired("",_internalState);
  186. }
  187. _internalState |= ConnectionState.Executing;
  188. }
  189. else {
  190. if (!IsExecuting) {
  191. throw new InvalidOperationException("Connection : Impossible to tear down from state " + ConnectionState.Executing.ToString() + " while in state " + _internalState.ToString());
  192. }
  193. _internalState &= ~ConnectionState.Executing;
  194. }
  195. }
  196. }
  197. }
  198. internal bool IsFetching
  199. {
  200. get {
  201. return ((_internalState & ConnectionState.Fetching) != 0);
  202. }
  203. set {
  204. lock(_internalStateSync) {
  205. if (value) {
  206. // to switch to fetching connection must be in opened, executing
  207. if (((_internalState & ConnectionState.Open) == 0) || ((_internalState & ConnectionState.Executing) == 0)) {
  208. throw ExceptionHelper.OpenConnectionRequired("",_internalState);
  209. }
  210. _internalState |= ConnectionState.Fetching;
  211. }
  212. else {
  213. if (!IsFetching) {
  214. throw new InvalidOperationException("Connection : Impossible to tear down from state " + ConnectionState.Fetching.ToString() + " while in state " + _internalState.ToString());
  215. }
  216. _internalState &= ~ConnectionState.Fetching;
  217. }
  218. }
  219. }
  220. }
  221. internal bool IsOpened
  222. {
  223. get {
  224. return ((_internalState & ConnectionState.Open) != 0);
  225. }
  226. set {
  227. lock(_internalStateSync) {
  228. if (value) {
  229. // only connecting connection can be opened
  230. if ((_internalState != ConnectionState.Connecting)) {
  231. throw ExceptionHelper.ConnectionAlreadyOpen(_internalState);
  232. }
  233. _internalState |= ConnectionState.Open;
  234. }
  235. else {
  236. if (!IsOpened) {
  237. throw new InvalidOperationException("Connection : Impossible to tear down from state " + ConnectionState.Open.ToString() + " while in state " + _internalState.ToString());
  238. }
  239. _internalState &= ~ConnectionState.Open;
  240. }
  241. }
  242. }
  243. }
  244. internal bool IsConnecting
  245. {
  246. get {
  247. return ((_internalState & ConnectionState.Connecting) != 0);
  248. }
  249. set {
  250. lock(_internalStateSync) {
  251. if (value) {
  252. // to switch to connecting conection must be in closed or in opened
  253. if ((_internalState != ConnectionState.Closed) && (_internalState != ConnectionState.Open)) {
  254. throw ExceptionHelper.ConnectionAlreadyOpen(_internalState);
  255. }
  256. _internalState |= ConnectionState.Connecting;
  257. }
  258. else {
  259. if (!IsConnecting) {
  260. throw new InvalidOperationException("Connection : Impossible to tear down from state " + ConnectionState.Connecting.ToString() + " while in state " + _internalState.ToString());
  261. }
  262. _internalState &= ~ConnectionState.Connecting;
  263. }
  264. }
  265. }
  266. }
  267. public override string DataSource
  268. {
  269. get {
  270. return (string)ConnectionStringBuilder["SERVERNAME"];
  271. }
  272. }
  273. internal ConnectionState InternalState
  274. {
  275. get { return _internalState; }
  276. }
  277. protected internal java.sql.Connection JdbcConnection
  278. {
  279. get { return _jdbcConnnection; }
  280. set { _jdbcConnnection = value; }
  281. }
  282. internal ObjectNameResolver[] SyntaxPatterns
  283. {
  284. get {
  285. if (_syntaxPatterns == null) {
  286. _syntaxPatterns = ObjectNamesHelper.GetSyntaxPatterns(this);
  287. }
  288. return _syntaxPatterns;
  289. }
  290. }
  291. protected internal IConnectionProvider ConnectionProvider {
  292. get {
  293. try {
  294. if (_connectionProvider == null)
  295. _connectionProvider = GetConnectionProvider();
  296. return _connectionProvider;
  297. }
  298. catch(SQLException exp) {
  299. throw CreateException(exp);
  300. }
  301. }
  302. }
  303. protected internal IConnectionStringDictionary ConnectionStringBuilder {
  304. get {
  305. try {
  306. if (_connectionStringBuilder == null)
  307. _connectionStringBuilder = ConnectionProvider.GetConnectionStringBuilder(ConnectionString);
  308. return _connectionStringBuilder;
  309. }
  310. catch(SQLException exp) {
  311. throw CreateException(exp);
  312. }
  313. }
  314. }
  315. protected abstract IConnectionProvider GetConnectionProvider();
  316. #endregion // Properties
  317. #region Methods
  318. // since WS also does not permits dynamically change of login timeout and tomcat does no implements - do not do it at all
  319. //ds.setLoginTimeout(ConnectionTimeout);
  320. protected internal abstract void OnSqlWarning(SQLWarning warning);
  321. protected internal abstract void OnStateChanged(ConnectionState orig, ConnectionState current);
  322. protected abstract SystemException CreateException(SQLException e);
  323. protected abstract SystemException CreateException(string message);
  324. public override void Close()
  325. {
  326. try {
  327. ClearReferences();
  328. if (JdbcConnection != null && !JdbcConnection.isClosed()) {
  329. if (!JdbcConnection.getAutoCommit())
  330. JdbcConnection.rollback();
  331. JdbcConnection.close();
  332. }
  333. }
  334. catch (Exception e) {
  335. // suppress exception
  336. #if DEBUG
  337. Console.WriteLine("Exception catched at Conection.Close() : {0}\n{1}\n{2}",e.GetType().FullName,e.Message,e.StackTrace);
  338. #endif
  339. }
  340. finally {
  341. JdbcConnection = null;
  342. lock(_internalStateSync) {
  343. _internalState = ConnectionState.Closed;
  344. }
  345. }
  346. }
  347. internal protected virtual void OnSqlException(SQLException exp)
  348. {
  349. throw CreateException(exp);
  350. }
  351. internal void AddReference(object referencedObject)
  352. { lock(_referencedObjects.SyncRoot) {
  353. _referencedObjects.Add(new WeakReference(referencedObject));
  354. }
  355. }
  356. internal void RemoveReference(object referencedObject)
  357. {
  358. lock(_referencedObjects.SyncRoot) {
  359. for(int i = 0; i < _referencedObjects.Count; i++) {
  360. WeakReference wr = (WeakReference) _referencedObjects[i];
  361. if (wr.IsAlive && (wr.Target == referencedObject)) {
  362. _referencedObjects.RemoveAt(i);
  363. }
  364. }
  365. }
  366. }
  367. private void ClearReferences()
  368. {
  369. ArrayList oldList = _referencedObjects;
  370. _referencedObjects = new ArrayList();
  371. for(int i = 0; i < oldList.Count; i++) {
  372. WeakReference wr = (WeakReference) oldList[i];
  373. if (wr.IsAlive) {
  374. ClearReference(wr.Target);
  375. }
  376. }
  377. }
  378. private void ClearReference(object referencedObject)
  379. {
  380. try {
  381. if (referencedObject is AbstractDbCommand) {
  382. ((AbstractDbCommand)referencedObject).CloseInternal();
  383. }
  384. else if (referencedObject is AbstractDataReader) {
  385. ((AbstractDataReader)referencedObject).CloseInternal();
  386. }
  387. }
  388. catch (SQLException) {
  389. // suppress exception since it's possible that command or reader are in inconsistent state
  390. }
  391. }
  392. public override void Open()
  393. {
  394. if (_connectionString == null || _connectionString.Length == 0) {
  395. throw ExceptionHelper.ConnectionStringNotInitialized();
  396. }
  397. IsConnecting = true;
  398. try {
  399. if (JdbcConnection != null && !JdbcConnection.isClosed()) {
  400. throw ExceptionHelper.ConnectionAlreadyOpen(_internalState);
  401. }
  402. JdbcConnection = ConnectionProvider.GetConnection (ConnectionStringBuilder);
  403. IsOpened = true;
  404. OnStateChanged(ConnectionState.Closed, ConnectionState.Open);
  405. }
  406. catch (SQLWarning warning) {
  407. OnSqlWarning(warning);
  408. }
  409. catch (SQLException exp) {
  410. OnSqlException(exp);
  411. }
  412. finally {
  413. IsConnecting = false;
  414. }
  415. }
  416. public override void ChangeDatabase(String database)
  417. {
  418. IsConnecting = true;
  419. try {
  420. ClearReferences();
  421. java.sql.Connection con = JdbcConnection;
  422. con.setCatalog(database);
  423. // ConnectionStringHelper.UpdateValue(UserParameters,StringManager.GetStringArray("CON_DATABASE"),database);
  424. }
  425. catch (SQLWarning warning) {
  426. OnSqlWarning(warning);
  427. }
  428. catch (SQLException exp) {
  429. throw CreateException(exp);
  430. }
  431. finally {
  432. IsConnecting = false;
  433. }
  434. }
  435. public override string ServerVersion {
  436. get {
  437. // only if the driver support this methods
  438. try {
  439. if (JdbcConnection == null)
  440. return String.Empty;
  441. DatabaseMetaData metaData = JdbcConnection.getMetaData();
  442. return metaData.getDatabaseProductVersion();
  443. }
  444. catch (SQLException exp) {
  445. throw CreateException(exp);
  446. }
  447. }
  448. }
  449. internal string JdbcProvider {
  450. get {
  451. // only if the driver support this methods
  452. try {
  453. if (JdbcConnection == null)
  454. return String.Empty;
  455. DatabaseMetaData metaData = JdbcConnection.getMetaData();
  456. return metaData.getDriverName() + " " + metaData.getDriverVersion();
  457. }
  458. catch (SQLException exp) {
  459. return String.Empty; //suppress
  460. }
  461. }
  462. }
  463. protected override void Dispose(bool disposing)
  464. {
  465. if (disposing) {
  466. try {
  467. if (JdbcConnection != null && !JdbcConnection.isClosed()) {
  468. JdbcConnection.close();
  469. }
  470. JdbcConnection = null;
  471. }
  472. catch (java.sql.SQLException exp) {
  473. throw CreateException(exp);
  474. }
  475. }
  476. base.Dispose(disposing);
  477. }
  478. internal void ValidateBeginTransaction()
  479. {
  480. if (State != ConnectionState.Open) {
  481. throw new InvalidOperationException(String.Format("{0} requires an open and available Connection. The connection's current state is {1}.", new object[] {"BeginTransaction", State}));
  482. }
  483. if (!JdbcConnection.getAutoCommit()) {
  484. throw new System.InvalidOperationException("Parallel transactions are not supported.");
  485. }
  486. }
  487. internal ArrayList GetProcedureColumns(String procedureString, AbstractDbCommand command)
  488. {
  489. ArrayList col = new ArrayList();
  490. try {
  491. ObjectNameResolver[] nameResolvers = SyntaxPatterns;
  492. ResultSet res = null;
  493. string catalog = null;
  494. string schema = null;
  495. string spname = null;
  496. DatabaseMetaData metadata = JdbcConnection.getMetaData();
  497. bool storesUpperCaseIdentifiers = false;
  498. bool storesLowerCaseIdentifiers = false;
  499. try {
  500. storesUpperCaseIdentifiers = metadata.storesUpperCaseIdentifiers();
  501. storesLowerCaseIdentifiers = metadata.storesLowerCaseIdentifiers();
  502. }
  503. catch (SQLException e) {
  504. // suppress
  505. }
  506. for(int i=0; i < nameResolvers.Length; i++) {
  507. ObjectNameResolver nameResolver = nameResolvers[i];
  508. Match match = nameResolver.Match(procedureString);
  509. if (match.Success) {
  510. spname = ObjectNameResolver.GetName(match);
  511. schema = ObjectNameResolver.GetSchema(match);
  512. catalog = ObjectNameResolver.GetCatalog(match);
  513. // make all identifiers uppercase or lowercase according to database metadata
  514. if (storesUpperCaseIdentifiers) {
  515. spname = (spname.Length > 0) ? spname.ToUpper() : null;
  516. schema = (schema.Length > 0) ? schema.ToUpper() : null;
  517. catalog = (catalog.Length > 0) ? catalog.ToUpper() : null;
  518. }
  519. else if (storesLowerCaseIdentifiers) {
  520. spname = (spname.Length > 0) ? spname.ToLower() : null;
  521. schema = (schema.Length > 0) ? schema.ToLower() : null;
  522. catalog = (catalog.Length > 0) ? catalog.ToLower() : null;
  523. }
  524. else {
  525. spname = (spname.Length > 0) ? spname : null;
  526. schema = (schema.Length > 0) ? schema : null;
  527. catalog = (catalog.Length > 0) ? catalog : null;
  528. }
  529. // catalog from db is always in correct caps
  530. if (catalog == null) {
  531. catalog = JdbcConnection.getCatalog();
  532. }
  533. try {
  534. // always get the first procedure that db returns
  535. res = metadata.getProcedures(catalog, schema, spname);
  536. if (res.next()) {
  537. catalog = res.getString(1);
  538. schema = res.getString(2);
  539. spname = res.getString(3);
  540. break;
  541. }
  542. spname = null;
  543. }
  544. catch { // suppress exception
  545. return null;
  546. }
  547. finally {
  548. if (res != null) {
  549. res.close();
  550. }
  551. }
  552. }
  553. }
  554. if (spname == null || spname.Length == 0) {
  555. return null;
  556. }
  557. try {
  558. // get procedure columns based o procedure metadata
  559. res = metadata.getProcedureColumns(catalog, schema, spname, null);
  560. while (res.next()) {
  561. // since there is still a possibility that some of the parameters to getProcedureColumn were nulls,
  562. // we need to filter the results with strict matching
  563. if ((res.getString(1) != catalog ) || (res.getString(2) != schema) || (res.getString(3) != spname)) {
  564. continue;
  565. }
  566. AbstractDbParameter parameter = (AbstractDbParameter)command.CreateParameter();
  567. parameter.SetParameterName(res);
  568. parameter.SetParameterDbType(res);
  569. parameter.SetSpecialFeatures(res);
  570. //get parameter direction
  571. short direction = res.getShort("COLUMN_TYPE");
  572. if(direction == 1) //DatabaseMetaData.procedureColumnIn
  573. parameter.Direction = ParameterDirection.Input;
  574. else if(direction == 2) //DatabaseMetaData.procedureColumnInOut
  575. parameter.Direction = ParameterDirection.InputOutput;
  576. else if(direction == 4) //DatabaseMetaData.procedureColumnOut
  577. parameter.Direction = ParameterDirection.Output;
  578. else if(direction == 5) //DatabaseMetaData.procedureColumnReturn
  579. parameter.Direction = ParameterDirection.ReturnValue;
  580. //get parameter precision and scale
  581. parameter.SetParameterPrecisionAndScale(res);
  582. parameter.SetParameterSize(res);
  583. parameter.SetParameterIsNullable(res);
  584. col.Add(parameter);
  585. }
  586. }
  587. finally {
  588. if (res != null) {
  589. res.close();
  590. }
  591. }
  592. }
  593. catch(Exception e) {
  594. //supress
  595. #if DEBUG
  596. Console.WriteLine("Exception catched at AbstractDBConnection.GetProcedureColumns() : {0}\n{1}\n{2}",e.GetType().FullName,e.Message,e.StackTrace);
  597. #endif
  598. }
  599. return col;
  600. }
  601. #endregion // Methods
  602. #region ICloneable Members
  603. public virtual object Clone() {
  604. AbstractDBConnection con = (AbstractDBConnection)MemberwiseClone();
  605. con._internalState = ConnectionState.Closed;
  606. con._internalStateSync = new object();
  607. con._jdbcConnnection = null;
  608. con._referencedObjects = new ArrayList();
  609. con._syntaxPatterns = null;
  610. return con;
  611. }
  612. #endregion
  613. }
  614. }