don't create empty tags on export

This commit is contained in:
Damien Elmes 2010-02-05 12:27:32 +09:00
parent 187caa874b
commit 582a9dd290
2 changed files with 8 additions and 6 deletions

View file

@ -54,7 +54,8 @@ class Exporter(object):
if not self.limitTags: if not self.limitTags:
cards = self.deck.s.column0("select id from cards") cards = self.deck.s.column0("select id from cards")
else: else:
d = tagIds(self.deck.s, self.limitTags) d = tagIds(self.deck.s, self.limitTags, create=False)
print d
cards = self.deck.s.column0( cards = self.deck.s.column0(
"select cardId from cardTags where tagid in %s" % "select cardId from cardTags where tagid in %s" %
ids2str(d.values())) ids2str(d.values()))

View file

@ -36,21 +36,22 @@ primary key(id))""")
except: except:
pass pass
def tagId(s, tag): def tagId(s, tag, create=True):
"Return ID for tag, creating if necessary." "Return ID for tag, creating if necessary."
id = s.scalar("select id from tags where tag = :tag", tag=tag) id = s.scalar("select id from tags where tag = :tag", tag=tag)
if id: if id or not create:
return id return id
s.statement(""" s.statement("""
insert or ignore into tags insert or ignore into tags
(tag) values (:tag)""", tag=tag) (tag) values (:tag)""", tag=tag)
return s.scalar("select id from tags where tag = :tag", tag=tag) return s.scalar("select id from tags where tag = :tag", tag=tag)
def tagIds(s, tags): def tagIds(s, tags, create=True):
"Return an ID for all tags, creating if necessary." "Return an ID for all tags, creating if necessary."
ids = {} ids = {}
s.statements("insert or ignore into tags (tag) values (:tag)", if create:
[{'tag': t} for t in tags]) s.statements("insert or ignore into tags (tag) values (:tag)",
[{'tag': t} for t in tags])
tagsD = dict([(x.lower(), y) for (x, y) in s.all(""" tagsD = dict([(x.lower(), y) for (x, y) in s.all("""
select tag, id from tags select tag, id from tags
where tag in (%s)""" % ",".join([ where tag in (%s)""" % ",".join([