diff --git a/plwn/storages/sqlite.py b/plwn/storages/sqlite.py index 3439c4629408d3b378ca8184481832c8204add1b..4d918a8444c918b70dd14436b73d4acca0b615af 100644 --- a/plwn/storages/sqlite.py +++ b/plwn/storages/sqlite.py @@ -198,7 +198,10 @@ CREATE TABLE "tbl_lexicalrelationchildpart" ( PRIMARY KEY ("id") ); CREATE UNIQUE INDEX "name" ON "tbl_lexicalrelationchildpart" ("name"); -CREATE UNIQUE INDEX "parentpart" ON "tbl_lexicalrelationtype" ("parentpart", "childpart"); +CREATE UNIQUE INDEX "parentpart" ON "tbl_lexicalrelationtype" ( + "parentpart", + "childpart" +); -- Next, gather these parts into relation types themselves. -- Parent can't be NULL - the no-parent case will be handled by a special empty @@ -225,13 +228,17 @@ CREATE TABLE "tbl_synsetrelationalias" ( "relationid" BLOB NOT NULL , PRIMARY KEY ("name") ); -CREATE INDEX "synsetrelationalias_irev" ON "tbl_synsetrelationalias" ("relationid"); +CREATE INDEX "synsetrelationalias_irev" ON "tbl_synsetrelationalias" ( + "relationid" +); CREATE TABLE "tbl_lexicalrelationalias" ( "name" VARCHAR(255) NOT NULL , "relationid" BLOB NOT NULL , PRIMARY KEY ("name") ); -CREATE INDEX "lexicalrelationalias_irev" ON "tbl_lexicalrelationalias" ("relationid"); +CREATE INDEX "lexicalrelationalias_irev" ON "tbl_lexicalrelationalias" ( + "relationid" +); -- Next are finally the relation instances CREATE TABLE "tbl_synsetrelation" ( @@ -360,8 +367,9 @@ class PLWordNet(bs.PLWordNetBase): cur.execute( u""" SELECT lemma, pos.value, variant, synset - FROM lexicalunit JOIN pos ON lexicalunit.pos = pos.id - WHERE lexicalunit.id = ? + FROM tbl_lexicalunit + JOIN tbl_pos ON tbl_lexicalunit.pos = tbl_pos.id + WHERE tbl_lexicalunit.id = ? """, (id_,) ) @@ -389,16 +397,16 @@ class PLWordNet(bs.PLWordNetBase): cur.execute( u""" SELECT source, target, relationtype - FROM lexicalrelation + FROM tbl_lexicalrelation """ + where_clause, param_tuple, ) lu_q = u""" SELECT lemma, pos.value, variant, synset - FROM lexicalunit - JOIN pos ON pos.id = lexicalunit.pos - WHERE lexicalunit.id = ? + FROM tbl_lexicalunit + JOIN tbl_pos ON tbl_pos.id = tbl_lexicalunit.pos + WHERE tbl_lexicalunit.id = ? """ edges = [] @@ -455,7 +463,7 @@ class PLWordNet(bs.PLWordNetBase): def synset_by_id(self, id_): with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT EXISTS (SELECT 1 FROM synset WHERE id = ?)", + u"SELECT EXISTS (SELECT 1 FROM tbl_synset WHERE id = ?)", (id_,), ) if not cur.fetchone()[0]: @@ -481,14 +489,14 @@ class PLWordNet(bs.PLWordNetBase): ) select_clause = u"SELECT source, target, relationtype" - from_clause = u"FROM synsetrelation" + from_clause = u"FROM tbl_synsetrelation" # Pre-fetch artificial status if skipping is necessary if skip_artificial: select_clause += u", parentsyn.isartificial, childsyn.isartificial" from_clause += ( - u" JOIN synset AS parentsyn ON parentsyn.id = source" - u" JOIN synset AS childsyn ON childsyn.id = target" + u" JOIN tbl_synset AS parentsyn ON parentsyn.id = source" + u" JOIN tbl_synset AS childsyn ON childsyn.id = target" ) yield_edges = self.__syn_edges_withskip else: @@ -538,11 +546,11 @@ class PLWordNet(bs.PLWordNetBase): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT lexicalunit.id, lemma, pos.value, variant, synset - FROM lexicalunit - JOIN pos ON lexicalunit.pos = pos.id + SELECT tbl_lexicalunit.id, lemma, pos.value, variant, synset + FROM tbl_lexicalunit + JOIN tbl_pos ON tbl_lexicalunit.pos = tbl_pos.id WHERE COALESCE(lemma = :lem, :defval) - AND COALESCE(pos.value = :pos, :defval) + AND COALESCE(tbl_pos.value = :pos, :defval) AND COALESCE(variant = :var, :defval) """, { @@ -575,34 +583,34 @@ class PLWordNet(bs.PLWordNetBase): with self._db: self._db.executemany( - u"INSERT OR IGNORE INTO pos (value) VALUES (?)", + u"INSERT OR IGNORE INTO tbl_pos (value) VALUES (?)", ((p.value,) for p in en.PoS), ).close() self._db.executemany( - u"INSERT OR IGNORE INTO verbaspect (value) VALUES (?)", + u"INSERT OR IGNORE INTO tbl_verbaspect (value) VALUES (?)", ((va.value,) for va in en.VerbAspect), ).close() self._db.executemany( - u"INSERT OR IGNORE INTO emotionmark (value) VALUES (?)", + u"INSERT OR IGNORE INTO tbl_emotionmark (value) VALUES (?)", ((em.value,) for em in en.EmotionMarkedness), ).close() self._db.executemany( - u"INSERT OR IGNORE INTO emotionname (value) VALUES (?)", + u"INSERT OR IGNORE INTO tbl_emotionname (value) VALUES (?)", ((en.value,) for en in en.EmotionName), ).close() self._db.executemany( - u"INSERT OR IGNORE INTO emotionvaluation (value) VALUES (?)", + u"INSERT OR IGNORE INTO tbl_emotionvaluation (value) VALUES (?)", ((ev.value,) for ev in en.EmotionValuation), ).close() self._db.executemany( - u"INSERT OR IGNORE INTO domain (value) VALUES (?)", + u"INSERT OR IGNORE INTO tbl_domain (value) VALUES (?)", ((dm.value,) for dm in en.Domain), ).close() # Insert version if the database is new self._db.execute( u""" - INSERT OR IGNORE INTO plwn_meta (name, value) + INSERT OR IGNORE INTO tbl_plwn_meta (name, value) VALUES ('version', ?) """, (self._SCHEMA_VERSION,), @@ -612,7 +620,7 @@ class PLWordNet(bs.PLWordNetBase): with closing(self._db.cursor()) as cur: try: cur.execute( - u"SELECT value FROM plwn_meta WHERE name = 'version'", + u"SELECT value FROM tbl_plwn_meta WHERE name = 'version'", ) except sqlite3.OperationalError: raise exc.LoadException( @@ -723,7 +731,7 @@ class LexicalUnit(bs.LexicalUnitBase): if self._def is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT definition FROM lexicalunit WHERE id = ?", + u"SELECT definition FROM tbl_lexicalunit WHERE id = ?", (self._id,), ) self._def = cur.fetchone()[0] @@ -734,7 +742,7 @@ class LexicalUnit(bs.LexicalUnitBase): if self._exms is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT example FROM senseexample WHERE unitid = ?", + u"SELECT example FROM tbl_senseexample WHERE unitid = ?", (self._id,), ) self._exms = tuple(row[0] for row in cur) @@ -745,7 +753,7 @@ class LexicalUnit(bs.LexicalUnitBase): if self._exms_srcs is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT source FROM senseexample WHERE unitid = ?", + u"SELECT source FROM tbl_senseexample WHERE unitid = ?", (self._id,), ) self._exms_srcs = tuple(row[0] for row in cur) @@ -756,7 +764,7 @@ class LexicalUnit(bs.LexicalUnitBase): if self._extl is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT link FROM externallink WHERE unitid = ?", + u"SELECT link FROM tbl_externallink WHERE unitid = ?", (self._id,), ) self._extl = tuple(row[0] for row in cur) @@ -767,7 +775,7 @@ class LexicalUnit(bs.LexicalUnitBase): if self._usn is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT note FROM usagenote WHERE unitid = ?", + u"SELECT note FROM tbl_usagenote WHERE unitid = ?", (self._id,), ) self._usn = tuple(row[0] for row in cur) @@ -779,10 +787,10 @@ class LexicalUnit(bs.LexicalUnitBase): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT domain.value - FROM lexicalunit JOIN domain - ON lexicalunit.domain = domain.id - WHERE lexicalunit.id = ? + SELECT tbl_domain.value + FROM tbl_lexicalunit JOIN tbl_domain + ON tbl_lexicalunit.domain = tbl_domain.id + WHERE tbl_lexicalunit.id = ? """, (self._id,), ) @@ -795,10 +803,10 @@ class LexicalUnit(bs.LexicalUnitBase): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT verbaspect.value - FROM lexicalunit JOIN verbaspect - ON lexicalunit.verbaspect = verbaspect.id - WHERE lexicalunit.id = ? + SELECT tbl_verbaspect.value + FROM tbl_lexicalunit JOIN tbl_verbaspect + ON tbl_lexicalunit.verbaspect = tbl_verbaspect.id + WHERE tbl_lexicalunit.id = ? """, (self._id,), ) @@ -811,7 +819,7 @@ class LexicalUnit(bs.LexicalUnitBase): if self._is_emo is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT isemotional FROM lexicalunit WHERE id = ?", + u"SELECT isemotional FROM tbl_lexicalunit WHERE id = ?", (self._id,), ) rowval = cur.fetchone()[0] @@ -824,10 +832,10 @@ class LexicalUnit(bs.LexicalUnitBase): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT emotionmark.value - FROM lexicalunit JOIN emotionmark - ON lexicalunit.emotionmark = emotionmark.id - WHERE lexicalunit.id = ? + SELECT tbl_emotionmark.value + FROM tbl_lexicalunit JOIN tbl_emotionmark + ON tbl_lexicalunit.emotionmark = tbl_emotionmark.id + WHERE tbl_lexicalunit.id = ? """, (self._id,), ) @@ -845,11 +853,11 @@ class LexicalUnit(bs.LexicalUnitBase): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT emotionname.value - FROM emotionname JOIN unitemotionname - ON emotionname.id = unitemotionname.nameid - WHERE unitemotionname.unitid = ? - ORDER BY emotionname.value + SELECT tbl_emotionname.value + FROM tbl_emotionname JOIN tbl_unitemotionname + ON tbl_emotionname.id = tbl_unitemotionname.nameid + WHERE tbl_unitemotionname.unitid = ? + ORDER BY tbl_emotionname.value """, (self._id,), ) @@ -862,12 +870,12 @@ class LexicalUnit(bs.LexicalUnitBase): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT emotionvaluation.value - FROM emotionvaluation JOIN unitemotionvaluation - ON emotionvaluation.id = - unitemotionvaluation.valuationid - WHERE unitemotionvaluation.unitid = ? - ORDER BY emotionvaluation.value + SELECT tbl_emotionvaluation.value + FROM tbl_emotionvaluation JOIN tbl_unitemotionvaluation + ON tbl_emotionvaluation.id = + tbl_unitemotionvaluation.valuationid + WHERE tbl_unitemotionvaluation.unitid = ? + ORDER BY tbl_emotionvaluation.value """, (self._id,), ) @@ -882,7 +890,7 @@ class LexicalUnit(bs.LexicalUnitBase): if self._emo_ex1 is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT emotionexample1 FROM lexicalunit WHERE id = ?", + u"SELECT emotionexample1 FROM tbl_lexicalunit WHERE id = ?", (self._id,), ) self._emo_ex1 = cur.fetchone()[0] @@ -893,7 +901,7 @@ class LexicalUnit(bs.LexicalUnitBase): if self._emo_ex2 is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT emotionexample2 FROM lexicalunit WHERE id = ?", + u"SELECT emotionexample2 FROM tbl_lexicalunit WHERE id = ?", (self._id,), ) self._emo_ex2 = cur.fetchone()[0] @@ -907,7 +915,7 @@ class LexicalUnit(bs.LexicalUnitBase): cur.execute( u""" SELECT DISTINCT relationtype - FROM lexicalrelation + FROM tbl_lexicalrelation WHERE source = ? """, (self._id,), @@ -926,10 +934,10 @@ class LexicalUnit(bs.LexicalUnitBase): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT lexicalunit.id, lemma, pos.value, variant, synset - FROM lexicalrelation - JOIN lexicalunit ON lexicalunit.id = target - JOIN pos ON lexicalunit.pos = pos.id + SELECT tbl_lexicalunit.id, lemma, tbl_pos.value, variant, synset + FROM tbl_lexicalrelation + JOIN tbl_lexicalunit ON tbl_lexicalunit.id = target + JOIN tbl_pos ON tbl_lexicalunit.pos = tbl_pos.id WHERE source = ? {} """.format(_make_relationtype_where(relinfos)), tuple(itt.chain( @@ -952,10 +960,10 @@ class LexicalUnit(bs.LexicalUnitBase): cur.execute( u""" SELECT relationtype, - lexicalunit.id, lemma, pos.value, variant, synset - FROM lexicalrelation - JOIN lexicalunit ON lexicalunit.id = target - JOIN pos ON lexicalunit.pos = pos.id + tbl_lexicalunit.id, lemma, tbl_pos.value, variant, synset + FROM tbl_lexicalrelation + JOIN tbl_lexicalunit ON tbl_lexicalunit.id = target + JOIN tbl_pos ON tbl_lexicalunit.pos = tbl_pos.id WHERE source = ? {} """.format(_make_relationtype_where(relinfos)), tuple(itt.chain( @@ -1024,8 +1032,8 @@ class Synset(bs.SynsetBase): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT lexicalunit.id, lemma, pos.value, variant - FROM lexicalunit JOIN pos ON lexicalunit.pos = pos.id + SELECT tbl_lexicalunit.id, lemma, tbl_pos.value, variant + FROM tbl_lexicalunit JOIN tbl_pos ON tbl_lexicalunit.pos = tbl_pos.id WHERE synset = ? ORDER BY unitindex """, @@ -1051,7 +1059,7 @@ class Synset(bs.SynsetBase): if self._def is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT definition FROM synset WHERE id = ?", + u"SELECT definition FROM tbl_synset WHERE id = ?", (self._id,), ) row = cur.fetchone() @@ -1064,7 +1072,7 @@ class Synset(bs.SynsetBase): if self._isart is _UNFETCHED: with closing(self._db.cursor()) as cur: cur.execute( - u"SELECT isartificial FROM synset WHERE id = ?", + u"SELECT isartificial FROM tbl_synset WHERE id = ?", (self._id,), ) row = cur.fetchone() @@ -1080,7 +1088,7 @@ class Synset(bs.SynsetBase): cur.execute( u""" SELECT DISTINCT relationtype - FROM synsetrelation + FROM tbl_synsetrelation WHERE source = ? """, (self._id,), @@ -1102,11 +1110,11 @@ class Synset(bs.SynsetBase): en.RelationKind.synset, ) select_clause = u"SELECT target" - from_clause = u"FROM synsetrelation" + from_clause = u"FROM tbl_synsetrelation" if skip_artificial: - select_clause += u", synset.isartificial, relationtype" - from_clause += u" JOIN synset ON target = synset.id" + select_clause += u", tbl_synset.isartificial, relationtype" + from_clause += u" JOIN tbl_synset ON target = tbl_synset.id" yield_related = self.__related_withskip else: yield_related = self.__related_noskip @@ -1139,11 +1147,11 @@ class Synset(bs.SynsetBase): en.RelationKind.synset, ) select_clause = u"SELECT relationtype, target" - from_clause = u"FROM synsetrelation" + from_clause = u"FROM tbl_synsetrelation" if skip_artificial: - select_clause += u", synset.isartificial" - from_clause += u" JOIN synset ON target = synset.id" + select_clause += u", tbl_synset.isartificial" + from_clause += u" JOIN tbl_synset ON target = tbl_synset.id" yield_related = self.__related_withskip_pairs else: yield_related = self.__related_noskip_pairs @@ -1305,18 +1313,18 @@ class _DBBuilder(object): # Synset to lexical units relations also need to be deferred. self._synid2lexids = coll.defaultdict(list) # Cache IDs of constant values - with closing(db.execute(u"SELECT value, id FROM pos")) as cur: + with closing(db.execute(u"SELECT value, id FROM tbl_pos")) as cur: self._posids = dict(cur) - with closing(db.execute(u"SELECT value, id FROM verbaspect")) as cur: + with closing(db.execute(u"SELECT value, id FROM tbl_verbaspect")) as cur: self._vaids = dict(cur) - with closing(db.execute(u"SELECT value, id FROM emotionmark")) as cur: + with closing(db.execute(u"SELECT value, id FROM tbl_emotionmark")) as cur: self._emids = dict(cur) - with closing(db.execute(u"SELECT value, id FROM emotionname")) as cur: + with closing(db.execute(u"SELECT value, id FROM tbl_emotionname")) as cur: self._enids = dict(cur) - with closing(db.execute(u"SELECT value, id FROM emotionvaluation")) \ + with closing(db.execute(u"SELECT value, id FROM tbl_emotionvaluation")) \ as cur: self._evids = dict(cur) - with closing(db.execute(u"SELECT value, id FROM domain")) as cur: + with closing(db.execute(u"SELECT value, id FROM tbl_domain")) as cur: self._dmids = dict(cur) def __call__(self, reader): @@ -1343,7 +1351,7 @@ class _DBBuilder(object): def _insert_synset(self, syn_node): self._db.execute( u""" - INSERT INTO synset (id, definition, isartificial) + INSERT INTO tbl_synset (id, definition, isartificial) VALUES (?, ?, ?) """, (syn_node.id, syn_node.definition, syn_node.is_artificial), @@ -1394,7 +1402,7 @@ class _DBBuilder(object): try: cur.execute( u""" - INSERT INTO lexicalunit ( + INSERT INTO tbl_lexicalunit ( id, lemma, pos, variant, synset, unitindex, definition, domain, verbaspect, @@ -1445,7 +1453,7 @@ class _DBBuilder(object): cur.executemany( u""" - INSERT INTO senseexample (unitid, example, source) + INSERT INTO tbl_senseexample (unitid, example, source) VALUES (?, ?, ?) """, ( @@ -1456,14 +1464,14 @@ class _DBBuilder(object): ) cur.executemany( u""" - INSERT INTO usagenote (unitid, note) + INSERT INTO tbl_usagenote (unitid, note) VALUES (?, ?) """, ((lu_node.id, note) for note in lu_node.usage_notes), ) cur.executemany( u""" - INSERT INTO externallink (unitid, link) + INSERT INTO tbl_externallink (unitid, link) VALUES (?, ?) """, ((lu_node.id, link) @@ -1471,7 +1479,7 @@ class _DBBuilder(object): ) cur.executemany( u""" - INSERT INTO unitemotionname (unitid, nameid) + INSERT INTO tbl_unitemotionname (unitid, nameid) VALUES (?, ?) """, ( @@ -1481,7 +1489,7 @@ class _DBBuilder(object): ) cur.executemany( u""" - INSERT INTO unitemotionvaluation (unitid, valuationid) + INSERT INTO tbl_unitemotionvaluation (unitid, valuationid) VALUES (?, ?) """, ( @@ -1618,10 +1626,10 @@ class _DBBuilder(object): with closing(self._db.cursor()) as cur: cur.execute( u""" - SELECT synset.id - FROM synset - LEFT JOIN lexicalunit ON synset.id = lexicalunit.synset - WHERE lexicalunit.synset IS NULL + SELECT tbl_synset.id + FROM tbl_synset + LEFT JOIN tbl_lexicalunit ON tbl_synset.id = tbl_lexicalunit.synset + WHERE tbl_lexicalunit.synset IS NULL """, ) empties = tuple(row[0] for row in cur) @@ -1634,7 +1642,7 @@ class _DBBuilder(object): _LOG.warning('Synset %d is empty', synid) self._db.execute( - u"DELETE FROM synset WHERE id IN ({})".format( + u"DELETE FROM tbl_synset WHERE id IN ({})".format( u','.join(u'?' * len(empties)) ), empties,