1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
|
//
// System.Data.Common.AbstractDBConnection
//
// Authors:
// Konstantin Triger <kostat@mainsoft.com>
// Boris Kirzner <borisk@mainsoft.com>
//
// (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Globalization;
using System.Data;
using System.Data.ProviderBase;
using System.Data.Configuration;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using Mainsoft.Data.Jdbc.Providers;
using System.Data.Common;
using java.sql;
using javax.sql;
using javax.naming;
using Mainsoft.Data.Configuration;
namespace System.Data.ProviderBase
{
public abstract class AbstractDBConnection : DbConnection, ICloneable
{
#region ObjectNamesHelper
private sealed class ObjectNamesHelper
{
//static readonly Regex NameOrder = new Regex(@"^\s*((\[(?<NAME>(\s*[^\[\]\s])+)\s*\])|(?<NAME>(\w|!|\#|\$)+(\s*(\w|!|\#|\$)+)*))\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
static readonly Regex NameOrder = new Regex(@"^((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
//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);
static readonly Regex SchemaNameOrder = new Regex(@"^((\[(?<SCHEMA>[^\]]+)\])|(?<SCHEMA>[^\.\[\]]+))\s*\.\s*((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
//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);
//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);
static readonly Regex CatalogSchemaNameOrder = new Regex(@"^((\[(?<CATALOG>[^\]]+)\])|(?<CATALOG>[^\.\[\]]+))\s*\.\s*((\[(?<SCHEMA>[^\]]+)\])|(?<SCHEMA>[^\.\[\]]+))\s*\.\s*((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
//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);
//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);
static readonly Regex CatalogNameOrder = new Regex(@"^((\[(?<CATALOG>[^\]]+)\])|(?<CATALOG>[^\.\[\]]+))\s*\.\s*((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
//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);
//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);
static readonly Regex SchemaCatalogNameOrder = new Regex(@"^((\[(?<SCHEMA>[^\]]+)\])|(?<SCHEMA>[^\.\[\]]+))\s*\.\s*((\[(?<CATALOG>[^\]]+)\])|(?<CATALOG>[^\.\[\]]+))\s*\.\s*((\[(?<NAME>[^\]]+)\])|(?<NAME>[^\.\[\]]+))$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
internal static ObjectNameResolver[] GetSyntaxPatterns(AbstractDBConnection connection)
{
ArrayList collection = new ArrayList();
collection.Add(new ObjectNameResolver(NameOrder));
ObjectNameResolversCollection basic = (ObjectNameResolversCollection) ConfigurationSettings.GetConfig ("Mainsoft.Data.Configuration/objectnameresolution");
java.sql.DatabaseMetaData metaData = connection.JdbcConnection.getMetaData();
string productName = metaData.getDatabaseProductName();
foreach(ObjectNameResolver nameResolver in basic) {
if (productName.IndexOf(nameResolver.DbName) != -1) {
collection.Add(nameResolver);
}
}
//defaults
if (metaData.isCatalogAtStart()) {
collection.Add(new ObjectNameResolver(SchemaNameOrder));
collection.Add(new ObjectNameResolver(CatalogNameOrder));
collection.Add(new ObjectNameResolver(CatalogSchemaNameOrder));
collection.Add(new ObjectNameResolver(SchemaCatalogNameOrder));
}
else {
collection.Add(new ObjectNameResolver(CatalogNameOrder));
collection.Add(new ObjectNameResolver(SchemaNameOrder));
collection.Add(new ObjectNameResolver(SchemaCatalogNameOrder));
collection.Add(new ObjectNameResolver(CatalogSchemaNameOrder));
}
return (ObjectNameResolver[])collection.ToArray(typeof(ObjectNameResolver));
}
}
#endregion // ObjectNamesHelper
#region Fields
private const int DEFAULT_TIMEOUT = 15;
private java.sql.Connection _jdbcConnnection;
private ConnectionState _internalState;
private object _internalStateSync = new object();
private string _connectionString = String.Empty;
IConnectionStringDictionary _connectionStringBuilder;
IConnectionProvider _connectionProvider;
private ArrayList _referencedObjects = new ArrayList();
private ObjectNameResolver[] _syntaxPatterns;
#endregion // Fields
#region Constructors
public AbstractDBConnection(string connectionString)
{
_connectionString = connectionString;
}
#endregion // Constructors
#region Properties
public override String ConnectionString
{
get { return _connectionString; }
set {
if (IsOpened) {
throw ExceptionHelper.NotAllowedWhileConnectionOpen("ConnectionString",_internalState);
}
_connectionString = value;
_connectionProvider = null;
_connectionStringBuilder = null;
}
}
public override int ConnectionTimeout
{
get {
string timeoutStr = (string)ConnectionStringBuilder["loginTimeout"];
if (timeoutStr != null && timeoutStr.Length > 0) {
try {
return Convert.ToInt32(timeoutStr);
}
catch(FormatException) {
throw ExceptionHelper.InvalidValueForKey("connect timeout");
}
catch (OverflowException) {
throw ExceptionHelper.InvalidValueForKey("connect timeout");
}
}
return DEFAULT_TIMEOUT;
}
}
public override String Database
{
get {
if ((State & ConnectionState.Open) != 0)
return JdbcConnection.getCatalog();
return (string)ConnectionStringBuilder["DATABASE"];
}
}
public override ConnectionState State
{
get {
try {
if ((JdbcConnection == null) || JdbcConnection.isClosed()) {
// jdbc connection not initialized or closed
if (_internalState == ConnectionState.Closed ) {
return ConnectionState.Closed;
}
}
else {
// jdbc connection is opened
if ((_internalState & ConnectionState.Open) != 0) {
return ConnectionState.Open;
}
}
return ConnectionState.Broken;
}
catch (SQLException) {
return ConnectionState.Broken;
}
}
}
internal bool IsExecuting
{
get {
return ((_internalState & ConnectionState.Executing) != 0);
}
set {
lock(_internalStateSync) {
// to switch to executing, the connection must be in opened
if (value) {
if (_internalState != ConnectionState.Open) {
if (IsFetching) {
throw ExceptionHelper.OpenedReaderExists();
}
throw ExceptionHelper.OpenConnectionRequired("",_internalState);
}
_internalState |= ConnectionState.Executing;
}
else {
if (!IsExecuting) {
throw new InvalidOperationException("Connection : Impossible to tear down from state " + ConnectionState.Executing.ToString() + " while in state " + _internalState.ToString());
}
_internalState &= ~ConnectionState.Executing;
}
}
}
}
internal bool IsFetching
{
get {
return ((_internalState & ConnectionState.Fetching) != 0);
}
set {
lock(_internalStateSync) {
if (value) {
// to switch to fetching connection must be in opened, executing
if (((_internalState & ConnectionState.Open) == 0) || ((_internalState & ConnectionState.Executing) == 0)) {
throw ExceptionHelper.OpenConnectionRequired("",_internalState);
}
_internalState |= ConnectionState.Fetching;
}
else {
if (!IsFetching) {
throw new InvalidOperationException("Connection : Impossible to tear down from state " + ConnectionState.Fetching.ToString() + " while in state " + _internalState.ToString());
}
_internalState &= ~ConnectionState.Fetching;
}
}
}
}
internal bool IsOpened
{
get {
return ((_internalState & ConnectionState.Open) != 0);
}
set {
lock(_internalStateSync) {
if (value) {
// only connecting connection can be opened
if ((_internalState != ConnectionState.Connecting)) {
throw ExceptionHelper.ConnectionAlreadyOpen(_internalState);
}
_internalState |= ConnectionState.Open;
}
else {
if (!IsOpened) {
throw new InvalidOperationException("Connection : Impossible to tear down from state " + ConnectionState.Open.ToString() + " while in state " + _internalState.ToString());
}
_internalState &= ~ConnectionState.Open;
}
}
}
}
internal bool IsConnecting
{
get {
return ((_internalState & ConnectionState.Connecting) != 0);
}
set {
lock(_internalStateSync) {
if (value) {
// to switch to connecting conection must be in closed or in opened
if ((_internalState != ConnectionState.Closed) && (_internalState != ConnectionState.Open)) {
throw ExceptionHelper.ConnectionAlreadyOpen(_internalState);
}
_internalState |= ConnectionState.Connecting;
}
else {
if (!IsConnecting) {
throw new InvalidOperationException("Connection : Impossible to tear down from state " + ConnectionState.Connecting.ToString() + " while in state " + _internalState.ToString());
}
_internalState &= ~ConnectionState.Connecting;
}
}
}
}
public override string DataSource
{
get {
return (string)ConnectionStringBuilder["SERVERNAME"];
}
}
internal ConnectionState InternalState
{
get { return _internalState; }
}
protected internal java.sql.Connection JdbcConnection
{
get { return _jdbcConnnection; }
set { _jdbcConnnection = value; }
}
internal ObjectNameResolver[] SyntaxPatterns
{
get {
if (_syntaxPatterns == null) {
_syntaxPatterns = ObjectNamesHelper.GetSyntaxPatterns(this);
}
return _syntaxPatterns;
}
}
protected internal IConnectionProvider ConnectionProvider {
get {
try {
if (_connectionProvider == null)
_connectionProvider = GetConnectionProvider();
return _connectionProvider;
}
catch(SQLException exp) {
throw CreateException(exp);
}
}
}
protected internal IConnectionStringDictionary ConnectionStringBuilder {
get {
try {
if (_connectionStringBuilder == null)
_connectionStringBuilder = ConnectionProvider.GetConnectionStringBuilder(ConnectionString);
return _connectionStringBuilder;
}
catch(SQLException exp) {
throw CreateException(exp);
}
}
}
protected abstract IConnectionProvider GetConnectionProvider();
static protected IConnectionProvider GetConnectionProvider(string sectionMame, string provider) {
if (provider == null)
throw new ArgumentNullException("provider");
IList providers = (IList) ConfigurationSettings.GetConfig(sectionMame);
if (providers.Count == 0)
throw new ArgumentException("Configuration section is empty.", "sectionName");
for (int i = 0; i < providers.Count; i++) {
IDictionary providerInfo = (IDictionary) providers[i];
string curProvider = (string)providerInfo[ConfigurationConsts.Name];
if (String.Compare(provider, 0, curProvider, 0, provider.Length, StringComparison.OrdinalIgnoreCase) == 0) {
string providerType = (string) providerInfo [ConfigurationConsts.ProviderType];
if (providerType == null || providerType.Length == 0)
return new GenericProvider (providerInfo);
else {
Type t = Type.GetType (providerType);
return (IConnectionProvider) Activator.CreateInstance (t , new object[] {providerInfo});
}
}
}
throw new ArgumentException(
String.Format("Unknown provider name '{0}'", provider), "ConnectionString");
}
#endregion // Properties
#region Methods
// since WS also does not permits dynamically change of login timeout and tomcat does no implements - do not do it at all
//ds.setLoginTimeout(ConnectionTimeout);
protected internal abstract void OnSqlWarning(SQLWarning warning);
protected abstract SystemException CreateException(SQLException e);
protected abstract SystemException CreateException(string message);
public override void Close()
{
ConnectionState orig = State;
try {
ClearReferences();
if (JdbcConnection != null && !JdbcConnection.isClosed()) {
if (!JdbcConnection.getAutoCommit())
JdbcConnection.rollback();
JdbcConnection.close();
}
}
catch (Exception e) {
// suppress exception
#if DEBUG
Console.WriteLine("Exception catched at Conection.Close() : {0}\n{1}\n{2}",e.GetType().FullName,e.Message,e.StackTrace);
#endif
}
finally {
JdbcConnection = null;
lock(_internalStateSync) {
_internalState = ConnectionState.Closed;
}
}
ConnectionState current = State;
if (current != orig)
OnStateChange (new StateChangeEventArgs (orig, current));
}
internal protected virtual void OnSqlException(SQLException exp)
{
throw CreateException(exp);
}
internal void AddReference(object referencedObject)
{ lock(_referencedObjects.SyncRoot) {
_referencedObjects.Add(new WeakReference(referencedObject));
}
}
internal void RemoveReference(object referencedObject)
{
lock(_referencedObjects.SyncRoot) {
for(int i = 0; i < _referencedObjects.Count; i++) {
WeakReference wr = (WeakReference) _referencedObjects[i];
if (wr.IsAlive && (wr.Target == referencedObject)) {
_referencedObjects.RemoveAt(i);
}
}
}
}
private void ClearReferences()
{
ArrayList oldList = _referencedObjects;
_referencedObjects = new ArrayList();
for(int i = 0; i < oldList.Count; i++) {
WeakReference wr = (WeakReference) oldList[i];
if (wr.IsAlive) {
ClearReference(wr.Target);
}
}
}
private void ClearReference(object referencedObject)
{
try {
if (referencedObject is AbstractDbCommand) {
((AbstractDbCommand)referencedObject).CloseInternal();
}
else if (referencedObject is AbstractDataReader) {
((AbstractDataReader)referencedObject).CloseInternal();
}
}
catch (SQLException) {
// suppress exception since it's possible that command or reader are in inconsistent state
}
}
public override void Open()
{
if (_connectionString == null || _connectionString.Length == 0) {
throw ExceptionHelper.ConnectionStringNotInitialized();
}
IsConnecting = true;
try {
if (JdbcConnection != null && !JdbcConnection.isClosed()) {
throw ExceptionHelper.ConnectionAlreadyOpen(_internalState);
}
JdbcConnection = ConnectionProvider.GetConnection (ConnectionStringBuilder);
IsOpened = true;
OnStateChange (new StateChangeEventArgs (ConnectionState.Closed, ConnectionState.Open));
}
catch (SQLWarning warning) {
OnSqlWarning(warning);
}
catch (SQLException exp) {
OnSqlException(exp);
}
finally {
IsConnecting = false;
}
}
public override void ChangeDatabase(String database)
{
IsConnecting = true;
try {
ClearReferences();
java.sql.Connection con = JdbcConnection;
con.setCatalog(database);
// ConnectionStringHelper.UpdateValue(UserParameters,StringManager.GetStringArray("CON_DATABASE"),database);
}
catch (SQLWarning warning) {
OnSqlWarning(warning);
}
catch (SQLException exp) {
throw CreateException(exp);
}
finally {
IsConnecting = false;
}
}
public override string ServerVersion {
get {
// only if the driver support this methods
try {
if (JdbcConnection == null)
return String.Empty;
java.sql.DatabaseMetaData metaData = JdbcConnection.getMetaData();
return metaData.getDatabaseProductVersion();
}
catch (SQLException exp) {
throw CreateException(exp);
}
}
}
internal string JdbcProvider {
get {
// only if the driver support this methods
try {
if (JdbcConnection == null)
return String.Empty;
java.sql.DatabaseMetaData metaData = JdbcConnection.getMetaData();
return metaData.getDriverName() + " " + metaData.getDriverVersion();
}
catch (SQLException exp) {
return String.Empty; //suppress
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing) {
try {
if (JdbcConnection != null && !JdbcConnection.isClosed()) {
JdbcConnection.close();
}
JdbcConnection = null;
}
catch (java.sql.SQLException exp) {
throw CreateException(exp);
}
}
base.Dispose(disposing);
}
internal void ValidateBeginTransaction()
{
if (State != ConnectionState.Open) {
throw new InvalidOperationException(String.Format("{0} requires an open and available Connection. The connection's current state is {1}.", new object[] {"BeginTransaction", State}));
}
if (!JdbcConnection.getAutoCommit()) {
throw new System.InvalidOperationException("Parallel transactions are not supported.");
}
}
internal ArrayList GetProcedureColumns(String procedureString, AbstractDbCommand command)
{
ArrayList col = new ArrayList();
try {
ObjectNameResolver[] nameResolvers = SyntaxPatterns;
java.sql.ResultSet res = null;
string catalog = null;
string schema = null;
string spname = null;
java.sql.DatabaseMetaData metadata = JdbcConnection.getMetaData();
bool storesUpperCaseIdentifiers = false;
bool storesLowerCaseIdentifiers = false;
try {
storesUpperCaseIdentifiers = metadata.storesUpperCaseIdentifiers();
storesLowerCaseIdentifiers = metadata.storesLowerCaseIdentifiers();
}
catch (SQLException e) {
// suppress
}
for(int i=0; i < nameResolvers.Length; i++) {
ObjectNameResolver nameResolver = nameResolvers[i];
Match match = nameResolver.Match(procedureString);
if (match.Success) {
spname = ObjectNameResolver.GetName(match);
schema = ObjectNameResolver.GetSchema(match);
catalog = ObjectNameResolver.GetCatalog(match);
// make all identifiers uppercase or lowercase according to database metadata
if (storesUpperCaseIdentifiers) {
spname = (spname.Length > 0) ? spname.ToUpper() : null;
schema = (schema.Length > 0) ? schema.ToUpper() : null;
catalog = (catalog.Length > 0) ? catalog.ToUpper() : null;
}
else if (storesLowerCaseIdentifiers) {
spname = (spname.Length > 0) ? spname.ToLower() : null;
schema = (schema.Length > 0) ? schema.ToLower() : null;
catalog = (catalog.Length > 0) ? catalog.ToLower() : null;
}
else {
spname = (spname.Length > 0) ? spname : null;
schema = (schema.Length > 0) ? schema : null;
catalog = (catalog.Length > 0) ? catalog : null;
}
// catalog from db is always in correct caps
if (catalog == null) {
catalog = JdbcConnection.getCatalog();
}
try {
// always get the first procedure that db returns
res = metadata.getProcedures(catalog, schema, spname);
if (res.next()) {
catalog = res.getString(1);
schema = res.getString(2);
spname = res.getString(3);
break;
}
spname = null;
}
catch { // suppress exception
return null;
}
finally {
if (res != null) {
res.close();
}
}
}
}
if (spname == null || spname.Length == 0) {
return null;
}
try {
// get procedure columns based o procedure metadata
res = metadata.getProcedureColumns(catalog, schema, spname, null);
while (res.next()) {
// since there is still a possibility that some of the parameters to getProcedureColumn were nulls,
// we need to filter the results with strict matching
if ((res.getString(1) != catalog ) || (res.getString(2) != schema) || (res.getString(3) != spname)) {
continue;
}
AbstractDbParameter parameter = (AbstractDbParameter)command.CreateParameter();
parameter.SetParameterName(res);
parameter.SetParameterDbType(res);
parameter.SetSpecialFeatures(res);
//get parameter direction
short direction = res.getShort("COLUMN_TYPE");
if(direction == 1) //DatabaseMetaData.procedureColumnIn
parameter.Direction = ParameterDirection.Input;
else if(direction == 2) //DatabaseMetaData.procedureColumnInOut
parameter.Direction = ParameterDirection.InputOutput;
else if(direction == 4) //DatabaseMetaData.procedureColumnOut
parameter.Direction = ParameterDirection.Output;
else if(direction == 5) //DatabaseMetaData.procedureColumnReturn
parameter.Direction = ParameterDirection.ReturnValue;
//get parameter precision and scale
parameter.SetParameterPrecisionAndScale(res);
parameter.SetParameterSize(res);
parameter.SetParameterIsNullable(res);
col.Add(parameter);
}
}
finally {
if (res != null) {
res.close();
}
}
}
catch(Exception e) {
//supress
#if DEBUG
Console.WriteLine("Exception catched at AbstractDBConnection.GetProcedureColumns() : {0}\n{1}\n{2}",e.GetType().FullName,e.Message,e.StackTrace);
#endif
}
return col;
}
#endregion // Methods
#region ICloneable Members
public virtual object Clone() {
AbstractDBConnection con = (AbstractDBConnection)MemberwiseClone();
con._internalState = ConnectionState.Closed;
con._internalStateSync = new object();
con._jdbcConnnection = null;
con._referencedObjects = new ArrayList();
con._syntaxPatterns = null;
return con;
}
#endregion
}
}
|