📄 tab-complete.c
字号:
/* * psql - the PostgreSQL interactive terminal * * Copyright (c) 2000-2005, PostgreSQL Global Development Group * * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.138.2.1 2005/11/14 17:46:07 momjian Exp $ *//*---------------------------------------------------------------------- * This file implements a somewhat more sophisticated readline "TAB * completion" in psql. It is not intended to be AI, to replace * learning SQL, or to relieve you from thinking about what you're * doing. Also it does not always give you all the syntactically legal * completions, only those that are the most common or the ones that * the programmer felt most like implementing. * * CAVEAT: Tab completion causes queries to be sent to the backend. * The number of tuples returned gets limited, in most default * installations to 1000, but if you still don't like this prospect, * you can turn off tab completion in your ~/.inputrc (or else * ${INPUTRC}) file so: * * $if psql * set disable-completion on * $endif * * See `man 3 readline' or `info readline' for the full details. Also, * hence the * * BUGS: * * - If you split your queries across lines, this whole thing gets * confused. (To fix this, one would have to read psql's query * buffer rather than readline's line buffer, which would require * some major revisions of things.) * * - Table or attribute names with spaces in it may confuse it. * * - Quotes, parenthesis, and other funny characters are not handled * all that gracefully. *---------------------------------------------------------------------- */#include "postgres_fe.h"#include "tab-complete.h"#include "input.h"/* If we don't have this, we might as well forget about the whole thing: */#ifdef USE_READLINE#include <ctype.h>#include "libpq-fe.h"#include "pqexpbuffer.h"#include "common.h"#include "settings.h"#ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION#define filename_completion_function rl_filename_completion_function#else/* missing in some header files */extern char *filename_completion_function();#endif#ifdef HAVE_RL_COMPLETION_MATCHES#define completion_matches rl_completion_matches#endif/* * This struct is used to define "schema queries", which are custom-built * to obtain possibly-schema-qualified names of database objects. There is * enough similarity in the structure that we don't want to repeat it each * time. So we put the components of each query into this struct and * assemble them with the common boilerplate in _complete_from_query(). */typedef struct SchemaQuery{ /* * Name of catalog or catalogs to be queried, with alias, eg. * "pg_catalog.pg_class c". Note that "pg_namespace n" will be added. */ const char *catname; /* * Selection condition --- only rows meeting this condition are candidates * to display. If catname mentions multiple tables, include the necessary * join condition here. For example, "c.relkind = 'r'". Write NULL (not * an empty string) if not needed. */ const char *selcondition; /* * Visibility condition --- which rows are visible without schema * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)". */ const char *viscondition; /* * Namespace --- name of field to join to pg_namespace.oid. For example, * "c.relnamespace". */ const char *namespace; /* * Result --- the appropriately-quoted name to return, in the case of an * unqualified name. For example, "pg_catalog.quote_ident(c.relname)". */ const char *result; /* * In some cases a different result must be used for qualified names. * Enter that here, or write NULL if result can be used. */ const char *qualresult;} SchemaQuery;/* Store maximum number of records we want from database queries * (implemented via SELECT ... LIMIT xx). */static int completion_max_records;/* * Communication variables set by COMPLETE_WITH_FOO macros and then used by * the completion callback functions. Ugly but there is no better way. */static const char *completion_charp; /* to pass a string */static const char *const * completion_charpp; /* to pass a list of strings */static const char *completion_info_charp; /* to pass a second string */static const SchemaQuery *completion_squery; /* to pass a SchemaQuery *//* A couple of macros to ease typing. You can use these to complete the given string with 1) The results from a query you pass it. (Perhaps one of those below?) 2) The results from a schema query you pass it. 3) The items from a null-pointer-terminated list. 4) A string constant 5) The list of attributes to the given table.*/#define COMPLETE_WITH_QUERY(query) \do { completion_charp = query; matches = completion_matches(text, complete_from_query); } while(0)#define COMPLETE_WITH_SCHEMA_QUERY(query,addon) \do { completion_squery = &(query); completion_charp = addon; matches = completion_matches(text, complete_from_schema_query); } while(0)#define COMPLETE_WITH_LIST(list) \do { completion_charpp = list; matches = completion_matches(text, complete_from_list); } while(0)#define COMPLETE_WITH_CONST(string) \do { completion_charp = string; matches = completion_matches(text, complete_from_const); } while(0)#define COMPLETE_WITH_ATTR(table) \do {completion_charp = Query_for_list_of_attributes; completion_info_charp = table; matches = completion_matches(text, complete_from_query); } while(0)/* * Assembly instructions for schema queries */static const SchemaQuery Query_for_list_of_aggregates = { /* catname */ "pg_catalog.pg_proc p", /* selcondition */ "p.proisagg", /* viscondition */ "pg_catalog.pg_function_is_visible(p.oid)", /* namespace */ "p.pronamespace", /* result */ "pg_catalog.quote_ident(p.proname)", /* qualresult */ NULL};static const SchemaQuery Query_for_list_of_datatypes = { /* catname */ "pg_catalog.pg_type t", /* selcondition --- ignore table rowtypes and array types */ "(t.typrelid = 0 " " OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) " "AND t.typname !~ '^_'", /* viscondition */ "pg_catalog.pg_type_is_visible(t.oid)", /* namespace */ "t.typnamespace", /* result */ "pg_catalog.format_type(t.oid, NULL)", /* qualresult */ "pg_catalog.quote_ident(t.typname)"};static const SchemaQuery Query_for_list_of_domains = { /* catname */ "pg_catalog.pg_type t", /* selcondition */ "t.typtype = 'd'", /* viscondition */ "pg_catalog.pg_type_is_visible(t.oid)", /* namespace */ "t.typnamespace", /* result */ "pg_catalog.quote_ident(t.typname)", /* qualresult */ NULL};static const SchemaQuery Query_for_list_of_functions = { /* catname */ "pg_catalog.pg_proc p", /* selcondition */ NULL, /* viscondition */ "pg_catalog.pg_function_is_visible(p.oid)", /* namespace */ "p.pronamespace", /* result */ "pg_catalog.quote_ident(p.proname)", /* qualresult */ NULL};static const SchemaQuery Query_for_list_of_indexes = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ "c.relkind IN ('i')", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ "c.relnamespace", /* result */ "pg_catalog.quote_ident(c.relname)", /* qualresult */ NULL};static const SchemaQuery Query_for_list_of_sequences = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ "c.relkind IN ('S')", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ "c.relnamespace", /* result */ "pg_catalog.quote_ident(c.relname)", /* qualresult */ NULL};static const SchemaQuery Query_for_list_of_tables = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ "c.relkind IN ('r')", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ "c.relnamespace", /* result */ "pg_catalog.quote_ident(c.relname)", /* qualresult */ NULL};static const SchemaQuery Query_for_list_of_tisv = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ "c.relkind IN ('r', 'i', 'S', 'v')", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ "c.relnamespace", /* result */ "pg_catalog.quote_ident(c.relname)", /* qualresult */ NULL};static const SchemaQuery Query_for_list_of_tsv = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ "c.relkind IN ('r', 'S', 'v')", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ "c.relnamespace", /* result */ "pg_catalog.quote_ident(c.relname)", /* qualresult */ NULL};static const SchemaQuery Query_for_list_of_views = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ "c.relkind IN ('v')", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ "c.relnamespace", /* result */ "pg_catalog.quote_ident(c.relname)", /* qualresult */ NULL};/* * Queries to get lists of names of various kinds of things, possibly * restricted to names matching a partially entered name. In these queries, * %s will be replaced by the text entered so far (suitably escaped to * become a SQL literal string). %d will be replaced by the length of the * string (in unescaped form). A second %s, if present, will be replaced * by a suitably-escaped version of the string provided in * completion_info_charp. * * Beware that the allowed sequences of %s and %d are determined by * _complete_from_query(). */#define Query_for_list_of_attributes \"SELECT pg_catalog.quote_ident(attname) "\" FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c "\" WHERE c.oid = a.attrelid "\" AND a.attnum > 0 "\" AND NOT a.attisdropped "\" AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\" AND pg_catalog.quote_ident(relname)='%s' "\" AND pg_catalog.pg_table_is_visible(c.oid)"#define Query_for_list_of_databases \"SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database "\" WHERE substring(pg_catalog.quote_ident(datname),1,%d)='%s'"#define Query_for_list_of_tablespaces \"SELECT pg_catalog.quote_ident(spcname) FROM pg_catalog.pg_tablespace "\" WHERE substring(pg_catalog.quote_ident(spcname),1,%d)='%s'"#define Query_for_list_of_encodings \" SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\" FROM pg_catalog.pg_conversion "\" WHERE substring(pg_catalog.pg_encoding_to_char(conforencoding),1,%d)=UPPER('%s')"#define Query_for_list_of_languages \"SELECT pg_catalog.quote_ident(lanname) "\" FROM pg_language "\" WHERE lanname != 'internal' "\" AND substring(pg_catalog.quote_ident(lanname),1,%d)='%s' "#define Query_for_list_of_schemas \"SELECT pg_catalog.quote_ident(nspname) FROM pg_catalog.pg_namespace "\" WHERE substring(pg_catalog.quote_ident(nspname),1,%d)='%s'"#define Query_for_list_of_set_vars \"SELECT name FROM "\" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\" WHERE context IN ('user', 'superuser') "\" UNION ALL SELECT 'constraints' "\" UNION ALL SELECT 'transaction' "\" UNION ALL SELECT 'session' "\" UNION ALL SELECT 'role' "\" UNION ALL SELECT 'all') ss "\" WHERE substring(name,1,%d)='%s'"#define Query_for_list_of_show_vars \"SELECT name FROM "\" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\" UNION ALL SELECT 'session authorization' "\" UNION ALL SELECT 'all') ss "\" WHERE substring(name,1,%d)='%s'"#define Query_for_list_of_system_relations \"SELECT pg_catalog.quote_ident(relname) "\" FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "\" WHERE c.relkind IN ('r', 'v', 's', 'S') "\" AND substring(pg_catalog.quote_ident(relname),1,%d)='%s' "\" AND c.relnamespace = n.oid "\" AND n.nspname = 'pg_catalog'"#define Query_for_list_of_roles \" SELECT pg_catalog.quote_ident(rolname) "\" FROM pg_catalog.pg_roles "\" WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"#define Query_for_list_of_grant_roles \" SELECT pg_catalog.quote_ident(rolname) "\" FROM pg_catalog.pg_roles "\" WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"\
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -