1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#ifndef FORMATSTATEMENT_H
#define FORMATSTATEMENT_H
#include "parser/ast/sqlitestatement.h"
#include "parser/ast/sqlitesortorder.h"
#include "parser/ast/sqliteconflictalgo.h"
#include "common/utils_sql.h"
#include "sqlenterpriseformatter.h"
#include <QString>
#include <QStringList>
#include <QHash>
#include <QStack>
#include <QVariant>
#include <functional>
class FormatStatement
{
public:
struct FormatToken
{
enum Type
{
KEYWORD,
LINED_UP_KEYWORD,
ID,
ID_NO_WRAP,
STRING_OR_ID,
OPERATOR,
STAR,
FLOAT,
STRING,
INTEGER,
BLOB,
BIND_PARAM,
ID_DOT,
PAR_DEF_LEFT,
PAR_DEF_RIGHT,
PAR_EXPR_LEFT,
PAR_EXPR_RIGHT,
PAR_FUNC_LEFT,
PAR_FUNC_RIGHT,
SEMICOLON,
COMMA_LIST,
COMMA_OPER, // for example in LIMIT
FUNC_ID,
DATA_TYPE,
NEW_LINE,
INDENT_MARKER,
INCR_INDENT,
SET_INDENT,
DECR_INDENT,
MARK_KEYWORD_LINEUP
};
enum Flag
{
NO_FLAG = 0x00,
NO_SPACE_BEFORE = 0x01,
NO_SPACE_AFTER = 0x02,
NO_NEWLINE_BEFORE = 0x04,
NO_NEWLINE_AFTER = 0x08
};
Q_DECLARE_FLAGS(Flags, Flag)
Type type;
QVariant value;
QVariant additionalValue;
Flags flags;
};
enum class ListSeparator
{
NONE,
COMMA,
EXPR_COMMA,
NEW_LINE,
SEMICOLON
};
typedef std::function<void(FormatStatement*)> FormatStatementEnricher;
FormatStatement();
virtual ~FormatStatement();
QString format();
void setSelectedWrapper(NameWrapper wrapper);
void setConfig(Cfg::SqlEnterpriseFormatterConfig* cfg);
static FormatStatement* forQuery(SqliteStatement *query);
FormatStatement& withKeyword(const QString& kw);
FormatStatement& withLinedUpKeyword(const QString& kw, const QString& lineUpName = QString());
FormatStatement& withId(const QString& id);
FormatStatement& withId(const QString& id, bool wrapIfNeeded);
FormatStatement& withIdList(const QStringList& names, const QString& indentName = QString(), ListSeparator sep = ListSeparator::COMMA);
FormatStatement& withOperator(const QString& oper, FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withStringOrId(const QString& id);
FormatStatement& withIdDot(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withStar(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withFloat(double value);
FormatStatement& withInteger(qint64 value);
FormatStatement& withString(const QString& value);
FormatStatement& withBlob(const QString& value);
FormatStatement& withBindParam(const QString& name);
FormatStatement& withParDefLeft(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withParDefRight(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withParExprLeft(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withParExprRight(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withParFuncLeft(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withParFuncRight(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withSemicolon(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withListComma(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withCommaOper(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatStatement& withSortOrder(SqliteSortOrder sortOrder);
FormatStatement& withConflict(SqliteConflictAlgo onConflict);
FormatStatement& withFuncId(const QString& func);
FormatStatement& withDataType(const QString& dataType);
FormatStatement& withNewLine();
FormatStatement& withLiteral(const QVariant& value);
FormatStatement& withStatement(SqliteStatement* stmt, const QString& indentName = QString(), FormatStatementEnricher enricher = nullptr);
FormatStatement& markIndent(const QString& name);
FormatStatement& markAndKeepIndent(const QString& name);
FormatStatement& withIncrIndent(int newIndent);
FormatStatement& withIncrIndent(const QString& name = QString());
FormatStatement& withDecrIndent();
FormatStatement& markKeywordLineUp(const QString& keyword, const QString& lineUpName = QString());
FormatStatement& withSeparator(ListSeparator sep, FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
template <class T>
FormatStatement& withStatementList(QList<T*> stmtList, FormatToken::Flags flags)
{
return withStatementList(stmtList, QString(), ListSeparator::COMMA, nullptr, flags);
}
template <class T>
FormatStatement& withStatementList(QList<T*> stmtList, const QString& indentName = QString(), ListSeparator sep = ListSeparator::COMMA,
FormatStatementEnricher enricher = nullptr, FormatToken::Flags flags = FormatToken::Flag::NO_FLAG)
{
if (!indentName.isNull())
markAndKeepIndent(indentName);
bool first = true;
for (T* stmt : stmtList)
{
if (!first)
withSeparator(sep, flags);
withStatement(stmt, QString(), enricher);
first = false;
}
if (!indentName.isNull())
withDecrIndent();
return *this;
}
template <class T>
T* getFormatStatement(SqliteStatement* stmt)
{
return dynamic_cast<T*>(forQuery(stmt, wrapper, cfg));
}
protected:
void handleExplainQuery(SqliteQuery* query);
virtual void formatInternal() = 0;
virtual void resetInternal();
NameWrapper wrapper = NameWrapper::BRACKET;
Cfg::SqlEnterpriseFormatterConfig* cfg = nullptr;
private:
FormatToken* withToken(FormatToken::Type type, const QVariant& value, const QVariant& additionalValue = QVariant(), FormatToken::Flags flags = FormatToken::Flag::NO_FLAG);
FormatToken* withToken(FormatToken::Type type, const QVariant& value, FormatToken::Flags flags);
void cleanup();
void buildTokens();
bool applyIndent();
void applySpace(FormatToken::Type type);
bool isSpaceExpectingType(FormatToken::Type type);
bool isMetaType(FormatToken::Type type);
void newLine();
void incrIndent(const QString& name = QString());
void decrIndent();
void setIndent(int newIndent);
bool endsWithSpace();
FormatToken* getLastRealToken(bool skipNewLines = false);
FormatToken* getLastToken();
QString detokenize();
void detokenizeLeftPar(FormatToken* token, bool spaceBefore, bool spaceAfter, bool nlBefore, bool nlAfter);
void detokenizeRightPar(FormatToken* token, bool spaceBefore, bool spaceAfter, bool nlBefore, bool nlAfter);
void resetIndents();
void removeAllSpaces();
void removeAllSpacesFromLine();
void updateLastToken(FormatToken* token);
QString getFinalLineUpName(const QString& lineUpName);
int predictCurrentIndent(FormatToken* currentMetaToken);
bool willStartWithNewLine(FormatToken* token);
void formatId(const QString& value, bool wrappingRequested);
int getLineUpValue(const QString& lineUpName);
static FormatStatement* forQuery(SqliteStatement *query, NameWrapper wrapper, Cfg::SqlEnterpriseFormatterConfig* cfg);
QHash<QString,int> kwLineUpPosition;
QHash<QString,int> namedIndents;
QStack<int> indents;
QList<FormatToken*> tokens;
bool deleteTokens = true;
QStringList lines;
QString line;
FormatToken* lastToken = nullptr;
QString statementName;
FormatStatement* parentFormatStatement = nullptr;
static qint64 nameSeq;
static const QString SPACE;
static const QString NEWLINE;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(FormatStatement::FormatToken::Flags)
#endif // FORMATSTATEMENT_H
|