@@ -3522,9 +3522,8 @@ static char *
3522
3522
_complete_from_query (int is_schema_query ,const char * text ,int state )
3523
3523
{
3524
3524
static int list_index ,
3525
- string_length ;
3525
+ byte_length ;
3526
3526
static PGresult * result = NULL ;
3527
-
3528
3527
/*
3529
3528
* If this is the first time for this completion, we fetch a list of our
3530
3529
* "things" from the backend.
@@ -3535,9 +3534,18 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3535
3534
char * e_text ;
3536
3535
char * e_info_charp ;
3537
3536
char * e_info_charp2 ;
3537
+ const char * pstr = text ;
3538
+ int char_length = 0 ;
3538
3539
3539
3540
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
+ }
3541
3549
3542
3550
/* Free any prior result */
3543
3551
PQclear (result );
@@ -3590,7 +3598,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3590
3598
completion_squery -> selcondition );
3591
3599
appendPQExpBuffer (& query_buffer ,"substring(%s,1,%d)='%s'" ,
3592
3600
completion_squery -> result ,
3593
- string_length ,e_text );
3601
+ char_length ,e_text );
3594
3602
appendPQExpBuffer (& query_buffer ," AND %s" ,
3595
3603
completion_squery -> viscondition );
3596
3604
@@ -3617,13 +3625,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3617
3625
"SELECT pg_catalog.quote_ident(n.nspname) || '.' "
3618
3626
"FROM pg_catalog.pg_namespace n "
3619
3627
"WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'" ,
3620
- string_length ,e_text );
3628
+ char_length ,e_text );
3621
3629
appendPQExpBuffer (& query_buffer ,
3622
3630
" AND (SELECT pg_catalog.count(*)"
3623
3631
" FROM pg_catalog.pg_namespace"
3624
3632
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
3625
3633
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1" ,
3626
- string_length ,e_text );
3634
+ char_length ,e_text );
3627
3635
3628
3636
/*
3629
3637
* 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)
3641
3649
completion_squery -> selcondition );
3642
3650
appendPQExpBuffer (& query_buffer ,"substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'" ,
3643
3651
qualresult ,
3644
- string_length ,e_text );
3652
+ char_length ,e_text );
3645
3653
3646
3654
/*
3647
3655
* 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)
3650
3658
appendPQExpBuffer (& query_buffer ,
3651
3659
" AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) ="
3652
3660
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)" ,
3653
- string_length ,e_text );
3661
+ char_length ,e_text );
3654
3662
appendPQExpBuffer (& query_buffer ,
3655
3663
" AND (SELECT pg_catalog.count(*)"
3656
3664
" FROM pg_catalog.pg_namespace"
3657
3665
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
3658
3666
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1" ,
3659
- string_length ,e_text );
3667
+ char_length ,e_text );
3660
3668
3661
3669
/* If an addon query was provided, use it */
3662
3670
if (completion_charp )
@@ -3666,7 +3674,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3666
3674
{
3667
3675
/* completion_charp is an sprintf-style format string */
3668
3676
appendPQExpBuffer (& query_buffer ,completion_charp ,
3669
- string_length ,e_text ,
3677
+ char_length ,e_text ,
3670
3678
e_info_charp ,e_info_charp ,
3671
3679
e_info_charp2 ,e_info_charp2 );
3672
3680
}
@@ -3692,7 +3700,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3692
3700
3693
3701
while (list_index < PQntuples (result )&&
3694
3702
(item = PQgetvalue (result ,list_index ++ ,0 )))
3695
- if (pg_strncasecmp (text ,item ,string_length )== 0 )
3703
+ if (pg_strncasecmp (text ,item ,byte_length )== 0 )
3696
3704
return pg_strdup (item );
3697
3705
}
3698
3706