#ifndef SQLCOMPLETIONPROVIDER_H #define SQLCOMPLETIONPROVIDER_H #include "SqlContext.h" #include #include #include class PgDatabaseCatalog; struct CompletionItem { QString display; // text shown in the list QString detail; // dimmed extra information, for example the relation a column belongs to QString insertText; // replaces the typed prefix when accepted CompletionKind kind = CompletionKind::None; bool ambiguous = false; // name occurs in more than one relation in scope }; /// Plain snapshot of the parts of the catalog needed for completion, so the provider does not need a database. struct CompletionRelation { QString schema; QString name; char kind = 'r'; // pg_class.relkind std::vector columns; // in column order, without dropped and system columns }; struct CompletionFunction { QString schema; QString name; char kind = 'f'; // pg_proc.prokind: f function, a aggregate, w window }; struct CompletionForeignKey { QString childSchema, childTable; // table that has the constraint QString parentSchema, parentTable; // referenced table std::vector childColumns; std::vector parentColumns; // same length as childColumns }; struct CompletionCatalog { std::vector schemas; std::vector relations; std::vector functions; std::vector foreignKeys; /// Schemas that are searched when a relation is not schema qualified, in order std::vector searchPath = { QStringLiteral("public"), QStringLiteral("pg_catalog") }; }; CompletionCatalog buildCompletionCatalog(const PgDatabaseCatalog &catalog); class SqlCompletionProvider { public: SqlCompletionProvider() = default; explicit SqlCompletionProvider(CompletionCatalog catalog); std::vector complete(const SqlContext &ctx) const; private: CompletionCatalog m_catalog; const CompletionRelation* resolve(const RelationRef &ref) const; std::vector relationItems(const SqlContext &ctx, const QString &schema) const; std::vector columnItems(const SqlContext &ctx) const; std::vector qualifiedColumnItems(const SqlContext &ctx) const; std::vector joinItems(const SqlContext &ctx) const; void addFunctionItems(const SqlContext &ctx, std::vector &items) const; }; #endif // SQLCOMPLETIONPROVIDER_H