Used slightly different approach. This tab is fully build in source code using subclasses to adjust behaviour of widgets for reuse in the other tabs. Uses custom proxy model for filtering triggers for correct table and supporting out of the box sorting by QTableView. SqlCodePreview: QPlainTextEditor which sql highlighter and in readonly mode but allows copy.
126 lines
2.9 KiB
C++
126 lines
2.9 KiB
C++
#include "PgTrigger.h"
|
|
#include "PgClassContainer.h"
|
|
#include "PgDatabaseCatalog.h"
|
|
#include "SqlFormattingUtils.h"
|
|
#include <QStringBuilder>
|
|
|
|
|
|
QString PgTrigger::dropSql(const PgDatabaseCatalog &catalog)
|
|
{
|
|
if (m_dropSql.isEmpty()) {
|
|
auto&& fqtablename = genFQTableName(catalog, catalog.classes()->getByKey(relid));
|
|
m_dropSql = "DROP TRIGGER " % quoteIdent(name)
|
|
% " ON " % fqtablename % ";";
|
|
}
|
|
return m_dropSql;
|
|
}
|
|
|
|
QString PgTrigger::createSql(const PgDatabaseCatalog &catalog)
|
|
{
|
|
if (m_createSql.isEmpty()) {
|
|
auto&& fqtablename = genFQTableName(catalog, catalog.classes()->getByKey(relid));
|
|
// if (GetLanguage() == wxT("edbspl"))
|
|
// sql += wxT("CREATE OR REPLACE TRIGGER ");
|
|
// else if (GetConnection()->BackendMinimumVersion(8, 2) && GetIsConstraint())
|
|
if (constraint != InvalidOid)
|
|
m_createSql += "CREATE CONSTRAINT TRIGGER ";
|
|
else
|
|
m_createSql += "CREATE TRIGGER ";
|
|
|
|
m_createSql += quoteIdent(name) + "\n "
|
|
+ typeFireWhen()
|
|
+ " " + event();
|
|
|
|
m_createSql += "\n ON " + fqtablename;
|
|
if (deferrable) {
|
|
m_createSql += "\n DEFERRABLE INITIALLY ";
|
|
if (initdeferred)
|
|
m_createSql += "DEFERRED";
|
|
else
|
|
m_createSql += "IMMEDIATE";
|
|
}
|
|
m_createSql += "\n FOR EACH " + forEach();
|
|
|
|
// if (GetConnection()->BackendMinimumVersion(8, 5)
|
|
// && !GetWhen().IsEmpty())
|
|
// sql += wxT("\n WHEN (") + GetWhen() + wxT(")");
|
|
|
|
// if (GetLanguage() == wxT("edbspl"))
|
|
// {
|
|
// sql += wxT("\n") + GetSource();
|
|
// if (!sql.Trim().EndsWith(wxT(";")))
|
|
// sql = sql.Trim() + wxT(";");
|
|
// sql += wxT("\n");
|
|
// }
|
|
// else
|
|
// {
|
|
// sql += wxT("\n EXECUTE PROCEDURE ") + triggerFunction->GetQuotedFullIdentifier()
|
|
// + wxT("(") + GetArguments() + wxT(")")
|
|
// + wxT(";\n");
|
|
// }
|
|
|
|
// if (!GetEnabled())
|
|
// {
|
|
// sql += wxT("ALTER TABLE ") + GetQuotedFullTable() + wxT(" ")
|
|
// + wxT("DISABLE TRIGGER ") + GetQuotedIdentifier() + wxT(";\n");
|
|
// }
|
|
|
|
// if (!GetComment().IsEmpty())
|
|
// sql += wxT("COMMENT ON TRIGGER ") + GetQuotedIdentifier() + wxT(" ON ") + GetQuotedFullTable()
|
|
// + wxT(" IS ") + qtDbString(GetComment()) + wxT(";\n");
|
|
}
|
|
|
|
return m_createSql;
|
|
}
|
|
|
|
|
|
QString PgTrigger::typeFireWhen() const
|
|
{
|
|
QString when;
|
|
|
|
if (type & TriggerTypeBefore)
|
|
when = "BEFORE";
|
|
else if (type & TriggerTypeInstead)
|
|
when = "INSTEAD OF";
|
|
else
|
|
when = "AFTER";
|
|
return when;
|
|
}
|
|
|
|
|
|
QString PgTrigger::eventAbbr() const
|
|
{
|
|
QString event;
|
|
if (type & TriggerTypeInsert)
|
|
event += "I";
|
|
if (type & TriggerTypeUpdate)
|
|
event += "U";
|
|
if (type & TriggerTypeDelete)
|
|
event += "D";
|
|
if (type & TriggerTypeTruncate)
|
|
event += "T";
|
|
return event;
|
|
}
|
|
|
|
QString PgTrigger::event() const
|
|
{
|
|
QString event;
|
|
if (type & TriggerTypeInsert)
|
|
event += "INSERT ";
|
|
if (type & TriggerTypeUpdate)
|
|
event += "UPDATE ";
|
|
if (type & TriggerTypeDelete)
|
|
event += "DELETE ";
|
|
if (type & TriggerTypeTruncate)
|
|
event += "TRUNCATE";
|
|
return event.trimmed();
|
|
}
|
|
|
|
QString PgTrigger::forEach() const
|
|
{
|
|
if (isRow())
|
|
return "ROW";
|
|
else
|
|
return "STATEMENT";
|
|
}
|
|
|