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 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// pgFunction.cpp - function class
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "frm/menu.h"
#include "utils/pgfeatures.h"
#include "utils/misc.h"
#include "schema/pgFunction.h"
#include "frm/frmMain.h"
#include "frm/frmReport.h"
#include "frm/frmHint.h"
pgFunction::pgFunction(pgSchema *newSchema, const wxString &newName)
: pgSchemaObject(newSchema, functionFactory, newName)
{
}
pgFunction::pgFunction(pgSchema *newSchema, pgaFactory &factory, const wxString &newName)
: pgSchemaObject(newSchema, factory, newName)
{
}
pgTriggerFunction::pgTriggerFunction(pgSchema *newSchema, const wxString &newName)
: pgFunction(newSchema, triggerFunctionFactory, newName)
{
}
pgProcedure::pgProcedure(pgSchema *newSchema, const wxString &newName)
: pgFunction(newSchema, procedureFactory, newName)
{
}
wxString pgFunction::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on function");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing function");
message += wxT(" ") + GetName();
break;
case DROPINCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop function \"%s\" including all objects that depend on it?"),
GetFullIdentifier().c_str());
break;
case DROPEXCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop function \"%s\"?"),
GetFullIdentifier().c_str());
break;
case DROPCASCADETITLE:
message = _("Drop function cascaded?");
break;
case DROPTITLE:
message = _("Drop function?");
break;
case PROPERTIESREPORT:
message = _("Function properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("Function properties");
break;
case DDLREPORT:
message = _("Function DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("Function DDL");
break;
case DEPENDENCIESREPORT:
message = _("Function dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("Function dependencies");
break;
case DEPENDENTSREPORT:
message = _("Function dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("Function dependents");
break;
}
return message;
}
wxString pgTriggerFunction::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on trigger function");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing trigger function");
message += wxT(" ") + GetName();
break;
case DROPINCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop trigger function \"%s\" including all objects that depend on it?"),
GetFullIdentifier().c_str());
break;
case DROPEXCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop trigger function \"%s\"?"),
GetFullIdentifier().c_str());
break;
case DROPCASCADETITLE:
message = _("Drop trigger function cascaded?");
break;
case DROPTITLE:
message = _("Drop trigger function?");
break;
case PROPERTIESREPORT:
message = _("Trigger function properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("Trigger function properties");
break;
case DDLREPORT:
message = _("Trigger function DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("Trigger function DDL");
break;
case DEPENDENCIESREPORT:
message = _("Trigger function dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("Trigger function dependencies");
break;
case DEPENDENTSREPORT:
message = _("Trigger function dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("Trigger function dependents");
break;
}
return message;
}
wxString pgProcedure::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on procedure");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing procedure");
message += wxT(" ") + GetName();
break;
case DROPINCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop procedure \"%s\" including all objects that depend on it?"),
GetFullIdentifier().c_str());
break;
case DROPEXCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop procedure \"%s\"?"),
GetFullIdentifier().c_str());
break;
case DROPCASCADETITLE:
message = _("Drop procedure cascaded?");
break;
case DROPTITLE:
message = _("Drop procedure?");
break;
case PROPERTIESREPORT:
message = _("Procedure properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("Procedure properties");
break;
case DDLREPORT:
message = _("Procedure DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("Procedure DDL");
break;
case DEPENDENCIESREPORT:
message = _("Procedure dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("Procedure dependencies");
break;
case DEPENDENTSREPORT:
message = _("Procedure dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("Procedure dependents");
break;
}
return message;
}
void pgFunction::ShowStatistics(frmMain *form, ctlListView *statistics)
{
if (GetConnection()->BackendMinimumVersion(8, 4))
{
wxString sql = wxT("SELECT calls AS ") + qtIdent(_("Number of calls")) +
wxT(", total_time AS ") + qtIdent(_("Total Time")) +
wxT(", self_time AS ") + qtIdent(_("Self Time")) +
wxT(" FROM pg_stat_user_functions") +
wxT(" WHERE funcid = ") + NumToStr(GetOid());
DisplayStatistics(statistics, sql);
}
}
bool pgFunction::IsUpToDate()
{
wxString sql = wxT("SELECT xmin FROM pg_proc WHERE oid = ") + this->GetOidStr();
if (!this->GetDatabase()->GetConnection() || this->GetDatabase()->ExecuteScalar(sql) != NumToStr(GetXid()))
return false;
else
return true;
}
bool pgFunction::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
wxString sql = wxT("DROP FUNCTION ") + this->GetSchema()->GetQuotedIdentifier() + wxT(".") + this->GetQuotedIdentifier() + wxT("(") + GetArgSigList() + wxT(")");
if (cascaded)
sql += wxT(" CASCADE");
return GetDatabase()->ExecuteVoid(sql);
}
bool pgFunction::ResetStats()
{
wxString sql = wxT("SELECT pg_stat_reset_single_function_counters(")
+ NumToStr(this->GetOid())
+ wxT(")");
return GetDatabase()->ExecuteVoid(sql);
}
wxString pgFunction::GetFullName()
{
return GetName() + wxT("(") + GetArgSigList() + wxT(")");
}
wxString pgProcedure::GetFullName()
{
if (GetArgSigList().IsEmpty())
return GetName();
else
return GetName() + wxT("(") + GetArgSigList() + wxT(")");
}
wxString pgFunction::GetSql(ctlTree *browser)
{
if (sql.IsNull())
{
wxString qtName = GetQuotedFullIdentifier() + wxT("(") + GetArgListWithNames() + wxT(")");
wxString qtSig = GetQuotedFullIdentifier() + wxT("(") + GetArgSigList() + wxT(")");
sql = wxT("-- Function: ") + qtSig + wxT("\n\n")
+ wxT("-- DROP FUNCTION ") + qtSig + wxT(";")
+ wxT("\n\nCREATE OR REPLACE FUNCTION ") + qtName;
// Use Oracle style syntax for edb-spl functions
if (GetLanguage() == wxT("edbspl") && GetProcType() == 2)
{
sql += wxT("\nRETURN ");
sql += GetReturnType();
sql += wxT(" AS");
if (GetSource().StartsWith(wxT("\n")))
sql += GetSource();
else
sql += wxT("\n") + GetSource();
}
else
{
sql += wxT("\n RETURNS ");
if (GetReturnAsSet() && !GetReturnType().StartsWith(wxT("TABLE")))
sql += wxT("SETOF ");
sql += GetReturnType();
sql += wxT(" AS\n");
if (GetLanguage().IsSameAs(wxT("C"), false))
{
sql += qtDbString(GetBin()) + wxT(", ") + qtDbString(GetSource());
}
else
{
if (GetConnection()->BackendMinimumVersion(7, 5))
sql += qtDbStringDollar(GetSource());
else
sql += qtDbString(GetSource());
}
sql += wxT("\n LANGUAGE ") + GetLanguage() + wxT(" ");
if (GetConnection()->BackendMinimumVersion(8, 4) && GetIsWindow())
sql += wxT("WINDOW ");
sql += GetVolatility();
if (GetConnection()->BackendMinimumVersion(9, 2) && GetIsLeakProof())
sql += wxT(" LEAKPROOF");
if (GetIsStrict())
sql += wxT(" STRICT");
if (GetSecureDefiner())
sql += wxT(" SECURITY DEFINER");
// PostgreSQL 8.3+ cost/row estimations
if (GetConnection()->BackendMinimumVersion(8, 3))
{
sql += wxT("\n COST ") + NumToStr(GetCost());
if (GetReturnAsSet())
sql += wxT("\n ROWS ") + NumToStr(GetRows());
}
}
if (!sql.Strip(wxString::both).EndsWith(wxT(";")))
sql += wxT(";");
size_t i;
for (i = 0 ; i < configList.GetCount() ; i++)
{
if (configList.Item(i).BeforeFirst('=') != wxT("search_path") &&
configList.Item(i).BeforeFirst('=') != wxT("temp_tablespaces"))
sql += wxT("\nALTER FUNCTION ") + qtSig
+ wxT(" SET ") + configList.Item(i).BeforeFirst('=') + wxT("='") + configList.Item(i).AfterFirst('=') + wxT("';\n");
else
sql += wxT("\nALTER FUNCTION ") + qtSig
+ wxT(" SET ") + configList.Item(i).BeforeFirst('=') + wxT("=") + configList.Item(i).AfterFirst('=') + wxT(";\n");
}
sql += wxT("\n")
+ GetOwnerSql(8, 0, wxT("FUNCTION ") + qtSig)
+ GetGrant(wxT("X"), wxT("FUNCTION ") + qtSig);
if (!GetComment().IsNull())
{
sql += wxT("COMMENT ON FUNCTION ") + qtSig
+ wxT(" IS ") + qtDbString(GetComment()) + wxT(";\n");
}
if (GetConnection()->BackendMinimumVersion(9, 1))
sql += GetSeqLabelsSql();
}
return sql;
}
void pgFunction::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
if (properties)
{
CreateListColumns(properties);
properties->AppendItem(_("Name"), GetName());
properties->AppendItem(_("OID"), GetOid());
properties->AppendItem(_("Owner"), GetOwner());
properties->AppendItem(_("Argument count"), GetArgCount());
properties->AppendItem(_("Arguments"), GetArgListWithNames());
properties->AppendItem(_("Signature arguments"), GetArgSigList());
if (!GetIsProcedure())
properties->AppendItem(_("Return type"), GetReturnType());
properties->AppendItem(_("Language"), GetLanguage());
properties->AppendYesNoItem(_("Returns a set?"), GetReturnAsSet());
if (GetLanguage().IsSameAs(wxT("C"), false))
{
properties->AppendItem(_("Object file"), GetBin());
properties->AppendItem(_("Link symbol"), GetSource());
}
else
properties->AppendItem(_("Source"), firstLineOnly(GetSource()));
if (GetConnection()->BackendMinimumVersion(8, 3))
{
properties->AppendItem(_("Estimated cost"), GetCost());
if (GetReturnAsSet())
properties->AppendItem(_("Estimated rows"), GetRows());
}
properties->AppendItem(_("Volatility"), GetVolatility());
if (GetConnection()->BackendMinimumVersion(9, 2))
properties->AppendYesNoItem(_("Leak proof?"), GetIsLeakProof());
properties->AppendYesNoItem(_("Security of definer?"), GetSecureDefiner());
properties->AppendYesNoItem(_("Strict?"), GetIsStrict());
if (GetConnection()->BackendMinimumVersion(8, 4))
properties->AppendYesNoItem(_("Window?"), GetIsWindow());
size_t i;
for (i = 0 ; i < configList.GetCount() ; i++)
{
wxString item = configList.Item(i);
properties->AppendItem(item.BeforeFirst('='), item.AfterFirst('='));
}
properties->AppendItem(_("ACL"), GetAcl());
properties->AppendYesNoItem(_("System function?"), GetSystemObject());
properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
if (!GetLabels().IsEmpty())
{
wxArrayString seclabels = GetProviderLabelArray();
if (seclabels.GetCount() > 0)
{
for (unsigned int index = 0 ; index < seclabels.GetCount() - 1 ; index += 2)
{
properties->AppendItem(seclabels.Item(index), seclabels.Item(index + 1));
}
}
}
}
}
void pgFunction::ShowHint(frmMain *form, bool force)
{
wxArrayString hints;
hints.Add(HINT_OBJECT_EDITING);
frmHint::ShowHint((wxWindow *)form, hints, GetFullIdentifier(), force);
}
wxString pgProcedure::GetSql(ctlTree *browser)
{
if (!GetConnection()->EdbMinimumVersion(8, 0))
return pgFunction::GetSql(browser);
if (sql.IsNull())
{
wxString qtName, qtSig;
if (GetArgListWithNames().IsEmpty())
{
qtName = GetQuotedFullIdentifier();
qtSig = GetQuotedFullIdentifier();
}
else
{
qtName = GetQuotedFullIdentifier() + wxT("(") + GetArgListWithNames() + wxT(")");
qtSig = GetQuotedFullIdentifier() + wxT("(") + GetArgSigList() + wxT(")");
}
sql = wxT("-- Procedure: ") + qtSig + wxT("\n\n")
+ wxT("-- DROP PROCEDURE ") + qtSig + wxT(";")
+ wxT("\n\nCREATE OR REPLACE PROCEDURE ") + qtName;
sql += wxT(" AS")
+ GetSource()
+ wxT("\n\n")
+ GetGrant(wxT("X"), wxT("PROCEDURE ") + qtSig);
if (!GetComment().IsNull())
{
sql += wxT("COMMENT ON PROCEDURE ") + GetQuotedFullIdentifier()
+ wxT(" IS ") + qtDbString(GetComment()) + wxT(";\n");
}
}
return sql;
}
bool pgProcedure::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
if (!GetConnection()->EdbMinimumVersion(8, 0))
return pgFunction::DropObject(frame, browser, cascaded);
wxString sql = wxT("DROP PROCEDURE ") + this->GetSchema()->GetQuotedIdentifier() + wxT(".") + this->GetQuotedIdentifier();
return GetDatabase()->ExecuteVoid(sql);
}
wxString pgFunction::GetArgListWithNames()
{
wxString args;
for (unsigned int i = 0; i < argTypesArray.Count(); i++)
{
/*
* All Table arguments lies at the end of the list
* Do not include them as the part of the argument list
*/
if (argModesArray.Item(i) == wxT("TABLE"))
break;
if (args.Length() > 0)
args += wxT(", ");
wxString arg;
if (GetIsProcedure())
{
if (!argModesArray.Item(i).IsEmpty())
{
arg += qtIdent(argNamesArray.Item(i));
}
else
{
if (!argNamesArray.Item(i).IsEmpty())
arg += qtIdent(argNamesArray.Item(i));
arg += wxT(" ") + argModesArray.Item(i);
}
if (!argModesArray.Item(i).IsEmpty())
{
if (arg.IsEmpty())
arg += argModesArray.Item(i);
else
arg += wxT(" ") + argModesArray.Item(i);
}
}
else
{
if (!argModesArray.Item(i).IsEmpty())
arg += argModesArray.Item(i);
if (!argNamesArray.Item(i).IsEmpty())
{
if (arg.IsEmpty())
arg += qtIdent(argNamesArray.Item(i));
else
arg += wxT(" ") + qtIdent(argNamesArray.Item(i));
}
}
if (!arg.IsEmpty())
arg += wxT(" ") + argTypesArray.Item(i);
else
arg += argTypesArray.Item(i);
// Parameter default value
if (GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS)
&& !argDefsArray.IsEmpty())
{
if ((argModesArray.Item(i).IsEmpty() ||
argModesArray.Item(i) == wxT("IN") ||
// 'edbspl' does not support default value with
// INOUT parameter
(argModesArray.Item(i) == wxT("INOUT") &&
GetLanguage() != wxT("edbspl")) ||
argModesArray.Item(i) == wxT("VARIADIC")) &&
!argDefsArray.Item(i).IsEmpty() &&
i < argDefsArray.Count())
{
arg += wxT(" DEFAULT ") + argDefsArray.Item(i);
}
}
args += arg;
}
return args;
}
// Return the signature arguments list. If forScript = true, we format the list
// appropriately for use in a SELECT script.
wxString pgFunction::GetArgSigList(const bool forScript)
{
wxString args;
for (unsigned int i = 0; i < argTypesArray.Count(); i++)
{
// OUT parameters are not considered part of the signature, except for EDB-SPL,
// although this is not true for EDB AS90 onwards..
if (argModesArray.Item(i) != wxT("OUT") && argModesArray.Item(i) != wxT("TABLE"))
{
if (args.Length() > 0)
{
if (forScript)
args += wxT(",\n");
else
args += wxT(", ");
}
if (forScript)
args += wxT(" <") + argTypesArray.Item(i) + wxT(">");
else
args += argTypesArray.Item(i);
}
else
{
if (GetLanguage() == wxT("edbspl") && argModesArray.Item(i) != wxT("TABLE") &&
!this->GetConnection()->EdbMinimumVersion(9, 0))
{
if (args.Length() > 0)
{
if (forScript)
args += wxT(",\n");
else
args += wxT(", ");
}
if (forScript)
args += wxT(" <") + argTypesArray.Item(i) + wxT(">");
else
args += argTypesArray.Item(i);
}
}
}
return args;
}
pgFunction *pgFunctionFactory::AppendFunctions(pgObject *obj, pgSchema *schema, ctlTree *browser, const wxString &restriction)
{
// Caches
cacheMap typeCache, exprCache;
pgFunction *function = 0;
wxString argNamesCol, argDefsCol, proConfigCol, proType, seclab;
if (obj->GetConnection()->BackendMinimumVersion(8, 0))
argNamesCol = wxT("proargnames, ");
if (obj->GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS) && !obj->GetConnection()->BackendMinimumVersion(8, 4))
argDefsCol = wxT("proargdefvals, COALESCE(substring(array_dims(proargdefvals), E'1:(.*)\\]')::integer, 0) AS pronargdefaults, ");
if (obj->GetConnection()->BackendMinimumVersion(8, 4))
argDefsCol = wxT("pg_get_expr(proargdefaults, 'pg_catalog.pg_class'::regclass) AS proargdefaultvals, pronargdefaults, ");
if (obj->GetConnection()->BackendMinimumVersion(8, 3))
proConfigCol = wxT("proconfig, ");
if (obj->GetConnection()->EdbMinimumVersion(8, 1))
proType = wxT("protype, ");
if (obj->GetConnection()->BackendMinimumVersion(9, 1))
{
seclab = wxT(",\n")
wxT("(SELECT array_agg(label) FROM pg_seclabels sl1 WHERE sl1.objoid=pr.oid) AS labels,\n")
wxT("(SELECT array_agg(provider) FROM pg_seclabels sl2 WHERE sl2.objoid=pr.oid) AS providers");
}
pgSet *functions = obj->GetDatabase()->ExecuteSet(
wxT("SELECT pr.oid, pr.xmin, pr.*, format_type(TYP.oid, NULL) AS typname, typns.nspname AS typnsp, lanname, ") +
argNamesCol + argDefsCol + proConfigCol + proType +
wxT(" pg_get_userbyid(proowner) as funcowner, description") + seclab + wxT("\n")
wxT(" FROM pg_proc pr\n")
wxT(" JOIN pg_type typ ON typ.oid=prorettype\n")
wxT(" JOIN pg_namespace typns ON typns.oid=typ.typnamespace\n")
wxT(" JOIN pg_language lng ON lng.oid=prolang\n")
wxT(" LEFT OUTER JOIN pg_description des ON (des.objoid=pr.oid AND des.classoid='pg_proc'::regclass)\n")
+ restriction +
wxT(" ORDER BY proname"));
pgSet *types = obj->GetDatabase()->ExecuteSet(wxT(
"SELECT oid, format_type(oid, NULL) AS typname FROM pg_type"));
if (types)
{
while(!types->Eof())
{
typeCache[types->GetVal(wxT("oid"))] = types->GetVal(wxT("typname"));
types->MoveNext();
}
}
if (functions)
{
while (!functions->Eof())
{
bool isProcedure = false;
wxString lanname = functions->GetVal(wxT("lanname"));
wxString typname = functions->GetVal(wxT("typname"));
// Is this an EDB Stored Procedure?
if (obj->GetConnection()->EdbMinimumVersion(8, 1))
{
wxString protype = functions->GetVal(wxT("protype"));
if (protype == wxT("1"))
isProcedure = true;
}
else if (obj->GetConnection()->EdbMinimumVersion(8, 0) &&
lanname == wxT("edbspl") && typname == wxT("void"))
isProcedure = true;
// Create the new object
if (isProcedure)
function = new pgProcedure(schema, functions->GetVal(wxT("proname")));
else if (typname == wxT("\"trigger\"") || typname == wxT("trigger") || typname == wxT("event_trigger") || typname == wxT("\"event_trigger\""))
function = new pgTriggerFunction(schema, functions->GetVal(wxT("proname")));
else
function = new pgFunction(schema, functions->GetVal(wxT("proname")));
// Tokenize the arguments
wxStringTokenizer argTypesTkz(wxEmptyString), argModesTkz(wxEmptyString);
wxString tmp;
wxArrayString argNamesArray;
wxArrayString argDefValArray;
// We always have types
argTypesTkz.SetString(functions->GetVal(wxT("proargtypes")));
// We only have names in 8.0+
if (obj->GetConnection()->BackendMinimumVersion(8, 0))
{
tmp = functions->GetVal(wxT("proargnames"));
if (!tmp.IsEmpty())
getArrayFromCommaSeparatedList(tmp.Mid(1, tmp.Length() - 2), argNamesArray);
}
// EDB 8.0 had modes in pg_proc.proargdirs
if (!obj->GetConnection()->EdbMinimumVersion(8, 1) && isProcedure)
argModesTkz.SetString(functions->GetVal(wxT("proargdirs")));
if (obj->GetConnection()->EdbMinimumVersion(8, 1))
function->iSetProcType(functions->GetLong(wxT("protype")));
// EDB 8.1 and PostgreSQL 8.1 have modes in pg_proc.proargmodes
if (obj->GetConnection()->BackendMinimumVersion(8, 1))
{
tmp = functions->GetVal(wxT("proallargtypes"));
if (!tmp.IsEmpty())
argTypesTkz.SetString(tmp.Mid(1, tmp.Length() - 2), wxT(","));
tmp = functions->GetVal(wxT("proargmodes"));
if (!tmp.IsEmpty())
argModesTkz.SetString(tmp.Mid(1, tmp.Length() - 2), wxT(","));
}
// EDB 8.3: Function defaults
if (obj->GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS) &&
!obj->GetConnection()->BackendMinimumVersion(8, 4))
{
tmp = functions->GetVal(wxT("proargdefvals"));
if (!tmp.IsEmpty())
getArrayFromCommaSeparatedList(tmp.Mid(1, tmp.Length() - 2), argDefValArray);
function->iSetArgDefValCount(functions->GetLong(wxT("pronargdefaults")));
}
if (obj->GetConnection()->BackendMinimumVersion(8, 4))
{
tmp = functions->GetVal(wxT("proargdefaultvals"));
getArrayFromCommaSeparatedList(tmp, argDefValArray);
function->iSetArgDefValCount(functions->GetLong(wxT("pronargdefaults")));
// Check if it is a window function
function->iSetIsWindow(functions->GetBool(wxT("proiswindow")));
}
else
function->iSetIsWindow(false);
// Now iterate the arguments and build the arrays
wxString type, name, mode;
size_t nArgsIN = 0;
size_t nArgNames = 0;
while (argTypesTkz.HasMoreTokens())
{
if (nArgNames < argNamesArray.GetCount())
{
name = argNamesArray[nArgNames++];
}
else
name = wxEmptyString;
if (!name.IsEmpty())
{
// Now add the name, stripping the quotes and \" if
// necessary.
if (name[0] == '"')
name = name.Mid(1, name.Length() - 2);
name.Replace(wxT("\\\""), wxT("\""));
// In EDBAS 90, if an SPL-function has both an OUT-parameter
// and a return value (which is not possible on PostgreSQL otherwise),
// the return value is transformed into an extra OUT-parameter
// named "_retval_"
if (obj->GetConnection()->EdbMinimumVersion(9, 0))
{
if (name == wxT("_retval_"))
{
type = argTypesTkz.GetNextToken();
// this will be the return type for this object
function->iSetReturnType(typeCache[type]);
// consume uniformly, mode will definitely be "OUT"
mode = argModesTkz.GetNextToken();
continue;
}
}
function->iAddArgName(name);
}
else
function->iAddArgName(wxEmptyString);
// Add the arg type. This is a type oid, so
// look it up in the hashmap
type = argTypesTkz.GetNextToken();
function->iAddArgType(typeCache[type]);
// Now the mode
mode = argModesTkz.GetNextToken();
if (!mode.IsEmpty())
{
if (mode == wxT('o') || mode == wxT("2"))
mode = wxT("OUT");
else if (mode == wxT("b") || mode == wxT("3"))
{
if (isProcedure)
mode = wxT("IN OUT");
else
{
mode = wxT("INOUT");
// 'edbspl' does not support default values for the
// INOUT parameters.
if (lanname != wxT("edbspl"))
{
nArgsIN++;
}
}
}
else if (mode == wxT("v"))
{
mode = wxT("VARIADIC");
nArgsIN++;
}
else if (mode == wxT("t"))
mode = wxT("TABLE");
else
{
mode = wxT("IN");
nArgsIN++;
}
function->iAddArgMode(mode);
}
else
{
function->iAddArgMode(wxEmptyString);
nArgsIN++;
}
}
function->iSetArgCount(functions->GetLong(wxT("pronargs")));
wxString strReturnTableArgs;
// Process default values
size_t currINindex = 0;
for (size_t index = 0; index < function->GetArgModesArray().Count(); index++)
{
wxString def = wxEmptyString;
if(function->GetArgModesArray()[index].IsEmpty() ||
function->GetArgModesArray()[index] == wxT("IN") ||
(function->GetArgModesArray()[index] == wxT("INOUT") &&
lanname != wxT("edbspl")) ||
function->GetArgModesArray()[index] == wxT("VARIADIC"))
{
if (!argDefValArray.IsEmpty() && nArgsIN <= argDefValArray.GetCount())
{
def = argDefValArray[currINindex++];
if (!def.IsEmpty() && def != wxT("-"))
{
// Only EDB 8.3 does not support get the default value
// using pg_get_expr directly
if (function->GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS) &&
!function->GetConnection()->BackendMinimumVersion(8, 4))
{
// Check the cache first - if we don't have a value, get it and cache for next time
wxString val = exprCache[def];
if (val == wxEmptyString)
{
val = obj->GetDatabase()->ExecuteScalar(
wxT("SELECT pg_get_expr('") + def.Mid(1, def.Length() - 2) + wxT("', 'pg_catalog.pg_class'::regclass)"));
exprCache[def] = val;
}
def = val;
}
}
else
{
def = wxEmptyString;
}
}
nArgsIN--;
}
else if(function->GetConnection()->BackendMinimumVersion(8, 4) &&
function->GetArgModesArray()[index] == wxT("TABLE"))
{
if (strReturnTableArgs.Length() > 0)
strReturnTableArgs += wxT(", ");
wxString strName = function->GetArgNamesArray()[index];
if (!strName.IsEmpty())
strReturnTableArgs += qtIdent(strName) + wxT(" ");
strReturnTableArgs += function->GetArgTypesArray()[index];
}
function->iAddArgDef(def);
}
function->iSetOid(functions->GetOid(wxT("oid")));
function->iSetXid(functions->GetOid(wxT("xmin")));
if (browser)
function->UpdateSchema(browser, functions->GetOid(wxT("pronamespace")));
function->iSetOwner(functions->GetVal(wxT("funcowner")));
function->iSetAcl(functions->GetVal(wxT("proacl")));
// set the return type only if not already set..
if (function->GetReturnType().IsEmpty())
{
wxString strType = functions->GetVal(wxT("typname"));
if (strType.Lower() == wxT("record") && !strReturnTableArgs.IsEmpty())
{
strType = wxT("TABLE(") + strReturnTableArgs + wxT(")");
}
function->iSetReturnType(strType);
}
function->iSetComment(functions->GetVal(wxT("description")));
function->iSetLanguage(lanname);
function->iSetSecureDefiner(functions->GetBool(wxT("prosecdef")));
function->iSetReturnAsSet(functions->GetBool(wxT("proretset")));
function->iSetIsStrict(functions->GetBool(wxT("proisstrict")));
function->iSetSource(functions->GetVal(wxT("prosrc")));
function->iSetBin(functions->GetVal(wxT("probin")));
wxString vol = functions->GetVal(wxT("provolatile"));
function->iSetVolatility(
vol.IsSameAs(wxT("i")) ? wxT("IMMUTABLE") :
vol.IsSameAs(wxT("s")) ? wxT("STABLE") :
vol.IsSameAs(wxT("v")) ? wxT("VOLATILE") : wxT("unknown"));
// PostgreSQL 8.3 cost/row estimations
if (obj->GetConnection()->BackendMinimumVersion(8, 3))
{
function->iSetCost(functions->GetLong(wxT("procost")));
function->iSetRows(functions->GetLong(wxT("prorows")));
wxString cfg = functions->GetVal(wxT("proconfig"));
if (!cfg.IsEmpty())
FillArray(function->GetConfigList(), cfg.Mid(1, cfg.Length() - 2));
}
if (obj->GetConnection()->BackendMinimumVersion(9, 1))
{
function->iSetProviders(functions->GetVal(wxT("providers")));
function->iSetLabels(functions->GetVal(wxT("labels")));
}
if (obj->GetConnection()->BackendMinimumVersion(9, 2))
{
function->iSetIsLeakProof(functions->GetBool(wxT("proleakproof")));
}
if (browser)
{
browser->AppendObject(obj, function);
functions->MoveNext();
}
else
break;
}
delete functions;
delete types;
}
return function;
}
pgObject *pgFunction::Refresh(ctlTree *browser, const wxTreeItemId item)
{
pgObject *function = 0;
pgCollection *coll = browser->GetParentCollection(item);
if (coll)
function = functionFactory.AppendFunctions(coll, GetSchema(), 0, wxT(" WHERE pr.oid=") + GetOidStr() + wxT("\n"));
// We might be linked to trigger....
pgObject *trigger = browser->GetParentObject(item);
if (trigger->GetMetaType() == PGM_TRIGGER || trigger->GetMetaType() == PGM_EVENTTRIGGER)
function = functionFactory.AppendFunctions(trigger, GetSchema(), 0, wxT(" WHERE pr.oid=") + GetOidStr() + wxT("\n"));
return function;
}
// Generate the SELECT Script SQL
wxString pgFunction::GetSelectSql(ctlTree *browser)
{
wxString args = GetArgSigList(true);
wxString sql = wxT("SELECT ") + GetQuotedFullIdentifier();
if (args.Length())
sql += wxT("(\n") + args + wxT("\n);\n");
else
sql += wxT("();\n");
return sql;
}
// Generate the EXEC Script SQL
wxString pgProcedure::GetExecSql(ctlTree *browser)
{
wxString args = GetArgSigList(true);
wxString sql = wxT("EXEC ") + GetQuotedFullIdentifier();
if (args.Length())
sql += wxT("(\n") + args + wxT("\n);\n");
else
sql += wxT(";\n");
return sql;
}
pgObject *pgFunctionFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restr)
{
wxString funcRestriction = wxT(
" WHERE proisagg = FALSE AND pronamespace = ") + NumToStr(collection->GetSchema()->GetOid())
+ wxT("::oid\n AND typname NOT IN ('trigger', 'event_trigger') \n");
if (collection->GetConnection()->EdbMinimumVersion(8, 1))
funcRestriction += wxT(" AND NOT (lanname = 'edbspl' AND protype = '1')\n");
else if (collection->GetConnection()->EdbMinimumVersion(8, 0))
funcRestriction += wxT(" AND NOT (lanname = 'edbspl' AND typname = 'void')\n");
// Get the Functions
return AppendFunctions(collection, collection->GetSchema(), browser, funcRestriction);
}
pgCollection *pgFunctionFactory::CreateCollection(pgObject *obj)
{
return new pgFunctionCollection(GetCollectionFactory(), (pgSchema *)obj);
}
pgObject *pgTriggerFunctionFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restr)
{
wxString funcRestriction = wxT(
" WHERE proisagg = FALSE AND pronamespace = ") + NumToStr(collection->GetSchema()->GetOid())
+ wxT("::oid\n");
if(collection->GetConnection()->BackendMinimumVersion(9, 3))
{
funcRestriction += wxT("AND (typname IN ('trigger', 'event_trigger') \nAND lanname NOT IN ('edbspl', 'sql', 'internal'))");
}
else
{
funcRestriction += wxT("AND (typname = 'trigger'\n AND lanname != 'edbspl')");
}
// Get the Functions
return AppendFunctions(collection, collection->GetSchema(), browser, funcRestriction);
}
pgObject *pgProcedureFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restr)
{
wxString funcRestriction = wxT(
" WHERE proisagg = FALSE AND pronamespace = ") + NumToStr(collection->GetSchema()->GetOid())
+ wxT("::oid AND lanname = 'edbspl'\n");
if (collection->GetConnection()->EdbMinimumVersion(8, 1))
funcRestriction += wxT(" AND protype = '1'\n");
else
funcRestriction += wxT(" AND typname = 'void'\n");
// Get the Functions
return AppendFunctions(collection, collection->GetSchema(), browser, funcRestriction);
}
#include "images/function.pngc"
#include "images/functions.pngc"
pgFunctionFactory::pgFunctionFactory(const wxChar *tn, const wxChar *ns, const wxChar *nls, wxImage *img)
: pgSchemaObjFactory(tn, ns, nls, img)
{
metaType = PGM_FUNCTION;
}
pgFunctionFactory functionFactory(__("Function"), __("New Function..."), __("Create a new Function."), function_png_img);
static pgaCollectionFactory cf(&functionFactory, __("Functions"), functions_png_img);
#include "images/triggerfunction.pngc"
#include "images/triggerfunctions.pngc"
pgTriggerFunctionFactory::pgTriggerFunctionFactory()
: pgFunctionFactory(__("Trigger Function"), __("New Trigger Function..."), __("Create a new Trigger Function."), triggerfunction_png_img)
{
}
pgTriggerFunctionFactory triggerFunctionFactory;
static pgaCollectionFactory cft(&triggerFunctionFactory, __("Trigger Functions"), triggerfunctions_png_img);
#include "images/procedure.pngc"
#include "images/procedures.pngc"
pgProcedureFactory::pgProcedureFactory()
: pgFunctionFactory(__("Procedure"), __("New Procedure"), __("Create a new Procedure."), procedure_png_img)
{
}
pgProcedureFactory procedureFactory;
static pgaCollectionFactory cfp(&procedureFactory, __("Procedures"), procedures_png_img);
pgFunctionCollection::pgFunctionCollection(pgaFactory *factory, pgSchema *sch)
: pgSchemaObjCollection(factory, sch)
{
}
wxString pgFunctionCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on functions");
break;
case REFRESHINGDETAILS:
message = _("Refreshing functions");
break;
case GRANTWIZARDTITLE:
message = _("Privileges for functions");
break;
case OBJECTSLISTREPORT:
message = _("Functions list report");
break;
}
return message;
}
void pgFunctionCollection::ShowStatistics(frmMain *form, ctlListView *statistics)
{
if (GetConnection()->BackendMinimumVersion(8, 4))
{
wxLogInfo(wxT("Displaying statistics for functions on ") + GetSchema()->GetName());
wxString sql = wxT("SELECT funcname, calls, total_time, self_time")
wxT(" FROM pg_stat_user_functions")
wxT(" WHERE schemaname = ") + qtDbString(GetSchema()->GetName())
+ wxT(" ORDER BY funcname");
// Add the statistics view columns
statistics->ClearAll();
statistics->AddColumn(_("Function"), 60);
statistics->AddColumn(_("Calls"), 50);
statistics->AddColumn(_("Total Time"), 60);
statistics->AddColumn(_("Self Time"), 60);
pgSet *stats = GetDatabase()->ExecuteSet(sql);
if (stats)
{
long pos = 0;
while (!stats->Eof())
{
statistics->InsertItem(pos, stats->GetVal(wxT("funcname")), PGICON_STATISTICS);
statistics->SetItem(pos, 1, stats->GetVal(wxT("calls")));
statistics->SetItem(pos, 2, stats->GetVal(wxT("total_time")));
statistics->SetItem(pos, 3, stats->GetVal(wxT("self_time")));
stats->MoveNext();
pos++;
}
delete stats;
}
}
}
resetFunctionStatsFactory::resetFunctionStatsFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
{
mnu->Append(id, _("&Reset function statistics"), _("Reset statistics of the selected function."));
}
wxWindow *resetFunctionStatsFactory::StartDialog(frmMain *form, pgObject *obj)
{
if (wxMessageBox(_("Are you sure you wish to reset statistics of this function?"), _("Reset function statistics"), wxYES_NO) != wxYES)
return 0;
((pgFunction *)obj)->ResetStats();
((pgFunction *)obj)->ShowStatistics(form, form->GetStatistics());
return 0;
}
bool resetFunctionStatsFactory::CheckEnable(pgObject *obj)
{
return obj && obj->IsCreatedBy(functionFactory) && ((pgFunction *)obj)->GetConnection()->BackendMinimumVersion(9, 0);
}
wxString pgTriggerFunctionCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on trigger functions");
break;
case REFRESHINGDETAILS:
message = _("Refreshing trigger functions");
break;
case GRANTWIZARDTITLE:
message = _("Privileges for trigger functions");
break;
case OBJECTSLISTREPORT:
message = _("Trigger functions list report");
break;
}
return message;
}
wxString pgProcedureCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on procedures");
break;
case REFRESHINGDETAILS:
message = _("Refreshing procedures");
break;
case GRANTWIZARDTITLE:
message = _("Privileges for procedures");
break;
case OBJECTSLISTREPORT:
message = _("Procedures list report");
break;
}
return message;
}
|