aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/sqlitetester/main.cpp
blob: 1f514baed605d44dab70b26c14d9f32d65ef7fa9 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <sqlitedatabase.h>
#include <sqlitelibraryinitializer.h>

#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QTextBrowser>
#include <QTemporaryFile>
#include <QDir>

#include <windows.h>

class DatabaseApp : public QWidget {
    Q_OBJECT

public:
    DatabaseApp(QWidget *parent = nullptr);

private slots:
    void onSetDirectoryClicked();
    void createDatabase(const QString &dirPath);

private:
    void deleteFileIfExist(const QString &filePath);
    void logMessage(const QString &message);
    void logDetailedWindowsError();

    QLineEdit *directoryLineEdit;
    QTextBrowser *logBrowser;
    QString databaseDirectory;
};

DatabaseApp::DatabaseApp(QWidget *parent) : QWidget(parent) {
    QVBoxLayout *layout = new QVBoxLayout(this);

    directoryLineEdit = new QLineEdit(this);
    directoryLineEdit->setPlaceholderText("Enter database directory path");
    layout->addWidget(directoryLineEdit);

    QPushButton *setDirectoryButton = new QPushButton("Set Directory and Create Database", this);
    layout->addWidget(setDirectoryButton);

    logBrowser = new QTextBrowser(this);
    layout->addWidget(logBrowser);

    connect(setDirectoryButton, &QPushButton::clicked, this, &DatabaseApp::onSetDirectoryClicked);
}

void DatabaseApp::onSetDirectoryClicked() {
    QString dirPath = directoryLineEdit->text();
    if (dirPath.isEmpty()) {
        logMessage("Directory path is empty.");
        return;
    }

    QDir dir(dirPath);
    if (!dir.exists()) {
        logMessage("Directory does not exist.");
        return;
    }

    QTemporaryFile tempFile(dirPath + "/tempfileXXXXXX");
    if (!tempFile.open()) {
        logMessage("Cannot create temporary file in the directory: " + tempFile.errorString());
        logDetailedWindowsError();
        return;
    }
    tempFile.close();

    createDatabase(dirPath);
}

void DatabaseApp::deleteFileIfExist(const QString &filePath) {
    if (QFile::exists(filePath)) {
        if (!QFile::remove(filePath)) {
            logMessage(QString("Failed to delete existing file %1 before creating database").arg(filePath));
            logDetailedWindowsError();
            return;
        }
    }
}

void DatabaseApp::createDatabase(const QString &dirPath) {
    databaseDirectory = dirPath;
    QString dbPath = dirPath + "/mysqlitetester.db";
    dbPath.replace("\\", "/");
    deleteFileIfExist(dbPath);

    try {
        Sqlite::Database database{Utils::PathString{dbPath}};
    } catch (const Sqlite::Exception &e) {
        logMessage(QString("Cannot create %1: %2").arg(dbPath, QString::fromUtf8(e.what())));
        logDetailedWindowsError();
    }
    deleteFileIfExist(dbPath);
    logMessage(QString("Test with %1 was successful.").arg(dbPath));
}

void DatabaseApp::logMessage(const QString &message) {
    logBrowser->append(message);
}

void DatabaseApp::logDetailedWindowsError() {
    DWORD errorCode = GetLastError();
    if (errorCode != 0) {
        LPVOID errorMsg;
        DWORD size = FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPWSTR)&errorMsg, 0, NULL);

        if (size) {
            QString detailedMessage = QString::fromWCharArray((LPWSTR)errorMsg, size);
            logMessage("Windows error: " + detailedMessage);
            LocalFree(errorMsg);
        }
    }
}

int main(int argc, char *argv[]) {
    Sqlite::LibraryInitializer::initialize();


    QApplication app(argc, argv);
    DatabaseApp window;
    window.setWindowTitle("SQLite Write Database Tester");
    window.resize(400, 300);
    window.show();
    return app.exec();
}

#include "main.moc"