⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tab-complete.c

📁 PostgreSQL7.4.6 for Linux
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * psql - the PostgreSQL interactive terminal * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.91.2.1 2003/11/08 20:54:37 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>#ifdef USE_ASSERT_CHECKING#include <assert.h>#endif#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_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_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_users \" SELECT pg_catalog.quote_ident(usename) "\"   FROM pg_catalog.pg_user "\"  WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'"/* the silly-looking length condition is just to eat up the current word */#define Query_for_table_owning_index \"SELECT pg_catalog.quote_ident(c1.relname) "\"  FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\"       and (%d = length('%s'))"\"       and pg_catalog.quote_ident(c2.relname)='%s'"\"       and pg_catalog.pg_table_is_visible(c2.oid)"/* * This is a list of all "things" in Pgsql, which can show up after CREATE or * DROP; and there is also a query to get a list of them. */typedef struct{	const char *name;	const char *query;			/* simple query, or NULL */	const SchemaQuery *squery;	/* schema query, or NULL */} pgsql_thing_t;static const pgsql_thing_t words_after_create[] = {	{"AGGREGATE", NULL, &Query_for_list_of_aggregates},	{"CAST", NULL, NULL},		/* Casts have complex structures for								 * names, so skip it */	{"CONVERSION", "SELECT pg_catalog.quote_ident(conname) FROM pg_catalog.pg_conversion WHERE substring(pg_catalog.quote_ident(conname),1,%d)='%s'"},	{"DATABASE", Query_for_list_of_databases},	{"DOMAIN", NULL, &Query_for_list_of_domains},	{"FUNCTION", NULL, &Query_for_list_of_functions},	{"GROUP", "SELECT pg_catalog.quote_ident(groname) FROM pg_catalog.pg_group WHERE substring(pg_catalog.quote_ident(groname),1,%d)='%s'"},	{"LANGUAGE", Query_for_list_of_languages},	{"INDEX", NULL, &Query_for_list_of_indexes},	{"OPERATOR", NULL, NULL},	/* Querying for this is probably								 * not such a good idea. */	{"RULE", "SELECT pg_catalog.quote_ident(rulename) FROM pg_catalog.pg_rules WHERE substring(pg_catalog.quote_ident(rulename),1,%d)='%s'"},	{"SCHEMA", Query_for_list_of_schemas},	{"SEQUENCE", NULL, &Query_for_list_of_sequences},	{"TABLE", NULL, &Query_for_list_of_tables},	{"TEMP", NULL, NULL},	/* for CREATE TEMP TABLE ... */	{"TRIGGER", "SELECT pg_catalog.quote_ident(tgname) FROM pg_catalog.pg_trigger WHERE substring(pg_catalog.quote_ident(tgname),1,%d)='%s'"},	{"TYPE", NULL, &Query_for_list_of_datatypes},	{"UNIQUE", NULL, NULL},	/* for CREATE UNIQUE INDEX ... */	{"USER", Query_for_list_of_users},	{"VIEW", NULL, &Query_for_list_of_views},	{NULL, NULL, NULL}			/* end of list */};/* Forward declaration of functions */static char **psql_completion(char *text, int start, int end);static char *create_command_generator(const char *text, int state);static char *complete_from_query(const char *text, int state);static char *complete_from_schema_query(const char *text, int state);static char *_complete_from_query(int is_schema_query,					 const char *text, int state);static char *complete_from_const(const char *text, int state);static char *complete_from_list(const char *text, int state);static PGresult *exec_query(const char *query);static char *previous_word(int point, int skip);#if 0static char *quote_file_name(char *text, int match_type, char *quote_pointer);static char *dequote_file_name(char *text, char quote_char);#endif/* Initialize the readline library for our purposes. */voidinitialize_readline(void){	rl_readline_name = pset.progname;	rl_attempted_completion_function = (void *) psql_completion;	rl_basic_word_break_characters = "\t\n@$><=;|&{( ";	completion_max_records = 1000;	/*	 * There is a variable rl_completion_query_items for this but	 * apparently it's not defined everywhere.	 */}/* The completion function. Acc. to readline spec this gets passed the text   entered to far and its start and end in the readline buffer. The return value   is some partially obscure list format that can be generated by the readline   libraries completion_matches() function, so we don't have to worry about it.*/static char **psql_completion(char *text, int start, int end){	/* This is the variable we'll return. */	char	  **matches = NULL;	/* These are going to contain some scannage of the input line. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -