@@ -3198,9 +3198,8 @@ static char *
3198
3198
_complete_from_query (int is_schema_query , const char * text , int state )
3199
3199
{
3200
3200
static int list_index ,
3201
- string_length ;
3201
+ byte_length ;
3202
3202
static PGresult * result = NULL ;
3203
-
3204
3203
/*
3205
3204
* If this is the first time for this completion, we fetch a list of our
3206
3205
* "things" from the backend.
@@ -3211,9 +3210,18 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3211
3210
char * e_text ;
3212
3211
char * e_info_charp ;
3213
3212
char * e_info_charp2 ;
3213
+ const char * pstr = text ;
3214
+ int char_length = 0 ;
3214
3215
3215
3216
list_index = 0 ;
3216
- string_length = strlen (text );
3217
+ byte_length = strlen (text );
3218
+
3219
+ /* Count length as number of characters (not bytes), for passing to substring */
3220
+ while (* pstr )
3221
+ {
3222
+ char_length ++ ;
3223
+ pstr += PQmblen (pstr , pset .encoding );
3224
+ }
3217
3225
3218
3226
/* Free any prior result */
3219
3227
PQclear (result );
@@ -3251,7 +3259,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3251
3259
completion_squery -> selcondition );
3252
3260
appendPQExpBuffer (& query_buffer , "substring(%s,1,%d)='%s'" ,
3253
3261
completion_squery -> result ,
3254
- string_length , e_text );
3262
+ char_length , e_text );
3255
3263
appendPQExpBuffer (& query_buffer , " AND %s" ,
3256
3264
completion_squery -> viscondition );
3257
3265
@@ -3278,13 +3286,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3278
3286
"SELECT pg_catalog.quote_ident(n.nspname) || '.' "
3279
3287
"FROM pg_catalog.pg_namespace n "
3280
3288
"WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'" ,
3281
- string_length , e_text );
3289
+ char_length , e_text );
3282
3290
appendPQExpBuffer (& query_buffer ,
3283
3291
" AND (SELECT pg_catalog.count(*)"
3284
3292
" FROM pg_catalog.pg_namespace"
3285
3293
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
3286
3294
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1" ,
3287
- string_length , e_text );
3295
+ char_length , e_text );
3288
3296
3289
3297
/*
3290
3298
* Add in matching qualified names, but only if there is exactly
@@ -3302,7 +3310,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3302
3310
completion_squery -> selcondition );
3303
3311
appendPQExpBuffer (& query_buffer , "substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'" ,
3304
3312
qualresult ,
3305
- string_length , e_text );
3313
+ char_length , e_text );
3306
3314
3307
3315
/*
3308
3316
* This condition exploits the single-matching-schema rule to
@@ -3311,13 +3319,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3311
3319
appendPQExpBuffer (& query_buffer ,
3312
3320
" AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) ="
3313
3321
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)" ,
3314
- string_length , e_text );
3322
+ char_length , e_text );
3315
3323
appendPQExpBuffer (& query_buffer ,
3316
3324
" AND (SELECT pg_catalog.count(*)"
3317
3325
" FROM pg_catalog.pg_namespace"
3318
3326
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
3319
3327
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1" ,
3320
- string_length , e_text );
3328
+ char_length , e_text );
3321
3329
3322
3330
/* If an addon query was provided, use it */
3323
3331
if (completion_charp )
@@ -3327,7 +3335,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3327
3335
{
3328
3336
/* completion_charp is an sprintf-style format string */
3329
3337
appendPQExpBuffer (& query_buffer , completion_charp ,
3330
- string_length , e_text ,
3338
+ char_length , e_text ,
3331
3339
e_info_charp , e_info_charp ,
3332
3340
e_info_charp2 , e_info_charp2 );
3333
3341
}
@@ -3353,7 +3361,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
3353
3361
3354
3362
while (list_index < PQntuples (result ) &&
3355
3363
(item = PQgetvalue (result , list_index ++ , 0 )))
3356
- if (pg_strncasecmp (text , item , string_length ) == 0 )
3364
+ if (pg_strncasecmp (text , item , byte_length ) == 0 )
3357
3365
return pg_strdup (item );
3358
3366
}
3359
3367
0 commit comments