-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
190 lines (180 loc) · 6.39 KB
/
database.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
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
// https://github.com/Enderceliik
// Ender CELIK
#include "database.h"
QSqlDatabase database::sqliteDatabase;
database::database() {
if (!sqliteDatabase.isValid()) {
sqliteDatabase = QSqlDatabase::addDatabase("QSQLITE");
sqliteDatabase.setDatabaseName("banking_automation_sqlite_database.db");
if (!sqliteDatabase.open()) {
qDebug() << "The database could not be opened.";
}
}
}
database::~database() {
sqliteDatabase.close();
}
void database::initial_database_definition()
{
QSqlQuery query(sqliteDatabase);
if (!sqliteDatabase.open()) {
sqliteDatabase.open();
}
query.prepare("CREATE TABLE users ("
"userID INTEGER PRIMARY KEY AUTOINCREMENT,"
"password VARCHAR(16),"
"name VARCHAR(50),"
"surname VARCHAR(50),"
"balance DOUBLE,"
"Iban VARCHAR(26),"
"userType INT"
");");
if(query.exec())
{
qDebug() << "users table CREATED succesfully!";
}
query.prepare("CREATE TABLE transactions ("
"transactionID INTEGER PRIMARY KEY AUTOINCREMENT,"
"transactionDate DATETIME DEFAULT CURRENT_TIMESTAMP,"
"amountTransferred DOUBLE,"
"senderIban VARCHAR(26),"
"receivingPartyIban VARCHAR(26)"
// "FOREIGN KEY(senderID) REFERENCES users(userID),"
// "FOREIGN KEY(receivingPartyID) REFERENCES users(userID)"
");");
if(query.exec())
{
qDebug() << "transactions table CREATED succesfully!";
}
else
{
qDebug() << query.lastError().text() + " transactions";
}
queryString = QString("INSERT INTO users (password, "
"name, "
"surname, "
"balance, "
"Iban, "
"userType) "
"values ('%1', '%2', '%3', %4, '%5', %6);")
.arg("159*")
.arg("Ender")
.arg("Çelik")
.arg(19500)
.arg("TR 29296161")
.arg(0);
query.prepare(queryString);
if(query.exec())
{
qDebug() << "users table INSERT succesfully!";
}
queryString = QString("INSERT INTO users (password, "
"name, "
"surname, "
"balance, "
"Iban, "
"userType) "
"values ('%1', '%2', '%3', %4, '%5', %6);")
.arg("159-")
.arg("Lokman")
.arg("Pehlivan")
.arg(42000)
.arg("TR 42424242")
.arg(0);
query.prepare(queryString);
if(query.exec())
{
qDebug() << "users table INSERT 2 succesfully!";
}
queryString = QString("INSERT INTO users (password, "
"name, "
"surname, "
"balance, "
"Iban, "
"userType) "
"values ('%1', '%2', '%3', %4, '%5', %6);")
.arg("654")
.arg("Ahmet")
.arg("Alkaç")
.arg(22000)
.arg("TR 26262929")
.arg(0);
query.prepare(queryString);
if(query.exec())
{
qDebug() << "users table INSERT 3 succesfully!";
}
}
QString database::verification(int userID)
{
if (!sqliteDatabase.isOpen()) {
sqliteDatabase.open();
}
QSqlQuery query(sqliteDatabase);
queryString = QString("SELECT password FROM users where userID = %1").arg(userID);
query.prepare(queryString);
query.exec();
query.next();
return query.value(0).toString();
}
QSqlQuery database::interface_info_fetch(int userID)
{
if (!sqliteDatabase.isOpen()) {
sqliteDatabase.open();
}
QSqlQuery query(sqliteDatabase);
queryString = QString("SELECT * FROM users WHERE userID = %1").arg(userID);
query.prepare(queryString);
query.exec();
query.next();
return query;
}
void database::process(QMap<QString,QVariant> interfaceMap)
{
if (!sqliteDatabase.isOpen()) {
sqliteDatabase.open();
}
QSqlQuery query(sqliteDatabase);
queryString = QString("UPDATE users SET balance = balance - %1 where userID = %2;").arg(interfaceMap.value("amountTransferred").toDouble()).arg(interfaceMap.value("userID").toInt());
query.prepare(queryString);
if(query.exec())
{
qDebug() << "users table insert succesfully!";
}
queryString = QString("UPDATE users SET balance = balance + %1 where userID = %2;").arg(interfaceMap.value("amountTransferred").toDouble()).arg(interfaceMap.value("receivingPartyID").toInt());
query.prepare(queryString);
if(query.exec())
{
qDebug() << "users table insert succesfully!";
}
queryString = QString("INSERT INTO transactions (amountTransferred, senderIban, receivingPartyIban) values(%1, '%2', '%3');").arg(interfaceMap.value("amountTransferred").toDouble())
.arg(interfaceMap.value("Iban").toString())
.arg(interfaceMap.value("receivingPartyIban").toString());
query.prepare(queryString);
if(query.exec())
{
qDebug() << "users table insert succesfully!";
}
}
QSqlQueryModel* database::database_query(QString parameter)
{
if (!sqliteDatabase.isOpen()) {
sqliteDatabase.open();
}
queryString = QString("SELECT transactionDate, amountTransferred, senderIban, receivingPartyIban FROM transactions WHERE senderIban = '%1' OR receivingPartyIban = '%2'").arg(parameter, parameter);
QSqlQueryModel *model = new QSqlQueryModel();
model->setQuery(queryString);
return model;
}
QSqlQuery database::check_iban(QString Iban)
{
if (!sqliteDatabase.isOpen()) {
sqliteDatabase.open();
}
QSqlQuery query(sqliteDatabase);
queryString = QString("SELECT userID, name, surname FROM users WHERE Iban= '%1'").arg(Iban);
query.prepare(queryString);
query.exec();
query.next();
return query;
}