File indexing completed on 2024-05-12 04:41:20

0001 // SPDX-FileCopyrightText: 2022 Jonah BrĂ¼chert <jbb@kaidan.im
0002 //
0003 // SPDX-License-Identifier: BSD-2-Clause
0004 
0005 
0006 // Qt
0007 #include "qstringliteral.h"
0008 #include <QCoreApplication>
0009 #include <QTimer>
0010 
0011 // QCoro
0012 #include <QCoro/QCoroTask>
0013 #include <QCoro/QCoroFuture>
0014 
0015 // FutureSQL
0016 #include <ThreadedDatabase>
0017 
0018 // STL
0019 #include <tuple>
0020 
0021 
0022 // A data structure that represents data from the "test" table
0023 struct HelloWorld {
0024     // Types that the database columns can be converted to. The types must be convertible from QVariant.
0025     using ColumnTypes = std::tuple<int, QString>;
0026 
0027     // This function gets a row from the database as a tuple, and puts it into the HelloWorld structs.
0028     // If the ColumnTypes already match the types and order of the attributes in the struct, you don't need to implement it.
0029     //
0030     // Try to comment it out, the example should still compile and work.
0031     static HelloWorld fromSql(ColumnTypes &&tuple) {
0032         auto [id, data] = tuple;
0033         return HelloWorld { id, data };
0034     }
0035 
0036     // attributes
0037     int id;
0038     QString data;
0039 };
0040 
0041 QCoro::Task<> databaseExample() {
0042     // This object contains the database configuration,
0043     // in this case just the path to the SQLite file, and the database type (SQLite).
0044     DatabaseConfiguration config;
0045     config.setDatabaseName(QStringLiteral("database.sqlite"));
0046     config.setType(DatabaseType::SQLite);
0047 
0048     // Here we open the database file, and get a handle to the database.
0049     auto database = ThreadedDatabase::establishConnection(config);
0050 
0051     // Execute some queries.
0052     co_await database->execute(QStringLiteral("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT)"));
0053 
0054     // Query parameters are bound by position in the query. The execute function is variadic and you can add as many parameters as you need.
0055     co_await database->execute(QStringLiteral("INSERT INTO test (data) VALUES (?))"), QStringLiteral("Hello World"));
0056 
0057     // Retrieve some data from the database.
0058     // The data is directly returned as our HelloWorld struct.
0059     auto results = co_await database->getResults<HelloWorld>(QStringLiteral("SELECT * FROM test"));
0060 
0061     // Print out the data in the result list
0062     for (const auto &result : results) {
0063         qDebug() << result.id << result.data;
0064     }
0065 
0066     // Quit the event loop as we are done
0067     QCoreApplication::instance()->quit();
0068 }
0069 
0070 // Just a minimal main function for QCoro, to start the Qt event loop.
0071 int main(int argc, char *argv[]) {
0072     QCoreApplication app(argc, argv);
0073     QTimer::singleShot(0, databaseExample);
0074     return app.exec();
0075 }