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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// pgSchema.cpp - schema class
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "frm/menu.h"
#include "utils/misc.h"
#include "schema/pgSchema.h"
#include "frm/frmMain.h"
#include "schema/pgCatalogObject.h"
#include "schema/edbPackage.h"
#include "schema/pgCollation.h"
#include "schema/pgDomain.h"
#include "schema/pgAggregate.h"
#include "schema/pgConversion.h"
#include "schema/pgForeignTable.h"
#include "schema/pgFunction.h"
#include "schema/pgOperator.h"
#include "schema/pgOperatorClass.h"
#include "schema/pgOperatorFamily.h"
#include "schema/pgSequence.h"
#include "schema/pgTable.h"
#include "schema/gpExtTable.h"
#include "schema/pgTextSearchConfiguration.h"
#include "schema/pgTextSearchDictionary.h"
#include "schema/pgTextSearchParser.h"
#include "schema/pgTextSearchTemplate.h"
#include "schema/pgType.h"
#include "schema/pgView.h"
#include "schema/gpPartition.h"
#include "schema/edbPrivateSynonym.h"
#include "frm/frmReport.h"
#include "wx/regex.h"
pgSchema::pgSchema(const wxString &newName)
: pgSchemaBase(schemaFactory, newName)
{
}
wxString pgSchema::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on schema");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing schema");
message += wxT(" ") + GetName();
break;
case GRANTWIZARDTITLE:
message = _("Privileges for schema");
message += wxT(" ") + GetName();
break;
case DROPINCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop schema \"%s\" including all objects that depend on it?"),
GetFullIdentifier().c_str());
break;
case DROPEXCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop schema \"%s\"?"),
GetFullIdentifier().c_str());
break;
case DROPCASCADETITLE:
message = _("Drop schema cascaded?");
break;
case DROPTITLE:
message = _("Drop schema?");
break;
case PROPERTIESREPORT:
message = _("Schema properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("Schema properties");
break;
case DDLREPORT:
message = _("Schema DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("Schema DDL");
break;
case DEPENDENCIESREPORT:
message = _("Schema dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("Schema dependencies");
break;
case DEPENDENTSREPORT:
message = _("Schema dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("Schema dependents");
break;
case BACKUPTITLE:
message = wxString::Format(_("Backup schema \"%s\""),
GetFullIdentifier().c_str());
break;
case RESTORETITLE:
message = wxString::Format(_("Restore schema \"%s\""),
GetFullIdentifier().c_str());
break;
}
return message;
}
pgCatalog::pgCatalog(const wxString &newName)
: pgSchemaBase(catalogFactory, newName)
{
}
wxString pgCatalog::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on catalog");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing catalog");
message += wxT(" ") + GetName();
break;
case PROPERTIESREPORT:
message = _("Catalog properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("Catalog properties");
break;
case DDLREPORT:
message = _("Catalog DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("Catalog DDL");
break;
case DEPENDENCIESREPORT:
message = _("Catalog dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("Catalog dependencies");
break;
case DEPENDENTSREPORT:
message = _("Catalog dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("Catalog dependents");
break;
}
return message;
}
pgSchemaBase::pgSchemaBase(pgaFactory &factory, const wxString &newName)
: pgDatabaseObject(factory, newName)
{
}
wxString pgCatalog::GetDisplayName()
{
if (GetFullName() == wxT("pg_catalog"))
return wxT("PostgreSQL (pg_catalog)");
else if (GetFullName() == wxT("pgagent"))
return wxT("pgAgent (pgagent)");
else if (GetFullName() == wxT("information_schema"))
return wxT("ANSI (information_schema)");
else if (GetFullName().StartsWith(wxT("_")))
return wxT("Slony cluster (") + GetFullName().AfterFirst('_') + wxT(")");
else if (GetFullName() == wxT("dbo"))
return wxT("Redmond (dbo)");
else if (GetFullName() == wxT("sys"))
return wxT("Redwood (sys)");
else
return GetFullName();
}
wxMenu *pgSchemaBase::GetNewMenu()
{
wxMenu *menu = pgObject::GetNewMenu();
if (GetCreatePrivilege())
{
if (settings->GetDisplayOption(_("Aggregates")))
aggregateFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Collations")) && GetConnection()->BackendMinimumVersion(9, 1))
collationFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Conversions")))
conversionFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Domains")))
domainFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Functions")))
functionFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Packages")))
{
if (GetConnection()->EdbMinimumVersion(8, 0))
packageFactory.AppendMenu(menu);
}
if (settings->GetDisplayOption(_("Procedures")))
{
if (GetConnection()->EdbMinimumVersion(8, 0))
procedureFactory.AppendMenu(menu);
}
if (settings->GetDisplayOption(_("Operators")))
operatorFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Sequences")))
sequenceFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Tables")))
tableFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("FTS Configurations")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
textSearchConfigurationFactory.AppendMenu(menu);
}
if (settings->GetDisplayOption(_("FTS Dictionaries")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
textSearchDictionaryFactory.AppendMenu(menu);
}
if (settings->GetDisplayOption(_("FTS Parsers")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
textSearchParserFactory.AppendMenu(menu);
}
if (settings->GetDisplayOption(_("FTS Templates")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
textSearchTemplateFactory.AppendMenu(menu);
}
if (settings->GetDisplayOption(_("Foreign Tables")))
{
if (GetConnection()->BackendMinimumVersion(9, 1))
foreignTableFactory.AppendMenu(menu);
}
if (settings->GetDisplayOption(_("Trigger Functions")))
triggerFunctionFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Types")))
typeFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("Views")))
viewFactory.AppendMenu(menu);
if (settings->GetDisplayOption(_("External Tables")))
{
if (GetConnection() != 0 && GetConnection()->GetIsGreenplum())
extTableFactory.AppendMenu(menu);
}
if (settings->GetDisplayOption(_("Synonyms")) && GetConnection()->EdbMinimumVersion(8, 4))
edbPrivFactory.AppendMenu(menu);
}
return menu;
}
bool pgSchemaBase::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
wxString sql = wxT("DROP SCHEMA ") + GetQuotedFullIdentifier();
if (cascaded)
sql += wxT(" CASCADE");
return GetDatabase()->ExecuteVoid(sql);
}
wxString pgSchemaBase::GetSql(ctlTree *browser)
{
if (sql.IsNull())
{
wxString strName = qtIdent(GetName());
if (GetMetaType() == PGM_CATALOG)
sql = wxT("-- Catalog: ") + GetName() + wxT("\n\n");
else
sql = wxT("-- Schema: ") + GetName() + wxT("\n\n");
sql += wxT("-- DROP SCHEMA ") + GetQuotedFullIdentifier() + wxT(";")
+ wxT("\n\nCREATE SCHEMA ") + strName
+ wxT("\n AUTHORIZATION ") + qtIdent(GetOwner());
sql += wxT(";\n\n");
sql += GetGrant(wxT("UC"), wxT("SCHEMA ") + GetQuotedFullIdentifier())
+ GetCommentSql();
sql += wxT("\n") + pgDatabase::GetDefaultPrivileges('r', m_defPrivsOnTables, strName);
sql += pgDatabase::GetDefaultPrivileges('S', m_defPrivsOnSeqs, strName);
sql += pgDatabase::GetDefaultPrivileges('f', m_defPrivsOnFuncs, strName);
if (GetConnection()->BackendMinimumVersion(9, 2))
sql += pgDatabase::GetDefaultPrivileges('T', m_defPrivsOnTypes, strName);
if (GetConnection()->BackendMinimumVersion(9, 1))
sql += GetSeqLabelsSql();
}
return sql;
}
void pgSchemaBase::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
GetDatabase()->GetServer()->iSetLastDatabase(GetDatabase()->GetName());
GetDatabase()->GetServer()->iSetLastSchema(GetName());
if (!expandedKids)
{
expandedKids = true;
browser->RemoveDummyChild(this);
// Log
wxLogInfo(wxT("Adding child object to schema %s"), GetIdentifier().c_str());
if (!(GetMetaType() == PGM_CATALOG && (GetFullName() == wxT("dbo") || GetFullName() == wxT("sys") || GetFullName() == wxT("information_schema"))))
{
if (settings->GetDisplayOption(_("Aggregates")))
browser->AppendCollection(this, aggregateFactory);
if (settings->GetDisplayOption(_("Collations")) && GetConnection()->BackendMinimumVersion(9, 1))
browser->AppendCollection(this, collationFactory);
if (settings->GetDisplayOption(_("Conversions")))
browser->AppendCollection(this, conversionFactory);
if (settings->GetDisplayOption(_("Domains")))
browser->AppendCollection(this, domainFactory);
if (settings->GetDisplayOption(_("Foreign Tables")) && GetConnection()->BackendMinimumVersion(9, 1))
browser->AppendCollection(this, foreignTableFactory);
if (settings->GetDisplayOption(_("FTS Configurations")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
browser->AppendCollection(this, textSearchConfigurationFactory);
}
if (settings->GetDisplayOption(_("FTS Dictionaries")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
browser->AppendCollection(this, textSearchDictionaryFactory);
}
if (settings->GetDisplayOption(_("FTS Parsers")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
browser->AppendCollection(this, textSearchParserFactory);
}
if (settings->GetDisplayOption(_("FTS Templates")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
browser->AppendCollection(this, textSearchTemplateFactory);
}
if (settings->GetDisplayOption(_("Functions")))
browser->AppendCollection(this, functionFactory);
if (settings->GetDisplayOption(_("Operators")))
browser->AppendCollection(this, operatorFactory);
if (settings->GetDisplayOption(_("Operator Classes")))
browser->AppendCollection(this, operatorClassFactory);
if (settings->GetDisplayOption(_("Operator Families")))
{
if (GetConnection()->BackendMinimumVersion(8, 3))
browser->AppendCollection(this, operatorFamilyFactory);
}
if (settings->GetDisplayOption(_("Packages")) && GetConnection()->EdbMinimumVersion(8, 1))
browser->AppendCollection(this, packageFactory);
if (settings->GetDisplayOption(_("Procedures")))
{
if (GetConnection()->EdbMinimumVersion(8, 0))
browser->AppendCollection(this, procedureFactory);
}
if (settings->GetDisplayOption(_("Sequences")))
browser->AppendCollection(this, sequenceFactory);
if (settings->GetDisplayOption(_("Tables")))
browser->AppendCollection(this, tableFactory);
if (settings->GetDisplayOption(_("External Tables")))
{
if (GetConnection() != 0 && GetConnection()->GetIsGreenplum())
browser->AppendCollection(this, extTableFactory);
}
if (settings->GetDisplayOption(_("Trigger Functions")))
browser->AppendCollection(this, triggerFunctionFactory);
if (settings->GetDisplayOption(_("Types")))
browser->AppendCollection(this, typeFactory);
if (settings->GetDisplayOption(_("Views")))
browser->AppendCollection(this, viewFactory);
if (settings->GetDisplayOption(_("Synonyms")) && GetConnection()->EdbMinimumVersion(8, 4))
browser->AppendCollection(this, edbPrivFactory);
}
else
browser->AppendCollection(this, catalogObjectFactory);
if (GetConnection()->BackendMinimumVersion(9, 0))
{
m_defPrivsOnTables = GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = ") + GetOidStr() + wxT(" AND defaclobjtype='r'"));
m_defPrivsOnSeqs = GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = ") + GetOidStr() + wxT(" AND defaclobjtype='S'"));
m_defPrivsOnFuncs = GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = ") + GetOidStr() + wxT(" AND defaclobjtype='f'"));
}
if (GetConnection()->BackendMinimumVersion(9, 2))
{
m_defPrivsOnTypes = GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = ") + GetOidStr() + wxT(" AND defaclobjtype='T'"));
}
}
if (properties)
{
CreateListColumns(properties);
properties->AppendItem(_("Name"), GetName());
properties->AppendItem(_("OID"), GetOid());
properties->AppendItem(_("Owner"), GetOwner());
properties->AppendItem(_("ACL"), GetAcl());
if (GetConnection()->BackendMinimumVersion(9, 0))
{
properties->AppendItem(_("Default table ACL"), m_defPrivsOnTables);
properties->AppendItem(_("Default sequence ACL"), m_defPrivsOnSeqs);
properties->AppendItem(_("Default function ACL"), m_defPrivsOnFuncs);
}
if (GetConnection()->BackendMinimumVersion(9, 2))
properties->AppendItem(_("Default type ACL"), m_defPrivsOnTypes);
if (GetMetaType() != PGM_CATALOG)
properties->AppendYesNoItem(_("System schema?"), 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));
}
}
}
}
}
pgObject *pgSchemaBase::Refresh(ctlTree *browser, const wxTreeItemId item)
{
pgObject *schema = 0;
pgCollection *coll = browser->GetParentCollection(item);
if (coll)
{
if (coll->IsCollectionForType(PGM_CATALOG))
schema = catalogFactory.CreateObjects(coll, 0, wxT(" WHERE nsp.oid=") + GetOidStr() + wxT("\n"));
else
schema = schemaFactory.CreateObjects(coll, 0, wxT(" WHERE nsp.oid=") + GetOidStr() + wxT("\n"));
}
return schema;
}
pgObject *pgSchemaBaseFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
{
pgSchema *schema = 0;
pgCatalog *catalog = 0;
wxString restr = restriction;
if (restr.IsEmpty())
restr += wxT(" WHERE ");
else
restr += wxT(" AND ");
if (GetMetaType() != PGM_CATALOG)
{
restr += wxT("NOT ");
}
restr += wxT("((nspname = 'pg_catalog' AND EXISTS (SELECT 1 FROM pg_class WHERE relname = 'pg_class' AND relnamespace = nsp.oid LIMIT 1)) OR\n");
restr += wxT("(nspname = 'pgagent' AND EXISTS (SELECT 1 FROM pg_class WHERE relname = 'pga_job' AND relnamespace = nsp.oid LIMIT 1)) OR\n");
restr += wxT("(nspname = 'information_schema' AND EXISTS (SELECT 1 FROM pg_class WHERE relname = 'tables' AND relnamespace = nsp.oid LIMIT 1)) OR\n");
restr += wxT("(nspname LIKE '_%' AND EXISTS (SELECT 1 FROM pg_proc WHERE proname='slonyversion' AND pronamespace = nsp.oid LIMIT 1))\n");
// We add the below schemas at initdb time itself. So users cannot create
// schemas of the same name later. However if DB is started in PostgreSQL
// mode, we do not install all other edb-sys.sql objects. So just checking
// for presence of schemas with such names should be enough..
if (collection->GetConnection()->EdbMinimumVersion(8, 1))
restr += wxT("OR (nspname = 'dbo' OR nspname = 'sys'))\n");
else
restr += wxT(")");
if (collection->GetConnection()->EdbMinimumVersion(8, 2))
{
restr += wxT(" AND nsp.nspparent = 0\n");
// Do not show dbms_job_procedure in schemas
if (!settings->GetShowSystemObjects())
restr += wxT("AND NOT (nspname = 'dbms_job_procedure' AND EXISTS(SELECT 1 FROM pg_proc WHERE pronamespace = nsp.oid and proname = 'run_job' LIMIT 1))\n");
}
if (!collection->GetDatabase()->GetSchemaRestriction().IsEmpty())
restr += wxT(" AND nspname IN (") + collection->GetDatabase()->GetSchemaRestriction() + wxT(")");
// Don't fetch temp schemas if not actually required, as Greenplum seems to
// generate thousands in some circumstances.
if (!settings->GetShowSystemObjects())
{
if (collection->GetDatabase()->BackendMinimumVersion(8, 1))
restr += wxT(" AND nspname NOT LIKE E'pg\\\\_temp\\\\_%'AND nspname NOT LIKE E'pg\\\\_toast_temp\\\\_%'");
else
restr += wxT(" AND nspname NOT LIKE 'pg\\\\_temp\\\\_%'AND nspname NOT LIKE 'pg\\\\_toast_temp\\\\_%'");
}
wxString sql;
if (GetMetaType() == PGM_CATALOG)
{
sql = wxT("SELECT 2 AS nsptyp,\n")
wxT(" nsp.nspname, nsp.oid, pg_get_userbyid(nspowner) AS namespaceowner, nspacl, description,")
wxT(" FALSE as cancreate\n")
wxT(" FROM pg_namespace nsp\n")
wxT(" LEFT OUTER JOIN pg_description des ON (des.objoid=nsp.oid AND des.classoid='pg_namespace'::regclass)\n")
+ restr +
wxT(" ORDER BY 1, nspname");
}
else
{
if (collection->GetDatabase()->BackendMinimumVersion(8, 1))
{
sql = wxT("SELECT CASE WHEN nspname LIKE E'pg\\\\_temp\\\\_%' THEN 1\n")
wxT(" WHEN (nspname LIKE E'pg\\\\_%') THEN 0\n");
}
else
{
sql = wxT("SELECT CASE WHEN nspname LIKE 'pg\\\\_temp\\\\_%' THEN 1\n")
wxT(" WHEN (nspname LIKE 'pg\\\\_%') THEN 0\n");
}
sql += wxT(" ELSE 3 END AS nsptyp,\n")
wxT(" nsp.nspname, nsp.oid, pg_get_userbyid(nspowner) AS namespaceowner, nspacl, description,")
wxT(" has_schema_privilege(nsp.oid, 'CREATE') as cancreate");
if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
{
sql += wxT(",\n(SELECT array_agg(label) FROM pg_seclabels sl1 WHERE sl1.objoid=nsp.oid) AS labels");
sql += wxT(",\n(SELECT array_agg(provider) FROM pg_seclabels sl2 WHERE sl2.objoid=nsp.oid) AS providers");
}
sql += wxT("\n FROM pg_namespace nsp\n")
wxT(" LEFT OUTER JOIN pg_description des ON (des.objoid=nsp.oid AND des.classoid='pg_namespace'::regclass)\n")
+ restr +
wxT(" ORDER BY 1, nspname");
}
pgSet *schemas = collection->GetDatabase()->ExecuteSet(sql);
if (schemas)
{
while (!schemas->Eof())
{
wxString name = schemas->GetVal(wxT("nspname"));
long nsptyp = schemas->GetLong(wxT("nsptyp"));
wxStringTokenizer tokens(settings->GetSystemSchemas(), wxT(","));
while (tokens.HasMoreTokens())
{
wxRegEx regex(tokens.GetNextToken());
if (regex.Matches(name))
{
nsptyp = SCHEMATYP_USERSYS;
break;
}
}
if (nsptyp <= SCHEMATYP_USERSYS && this->GetMetaType() != PGM_CATALOG && !settings->GetShowSystemObjects())
{
schemas->MoveNext();
continue;
}
if (GetMetaType() == PGM_CATALOG)
{
catalog = new pgCatalog(name);
catalog->iSetSchemaTyp(nsptyp);
catalog->iSetDatabase(collection->GetDatabase());
catalog->iSetComment(schemas->GetVal(wxT("description")));
catalog->iSetOid(schemas->GetOid(wxT("oid")));
catalog->iSetOwner(schemas->GetVal(wxT("namespaceowner")));
catalog->iSetAcl(schemas->GetVal(wxT("nspacl")));
catalog->iSetCreatePrivilege(false);
if (collection->GetDatabase()->BackendMinimumVersion(9, 0))
{
catalog->iSetDefPrivsOnTables(collection->GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = " + catalog->GetOidStr() + wxT(" AND defaclobjtype='r'"))));
catalog->iSetDefPrivsOnSeqs(collection->GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = " + catalog->GetOidStr() + wxT(" AND defaclobjtype='S'"))));
catalog->iSetDefPrivsOnFuncs(collection->GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = " + catalog->GetOidStr() + wxT(" AND defaclobjtype='f'"))));
}
if (collection->GetDatabase()->BackendMinimumVersion(9, 2))
{
catalog->iSetDefPrivsOnTypes(collection->GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = " + catalog->GetOidStr() + wxT(" AND defaclobjtype='T'"))));
}
if (browser)
{
browser->AppendObject(collection, catalog);
schemas->MoveNext();
}
else
break;
}
else
{
schema = new pgSchema(name);
schema->iSetSchemaTyp(nsptyp);
schema->iSetDatabase(collection->GetDatabase());
schema->iSetComment(schemas->GetVal(wxT("description")));
schema->iSetOid(schemas->GetOid(wxT("oid")));
schema->iSetOwner(schemas->GetVal(wxT("namespaceowner")));
schema->iSetAcl(schemas->GetVal(wxT("nspacl")));
schema->iSetCreatePrivilege(schemas->GetBool(wxT("cancreate")));
if (collection->GetDatabase()->BackendMinimumVersion(9, 0))
{
schema->iSetDefPrivsOnTables(collection->GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = " + schema->GetOidStr() + wxT(" AND defaclobjtype='r'"))));
schema->iSetDefPrivsOnSeqs(collection->GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = " + schema->GetOidStr() + wxT(" AND defaclobjtype='S'"))));
schema->iSetDefPrivsOnFuncs(collection->GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = " + schema->GetOidStr() + wxT(" AND defaclobjtype='f'"))));
}
if (collection->GetDatabase()->BackendMinimumVersion(9, 2))
{
schema->iSetDefPrivsOnTypes(collection->GetConnection()->ExecuteScalar(wxT("SELECT defaclacl FROM pg_catalog.pg_default_acl dacl WHERE dacl.defaclnamespace = " + schema->GetOidStr() + wxT(" AND defaclobjtype='T'"))));
}
if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
{
schema->iSetProviders(schemas->GetVal(wxT("providers")));
schema->iSetLabels(schemas->GetVal(wxT("labels")));
}
if (browser)
{
browser->AppendObject(collection, schema);
schemas->MoveNext();
}
else
break;
}
}
delete schemas;
}
if (GetMetaType() == PGM_CATALOG)
return catalog;
else
return schema;
}
pgObject *pgSchemaBase::ReadObjects(pgCollection *collection, ctlTree *browser)
{
wxString systemRestriction;
if (!settings->GetShowSystemObjects())
systemRestriction = wxT("WHERE ") + collection->GetConnection()->SystemNamespaceRestriction(wxT("nsp.nspname"));
// Get the schemas
return schemaFactory.CreateObjects(collection, browser, systemRestriction);
}
/////////////////////////////////////////////////////
pgSchemaObjCollection::pgSchemaObjCollection(pgaFactory *factory, pgSchema *sch)
: pgCollection(factory)
{
schema = sch;
database = schema->GetDatabase();
server = database->GetServer();
iSetOid(sch->GetOid());
}
bool pgSchemaObjCollection::CanCreate()
{
if(IsCollectionForType(PGM_OPCLASS) || IsCollectionForType(PGM_OPFAMILY))
return false;
// TODO
// OK, this is a hack. Rules and Views are both derived from pgRuleObject, which
// is derived from pgSchemaObject. In order that they attach to the treeview
// under the relevant node however, the Schema object is actually the table or
// View (yeah, I know - I didn't write it :-p ). This works fine *except* for
// Get CreatePrivilege() which doesn't exist in these classes so must be fixed
// up at this level. This needs a major rethink in the longer term
if (GetSchema()->GetMetaType() == PGM_TABLE || GetSchema()->GetMetaType() == PGM_VIEW || GetSchema()->GetMetaType() == GP_EXTTABLE || GetSchema()->GetMetaType() == GP_PARTITION)
return GetSchema()->GetSchema()->GetCreatePrivilege();
else
return GetSchema()->GetCreatePrivilege();
}
/////////////////////////////
pgSchemaCollection::pgSchemaCollection(pgaFactory *factory, pgDatabase *db)
: pgDatabaseObjCollection(factory, db)
{
}
wxString pgSchemaCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on schemas");
break;
case REFRESHINGDETAILS:
message = _("Refreshing schemas");
break;
case OBJECTSLISTREPORT:
message = _("Schemas list report");
break;
}
return message;
}
/////////////////////////////
pgCatalogCollection::pgCatalogCollection(pgaFactory *factory, pgDatabase *db)
: pgDatabaseObjCollection(factory, db)
{
}
wxString pgCatalogCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on catalogs");
break;
case REFRESHINGDETAILS:
message = _("Refreshing catalogs");
break;
case OBJECTSLISTREPORT:
message = _("Catalogs list report");
break;
}
return message;
}
/////////////////////////////////////////////////////
#include "images/namespace.pngc"
#include "images/namespace-sm.pngc"
#include "images/namespaces.pngc"
#include "images/catalog.pngc"
#include "images/catalog-sm.pngc"
#include "images/catalogs.pngc"
pgSchemaBaseFactory::pgSchemaBaseFactory(const wxChar *tn, const wxChar *ns, const wxChar *nls, wxImage *img, wxImage *imgSm)
: pgDatabaseObjFactory(tn, ns, nls, img, imgSm)
{
}
pgSchemaFactory::pgSchemaFactory()
: pgSchemaBaseFactory(__("Schema"), __("New Schema..."), __("Create a new Schema."), namespace_png_img, namespace_sm_png_img)
{
metaType = PGM_SCHEMA;
}
pgCollection *pgSchemaFactory::CreateCollection(pgObject *obj)
{
return new pgSchemaCollection(GetCollectionFactory(), (pgDatabase *)obj);
}
pgCatalogFactory::pgCatalogFactory()
: pgSchemaBaseFactory(__("Catalog"), __("New Catalog..."), __("Create a new Catalog."), catalog_png_img, catalog_sm_png_img)
{
metaType = PGM_CATALOG;
}
pgCollection *pgCatalogFactory::CreateCollection(pgObject *obj)
{
return new pgCatalogCollection(GetCollectionFactory(), (pgDatabase *)obj);
}
pgCollection *pgSchemaObjFactory::CreateCollection(pgObject *obj)
{
return new pgSchemaObjCollection(GetCollectionFactory(), (pgSchema *)obj);
}
pgSchemaFactory schemaFactory;
static pgaCollectionFactory scf(&schemaFactory, __("Schemas"), namespaces_png_img);
pgCatalogFactory catalogFactory;
static pgaCollectionFactory ccf(&catalogFactory, __("Catalogs"), catalogs_png_img);
|