aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/impl/dbattacherimpl.h
blob: 6fbe6c8d9fe868c725ecbff7bd6e70627b672ec9 (plain) (blame)
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
#ifndef DBATTACHERIMPL_H
#define DBATTACHERIMPL_H

#include "dbattacher.h"

class DbAttacherImpl : public DbAttacher
{
    public:
        /**
         * @brief Creates attacher with the given database as the main.
         * @param db Database that the query will be executed on.
         */
        DbAttacherImpl(Db* db);

        bool attachDatabases(const QString& query);
        bool attachDatabases(const QList<SqliteQueryPtr>& queries);
        bool attachDatabases(SqliteQueryPtr query);
        void detachDatabases();
        BiStrHash getDbNameToAttach() const;
        QString getQuery() const;
        bool getMainDbNameUsed() const;

    private:
        /**
         * @brief Does the actual job, after having all input queries as parsed objects.
         * @return true on success, false on failure.
         */
        bool attachDatabases();

        /**
         * @brief Finds tokens representing databases in the query.
         * @return List of tokens. Some tokens have non-printable value (spaces, etc), others are database names.
         */
        TokenList getDbTokens();

        /**
         * @brief Detaches all databases currently attached by the attacher.
         *
         * Also clears names mappings.
         */
        void detachAttached();

        /**
         * @brief Generates mapping of database name to its Db object for all registered databases.
         */
        void prepareNameToDbMap();

        /**
         * @brief Groups tokens by the name of database they refer to.
         * @param dbTokens Tokens representing databases in the query.
         *
         * This method is used to learn if some database is used more than once in the query,
         * so we attach it only once, then replace all tokens referring to it by the attach name.
         */
        QHash<QString,TokenList> groupDbTokens(const TokenList& dbTokens);

        /**
         * @brief Tries to attach all required databases.
         * @param groupedDbTokens Database tokens grouped by database name, as returned from groupDbTokens().
         * @return true on success, false on any problem.
         *
         * Major problem that can happen is when "<tt>ATTACH 'path to file'</tt>" fails for any reason. In that case
         * detachAttached() is called and false is returned.
         */
        bool attachAllDbs(const QHash<QString,TokenList>& groupedDbTokens);

        /**
         * @brief Creates token-to-token replace map to update the query.
         * @param dbTokens Tokens representing databases in the query.
         * @return Mapping to be used when replacing tokens in the query.
         */
        QHash<TokenPtr, TokenPtr> getTokenMapping(const TokenList& dbTokens);

        /**
         * @brief Replaces tokens in the query.
         * @param tokenMapping Map of tokens to replace.
         *
         * Replacing takes place in token lists of each query in the queries member.
         */
        void replaceTokensInQueries(const QHash<TokenPtr, TokenPtr>& tokenMapping);

        QList<SqliteQueryPtr> queries;
        Db* db = nullptr;
        Dialect dialect;
        BiStrHash dbNameToAttach;
        StrHash<Db*> nameToDbMap;
        bool mainDbNameUsed = false;
};

class DbAttacherDefaultFactory : public DbAttacherFactory
{
    public:
        DbAttacher* create(Db* db);
};

#endif // DBATTACHERIMPL_H