@@ -4126,9 +4126,8 @@ static char *
4126
4126
_complete_from_query (int is_schema_query ,const char * text ,int state )
4127
4127
{
4128
4128
static int list_index ,
4129
- string_length ;
4129
+ byte_length ;
4130
4130
static PGresult * result = NULL ;
4131
-
4132
4131
/*
4133
4132
* If this is the first time for this completion, we fetch a list of our
4134
4133
* "things" from the backend.
@@ -4139,9 +4138,18 @@ _complete_from_query(int is_schema_query, const char *text, int state)
4139
4138
char * e_text ;
4140
4139
char * e_info_charp ;
4141
4140
char * e_info_charp2 ;
4141
+ const char * pstr = text ;
4142
+ int char_length = 0 ;
4142
4143
4143
4144
list_index = 0 ;
4144
- string_length = strlen (text );
4145
+ byte_length = strlen (text );
4146
+
4147
+ /* Count length as number of characters (not bytes), for passing to substring */
4148
+ while (* pstr )
4149
+ {
4150
+ char_length ++ ;
4151
+ pstr += PQmblen (pstr ,pset .encoding );
4152
+ }
4145
4153
4146
4154
/* Free any prior result */
4147
4155
PQclear (result );
@@ -4194,7 +4202,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
4194
4202
completion_squery -> selcondition );
4195
4203
appendPQExpBuffer (& query_buffer ,"substring(%s,1,%d)='%s'" ,
4196
4204
completion_squery -> result ,
4197
- string_length ,e_text );
4205
+ char_length ,e_text );
4198
4206
appendPQExpBuffer (& query_buffer ," AND %s" ,
4199
4207
completion_squery -> viscondition );
4200
4208
@@ -4221,13 +4229,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
4221
4229
"SELECT pg_catalog.quote_ident(n.nspname) || '.' "
4222
4230
"FROM pg_catalog.pg_namespace n "
4223
4231
"WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'" ,
4224
- string_length ,e_text );
4232
+ char_length ,e_text );
4225
4233
appendPQExpBuffer (& query_buffer ,
4226
4234
" AND (SELECT pg_catalog.count(*)"
4227
4235
" FROM pg_catalog.pg_namespace"
4228
4236
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
4229
4237
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1" ,
4230
- string_length ,e_text );
4238
+ char_length ,e_text );
4231
4239
4232
4240
/*
4233
4241
* Add in matching qualified names, but only if there is exactly
@@ -4245,7 +4253,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
4245
4253
completion_squery -> selcondition );
4246
4254
appendPQExpBuffer (& query_buffer ,"substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'" ,
4247
4255
qualresult ,
4248
- string_length ,e_text );
4256
+ char_length ,e_text );
4249
4257
4250
4258
/*
4251
4259
* This condition exploits the single-matching-schema rule to
@@ -4254,13 +4262,13 @@ _complete_from_query(int is_schema_query, const char *text, int state)
4254
4262
appendPQExpBuffer (& query_buffer ,
4255
4263
" AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) ="
4256
4264
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)" ,
4257
- string_length ,e_text );
4265
+ char_length ,e_text );
4258
4266
appendPQExpBuffer (& query_buffer ,
4259
4267
" AND (SELECT pg_catalog.count(*)"
4260
4268
" FROM pg_catalog.pg_namespace"
4261
4269
" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
4262
4270
" substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1" ,
4263
- string_length ,e_text );
4271
+ char_length ,e_text );
4264
4272
4265
4273
/* If an addon query was provided, use it */
4266
4274
if (completion_charp )
@@ -4270,7 +4278,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
4270
4278
{
4271
4279
/* completion_charp is an sprintf-style format string */
4272
4280
appendPQExpBuffer (& query_buffer ,completion_charp ,
4273
- string_length ,e_text ,
4281
+ char_length ,e_text ,
4274
4282
e_info_charp ,e_info_charp ,
4275
4283
e_info_charp2 ,e_info_charp2 );
4276
4284
}
@@ -4296,7 +4304,7 @@ _complete_from_query(int is_schema_query, const char *text, int state)
4296
4304
4297
4305
while (list_index < PQntuples (result )&&
4298
4306
(item = PQgetvalue (result ,list_index ++ ,0 )))
4299
- if (pg_strncasecmp (text ,item ,string_length )== 0 )
4307
+ if (pg_strncasecmp (text ,item ,byte_length )== 0 )
4300
4308
return pg_strdup (item );
4301
4309
}
4302
4310