#ifndef SQLCONTEXT_H #define SQLCONTEXT_H #include #include /** * Kind of names that are expected at the cursor position. */ enum class CompletionKind { None, Relation, // schema, table or view name Column, // column name of the relations in scope QualifiedColumn, // column after "alias." or "table." Schema, Function, JoinTarget // complete "table alias ON ..." proposal after JOIN }; /** * Part of the statement the cursor is in. */ enum class SqlClause { None, Select, From, Join, On, Where, GroupBy, OrderBy, Having, Set, Update, InsertInto, Returning, Values }; /** * An item of a FROM clause (or UPDATE/INSERT target, CTE, derived table). * Names are normalised: unquoted identifiers are folded to lower case. */ struct RelationRef { QString schema; QString name; // may be a CTE name or the alias of a derived table QString alias; bool quoted = false; // name was written as quoted identifier bool derived = false; // CTE or sub query, columns are not in the catalog }; struct SqlContext { CompletionKind kind = CompletionKind::None; SqlClause clause = SqlClause::None; QString prefix; // identifier text typed so far (without opening quote) int prefixStart = 0; // replace range in document int prefixLen = 0; QString qualifier; // text before '.', if any QString qualifierSchema; // for schema.table. std::vector relations; // relations in scope, innermost query first bool prefixWasQuoted = false; }; /** * Tolerant analysis of possibly incomplete SQL around the cursor. * @param cursorPos position in text (between characters) where completion is requested. */ SqlContext analyzeSqlContext(const QString &text, int cursorPos); #endif // SQLCONTEXT_H