@@ -3689,9 +3689,8 @@ static char *
3689
3689
_complete_from_query (int is_schema_query , const char * text , int state )
3690
3690
{
3691
3691
static int list_index ,
3692
- string_length ;
3692
+ byte_length ;
3693
3693
static PGresult * result = NULL ;
3694
-
3695
3694
/*
3696
3695
* If this is the first time for this completion, we fetch a list of our
3697
3696
* "things" from the backend.
@@ -3702,9 +3701,18 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3702
3701
char * e_text ;
3703
3702
char * e_info_charp ;
3704
3703
char * e_info_charp2 ;
3704
+ const char * pstr = text ;
3705
+ int char_length = 0 ;
3705
3706
3706
3707
list_index = 0 ;
3707
- string_length = strlen (text );
3708
+ byte_length = strlen (text );
3709
+
3710
+ /* Count length as number of characters (not bytes), for passing to substring */
3711
+ while (* pstr )
3712
+ {
3713
+ char_length ++ ;
3714
+ pstr += PQmblen (pstr , pset .encoding );
3715
+ }
3708
3716
3709
3717
/* Free any prior result */
3710
3718
PQclear (result );
@@ -3757,7 +3765,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3757
3765
completion_squery -> selcondition );
3758
3766
appendPQExpBuffer (& query_buffer , "substring(%s,1,%d)='%s'" ,
3759
3767
completion_squery -> result ,
3760
- string_length , e_text );
3768
+ char_length , e_text );
3761
3769
appendPQExpBuffer (& query_buffer , " AND %s" ,
3762
3770
completion_squery -> viscondition );
3763
3771
@@ -3784,13 +3792,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3784
3792
"SELECT pg_catalog.quote_ident(n.nspname) || '.' "
3785
3793
"FROM pg_catalog.pg_namespace n "
3786
3794
"WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'" ,
3787
- string_length , e_text );
3795
+ char_length , e_text );
3788
3796
appendPQExpBuffer (& query_buffer ,
3789
3797
" AND (SELECT pg_catalog.count(*)"
3790
3798
" FROM pg_catalog.pg_namespace"
3791
3799
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
3792
3800
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1" ,
3793
- string_length , e_text );
3801
+ char_length , e_text );
3794
3802
3795
3803
/*
3796
3804
* Add in matching qualified names, but only if there is exactly
@@ -3808,7 +3816,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3808
3816
completion_squery -> selcondition );
3809
3817
appendPQExpBuffer (& query_buffer , "substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'" ,
3810
3818
qualresult ,
3811
- string_length , e_text );
3819
+ char_length , e_text );
3812
3820
3813
3821
/*
3814
3822
* This condition exploits the single-matching-schema rule to
@@ -3817,13 +3825,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3817
3825
appendPQExpBuffer (& query_buffer ,
3818
3826
" AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) ="
3819
3827
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)" ,
3820
- string_length , e_text );
3828
+ char_length , e_text );
3821
3829
appendPQExpBuffer (& query_buffer ,
3822
3830
" AND (SELECT pg_catalog.count(*)"
3823
3831
" FROM pg_catalog.pg_namespace"
3824
3832
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
3825
3833
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1" ,
3826
- string_length , e_text );
3834
+ char_length , e_text );
3827
3835
3828
3836
/* If an addon query was provided, use it */
3829
3837
if (completion_charp )
@@ -3833,7 +3841,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3833
3841
{
3834
3842
/* completion_charp is an sprintf-style format string */
3835
3843
appendPQExpBuffer (& query_buffer , completion_charp ,
3836
- string_length , e_text ,
3844
+ char_length , e_text ,
3837
3845
e_info_charp , e_info_charp ,
3838
3846
e_info_charp2 , e_info_charp2 );
3839
3847
}
@@ -3859,7 +3867,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3859
3867
3860
3868
while (list_index < PQntuples (result ) &&
3861
3869
(item = PQgetvalue (result , list_index ++ , 0 )))
3862
- if (pg_strncasecmp (text , item , string_length ) == 0 )
3870
+ if (pg_strncasecmp (text , item , byte_length ) == 0 )
3863
3871
return pg_strdup (item );
3864
3872
}
3865
3873
0 commit comments