-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdb.cpp
More file actions
63 lines (53 loc) · 1.39 KB
/
db.cpp
File metadata and controls
63 lines (53 loc) · 1.39 KB
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
#include "db.h"
#include <QDateTime>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
bool DB::initialize(const QString &driver, const QString &host, int port,
const QString &database, const QString &username, const QString &password)
{
DB::driver = driver;
QSqlDatabase db = QSqlDatabase::addDatabase(driver);
db.setHostName(host);
db.setPort(port);
db.setUserName(username);
db.setPassword(password);
db.setDatabaseName(database);
if (db.open() == false)
setErrorMessage(db.lastError().text());
return db.isOpen();
}
Builder DB::table(const QString &table)
{
return Builder(table);
}
bool DB::create(const QString &tableName, F func)
{
BluePrint table("create table", tableName);
func(&table);
return table.commit();
}
bool DB::createIfNotExists(const QString &tableName, F func)
{
BluePrint table("create table if not exists", tableName);
func(&table);
return table.commit();
}
bool DB::isSqlite()
{
return driver.contains("SQLITE", Qt::CaseInsensitive);
}
QString DB::lastErrorMessage()
{
return errorMessage;
}
QString DB::sqlTime(int relativeSeconds)
{
return QDateTime::currentDateTime().addSecs(relativeSeconds).toString("yyyy-MM-dd HH:mm:ss");
}
void DB::setErrorMessage(const QString &error)
{
errorMessage = error;
}
QString DB::errorMessage;
QString DB::driver("undefined");