-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
143 lines (135 loc) · 3.72 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
#include "database.h"
#include <QDebug>
Database::Database()
{
}
void Database::connectionOpen()
{
servername="BRANIAC\\BRANIAC";
databaseName="vault";
db= QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
dsn=QString("DRIVER={SQL Server}; SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(databaseName);
db.setDatabaseName(dsn);
if(db.open())
{
qDebug()<<"Opened...";//database opened
}
else
{
qDebug()<<"Error="<<db.lastError().text();
}
}
void Database::connectionClose()
{
db.close();//close database
qDebug()<<"Database closed...";
}
void Database::SetDbID(QString id)
{
dbID=id;
}
void Database::SetMasterKey(QString key)
{
masterKey=key;
}
void Database::SetDbName(QString name)
{
dbName=name;
}
void Database::SetDescription(QString text)
{
description=text;
}
void Database::SetUserName(QString name)
{
userName=name;
}
void Database::SaveSettings()
{
QSqlQuery save;
save.prepare("insert into databases (dbName,masterKey,description,userName) values('"+dbName+"',HASHBYTES('sha1','"+masterKey+"'),'"+description+"','"+userName+"')");
if(save.exec())
{
qDebug()<<"Saved...";
InsertDefaults();
}
else
{
qDebug()<<"Not Saved..."<<save.lastError().text();
}
}
void Database::EditSettings()
{
QSqlQuery edit;
edit.prepare("update databases set dbName='"+dbName+"',description='"+description+"',userName='"+userName+"' where dbID='"+dbID+"'");
if(edit.exec())
{
qDebug()<<"Edited...";
}
else
{
qDebug()<<"Not Edited..."<<edit.lastError().text();
}
}
QString Database::getDbID()
{
QSqlQuery id;
id.prepare("select dbID from databases where dbName='"+dbName+"'");
if(id.exec())
{
while(id.next())
{
dbID=id.value(0).toString();
}
return dbID;
}
else
qDebug()<<id.lastError().text();
return "error";
}
QString Database::getDbName()
{
return dbName;
}
void Database::InsertDefaults()
{
QString id=getDbID();
QString defaultGroupName="Default",title="Example",password="12345",userName="User123",url="www.deenze.com",notes="Sample notes";
QString defaultKeyTitle="Software Y",key="xxxx-xxxx-xxxx-xxxx-xxxx",characters="*****";
QSqlQuery groupDefault,passwordDefault,getGroupID,keyDefault;
groupDefault.prepare("insert into groups(groupName,dbID) values('"+defaultGroupName+"','"+id+"')");
if(groupDefault.exec()){qDebug()<<"group inserted..";}
getGroupID.prepare("select groupID from groups where groupName='"+defaultGroupName+"' and dbID='"+id+"'");
QString groupID;
if(getGroupID.exec())
{
while(getGroupID.next())
{
groupID=getGroupID.value(0).toString();
}
}
else
{
qDebug()<<getGroupID.lastError().text();
}
passwordDefault.prepare("insert into passwords(dbID,groupID,title,userName,password,notes,url,creationTime,modificationTime,hide)"
"values('"+id+"','"+groupID+"','"+title+"','"+userName+"','"+password+"','"+notes+"','"+url+"',GETDATE(),GETDATE(),'"+characters+"')");
if(passwordDefault.exec())
{
qDebug()<<"Password inserted...";
keyDefault.prepare("insert into serials(dbID,title,serialKey,notes,groupID)values('"+id+"','"+defaultKeyTitle+"','"+key+"','"+notes+"','"+groupID+"')");
if(keyDefault.exec())
{
qDebug()<<"Sample Key inserted...";
}
else
{
qDebug()<<"Sample Key not added..."<<keyDefault.lastError().text();
}
}
else
{
qDebug()<<passwordDefault.lastError().text();
}
}