forked from lemirep/Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLiteListItemBinder.cpp
39 lines (32 loc) · 1.34 KB
/
SQLiteListItemBinder.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
#include "SQLiteListItemBinder.h"
Models::SQLiteListItemBinder::SQLiteListItemBinder()
{
}
void Models::SQLiteListItemBinder::fromQSqlRecord(const QSqlRecord &record, Models::ListItem *item)
{
if (item == NULL)
return ;
QHash<QByteArray, int> nameToRoles = item->roleTypesFromName();
for (int i = 0; i < record.count(); i++)
item->setData(nameToRoles[record.fieldName(i).toLocal8Bit()], record.value(i));
}
QString Models::SQLiteListItemBinder::toQSqlQuery(Models::ListItem *item, const QString &tableName)
{
if (item == NULL || tableName.isEmpty())
return "";
QString queryBase("INSERT OR REPLACE INTO " + tableName + " (");
QString queryValues(" VALUES(");
QHash<int, QByteArray> roles = item->roleNames();
QList<int> roleKeys = roles.keys();
for (int i = 0; i < roles.count(); i++)
{
int roleKey = roleKeys.at(i);
QVariant val = item->data(roleKey);
queryBase += roles[roleKey];
queryValues += (val.type() == QMetaType::QString || val.type() == QMetaType::QUrl) ?
("\'" + val.toString() + "\'") : val.toString();
queryBase += (i + 1 < roles.count()) ? ", " : ")";
queryValues += (i + 1 < roles.count()) ? ", " : ");";
}
return queryBase + queryValues;
}