launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #26343
[Merge] ~cjwatson/launchpad:py3-test-textsearching into launchpad:master
Colin Watson has proposed merging ~cjwatson/launchpad:py3-test-textsearching into launchpad:master.
Commit message:
Fix textsearching.txt for Python 3
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/398260
There are a few fixes here, but it's mainly a matter of using non-deprecated DB interfaces.
--
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-test-textsearching into launchpad:master.
diff --git a/lib/lp/services/database/doc/textsearching.txt b/lib/lp/services/database/doc/textsearching.txt
index 77c022f..65d8f9d 100644
--- a/lib/lp/services/database/doc/textsearching.txt
+++ b/lib/lp/services/database/doc/textsearching.txt
@@ -17,21 +17,22 @@ Querying
The examples use the following helper function to execute SQL commands
against the database and display the results:
- >>> from lp.services.database.sqlbase import cursor
+ >>> from lp.services.database.interfaces import (
+ ... DEFAULT_FLAVOR,
+ ... IStoreSelector,
+ ... MAIN_STORE,
+ ... )
+
+ >>> store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
+
>>> def runsql(query, *args):
... '''Run an SQL query and return the results as text'''
... colsize = 25
- ... cur = cursor() # Get a cursor
- ... if args:
- ... cur.execute(query, args)
- ... else:
- ... cur.execute(query)
- ... for row in cur.fetchall():
+ ... for row in store.execute(query, args):
... line = ''
... for col in row:
- ... if isinstance(col, (float,int,long)):
+ ... if isinstance(col, (float, six.integer_types)):
... col = '%1.2f' % col
- ... col = col.encode('ascii', 'replace')
... if len(col) > colsize:
... line += '%s... ' % col[:colsize-3]
... else:
@@ -50,14 +51,14 @@ Queries are all case insensitive:
>>> runsql("""SELECT displayname FROM Person WHERE fti @@ ftq('cArlos')
... ORDER BY displayname""")
- Carlos Perell? Mar?n
- Carlos Valdivia Yag?e
+ Carlos Perelló Marín
+ Carlos Valdivia Yagüe
If a query contains multiple words, an AND query is performed:
>>> runsql("""SELECT displayname FROM Person
... WHERE fti @@ ftq('Carlos Valdivia')""")
- Carlos Valdivia Yag?e
+ Carlos Valdivia Yagüe
This can also be explicity performed by joining the words with 'and':
@@ -65,7 +66,7 @@ This can also be explicity performed by joining the words with 'and':
... SELECT displayname FROM Person
... WHERE fti @@ ftq('carlos AND valdivia') ORDER BY displayname
... """)
- Carlos Valdivia Yag?e
+ Carlos Valdivia Yagüe
We also support 'OR' as a boolean operation:
@@ -73,7 +74,7 @@ We also support 'OR' as a boolean operation:
... SELECT displayname FROM Person
... WHERE fti @@ ftq('valdivia OR mark') ORDER BY displayname
... """)
- Carlos Valdivia Yag?e
+ Carlos Valdivia Yagüe
Mark Shuttleworth
NULL searches will return nothing:
@@ -97,32 +98,30 @@ The following examples show the text version of the query using
>>> from lp.services.database.sqlbase import SQLBase
>>> def ftq(query):
- ... query = query.encode('UTF-8')
- ... cur = cursor()
... try:
- ... cur.execute("SELECT _ftq(%s), ftq(%s)", (query, query))
- ... uncompiled, compiled = cur.fetchone()
+ ... result = store.execute(
+ ... "SELECT _ftq(%s), ftq(%s)", (query, query))
+ ... uncompiled, compiled = result.get_one()
... except Exception:
- ... SQLBase._connection._connection.rollback()
+ ... store.rollback()
... raise
... if uncompiled is not None:
... uncompiled = backslashreplace(uncompiled)
... uncompiled = uncompiled.replace(' ','')
... if compiled is not None:
- ... compiled = compiled.decode('UTF-8')
... compiled = backslashreplace(compiled)
... print('%s <=> %s' % (uncompiled, compiled))
>>>
>>> def search(text_to_search, search_phrase):
- ... cur = cursor()
- ... cur.execute("SELECT to_tsvector(%s)", (text_to_search, ))
- ... ts_vector = cur.fetchall()[0][0]
- ... cur.execute("SELECT ftq(%s)", (search_phrase, ))
- ... ts_query = cur.fetchall()[0][0]
- ... cur.execute(
+ ... result = store.execute(
+ ... "SELECT to_tsvector(%s)", (text_to_search, ))
+ ... ts_vector = result.get_all()[0][0]
+ ... result = store.execute("SELECT ftq(%s)", (search_phrase, ))
+ ... ts_query = result.get_all()[0][0]
+ ... result = store.execute(
... "SELECT to_tsvector(%s) @@ ftq(%s)",
... (text_to_search, search_phrase))
- ... match = cur.fetchall()[0][0]
+ ... match = result.get_all()[0][0]
... return six.ensure_str("FTI data: %s query: %s match: %s") % (
... ts_vector, ts_query, str(match))
>>>
@@ -375,10 +374,10 @@ NB. This gets stemmed, see below.
Bug #44913 - Unicode characters in the wrong place.
- >>> search_same(u'abc-a\N{LATIN SMALL LETTER C WITH CEDILLA}')
- "FTI data: 'abc':2 'abc-a\xc3\xa7':1 'a\xc3\xa7':3
- query: 'abc-a\xc3\xa7' & 'abc' & 'a\xc3\xa7'
- match: True"
+ >>> print(search_same(u'abc-a\N{LATIN SMALL LETTER C WITH CEDILLA}'))
+ FTI data: 'abc':2 'abc-aç':1 'aç':3
+ query: 'abc-aç' & 'abc' & 'aç'
+ match: True
Cut & Paste of 'Smart' quotes. Note that the quotation mark is retained
in the FTI.