Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54139ac

Browse files
committed
Fix query-based tab completion for multibyte characters.
The existing code confuses the byte length of the string (which isrelevant when passing it to pg_strncasecmp) with the character lengthof the string (which is relevant when it is used with the SQL substringfunction). Separate those two concepts.Report and patch by Kyotaro Horiguchi, reviewed by Thomas Munro andreviewed and further revised by me.
1 parentff45274 commit54139ac

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

‎src/bin/psql/tab-complete.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,9 +3522,8 @@ static char *
35223522
_complete_from_query(intis_schema_query,constchar*text,intstate)
35233523
{
35243524
staticintlist_index,
3525-
string_length;
3525+
byte_length;
35263526
staticPGresult*result=NULL;
3527-
35283527
/*
35293528
* If this is the first time for this completion, we fetch a list of our
35303529
* "things" from the backend.
@@ -3535,9 +3534,18 @@ _complete_from_query(int is_schema_query, const char *text, int state)
35353534
char*e_text;
35363535
char*e_info_charp;
35373536
char*e_info_charp2;
3537+
constchar*pstr=text;
3538+
intchar_length=0;
35383539

35393540
list_index=0;
3540-
string_length=strlen(text);
3541+
byte_length=strlen(text);
3542+
3543+
/* Count length as number of characters (not bytes), for passing to substring */
3544+
while (*pstr)
3545+
{
3546+
char_length++;
3547+
pstr+=PQmblen(pstr,pset.encoding);
3548+
}
35413549

35423550
/* Free any prior result */
35433551
PQclear(result);
@@ -3590,7 +3598,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
35903598
completion_squery->selcondition);
35913599
appendPQExpBuffer(&query_buffer,"substring(%s,1,%d)='%s'",
35923600
completion_squery->result,
3593-
string_length,e_text);
3601+
char_length,e_text);
35943602
appendPQExpBuffer(&query_buffer," AND %s",
35953603
completion_squery->viscondition);
35963604

@@ -3617,13 +3625,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
36173625
"SELECT pg_catalog.quote_ident(n.nspname) || '.' "
36183626
"FROM pg_catalog.pg_namespace n "
36193627
"WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'",
3620-
string_length,e_text);
3628+
char_length,e_text);
36213629
appendPQExpBuffer(&query_buffer,
36223630
" AND (SELECT pg_catalog.count(*)"
36233631
" FROM pg_catalog.pg_namespace"
36243632
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
36253633
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1",
3626-
string_length,e_text);
3634+
char_length,e_text);
36273635

36283636
/*
36293637
* Add in matching qualified names, but only if there is exactly
@@ -3641,7 +3649,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
36413649
completion_squery->selcondition);
36423650
appendPQExpBuffer(&query_buffer,"substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'",
36433651
qualresult,
3644-
string_length,e_text);
3652+
char_length,e_text);
36453653

36463654
/*
36473655
* This condition exploits the single-matching-schema rule to
@@ -3650,13 +3658,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
36503658
appendPQExpBuffer(&query_buffer,
36513659
" AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) ="
36523660
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)",
3653-
string_length,e_text);
3661+
char_length,e_text);
36543662
appendPQExpBuffer(&query_buffer,
36553663
" AND (SELECT pg_catalog.count(*)"
36563664
" FROM pg_catalog.pg_namespace"
36573665
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
36583666
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1",
3659-
string_length,e_text);
3667+
char_length,e_text);
36603668

36613669
/* If an addon query was provided, use it */
36623670
if (completion_charp)
@@ -3666,7 +3674,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
36663674
{
36673675
/* completion_charp is an sprintf-style format string */
36683676
appendPQExpBuffer(&query_buffer,completion_charp,
3669-
string_length,e_text,
3677+
char_length,e_text,
36703678
e_info_charp,e_info_charp,
36713679
e_info_charp2,e_info_charp2);
36723680
}
@@ -3692,7 +3700,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
36923700

36933701
while (list_index<PQntuples(result)&&
36943702
(item=PQgetvalue(result,list_index++,0)))
3695-
if (pg_strncasecmp(text,item,string_length)==0)
3703+
if (pg_strncasecmp(text,item,byte_length)==0)
36963704
returnpg_strdup(item);
36973705
}
36983706

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp