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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// pgForeignKey.cpp - ForeignKey class
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "frm/frmMain.h"
#include "schema/pgForeignKey.h"
#include "schema/pgConstraints.h"
pgForeignKey::pgForeignKey(pgSchema *newSchema, const wxString &newName)
: pgSchemaObject(newSchema, foreignKeyFactory, newName)
{
}
pgForeignKey::~pgForeignKey()
{
}
wxString pgForeignKey::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on foreign key");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing foreign key");
message += wxT(" ") + GetName();
break;
case GRANTWIZARDTITLE:
message = _("Privileges for foreign key");
message += wxT(" ") + GetName();
break;
case DROPINCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop foreign key \"%s\" including all objects that depend on it?"),
GetFullIdentifier().c_str());
break;
case DROPEXCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop foreign key \"%s\"?"),
GetFullIdentifier().c_str());
break;
case DROPCASCADETITLE:
message = _("Drop foreign key cascaded?");
break;
case DROPTITLE:
message = _("Drop foreign key?");
break;
case PROPERTIESREPORT:
message = _("Foreign key properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("Foreign key properties");
break;
case DDLREPORT:
message = _("Foreign key DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("Foreign key DDL");
break;
case DEPENDENCIESREPORT:
message = _("Foreign key dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("Foreign key dependencies");
break;
case DEPENDENTSREPORT:
message = _("Foreign key dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("Foreign key dependents");
break;
}
return message;
}
int pgForeignKey::GetIconId()
{
if (!GetDatabase()->BackendMinimumVersion(9, 1) || GetValid())
return foreignKeyFactory.GetIconId();
else
return foreignKeyFactory.GetClosedIconId();
}
bool pgForeignKey::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
wxString sql = wxT("ALTER TABLE ") + GetQuotedSchemaPrefix(fkSchema) + qtIdent(fkTable)
+ wxT(" DROP CONSTRAINT ") + GetQuotedIdentifier();
if (cascaded)
sql += wxT(" CASCADE");
return GetDatabase()->ExecuteVoid(sql);
}
wxString pgForeignKey::GetDefinition()
{
wxString sql;
sql = wxT("(") + GetQuotedFkColumns()
+ wxT(")\n REFERENCES ") + GetQuotedSchemaPrefix(GetRefSchema()) + qtIdent(GetReferences())
+ wxT(" (") + GetQuotedRefColumns()
+ wxT(")");
if (GetDatabase()->BackendMinimumVersion(7, 4) || GetMatch() == wxT("FULL"))
sql += wxT(" MATCH ") + GetMatch();
sql += wxT("\n ON UPDATE ") + GetOnUpdate()
+ wxT(" ON DELETE ") + GetOnDelete();
if (GetDeferrable())
{
sql += wxT(" DEFERRABLE INITIALLY ");
if (GetDeferred())
sql += wxT("DEFERRED");
else
sql += wxT("IMMEDIATE");
}
if (GetDatabase()->BackendMinimumVersion(9, 1) && !GetValid())
sql += wxT("\n NOT VALID");
return sql;
}
wxString pgForeignKey::GetConstraint()
{
wxString sql;
sql = GetQuotedIdentifier()
+ wxT(" FOREIGN KEY ") + GetDefinition();
return sql;
}
wxString pgForeignKey::GetSql(ctlTree *browser)
{
if (sql.IsNull())
{
sql = wxT("-- Foreign Key: ") + GetQuotedFullIdentifier() + wxT("\n\n")
+ wxT("-- ALTER TABLE ") + GetQuotedSchemaPrefix(fkSchema) + qtIdent(fkTable)
+ wxT(" DROP CONSTRAINT ") + GetQuotedIdentifier() + wxT(";")
+ wxT("\n\nALTER TABLE ") + GetQuotedSchemaPrefix(fkSchema) + qtIdent(fkTable)
+ wxT("\n ADD CONSTRAINT ") + GetConstraint()
+ wxT(";\n");
if (!GetComment().IsEmpty())
sql += wxT("COMMENT ON CONSTRAINT ") + GetQuotedIdentifier() + wxT(" ON ") + GetQuotedSchemaPrefix(fkSchema) + qtIdent(fkTable)
+ wxT(" IS ") + qtDbString(GetComment()) + wxT(";\n");
}
return sql;
}
wxString pgForeignKey::GetFullName()
{
return GetName() + wxT(" -> ") + GetReferences();
}
void pgForeignKey::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
if (!expandedKids)
{
expandedKids = true;
wxTreeItemId id = browser->GetItemParent(GetId());
pgTable *table = (pgTable *)browser->GetObject(id);
wxStringTokenizer c1l(GetConkey(), wxT(","));
wxStringTokenizer c2l(GetConfkey(), wxT(","));
wxString c1, c2;
// resolve column names
while (c1l.HasMoreTokens())
{
c1 = c1l.GetNextToken();
c2 = c2l.GetNextToken();
pgSet *set = ExecuteSet(
wxT("SELECT a1.attname as conattname, a2.attname as confattname\n")
wxT(" FROM pg_attribute a1, pg_attribute a2\n")
wxT(" WHERE a1.attrelid=") + NumToStr(table->GetOid()) + wxT("::oid") + wxT(" AND a1.attnum=") + c1 + wxT("\n")
wxT(" AND a2.attrelid=") + GetRelTableOidStr() + wxT(" AND a2.attnum=") + c2);
if (set)
{
if (!fkColumns.IsNull())
{
fkColumns += wxT(", ");
refColumns += wxT(", ");
quotedFkColumns += wxT(", ");
quotedRefColumns += wxT(", ");
}
fkColumns += set->GetVal(0);
refColumns += set->GetVal(1);
quotedFkColumns += qtIdent(set->GetVal(0));
quotedRefColumns += qtIdent(set->GetVal(1));
delete set;
}
}
wxTreeItemId item = browser->GetItemParent(GetId());
while (item)
{
pgTable *table = (pgTable *)browser->GetObject(item);
if (table && table->IsCreatedBy(tableFactory))
{
coveringIndex = table->GetCoveringIndex(browser, fkColumns);
break;
}
item = browser->GetItemParent(item);
}
}
if (properties)
{
CreateListColumns(properties);
properties->AppendItem(_("Name"), GetName());
properties->AppendItem(_("OID"), NumToStr(GetOid()));
properties->AppendItem(_("Child columns"), GetFkColumns());
properties->AppendItem(_("References"), GetReferences()
+ wxT("(") + GetRefColumns() + wxT(")"));
properties->AppendItem(_("Covering index"), GetCoveringIndex());
properties->AppendItem(_("Match type"), GetMatch());
properties->AppendItem(_("On update"), GetOnUpdate());
properties->AppendItem(_("On delete"), GetOnDelete());
properties->AppendItem(_("Deferrable?"), BoolToYesNo(GetDeferrable()));
if (GetDeferrable())
properties->AppendItem(_("Initially?"),
GetDeferred() ? wxT("DEFERRED") : wxT("IMMEDIATE"));
if (GetDatabase()->BackendMinimumVersion(9, 1))
properties->AppendItem(_("Valid?"), BoolToYesNo(GetValid()));
properties->AppendItem(_("System foreign key?"), BoolToYesNo(GetSystemObject()));
properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
}
}
pgObject *pgForeignKey::Refresh(ctlTree *browser, const wxTreeItemId item)
{
pgObject *foreignKey = 0;
pgCollection *coll = browser->GetParentCollection(item);
if (coll)
foreignKey = foreignKeyFactory.CreateObjects(coll, 0, wxT("\n AND ct.oid=") + GetOidStr());
return foreignKey;
}
void pgForeignKey::Validate(frmMain *form)
{
wxString sql = wxT("ALTER TABLE ") + GetQuotedSchemaPrefix(fkSchema) + qtIdent(fkTable)
+ wxT("\n VALIDATE CONSTRAINT ") + GetQuotedIdentifier();
GetDatabase()->ExecuteVoid(sql);
iSetValid(true);
UpdateIcon(form->GetBrowser());
}
pgObject *pgForeignKeyFactory::CreateObjects(pgCollection *coll, ctlTree *browser, const wxString &restriction)
{
wxString sql;
pgTableObjCollection *collection = (pgTableObjCollection *)coll;
pgForeignKey *foreignKey = 0;
sql = wxT("SELECT ct.oid, conname, condeferrable, condeferred, confupdtype, confdeltype, confmatchtype, ")
wxT("conkey, confkey, confrelid, nl.nspname as fknsp, cl.relname as fktab, ")
wxT("nr.nspname as refnsp, cr.relname as reftab, description");
if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
sql += wxT(", convalidated");
sql += wxT("\n FROM pg_constraint ct\n")
wxT(" JOIN pg_class cl ON cl.oid=conrelid\n")
wxT(" JOIN pg_namespace nl ON nl.oid=cl.relnamespace\n")
wxT(" JOIN pg_class cr ON cr.oid=confrelid\n")
wxT(" JOIN pg_namespace nr ON nr.oid=cr.relnamespace\n")
wxT(" LEFT OUTER JOIN pg_description des ON (des.objoid=ct.oid AND des.classoid='pg_constraint'::regclass)\n")
wxT(" WHERE contype='f' AND conrelid = ") + collection->GetOidStr()
+ restriction + wxT("\n")
wxT(" ORDER BY conname");
pgSet *foreignKeys = collection->GetDatabase()->ExecuteSet(sql);
if (foreignKeys)
{
while (!foreignKeys->Eof())
{
foreignKey = new pgForeignKey(collection->GetSchema()->GetSchema(), foreignKeys->GetVal(wxT("conname")));
foreignKey->iSetOid(foreignKeys->GetOid(wxT("oid")));
foreignKey->iSetRelTableOid(foreignKeys->GetOid(wxT("confrelid")));
foreignKey->iSetFkSchema(foreignKeys->GetVal(wxT("fknsp")));
foreignKey->iSetComment(foreignKeys->GetVal(wxT("description")));
foreignKey->iSetFkTable(foreignKeys->GetVal(wxT("fktab")));
foreignKey->iSetRefSchema(foreignKeys->GetVal(wxT("refnsp")));
foreignKey->iSetReferences(foreignKeys->GetVal(wxT("reftab")));
if (collection->GetDatabase()->BackendMinimumVersion(9, 1))
foreignKey->iSetValid(foreignKeys->GetBool(wxT("convalidated")));
wxString onUpd = foreignKeys->GetVal(wxT("confupdtype"));
wxString onDel = foreignKeys->GetVal(wxT("confdeltype"));
wxString match = foreignKeys->GetVal(wxT("confmatchtype"));
foreignKey->iSetOnUpdate(
onUpd.IsSameAs('a') ? wxT("NO ACTION") :
onUpd.IsSameAs('r') ? wxT("RESTRICT") :
onUpd.IsSameAs('c') ? wxT("CASCADE") :
onUpd.IsSameAs('d') ? wxT("SET DEFAULT") :
onUpd.IsSameAs('n') ? wxT("SET NULL") : wxT("Unknown"));
foreignKey->iSetOnDelete(
onDel.IsSameAs('a') ? wxT("NO ACTION") :
onDel.IsSameAs('r') ? wxT("RESTRICT") :
onDel.IsSameAs('c') ? wxT("CASCADE") :
onDel.IsSameAs('d') ? wxT("SET DEFAULT") :
onDel.IsSameAs('n') ? wxT("SET NULL") : wxT("Unknown"));
foreignKey->iSetMatch(
match.IsSameAs('f') ? wxT("FULL") :
match.IsSameAs('s') ? wxT("SIMPLE") :
match.IsSameAs('u') ? wxT("SIMPLE") : wxT("Unknown"));
wxString cn = foreignKeys->GetVal(wxT("conkey"));
cn = cn.Mid(1, cn.Length() - 2);
foreignKey->iSetConkey(cn);
cn = foreignKeys->GetVal(wxT("confkey"));
cn = cn.Mid(1, cn.Length() - 2);
foreignKey->iSetConfkey(cn);
foreignKey->iSetDeferrable(foreignKeys->GetBool(wxT("condeferrable")));
foreignKey->iSetDeferred(foreignKeys->GetBool(wxT("condeferred")));
if (browser)
{
browser->AppendObject(collection, foreignKey);
foreignKeys->MoveNext();
}
else
break;
}
delete foreignKeys;
}
return foreignKey;
}
/////////////////////////////
wxString pgForeignKeyCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on foreign keys");
break;
case REFRESHINGDETAILS:
message = _("Refreshing foreign keys");
break;
case OBJECTSLISTREPORT:
message = _("Foreign keys list report");
break;
}
return message;
}
/////////////////////////////
#include "images/foreignkey.pngc"
#include "images/foreignkeybad.pngc"
pgForeignKeyFactory::pgForeignKeyFactory()
: pgSchemaObjFactory(__("Foreign Key"), __("New Foreign Key..."), __("Create a new Foreign Key constraint."), foreignkey_png_img)
{
metaType = PGM_FOREIGNKEY;
collectionFactory = &constraintCollectionFactory;
closedId = addIcon(foreignkeybad_png_img);
}
pgForeignKeyFactory foreignKeyFactory;
validateForeignKeyFactory::validateForeignKeyFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
{
mnu->Append(id, _("Validate foreign key"), _("Validate the selected foreign key."));
}
wxWindow *validateForeignKeyFactory::StartDialog(frmMain *form, pgObject *obj)
{
((pgForeignKey *)obj)->Validate(form);
((pgForeignKey *)obj)->SetDirty();
wxTreeItemId item = form->GetBrowser()->GetSelection();
if (obj == form->GetBrowser()->GetObject(item))
{
obj->ShowTreeDetail(form->GetBrowser(), 0, form->GetProperties());
form->GetSqlPane()->SetReadOnly(false);
form->GetSqlPane()->SetText(((pgForeignKey *)obj)->GetSql(form->GetBrowser()));
form->GetSqlPane()->SetReadOnly(true);
}
form->GetMenuFactories()->CheckMenu(obj, form->GetMenuBar(), (ctlMenuToolbar *)form->GetToolBar());
return 0;
}
bool validateForeignKeyFactory::CheckEnable(pgObject *obj)
{
return obj && obj->IsCreatedBy(foreignKeyFactory) && obj->CanEdit()
&& ((pgForeignKey *)obj)->GetConnection()->BackendMinimumVersion(9, 1)
&& !((pgForeignKey *)obj)->GetValid();
}
|