#ifndef RESULTSOURCEMAPPING_H #define RESULTSOURCEMAPPING_H #include #include 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 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 columns, const std::vector &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 primaryKeyColumns(Oid tableOid) const; private: struct TableInfo { Oid tableOid = InvalidOid; bool editable = false; std::vector pkColumns; }; std::vector m_columns; std::vector 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