@@ -79,16 +79,53 @@ def find(self, wordlist):
7979
8080 l = [word .upper () for word in wordlist if 26 > len (word ) > 2 ]
8181
82- a = ',' .join ([self .db .arg ] * len (l ))
83- sql = 'select distinct(_textid) from __words where _word in (%s)' % a
84- self .db .cursor .execute (sql , tuple (l ))
85- r = self .db .cursor .fetchall ()
86- if not r :
82+ if not l :
8783 return {}
88- a = ',' .join ([self .db .arg ] * len (r ))
89- sql = 'select _class, _itemid, _prop from __textids ' \
90- 'where _textid in (%s)' % a
91- self .db .cursor .execute (sql , tuple ([int (id ) for (id ,) in r ]))
84+
85+ if self .db .implements_intersect :
86+ # simple AND search
87+ sql = 'select distinct(_textid) from __words where _word=%s' % self .db .arg
88+ sql = '\n INTERSECT\n ' .join ([sql ]* len (l ))
89+ self .db .cursor .execute (sql , tuple (l ))
90+ r = self .db .cursor .fetchall ()
91+ if not r :
92+ return {}
93+ a = ',' .join ([self .db .arg ] * len (r ))
94+ sql = 'select _class, _itemid, _prop from __textids ' \
95+ 'where _textid in (%s)' % a
96+ self .db .cursor .execute (sql , tuple ([int (id ) for (id ,) in r ]))
97+
98+ else :
99+ # A more complex version for MySQL since it doesn't implement INTERSECT
100+
101+ # Construct SQL statement to join __words table to itself
102+ # multiple times.
103+ sql = """select distinct(__words1._textid)
104+ from __words as __words1 %s
105+ where __words1._word=%s %s"""
106+
107+ join_tmpl = ' left join __words as __words%d using (_textid) \n '
108+ match_tmpl = ' and __words%d._word=%s \n '
109+
110+ join_list = []
111+ match_list = []
112+ for n in xrange (len (l ) - 1 ):
113+ join_list .append (join_tmpl % (n + 2 ))
114+ match_list .append (match_tmpl % (n + 2 , self .db .arg ))
115+
116+ sql = sql % (' ' .join (join_list ), self .db .arg , ' ' .join (match_list ))
117+ self .db .cursor .execute (sql , l )
118+
119+ r = map (lambda x : x [0 ], self .db .cursor .fetchall ())
120+ if not r :
121+ return {}
122+
123+ a = ',' .join ([self .db .arg ] * len (r ))
124+ sql = 'select _class, _itemid, _prop from __textids ' \
125+ 'where _textid in (%s)' % a
126+
127+ self .db .cursor .execute (sql , tuple (map (int , r )))
128+
92129 # self.search_index has the results as {some id: identifier} ...
93130 # sigh
94131 r = {}
0 commit comments