Overview of triggers extended with function name and arguments.

Did a lot of refactoring on the catalog to keep things clean.
This commit is contained in:
eelke 2018-11-18 19:30:45 +01:00
parent 35813ae926
commit fcb191f2cc
44 changed files with 797 additions and 404 deletions

View file

@ -1,33 +1,37 @@
#include "PgTrigger.h"
#include "PgClassContainer.h"
#include "PgDatabaseCatalog.h"
#include "PgProcContainer.h"
#include "SqlFormattingUtils.h"
#include <QStringBuilder>
QString PgTrigger::objectName() const
{
return name;
}
QString PgTrigger::dropSql(const PgDatabaseCatalog &catalog)
QString PgTrigger::dropSql()
{
if (m_dropSql.isEmpty()) {
auto&& fqtablename = genFQTableName(catalog, catalog.classes()->getByKey(relid));
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)
QString PgTrigger::createSql()
{
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())
auto&& fqtablename = genFQTableName(catalog(), *catalog().classes()->getByKey(relid));
auto&& triggername = quoteIdent(name);
if (constraint != InvalidOid)
m_createSql += "CREATE CONSTRAINT TRIGGER ";
else
m_createSql += "CREATE TRIGGER ";
m_createSql += quoteIdent(name) + "\n "
m_createSql += triggername + "\n "
+ typeFireWhen()
+ " " + event();
@ -41,29 +45,14 @@ QString PgTrigger::createSql(const PgDatabaseCatalog &catalog)
}
m_createSql += "\n FOR EACH " + forEach();
// if (GetConnection()->BackendMinimumVersion(8, 5)
// && !GetWhen().IsEmpty())
// sql += wxT("\n WHEN (") + GetWhen() + wxT(")");
// requires atleast 8.5 don;t think we have to support older
if (!whenclause.isEmpty())
m_createSql += "\n WHEN (" + whenclause + ")";
// 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");
// }
m_createSql += QString("\n EXECUTE PROCEDURE %1;\n").arg(procedure());
// if (!GetEnabled())
// {
// sql += wxT("ALTER TABLE ") + GetQuotedFullTable() + wxT(" ")
// + wxT("DISABLE TRIGGER ") + GetQuotedIdentifier() + wxT(";\n");
// }
if (!enabled)
m_createSql += QString("ALTER TABLE %1 DISABLE TRIGGER %2;\n").arg(fqtablename, triggername);
// if (!GetComment().IsEmpty())
// sql += wxT("COMMENT ON TRIGGER ") + GetQuotedIdentifier() + wxT(" ON ") + GetQuotedFullTable()
@ -124,3 +113,49 @@ QString PgTrigger::forEach() const
return "STATEMENT";
}
QString PgTrigger::procedure() const
{
const PgProc *proc = catalog().procs()->getByKey(foid);
QString func_name = proc->fullyQualifiedQuotedObjectName();
return QString("%1(%2)").arg(func_name, arguments());
}
QString PgTrigger::arguments() const
{
QString arglist;
if (nargs > 0)
arglist = args;
QString output;
while (!arglist.isEmpty()) {
int pos = arglist.indexOf(QChar::Null);
if (pos != 0) {
QString arg;
if (pos > 0)
arg = arglist.left(pos);
else
arg = arglist;
if (!output.isEmpty())
output += ", ";
bool conversion_ok = false;
arg.toLongLong(&conversion_ok);
if (conversion_ok)
output += arg;
else
output += escapeLiteral(arg);
}
else {
if (!output.isEmpty())
output += ", ";
output += escapeLiteral(QString());
}
if (pos >= 0)
arglist = arglist.mid(pos + 4);
else
break;
}
return output;
}