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:
cards = self.deck.s.column0("select id from cards")
else:
d = tagIds(self.deck.s, self.limitTags)
d = tagIds(self.deck.s, self.limitTags, create=False)
print d
cards = self.deck.s.column0(
"select cardId from cardTags where tagid in %s" %
ids2str(d.values()))

View file

@ -36,21 +36,22 @@ primary key(id))""")
except:
pass
def tagId(s, tag):
def tagId(s, tag, create=True):
"Return ID for tag, creating if necessary."
id = s.scalar("select id from tags where tag = :tag", tag=tag)
if id:
if id or not create:
return id
s.statement("""
insert or ignore into tags
(tag) values (: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."
ids = {}
s.statements("insert or ignore into tags (tag) values (:tag)",
[{'tag': t} for t in tags])
if create:
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("""
select tag, id from tags
where tag in (%s)""" % ",".join([