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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/*
Copyright (C) 2006-2010 Tuomas Suutari <thsuut@utu.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see the file COPYING); if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301 USA.
*/
#include "SQLMemberMap.h"
#include "QueryHelper.h"
#include "QueryErrors.h"
#include <QList>
#include "Utilities/Graph.h"
#include "Utilities/QStr.h"
using namespace SQLDB;
SQLMemberMap::SQLMemberMap(QueryHelper& queryHelper):
_qh(queryHelper)
{
}
DB::MemberMap& SQLMemberMap::operator=(const DB::MemberMap& other)
{
// Works also in the _rare_ case this == &other, so don't check
overwriteWithMemberMap(other.memberMap());
return *this;
}
QStringList SQLMemberMap::groups(const QString& category) const
{
return
_qh.executeQuery("SELECT tag.name FROM tag, category "
"WHERE tag.category_id=category.id AND "
"tag.isGroup AND category.name=?",
QueryHelper::Bindings() << category).asList<QString>();
}
void SQLMemberMap::deleteGroup(const QString& category, const QString& name)
{
_qh.executeStatement("DELETE FROM tag_member "
"WHERE tag_id=(SELECT tag.id FROM tag, category "
"WHERE tag.category_id=category.id AND "
"category.name=? AND tag.name=?)",
QueryHelper::Bindings() << category << name);
_qh.executeStatement("UPDATE tag, category SET tag.isGroup='0' "
"WHERE tag.category_id=category.id AND "
"category.name=? AND tag.name=?",
QueryHelper::Bindings() << category << name);
}
QStringList SQLMemberMap::members(const QString& category,
const QString& memberGroup,
bool closure) const
{
if (closure) {
QList<int> idList = _qh.tagIdList(category, memberGroup);
idList.pop_front(); // the tag itself
return
_qh.executeQuery("SELECT name FROM tag WHERE id IN (?)",
QueryHelper::Bindings() << QVariant(toVariantList(idList))
).asList<QString>();
}
else
return
_qh.executeQuery("SELECT m.name "
"FROM tag t, tag m, tag_member tm, category c "
"WHERE m.id=tm.member_tag_id AND "
"t.id=tm.tag_id AND "
"t.category_id=c.id AND c.name=? AND t.name=?",
QueryHelper::Bindings() <<
category << memberGroup).asList<QString>();
}
void SQLMemberMap::setMembers(const QString& category,
const QString& memberGroup,
const QStringList& members)
{
if (category == QStr("Folder"))
return;
int groupId = _qh.tagId(category, memberGroup);
_qh.executeStatement("UPDATE tag SET isGroup='1' WHERE id=?",
QueryHelper::Bindings() << groupId);
_qh.executeStatement("DELETE FROM tag_member WHERE tag_id=?",
QueryHelper::Bindings() << groupId);
for (QStringList::const_iterator i = members.begin();
i != members.end(); ++i) {
int memberId = _qh.tagId(category, *i);
_qh.executeStatement("INSERT INTO tag_member (tag_id, member_tag_id) "
"VALUES (?, ?)",
QueryHelper::Bindings() << groupId << memberId);
}
}
bool SQLMemberMap::isEmpty() const
{
return _qh.executeQuery("SELECT COUNT(*) FROM tag WHERE isGroup"
).firstItem().toUInt() == 0;
}
bool SQLMemberMap::isGroup(const QString& category,
const QString& memberGroup) const
{
return
_qh.executeQuery("SELECT COUNT(*) FROM tag, category "
"WHERE tag.category_id=category.id AND "
"tag.isGroup AND "
"tag.name=? AND category.name=?",
QueryHelper::Bindings() << memberGroup << category
).firstItem().toUInt() > 0;
}
QMap<QString, StringSet>
SQLMemberMap::groupMap(const QString& category) const
{
return Utilities::closure(Utilities::pairsToMap
(_qh.memberGroupConfiguration(category)));
}
QMap<QString, StringSet>
SQLMemberMap::inverseMap(const QString& category) const
{
QMap<QString, StringSet> r;
const StringStringList pairs =
_qh.memberGroupConfiguration(category);
for (StringStringList::const_iterator i = pairs.begin();
i != pairs.end(); ++i)
r[(*i).second].insert((*i).first);
return r;
}
void SQLMemberMap::renameGroup(const QString& category,
const QString& oldName, const QString& newName)
{
// Real renaming is done elsewhere. This is just called to make
// sure membermap is updated, so there's nothing to do.
Q_UNUSED(category);
Q_UNUSED(oldName);
Q_UNUSED(newName);
}
void SQLMemberMap::renameCategory(const QString& oldName,
const QString& newName)
{
// Real renaming is done elsewhere. This is just called to make
// sure membermap is updated, so there's nothing to do.
Q_UNUSED(oldName);
Q_UNUSED(newName);
}
void SQLMemberMap::addGroup(const QString& category, const QString& group)
{
if (category == QStr("Folder"))
return;
if (_qh.executeQuery("SELECT COUNT(*) FROM tag, category "
"WHERE tag.categoryId=category.id AND "
"category.name=? AND tag.name=?",
QueryHelper::Bindings() << category << group
).firstItem().toUInt() == 0)
_qh.insertTagFirst(_qh.categoryId(category), group);
_qh.executeStatement("UPDATE tag, category SET tag.isGroup='1' "
"WHERE tag.categoryId=category.id AND "
"category.name=? AND tag.name=?",
QueryHelper::Bindings() << category << group);
}
void SQLMemberMap::addMemberToGroup(const QString& category,
const QString& group, const QString& item)
{
if (category == QStr("Folder"))
return;
int groupId;
int memberId;
try {
groupId = _qh.tagId(category, group);
}
catch (EntryNotFoundError&) {
addGroup(category, group);
groupId = _qh.tagId(category, group);
}
try {
memberId = _qh.tagId(category, item);
}
catch (EntryNotFoundError&) {
_qh.insertTagFirst(_qh.categoryId(category), item);
memberId = _qh.tagId(category, item);
}
if (_qh.executeQuery("SELECT COUNT(*) FROM tag_member "
"WHERE member_tag_id=? AND tag_id=?",
QueryHelper::Bindings() << memberId << groupId
).firstItem().toUInt() == 0)
_qh.executeStatement("INSERT INTO tag_member (tag_id, member_tag_id) "
"VALUES (?, ?)", QueryHelper::Bindings() <<
groupId << memberId);
}
void SQLMemberMap::removeMemberFromGroup(const QString& category,
const QString& group,
const QString& item)
{
if (category == QStr("Folder"))
return;
int groupId = _qh.tagId(category, group);
int memberId = _qh.tagId(category, item);
_qh.executeStatement("DELETE FROM tag_member "
"WHERE tag_id=? AND member_tag_id=?",
QueryHelper::Bindings() << groupId << memberId);
}
const SQLMemberMap::MemberMapping& SQLMemberMap::memberMap() const
{
updateMemberMapCache();
return _memberMapCache;
}
void SQLMemberMap::updateMemberMapCache() const
{
_memberMapCache.clear();
const QStringList c = _qh.categoryNames();
for (QStringList::const_iterator i = c.begin(); i != c.end(); ++i)
_memberMapCache[*i] =
Utilities::pairsToMap(_qh.memberGroupConfiguration(*i));
}
void SQLMemberMap::deleteItem(DB::Category* category, const QString& name)
{
Q_UNUSED(category);
Q_UNUSED(name);
}
void SQLMemberMap::renameItem(DB::Category* category,
const QString& oldName, const QString& newName)
{
Q_UNUSED(category);
Q_UNUSED(oldName);
Q_UNUSED(newName);
}
void SQLMemberMap::overwriteWithMemberMap(const MemberMapping& map)
{
_qh.executeStatement("DELETE FROM tag_member");
for (MemberMapping::const_iterator i = map.constBegin(); i != map.constEnd(); ++i) {
QString category = i.key();
if (category == QStr("Folder"))
continue;
CategoryGroups groups = i.value();
for (CategoryGroups::const_iterator j = groups.constBegin();
j != groups.constEnd(); ++j) {
QString group = j.key();
StringSet members = j.value();
int groupId;
try {
groupId = _qh.tagId(category, group);
}
catch (EntryNotFoundError&) {
// TODO: this
qDebug("NYI: blaa blaa blaa");
continue;
}
_qh.executeStatement("UPDATE tag SET isGroup='1' WHERE id=?",
QueryHelper::Bindings() << groupId);
for (StringSet::const_iterator k = members.begin();
k != members.end(); ++k) {
int memberId;
try {
memberId = _qh.tagId(category, *k);
}
catch (EntryNotFoundError&) {
// TODO: this
qDebug("NYI: blee bloo blee");
continue;
}
_qh.executeStatement("INSERT INTO tag_member "
"(tag_id, member_tag_id) VALUES (?, ?)",
QueryHelper::Bindings() <<
groupId << memberId);
}
}
}
}
#include "SQLMemberMap.moc"
|