-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDBDatabase.cpp
68 lines (56 loc) · 1.54 KB
/
TDBDatabase.cpp
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
#include "TDBDatabase.h"
TDBDatabase *TDBDatabase::default_db = NULL;
int TDBDatabase::open_count = 0;
TDBDatabase::TDBDatabase(QString host, QString database, QString login, QString password)
: db (QSqlDatabase::addDatabase("QMYSQL"))
{
if (host.isNull() || login.isNull() || database.isNull())
qFatal("Error in database information");
if(password.isNull())
{
TDBPasswordDialog pwd;
pwd.exec();
password = pwd.get_pw();
}
db.setDatabaseName(database);
db.setUserName(login);
db.setPassword(password);
db.setHostName(host);
}
void TDBDatabase::open()
{
if (default_db == NULL)
qFatal("Default db was not set\n");
// need to open
if(TDBDatabase::open_count == 0 || !default_db->db.isOpen())
{
TDBDatabase::open_count = 0;
if (!default_db->db.open())
{
qDebug() << QSqlDatabase::drivers();
QMessageBox::critical(0, "zou.sql", QString("Impossible de se connecter à la base de données:\n").append(default_db->db.lastError().text()));
exit(1);
}
}
TDBDatabase::open_count++;
}
void TDBDatabase::close(bool force)
{
// leave open
if(!force && TDBDatabase::open_count > 1 && default_db->db.isOpen())
TDBDatabase::open_count--;
// closing
else
{
// link not broken
if(force || TDBDatabase::open_count <= 1)
{
default_db->db.close();
}
TDBDatabase::open_count = 0;
}
}
TDBDatabase::~TDBDatabase()
{
default_db = NULL;
}