Merge ~cjwatson/launchpad:py3-test-textsearching into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 0b5a516267bcb878d790d18f6289795f28a3868e
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:py3-test-textsearching
Merge into: launchpad:master
Diff against target: 131 lines (+30/-31)
1 file modified
lib/lp/services/database/doc/textsearching.txt (+30/-31)
Reviewer Review Type Date Requested Status
Cristian Gonzalez (community) Approve
Review via email: mp+398260@code.launchpad.net

Commit message

Fix textsearching.txt for Python 3

Description of the change

There are a few fixes here, but it's mainly a matter of using non-deprecated DB interfaces.

To post a comment you must log in.
Revision history for this message
Cristian Gonzalez (cristiangsp) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/database/doc/textsearching.txt b/lib/lp/services/database/doc/textsearching.txt
2index 77c022f..65d8f9d 100644
3--- a/lib/lp/services/database/doc/textsearching.txt
4+++ b/lib/lp/services/database/doc/textsearching.txt
5@@ -17,21 +17,22 @@ Querying
6 The examples use the following helper function to execute SQL commands
7 against the database and display the results:
8
9- >>> from lp.services.database.sqlbase import cursor
10+ >>> from lp.services.database.interfaces import (
11+ ... DEFAULT_FLAVOR,
12+ ... IStoreSelector,
13+ ... MAIN_STORE,
14+ ... )
15+
16+ >>> store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
17+
18 >>> def runsql(query, *args):
19 ... '''Run an SQL query and return the results as text'''
20 ... colsize = 25
21- ... cur = cursor() # Get a cursor
22- ... if args:
23- ... cur.execute(query, args)
24- ... else:
25- ... cur.execute(query)
26- ... for row in cur.fetchall():
27+ ... for row in store.execute(query, args):
28 ... line = ''
29 ... for col in row:
30- ... if isinstance(col, (float,int,long)):
31+ ... if isinstance(col, (float, six.integer_types)):
32 ... col = '%1.2f' % col
33- ... col = col.encode('ascii', 'replace')
34 ... if len(col) > colsize:
35 ... line += '%s... ' % col[:colsize-3]
36 ... else:
37@@ -50,14 +51,14 @@ Queries are all case insensitive:
38
39 >>> runsql("""SELECT displayname FROM Person WHERE fti @@ ftq('cArlos')
40 ... ORDER BY displayname""")
41- Carlos Perell? Mar?n
42- Carlos Valdivia Yag?e
43+ Carlos Perelló Marín
44+ Carlos Valdivia Yagüe
45
46 If a query contains multiple words, an AND query is performed:
47
48 >>> runsql("""SELECT displayname FROM Person
49 ... WHERE fti @@ ftq('Carlos Valdivia')""")
50- Carlos Valdivia Yag?e
51+ Carlos Valdivia Yagüe
52
53 This can also be explicity performed by joining the words with 'and':
54
55@@ -65,7 +66,7 @@ This can also be explicity performed by joining the words with 'and':
56 ... SELECT displayname FROM Person
57 ... WHERE fti @@ ftq('carlos AND valdivia') ORDER BY displayname
58 ... """)
59- Carlos Valdivia Yag?e
60+ Carlos Valdivia Yagüe
61
62 We also support 'OR' as a boolean operation:
63
64@@ -73,7 +74,7 @@ We also support 'OR' as a boolean operation:
65 ... SELECT displayname FROM Person
66 ... WHERE fti @@ ftq('valdivia OR mark') ORDER BY displayname
67 ... """)
68- Carlos Valdivia Yag?e
69+ Carlos Valdivia Yagüe
70 Mark Shuttleworth
71
72 NULL searches will return nothing:
73@@ -97,32 +98,30 @@ The following examples show the text version of the query using
74
75 >>> from lp.services.database.sqlbase import SQLBase
76 >>> def ftq(query):
77- ... query = query.encode('UTF-8')
78- ... cur = cursor()
79 ... try:
80- ... cur.execute("SELECT _ftq(%s), ftq(%s)", (query, query))
81- ... uncompiled, compiled = cur.fetchone()
82+ ... result = store.execute(
83+ ... "SELECT _ftq(%s), ftq(%s)", (query, query))
84+ ... uncompiled, compiled = result.get_one()
85 ... except Exception:
86- ... SQLBase._connection._connection.rollback()
87+ ... store.rollback()
88 ... raise
89 ... if uncompiled is not None:
90 ... uncompiled = backslashreplace(uncompiled)
91 ... uncompiled = uncompiled.replace(' ','')
92 ... if compiled is not None:
93- ... compiled = compiled.decode('UTF-8')
94 ... compiled = backslashreplace(compiled)
95 ... print('%s <=> %s' % (uncompiled, compiled))
96 >>>
97 >>> def search(text_to_search, search_phrase):
98- ... cur = cursor()
99- ... cur.execute("SELECT to_tsvector(%s)", (text_to_search, ))
100- ... ts_vector = cur.fetchall()[0][0]
101- ... cur.execute("SELECT ftq(%s)", (search_phrase, ))
102- ... ts_query = cur.fetchall()[0][0]
103- ... cur.execute(
104+ ... result = store.execute(
105+ ... "SELECT to_tsvector(%s)", (text_to_search, ))
106+ ... ts_vector = result.get_all()[0][0]
107+ ... result = store.execute("SELECT ftq(%s)", (search_phrase, ))
108+ ... ts_query = result.get_all()[0][0]
109+ ... result = store.execute(
110 ... "SELECT to_tsvector(%s) @@ ftq(%s)",
111 ... (text_to_search, search_phrase))
112- ... match = cur.fetchall()[0][0]
113+ ... match = result.get_all()[0][0]
114 ... return six.ensure_str("FTI data: %s query: %s match: %s") % (
115 ... ts_vector, ts_query, str(match))
116 >>>
117@@ -375,10 +374,10 @@ NB. This gets stemmed, see below.
118
119 Bug #44913 - Unicode characters in the wrong place.
120
121- >>> search_same(u'abc-a\N{LATIN SMALL LETTER C WITH CEDILLA}')
122- "FTI data: 'abc':2 'abc-a\xc3\xa7':1 'a\xc3\xa7':3
123- query: 'abc-a\xc3\xa7' & 'abc' & 'a\xc3\xa7'
124- match: True"
125+ >>> print(search_same(u'abc-a\N{LATIN SMALL LETTER C WITH CEDILLA}'))
126+ FTI data: 'abc':2 'abc-aç':1 'aç':3
127+ query: 'abc-aç' & 'abc' & 'aç'
128+ match: True
129
130 Cut & Paste of 'Smart' quotes. Note that the quotation mark is retained
131 in the FTI.

Subscribers

People subscribed via source and target branches

to status/vote changes: