aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/clangpchmanagerbackend/source/builddependenciesstorage.h
blob: e7269dd0f9459c3196a226d67ba2bfc532d7789e (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "builddependenciesstorageinterface.h"

#include <compilermacro.h>
#include <sqliteexception.h>
#include <sqlitetable.h>
#include <sqlitetransaction.h>

#include <utils/smallstringview.h>

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

namespace ClangBackEnd {

template<typename Database=Sqlite::Database>
class BuildDependenciesStorage final : public BuildDependenciesStorageInterface
{
    using ReadStatement = typename Database::ReadStatement;
    using WriteStatement = typename Database::WriteStatement;
public:
    BuildDependenciesStorage(Database &database)
        : transaction(database),
          database(database)
    {
        transaction.commit();
    }

    void insertOrUpdateSources(const SourceEntries &sourceEntries, ProjectPartId projectPartId) override
    {
        deleteAllProjectPartsFilesWithProjectPartNameStatement.write(projectPartId.projectPathId);

        for (const SourceEntry &entry : sourceEntries) {
            insertOrUpdateProjectPartsFilesStatement.write(entry.sourceId.filePathId,
                                                             projectPartId.projectPathId,
                                                             static_cast<uchar>(entry.sourceType),
                                                             static_cast<uchar>(
                                                                 entry.hasMissingIncludes));
        }
    }

    void insertOrUpdateFileStatuses(const FileStatuses &fileStatuses) override
    {
        WriteStatement &statement = insertOrUpdateFileStatusesStatement;

        for (const FileStatus &fileStatus : fileStatuses)
            statement.write(fileStatus.filePathId.filePathId,
                            fileStatus.size,
                            fileStatus.lastModified);
    }

    long long fetchLowestLastModifiedTime(FilePathId sourceId) const override
    {
        ReadStatement &statement = getLowestLastModifiedTimeOfDependencies;

        return statement.template value<long long>(sourceId.filePathId).value_or(0);
    }

    void insertOrUpdateUsedMacros(const UsedMacros &usedMacros) override
    {
        WriteStatement &insertStatement = insertIntoNewUsedMacrosStatement;
        for (const UsedMacro &usedMacro : usedMacros)
            insertStatement.write(usedMacro.filePathId.filePathId,
                                  usedMacro.macroName);

        syncNewUsedMacrosStatement.execute();
        deleteOutdatedUsedMacrosStatement.execute();
        deleteNewUsedMacrosTableStatement.execute();
    }

    void insertOrUpdateSourceDependencies(const SourceDependencies &sourceDependencies) override
    {
        WriteStatement &insertStatement = insertIntoNewSourceDependenciesStatement;
        for (SourceDependency sourceDependency : sourceDependencies)
            insertStatement.write(sourceDependency.filePathId.filePathId,
                                  sourceDependency.dependencyFilePathId.filePathId);

        syncNewSourceDependenciesStatement.execute();
        deleteOutdatedSourceDependenciesStatement.execute();
        deleteNewSourceDependenciesStatement.execute();
    }

    ProjectPartId fetchProjectPartId(Utils::SmallStringView projectPartName) override
    {
        auto projectPartId = fetchProjectPartIdStatement.template value<ProjectPartId>(projectPartName);

        if (projectPartId)
            return *projectPartId;

        insertProjectPartNameStatement.write(projectPartName);

        return static_cast<int>(database.lastInsertedRowId());
    }

    SourceEntries fetchDependSources(FilePathId sourceId, ProjectPartId projectPartId) const override
    {
        return fetchSourceDependenciesStatement
            .template values<SourceEntry, 4>(300, sourceId.filePathId, projectPartId.projectPathId);
    }

    UsedMacros fetchUsedMacros(FilePathId sourceId) const override
    {
        return fetchUsedMacrosStatement.template values<UsedMacro, 2>(128, sourceId.filePathId);
    }

    void updatePchCreationTimeStamp(long long pchCreationTimeStamp, ProjectPartId projectPartId) override
    {
        Sqlite::ImmediateTransaction transaction{database};

        updatePchCreationTimeStampStatement.write(pchCreationTimeStamp, projectPartId.projectPathId);

        transaction.commit();
    }

    static Utils::SmallString toJson(const Utils::SmallStringVector &strings)
    {
        QJsonDocument document;
        QJsonArray array;

        std::transform(strings.begin(), strings.end(), std::back_inserter(array), [] (const auto &string) {
            return QJsonValue(string.data());
        });

        document.setArray(array);

        return document.toJson(QJsonDocument::Compact);
    }

    static Utils::SmallString toJson(const CompilerMacros &compilerMacros)
    {
        QJsonDocument document;
        QJsonObject object;

        for (const CompilerMacro &macro : compilerMacros)
            object.insert(QString(macro.key), QString(macro.value));

        document.setObject(object);

        return document.toJson(QJsonDocument::Compact);
    }

    Sqlite::Table createNewUsedMacrosTable() const
    {
        Sqlite::Table table;
        table.setName("newUsedMacros");
        table.setUseTemporaryTable(true);
        const Sqlite::Column &sourceIdColumn = table.addColumn("sourceId", Sqlite::ColumnType::Integer);
        const Sqlite::Column &macroNameColumn = table.addColumn("macroName", Sqlite::ColumnType::Text);
        table.addIndex({sourceIdColumn, macroNameColumn});

        table.initialize(database);

        return table;
    }

    Sqlite::Table createNewSourceDependenciesTable() const
    {
        Sqlite::Table table;
        table.setName("newSourceDependencies");
        table.setUseTemporaryTable(true);
        const Sqlite::Column &sourceIdColumn = table.addColumn("sourceId", Sqlite::ColumnType::Integer);
        const Sqlite::Column &dependencySourceIdColumn = table.addColumn("dependencySourceId", Sqlite::ColumnType::Text);
        table.addIndex({sourceIdColumn, dependencySourceIdColumn});

        table.initialize(database);

        return table;
    }

public:
    Sqlite::ImmediateNonThrowingDestructorTransaction transaction;
    Database &database;
    Sqlite::Table newUsedMacroTable{createNewUsedMacrosTable()};
    Sqlite::Table newNewSourceDependenciesTable{createNewSourceDependenciesTable()};
    WriteStatement insertIntoNewUsedMacrosStatement{
        "INSERT INTO newUsedMacros(sourceId, macroName) VALUES (?,?)",
        database
    };
    WriteStatement syncNewUsedMacrosStatement{
        "INSERT INTO usedMacros(sourceId, macroName) SELECT sourceId, macroName FROM newUsedMacros WHERE NOT EXISTS (SELECT sourceId FROM usedMacros WHERE usedMacros.sourceId == newUsedMacros.sourceId AND usedMacros.macroName == newUsedMacros.macroName)",
        database
    };
    WriteStatement deleteOutdatedUsedMacrosStatement{
        "DELETE FROM usedMacros WHERE sourceId IN (SELECT sourceId FROM newUsedMacros) AND NOT EXISTS (SELECT sourceId FROM newUsedMacros WHERE newUsedMacros.sourceId == usedMacros.sourceId AND newUsedMacros.macroName == usedMacros.macroName)",
        database
    };
    WriteStatement deleteNewUsedMacrosTableStatement{
        "DELETE FROM newUsedMacros",
        database
    };
    mutable ReadStatement getLowestLastModifiedTimeOfDependencies{
        "WITH RECURSIVE sourceIds(sourceId) AS (VALUES(?) UNION SELECT dependencySourceId FROM sourceDependencies, sourceIds WHERE sourceDependencies.sourceId = sourceIds.sourceId) SELECT min(lastModified) FROM fileStatuses, sourceIds WHERE fileStatuses.sourceId = sourceIds.sourceId",
        database
    };
    WriteStatement insertIntoNewSourceDependenciesStatement{
        "INSERT INTO newSourceDependencies(sourceId, dependencySourceId) VALUES (?,?)",
        database
    };
    WriteStatement insertOrUpdateFileStatusesStatement{
        "INSERT INTO fileStatuses(sourceId, size, lastModified) VALUES "
        "(?001,?002,?003) ON "
        "CONFLICT(sourceId) DO UPDATE SET size = ?002, lastModified = ?003",
        database};
    WriteStatement syncNewSourceDependenciesStatement{
        "INSERT INTO sourceDependencies(sourceId, dependencySourceId) SELECT sourceId, dependencySourceId FROM newSourceDependencies WHERE NOT EXISTS (SELECT sourceId FROM sourceDependencies WHERE sourceDependencies.sourceId == newSourceDependencies.sourceId AND sourceDependencies.dependencySourceId == newSourceDependencies.dependencySourceId)",
        database
    };
    WriteStatement deleteOutdatedSourceDependenciesStatement{
        "DELETE FROM sourceDependencies WHERE sourceId IN (SELECT sourceId FROM newSourceDependencies) AND NOT EXISTS (SELECT sourceId FROM newSourceDependencies WHERE newSourceDependencies.sourceId == sourceDependencies.sourceId AND newSourceDependencies.dependencySourceId == sourceDependencies.dependencySourceId)",
        database
    };
    WriteStatement deleteNewSourceDependenciesStatement{
        "DELETE FROM newSourceDependencies",
        database
    };
    WriteStatement insertOrUpdateProjectPartsFilesStatement{
        "INSERT INTO projectPartsFiles(sourceId, projectPartId, "
        "sourceType, hasMissingIncludes) VALUES (?001, ?002, ?003, ?004) ON "
        "CONFLICT(sourceId, projectPartId) DO UPDATE SET sourceType = ?003, "
        "hasMissingIncludes = ?004",
        database};
    mutable ReadStatement fetchSourceDependenciesStatement{
        "WITH RECURSIVE collectedDependencies(sourceId) AS (VALUES(?) UNION "
        "SELECT dependencySourceId FROM sourceDependencies, "
        "collectedDependencies WHERE sourceDependencies.sourceId == "
        "collectedDependencies.sourceId) SELECT sourceId, "
        "pchCreationTimeStamp, sourceType, hasMissingIncludes FROM "
        "collectedDependencies NATURAL JOIN projectPartsFiles WHERE "
        "projectPartId = ? ORDER BY sourceId",
        database};
    mutable ReadStatement fetchProjectPartIdStatement{
        "SELECT projectPartId FROM projectParts WHERE projectPartName = ?",
        database
    };
    WriteStatement insertProjectPartNameStatement{
        "INSERT INTO projectParts(projectPartName) VALUES (?)", database};
    mutable ReadStatement fetchUsedMacrosStatement{
        "SELECT macroName, sourceId FROM usedMacros WHERE sourceId = ? ORDER BY sourceId, macroName",
        database
    };
    WriteStatement updatePchCreationTimeStampStatement{
        "UPDATE projectPartsFiles SET pchCreationTimeStamp = ?001 WHERE projectPartId = ?002",
        database};
    WriteStatement deleteAllProjectPartsFilesWithProjectPartNameStatement{
        "DELETE FROM projectPartsFiles WHERE projectPartId = ?", database};
};
}