Hi Andrew,
Andrew Lazarus a écrit :
> Wednesday, October 3, 2007, 1:12:52 AM, you wrote:
>
> GL> andrew@pillette.com a écrit :
>>> to go with INSERT, DELETE, etc.
>
> GL> I don't see much interest in a TRUNCATE script because there's nothing
> GL> more to add on the script. How about a simple "Truncate" or "Empty
> GL> Table" menu in the contextual menu ?
>
> Works for me! (Maybe even two: TRUNCATE and TRUNCATE ... CASCADE)
>
Here is the patch that adds a TRUNCATE and a TRUNCATE CASCADE menus.
They won't be in the 1.8 release but I hope it will be applied for the
next one.
Regards.
--
Guillaume.
http://www.postgresqlfr.org
http://dalibo.com
Index: include/schema/pgTable.h
===================================================================
--- include/schema/pgTable.h (révision 6713)
+++ include/schema/pgTable.h (copie de travail)
@@ -83,6 +83,7 @@
bool EnableTriggers(const bool b);
void UpdateRows();
bool DropObject(wxFrame *frame, ctlTree *browser, bool cascaded);
+ bool TruncateObject(bool cascaded);
bool CanView() { return true; }
bool CanMaintenance() { return true; }
bool CanBackup() { return true; }
@@ -203,4 +204,21 @@
bool CheckEnable(pgObject *obj);
};
+class truncateFactory : public contextActionFactory
+{
+public:
+ truncateFactory(menuFactoryList *list, wxMenu *mnu, wxToolBar *toolbar);
+ wxWindow *StartDialog(frmMain *form, pgObject *obj);
+ bool CheckEnable(pgObject *obj);
+};
+
+
+class truncateCascadedFactory : public contextActionFactory
+{
+public:
+ truncateCascadedFactory(menuFactoryList *list, wxMenu *mnu, wxToolBar *toolbar);
+ wxWindow *StartDialog(frmMain *form, pgObject *obj);
+ bool CheckEnable(pgObject *obj);
+};
+
#endif
Index: frm/frmMain.cpp
===================================================================
--- frm/frmMain.cpp (révision 6713)
+++ frm/frmMain.cpp (copie de travail)
@@ -321,6 +321,8 @@
new createFactory(menuFactories, editMenu, toolBar);
new dropFactory(menuFactories, editMenu, toolBar);
new dropCascadedFactory(menuFactories, editMenu, 0);
+ new truncateFactory(menuFactories, editMenu, 0);
+ new truncateCascadedFactory(menuFactories, editMenu, 0);
new separatorFactory(menuFactories);
Index: schema/pgTable.cpp
===================================================================
--- schema/pgTable.cpp (révision 6713)
+++ schema/pgTable.cpp (copie de travail)
@@ -133,6 +133,15 @@
}
+bool pgTable::TruncateObject(bool cascaded)
+{
+ wxString sql = wxT("TRUNCATE TABLE ") + this->GetSchema()->GetQuotedIdentifier() + wxT(".") +
this->GetQuotedIdentifier();
+ if (cascaded)
+ sql += wxT(" CASCADE");
+ return GetDatabase()->ExecuteVoid(sql);
+}
+
+
void pgTable::AppendStuff(wxString &sql, ctlTree *browser, pgaFactory &factory)
{
wxString tmp;
@@ -1063,3 +1072,48 @@
&& ((pgTable*)obj)->GetConnection()->BackendMinimumVersion(8, 1);
}
+truncateFactory::truncateFactory(menuFactoryList *list, wxMenu *mnu, wxToolBar *toolbar) : contextActionFactory(list)
+{
+ mnu->Append(id, _("&Truncate"), _("Truncate the selected table."));
+}
+
+
+wxWindow *truncateFactory::StartDialog(frmMain *form, pgObject *obj)
+{
+ if (wxMessageBox(_("Are you sure you wish to truncate this table?"), _("Truncate table"), wxYES_NO) == wxNO)
+ return 0;
+
+ ((pgTable*)obj)->TruncateObject(false);
+
+ return 0;
+}
+
+
+bool truncateFactory::CheckEnable(pgObject *obj)
+{
+ return obj && obj->IsCreatedBy(tableFactory);
+}
+
+
+truncateCascadedFactory::truncateCascadedFactory(menuFactoryList *list, wxMenu *mnu, wxToolBar *toolbar) :
contextActionFactory(list)
+{
+ mnu->Append(id, _("Truncate Cascaded"), _("Truncate the selected table and all objects dependent on it."));
+}
+
+
+wxWindow *truncateCascadedFactory::StartDialog(frmMain *form, pgObject *obj)
+{
+ if (wxMessageBox(_("Are you sure you wish to truncate this table and all tables that have foreign-key references
tothis table?"), _("Truncate table cascaded"), wxYES_NO) == wxNO)
+ return 0;
+
+ ((pgTable*)obj)->TruncateObject(true);
+
+ return 0;
+}
+
+
+bool truncateCascadedFactory::CheckEnable(pgObject *obj)
+{
+ return obj && obj->IsCreatedBy(tableFactory) && ((pgTable*)obj)->GetConnection()->BackendMinimumVersion(8, 2);
+}
+