SqlRow: Replace class with a new that takes values by column field name
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -21,20 +20,74 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <QVariant>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlRecord>
|
||||
|
||||
#include "sqlrow.h"
|
||||
|
||||
#include "collectionquery.h"
|
||||
|
||||
SqlRow::SqlRow(const QSqlQuery &query) { Init(query); }
|
||||
|
||||
void SqlRow::Init(const QSqlQuery &query) {
|
||||
|
||||
int rows = query.record().count();
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
columns_ << query.value(i);
|
||||
const QSqlRecord r = query.record();
|
||||
for (int i = 0; i < r.count(); ++i) {
|
||||
columns_by_number_.insert(i, query.value(i));
|
||||
const QString field_name = r.fieldName(i);
|
||||
if (!columns_by_name_.contains(field_name) || columns_by_name_[field_name].isNull()) {
|
||||
columns_by_name_.insert(field_name, query.value(i));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const QVariant SqlRow::value(const int number) const {
|
||||
|
||||
if (columns_by_number_.contains(number)) {
|
||||
return columns_by_number_[number];
|
||||
}
|
||||
else {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const QVariant SqlRow::value(const QString &name) const {
|
||||
|
||||
if (columns_by_name_.contains(name)) {
|
||||
return columns_by_name_[name];
|
||||
}
|
||||
else {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QString SqlRow::ValueToString(const QString &n) const {
|
||||
return value(n).isNull() ? QString() : value(n).toString();
|
||||
}
|
||||
|
||||
QUrl SqlRow::ValueToUrl(const QString &n) const {
|
||||
return value(n).isNull() ? QUrl() : QUrl(value(n).toString());
|
||||
}
|
||||
|
||||
int SqlRow::ValueToInt(const QString &n) const {
|
||||
return value(n).isNull() ? -1 : value(n).toInt();
|
||||
}
|
||||
|
||||
uint SqlRow::ValueToUInt(const QString &n) const {
|
||||
return value(n).isNull() || value(n).toInt() < 0 ? 0 : value(n).toInt();
|
||||
}
|
||||
|
||||
qint64 SqlRow::ValueToLongLong(const QString &n) const {
|
||||
return value(n).isNull() ? -1 : value(n).toLongLong();
|
||||
}
|
||||
|
||||
float SqlRow::ValueToFloat(const QString &n) const {
|
||||
return value(n).isNull() ? -1.0F : value(n).toFloat();
|
||||
}
|
||||
|
||||
bool SqlRow::ValueToBool(const QString &n) const {
|
||||
return !value(n).isNull() && value(n).toInt() == 1;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -25,24 +24,33 @@
|
||||
|
||||
#include <QList>
|
||||
#include <QVariant>
|
||||
#include <QUrl>
|
||||
#include <QSqlQuery>
|
||||
|
||||
class CollectionQuery;
|
||||
|
||||
class SqlRow {
|
||||
|
||||
public:
|
||||
SqlRow(const QSqlQuery &query);
|
||||
|
||||
const QVariant &value(int i) const { return columns_[i]; }
|
||||
const QVariant value(const int number) const;
|
||||
const QVariant value(const QString &name) const;
|
||||
|
||||
QList<QVariant> columns_;
|
||||
QString ValueToString(const QString &n) const;
|
||||
QUrl ValueToUrl(const QString &n) const;
|
||||
int ValueToInt(const QString &n) const;
|
||||
uint ValueToUInt(const QString &n) const;
|
||||
qint64 ValueToLongLong(const QString &n) const;
|
||||
float ValueToFloat(const QString &n) const;
|
||||
bool ValueToBool(const QString& n) const;
|
||||
|
||||
private:
|
||||
SqlRow();
|
||||
|
||||
void Init(const QSqlQuery &query);
|
||||
|
||||
QMap<int, QVariant> columns_by_number_;
|
||||
QMap<QString, QVariant> columns_by_name_;
|
||||
|
||||
};
|
||||
|
||||
typedef QList<SqlRow> SqlRowList;
|
||||
|
||||
Reference in New Issue
Block a user