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

Commit69f4f1c

Browse files
committed
psql: Case preserving completion of SQL key words
Instead of always completing SQL key words in upper case, look at theword being completed and match the case.reviewed by Fujii Masao
1 parent500cf66 commit69f4f1c

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

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

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ static const char *const * completion_charpp;/* to pass a list of strings */
132132
staticconstchar*completion_info_charp;/* to pass a second string */
133133
staticconstchar*completion_info_charp2;/* to pass a third string */
134134
staticconstSchemaQuery*completion_squery;/* to pass a SchemaQuery */
135+
staticboolcompletion_case_sensitive;/* completion is case sensitive */
135136

136137
/*
137138
* A few macros to ease typing. You can use these to complete the given
@@ -155,15 +156,24 @@ do { \
155156
matches = completion_matches(text, complete_from_schema_query); \
156157
} while (0)
157158

159+
#defineCOMPLETE_WITH_LIST_CS(list) \
160+
do { \
161+
completion_charpp = list; \
162+
completion_case_sensitive = true; \
163+
matches = completion_matches(text, complete_from_list); \
164+
} while (0)
165+
158166
#defineCOMPLETE_WITH_LIST(list) \
159167
do { \
160168
completion_charpp = list; \
169+
completion_case_sensitive = false; \
161170
matches = completion_matches(text, complete_from_list); \
162171
} while (0)
163172

164173
#defineCOMPLETE_WITH_CONST(string) \
165174
do { \
166175
completion_charp = string; \
176+
completion_case_sensitive = false; \
167177
matches = completion_matches(text, complete_from_const); \
168178
} while (0)
169179

@@ -671,6 +681,7 @@ static char *complete_from_const(const char *text, int state);
671681
staticchar**complete_from_variables(char*text,
672682
constchar*prefix,constchar*suffix);
673683

684+
staticchar*pg_strdup_same_case(constchar*s,constchar*ref);
674685
staticPGresult*exec_query(constchar*query);
675686

676687
staticvoidget_previous_words(intpoint,char**previous_words,intnwords);
@@ -771,7 +782,7 @@ psql_completion(char *text, int start, int end)
771782

772783
/* If a backslash command was started, continue */
773784
if (text[0]=='\\')
774-
COMPLETE_WITH_LIST(backslash_commands);
785+
COMPLETE_WITH_LIST_CS(backslash_commands);
775786

776787
/* Variable interpolation */
777788
elseif (text[0]==':'&&text[1]!=':')
@@ -2907,7 +2918,7 @@ psql_completion(char *text, int start, int end)
29072918
"null","fieldsep","tuples_only","title","tableattr",
29082919
"linestyle","pager","recordsep",NULL};
29092920

2910-
COMPLETE_WITH_LIST(my_list);
2921+
COMPLETE_WITH_LIST_CS(my_list);
29112922
}
29122923
elseif (strcmp(prev2_wd,"\\pset")==0)
29132924
{
@@ -2917,14 +2928,14 @@ psql_completion(char *text, int start, int end)
29172928
{"unaligned","aligned","wrapped","html","latex",
29182929
"troff-ms",NULL};
29192930

2920-
COMPLETE_WITH_LIST(my_list);
2931+
COMPLETE_WITH_LIST_CS(my_list);
29212932
}
29222933
elseif (strcmp(prev_wd,"linestyle")==0)
29232934
{
29242935
staticconstchar*constmy_list[]=
29252936
{"ascii","old-ascii","unicode",NULL};
29262937

2927-
COMPLETE_WITH_LIST(my_list);
2938+
COMPLETE_WITH_LIST_CS(my_list);
29282939
}
29292940
}
29302941
elseif (strcmp(prev_wd,"\\set")==0)
@@ -3030,7 +3041,7 @@ create_or_drop_command_generator(const char *text, int state, bits32 excluded)
30303041
{
30313042
if ((pg_strncasecmp(name,text,string_length)==0)&&
30323043
!(words_after_create[list_index-1].flags&excluded))
3033-
returnpg_strdup(name);
3044+
returnpg_strdup_same_case(name,text);
30343045
}
30353046
/* if nothing matches, return NULL */
30363047
returnNULL;
@@ -3298,7 +3309,7 @@ complete_from_list(const char *text, int state)
32983309
{
32993310
list_index=0;
33003311
string_length=strlen(text);
3301-
casesensitive=true;
3312+
casesensitive=completion_case_sensitive;
33023313
matches=0;
33033314
}
33043315

@@ -3313,7 +3324,14 @@ complete_from_list(const char *text, int state)
33133324

33143325
/* Second pass is case insensitive, don't bother counting matches */
33153326
if (!casesensitive&&pg_strncasecmp(text,item,string_length)==0)
3316-
returnpg_strdup(item);
3327+
{
3328+
if (completion_case_sensitive)
3329+
returnpg_strdup(item);
3330+
else
3331+
/* If case insensitive matching was requested initially, return
3332+
* it in the case of what was already entered. */
3333+
returnpg_strdup_same_case(item,text);
3334+
}
33173335
}
33183336

33193337
/*
@@ -3343,12 +3361,16 @@ complete_from_list(const char *text, int state)
33433361
staticchar*
33443362
complete_from_const(constchar*text,intstate)
33453363
{
3346-
(void)text;/* We don't care about what was entered
3347-
* already. */
3348-
33493364
psql_assert(completion_charp);
33503365
if (state==0)
3351-
returnpg_strdup(completion_charp);
3366+
{
3367+
if (completion_case_sensitive)
3368+
returnpg_strdup(completion_charp);
3369+
else
3370+
/* If case insensitive matching was requested initially, return it
3371+
* in the case of what was already entered. */
3372+
returnpg_strdup_same_case(completion_charp,text);
3373+
}
33523374
else
33533375
returnNULL;
33543376
}
@@ -3394,7 +3416,7 @@ complete_from_variables(char *text, const char *prefix, const char *suffix)
33943416
}
33953417

33963418
varnames[nvars]=NULL;
3397-
COMPLETE_WITH_LIST((constchar*const*)varnames);
3419+
COMPLETE_WITH_LIST_CS((constchar*const*)varnames);
33983420

33993421
for (i=0;i<nvars;i++)
34003422
free(varnames[i]);
@@ -3407,6 +3429,31 @@ complete_from_variables(char *text, const char *prefix, const char *suffix)
34073429
/* HELPER FUNCTIONS */
34083430

34093431

3432+
/*
3433+
* Make a pg_strdup copy of s and convert it to the same case as ref.
3434+
*/
3435+
staticchar*
3436+
pg_strdup_same_case(constchar*s,constchar*ref)
3437+
{
3438+
char*ret,*p;
3439+
unsignedcharfirst=ref[0];
3440+
3441+
if (isalpha(first))
3442+
{
3443+
ret=pg_strdup(s);
3444+
if (islower(first))
3445+
for (p=ret;*p;p++)
3446+
*p=pg_tolower((unsignedchar)*p);
3447+
else
3448+
for (p=ret;*p;p++)
3449+
*p=pg_toupper((unsignedchar)*p);
3450+
returnret;
3451+
}
3452+
else
3453+
returnpg_strdup(s);
3454+
}
3455+
3456+
34103457
/*
34113458
* Execute a query and report any errors. This should be the preferred way of
34123459
* talking to the database in this file.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp