2018-11-18 19:30:45 +01:00
|
|
|
|
#include "PgServerObject.h"
|
2018-12-24 07:51:27 +01:00
|
|
|
|
#include "ArrayParser.h"
|
2018-12-25 13:17:04 +01:00
|
|
|
|
#include "PgAuthIdContainer.h"
|
|
|
|
|
|
#include "PgDatabaseCatalog.h"
|
|
|
|
|
|
#include "SqlFormattingUtils.h"
|
2018-12-24 07:51:27 +01:00
|
|
|
|
#include <QStringBuilder>
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
void PgServerObject::setOwnerOid(Oid oid)
|
2018-12-24 07:51:27 +01:00
|
|
|
|
{
|
2018-12-25 13:17:04 +01:00
|
|
|
|
m_ownerOid = oid;
|
|
|
|
|
|
m_owner = catalog().authIds()->getByKey(oid);
|
2018-12-24 07:51:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
Oid PgServerObject::ownerOid() const
|
2018-12-24 07:51:27 +01:00
|
|
|
|
{
|
2018-12-25 13:17:04 +01:00
|
|
|
|
return m_ownerOid;
|
2018-12-24 07:51:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-25 16:39:58 +01:00
|
|
|
|
bool PgServerObject::hasOwner() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_ownerOid != InvalidOid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
QString PgServerObject::ownerName() const
|
2018-12-24 07:51:27 +01:00
|
|
|
|
{
|
2018-12-25 16:39:58 +01:00
|
|
|
|
return m_owner->objectName();
|
2018-12-24 07:51:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
const PgAuthId* PgServerObject::owner() const
|
2018-12-24 07:51:27 +01:00
|
|
|
|
{
|
2018-12-25 13:17:04 +01:00
|
|
|
|
return m_owner;
|
2018-12-24 07:51:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
QString PgServerObject::alterOwnerSql(const QString& ident) const
|
2018-12-24 07:51:27 +01:00
|
|
|
|
{
|
2018-12-25 13:17:04 +01:00
|
|
|
|
return QString("\nALTER %1 OWNER TO %2;").arg(ident, quoteIdent(ownerName()));
|
2018-12-24 07:51:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
void PgServerObject::setAcls(boost::optional<AclList> acls)
|
2018-12-24 07:51:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_acls = std::move(acls);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString PgServerObject::aclString() const
|
|
|
|
|
|
{
|
|
|
|
|
|
QString result;
|
2018-12-25 13:17:04 +01:00
|
|
|
|
if (m_acls) {
|
|
|
|
|
|
for (auto&& acl : *m_acls) {
|
|
|
|
|
|
if (result.isEmpty()) result += "{";
|
|
|
|
|
|
else result += ",";
|
|
|
|
|
|
result += acl.singleString();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!result.isEmpty())
|
|
|
|
|
|
result += "}";
|
2018-12-24 07:51:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
QString PgServerObject::grantSql() const
|
2018-12-24 07:51:27 +01:00
|
|
|
|
{
|
2018-12-25 13:17:04 +01:00
|
|
|
|
// object_type_and_name => TABLE X
|
|
|
|
|
|
// GRANT ??? ON object_type_and_name TO ...
|
2018-12-24 07:51:27 +01:00
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
const QString column; ///< \todo should be set when called on a column object
|
2018-12-24 07:51:27 +01:00
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
QString all_pattern = aclAllPattern();
|
|
|
|
|
|
if (all_pattern.isEmpty())
|
|
|
|
|
|
return {};
|
2018-12-24 07:51:27 +01:00
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
QString grant;
|
|
|
|
|
|
auto grant_on_object = QString("%1 %2").arg(typeName(), fullyQualifiedQuotedObjectName());
|
|
|
|
|
|
if (m_acls) {
|
|
|
|
|
|
for (auto&& acl : *m_acls) {
|
|
|
|
|
|
grant += acl.sql(all_pattern, grant_on_object, column);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
// PUBLIC, no priviliges
|
|
|
|
|
|
grant += PgAcl("=").sql(all_pattern, grant_on_object, column);
|
|
|
|
|
|
// owner no privileges
|
2018-12-25 16:39:58 +01:00
|
|
|
|
if (hasOwner())
|
|
|
|
|
|
grant += PgAcl(ownerName() + "=").sql(all_pattern, grant_on_object, column);
|
2018-12-25 13:17:04 +01:00
|
|
|
|
}
|
2018-12-24 07:51:27 +01:00
|
|
|
|
return grant;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-25 13:17:04 +01:00
|
|
|
|
QString PgServerObject::aclAllPattern() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
2019-02-09 09:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
QString PgServerObject::dropSql() const
|
|
|
|
|
|
{
|
2022-01-20 20:13:56 +01:00
|
|
|
|
return "DROP " % typeName() % " " % fullyQualifiedQuotedObjectName() % ";";
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString PgServerObject::createSql() const
|
|
|
|
|
|
{
|
2022-01-20 20:13:56 +01:00
|
|
|
|
return {};
|
2019-02-09 09:49:27 +01:00
|
|
|
|
}
|
2022-01-20 20:13:56 +01:00
|
|
|
|
|
|
|
|
|
|
QString PgServerObject::commentSql() const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (description.isEmpty())
|
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
|
|
return "COMMENT ON " + typeName() + " " + fullyQualifiedQuotedObjectName()
|
|
|
|
|
|
+ " IS " + escapeLiteral(description) + ";";
|
|
|
|
|
|
}
|
|
|
|
|
|
|