2018-10-07 19:40:06 +02:00
|
|
|
|
#include "PgTrigger.h"
|
|
|
|
|
|
#include "PgClassContainer.h"
|
|
|
|
|
|
#include "PgDatabaseCatalog.h"
|
|
|
|
|
|
#include "SqlFormattingUtils.h"
|
|
|
|
|
|
#include <QStringBuilder>
|
2018-09-23 10:43:32 +02:00
|
|
|
|
|
2018-10-07 19:40:06 +02:00
|
|
|
|
|
|
|
|
|
|
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
|
2018-09-23 10:43:32 +02:00
|
|
|
|
{
|
2018-10-07 19:40:06 +02:00
|
|
|
|
QString when;
|
2018-09-23 10:43:32 +02:00
|
|
|
|
|
2018-10-07 19:40:06 +02:00
|
|
|
|
if (type & TriggerTypeBefore)
|
|
|
|
|
|
when = "BEFORE";
|
|
|
|
|
|
else if (type & TriggerTypeInstead)
|
|
|
|
|
|
when = "INSTEAD OF";
|
|
|
|
|
|
else
|
|
|
|
|
|
when = "AFTER";
|
|
|
|
|
|
return when;
|
2018-09-23 10:43:32 +02:00
|
|
|
|
}
|
2018-10-07 19:40:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
}
|
|
|
|
|
|
|