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 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPostgreSQLQuery.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkPostgreSQLQuery.h"
#include "vtkObjectFactory.h"
#include "vtkPostgreSQLDatabase.h"
#include "vtkStringArray.h"
#include "vtkVariant.h"
#include "vtkVariantArray.h"
#include "vtkPostgreSQLDatabasePrivate.h"
#include <cassert>
#include <limits> // man, I hope all platforms have this nowadays
#include <sstream>
#define BEGIN_TRANSACTION "BEGIN"
#define COMMIT_TRANSACTION "COMMIT"
#define ROLLBACK_TRANSACTION "ROLLBACK"
#define DECLARE_CONVERTER(TargetType) vtkVariant ConvertStringTo##TargetType(bool, const char *);
DECLARE_CONVERTER(Boolean);
DECLARE_CONVERTER(SignedChar);
DECLARE_CONVERTER(UnsignedChar);
DECLARE_CONVERTER(SignedShort);
DECLARE_CONVERTER(UnsignedShort);
DECLARE_CONVERTER(SignedInt);
DECLARE_CONVERTER(UnsignedInt);
DECLARE_CONVERTER(SignedLong);
DECLARE_CONVERTER(UnsignedLong);
DECLARE_CONVERTER(Float);
DECLARE_CONVERTER(Double);
DECLARE_CONVERTER(VtkIdType);
DECLARE_CONVERTER(String);
DECLARE_CONVERTER(SignedLongLong);
DECLARE_CONVERTER(UnsignedLongLong);
template<typename T>
void ConvertFromNetworkOrder(T &target, const char *rawBytes)
{
for (unsigned int i = 0; i < sizeof(T); ++i)
{
int targetByte = sizeof(T) - (i+1);
target |= (rawBytes[i] << (8*targetByte));
}
}
// ----------------------------------------------------------------------
vtkStandardNewMacro(vtkPostgreSQLQuery);
// ----------------------------------------------------------------------
class vtkPostgreSQLQueryPrivate
{
public:
vtkPostgreSQLQueryPrivate()
{
this->QueryResults = NULL;
this->CurrentRow = -1;
}
~vtkPostgreSQLQueryPrivate()
{
if (this->QueryResults)
{
PQclear(this->QueryResults);
}
}
PGresult *QueryResults;
int CurrentRow;
};
// ----------------------------------------------------------------------
vtkVariant vtkPostgreSQLQuery::DataValue( vtkIdType column )
{
if ( this->IsActive() == false )
{
vtkWarningMacro( "DataValue() called on inactive query" );
return vtkVariant();
}
else if ( column < 0 || column >= this->GetNumberOfFields() )
{
vtkWarningMacro( "DataValue() called with out-of-range column index " << column );
return vtkVariant();
}
else if (this->QueryInternals->CurrentRow < 0)
{
vtkWarningMacro( "DataValue() cannot be called before advancing to the first row with NextRow()." );
return vtkVariant();
}
// Since null is independent of data type, check that next
if (PQgetisnull(this->QueryInternals->QueryResults,
this->QueryInternals->CurrentRow,
column))
{
return vtkVariant();
}
int colType = this->GetFieldType( column );
bool isBinary = this->IsColumnBinary(column);
const char *rawData = this->GetColumnRawData(column);
switch ( colType )
{
case VTK_VOID:
return vtkVariant();
case VTK_BIT:
{
return ConvertStringToBoolean(isBinary, rawData);
}
case VTK_CHAR:
case VTK_SIGNED_CHAR:
{
return ConvertStringToSignedChar(isBinary, rawData);
}
case VTK_UNSIGNED_CHAR:
{
return ConvertStringToUnsignedChar(isBinary, rawData);
}
case VTK_SHORT:
{
return ConvertStringToSignedShort(isBinary, rawData);
}
case VTK_UNSIGNED_SHORT:
{
return ConvertStringToUnsignedShort(isBinary, rawData);
}
case VTK_INT:
{
return ConvertStringToSignedInt(isBinary, rawData);
}
case VTK_UNSIGNED_INT:
{
return ConvertStringToUnsignedInt(isBinary, rawData);
}
case VTK_LONG:
{
return ConvertStringToSignedLong(isBinary, rawData);
}
case VTK_UNSIGNED_LONG:
{
return ConvertStringToUnsignedLong(isBinary, rawData);
}
case VTK_LONG_LONG:
{
return ConvertStringToSignedLongLong(isBinary, rawData);
}
case VTK_UNSIGNED_LONG_LONG:
{
return ConvertStringToUnsignedLongLong(isBinary, rawData);
}
case VTK_FLOAT:
{
return ConvertStringToFloat(isBinary, rawData);
}
case VTK_DOUBLE:
{
return ConvertStringToDouble(isBinary, rawData);
}
case VTK_ID_TYPE:
{
return ConvertStringToVtkIdType(isBinary, rawData);
}
case VTK_STRING:
{
return vtkVariant(rawData);
}
default:
{
return vtkVariant();
}
} // end of switch on column type
} // end of DataValue(int column)
// ----------------------------------------------------------------------
vtkPostgreSQLQuery::vtkPostgreSQLQuery()
{
this->TransactionInProgress = false;
this->LastErrorText = NULL;
this->QueryInternals = NULL;
}
// ----------------------------------------------------------------------
vtkPostgreSQLQuery::~vtkPostgreSQLQuery()
{
this->SetDatabase(NULL);
this->SetLastErrorText(NULL);
delete this->QueryInternals;
}
// ----------------------------------------------------------------------
void vtkPostgreSQLQuery::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Transaction in progress: "
<< (this->TransactionInProgress ? "YES" : "NO") << "\n";
os << indent << "Last error message: "
<< (this->LastErrorText ? this->LastErrorText : "(null)")
<< "\n";
os << indent << "Internals: ";
if (this->QueryInternals)
{
os << this->QueryInternals;
}
else
{
os << "(null)";
}
os << "\n";
}
// ----------------------------------------------------------------------
bool vtkPostgreSQLQuery::Execute()
{
if ( this->Query == 0 )
{
vtkErrorMacro( "Cannot execute before a query has been set." );
return false;
}
vtkPostgreSQLDatabase* db = vtkPostgreSQLDatabase::SafeDownCast( this->Database );
assert( db );
// If a query is already in progress clear out its results so we can
// begin anew.
if (this->QueryInternals)
{
this->DeleteQueryResults();
}
if (!db->IsOpen())
{
this->SetLastErrorText("Cannot execute query. Database connection is closed.");
vtkErrorMacro(<<"Cannot execute query. Database connection is closed.");
this->Active = false;
return false;
}
this->QueryInternals = new vtkPostgreSQLQueryPrivate;
this->QueryInternals->QueryResults = PQexec(db->Connection->Connection,
this->Query);
bool returnStatus;
switch (PQresultStatus(this->QueryInternals->QueryResults))
{
case PGRES_EMPTY_QUERY:
{
returnStatus = true;
this->Active = false;
this->DeleteQueryResults();
vtkWarningMacro(<<"Query string was set but empty.");
this->SetLastErrorText(0);
}; break;
case PGRES_COMMAND_OK: // success on a command returning no data
{
returnStatus = true;
this->Active = true;
this->DeleteQueryResults();
this->SetLastErrorText(0);
}; break;
case PGRES_TUPLES_OK:
{
returnStatus = true;
this->Active = true;
this->SetLastErrorText(0);
}; break;
case PGRES_BAD_RESPONSE:
{
returnStatus = false;
this->Active = false;
this->DeleteQueryResults();
this->SetLastErrorText("Incomprehensible server response");
}; break;
case PGRES_FATAL_ERROR:
{
returnStatus = false;
this->Active = false;
this->SetLastErrorText(PQerrorMessage(db->Connection->Connection));
vtkErrorMacro(<<"Fatal error during query: "
<< this->GetLastErrorText());
this->DeleteQueryResults();
}; break;
default:
{
returnStatus = false;
this->Active = false;
std::ostringstream sbuf;
sbuf << "Unhandled server response: ";
sbuf << PQresStatus(PQresultStatus(this->QueryInternals->QueryResults));
this->SetLastErrorText(sbuf.str().c_str());
vtkErrorMacro(<< "Unhandled server response: "
<< this->GetLastErrorText());
this->DeleteQueryResults();
}; break;
}
return returnStatus;
}
// ----------------------------------------------------------------------
int vtkPostgreSQLQuery::GetNumberOfFields()
{
if ( ! this->Active || ! this->QueryInternals )
{
vtkErrorMacro( "Query is not active!" );
return 0;
}
return PQnfields(this->QueryInternals->QueryResults);
}
// ----------------------------------------------------------------------
const char* vtkPostgreSQLQuery::GetFieldName( int column )
{
if ( ! this->Active || ! this->QueryInternals->QueryResults )
{
vtkErrorMacro( "Query is not active!" );
return 0;
}
else if ( column < 0 || column >= this->GetNumberOfFields() )
{
vtkErrorMacro( "Illegal field index " << column );
return 0;
}
return PQfname(this->QueryInternals->QueryResults, column);
}
// ----------------------------------------------------------------------
int vtkPostgreSQLQuery::GetFieldType( int column )
{
if ( ! this->Active || ! this->QueryInternals )
{
vtkErrorMacro( "Query is not active!" );
return -1;
}
else if ( column < 0 || column >= this->GetNumberOfFields() )
{
vtkErrorMacro( "Illegal field index " << column );
return -1;
}
vtkPostgreSQLDatabase *db =
vtkPostgreSQLDatabase::SafeDownCast(this->Database);
if (!db)
{
vtkErrorMacro(<<"No database! How did this happen?");
return -1;
}
return db->Connection->GetVTKTypeFromOID(PQftype(this->QueryInternals->QueryResults,
column));
}
// ----------------------------------------------------------------------
bool vtkPostgreSQLQuery::NextRow()
{
if ( ! this->IsActive() || ! this->QueryInternals )
{
vtkErrorMacro( "Query is not active!" );
return false;
}
if (this->QueryInternals->CurrentRow < (this->GetNumberOfRows() - 1))
{
++ this->QueryInternals->CurrentRow;
return true;
}
else
{
return false;
}
}
// ----------------------------------------------------------------------
const char * vtkPostgreSQLQuery::GetLastErrorText()
{
if ( ! this->Database )
{
return "No database";
}
return this->LastErrorText;
}
// ----------------------------------------------------------------------
vtkStdString vtkPostgreSQLQuery::EscapeString( vtkStdString s, bool addSurroundingQuotes )
{
vtkStdString retval;
if ( addSurroundingQuotes )
{
retval = "'";
}
vtkPostgreSQLDatabase* db = static_cast<vtkPostgreSQLDatabase*>( this->Database );
if ( db && db->Connection )
{
char *escaped = new char[2 * s.size() + 1];
int error;
PQescapeStringConn(db->Connection->Connection,
escaped,
s.c_str(),
s.size(),
&error);
retval.append(escaped);
delete [] escaped;
if (error)
{
vtkErrorMacro(<<"Error while escaping string. Expect the result to be unusable.");
}
}
else
{
retval.append( this->Superclass::EscapeString( s, false ) );
}
if ( addSurroundingQuotes )
{
retval += "'";
}
return retval;
}
// ----------------------------------------------------------------------
bool vtkPostgreSQLQuery::HasError()
{
if ( ! this->Database )
{
return false;
}
return this->LastErrorText != 0;
}
// ----------------------------------------------------------------------
bool vtkPostgreSQLQuery::BeginTransaction()
{
if (this->TransactionInProgress)
{
vtkErrorMacro(<<"Cannot start a transaction. One is already in progress.");
return false;
}
vtkPostgreSQLDatabase* db = vtkPostgreSQLDatabase::SafeDownCast( this->Database );
assert( db );
bool status;
PGresult *result = PQexec(db->Connection->Connection, BEGIN_TRANSACTION);
switch (PQresultStatus(result))
{
case PGRES_COMMAND_OK:
{
this->SetLastErrorText(0);
this->TransactionInProgress = true;
status = true;
}; break;
case PGRES_FATAL_ERROR:
{
this->SetLastErrorText(PQresultErrorMessage(result));
vtkErrorMacro(<< "Error in BeginTransaction: "
<< this->GetLastErrorText());
status = false;
}; break;
default:
{
this->SetLastErrorText(PQresultErrorMessage(result));
vtkWarningMacro(<<"Unexpected return code "
<< PQresultStatus(result) << " ("
<< PQresStatus(PQresultStatus(result))
<< ") with error message "
<< (this->LastErrorText ? this->LastErrorText
: "(null)"));
status = false;
}; break;
}
PQclear(result);
return status;
}
// ----------------------------------------------------------------------
bool vtkPostgreSQLQuery::CommitTransaction()
{
if (!this->TransactionInProgress)
{
vtkErrorMacro(<<"Cannot commit: no transaction is in progress.");
return false;
}
vtkPostgreSQLDatabase *db = vtkPostgreSQLDatabase::SafeDownCast(this->Database);
assert(db);
PGresult *result = PQexec(db->Connection->Connection, COMMIT_TRANSACTION);
bool status;
switch (PQresultStatus(result))
{
case PGRES_COMMAND_OK:
{
this->SetLastErrorText(0);
this->TransactionInProgress = false;
status = true;
}; break;
case PGRES_FATAL_ERROR:
{
this->SetLastErrorText(PQresultErrorMessage(result));
vtkErrorMacro(<< "Error in CommitTransaction: "
<< this->GetLastErrorText());
this->TransactionInProgress = false;
status = false;
}; break;
default:
{
this->SetLastErrorText(PQresultErrorMessage(result));
vtkWarningMacro(<<"Unexpected return code "
<< PQresultStatus(result) << " ("
<< PQresStatus(PQresultStatus(result))
<< ") with error message "
<< (this->LastErrorText ? this->LastErrorText
: "(null)"));
this->TransactionInProgress = false;
status = false;
}; break;
}
PQclear(result);
return status;
}
// ----------------------------------------------------------------------
bool vtkPostgreSQLQuery::RollbackTransaction()
{
if (!this->TransactionInProgress)
{
vtkErrorMacro(<<"Cannot rollback: no transaction is in progress.");
return false;
}
vtkPostgreSQLDatabase *db = vtkPostgreSQLDatabase::SafeDownCast(this->Database);
assert(db);
PGresult *result = PQexec(db->Connection->Connection, ROLLBACK_TRANSACTION);
bool status;
switch (PQresultStatus(result))
{
case PGRES_COMMAND_OK:
{
this->SetLastErrorText(0);
this->TransactionInProgress = false;
status = true;
}; break;
case PGRES_FATAL_ERROR:
{
this->SetLastErrorText(PQresultErrorMessage(result));
vtkErrorMacro(<< "Error in RollbackTransaction: "
<< this->GetLastErrorText());
this->TransactionInProgress = false;
status = false;
}; break;
default:
{
this->SetLastErrorText(PQresultErrorMessage(result));
vtkWarningMacro(<<"Unexpected return code "
<< PQresultStatus(result) << " ("
<< PQresStatus(PQresultStatus(result))
<< ") with error message "
<< (this->LastErrorText ? this->LastErrorText
: "(null)"));
this->TransactionInProgress = false;
status = false;
}; break;
}
PQclear(result);
return status;
}
// ----------------------------------------------------------------------
void
vtkPostgreSQLQuery::DeleteQueryResults()
{
this->Active = false;
delete this->QueryInternals;
this->QueryInternals = NULL;
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToBoolean(bool, const char *rawData)
{
// Since there are only a few possibilities I'm going to check
// them all by hand.
switch (rawData[0])
{
case 'T':
case 't':
case 'Y':
case 'y':
case '1':
case 1:
{
return vtkVariant(true);
}
case 'F':
case 'f':
case 'N':
case 'n':
case '0':
case 0:
{
return vtkVariant(false);
}
default:
{
vtkGenericWarningMacro(<<"Unable to convert raw data to boolean. Data length is "
<< strlen(rawData) << " and string is '"
<< rawData << "'");
return vtkVariant();
}
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToSignedChar(bool isBinary, const char *rawData)
{
if (isBinary)
{
return vtkVariant(rawData[0]);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToChar());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToUnsignedChar(bool isBinary, const char *rawData)
{
if (isBinary)
{
return vtkVariant(static_cast<unsigned char>(rawData[0]));
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToUnsignedChar());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToSignedShort(bool isBinary, const char *rawData)
{
if (isBinary)
{
short result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToShort());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToUnsignedShort(bool isBinary, const char *rawData)
{
if (isBinary)
{
unsigned short result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToUnsignedShort());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToSignedInt(bool isBinary, const char *rawData)
{
if (isBinary)
{
int result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToInt());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToUnsignedInt(bool isBinary, const char *rawData)
{
if (isBinary)
{
unsigned int result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToUnsignedInt());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToSignedLong(bool isBinary, const char *rawData)
{
if (isBinary)
{
signed long result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToLong());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToUnsignedLong(bool isBinary, const char *rawData)
{
if (isBinary)
{
unsigned long result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToLong());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToSignedLongLong(bool isBinary, const char *rawData)
{
if (isBinary)
{
long long result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToLongLong());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToUnsignedLongLong(bool isBinary, const char *rawData)
{
if (isBinary)
{
unsigned long long result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
vtkVariant converter(rawData);
return vtkVariant(converter.ToUnsignedLongLong());
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToFloat(bool isBinary, const char *rawData)
{
if (isBinary)
{
// As of PostgreSQL version 8.3.0, libpq transmits a float in network
// byte order -- that is, it reinterprets the bits as an unsigned int
// and then transmits them that way. This... frightens me. It assumes
// that both sender and recipient use IEEE floats. Still, I'm not sure
// there's any other good way to do it.
unsigned int intResult = 0;
ConvertFromNetworkOrder(intResult, rawData);
// This is the idiom that libpq uses internally to convert between the
// two types.
union {
unsigned int i;
float f;
} swap;
swap.i = intResult;
float floatResult = swap.f;
return vtkVariant(floatResult);
}
else
{
vtkStdString rawString(rawData);
float finalResult;
// Catch NaN
if (rawData[0] == 'N' || rawData[0] == 'n')
{
if (std::numeric_limits<float>::has_quiet_NaN)
{
finalResult = std::numeric_limits<float>::quiet_NaN();
}
else
{
// C99 defines a NAN macro. If it's there, that solves our problem.
#if defined(NAN)
finalResult = NAN;
#else
float zero = 0.0;
finalResult = zero / zero;
#endif
}
}
else if (rawString == "Infinity")
{
if (std::numeric_limits<float>::has_infinity)
{
finalResult = std::numeric_limits<float>::infinity();
}
else
{
finalResult = VTK_FLOAT_MAX;
}
}
else if (rawString == "-Infinity")
{
if (std::numeric_limits<float>::has_infinity)
{
finalResult = - std::numeric_limits<float>::infinity();
}
else
{
finalResult = -VTK_FLOAT_MAX;
}
}
else
{
// hurray, it's an ordinary float
vtkVariant converter(rawData);
finalResult = converter.ToFloat();
}
return vtkVariant(finalResult);
} // end of handling string representation
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToVtkIdType(bool isBinary, const char *rawData)
{
if (isBinary)
{
vtkIdType result = 0;
ConvertFromNetworkOrder(result, rawData);
return vtkVariant(result);
}
else
{
std::stringstream convertStream;
convertStream.str(rawData);
vtkIdType result;
convertStream >> result;
return vtkVariant(result);
}
}
// ----------------------------------------------------------------------
vtkVariant ConvertStringToDouble(bool isBinary, const char *rawData)
{
if (isBinary)
{
// As of PostgreSQL version 8.3.0, libpq transmits a float in network
// byte order -- that is, it reinterprets the bits as an unsigned int
// and then transmits them that way. This... frightens me. It assumes
// that both sender and recipient use IEEE floats. Still, I'm not sure
// there's any other good way to do it.
// Let's hope that we always have a 64-bit type.
vtkTypeUInt64 intResult = 0;
ConvertFromNetworkOrder(intResult, rawData);
union {
vtkTypeUInt64 i;
double d;
} swap;
swap.i = intResult;
return vtkVariant(swap.d);
}
else
{
double finalResult;
vtkStdString rawString(rawData);
// Catch NaN
if (rawData[0] == 'N' || rawData[0] == 'n')
{
if (std::numeric_limits<double>::has_quiet_NaN)
{
finalResult = std::numeric_limits<double>::quiet_NaN();
}
else
{
// C99 defines a NAN macro. If it's there, that solves our problem.
#if defined(NAN)
finalResult = NAN;
#else
double zero = 0.0;
finalResult = zero / zero;
#endif
}
}
else if (rawString == "Infinity")
{
if (std::numeric_limits<double>::has_infinity)
{
finalResult = std::numeric_limits<double>::infinity();
}
else
{
finalResult = VTK_DOUBLE_MAX;
}
}
else if (rawString == "-Infinity")
{
if (std::numeric_limits<double>::has_infinity)
{
finalResult = - std::numeric_limits<double>::infinity();
}
else
{
finalResult = -VTK_DOUBLE_MAX;
}
}
else
{
// hurray, it's an ordinary double
vtkVariant converter(rawData);
finalResult = converter.ToDouble();
}
return vtkVariant(finalResult);
} // end of handling string representation
}
// ----------------------------------------------------------------------
bool
vtkPostgreSQLQuery::IsColumnBinary(int whichColumn)
{
if ((!this->Active) ||
(!this->Database) ||
(!this->QueryInternals->QueryResults))
{
vtkWarningMacro(<<"No active query!");
return false;
}
else if (whichColumn < 0 || whichColumn >= this->GetNumberOfFields())
{
vtkWarningMacro(<<"Illegal column index " << whichColumn);
return false;
}
else
{
return (PQfformat(this->QueryInternals->QueryResults, whichColumn) == 1);
}
}
// ----------------------------------------------------------------------
const char *
vtkPostgreSQLQuery::GetColumnRawData(int whichColumn)
{
if ((!this->Active) ||
(!this->Database) ||
(!this->QueryInternals->QueryResults))
{
vtkWarningMacro(<<"No active query!");
return NULL;
}
else if (whichColumn < 0 || whichColumn >= this->GetNumberOfFields())
{
vtkWarningMacro(<<"Illegal column index " << whichColumn);
return NULL;
}
else
{
return PQgetvalue(this->QueryInternals->QueryResults,
this->QueryInternals->CurrentRow,
whichColumn);
}
}
// ----------------------------------------------------------------------
int
vtkPostgreSQLQuery::GetNumberOfRows()
{
if (!this->Database ||
!this->Database->IsOpen() ||
!this->QueryInternals ||
!this->Active)
{
vtkWarningMacro(<<"No active query. Cannot retrieve number of rows.");
return 0;
}
else
{
return PQntuples(this->QueryInternals->QueryResults);
}
}
|