2018-01-06 21:22:22 +01:00
|
|
|
|
#include "SqlFormattingUtils.h"
|
|
|
|
|
|
#include <QStringBuilder>
|
|
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
2018-12-16 10:17:59 +01:00
|
|
|
|
#include "catalog/PgKeywordList.h"
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
2018-12-16 10:17:59 +01:00
|
|
|
|
#include "catalog/PgConstraint.h"
|
|
|
|
|
|
#include "catalog/PgAttributeContainer.h"
|
|
|
|
|
|
#include "catalog/PgClass.h"
|
|
|
|
|
|
#include "catalog/PgClassContainer.h"
|
|
|
|
|
|
#include "catalog/PgIndex.h"
|
|
|
|
|
|
#include "catalog/PgNamespace.h"
|
|
|
|
|
|
#include "catalog/PgNamespaceContainer.h"
|
|
|
|
|
|
#include "catalog/PgDatabaseCatalog.h"
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
2018-11-18 19:30:45 +01:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
2022-07-08 19:53:45 +02:00
|
|
|
|
QString escapeInternal(const QString &input, QChar quote_char)
|
2018-11-18 19:30:45 +01:00
|
|
|
|
{
|
2022-07-08 19:53:45 +02:00
|
|
|
|
int num_quotes = 0; /* single or double, depending on as_ident */
|
2018-11-18 19:30:45 +01:00
|
|
|
|
const int len = input.length();
|
2022-07-08 19:53:45 +02:00
|
|
|
|
for (int idx = 0; idx < len; ++idx)
|
|
|
|
|
|
if (input[idx] == quote_char)
|
2018-11-18 19:30:45 +01:00
|
|
|
|
++num_quotes;
|
|
|
|
|
|
|
|
|
|
|
|
int output_size = len + num_quotes + 2; // + 2 for the quotes
|
|
|
|
|
|
|
2022-07-08 19:53:45 +02:00
|
|
|
|
QString output;
|
|
|
|
|
|
output.reserve(output_size);
|
2018-11-18 19:30:45 +01:00
|
|
|
|
output += quote_char;
|
2022-07-08 19:53:45 +02:00
|
|
|
|
if (num_quotes == 0) {
|
2018-11-18 19:30:45 +01:00
|
|
|
|
output += input;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
for (int idx = 0; idx < len; ++idx) {
|
|
|
|
|
|
QChar c = input[idx];
|
|
|
|
|
|
output += c;
|
2022-07-08 19:53:45 +02:00
|
|
|
|
if (c == quote_char)
|
2018-11-18 19:30:45 +01:00
|
|
|
|
output += c;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
output += quote_char;
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString escapeIdent(const QString &input)
|
|
|
|
|
|
{
|
2022-07-08 19:53:45 +02:00
|
|
|
|
return escapeInternal(input, '"');
|
2018-11-18 19:30:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString escapeLiteral(const QString &input)
|
|
|
|
|
|
{
|
2022-07-08 19:53:45 +02:00
|
|
|
|
return escapeInternal(input, '\'');
|
2018-11-18 19:30:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-06 21:22:22 +01:00
|
|
|
|
bool identNeedsQuotes(QString ident)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ident[0].isDigit())
|
|
|
|
|
|
return true;
|
|
|
|
|
|
for (auto c : ident)
|
2018-11-29 20:21:36 +01:00
|
|
|
|
if ((c < 'a' || c > 'z') && c != '_' && (c < '0' || c > '9'))
|
2018-01-06 21:22:22 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
auto kw = getPgsqlKeyword(ident);
|
|
|
|
|
|
if (kw == nullptr)
|
|
|
|
|
|
return false;
|
2026-10-18 15:30:50 +02:00
|
|
|
|
if (kw->getCategory() == UNRESERVED_KEYWORD)
|
2018-01-06 21:22:22 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString quoteIdent(QString ident)
|
|
|
|
|
|
{
|
|
|
|
|
|
assert(ident.length() > 0);
|
|
|
|
|
|
|
2026-10-17 16:21:22 +02:00
|
|
|
|
static const QChar dquote = u'"';
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
if (identNeedsQuotes(ident)) {
|
|
|
|
|
|
QString out;
|
|
|
|
|
|
out += dquote;
|
|
|
|
|
|
out += ident.replace("\"", "\"\"");
|
|
|
|
|
|
out += dquote;
|
|
|
|
|
|
return out;
|
|
|
|
|
|
}
|
2026-10-18 15:30:50 +02:00
|
|
|
|
return ident;
|
2018-01-06 21:22:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-25 09:06:01 +01:00
|
|
|
|
QString dollarQuoteString(const QString &value)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString def_tag = "BODY";
|
|
|
|
|
|
QString tag = QString("$%1$").arg(def_tag);
|
|
|
|
|
|
|
|
|
|
|
|
int counter = 1;
|
|
|
|
|
|
while (value.indexOf(tag) >= 0)
|
|
|
|
|
|
tag = QString("$%1%2$").arg(def_tag, counter++);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return tag
|
|
|
|
|
|
+ value
|
|
|
|
|
|
+ tag;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-25 19:45:06 +01:00
|
|
|
|
//QString genSchemaPrefix(const PgNamespace &ns)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// QString str;
|
|
|
|
|
|
// if (!ns.name.isEmpty()) {
|
|
|
|
|
|
// str = quoteIdent(ns.name) % QString::fromUtf16(u".");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return str;
|
|
|
|
|
|
//}
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-11-25 19:45:06 +01:00
|
|
|
|
//QString genFQTableName(const PgDatabaseCatalog &catalog, const PgClass &cls)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// auto ns = catalog.namespaces()->getByKey(cls.relnamespace);
|
|
|
|
|
|
////cls.fullyQualifiedQuotedObjectName()
|
|
|
|
|
|
// return ns->quotedObjectName() % "." % cls.quotedObjectName();
|
|
|
|
|
|
//}
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
2018-11-30 18:41:38 +01:00
|
|
|
|
QString genAlterTable(const PgDatabaseCatalog &, const PgClass &cls)
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
2018-11-25 19:45:06 +01:00
|
|
|
|
return "ALTER TABLE " % cls.fullyQualifiedQuotedObjectName(); // genFQTableName(catalog, cls);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString getDropConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
|
|
|
|
|
{
|
2018-11-18 19:30:45 +01:00
|
|
|
|
const PgClass *cls = catalog.classes()->getByKey(constraint.relid);
|
2018-11-30 18:41:38 +01:00
|
|
|
|
return genAlterTable(catalog, *cls) % " DROP CONSTRAINT " % quoteIdent(constraint.objectName()) % ";";
|
2018-01-06 21:22:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Helper class that builds comma seperated identifier list
|
|
|
|
|
|
class IdentListString {
|
|
|
|
|
|
public:
|
|
|
|
|
|
void add(QString ident)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_str.isEmpty())
|
|
|
|
|
|
m_str += ",";
|
|
|
|
|
|
m_str += quoteIdent(ident);
|
|
|
|
|
|
}
|
|
|
|
|
|
const QString& str() const { return m_str; }
|
|
|
|
|
|
private:
|
|
|
|
|
|
QString m_str;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-02-05 21:42:54 +01:00
|
|
|
|
QString getColumnNameList(const PgDatabaseCatalog &catalog, Oid relid, const SmallAttNumVec<5> &attnums)
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
IdentListString result;
|
|
|
|
|
|
|
|
|
|
|
|
const auto ac = catalog.attributes();
|
|
|
|
|
|
for (auto an : attnums) {
|
2018-11-18 19:30:45 +01:00
|
|
|
|
result.add(ac->getByKey({ relid, an })->name);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
return result.str();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-01 07:09:18 +01:00
|
|
|
|
QString getForeignKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint, const QString &indent)
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
//PgClass cls = catalog.classes()->getByKey(constraint.relid);
|
2018-11-18 19:30:45 +01:00
|
|
|
|
const PgClass *fcls = catalog.classes()->getByKey(constraint.frelid);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
QString deferrable;
|
|
|
|
|
|
QString validated;
|
|
|
|
|
|
if (!constraint.validated)
|
|
|
|
|
|
validated += " NOT VALID";
|
|
|
|
|
|
if (constraint.deferrable) {
|
|
|
|
|
|
deferrable = QLatin1String(" DEFERRABLE INITIALLY ") % (constraint.deferred ? "DEFERRED" : "IMMEDIATE");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-01 07:09:18 +01:00
|
|
|
|
return "\n" + indent + "FOREIGN KEY ("
|
|
|
|
|
|
% getColumnNameList(catalog, constraint.relid, constraint.key) % ")\n" + indent + "REFERENCES "
|
2018-11-25 19:45:06 +01:00
|
|
|
|
% fcls->fullyQualifiedQuotedObjectName() % " ("
|
2019-12-01 07:09:18 +01:00
|
|
|
|
% getColumnNameList(catalog, constraint.frelid, constraint.fkey) % ")\n" + indent + "MATCH "
|
2018-01-06 21:22:22 +01:00
|
|
|
|
% ForeignKeyMatchToString(constraint.fmatchtype)
|
|
|
|
|
|
% " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype)
|
|
|
|
|
|
% " ON DELETE " % ForeignKeyActionToString(constraint.fdeltype)
|
2018-12-03 21:03:49 +01:00
|
|
|
|
% deferrable % validated;
|
2018-11-10 13:36:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString getForeignKeyConstraintReferences(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
|
|
|
|
|
{
|
2018-11-18 19:30:45 +01:00
|
|
|
|
const PgClass *fcls = catalog.classes()->getByKey(constraint.frelid);
|
2018-11-10 13:36:36 +01:00
|
|
|
|
QString deferrable;
|
|
|
|
|
|
QString validated;
|
|
|
|
|
|
if (!constraint.validated)
|
|
|
|
|
|
validated += " NOT VALID";
|
|
|
|
|
|
if (constraint.deferrable) {
|
|
|
|
|
|
deferrable = QLatin1String(" DEFERRABLE INITIALLY ") % (constraint.deferred ? "DEFERRED" : "IMMEDIATE");
|
|
|
|
|
|
}
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
2018-11-10 13:36:36 +01:00
|
|
|
|
return "REFERENCES "
|
2018-11-25 19:45:06 +01:00
|
|
|
|
% fcls->fullyQualifiedQuotedObjectName() % " ("
|
2018-11-10 13:36:36 +01:00
|
|
|
|
% getColumnNameList(catalog, constraint.frelid, constraint.fkey) % ") MATCH "
|
|
|
|
|
|
% ForeignKeyMatchToString(constraint.fmatchtype)
|
|
|
|
|
|
% " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype)
|
|
|
|
|
|
% " ON DELETE " % ForeignKeyActionToString(constraint.fdeltype)
|
|
|
|
|
|
% deferrable % validated;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString getForeignKeyConstraintReferencesShort(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
|
|
|
|
|
{
|
2018-11-18 19:30:45 +01:00
|
|
|
|
const PgClass *fcls = catalog.classes()->getByKey(constraint.frelid);
|
2018-11-10 13:36:36 +01:00
|
|
|
|
QString deferrable;
|
|
|
|
|
|
QString validated;
|
|
|
|
|
|
if (!constraint.validated)
|
|
|
|
|
|
validated += " NOT VALID";
|
|
|
|
|
|
if (constraint.deferrable) {
|
|
|
|
|
|
deferrable = QLatin1String(" DEFERRABLE") % (constraint.deferred ? " INITIALLY DEFERRED" : "");
|
|
|
|
|
|
}
|
|
|
|
|
|
QString on_update = constraint.fupdtype == ForeignKeyAction::NoAction ? QString() : " ON UPDATE " % ForeignKeyActionToString(constraint.fupdtype);
|
|
|
|
|
|
QString on_delete = constraint.fdeltype == ForeignKeyAction::NoAction ? QString() : " ON DELETE " % ForeignKeyActionToString(constraint.fdeltype);
|
|
|
|
|
|
QString match_type = constraint.fmatchtype == ForeignKeyMatch::Simple ? QString() : " MATCH " % ForeignKeyMatchToString(constraint.fmatchtype);
|
|
|
|
|
|
|
2018-11-25 19:45:06 +01:00
|
|
|
|
return fcls->fullyQualifiedQuotedObjectName() % " ("
|
2018-11-10 13:36:36 +01:00
|
|
|
|
% getColumnNameList(catalog, constraint.frelid, constraint.fkey) % ")"
|
|
|
|
|
|
% match_type
|
|
|
|
|
|
% on_update
|
|
|
|
|
|
% on_delete
|
|
|
|
|
|
% deferrable % validated;
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString getPrimaryKeyConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString ddl = " PRIMARY KEY ("
|
2018-11-30 18:41:38 +01:00
|
|
|
|
% getColumnNameList(catalog, constraint.relid, constraint.key) % ")";
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
return ddl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString getUniqueConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString ddl = " UNIQUE ("
|
2018-11-30 18:41:38 +01:00
|
|
|
|
% getColumnNameList(catalog, constraint.relid, constraint.key) % ")";
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
return ddl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-30 18:41:38 +01:00
|
|
|
|
QString getAlterTableConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint)
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
2018-11-18 19:30:45 +01:00
|
|
|
|
const PgClass *cls = catalog.classes()->getByKey(constraint.relid);
|
2018-11-30 18:41:38 +01:00
|
|
|
|
QString result = genAlterTable(catalog, *cls) % "\n ADD ";
|
2019-12-01 07:09:18 +01:00
|
|
|
|
result += getConstraintDefinition(catalog, constraint, " ") % ";";
|
2018-11-30 18:41:38 +01:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-01 07:09:18 +01:00
|
|
|
|
QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstraint &constraint, const QString &indent)
|
2018-11-30 18:41:38 +01:00
|
|
|
|
{
|
|
|
|
|
|
QString result = "CONSTRAINT " % quoteIdent(constraint.objectName());
|
2018-01-06 21:22:22 +01:00
|
|
|
|
switch (constraint.type) {
|
|
|
|
|
|
case ConstraintType::ForeignKey:
|
2019-12-01 07:09:18 +01:00
|
|
|
|
result += getForeignKeyConstraintDefinition(catalog, constraint, indent);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case ConstraintType::PrimaryKey:
|
|
|
|
|
|
result += getPrimaryKeyConstraintDefinition(catalog, constraint);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ConstraintType::Unique:
|
|
|
|
|
|
result += getUniqueConstraintDefinition(catalog, constraint);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ConstraintType::Check:
|
|
|
|
|
|
default:
|
|
|
|
|
|
result = result % " " % constraint.definition;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|