70 lines
2.4 KiB
C
70 lines
2.4 KiB
C
|
|
#ifndef SQLCOMPLETIONPROVIDER_H
|
||
|
|
#define SQLCOMPLETIONPROVIDER_H
|
||
|
|
|
||
|
|
#include "SqlContext.h"
|
||
|
|
#include <QString>
|
||
|
|
#include <memory>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
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<QString> 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<QString> childColumns;
|
||
|
|
std::vector<QString> parentColumns; // same length as childColumns
|
||
|
|
};
|
||
|
|
|
||
|
|
struct CompletionCatalog {
|
||
|
|
std::vector<QString> schemas;
|
||
|
|
std::vector<CompletionRelation> relations;
|
||
|
|
std::vector<CompletionFunction> functions;
|
||
|
|
std::vector<CompletionForeignKey> foreignKeys;
|
||
|
|
/// Schemas that are searched when a relation is not schema qualified, in order
|
||
|
|
std::vector<QString> searchPath = { QStringLiteral("public"), QStringLiteral("pg_catalog") };
|
||
|
|
};
|
||
|
|
|
||
|
|
CompletionCatalog buildCompletionCatalog(const PgDatabaseCatalog &catalog);
|
||
|
|
|
||
|
|
class SqlCompletionProvider {
|
||
|
|
public:
|
||
|
|
SqlCompletionProvider() = default;
|
||
|
|
explicit SqlCompletionProvider(CompletionCatalog catalog);
|
||
|
|
|
||
|
|
std::vector<CompletionItem> complete(const SqlContext &ctx) const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
CompletionCatalog m_catalog;
|
||
|
|
|
||
|
|
const CompletionRelation* resolve(const RelationRef &ref) const;
|
||
|
|
std::vector<CompletionItem> relationItems(const SqlContext &ctx, const QString &schema) const;
|
||
|
|
std::vector<CompletionItem> columnItems(const SqlContext &ctx) const;
|
||
|
|
std::vector<CompletionItem> qualifiedColumnItems(const SqlContext &ctx) const;
|
||
|
|
std::vector<CompletionItem> joinItems(const SqlContext &ctx) const;
|
||
|
|
void addFunctionItems(const SqlContext &ctx, std::vector<CompletionItem> &items) const;
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif // SQLCOMPLETIONPROVIDER_H
|