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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// pgRule.cpp - Rule class
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "frm/frmMain.h"
#include "utils/misc.h"
#include "schema/pgRule.h"
pgRule::pgRule(pgSchema *newSchema, const wxString &newName)
: pgRuleObject(newSchema, ruleFactory, newName)
{
}
pgRule::~pgRule()
{
}
wxString pgRule::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on rule");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing rule");
message += wxT(" ") + GetName();
break;
case GRANTWIZARDTITLE:
message = _("Privileges for rule");
message += wxT(" ") + GetName();
break;
case DROPINCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop rule \"%s\" including all objects that depend on it?"),
GetFullIdentifier().c_str());
break;
case DROPEXCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop rule \"%s\"?"),
GetFullIdentifier().c_str());
break;
case DROPCASCADETITLE:
message = _("Drop rule cascaded?");
break;
case DROPTITLE:
message = _("Drop rule?");
break;
case PROPERTIESREPORT:
message = _("Rule properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("Rule properties");
break;
case DDLREPORT:
message = _("Rule DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("Rule DDL");
break;
case DEPENDENCIESREPORT:
message = _("Rule dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("Rule dependencies");
break;
case DEPENDENTSREPORT:
message = _("Rule dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("Rule dependents");
break;
}
return message;
}
int pgRule::GetIconId()
{
if (GetEnabled())
return ruleFactory.GetIconId();
else
return ruleFactory.GetClosedIconId();
}
bool pgRule::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
wxString sql = wxT("DROP RULE ") + GetQuotedIdentifier() + wxT(" ON ") + GetQuotedFullTable();
if (cascaded)
sql += wxT(" CASCADE");
return GetDatabase()->ExecuteVoid(sql);
}
void pgRule::SetEnabled(ctlTree *browser, const bool b)
{
if (GetQuotedFullTable().Len() > 0 && ((enabled && !b) || (!enabled && b)))
{
wxString sql = wxT("ALTER TABLE ") + GetQuotedFullTable() + wxT(" ");
if (enabled && !b)
sql += wxT("DISABLE");
else if (!enabled && b)
sql += wxT("ENABLE");
sql += wxT(" RULE ") + GetQuotedIdentifier();
GetDatabase()->ExecuteVoid(sql);
}
enabled = b;
UpdateIcon(browser);
}
wxString pgRule::GetSql(ctlTree *browser)
{
if (sql.IsNull())
{
sql = wxT("-- Rule: ") + GetQuotedIdentifier() + wxT(" ON ") + GetQuotedFullTable() + wxT("\n\n")
+ wxT("-- DROP RULE ") + GetQuotedIdentifier() + wxT(" ON ") + GetQuotedFullTable() + wxT(";\n\n")
+ wxT("CREATE OR REPLACE") + GetFormattedDefinition().Mid(6) // the backend pg_get_ruledef gives CREATE only
+ wxT("\n");
if (!GetEnabled())
{
sql += wxT("ALTER TABLE ") + GetQuotedFullTable() + wxT(" ")
+ wxT("DISABLE RULE ") + GetQuotedIdentifier() + wxT(";\n");
}
if (!GetComment().IsEmpty())
sql += wxT("COMMENT ON RULE ") + GetQuotedIdentifier() + wxT(" ON ") + GetQuotedFullTable()
+ wxT(" IS ") + qtDbString(GetComment()) + wxT(";\n");
}
return sql;
}
void pgRule::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
if (properties)
{
CreateListColumns(properties);
wxString def = GetFormattedDefinition();
if (!def.IsEmpty())
{
int doPos = def.Find(wxT(" DO INSTEAD "));
if (doPos > 0)
def = def.Mid(doPos + 12).Strip(wxString::both);
else
{
doPos = def.Find(wxT(" DO "));
if (doPos > 0)
def = def.Mid(doPos + 4).Strip(wxString::both);
}
}
properties->AppendItem(_("Name"), GetName());
properties->AppendItem(_("OID"), GetOid());
properties->AppendItem(_("Event"), GetEvent());
properties->AppendItem(_("Condition"), GetCondition());
properties->AppendYesNoItem(_("Do instead?"), GetDoInstead());
properties->AppendItem(_("Definition"), firstLineOnly(def));
if (this->GetDatabase()->connection()->BackendMinimumVersion(8, 3))
properties->AppendYesNoItem(_("Enabled?"), GetEnabled());
properties->AppendYesNoItem(_("System rule?"), GetSystemObject());
properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
}
}
pgObject *pgRule::Refresh(ctlTree *browser, const wxTreeItemId item)
{
pgObject *rule = 0;
pgCollection *coll = browser->GetParentCollection(item);
if (coll)
rule = ruleFactory.CreateObjects(coll, 0, wxT("\n AND rw.oid=") + GetOidStr());
return rule;
}
pgObject *pgRuleFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
{
pgRule *rule = 0;
pgSet *rules = collection->GetDatabase()->ExecuteSet(
wxT("SELECT rw.oid, rw.*, relname, CASE WHEN relkind = 'r' THEN TRUE ELSE FALSE END AS parentistable, nspname, description,\n")
wxT(" pg_get_ruledef(rw.oid") + collection->GetDatabase()->GetPrettyOption() + wxT(") AS definition\n")
wxT(" FROM pg_rewrite rw\n")
wxT(" JOIN pg_class cl ON cl.oid=rw.ev_class\n")
wxT(" JOIN pg_namespace nsp ON nsp.oid=cl.relnamespace\n")
wxT(" LEFT OUTER JOIN pg_description des ON (des.objoid=rw.oid AND des.classoid='pg_rewrite'::regclass)\n")
wxT(" WHERE ev_class = ") + NumToStr(collection->GetOid())
+ restriction + wxT("\n")
wxT(" ORDER BY rw.rulename"));
if (rules)
{
while (!rules->Eof())
{
// Be careful that the schema of a rule (and a trigger) is the schema of the schema
rule = new pgRule(collection->GetSchema()->GetSchema(), rules->GetVal(wxT("rulename")));
rule->iSetOid(rules->GetOid(wxT("oid")));
rule->iSetComment(rules->GetVal(wxT("description")));
if (collection->GetDatabase()->connection()->BackendMinimumVersion(8, 3))
{
if (rules->GetVal(wxT("ev_enabled")) != wxT("D"))
rule->iSetEnabled(true);
else
rule->iSetEnabled(false);
}
rule->iSetParentIsTable(rules->GetBool(wxT("parentistable")));
rule->iSetDoInstead(rules->GetBool(wxT("is_instead")));
rule->iSetAction(rules->GetVal(wxT("ev_action")));
wxString definition = rules->GetVal(wxT("definition"));
int doPos = definition.Find(wxT(" DO "));
int wherePos = definition.Find(wxT(" WHERE "));
if (wherePos > 0 && wherePos < doPos)
rule->iSetCondition(definition.Mid(wherePos + 7, doPos - wherePos - 7));
rule->iSetDefinition(definition);
rule->iSetQuotedFullTable(collection->GetDatabase()->GetQuotedSchemaPrefix(rules->GetVal(wxT("nspname")))
+ qtIdent(rules->GetVal(wxT("relname"))));
const wxChar *evts[] = {0, wxT("SELECT"), wxT("UPDATE"), wxT("INSERT"), wxT("DELETE")};
int evno = StrToLong(rules->GetVal(wxT("ev_type")));
if (evno > 0 && evno < 5)
rule->iSetEvent(evts[evno]);
else
rule->iSetEvent(wxT("Unknown"));
if (browser)
{
browser->AppendObject(collection, rule);
rules->MoveNext();
}
else
break;
}
delete rules;
}
return rule;
}
/////////////////////////////
pgRuleCollection::pgRuleCollection(pgaFactory *factory, pgSchema *sch)
: pgSchemaObjCollection(factory, sch)
{
}
wxString pgRuleCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on rules");
break;
case REFRESHINGDETAILS:
message = _("Refreshing rules");
break;
case OBJECTSLISTREPORT:
message = _("Rules list report");
break;
}
return message;
}
/////////////////////////////
#include "images/rule.pngc"
#include "images/rulebad.pngc"
#include "images/rules.pngc"
pgRuleFactory::pgRuleFactory()
: pgSchemaObjFactory(__("Rule"), __("New Rule..."), __("Create a new Rule."), rule_png_img)
{
metaType = PGM_RULE;
closedId = addIcon(rulebad_png_img);
}
pgCollection *pgRuleFactory::CreateCollection(pgObject *obj)
{
return new pgRuleCollection(GetCollectionFactory(), (pgSchema *)obj);
}
pgRuleFactory ruleFactory;
static pgaCollectionFactory cf(&ruleFactory, __("Rules"), rules_png_img);
enabledisableRuleFactory::enabledisableRuleFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
{
mnu->Append(id, _("Rule enabled?"), _("Enable or disable selected rule."), wxITEM_CHECK);
}
wxWindow *enabledisableRuleFactory::StartDialog(frmMain *form, pgObject *obj)
{
((pgRule *)obj)->SetEnabled(form->GetBrowser(), !((pgRule *)obj)->GetEnabled());
((pgRule *)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(((pgRule *)obj)->GetSql(form->GetBrowser()));
form->GetSqlPane()->SetReadOnly(true);
}
form->GetMenuFactories()->CheckMenu(obj, form->GetMenuBar(), (ctlMenuToolbar *)form->GetToolBar());
return 0;
}
bool enabledisableRuleFactory::CheckEnable(pgObject *obj)
{
return obj && obj->IsCreatedBy(ruleFactory)
&& ((pgRule *)obj)->GetConnection()->BackendMinimumVersion(8, 3)
&& ((pgRule *)obj)->GetParentIsTable();
}
bool enabledisableRuleFactory::CheckChecked(pgObject *obj)
{
return obj && obj->IsCreatedBy(ruleFactory) && ((pgRule *)obj)->GetEnabled();
}
|