pgLab/pglablib/ResultSourceMapping.h

62 lines
1.9 KiB
C
Raw Permalink Normal View History

2026-10-18 07:48:33 +02:00
#ifndef RESULTSOURCEMAPPING_H
#define RESULTSOURCEMAPPING_H
#include <libpq-fe.h>
#include <vector>
class PgDatabaseCatalog;
namespace Pgsql {
class Result;
}
/// The table column a result column is read from, as reported by libpq (PQftable/PQftablecol).
/// tableOid is InvalidOid and attnum is 0 for expression columns.
struct ResultColumnSource {
Oid tableOid = InvalidOid;
int attnum = 0;
};
/// The primary key of a table, attnums in key order.
struct TablePrimaryKey {
Oid tableOid = InvalidOid;
std::vector<int> attnums;
};
/// Maps the columns of a query result to the table columns they come from and decides which
/// cells can be edited. A cell is editable when it maps to a user column of a table whose full
/// primary key is present in the result and which is not ambiguous.
class ResultSourceMapping {
public:
ResultSourceMapping(std::vector<ResultColumnSource> columns, const std::vector<TablePrimaryKey> &primaryKeys);
int columnCount() const;
/// Table the column is read from, InvalidOid for expression columns.
Oid tableOid(int col) const;
/// attnum of the column in its table, 0 for expression columns.
int attnum(int col) const;
bool isEditable(int col) const;
/// Result column indices holding the primary key of the table, in key order.
/// Empty when the table is not editable.
std::vector<int> primaryKeyColumns(Oid tableOid) const;
private:
struct TableInfo {
Oid tableOid = InvalidOid;
bool editable = false;
std::vector<int> pkColumns;
};
std::vector<ResultColumnSource> m_columns;
std::vector<TableInfo> m_tables;
const TableInfo *findTable(Oid tableOid) const;
};
/// Builds the mapping for a query result, looking up the primary keys of the source tables in the catalog.
ResultSourceMapping createResultSourceMapping(const Pgsql::Result &result, const PgDatabaseCatalog &catalog);
#endif // RESULTSOURCEMAPPING_H