-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabaseadapter.cpp
121 lines (107 loc) · 3.94 KB
/
databaseadapter.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
#include "databaseadapter.h"
#include <QMessageBox>
#include <QObject>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariant>
#include <QDir>
#include <QTextStream>
#include <QtDebug>
#include <QStandardPaths>
DatabaseAdapter::DatabaseAdapter()
{
}
void DatabaseAdapter::initializeDatabase()
{
if(!QSqlDatabase::isDriverAvailable("QSQLITE"))
{
QMessageBox::critical (nullptr,QObject::tr("Fatal error"), QObject::tr("The driver for the database is not available. This can happen if the file sqldrivers/qsqlite.dll cannot be found."));
return;
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QString writableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if( writableLocation.isEmpty() )
{
QMessageBox::information (nullptr,QObject::tr("Error Message"),QObject::tr("There was a problem in opening the database. No writable location could be found. It is unlikely that you will solve this on your own. Rather you had better contact the developer.").arg(db.lastError().databaseText()) );
return;
}
QDir dir(writableLocation);
dir.mkpath(writableLocation);
db.setDatabaseName( dir.absoluteFilePath("names.db"));
if(!db.open())
{
QMessageBox::information (nullptr,QObject::tr("Error Message"),QObject::tr("There was a problem in opening the database. The program said: %1. It is unlikely that you will solve this on your own. Rather you had better contact the developer.").arg(db.lastError().databaseText()) );
return;
}
if ( db.tables().contains( QLatin1String("names") ) ) {
QSqlQuery q;
q.exec("select count(*) from names;");
q.next();
if(q.value(0).toInt()<21742)
{
populateDatabaseFromResource();
}
} else {
populateDatabaseFromResource();
}
}
QString DatabaseAdapter::nameFromCodepoint(quint32 character)
{
QSqlDatabase db = QSqlDatabase::database();
QString unicode = QString("%1").arg(character,4,16,QLatin1Char('0')).toUpper();
QString name="Codepoint not found in database!";
QSqlQuery query(db);
query.prepare("select name from names where codepoint=?;");
query.bindValue(0, unicode);
if(query.exec() && query.next())
name = query.value(0).toString();
return name;
}
quint32 DatabaseAdapter::codepointFromName(const QString &name)
{
QSqlDatabase db = QSqlDatabase::database();
quint32 codepoint = 0xFFFF;
QSqlQuery query(db);
query.prepare("select codepoint from names where name=?;");
query.bindValue(0, name);
if(query.exec() && query.next())
codepoint = uintFromHexCodepoint( query.value(0).toString() );
return codepoint;
}
quint32 DatabaseAdapter::uintFromHexCodepoint(QString codepoint)
{
bool ok;
return codepoint.toUInt(&ok,16);
}
void DatabaseAdapter::populateDatabaseFromResource()
{
QSqlDatabase db = QSqlDatabase::database();
db.transaction();
db.exec("DROP TABLE IF EXISTS names;");
db.exec("CREATE TABLE names ( codepoint text, name text);");
db.exec("CREATE INDEX name_index ON names(name);");
db.exec("CREATE UNIQUE INDEX codepoint_index ON names(codepoint);");
QFile data(":/resources/UnicodeData.txt");
if (data.open(QFile::ReadOnly)) {
QTextStream in(&data);
QSqlQuery q(db);
q.prepare("INSERT INTO names VALUES (?,?);");
while(!in.atEnd()) {
QStringList fields = in.readLine().split(';');
QString name = fields.at(1);
if( name == "<control>" )
{
name = fields.at(10);
}
q.bindValue(0,fields.at(0));
q.bindValue(1,name);
if( ! q.exec() ) {
qDebug() << "DatabaseAdapter::populateDatabaseFromResource" << q.lastError().text() << q.lastQuery();
}
}
db.commit();
} else {
db.rollback();
qDebug() << "Error opening resource";
}
}