File: queryexecutorreplaceviews.h

package info (click to toggle)
sqlitestudio 3.4.21%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 54,880 kB
  • sloc: ansic: 406,208; cpp: 123,872; yacc: 2,692; tcl: 497; sh: 462; xml: 426; makefile: 19
file content (120 lines) | stat: -rw-r--r-- 3,750 bytes parent folder | download
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
#ifndef QUERYEXECUTORREPLACEVIEWS_H
#define QUERYEXECUTORREPLACEVIEWS_H

#include "queryexecutorstep.h"
#include "parser/ast/sqlitecreateview.h"

class SchemaResolver;

/**
 * @brief Replaces all references to views in query with SELECTs from those views.
 *
 * Replacing views with their SELECTs (as subselects) simplifies later tasks
 * with the query.
 */
class QueryExecutorReplaceViews : public QueryExecutorStep
{
        Q_OBJECT

    public:
        ~QueryExecutorReplaceViews();

        bool exec();

    protected:
        void init();

    private:
        /**
         * @brief View representation in context of QueryExecutorReplaceViews step.
         */
        struct View
        {
            /**
             * @brief Creates view.
             * @param database Database of the view.
             * @param view View name.
             */
            View(const QString& database, const QString& view);

            /**
             * @brief Database of the view.
             */
            QString database;

            /**
             * @brief View name.
             */
            QString view;

            /**
             * @brief Checks if it's the same view as the \p other.
             * @param other Other view to compare.
             * @return 1 if other view is the same one, or 0 otherwise.
             *
             * Views are equal if they have equal name and database.
             */
            int operator==(const View& other) const;
        };

        friend uint qHash(const View& view);

        /**
         * @brief Provides all views existing in the database.
         * @param database Database name as typed in the query.
         * @return List of view names.
         *
         * Uses internal cache (using views).
         */
        QStringList getViews(const QString& database);

        /**
         * @brief Reads view's DDL, parses it and returns results.
         * @param database Database of the view.
         * @param viewName View name.
         * @return Parsed view or null pointer if view doesn't exist or could not be parsed.
         *
         * It uses internal cache (using viewStatements).
         */
        SqliteCreateViewPtr getView(const QString& database, const QString& viewName);

        /**
         * @brief Replaces views in the query with SELECT statements.
         * @param select SELECT statement to replace views in.
         *
         * It explores the \p select looking for view names and replaces them with
         * apropriate subselect queries, using getView() calls.
         */
        void replaceViews(SqliteSelect* select);

        /**
         * @brief Tells whether particular SELECT statement has any View as a data source.
         * @param select Parsed SELECT statement.
         * @param viewsInDatabase Prepared list of views existing in the database.
         * @return true if the SELECT uses at least one existing View.
         */
        bool usesAnyView(SqliteSelect* select, const QStringList& viewsInDatabase);

        /**
         * @brief Used for caching view list per database.
         */
        QHash<QString,QStringList> views;

        /**
         * @brief Resolver used several time in this step.
         *
         * It's stored as member of this class, cause otherwise it would be created
         * and deleted many times. Instead it's shared across all calls to resolve something
         * from schema.
         */
        SchemaResolver* schemaResolver = nullptr;

        /**
         * @brief Used for caching parsed view statement.
         */
        QHash<View,SqliteCreateViewPtr> viewStatements;
};

uint qHash(const QueryExecutorReplaceViews::View& view);

#endif // QUERYEXECUTORREPLACEVIEWS_H