-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
139 lines (125 loc) · 3.15 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
#include "database.h"
#include <QFile>
#include <QDebug>
void Database::initialize_from_file()
{
/// Use file to initialize
}
Database::Database()
{
}
void Database::fromFile(QString Filename)
{
target_file=Filename ;
initialize_from_file() ;
}
void Database::save_to_file(QString filename)
{
QFile file("save");
if (!file.open(QIODevice::ReadWrite))
{
qDebug()<<"Unable to open the file!"<<endl ;
return;
}
QString to_be_written="***Database\n" ;
to_be_written+="***tables\n" ;
for(int i=0; i<tables.size(); i++)
{
to_be_written+=tables[i]->getTable_name()+"\n" ;
}
to_be_written+="###tables\n" ;
file.write(to_be_written.toLocal8Bit()) ;
for(int i=0; i<tables.size(); i++)
{
tables[i]->serialized_to_file(&file) ;
}
to_be_written="###Database\n" ;
file.write(to_be_written.toLocal8Bit()) ;
qDebug()<<"Written to file"<<endl ;
return ;
}
void Database::addTable(Table *table)
{
tables.append(table) ;
}
bool Database::dropTable(QString table_name)
{
for(int i=0; i<tables.size(); i++)
{
if(tables[i]->getTable_name()==table_name)
{
delete tables[i] ;
tables.removeAt(i) ;
return true;
}
}
return false ;
}
QString Database::getName() const
{
return name;
}
void Database::setName(const QString &value)
{
name = value;
}
bool Database::createRecord(QString tableName, Record *val_record)
{
for(int i=0; i<tables.size(); i++)
{
if(tables[i]->getTable_name()==tableName)
{
return tables[i]->createRecord(val_record) ;
}
}
return false ;
}
QHash<QString, QString> Database::readRecord(QString tableName, QList<QString> attributes, QString where_attribute, QString equals_value)
{
for(int i=0; i<tables.size(); i++)
{
if(tables[i]->getTable_name()==tableName)
{
return tables[i]->readRecord(attributes, where_attribute, equals_value) ;
}
}
return QHash<QString, QString>() ;
}
bool Database::updateRecord(QString tableName, QString attribute_to_change,QString new_value, QString where_attribute, QString has_value)
{
for(int i=0; i<tables.size(); i++)
{
if(tables[i]->getTable_name()==tableName)
{
return tables[i]->updateRecord(attribute_to_change, new_value, where_attribute, has_value) ;
}
}
return false ;
}
bool Database::deleteRecord(QString tableName, QString where_attribute, QString equals_value)
{
for(int i=0; i<tables.size(); i++)
{
if(tables[i]->getTable_name()==tableName)
{
return tables[i]->deleteRecord(where_attribute, equals_value) ;
}
}
return false ;
}
void Database::add_index(QString tablename, Index *index)
{
for(int i=0; i<tables.size(); i++)
{
if(tables[i]->getTable_name()==tablename)
{
return tables[i]->addIndex(index) ;
}
}
}
void Database::print_table(QString table_name)
{
foreach (Table* table, tables) {
if(table->getTable_name()==table_name) {table->print_table() ; return;}
}
}