1 #ifndef UTILS_SQL_H 2 #define UTILS_SQL_H 3 4 #include "parser/token.h" 5 #include "coreSQLiteStudio_global.h" 6 #include <QString> 7 #include <QChar> 8 #include <QPair> 9 10 // TODO: unit tests for most of methods from this module 11 12 enum class NameWrapper 13 { 14 DOUBLE_QUOTE, 15 QUOTE, 16 BACK_QUOTE, 17 BRACKET, 18 null 19 }; 20 21 enum class QueryAccessMode 22 { 23 READ, 24 WRITE 25 }; 26 27 enum class SqliteDataType 28 { 29 UNKNOWN = -1, 30 _NULL = 0, 31 INTEGER = 1, 32 REAL = 2, 33 TEXT = 3, 34 BLOB = 4 35 }; 36 37 typedef QPair<QString,QStringList> QueryWithParamNames; 38 typedef QPair<QString,int> QueryWithParamCount; 39 40 API_EXPORT void initUtilsSql(); 41 API_EXPORT SqliteDataType toSqliteDataType(const QString& typeStr); 42 API_EXPORT bool doesObjectNeedWrapping(const QString& str); 43 API_EXPORT bool doesObjectNeedWrapping(const QChar& c); 44 API_EXPORT bool isObjectWrapped(const QChar& c); 45 API_EXPORT bool doesStringNeedWrapping(const QString& str); 46 API_EXPORT bool isStringWrapped(const QString& str); 47 API_EXPORT QString wrapObjIfNeeded(const QString& obj, NameWrapper favWrapper = NameWrapper::null); 48 API_EXPORT QString wrapObjIfNeeded(const QString& obj, bool useDoubleQuoteForEmptyValue, NameWrapper favWrapper = NameWrapper::null); 49 API_EXPORT QString wrapObjName(const QString& obj, NameWrapper favWrapper = NameWrapper::null); 50 API_EXPORT QString wrapObjName(const QString& obj, bool useDoubleQuoteForEmptyValue, NameWrapper favWrapper = NameWrapper::null); 51 API_EXPORT TokenPtr stripObjName(TokenPtr token); 52 API_EXPORT QString stripObjName(const QString &str); 53 API_EXPORT QString stripObjName(QString& str); 54 API_EXPORT bool isObjWrapped(const QString& str); 55 API_EXPORT NameWrapper getObjWrapper(const QString& str); 56 API_EXPORT bool isWrapperChar(const QChar& c); 57 API_EXPORT QString wrapString(const QString& str); 58 API_EXPORT QStringList wrapStrings(const QStringList& strList); 59 API_EXPORT QString wrapStringIfNeeded(const QString& str); 60 API_EXPORT QString escapeString(QString &str); 61 API_EXPORT QString escapeString(const QString& str); 62 API_EXPORT QString stripString(QString& str); 63 API_EXPORT QString stripString(const QString& str); 64 API_EXPORT QString stripEndingSemicolon(const QString& str); 65 API_EXPORT QPair<QChar,QChar> getQuoteCharacter(QString& obj, NameWrapper favWrapper = NameWrapper::null); 66 API_EXPORT QList<QString> wrapObjNames(const QList<QString>& objList, NameWrapper favWrapper = NameWrapper::null); 67 API_EXPORT QList<QString> wrapObjNamesIfNeeded(const QList<QString>& objList, NameWrapper favWrapper = NameWrapper::null); 68 API_EXPORT QList<NameWrapper> getAllNameWrappers(); 69 API_EXPORT QString wrapValueIfNeeded(const QString& str); 70 API_EXPORT QString wrapValueIfNeeded(const QVariant& value); 71 API_EXPORT int qHash(NameWrapper wrapper); 72 API_EXPORT QString getPrefixDb(const QString& origDbName); 73 API_EXPORT bool isSystemTable(const QString& name); 74 API_EXPORT bool isSystemIndex(const QString& name); 75 API_EXPORT QString removeComments(const QString& value); 76 API_EXPORT QList<TokenList> splitQueries(const TokenList& tokenizedQueries, bool* complete = nullptr); 77 API_EXPORT QStringList splitQueries(const QString& sql, bool keepEmptyQueries = true, bool removeComments = false, bool* complete = nullptr); 78 API_EXPORT QStringList quickSplitQueries(const QString& sql, bool keepEmptyQueries = true, bool removeComments = false); 79 API_EXPORT QString getQueryWithPosition(const QStringList& queries, int position, int* startPos = nullptr); 80 API_EXPORT QString getQueryWithPosition(const QString& queries, int position, int* startPos = nullptr); 81 API_EXPORT QList<QueryWithParamNames> getQueriesWithParamNames(const QString& query); 82 API_EXPORT QList<QueryWithParamCount> getQueriesWithParamCount(const QString& query); 83 API_EXPORT QueryWithParamNames getQueryWithParamNames(const QString& query); 84 API_EXPORT QueryWithParamCount getQueryWithParamCount(const QString& query); 85 API_EXPORT QString trimBindParamPrefix(const QString& param); 86 API_EXPORT QString commentAllSqlLines(const QString& sql); 87 API_EXPORT QString getBindTokenName(const TokenPtr& token); 88 API_EXPORT QueryAccessMode getQueryAccessMode(const QString& query, bool* isSelect = nullptr); 89 API_EXPORT QStringList valueListToSqlList(const QList<QVariant>& values); 90 API_EXPORT QString trimQueryEnd(const QString& query); 91 92 93 #endif // UTILS_SQL_H 94