-
Notifications
You must be signed in to change notification settings - Fork 1
/
itembaserequirementstable.cpp
105 lines (90 loc) · 4.21 KB
/
itembaserequirementstable.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
#include "itembaserequirementstable.h"
#include <QSqlQuery>
#include <QVariant>
const QString ItemBaseRequirementsTable::TableName = "ItemBaseRequirements";
const QString ItemBaseRequirementsTable::ItemBaseRequirementsId = "itemBaseRequirementsId";
const QString ItemBaseRequirementsTable::Level = "level";
const QString ItemBaseRequirementsTable::LevelRequired = "levelRequired";
const QString ItemBaseRequirementsTable::LevelRequiredNormal = "levelRequiredNormal";
const QString ItemBaseRequirementsTable::LevelRequiredSocketable = "levelRequiredSocketable";
const QString ItemBaseRequirementsTable::DefenseRequired = "defenseRequired";
const QString ItemBaseRequirementsTable::DexterityRequired = "dexterityRequired";
const QString ItemBaseRequirementsTable::MagicRequired = "magicRequired";
const QString ItemBaseRequirementsTable::StrengthRequired = "strengthRequired";
ItemBaseRequirementsTable::ItemBaseRequirementsTable(QString databaseName)
{
DatabasePath(databaseName);
}
void ItemBaseRequirementsTable::DatabasePath(QString databaseName)
{
DatabaseName(databaseName);
QSqlDatabase db = GetDatabase();
if (db.open())
{
QSqlQuery query(db);
if (!query.exec("DROP TABLE IF EXISTS " + TableName))
{
PrintSqlError(query.lastError());
}
else if (!query.exec("CREATE TABLE " + TableName + " (" +
ItemBaseRequirementsId + " INTEGER PRIMARY KEY, " +
Level + " INTEGER, " +
LevelRequired + " INTEGER, " +
LevelRequiredNormal + " INTEGER, " +
LevelRequiredSocketable + " INTEGER, " +
DefenseRequired + " INTEGER, " +
DexterityRequired + " INTEGER, " +
MagicRequired + " INTEGER, " +
StrengthRequired + " INTEGER)"))
{
PrintSqlError(query.lastError());
}
else if (!query.exec("CREATE UNIQUE INDEX " + TableName + Level + "Index ON " + TableName +
"(" + Level + ")"))
{
PrintSqlError(query.lastError());
}
db.close();
}
}
qint64 ItemBaseRequirementsTable::AddItemBaseRequirements(ItemBaseRequirements itemRequirements)
{
qint64 result = -1;
QSqlDatabase db = GetDatabase();
if (db.isOpen() || db.open())
{
QSqlQuery query(db);
if (query.prepare("INSERT INTO " + TableName + "(" + Level + ", " + LevelRequired + ", " +
LevelRequiredNormal + ", " + LevelRequiredSocketable + ", " +
DefenseRequired + ", " + DexterityRequired + ", " + MagicRequired + ", " +
StrengthRequired + ") VALUES (" +
":level, :levelRequired, :levelRequiredNormal, :levelRequiredSocketable, " +
":defenseRequired, :dexterityRequired, :magicRequired, :strengthRequired)"))
{
query.bindValue(":level", itemRequirements.level);
query.bindValue(":levelRequired", itemRequirements.levelRequired);
query.bindValue(":levelRequiredNormal", itemRequirements.levelRequiredNormal);
query.bindValue(":levelRequiredSocketable", itemRequirements.levelRequiredSocketable);
query.bindValue(":defenseRequired", itemRequirements.defenseRequired);
query.bindValue(":dexterityRequired", itemRequirements.dexterityRequired);
query.bindValue(":magicRequired", itemRequirements.magicRequired);
query.bindValue(":strengthRequired", itemRequirements.strengthRequired);
if (query.exec())
{
if (!query.lastInsertId().isNull() && query.lastInsertId().isValid())
result = query.lastInsertId().toLongLong();
}
else
{
PrintSqlError(query.lastError());
}
}
else
{
PrintSqlError(query.lastError());
}
if (!mDoingTransaction)
db.close();
}
return result;
}