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
|
/*
* Firebird ADO.NET Data provider for .NET and Mono
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* Copyright (c) 2002, 2005 Carlos Guzman Alvarez
* All Rights Reserved.
*/
using System;
using System.Collections;
using System.Text;
using FirebirdSql.Data.Common;
namespace FirebirdSql.Data.Embedded
{
internal sealed class FesStatement : StatementBase
{
#region Fields
private int handle;
private FesDatabase db;
private FesTransaction transaction;
private Descriptor parameters;
private Descriptor fields;
private StatementState state;
private DbStatementType statementType;
private bool allRowsFetched;
private Queue outputParams;
private int recordsAffected;
#endregion
#region Properties
public override IDatabase DB
{
get { return this.db; }
set { this.db = (FesDatabase)value; }
}
public override ITransaction Transaction
{
get { return this.transaction; }
set
{
if (this.transaction != value)
{
if (this.TransactionUpdate != null && this.transaction != null)
{
this.transaction.Update -= this.TransactionUpdate;
this.TransactionUpdate = null;
}
if (value == null)
{
this.transaction = null;
}
else
{
this.transaction = (FesTransaction)value;
this.TransactionUpdate = new TransactionUpdateEventHandler(this.TransactionUpdated);
this.transaction.Update += this.TransactionUpdate;
}
}
}
}
public override Descriptor Parameters
{
get { return this.parameters; }
set { this.parameters = value; }
}
public override Descriptor Fields
{
get { return this.fields; }
}
public override int RecordsAffected
{
get { return this.recordsAffected; }
}
public override bool IsPrepared
{
get
{
if (this.state == StatementState.Deallocated ||
this.state == StatementState.Error)
{
return false;
}
else
{
return true;
}
}
}
public override DbStatementType StatementType
{
get { return this.statementType; }
set { this.statementType = value; }
}
public override StatementState State
{
get { return this.state; }
set { this.state = value; }
}
public override int FetchSize
{
get { return 200; }
set { ; }
}
#endregion
#region Constructors
public FesStatement(IDatabase db) : this(db, null)
{
}
public FesStatement(IDatabase db, ITransaction transaction)
{
if (!(db is FesDatabase))
{
throw new ArgumentException("Specified argument is not of FesDatabase type.");
}
this.recordsAffected = -1;
this.db = (FesDatabase)db;
this.outputParams = new Queue();
if (transaction != null)
{
this.Transaction = transaction;
}
GC.SuppressFinalize(this);
}
#endregion
#region IDisposable Methods
protected override void Dispose(bool disposing)
{
if (!this.IsDisposed)
{
try
{
// release any unmanaged resources
this.Release();
// release any managed resources
if (disposing)
{
this.Clear();
this.db = null;
this.fields = null;
this.parameters = null;
this.transaction = null;
this.outputParams = null;
this.allRowsFetched = false;
this.recordsAffected = 0;
this.state = StatementState.Deallocated;
this.handle = 0;
}
}
finally
{
base.Dispose(disposing);
}
}
}
#endregion
#region Blob Creation Metods
public override BlobBase CreateBlob()
{
return new FesBlob(this.db, this.transaction);
}
public override BlobBase CreateBlob(long blobId)
{
return new FesBlob(this.db, this.transaction, blobId);
}
#endregion
#region Array Creation Methods
public override ArrayBase CreateArray(ArrayDesc descriptor)
{
return new FesArray(descriptor);
}
public override ArrayBase CreateArray(string tableName, string fieldName)
{
return new FesArray(this.db, this.transaction, tableName, fieldName);
}
public override ArrayBase CreateArray(long handle, string tableName, string fieldName)
{
return new FesArray(this.db, this.transaction, handle, tableName, fieldName);
}
#endregion
#region Methods
public override void Prepare(string commandText)
{
// Clear data
this.Clear();
this.parameters = null;
this.fields = null;
lock (this.db)
{
if (this.state == StatementState.Deallocated)
{
// Allocate statement
this.Allocate();
}
// Marshal structures to pointer
XsqldaMarshaler marshaler = XsqldaMarshaler.GetInstance();
// Setup fields structure
this.fields = new Descriptor(1);
IntPtr sqlda = marshaler.MarshalManagedToNative(this.db.Charset, this.fields);
int[] statusVector = FesConnection.GetNewStatusVector();
int trHandle = this.transaction.Handle;
int stmtHandle = this.handle;
byte[] buffer = this.db.Charset.GetBytes(commandText);
FbClient.isc_dsql_prepare(
statusVector,
ref trHandle,
ref stmtHandle,
(short)buffer.Length,
buffer,
this.db.Dialect,
sqlda);
// Marshal Pointer
Descriptor descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda);
// Free memory
marshaler.CleanUpNativeData(ref sqlda);
// Parse status vector
this.db.ParseStatusVector(statusVector);
// Describe fields
this.fields = descriptor;
if (this.fields.ActualCount > 0 &&
this.fields.ActualCount != this.fields.Count)
{
this.Describe();
}
else
{
if (this.fields.ActualCount == 0)
{
this.fields = new Descriptor(0);
}
}
// Reset actual field values
this.fields.ResetValues();
// Get Statement type
this.statementType = this.GetStatementType();
// Update state
this.state = StatementState.Prepared;
}
}
public override void Execute()
{
if (this.state == StatementState.Deallocated)
{
throw new InvalidOperationException("Statment is not correctly created.");
}
lock (this.db)
{
// Marshal structures to pointer
XsqldaMarshaler marshaler = XsqldaMarshaler.GetInstance();
IntPtr inSqlda = IntPtr.Zero;
IntPtr outSqlda = IntPtr.Zero;
if (this.parameters != null)
{
inSqlda = marshaler.MarshalManagedToNative(this.db.Charset, this.parameters);
}
if (this.statementType == DbStatementType.StoredProcedure)
{
this.Fields.ResetValues();
outSqlda = marshaler.MarshalManagedToNative(this.db.Charset, this.fields);
}
int[] statusVector = FesConnection.GetNewStatusVector();
int trHandle = this.transaction.Handle;
int stmtHandle = this.handle;
FbClient.isc_dsql_execute2(
statusVector,
ref trHandle,
ref stmtHandle,
IscCodes.SQLDA_VERSION1,
inSqlda,
outSqlda);
if (outSqlda != IntPtr.Zero)
{
Descriptor descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, outSqlda);
// This would be an Execute procedure
DbValue[] values = new DbValue[descriptor.Count];
for (int i = 0; i < values.Length; i++)
{
values[i] = new DbValue(this, descriptor[i]);
}
this.outputParams.Enqueue(values);
}
// Free memory
marshaler.CleanUpNativeData(ref inSqlda);
marshaler.CleanUpNativeData(ref outSqlda);
this.db.ParseStatusVector(statusVector);
this.UpdateRecordsAffected();
this.state = StatementState.Executed;
}
}
public override DbValue[] Fetch()
{
DbValue[] row = null;
if (this.state == StatementState.Deallocated)
{
throw new InvalidOperationException("Statment is not correctly created.");
}
if (this.statementType != DbStatementType.Select &&
this.statementType != DbStatementType.SelectForUpdate)
{
return null;
}
lock (this.db)
{
if (!this.allRowsFetched)
{
// Marshal structures to pointer
XsqldaMarshaler marshaler = XsqldaMarshaler.GetInstance();
// Reset actual field values
this.fields.ResetValues();
IntPtr sqlda = marshaler.MarshalManagedToNative(this.db.Charset, fields);
int[] statusVector = FesConnection.GetNewStatusVector();
int stmtHandle = this.handle;
int status = FbClient.isc_dsql_fetch(
statusVector,
ref stmtHandle,
IscCodes.SQLDA_VERSION1,
sqlda);
// Obtain values
Descriptor rowDesc = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda);
if (this.fields.Count == rowDesc.Count)
{
// Try to preserve Array Handle information
for (int i = 0; i < this.fields.Count; i++)
{
if (this.fields[i].IsArray() &&
this.fields[i].ArrayHandle != null)
{
rowDesc[i].ArrayHandle = this.fields[i].ArrayHandle;
}
}
}
this.fields = rowDesc;
// Free memory
marshaler.CleanUpNativeData(ref sqlda);
// Parse status vector
this.db.ParseStatusVector(statusVector);
if (status == 100)
{
this.allRowsFetched = true;
}
else
{
// Set row values
row = new DbValue[this.fields.ActualCount];
for (int i = 0; i < row.Length; i++)
{
row[i] = new DbValue(this, this.fields[i]);
}
}
}
}
return row;
}
public override DbValue[] GetOuputParameters()
{
if (this.outputParams != null && this.outputParams.Count > 0)
{
return (DbValue[])this.outputParams.Dequeue();
}
return null;
}
public override void Describe()
{
lock (this.db)
{
// Update structure
this.fields = new Descriptor(this.fields.ActualCount);
// Marshal structures to pointer
XsqldaMarshaler marshaler = XsqldaMarshaler.GetInstance();
IntPtr sqlda = marshaler.MarshalManagedToNative(this.db.Charset, fields);
int[] statusVector = FesConnection.GetNewStatusVector();
int stmtHandle = this.handle;
FbClient.isc_dsql_describe(
statusVector,
ref stmtHandle,
IscCodes.SQLDA_VERSION1,
sqlda);
// Marshal Pointer
Descriptor descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda);
// Free memory
marshaler.CleanUpNativeData(ref sqlda);
// Parse status vector
this.db.ParseStatusVector(statusVector);
// Update field descriptor
this.fields = descriptor;
}
}
public override void DescribeParameters()
{
lock (this.db)
{
// Marshal structures to pointer
XsqldaMarshaler marshaler = XsqldaMarshaler.GetInstance();
this.parameters = new Descriptor(1);
IntPtr sqlda = marshaler.MarshalManagedToNative(this.db.Charset, parameters);
int[] statusVector = FesConnection.GetNewStatusVector();
int stmtHandle = this.handle;
FbClient.isc_dsql_describe_bind(
statusVector,
ref stmtHandle,
IscCodes.SQLDA_VERSION1,
sqlda);
Descriptor descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda);
// Parse status vector
this.db.ParseStatusVector(statusVector);
if (descriptor.ActualCount != 0 &&
descriptor.Count != descriptor.ActualCount)
{
short n = descriptor.ActualCount;
descriptor = new Descriptor(n);
// Fre memory
marshaler.CleanUpNativeData(ref sqlda);
// Marshal new structure
sqlda = marshaler.MarshalManagedToNative(this.db.Charset, descriptor);
FbClient.isc_dsql_describe_bind(
statusVector,
ref stmtHandle,
IscCodes.SQLDA_VERSION1,
sqlda);
descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda);
// Free memory
marshaler.CleanUpNativeData(ref sqlda);
// Parse status vector
this.db.ParseStatusVector(statusVector);
}
else
{
if (descriptor.ActualCount == 0)
{
descriptor = new Descriptor(0);
}
}
// Free memory
if (sqlda != IntPtr.Zero)
{
marshaler.CleanUpNativeData(ref sqlda);
}
// Update parameter descriptor
this.parameters = descriptor;
}
}
public override byte[] GetSqlInfo(byte[] items, int bufferLength)
{
lock (this.db)
{
byte[] buffer = new byte[bufferLength];
int[] statusVector = FesConnection.GetNewStatusVector();
int stmtHandle = this.handle;
FbClient.isc_dsql_sql_info(
statusVector,
ref stmtHandle,
(short)items.Length,
items,
(short)bufferLength,
buffer);
this.db.ParseStatusVector(statusVector);
return buffer;
}
}
#endregion
#region Protected Methods
protected override void Free(int option)
{
// Does not seem to be possible or necessary to close
// an execute procedure statement.
if (this.StatementType == DbStatementType.StoredProcedure &&
option == IscCodes.DSQL_close)
{
return;
}
lock (this.db)
{
int[] statusVector = FesConnection.GetNewStatusVector();
int stmtHandle = this.handle;
FbClient.isc_dsql_free_statement(
statusVector,
ref stmtHandle,
(short)option);
this.handle = stmtHandle;
// Reset statement information
if (option == IscCodes.DSQL_drop)
{
this.parameters = null;
this.fields = null;
}
this.Clear();
this.allRowsFetched = false;
this.db.ParseStatusVector(statusVector);
}
}
protected override void TransactionUpdated(object sender, EventArgs e)
{
lock (this)
{
if (this.Transaction != null && this.TransactionUpdate != null)
{
this.Transaction.Update -= this.TransactionUpdate;
}
this.Clear();
this.State = StatementState.Closed;
this.TransactionUpdate = null;
this.allRowsFetched = false;
}
}
#endregion
#region Private Methods
private void Clear()
{
if (this.outputParams != null && this.outputParams.Count > 0)
{
this.outputParams.Clear();
}
}
private void Allocate()
{
lock (this.db)
{
int[] statusVector = FesConnection.GetNewStatusVector();
int dbHandle = this.db.Handle;
int stmtHandle = this.handle;
FbClient.isc_dsql_allocate_statement(
statusVector,
ref dbHandle,
ref stmtHandle);
this.db.ParseStatusVector(statusVector);
this.handle = stmtHandle;
this.allRowsFetched = false;
this.state = StatementState.Allocated;
this.statementType = DbStatementType.None;
}
}
private void UpdateRecordsAffected()
{
if (this.StatementType == DbStatementType.Insert ||
this.StatementType == DbStatementType.Delete ||
this.StatementType == DbStatementType.Update ||
this.StatementType == DbStatementType.StoredProcedure)
{
this.recordsAffected = this.GetRecordsAffected();
}
else
{
this.recordsAffected = -1;
}
}
#endregion
}
}
|