Skip to content

Commit fdf9a3f

Browse files
author
James William Pye
committed
Fix command extraction.
Prior, only the first field was extracted. There may be multiple words in the command tag. Fixes #22
1 parent c5cbda1 commit fdf9a3f

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

postgresql/protocol/element3.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,19 @@ def extract_count(self):
244244
"""
245245
Extract the last set of digits as an integer.
246246
"""
247-
rms = self.data.strip().split()
248-
if rms[0].lower() == b'copy':
249-
if len(rms) > 1:
250-
return int(rms[-1])
251-
elif rms[-1].isdigit():
252-
return int(rms[-1])
247+
# Find the last sequence of digits.
248+
# If there are no fields consisting only of digits, there is no count.
249+
for x in reversed(self.data.split()):
250+
if x.isdigit():
251+
return int(x)
252+
return None
253253

254254
def extract_command(self):
255-
t = self.data.strip().split()
256-
if t:
257-
return t[0]
258-
return None
255+
"""
256+
Strip all the *surrounding* digits and spaces from the command tag,
257+
and return that string.
258+
"""
259+
return self.data.strip(b'\c\n\t 0123456789') or None
259260

260261
class Null(EmptyMessage):
261262
'Null command'

postgresql/test/test_lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _testILF(self, lib):
7878
self.failUnlessEqual(b.sym_const_column, [1])
7979
self.failUnlessEqual(b.sym_const_rows, [(1,)])
8080
self.failUnlessEqual(b.sym_const_chunks, [[(1,)]])
81-
self.failUnlessEqual(b.sym_const_ddl, ('CREATE', None))
81+
self.failUnlessEqual(b.sym_const_ddl, ('CREATE TABLE', None))
8282
self.failUnlessEqual(b.sym_preload(), 1)
8383
# now stored procs
8484
self.failUnlessEqual(b.sym_proc(2,), 2)

postgresql/test/test_protocol.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,26 @@ def testUnknownNoticeFields(self):
250250
self.failUnlessEqual(E[b'P'], b'foobar')
251251
self.failUnlessEqual(len(N), 4)
252252
self.failUnlessEqual(len(E), 4)
253+
254+
def testCompleteExtracts(self):
255+
x = e3.Complete(b'FOO BAR 1321')
256+
self.failUnlessEqual(x.extract_command(), b'FOO BAR')
257+
self.failUnlessEqual(x.extract_count(), 1321)
258+
x = e3.Complete(b' CREATE TABLE 13210 ')
259+
self.failUnlessEqual(x.extract_command(), b'CREATE TABLE')
260+
self.failUnlessEqual(x.extract_count(), 13210)
261+
x = e3.Complete(b' CREATE TABLE \t713210 ')
262+
self.failUnlessEqual(x.extract_command(), b'CREATE TABLE')
263+
self.failUnlessEqual(x.extract_count(), 713210)
264+
x = e3.Complete(b' CREATE TABLE 0 \t13210 ')
265+
self.failUnlessEqual(x.extract_command(), b'CREATE TABLE')
266+
self.failUnlessEqual(x.extract_count(), 13210)
267+
x = e3.Complete(b' 0 \t13210 ')
268+
self.failUnlessEqual(x.extract_command(), None)
269+
self.failUnlessEqual(x.extract_count(), 13210)
270+
253271
##
254-
# xact3 tests
272+
# .protocol.xact3 tests
255273
##
256274

257275
xact_samples = [

0 commit comments

Comments
 (0)