Обсуждение: Patch to enable/disable rule on a table

Поиск
Список
Период
Сортировка

Patch to enable/disable rule on a table

От
Guillaume Lelarge
Дата:
Hi all,

Here is a patch to support this new PostgreSQL 8.3 feature : the
possibility to enable and disable a rule on a table.

Regards.


--
Guillaume.
 http://www.postgresqlfr.org
 http://dalibo.com
Index: pgadmin/include/schema/pgRule.h
===================================================================
--- pgadmin/include/schema/pgRule.h    (révision 6675)
+++ pgadmin/include/schema/pgRule.h    (copie de travail)
@@ -61,7 +61,7 @@
     bool GetDoInstead() const { return doInstead; }
     void iSetDoInstead(const bool b) { doInstead=b; }
     bool GetEnabled() const { return enabled; }
-    void iSetEnabled(const bool b) { enabled=b; }
+    void iSetEnabled(const bool b);
     wxString GetQuotedFullTable() const { return quotedFullTable; }
     void iSetQuotedFullTable(const wxString &s) { quotedFullTable=s; }

@@ -79,4 +79,13 @@
     bool doInstead, enabled;
 };

+class enabledisableRuleFactory : public contextActionFactory
+{
+public:
+    enabledisableRuleFactory(menuFactoryList *list, wxMenu *mnu, wxToolBar *toolbar);
+    wxWindow *StartDialog(frmMain *form, pgObject *obj);
+    bool CheckEnable(pgObject *obj);
+    bool CheckChecked(pgObject *obj);
+};
+
 #endif
Index: pgadmin/frm/frmMain.cpp
===================================================================
--- pgadmin/frm/frmMain.cpp    (révision 6675)
+++ pgadmin/frm/frmMain.cpp    (copie de travail)
@@ -67,6 +67,7 @@
 #include "schema/pgTable.h"
 #include "schema/pgIndex.h"
 #include "schema/pgTrigger.h"
+#include "schema/pgRule.h"
 #include "schema/pgServer.h"
 #include "slony/slCluster.h"
 #include "slony/slSet.h"
@@ -266,6 +267,7 @@
     new enabledisableTriggerFactory(menuFactories, toolsMenu, 0);
     new disableAllTriggersFactory(menuFactories, toolsMenu, 0);
     new enableAllTriggersFactory(menuFactories, toolsMenu, 0);
+    new enabledisableRuleFactory(menuFactories, toolsMenu, 0);

     //--------------------------
     new separatorFactory(menuFactories);
Index: pgadmin/schema/pgRule.cpp
===================================================================
--- pgadmin/schema/pgRule.cpp    (révision 6675)
+++ pgadmin/schema/pgRule.cpp    (copie de travail)
@@ -14,6 +14,7 @@

 // App headers
 #include "pgAdmin3.h"
+#include "frm/frmMain.h"
 #include "utils/misc.h"
 #include "schema/pgRule.h"

@@ -36,6 +37,23 @@
 }


+void pgRule::iSetEnabled(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;
+       }
+
+
 wxString pgRule::GetSql(ctlTree *browser)
 {
     if (sql.IsNull())
@@ -176,3 +194,34 @@

 pgRuleFactory ruleFactory;
 static pgaCollectionFactory cf(&ruleFactory, __("Rules"), rules_xpm);
+
+
+enabledisableRuleFactory::enabledisableRuleFactory(menuFactoryList *list, wxMenu *mnu, wxToolBar *toolbar) :
contextActionFactory(list)
+{
+    mnu->Append(id, _("Rule enabled?"), _("Enable or disable selected rule."), wxITEM_CHECK);
+}
+
+
+wxWindow *enabledisableRuleFactory::StartDialog(frmMain *form, pgObject *obj)
+{
+    ((pgRule*)obj)->iSetEnabled(!((pgRule*)obj)->GetEnabled());
+
+    wxTreeItemId item=form->GetBrowser()->GetSelection();
+    if (obj == form->GetBrowser()->GetObject(item))
+        obj->ShowTreeDetail(form->GetBrowser(), 0, form->GetProperties());
+    form->GetMenuFactories()->CheckMenu(obj, form->GetMenuBar(), form->GetToolBar());
+
+    return 0;
+}
+
+
+bool enabledisableRuleFactory::CheckEnable(pgObject *obj)
+{
+    return obj && obj->IsCreatedBy(ruleFactory)
+        && ((pgRule*)obj)->GetConnection()->BackendMinimumVersion(8, 3);
+}
+
+bool enabledisableRuleFactory::CheckChecked(pgObject *obj)
+{
+    return obj && obj->IsCreatedBy(ruleFactory) && ((pgRule*)obj)->GetEnabled();
+}

Re: Patch to enable/disable rule on a table

От
Dave Page
Дата:
Guillaume Lelarge wrote:
> Hi all,
>
> Here is a patch to support this new PostgreSQL 8.3 feature : the
> possibility to enable and disable a rule on a table.
>
> Regards.
>
>

Filed for the next release.

Thanks, Dave

Re: Patch to enable/disable rule on a table

От
Dave Page
Дата:
Guillaume Lelarge wrote:
> Hi all,
>
> Here is a patch to support this new PostgreSQL 8.3 feature : the
> possibility to enable and disable a rule on a table.

Thanks, applied with minor changes:

- Correct the naming of pgRule::iSetEnabled to pgRule::SetEnabled, and
added iSetEnabled with the correct functionality. In a nutshell, the
functions prefixed with 'i' are used to setup the internal state of the
object (usually called by CreateObjects in the appropriate factory), but
not change it, whilst those without the 'i' actually change the object
and it's internal state. This was also broken in the Trigger
enable/disable code which I guess you probably copied - fixed there too.

- Added code to prevent the option being offered for rules under views
as the syntax isn't there to allow rules on views (or rules that make
views) to be enabled or disabled.

- Re-jigged the menus a touch as they were starting to look a little
messy with various new options being added.

Cheers, Dave.

Re: Patch to enable/disable rule on a table

От
Guillaume Lelarge
Дата:
Dave Page a écrit :
> Guillaume Lelarge wrote:
>> Hi all,
>>
>> Here is a patch to support this new PostgreSQL 8.3 feature : the
>> possibility to enable and disable a rule on a table.
>
> Thanks, applied with minor changes:
>
> - Correct the naming of pgRule::iSetEnabled to pgRule::SetEnabled, and
> added iSetEnabled with the correct functionality. In a nutshell, the
> functions prefixed with 'i' are used to setup the internal state of the
> object (usually called by CreateObjects in the appropriate factory), but
> not change it, whilst those without the 'i' actually change the object
> and it's internal state. This was also broken in the Trigger
> enable/disable code which I guess you probably copied - fixed there too.
>

You're right, I copied it... much easier, but alas much error prone :-/

Thanks for the info on the 'i' functions.

> - Added code to prevent the option being offered for rules under views
> as the syntax isn't there to allow rules on views (or rules that make
> views) to be enabled or disabled.
>
> - Re-jigged the menus a touch as they were starting to look a little
> messy with various new options being added.
>

Yes, I thought about this too but didn't find a way to do it properly.

Thanks for all.


--
Guillaume.
 http://www.postgresqlfr.org
 http://dalibo.com