PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/larkly/postgres-docker
C | 4352 lines | 3625 code | 266 blank | 461 comment | 1092 complexity | bd95d0eaeff396c9dcede159ecb1fe98 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. * psql - the PostgreSQL interactive terminal
  3. *
  4. * Copyright (c) 2000-2014, PostgreSQL Global Development Group
  5. *
  6. * src/bin/psql/tab-complete.c
  7. */
  8. /*----------------------------------------------------------------------
  9. * This file implements a somewhat more sophisticated readline "TAB
  10. * completion" in psql. It is not intended to be AI, to replace
  11. * learning SQL, or to relieve you from thinking about what you're
  12. * doing. Also it does not always give you all the syntactically legal
  13. * completions, only those that are the most common or the ones that
  14. * the programmer felt most like implementing.
  15. *
  16. * CAVEAT: Tab completion causes queries to be sent to the backend.
  17. * The number of tuples returned gets limited, in most default
  18. * installations to 1000, but if you still don't like this prospect,
  19. * you can turn off tab completion in your ~/.inputrc (or else
  20. * ${INPUTRC}) file so:
  21. *
  22. * $if psql
  23. * set disable-completion on
  24. * $endif
  25. *
  26. * See `man 3 readline' or `info readline' for the full details. Also,
  27. * hence the
  28. *
  29. * BUGS:
  30. *
  31. * - If you split your queries across lines, this whole thing gets
  32. * confused. (To fix this, one would have to read psql's query
  33. * buffer rather than readline's line buffer, which would require
  34. * some major revisions of things.)
  35. *
  36. * - Table or attribute names with spaces in it may confuse it.
  37. *
  38. * - Quotes, parenthesis, and other funny characters are not handled
  39. * all that gracefully.
  40. *----------------------------------------------------------------------
  41. */
  42. #include "postgres_fe.h"
  43. #include "tab-complete.h"
  44. #include "input.h"
  45. /* If we don't have this, we might as well forget about the whole thing: */
  46. #ifdef USE_READLINE
  47. #include <ctype.h>
  48. #include "libpq-fe.h"
  49. #include "pqexpbuffer.h"
  50. #include "common.h"
  51. #include "settings.h"
  52. #include "stringutils.h"
  53. #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
  54. #define filename_completion_function rl_filename_completion_function
  55. #else
  56. /* missing in some header files */
  57. extern char *filename_completion_function();
  58. #endif
  59. #ifdef HAVE_RL_COMPLETION_MATCHES
  60. #define completion_matches rl_completion_matches
  61. #endif
  62. /* word break characters */
  63. #define WORD_BREAKS "\t\n@$><=;|&{() "
  64. /*
  65. * This struct is used to define "schema queries", which are custom-built
  66. * to obtain possibly-schema-qualified names of database objects. There is
  67. * enough similarity in the structure that we don't want to repeat it each
  68. * time. So we put the components of each query into this struct and
  69. * assemble them with the common boilerplate in _complete_from_query().
  70. */
  71. typedef struct SchemaQuery
  72. {
  73. /*
  74. * Name of catalog or catalogs to be queried, with alias, eg.
  75. * "pg_catalog.pg_class c". Note that "pg_namespace n" will be added.
  76. */
  77. const char *catname;
  78. /*
  79. * Selection condition --- only rows meeting this condition are candidates
  80. * to display. If catname mentions multiple tables, include the necessary
  81. * join condition here. For example, "c.relkind = 'r'". Write NULL (not
  82. * an empty string) if not needed.
  83. */
  84. const char *selcondition;
  85. /*
  86. * Visibility condition --- which rows are visible without schema
  87. * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)".
  88. */
  89. const char *viscondition;
  90. /*
  91. * Namespace --- name of field to join to pg_namespace.oid. For example,
  92. * "c.relnamespace".
  93. */
  94. const char *namespace;
  95. /*
  96. * Result --- the appropriately-quoted name to return, in the case of an
  97. * unqualified name. For example, "pg_catalog.quote_ident(c.relname)".
  98. */
  99. const char *result;
  100. /*
  101. * In some cases a different result must be used for qualified names.
  102. * Enter that here, or write NULL if result can be used.
  103. */
  104. const char *qualresult;
  105. } SchemaQuery;
  106. /* Store maximum number of records we want from database queries
  107. * (implemented via SELECT ... LIMIT xx).
  108. */
  109. static int completion_max_records;
  110. /*
  111. * Communication variables set by COMPLETE_WITH_FOO macros and then used by
  112. * the completion callback functions. Ugly but there is no better way.
  113. */
  114. static const char *completion_charp; /* to pass a string */
  115. static const char *const * completion_charpp; /* to pass a list of strings */
  116. static const char *completion_info_charp; /* to pass a second string */
  117. static const char *completion_info_charp2; /* to pass a third string */
  118. static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */
  119. static bool completion_case_sensitive; /* completion is case sensitive */
  120. /*
  121. * A few macros to ease typing. You can use these to complete the given
  122. * string with
  123. * 1) The results from a query you pass it. (Perhaps one of those below?)
  124. * 2) The results from a schema query you pass it.
  125. * 3) The items from a null-pointer-terminated list.
  126. * 4) A string constant.
  127. * 5) The list of attributes of the given table (possibly schema-qualified).
  128. * 6/ The list of arguments to the given function (possibly schema-qualified).
  129. */
  130. #define COMPLETE_WITH_QUERY(query) \
  131. do { \
  132. completion_charp = query; \
  133. matches = completion_matches(text, complete_from_query); \
  134. } while (0)
  135. #define COMPLETE_WITH_SCHEMA_QUERY(query, addon) \
  136. do { \
  137. completion_squery = &(query); \
  138. completion_charp = addon; \
  139. matches = completion_matches(text, complete_from_schema_query); \
  140. } while (0)
  141. #define COMPLETE_WITH_LIST_CS(list) \
  142. do { \
  143. completion_charpp = list; \
  144. completion_case_sensitive = true; \
  145. matches = completion_matches(text, complete_from_list); \
  146. } while (0)
  147. #define COMPLETE_WITH_LIST(list) \
  148. do { \
  149. completion_charpp = list; \
  150. completion_case_sensitive = false; \
  151. matches = completion_matches(text, complete_from_list); \
  152. } while (0)
  153. #define COMPLETE_WITH_CONST(string) \
  154. do { \
  155. completion_charp = string; \
  156. completion_case_sensitive = false; \
  157. matches = completion_matches(text, complete_from_const); \
  158. } while (0)
  159. #define COMPLETE_WITH_ATTR(relation, addon) \
  160. do { \
  161. char *_completion_schema; \
  162. char *_completion_table; \
  163. \
  164. _completion_schema = strtokx(relation, " \t\n\r", ".", "\"", 0, \
  165. false, false, pset.encoding); \
  166. (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \
  167. false, false, pset.encoding); \
  168. _completion_table = strtokx(NULL, " \t\n\r", ".", "\"", 0, \
  169. false, false, pset.encoding); \
  170. if (_completion_table == NULL) \
  171. { \
  172. completion_charp = Query_for_list_of_attributes addon; \
  173. completion_info_charp = relation; \
  174. } \
  175. else \
  176. { \
  177. completion_charp = Query_for_list_of_attributes_with_schema addon; \
  178. completion_info_charp = _completion_table; \
  179. completion_info_charp2 = _completion_schema; \
  180. } \
  181. matches = completion_matches(text, complete_from_query); \
  182. } while (0)
  183. #define COMPLETE_WITH_FUNCTION_ARG(function) \
  184. do { \
  185. char *_completion_schema; \
  186. char *_completion_function; \
  187. \
  188. _completion_schema = strtokx(function, " \t\n\r", ".", "\"", 0, \
  189. false, false, pset.encoding); \
  190. (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \
  191. false, false, pset.encoding); \
  192. _completion_function = strtokx(NULL, " \t\n\r", ".", "\"", 0, \
  193. false, false, pset.encoding); \
  194. if (_completion_function == NULL) \
  195. { \
  196. completion_charp = Query_for_list_of_arguments; \
  197. completion_info_charp = function; \
  198. } \
  199. else \
  200. { \
  201. completion_charp = Query_for_list_of_arguments_with_schema; \
  202. completion_info_charp = _completion_function; \
  203. completion_info_charp2 = _completion_schema; \
  204. } \
  205. matches = completion_matches(text, complete_from_query); \
  206. } while (0)
  207. /*
  208. * Assembly instructions for schema queries
  209. */
  210. static const SchemaQuery Query_for_list_of_aggregates = {
  211. /* catname */
  212. "pg_catalog.pg_proc p",
  213. /* selcondition */
  214. "p.proisagg",
  215. /* viscondition */
  216. "pg_catalog.pg_function_is_visible(p.oid)",
  217. /* namespace */
  218. "p.pronamespace",
  219. /* result */
  220. "pg_catalog.quote_ident(p.proname)",
  221. /* qualresult */
  222. NULL
  223. };
  224. static const SchemaQuery Query_for_list_of_datatypes = {
  225. /* catname */
  226. "pg_catalog.pg_type t",
  227. /* selcondition --- ignore table rowtypes and array types */
  228. "(t.typrelid = 0 "
  229. " OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
  230. "AND t.typname !~ '^_'",
  231. /* viscondition */
  232. "pg_catalog.pg_type_is_visible(t.oid)",
  233. /* namespace */
  234. "t.typnamespace",
  235. /* result */
  236. "pg_catalog.format_type(t.oid, NULL)",
  237. /* qualresult */
  238. "pg_catalog.quote_ident(t.typname)"
  239. };
  240. static const SchemaQuery Query_for_list_of_domains = {
  241. /* catname */
  242. "pg_catalog.pg_type t",
  243. /* selcondition */
  244. "t.typtype = 'd'",
  245. /* viscondition */
  246. "pg_catalog.pg_type_is_visible(t.oid)",
  247. /* namespace */
  248. "t.typnamespace",
  249. /* result */
  250. "pg_catalog.quote_ident(t.typname)",
  251. /* qualresult */
  252. NULL
  253. };
  254. static const SchemaQuery Query_for_list_of_functions = {
  255. /* catname */
  256. "pg_catalog.pg_proc p",
  257. /* selcondition */
  258. NULL,
  259. /* viscondition */
  260. "pg_catalog.pg_function_is_visible(p.oid)",
  261. /* namespace */
  262. "p.pronamespace",
  263. /* result */
  264. "pg_catalog.quote_ident(p.proname)",
  265. /* qualresult */
  266. NULL
  267. };
  268. static const SchemaQuery Query_for_list_of_indexes = {
  269. /* catname */
  270. "pg_catalog.pg_class c",
  271. /* selcondition */
  272. "c.relkind IN ('i')",
  273. /* viscondition */
  274. "pg_catalog.pg_table_is_visible(c.oid)",
  275. /* namespace */
  276. "c.relnamespace",
  277. /* result */
  278. "pg_catalog.quote_ident(c.relname)",
  279. /* qualresult */
  280. NULL
  281. };
  282. static const SchemaQuery Query_for_list_of_sequences = {
  283. /* catname */
  284. "pg_catalog.pg_class c",
  285. /* selcondition */
  286. "c.relkind IN ('S')",
  287. /* viscondition */
  288. "pg_catalog.pg_table_is_visible(c.oid)",
  289. /* namespace */
  290. "c.relnamespace",
  291. /* result */
  292. "pg_catalog.quote_ident(c.relname)",
  293. /* qualresult */
  294. NULL
  295. };
  296. static const SchemaQuery Query_for_list_of_foreign_tables = {
  297. /* catname */
  298. "pg_catalog.pg_class c",
  299. /* selcondition */
  300. "c.relkind IN ('f')",
  301. /* viscondition */
  302. "pg_catalog.pg_table_is_visible(c.oid)",
  303. /* namespace */
  304. "c.relnamespace",
  305. /* result */
  306. "pg_catalog.quote_ident(c.relname)",
  307. /* qualresult */
  308. NULL
  309. };
  310. static const SchemaQuery Query_for_list_of_tables = {
  311. /* catname */
  312. "pg_catalog.pg_class c",
  313. /* selcondition */
  314. "c.relkind IN ('r')",
  315. /* viscondition */
  316. "pg_catalog.pg_table_is_visible(c.oid)",
  317. /* namespace */
  318. "c.relnamespace",
  319. /* result */
  320. "pg_catalog.quote_ident(c.relname)",
  321. /* qualresult */
  322. NULL
  323. };
  324. static const SchemaQuery Query_for_list_of_constraints_with_schema = {
  325. /* catname */
  326. "pg_catalog.pg_constraint c",
  327. /* selcondition */
  328. "c.conrelid <> 0",
  329. /* viscondition */
  330. "true", /* there is no pg_constraint_is_visible */
  331. /* namespace */
  332. "c.connamespace",
  333. /* result */
  334. "pg_catalog.quote_ident(c.conname)",
  335. /* qualresult */
  336. NULL
  337. };
  338. /* Relations supporting INSERT, UPDATE or DELETE */
  339. static const SchemaQuery Query_for_list_of_updatables = {
  340. /* catname */
  341. "pg_catalog.pg_class c",
  342. /* selcondition */
  343. "c.relkind IN ('r', 'f', 'v')",
  344. /* viscondition */
  345. "pg_catalog.pg_table_is_visible(c.oid)",
  346. /* namespace */
  347. "c.relnamespace",
  348. /* result */
  349. "pg_catalog.quote_ident(c.relname)",
  350. /* qualresult */
  351. NULL
  352. };
  353. static const SchemaQuery Query_for_list_of_relations = {
  354. /* catname */
  355. "pg_catalog.pg_class c",
  356. /* selcondition */
  357. NULL,
  358. /* viscondition */
  359. "pg_catalog.pg_table_is_visible(c.oid)",
  360. /* namespace */
  361. "c.relnamespace",
  362. /* result */
  363. "pg_catalog.quote_ident(c.relname)",
  364. /* qualresult */
  365. NULL
  366. };
  367. static const SchemaQuery Query_for_list_of_tsvmf = {
  368. /* catname */
  369. "pg_catalog.pg_class c",
  370. /* selcondition */
  371. "c.relkind IN ('r', 'S', 'v', 'm', 'f')",
  372. /* viscondition */
  373. "pg_catalog.pg_table_is_visible(c.oid)",
  374. /* namespace */
  375. "c.relnamespace",
  376. /* result */
  377. "pg_catalog.quote_ident(c.relname)",
  378. /* qualresult */
  379. NULL
  380. };
  381. static const SchemaQuery Query_for_list_of_tmf = {
  382. /* catname */
  383. "pg_catalog.pg_class c",
  384. /* selcondition */
  385. "c.relkind IN ('r', 'm', 'f')",
  386. /* viscondition */
  387. "pg_catalog.pg_table_is_visible(c.oid)",
  388. /* namespace */
  389. "c.relnamespace",
  390. /* result */
  391. "pg_catalog.quote_ident(c.relname)",
  392. /* qualresult */
  393. NULL
  394. };
  395. static const SchemaQuery Query_for_list_of_tm = {
  396. /* catname */
  397. "pg_catalog.pg_class c",
  398. /* selcondition */
  399. "c.relkind IN ('r', 'm')",
  400. /* viscondition */
  401. "pg_catalog.pg_table_is_visible(c.oid)",
  402. /* namespace */
  403. "c.relnamespace",
  404. /* result */
  405. "pg_catalog.quote_ident(c.relname)",
  406. /* qualresult */
  407. NULL
  408. };
  409. static const SchemaQuery Query_for_list_of_views = {
  410. /* catname */
  411. "pg_catalog.pg_class c",
  412. /* selcondition */
  413. "c.relkind IN ('v')",
  414. /* viscondition */
  415. "pg_catalog.pg_table_is_visible(c.oid)",
  416. /* namespace */
  417. "c.relnamespace",
  418. /* result */
  419. "pg_catalog.quote_ident(c.relname)",
  420. /* qualresult */
  421. NULL
  422. };
  423. static const SchemaQuery Query_for_list_of_matviews = {
  424. /* catname */
  425. "pg_catalog.pg_class c",
  426. /* selcondition */
  427. "c.relkind IN ('m')",
  428. /* viscondition */
  429. "pg_catalog.pg_table_is_visible(c.oid)",
  430. /* namespace */
  431. "c.relnamespace",
  432. /* result */
  433. "pg_catalog.quote_ident(c.relname)",
  434. /* qualresult */
  435. NULL
  436. };
  437. /*
  438. * Queries to get lists of names of various kinds of things, possibly
  439. * restricted to names matching a partially entered name. In these queries,
  440. * the first %s will be replaced by the text entered so far (suitably escaped
  441. * to become a SQL literal string). %d will be replaced by the length of the
  442. * string (in unescaped form). A second and third %s, if present, will be
  443. * replaced by a suitably-escaped version of the string provided in
  444. * completion_info_charp. A fourth and fifth %s are similarly replaced by
  445. * completion_info_charp2.
  446. *
  447. * Beware that the allowed sequences of %s and %d are determined by
  448. * _complete_from_query().
  449. */
  450. #define Query_for_list_of_attributes \
  451. "SELECT pg_catalog.quote_ident(attname) "\
  452. " FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c "\
  453. " WHERE c.oid = a.attrelid "\
  454. " AND a.attnum > 0 "\
  455. " AND NOT a.attisdropped "\
  456. " AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\
  457. " AND (pg_catalog.quote_ident(relname)='%s' "\
  458. " OR '\"' || relname || '\"'='%s') "\
  459. " AND pg_catalog.pg_table_is_visible(c.oid)"
  460. #define Query_for_list_of_attributes_with_schema \
  461. "SELECT pg_catalog.quote_ident(attname) "\
  462. " FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c, pg_catalog.pg_namespace n "\
  463. " WHERE c.oid = a.attrelid "\
  464. " AND n.oid = c.relnamespace "\
  465. " AND a.attnum > 0 "\
  466. " AND NOT a.attisdropped "\
  467. " AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\
  468. " AND (pg_catalog.quote_ident(relname)='%s' "\
  469. " OR '\"' || relname || '\"' ='%s') "\
  470. " AND (pg_catalog.quote_ident(nspname)='%s' "\
  471. " OR '\"' || nspname || '\"' ='%s') "
  472. #define Query_for_list_of_template_databases \
  473. "SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database "\
  474. " WHERE substring(pg_catalog.quote_ident(datname),1,%d)='%s' AND datistemplate"
  475. #define Query_for_list_of_databases \
  476. "SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database "\
  477. " WHERE substring(pg_catalog.quote_ident(datname),1,%d)='%s'"
  478. #define Query_for_list_of_tablespaces \
  479. "SELECT pg_catalog.quote_ident(spcname) FROM pg_catalog.pg_tablespace "\
  480. " WHERE substring(pg_catalog.quote_ident(spcname),1,%d)='%s'"
  481. #define Query_for_list_of_encodings \
  482. " SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\
  483. " FROM pg_catalog.pg_conversion "\
  484. " WHERE substring(pg_catalog.pg_encoding_to_char(conforencoding),1,%d)=UPPER('%s')"
  485. #define Query_for_list_of_languages \
  486. "SELECT pg_catalog.quote_ident(lanname) "\
  487. " FROM pg_catalog.pg_language "\
  488. " WHERE lanname != 'internal' "\
  489. " AND substring(pg_catalog.quote_ident(lanname),1,%d)='%s'"
  490. #define Query_for_list_of_schemas \
  491. "SELECT pg_catalog.quote_ident(nspname) FROM pg_catalog.pg_namespace "\
  492. " WHERE substring(pg_catalog.quote_ident(nspname),1,%d)='%s'"
  493. #define Query_for_list_of_alter_system_set_vars \
  494. "SELECT name FROM "\
  495. " (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\
  496. " WHERE context != 'internal') ss "\
  497. " WHERE substring(name,1,%d)='%s'"
  498. #define Query_for_list_of_set_vars \
  499. "SELECT name FROM "\
  500. " (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\
  501. " WHERE context IN ('user', 'superuser') "\
  502. " UNION ALL SELECT 'constraints' "\
  503. " UNION ALL SELECT 'transaction' "\
  504. " UNION ALL SELECT 'session' "\
  505. " UNION ALL SELECT 'role' "\
  506. " UNION ALL SELECT 'tablespace' "\
  507. " UNION ALL SELECT 'all') ss "\
  508. " WHERE substring(name,1,%d)='%s'"
  509. #define Query_for_list_of_show_vars \
  510. "SELECT name FROM "\
  511. " (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\
  512. " UNION ALL SELECT 'session authorization' "\
  513. " UNION ALL SELECT 'all') ss "\
  514. " WHERE substring(name,1,%d)='%s'"
  515. #define Query_for_list_of_roles \
  516. " SELECT pg_catalog.quote_ident(rolname) "\
  517. " FROM pg_catalog.pg_roles "\
  518. " WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"
  519. #define Query_for_list_of_grant_roles \
  520. " SELECT pg_catalog.quote_ident(rolname) "\
  521. " FROM pg_catalog.pg_roles "\
  522. " WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"\
  523. " UNION ALL SELECT 'PUBLIC'"
  524. /* the silly-looking length condition is just to eat up the current word */
  525. #define Query_for_table_owning_index \
  526. "SELECT pg_catalog.quote_ident(c1.relname) "\
  527. " FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\
  528. " WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\
  529. " and (%d = pg_catalog.length('%s'))"\
  530. " and pg_catalog.quote_ident(c2.relname)='%s'"\
  531. " and pg_catalog.pg_table_is_visible(c2.oid)"
  532. /* the silly-looking length condition is just to eat up the current word */
  533. #define Query_for_index_of_table \
  534. "SELECT pg_catalog.quote_ident(c2.relname) "\
  535. " FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\
  536. " WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\
  537. " and (%d = pg_catalog.length('%s'))"\
  538. " and pg_catalog.quote_ident(c1.relname)='%s'"\
  539. " and pg_catalog.pg_table_is_visible(c2.oid)"
  540. /* the silly-looking length condition is just to eat up the current word */
  541. #define Query_for_constraint_of_table \
  542. "SELECT pg_catalog.quote_ident(conname) "\
  543. " FROM pg_catalog.pg_class c1, pg_catalog.pg_constraint con "\
  544. " WHERE c1.oid=conrelid and (%d = pg_catalog.length('%s'))"\
  545. " and pg_catalog.quote_ident(c1.relname)='%s'"\
  546. " and pg_catalog.pg_table_is_visible(c1.oid)"
  547. #define Query_for_all_table_constraints \
  548. "SELECT pg_catalog.quote_ident(conname) "\
  549. " FROM pg_catalog.pg_constraint c "\
  550. " WHERE c.conrelid <> 0 "
  551. /* the silly-looking length condition is just to eat up the current word */
  552. #define Query_for_constraint_of_type \
  553. "SELECT pg_catalog.quote_ident(conname) "\
  554. " FROM pg_catalog.pg_type t, pg_catalog.pg_constraint con "\
  555. " WHERE t.oid=contypid and (%d = pg_catalog.length('%s'))"\
  556. " and pg_catalog.quote_ident(t.typname)='%s'"\
  557. " and pg_catalog.pg_type_is_visible(t.oid)"
  558. /* the silly-looking length condition is just to eat up the current word */
  559. #define Query_for_list_of_tables_for_constraint \
  560. "SELECT pg_catalog.quote_ident(relname) "\
  561. " FROM pg_catalog.pg_class"\
  562. " WHERE (%d = pg_catalog.length('%s'))"\
  563. " AND oid IN "\
  564. " (SELECT conrelid FROM pg_catalog.pg_constraint "\
  565. " WHERE pg_catalog.quote_ident(conname)='%s')"
  566. /* the silly-looking length condition is just to eat up the current word */
  567. #define Query_for_rule_of_table \
  568. "SELECT pg_catalog.quote_ident(rulename) "\
  569. " FROM pg_catalog.pg_class c1, pg_catalog.pg_rewrite "\
  570. " WHERE c1.oid=ev_class and (%d = pg_catalog.length('%s'))"\
  571. " and pg_catalog.quote_ident(c1.relname)='%s'"\
  572. " and pg_catalog.pg_table_is_visible(c1.oid)"
  573. /* the silly-looking length condition is just to eat up the current word */
  574. #define Query_for_list_of_tables_for_rule \
  575. "SELECT pg_catalog.quote_ident(relname) "\
  576. " FROM pg_catalog.pg_class"\
  577. " WHERE (%d = pg_catalog.length('%s'))"\
  578. " AND oid IN "\
  579. " (SELECT ev_class FROM pg_catalog.pg_rewrite "\
  580. " WHERE pg_catalog.quote_ident(rulename)='%s')"
  581. /* the silly-looking length condition is just to eat up the current word */
  582. #define Query_for_trigger_of_table \
  583. "SELECT pg_catalog.quote_ident(tgname) "\
  584. " FROM pg_catalog.pg_class c1, pg_catalog.pg_trigger "\
  585. " WHERE c1.oid=tgrelid and (%d = pg_catalog.length('%s'))"\
  586. " and pg_catalog.quote_ident(c1.relname)='%s'"\
  587. " and pg_catalog.pg_table_is_visible(c1.oid)"\
  588. " and not tgisinternal"
  589. /* the silly-looking length condition is just to eat up the current word */
  590. #define Query_for_list_of_tables_for_trigger \
  591. "SELECT pg_catalog.quote_ident(relname) "\
  592. " FROM pg_catalog.pg_class"\
  593. " WHERE (%d = pg_catalog.length('%s'))"\
  594. " AND oid IN "\
  595. " (SELECT tgrelid FROM pg_catalog.pg_trigger "\
  596. " WHERE pg_catalog.quote_ident(tgname)='%s')"
  597. #define Query_for_list_of_ts_configurations \
  598. "SELECT pg_catalog.quote_ident(cfgname) FROM pg_catalog.pg_ts_config "\
  599. " WHERE substring(pg_catalog.quote_ident(cfgname),1,%d)='%s'"
  600. #define Query_for_list_of_ts_dictionaries \
  601. "SELECT pg_catalog.quote_ident(dictname) FROM pg_catalog.pg_ts_dict "\
  602. " WHERE substring(pg_catalog.quote_ident(dictname),1,%d)='%s'"
  603. #define Query_for_list_of_ts_parsers \
  604. "SELECT pg_catalog.quote_ident(prsname) FROM pg_catalog.pg_ts_parser "\
  605. " WHERE substring(pg_catalog.quote_ident(prsname),1,%d)='%s'"
  606. #define Query_for_list_of_ts_templates \
  607. "SELECT pg_catalog.quote_ident(tmplname) FROM pg_catalog.pg_ts_template "\
  608. " WHERE substring(pg_catalog.quote_ident(tmplname),1,%d)='%s'"
  609. #define Query_for_list_of_fdws \
  610. " SELECT pg_catalog.quote_ident(fdwname) "\
  611. " FROM pg_catalog.pg_foreign_data_wrapper "\
  612. " WHERE substring(pg_catalog.quote_ident(fdwname),1,%d)='%s'"
  613. #define Query_for_list_of_servers \
  614. " SELECT pg_catalog.quote_ident(srvname) "\
  615. " FROM pg_catalog.pg_foreign_server "\
  616. " WHERE substring(pg_catalog.quote_ident(srvname),1,%d)='%s'"
  617. #define Query_for_list_of_user_mappings \
  618. " SELECT pg_catalog.quote_ident(usename) "\
  619. " FROM pg_catalog.pg_user_mappings "\
  620. " WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'"
  621. #define Query_for_list_of_access_methods \
  622. " SELECT pg_catalog.quote_ident(amname) "\
  623. " FROM pg_catalog.pg_am "\
  624. " WHERE substring(pg_catalog.quote_ident(amname),1,%d)='%s'"
  625. /* the silly-looking length condition is just to eat up the current word */
  626. #define Query_for_list_of_arguments \
  627. "SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\
  628. " FROM pg_catalog.pg_proc "\
  629. " WHERE (%d = pg_catalog.length('%s'))"\
  630. " AND (pg_catalog.quote_ident(proname)='%s'"\
  631. " OR '\"' || proname || '\"'='%s') "\
  632. " AND (pg_catalog.pg_function_is_visible(pg_proc.oid))"
  633. /* the silly-looking length condition is just to eat up the current word */
  634. #define Query_for_list_of_arguments_with_schema \
  635. "SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\
  636. " FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n "\
  637. " WHERE (%d = pg_catalog.length('%s'))"\
  638. " AND n.oid = p.pronamespace "\
  639. " AND (pg_catalog.quote_ident(proname)='%s' "\
  640. " OR '\"' || proname || '\"' ='%s') "\
  641. " AND (pg_catalog.quote_ident(nspname)='%s' "\
  642. " OR '\"' || nspname || '\"' ='%s') "
  643. #define Query_for_list_of_extensions \
  644. " SELECT pg_catalog.quote_ident(extname) "\
  645. " FROM pg_catalog.pg_extension "\
  646. " WHERE substring(pg_catalog.quote_ident(extname),1,%d)='%s'"
  647. #define Query_for_list_of_available_extensions \
  648. " SELECT pg_catalog.quote_ident(name) "\
  649. " FROM pg_catalog.pg_available_extensions "\
  650. " WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s' AND installed_version IS NULL"
  651. #define Query_for_list_of_prepared_statements \
  652. " SELECT pg_catalog.quote_ident(name) "\
  653. " FROM pg_catalog.pg_prepared_statements "\
  654. " WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'"
  655. #define Query_for_list_of_event_triggers \
  656. " SELECT pg_catalog.quote_ident(evtname) "\
  657. " FROM pg_catalog.pg_event_trigger "\
  658. " WHERE substring(pg_catalog.quote_ident(evtname),1,%d)='%s'"
  659. /*
  660. * This is a list of all "things" in Pgsql, which can show up after CREATE or
  661. * DROP; and there is also a query to get a list of them.
  662. */
  663. typedef struct
  664. {
  665. const char *name;
  666. const char *query; /* simple query, or NULL */
  667. const SchemaQuery *squery; /* schema query, or NULL */
  668. const bits32 flags; /* visibility flags, see below */
  669. } pgsql_thing_t;
  670. #define THING_NO_CREATE (1 << 0) /* should not show up after CREATE */
  671. #define THING_NO_DROP (1 << 1) /* should not show up after DROP */
  672. #define THING_NO_SHOW (THING_NO_CREATE | THING_NO_DROP)
  673. static const pgsql_thing_t words_after_create[] = {
  674. {"AGGREGATE", NULL, &Query_for_list_of_aggregates},
  675. {"CAST", NULL, NULL}, /* Casts have complex structures for names, so
  676. * skip it */
  677. {"COLLATION", "SELECT pg_catalog.quote_ident(collname) FROM pg_catalog.pg_collation WHERE collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding())) AND substring(pg_catalog.quote_ident(collname),1,%d)='%s'"},
  678. /*
  679. * CREATE CONSTRAINT TRIGGER is not supported here because it is designed
  680. * to be used only by pg_dump.
  681. */
  682. {"CONFIGURATION", Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW},
  683. {"CONVERSION", "SELECT pg_catalog.quote_ident(conname) FROM pg_catalog.pg_conversion WHERE substring(pg_catalog.quote_ident(conname),1,%d)='%s'"},
  684. {"DATABASE", Query_for_list_of_databases},
  685. {"DICTIONARY", Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW},
  686. {"DOMAIN", NULL, &Query_for_list_of_domains},
  687. {"EVENT TRIGGER", NULL, NULL},
  688. {"EXTENSION", Query_for_list_of_extensions},
  689. {"FOREIGN DATA WRAPPER", NULL, NULL},
  690. {"FOREIGN TABLE", NULL, NULL},
  691. {"FUNCTION", NULL, &Query_for_list_of_functions},
  692. {"GROUP", Query_for_list_of_roles},
  693. {"LANGUAGE", Query_for_list_of_languages},
  694. {"INDEX", NULL, &Query_for_list_of_indexes},
  695. {"MATERIALIZED VIEW", NULL, NULL},
  696. {"OPERATOR", NULL, NULL}, /* Querying for this is probably not such a
  697. * good idea. */
  698. {"OWNED", NULL, NULL, THING_NO_CREATE}, /* for DROP OWNED BY ... */
  699. {"PARSER", Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW},
  700. {"ROLE", Query_for_list_of_roles},
  701. {"RULE", "SELECT pg_catalog.quote_ident(rulename) FROM pg_catalog.pg_rules WHERE substring(pg_catalog.quote_ident(rulename),1,%d)='%s'"},
  702. {"SCHEMA", Query_for_list_of_schemas},
  703. {"SEQUENCE", NULL, &Query_for_list_of_sequences},
  704. {"SERVER", Query_for_list_of_servers},
  705. {"TABLE", NULL, &Query_for_list_of_tables},
  706. {"TABLESPACE", Query_for_list_of_tablespaces},
  707. {"TEMP", NULL, NULL, THING_NO_DROP}, /* for CREATE TEMP TABLE ... */
  708. {"TEMPLATE", Query_for_list_of_ts_templates, NULL, THING_NO_SHOW},
  709. {"TEXT SEARCH", NULL, NULL},
  710. {"TRIGGER", "SELECT pg_catalog.quote_ident(tgname) FROM pg_catalog.pg_trigger WHERE substring(pg_catalog.quote_ident(tgname),1,%d)='%s' AND NOT tgisinternal"},
  711. {"TYPE", NULL, &Query_for_list_of_datatypes},
  712. {"UNIQUE", NULL, NULL, THING_NO_DROP}, /* for CREATE UNIQUE INDEX ... */
  713. {"UNLOGGED", NULL, NULL, THING_NO_DROP}, /* for CREATE UNLOGGED TABLE
  714. * ... */
  715. {"USER", Query_for_list_of_roles},
  716. {"USER MAPPING FOR", NULL, NULL},
  717. {"VIEW", NULL, &Query_for_list_of_views},
  718. {NULL} /* end of list */
  719. };
  720. /* Forward declaration of functions */
  721. static char **psql_completion(const char *text, int start, int end);
  722. static char *create_command_generator(const char *text, int state);
  723. static char *drop_command_generator(const char *text, int state);
  724. static char *complete_from_query(const char *text, int state);
  725. static char *complete_from_schema_query(const char *text, int state);
  726. static char *_complete_from_query(int is_schema_query,
  727. const char *text, int state);
  728. static char *complete_from_list(const char *text, int state);
  729. static char *complete_from_const(const char *text, int state);
  730. static char **complete_from_variables(const char *text,
  731. const char *prefix, const char *suffix);
  732. static char *complete_from_files(const char *text, int state);
  733. static char *pg_strdup_keyword_case(const char *s, const char *ref);
  734. static PGresult *exec_query(const char *query);
  735. static void get_previous_words(int point, char **previous_words, int nwords);
  736. #ifdef NOT_USED
  737. static char *quote_file_name(char *text, int match_type, char *quote_pointer);
  738. static char *dequote_file_name(char *text, char quote_char);
  739. #endif
  740. /*
  741. * Initialize the readline library for our purposes.
  742. */
  743. void
  744. initialize_readline(void)
  745. {
  746. rl_readline_name = (char *) pset.progname;
  747. rl_attempted_completion_function = psql_completion;
  748. rl_basic_word_break_characters = WORD_BREAKS;
  749. completion_max_records = 1000;
  750. /*
  751. * There is a variable rl_completion_query_items for this but apparently
  752. * it's not defined everywhere.
  753. */
  754. }
  755. /*
  756. * The completion function.
  757. *
  758. * According to readline spec this gets passed the text entered so far and its
  759. * start and end positions in the readline buffer. The return value is some
  760. * partially obscure list format that can be generated by readline's
  761. * completion_matches() function, so we don't have to worry about it.
  762. */
  763. static char **
  764. psql_completion(const char *text, int start, int end)
  765. {
  766. /* This is the variable we'll return. */
  767. char **matches = NULL;
  768. /* This array will contain some scannage of the input line. */
  769. char *previous_words[6];
  770. /* For compactness, we use these macros to reference previous_words[]. */
  771. #define prev_wd (previous_words[0])
  772. #define prev2_wd (previous_words[1])
  773. #define prev3_wd (previous_words[2])
  774. #define prev4_wd (previous_words[3])
  775. #define prev5_wd (previous_words[4])
  776. #define prev6_wd (previous_words[5])
  777. static const char *const sql_commands[] = {
  778. "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
  779. "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
  780. "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN",
  781. "FETCH", "GRANT", "IMPORT", "INSERT", "LISTEN", "LOAD", "LOCK",
  782. "MOVE", "NOTIFY", "PREPARE",
  783. "REASSIGN", "REFRESH", "REINDEX", "RELEASE", "RESET", "REVOKE", "ROLLBACK",
  784. "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START",
  785. "TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES", "WITH",
  786. NULL
  787. };
  788. static const char *const backslash_commands[] = {
  789. "\\a", "\\connect", "\\conninfo", "\\C", "\\cd", "\\copy", "\\copyright",
  790. "\\d", "\\da", "\\db", "\\dc", "\\dC", "\\dd", "\\dD", "\\des", "\\det", "\\deu", "\\dew", "\\df",
  791. "\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL",
  792. "\\dn", "\\do", "\\dp", "\\drds", "\\ds", "\\dS", "\\dt", "\\dT", "\\dv", "\\du", "\\dx",
  793. "\\e", "\\echo", "\\ef", "\\encoding",
  794. "\\f", "\\g", "\\gset", "\\h", "\\help", "\\H", "\\i", "\\ir", "\\l",
  795. "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
  796. "\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\\qecho", "\\r",
  797. "\\set", "\\sf", "\\t", "\\T",
  798. "\\timing", "\\unset", "\\x", "\\w", "\\watch", "\\z", "\\!", NULL
  799. };
  800. (void) end; /* not used */
  801. #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
  802. rl_completion_append_character = ' ';
  803. #endif
  804. /* Clear a few things. */
  805. completion_charp = NULL;
  806. completion_charpp = NULL;
  807. completion_info_charp = NULL;
  808. completion_info_charp2 = NULL;
  809. /*
  810. * Scan the input line before our current position for the last few words.
  811. * According to those we'll make some smart decisions on what the user is
  812. * probably intending to type.
  813. */
  814. get_previous_words(start, previous_words, lengthof(previous_words));
  815. /* If a backslash command was started, continue */
  816. if (text[0] == '\\')
  817. COMPLETE_WITH_LIST_CS(backslash_commands);
  818. /* Variable interpolation */
  819. else if (text[0] == ':' && text[1] != ':')
  820. {
  821. if (text[1] == '\'')
  822. matches = complete_from_variables(text, ":'", "'");
  823. else if (text[1] == '"')
  824. matches = complete_from_variables(text, ":\"", "\"");
  825. else
  826. matches = complete_from_variables(text, ":", "");
  827. }
  828. /* If no previous word, suggest one of the basic sql commands */
  829. else if (prev_wd[0] == '\0')
  830. COMPLETE_WITH_LIST(sql_commands);
  831. /* CREATE */
  832. /* complete with something you can create */
  833. else if (pg_strcasecmp(prev_wd, "CREATE") == 0)
  834. matches = completion_matches(text, create_command_generator);
  835. /* DROP, but not DROP embedded in other commands */
  836. /* complete with something you can drop */
  837. else if (pg_strcasecmp(prev_wd, "DROP") == 0 &&
  838. prev2_wd[0] == '\0')
  839. matches = completion_matches(text, drop_command_generator);
  840. /* ALTER */
  841. /*
  842. * complete with what you can alter (TABLE, GROUP, USER, ...) unless we're
  843. * in ALTER TABLE sth ALTER
  844. */
  845. else if (pg_strcasecmp(prev_wd, "ALTER") == 0 &&
  846. pg_strcasecmp(prev3_wd, "TABLE") != 0)
  847. {
  848. static const char *const list_ALTER[] =
  849. {"AGGREGATE", "COLLATION", "CONVERSION", "DATABASE", "DEFAULT PRIVILEGES", "DOMAIN",
  850. "EVENT TRIGGER", "EXTENSION", "FOREIGN DATA WRAPPER", "FOREIGN TABLE", "FUNCTION",
  851. "GROUP", "INDEX", "LANGUAGE", "LARGE OBJECT", "MATERIALIZED VIEW", "OPERATOR",
  852. "ROLE", "RULE", "SCHEMA", "SERVER", "SEQUENCE", "SYSTEM SET", "TABLE",
  853. "TABLESPACE", "TEXT SEARCH", "TRIGGER", "TYPE",
  854. "USER", "USER MAPPING FOR", "VIEW", NULL};
  855. COMPLETE_WITH_LIST(list_ALTER);
  856. }
  857. /* ALTER AGGREGATE,FUNCTION <name> */
  858. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  859. (pg_strcasecmp(prev2_wd, "AGGREGATE") == 0 ||
  860. pg_strcasecmp(prev2_wd, "FUNCTION") == 0))
  861. COMPLETE_WITH_CONST("(");
  862. /* ALTER AGGREGATE,FUNCTION <name> (...) */
  863. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  864. (pg_strcasecmp(prev3_wd, "AGGREGATE") == 0 ||
  865. pg_strcasecmp(prev3_wd, "FUNCTION") == 0))
  866. {
  867. if (prev_wd[strlen(prev_wd) - 1] == ')')
  868. {
  869. static const char *const list_ALTERAGG[] =
  870. {"OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
  871. COMPLETE_WITH_LIST(list_ALTERAGG);
  872. }
  873. else
  874. COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
  875. }
  876. /* ALTER SCHEMA <name> */
  877. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  878. pg_strcasecmp(prev2_wd, "SCHEMA") == 0)
  879. {
  880. static const char *const list_ALTERGEN[] =
  881. {"OWNER TO", "RENAME TO", NULL};
  882. COMPLETE_WITH_LIST(list_ALTERGEN);
  883. }
  884. /* ALTER COLLATION <name> */
  885. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  886. pg_strcasecmp(prev2_wd, "COLLATION") == 0)
  887. {
  888. static const char *const list_ALTERGEN[] =
  889. {"OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
  890. COMPLETE_WITH_LIST(list_ALTERGEN);
  891. }
  892. /* ALTER CONVERSION <name> */
  893. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  894. pg_strcasecmp(prev2_wd, "CONVERSION") == 0)
  895. {
  896. static const char *const list_ALTERGEN[] =
  897. {"OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
  898. COMPLETE_WITH_LIST(list_ALTERGEN);
  899. }
  900. /* ALTER DATABASE <name> */
  901. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  902. pg_strcasecmp(prev2_wd, "DATABASE") == 0)
  903. {
  904. static const char *const list_ALTERDATABASE[] =
  905. {"RESET", "SET", "OWNER TO", "RENAME TO", "IS_TEMPLATE",
  906. "ALLOW_CONNECTIONS", "CONNECTION LIMIT", NULL};
  907. COMPLETE_WITH_LIST(list_ALTERDATABASE);
  908. }
  909. /* ALTER EVENT TRIGGER */
  910. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  911. pg_strcasecmp(prev2_wd, "EVENT") == 0 &&
  912. pg_strcasecmp(prev_wd, "TRIGGER") == 0)
  913. {
  914. COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
  915. }
  916. /* ALTER EVENT TRIGGER <name> */
  917. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  918. pg_strcasecmp(prev3_wd, "EVENT") == 0 &&
  919. pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
  920. {
  921. static const char *const list_ALTER_EVENT_TRIGGER[] =
  922. {"DISABLE", "ENABLE", "OWNER TO", "RENAME TO", NULL};
  923. COMPLETE_WITH_LIST(list_ALTER_EVENT_TRIGGER);
  924. }
  925. /* ALTER EVENT TRIGGER <name> ENABLE */
  926. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  927. pg_strcasecmp(prev4_wd, "EVENT") == 0 &&
  928. pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
  929. pg_strcasecmp(prev_wd, "ENABLE") == 0)
  930. {
  931. static const char *const list_ALTER_EVENT_TRIGGER_ENABLE[] =
  932. {"REPLICA", "ALWAYS", NULL};
  933. COMPLETE_WITH_LIST(list_ALTER_EVENT_TRIGGER_ENABLE);
  934. }
  935. /* ALTER EXTENSION <name> */
  936. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  937. pg_strcasecmp(prev2_wd, "EXTENSION") == 0)
  938. {
  939. static const char *const list_ALTEREXTENSION[] =
  940. {"ADD", "DROP", "UPDATE", "SET SCHEMA", NULL};
  941. COMPLETE_WITH_LIST(list_ALTEREXTENSION);
  942. }
  943. /* ALTER FOREIGN */
  944. else if (pg_strcasecmp(prev2_wd, "ALTER") == 0 &&
  945. pg_strcasecmp(prev_wd, "FOREIGN") == 0)
  946. {
  947. static const char *const list_ALTER_FOREIGN[] =
  948. {"DATA WRAPPER", "TABLE", NULL};
  949. COMPLETE_WITH_LIST(list_ALTER_FOREIGN);
  950. }
  951. /* ALTER FOREIGN DATA WRAPPER <name> */
  952. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  953. pg_strcasecmp(prev4_wd, "FOREIGN") == 0 &&
  954. pg_strcasecmp(prev3_wd, "DATA") == 0 &&
  955. pg_strcasecmp(prev2_wd, "WRAPPER") == 0)
  956. {
  957. static const char *const list_ALTER_FDW[] =
  958. {"HANDLER", "VALIDATOR", "OPTIONS", "OWNER TO", NULL};
  959. COMPLETE_WITH_LIST(list_ALTER_FDW);
  960. }
  961. /* ALTER FOREIGN TABLE <name> */
  962. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  963. pg_strcasecmp(prev3_wd, "FOREIGN") == 0 &&
  964. pg_strcasecmp(prev2_wd, "TABLE") == 0)
  965. {
  966. static const char *const list_ALTER_FOREIGN_TABLE[] =
  967. {"ALTER", "DROP", "RENAME", "OWNER TO", "SET SCHEMA", NULL};
  968. COMPLETE_WITH_LIST(list_ALTER_FOREIGN_TABLE);
  969. }
  970. /* ALTER INDEX <name> */
  971. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  972. pg_strcasecmp(prev2_wd, "INDEX") == 0)
  973. {
  974. static const char *const list_ALTERINDEX[] =
  975. {"OWNER TO", "RENAME TO", "SET", "RESET", NULL};
  976. COMPLETE_WITH_LIST(list_ALTERINDEX);
  977. }
  978. /* ALTER INDEX <name> SET */
  979. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  980. pg_strcasecmp(prev3_wd, "INDEX") == 0 &&
  981. pg_strcasecmp(prev_wd, "SET") == 0)
  982. {
  983. static const char *const list_ALTERINDEXSET[] =
  984. {"(", "TABLESPACE", NULL};
  985. COMPLETE_WITH_LIST(list_ALTERINDEXSET);
  986. }
  987. /* ALTER INDEX <name> RESET */
  988. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  989. pg_strcasecmp(prev3_wd, "INDEX") == 0 &&
  990. pg_strcasecmp(prev_wd, "RESET") == 0)
  991. COMPLETE_WITH_CONST("(");
  992. /* ALTER INDEX <foo> SET|RESET ( */
  993. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  994. pg_strcasecmp(prev4_wd, "INDEX") == 0 &&
  995. (pg_strcasecmp(prev2_wd, "SET") == 0 ||
  996. pg_strcasecmp(prev2_wd, "RESET") == 0) &&
  997. pg_strcasecmp(prev_wd, "(") == 0)
  998. {
  999. static const char *const list_INDEXOPTIONS[] =
  1000. {"fillfactor", "fastupdate", NULL};
  1001. COMPLETE_WITH_LIST(list_INDEXOPTIONS);
  1002. }
  1003. /* ALTER LANGUAGE <name> */
  1004. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1005. pg_strcasecmp(prev2_wd, "LANGUAGE") == 0)
  1006. {
  1007. static const char *const list_ALTERLANGUAGE[] =
  1008. {"OWNER TO", "RENAME TO", NULL};
  1009. COMPLETE_WITH_LIST(list_ALTERLANGUAGE);
  1010. }
  1011. /* ALTER LARGE OBJECT <oid> */
  1012. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1013. pg_strcasecmp(prev3_wd, "LARGE") == 0 &&
  1014. pg_strcasecmp(prev2_wd, "OBJECT") == 0)
  1015. {
  1016. static const char *const list_ALTERLARGEOBJECT[] =
  1017. {"OWNER TO", NULL};
  1018. COMPLETE_WITH_LIST(list_ALTERLARGEOBJECT);
  1019. }
  1020. /* ALTER MATERIALIZED VIEW */
  1021. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1022. pg_strcasecmp(prev2_wd, "MATERIALIZED") == 0 &&
  1023. pg_strcasecmp(prev_wd, "VIEW") == 0)
  1024. {
  1025. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL);
  1026. }
  1027. /* ALTER USER,ROLE <name> */
  1028. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1029. !(pg_strcasecmp(prev2_wd, "USER") == 0 && pg_strcasecmp(prev_wd, "MAPPING") == 0) &&
  1030. (pg_strcasecmp(prev2_wd, "USER") == 0 ||
  1031. pg_strcasecmp(prev2_wd, "ROLE") == 0))
  1032. {
  1033. static const char *const list_ALTERUSER[] =
  1034. {"CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER",
  1035. "ENCRYPTED", "INHERIT", "LOGIN", "NOCREATEDB", "NOCREATEROLE",
  1036. "NOCREATEUSER", "NOINHERIT", "NOLOGIN", "NOREPLICATION",
  1037. "NOSUPERUSER", "RENAME TO", "REPLICATION", "RESET", "SET",
  1038. "SUPERUSER", "UNENCRYPTED", "VALID UNTIL", "WITH", NULL};
  1039. COMPLETE_WITH_LIST(list_ALTERUSER);
  1040. }
  1041. /* ALTER USER,ROLE <name> WITH */
  1042. else if ((pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1043. (pg_strcasecmp(prev3_wd, "USER") == 0 ||
  1044. pg_strcasecmp(prev3_wd, "ROLE") == 0) &&
  1045. pg_strcasecmp(prev_wd, "WITH") == 0))
  1046. {
  1047. /* Similar to the above, but don't complete "WITH" again. */
  1048. static const char *const list_ALTERUSER_WITH[] =
  1049. {"CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER",
  1050. "ENCRYPTED", "INHERIT", "LOGIN", "NOCREATEDB", "NOCREATEROLE",
  1051. "NOCREATEUSER", "NOINHERIT", "NOLOGIN", "NOREPLICATION",
  1052. "NOSUPERUSER", "RENAME TO", "REPLICATION", "RESET", "SET",
  1053. "SUPERUSER", "UNENCRYPTED", "VALID UNTIL", NULL};
  1054. COMPLETE_WITH_LIST(list_ALTERUSER_WITH);
  1055. }
  1056. /* complete ALTER USER,ROLE <name> ENCRYPTED,UNENCRYPTED with PASSWORD */
  1057. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1058. (pg_strcasecmp(prev3_wd, "ROLE") == 0 || pg_strcasecmp(prev3_wd, "USER") == 0) &&
  1059. (pg_strcasecmp(prev_wd, "ENCRYPTED") == 0 || pg_strcasecmp(prev_wd, "UNENCRYPTED") == 0))
  1060. {
  1061. COMPLETE_WITH_CONST("PASSWORD");
  1062. }
  1063. /* ALTER DEFAULT PRIVILEGES */
  1064. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1065. pg_strcasecmp(prev2_wd, "DEFAULT") == 0 &&
  1066. pg_strcasecmp(prev_wd, "PRIVILEGES") == 0)
  1067. {
  1068. static const char *const list_ALTER_DEFAULT_PRIVILEGES[] =
  1069. {"FOR ROLE", "FOR USER", "IN SCHEMA", NULL};
  1070. COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES);
  1071. }
  1072. /* ALTER DEFAULT PRIVILEGES FOR */
  1073. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1074. pg_strcasecmp(prev3_wd, "DEFAULT") == 0 &&
  1075. pg_strcasecmp(prev2_wd, "PRIVILEGES") == 0 &&
  1076. pg_strcasecmp(prev_wd, "FOR") == 0)
  1077. {
  1078. static const char *const list_ALTER_DEFAULT_PRIVILEGES_FOR[] =
  1079. {"ROLE", "USER", NULL};
  1080. COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES_FOR);
  1081. }
  1082. /* ALTER DEFAULT PRIVILEGES { FOR ROLE ... | IN SCHEMA ... } */
  1083. else if (pg_strcasecmp(prev5_wd, "DEFAULT") == 0 &&
  1084. pg_strcasecmp(prev4_wd, "PRIVILEGES") == 0 &&
  1085. (pg_strcasecmp(prev3_wd, "FOR") == 0 ||
  1086. pg_strcasecmp(prev3_wd, "IN") == 0))
  1087. {
  1088. static const char *const list_ALTER_DEFAULT_PRIVILEGES_REST[] =
  1089. {"GRANT", "REVOKE", NULL};
  1090. COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES_REST);
  1091. }
  1092. /* ALTER DOMAIN <name> */
  1093. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1094. pg_strcasecmp(prev2_wd, "DOMAIN") == 0)
  1095. {
  1096. static const char *const list_ALTERDOMAIN[] =
  1097. {"ADD", "DROP", "OWNER TO", "RENAME", "SET", "VALIDATE CONSTRAINT", NULL};
  1098. COMPLETE_WITH_LIST(list_ALTERDOMAIN);
  1099. }
  1100. /* ALTER DOMAIN <sth> DROP */
  1101. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1102. pg_strcasecmp(prev3_wd, "DOMAIN") == 0 &&
  1103. pg_strcasecmp(prev_wd, "DROP") == 0)
  1104. {
  1105. static const char *const list_ALTERDOMAIN2[] =
  1106. {"CONSTRAINT", "DEFAULT", "NOT NULL", NULL};
  1107. COMPLETE_WITH_LIST(list_ALTERDOMAIN2);
  1108. }
  1109. /* ALTER DOMAIN <sth> DROP|RENAME|VALIDATE CONSTRAINT */
  1110. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1111. pg_strcasecmp(prev4_wd, "DOMAIN") == 0 &&
  1112. (pg_strcasecmp(prev2_wd, "DROP") == 0 ||
  1113. pg_strcasecmp(prev2_wd, "RENAME") == 0 ||
  1114. pg_strcasecmp(prev2_wd, "VALIDATE") == 0) &&
  1115. pg_strcasecmp(prev_wd, "CONSTRAINT") == 0)
  1116. {
  1117. completion_info_charp = prev3_wd;
  1118. COMPLETE_WITH_QUERY(Query_for_constraint_of_type);
  1119. }
  1120. /* ALTER DOMAIN <sth> RENAME */
  1121. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1122. pg_strcasecmp(prev3_wd, "DOMAIN") == 0 &&
  1123. pg_strcasecmp(prev_wd, "RENAME") == 0)
  1124. {
  1125. static const char *const list_ALTERDOMAIN[] =
  1126. {"CONSTRAINT", "TO", NULL};
  1127. COMPLETE_WITH_LIST(list_ALTERDOMAIN);
  1128. }
  1129. /* ALTER DOMAIN <sth> RENAME CONSTRAINT <sth> */
  1130. else if (pg_strcasecmp(prev5_wd, "DOMAIN") == 0 &&
  1131. pg_strcasecmp(prev3_wd, "RENAME") == 0 &&
  1132. pg_strcasecmp(prev2_wd, "CONSTRAINT") == 0)
  1133. COMPLETE_WITH_CONST("TO");
  1134. /* ALTER DOMAIN <sth> SET */
  1135. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1136. pg_strcasecmp(prev3_wd, "DOMAIN") == 0 &&
  1137. pg_strcasecmp(prev_wd, "SET") == 0)
  1138. {
  1139. static const char *const list_ALTERDOMAIN3[] =
  1140. {"DEFAULT", "NOT NULL", "SCHEMA", NULL};
  1141. COMPLETE_WITH_LIST(list_ALTERDOMAIN3);
  1142. }
  1143. /* ALTER SEQUENCE <name> */
  1144. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1145. pg_strcasecmp(prev2_wd, "SEQUENCE") == 0)
  1146. {
  1147. static const char *const list_ALTERSEQUENCE[] =
  1148. {"INCREMENT", "MINVALUE", "MAXVALUE", "RESTART", "NO", "CACHE", "CYCLE",
  1149. "SET SCHEMA", "OWNED BY", "OWNER TO", "RENAME TO", NULL};
  1150. COMPLETE_WITH_LIST(list_ALTERSEQUENCE);
  1151. }
  1152. /* ALTER SEQUENCE <name> NO */
  1153. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1154. pg_strcasecmp(prev3_wd, "SEQUENCE") == 0 &&
  1155. pg_strcasecmp(prev_wd, "NO") == 0)
  1156. {
  1157. static const char *const list_ALTERSEQUENCE2[] =
  1158. {"MINVALUE", "MAXVALUE", "CYCLE", NULL};
  1159. COMPLETE_WITH_LIST(list_ALTERSEQUENCE2);
  1160. }
  1161. /* ALTER SERVER <name> */
  1162. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1163. pg_strcasecmp(prev2_wd, "SERVER") == 0)
  1164. {
  1165. static const char *const list_ALTER_SERVER[] =
  1166. {"VERSION", "OPTIONS", "OWNER TO", NULL};
  1167. COMPLETE_WITH_LIST(list_ALTER_SERVER);
  1168. }
  1169. /* ALTER SYSTEM SET <name> */
  1170. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1171. pg_strcasecmp(prev2_wd, "SYSTEM") == 0 &&
  1172. pg_strcasecmp(prev_wd, "SET") == 0)
  1173. COMPLETE_WITH_QUERY(Query_for_list_of_alter_system_set_vars);
  1174. /* ALTER VIEW <name> */
  1175. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1176. pg_strcasecmp(prev2_wd, "VIEW") == 0)
  1177. {
  1178. static const char *const list_ALTERVIEW[] =
  1179. {"ALTER COLUMN", "OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
  1180. COMPLETE_WITH_LIST(list_ALTERVIEW);
  1181. }
  1182. /* ALTER MATERIALIZED VIEW <name> */
  1183. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1184. pg_strcasecmp(prev3_wd, "MATERIALIZED") == 0 &&
  1185. pg_strcasecmp(prev2_wd, "VIEW") == 0)
  1186. {
  1187. static const char *const list_ALTERMATVIEW[] =
  1188. {"ALTER COLUMN", "OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
  1189. COMPLETE_WITH_LIST(list_ALTERMATVIEW);
  1190. }
  1191. /* ALTER RULE <name>, add ON */
  1192. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1193. pg_strcasecmp(prev2_wd, "RULE") == 0)
  1194. COMPLETE_WITH_CONST("ON");
  1195. /* If we have ALTER RULE <name> ON, then add the correct tablename */
  1196. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1197. pg_strcasecmp(prev3_wd, "RULE") == 0 &&
  1198. pg_strcasecmp(prev_wd, "ON") == 0)
  1199. {
  1200. completion_info_charp = prev2_wd;
  1201. COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_rule);
  1202. }
  1203. /* ALTER RULE <name> ON <name> */
  1204. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1205. pg_strcasecmp(prev4_wd, "RULE") == 0)
  1206. COMPLETE_WITH_CONST("RENAME TO");
  1207. /* ALTER TRIGGER <name>, add ON */
  1208. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1209. pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
  1210. COMPLETE_WITH_CONST("ON");
  1211. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1212. pg_strcasecmp(prev3_wd, "TRIGGER") == 0)
  1213. {
  1214. completion_info_charp = prev2_wd;
  1215. COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
  1216. }
  1217. /*
  1218. * If we have ALTER TRIGGER <sth> ON, then add the correct tablename
  1219. */
  1220. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1221. pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
  1222. pg_strcasecmp(prev_wd, "ON") == 0)
  1223. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
  1224. /* ALTER TRIGGER <name> ON <name> */
  1225. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1226. pg_strcasecmp(prev4_wd, "TRIGGER") == 0 &&
  1227. pg_strcasecmp(prev2_wd, "ON") == 0)
  1228. COMPLETE_WITH_CONST("RENAME TO");
  1229. /*
  1230. * If we detect ALTER TABLE <name>, suggest sub commands
  1231. */
  1232. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1233. pg_strcasecmp(prev2_wd, "TABLE") == 0)
  1234. {
  1235. static const char *const list_ALTER2[] =
  1236. {"ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT",
  1237. "NO INHERIT", "RENAME", "RESET", "OWNER TO", "SET",
  1238. "VALIDATE CONSTRAINT", "REPLICA IDENTITY", NULL};
  1239. COMPLETE_WITH_LIST(list_ALTER2);
  1240. }
  1241. /* ALTER TABLE xxx ENABLE */
  1242. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1243. pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1244. pg_strcasecmp(prev_wd, "ENABLE") == 0)
  1245. {
  1246. static const char *const list_ALTERENABLE[] =
  1247. {"ALWAYS", "REPLICA", "RULE", "TRIGGER", NULL};
  1248. COMPLETE_WITH_LIST(list_ALTERENABLE);
  1249. }
  1250. else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1251. pg_strcasecmp(prev2_wd, "ENABLE") == 0 &&
  1252. (pg_strcasecmp(prev_wd, "REPLICA") == 0 ||
  1253. pg_strcasecmp(prev_wd, "ALWAYS") == 0))
  1254. {
  1255. static const char *const list_ALTERENABLE2[] =
  1256. {"RULE", "TRIGGER", NULL};
  1257. COMPLETE_WITH_LIST(list_ALTERENABLE2);
  1258. }
  1259. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1260. pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1261. pg_strcasecmp(prev2_wd, "ENABLE") == 0 &&
  1262. pg_strcasecmp(prev_wd, "RULE") == 0)
  1263. {
  1264. completion_info_charp = prev3_wd;
  1265. COMPLETE_WITH_QUERY(Query_for_rule_of_table);
  1266. }
  1267. else if (pg_strcasecmp(prev6_wd, "ALTER") == 0 &&
  1268. pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
  1269. pg_strcasecmp(prev3_wd, "ENABLE") == 0 &&
  1270. pg_strcasecmp(prev_wd, "RULE") == 0)
  1271. {
  1272. completion_info_charp = prev4_wd;
  1273. COMPLETE_WITH_QUERY(Query_for_rule_of_table);
  1274. }
  1275. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1276. pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1277. pg_strcasecmp(prev2_wd, "ENABLE") == 0 &&
  1278. pg_strcasecmp(prev_wd, "TRIGGER") == 0)
  1279. {
  1280. completion_info_charp = prev3_wd;
  1281. COMPLETE_WITH_QUERY(Query_for_trigger_of_table);
  1282. }
  1283. else if (pg_strcasecmp(prev6_wd, "ALTER") == 0 &&
  1284. pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
  1285. pg_strcasecmp(prev3_wd, "ENABLE") == 0 &&
  1286. pg_strcasecmp(prev_wd, "TRIGGER") == 0)
  1287. {
  1288. completion_info_charp = prev4_wd;
  1289. COMPLETE_WITH_QUERY(Query_for_trigger_of_table);
  1290. }
  1291. /* ALTER TABLE xxx INHERIT */
  1292. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1293. pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1294. pg_strcasecmp(prev_wd, "INHERIT") == 0)
  1295. {
  1296. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, "");
  1297. }
  1298. /* ALTER TABLE xxx NO INHERIT */
  1299. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1300. pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1301. pg_strcasecmp(prev2_wd, "NO") == 0 &&
  1302. pg_strcasecmp(prev_wd, "INHERIT") == 0)
  1303. {
  1304. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, "");
  1305. }
  1306. /* ALTER TABLE xxx DISABLE */
  1307. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1308. pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1309. pg_strcasecmp(prev_wd, "DISABLE") == 0)
  1310. {
  1311. static const char *const list_ALTERDISABLE[] =
  1312. {"RULE", "TRIGGER", NULL};
  1313. COMPLETE_WITH_LIST(list_ALTERDISABLE);
  1314. }
  1315. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1316. pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1317. pg_strcasecmp(prev2_wd, "DISABLE") == 0 &&
  1318. pg_strcasecmp(prev_wd, "RULE") == 0)
  1319. {
  1320. completion_info_charp = prev3_wd;
  1321. COMPLETE_WITH_QUERY(Query_for_rule_of_table);
  1322. }
  1323. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1324. pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1325. pg_strcasecmp(prev2_wd, "DISABLE") == 0 &&
  1326. pg_strcasecmp(prev_wd, "TRIGGER") == 0)
  1327. {
  1328. completion_info_charp = prev3_wd;
  1329. COMPLETE_WITH_QUERY(Query_for_trigger_of_table);
  1330. }
  1331. /* ALTER TABLE xxx ALTER */
  1332. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1333. pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1334. pg_strcasecmp(prev_wd, "ALTER") == 0)
  1335. COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN'");
  1336. /* ALTER TABLE xxx RENAME */
  1337. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1338. pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1339. pg_strcasecmp(prev_wd, "RENAME") == 0)
  1340. COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN' UNION SELECT 'CONSTRAINT' UNION SELECT 'TO'");
  1341. /*
  1342. * If we have TABLE <sth> ALTER COLUMN|RENAME COLUMN, provide list of
  1343. * columns
  1344. */
  1345. else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1346. (pg_strcasecmp(prev2_wd, "ALTER") == 0 ||
  1347. pg_strcasecmp(prev2_wd, "RENAME") == 0) &&
  1348. pg_strcasecmp(prev_wd, "COLUMN") == 0)
  1349. COMPLETE_WITH_ATTR(prev3_wd, "");
  1350. /* ALTER TABLE xxx RENAME yyy */
  1351. else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1352. pg_strcasecmp(prev2_wd, "RENAME") == 0 &&
  1353. pg_strcasecmp(prev_wd, "CONSTRAINT") != 0 &&
  1354. pg_strcasecmp(prev_wd, "TO") != 0)
  1355. COMPLETE_WITH_CONST("TO");
  1356. /* ALTER TABLE xxx RENAME COLUMN/CONSTRAINT yyy */
  1357. else if (pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
  1358. pg_strcasecmp(prev3_wd, "RENAME") == 0 &&
  1359. (pg_strcasecmp(prev2_wd, "COLUMN") == 0 ||
  1360. pg_strcasecmp(prev2_wd, "CONSTRAINT") == 0) &&
  1361. pg_strcasecmp(prev_wd, "TO") != 0)
  1362. COMPLETE_WITH_CONST("TO");
  1363. /* If we have TABLE <sth> DROP, provide COLUMN or CONSTRAINT */
  1364. else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1365. pg_strcasecmp(prev_wd, "DROP") == 0)
  1366. {
  1367. static const char *const list_TABLEDROP[] =
  1368. {"COLUMN", "CONSTRAINT", NULL};
  1369. COMPLETE_WITH_LIST(list_TABLEDROP);
  1370. }
  1371. /* If we have ALTER TABLE <sth> DROP COLUMN, provide list of columns */
  1372. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1373. pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1374. pg_strcasecmp(prev2_wd, "DROP") == 0 &&
  1375. pg_strcasecmp(prev_wd, "COLUMN") == 0)
  1376. COMPLETE_WITH_ATTR(prev3_wd, "");
  1377. /*
  1378. * If we have ALTER TABLE <sth> DROP|RENAME|VALIDATE CONSTRAINT, provide
  1379. * list of constraints
  1380. */
  1381. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1382. pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1383. (pg_strcasecmp(prev2_wd, "DROP") == 0 ||
  1384. pg_strcasecmp(prev2_wd, "RENAME") == 0 ||
  1385. pg_strcasecmp(prev2_wd, "VALIDATE") == 0) &&
  1386. pg_strcasecmp(prev_wd, "CONSTRAINT") == 0)
  1387. {
  1388. completion_info_charp = prev3_wd;
  1389. COMPLETE_WITH_QUERY(Query_for_constraint_of_table);
  1390. }
  1391. /* ALTER TABLE ALTER [COLUMN] <foo> */
  1392. else if ((pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1393. pg_strcasecmp(prev2_wd, "COLUMN") == 0) ||
  1394. (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1395. pg_strcasecmp(prev2_wd, "ALTER") == 0))
  1396. {
  1397. static const char *const list_COLUMNALTER[] =
  1398. {"TYPE", "SET", "RESET", "DROP", NULL};
  1399. COMPLETE_WITH_LIST(list_COLUMNALTER);
  1400. }
  1401. /* ALTER TABLE ALTER [COLUMN] <foo> SET */
  1402. else if (((pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1403. pg_strcasecmp(prev3_wd, "COLUMN") == 0) ||
  1404. (pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
  1405. pg_strcasecmp(prev3_wd, "ALTER") == 0)) &&
  1406. pg_strcasecmp(prev_wd, "SET") == 0)
  1407. {
  1408. static const char *const list_COLUMNSET[] =
  1409. {"(", "DEFAULT", "NOT NULL", "STATISTICS", "STORAGE", NULL};
  1410. COMPLETE_WITH_LIST(list_COLUMNSET);
  1411. }
  1412. /* ALTER TABLE ALTER [COLUMN] <foo> SET ( */
  1413. else if (((pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1414. pg_strcasecmp(prev4_wd, "COLUMN") == 0) ||
  1415. pg_strcasecmp(prev4_wd, "ALTER") == 0) &&
  1416. pg_strcasecmp(prev2_wd, "SET") == 0 &&
  1417. pg_strcasecmp(prev_wd, "(") == 0)
  1418. {
  1419. static const char *const list_COLUMNOPTIONS[] =
  1420. {"n_distinct", "n_distinct_inherited", NULL};
  1421. COMPLETE_WITH_LIST(list_COLUMNOPTIONS);
  1422. }
  1423. /* ALTER TABLE ALTER [COLUMN] <foo> SET STORAGE */
  1424. else if (((pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1425. pg_strcasecmp(prev4_wd, "COLUMN") == 0) ||
  1426. pg_strcasecmp(prev4_wd, "ALTER") == 0) &&
  1427. pg_strcasecmp(prev2_wd, "SET") == 0 &&
  1428. pg_strcasecmp(prev_wd, "STORAGE") == 0)
  1429. {
  1430. static const char *const list_COLUMNSTORAGE[] =
  1431. {"PLAIN", "EXTERNAL", "EXTENDED", "MAIN", NULL};
  1432. COMPLETE_WITH_LIST(list_COLUMNSTORAGE);
  1433. }
  1434. /* ALTER TABLE ALTER [COLUMN] <foo> DROP */
  1435. else if (((pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1436. pg_strcasecmp(prev3_wd, "COLUMN") == 0) ||
  1437. (pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
  1438. pg_strcasecmp(prev3_wd, "ALTER") == 0)) &&
  1439. pg_strcasecmp(prev_wd, "DROP") == 0)
  1440. {
  1441. static const char *const list_COLUMNDROP[] =
  1442. {"DEFAULT", "NOT NULL", NULL};
  1443. COMPLETE_WITH_LIST(list_COLUMNDROP);
  1444. }
  1445. else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1446. pg_strcasecmp(prev_wd, "CLUSTER") == 0)
  1447. COMPLETE_WITH_CONST("ON");
  1448. else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1449. pg_strcasecmp(prev2_wd, "CLUSTER") == 0 &&
  1450. pg_strcasecmp(prev_wd, "ON") == 0)
  1451. {
  1452. completion_info_charp = prev3_wd;
  1453. COMPLETE_WITH_QUERY(Query_for_index_of_table);
  1454. }
  1455. /* If we have TABLE <sth> SET, provide WITHOUT,TABLESPACE and SCHEMA */
  1456. else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1457. pg_strcasecmp(prev_wd, "SET") == 0)
  1458. {
  1459. static const char *const list_TABLESET[] =
  1460. {"(", "WITHOUT", "TABLESPACE", "SCHEMA", NULL};
  1461. COMPLETE_WITH_LIST(list_TABLESET);
  1462. }
  1463. /* If we have TABLE <sth> SET TABLESPACE provide a list of tablespaces */
  1464. else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1465. pg_strcasecmp(prev2_wd, "SET") == 0 &&
  1466. pg_strcasecmp(prev_wd, "TABLESPACE") == 0)
  1467. COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
  1468. /* If we have TABLE <sth> SET WITHOUT provide CLUSTER or OIDS */
  1469. else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1470. pg_strcasecmp(prev2_wd, "SET") == 0 &&
  1471. pg_strcasecmp(prev_wd, "WITHOUT") == 0)
  1472. {
  1473. static const char *const list_TABLESET2[] =
  1474. {"CLUSTER", "OIDS", NULL};
  1475. COMPLETE_WITH_LIST(list_TABLESET2);
  1476. }
  1477. /* ALTER TABLE <foo> RESET */
  1478. else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1479. pg_strcasecmp(prev_wd, "RESET") == 0)
  1480. COMPLETE_WITH_CONST("(");
  1481. /* ALTER TABLE <foo> SET|RESET ( */
  1482. else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1483. (pg_strcasecmp(prev2_wd, "SET") == 0 ||
  1484. pg_strcasecmp(prev2_wd, "RESET") == 0) &&
  1485. pg_strcasecmp(prev_wd, "(") == 0)
  1486. {
  1487. static const char *const list_TABLEOPTIONS[] =
  1488. {
  1489. "autovacuum_analyze_scale_factor",
  1490. "autovacuum_analyze_threshold",
  1491. "autovacuum_enabled",
  1492. "autovacuum_freeze_max_age",
  1493. "autovacuum_freeze_min_age",
  1494. "autovacuum_freeze_table_age",
  1495. "autovacuum_vacuum_cost_delay",
  1496. "autovacuum_vacuum_cost_limit",
  1497. "autovacuum_vacuum_scale_factor",
  1498. "autovacuum_vacuum_threshold",
  1499. "fillfactor",
  1500. "toast.autovacuum_enabled",
  1501. "toast.autovacuum_freeze_max_age",
  1502. "toast.autovacuum_freeze_min_age",
  1503. "toast.autovacuum_freeze_table_age",
  1504. "toast.autovacuum_vacuum_cost_delay",
  1505. "toast.autovacuum_vacuum_cost_limit",
  1506. "toast.autovacuum_vacuum_scale_factor",
  1507. "toast.autovacuum_vacuum_threshold",
  1508. NULL
  1509. };
  1510. COMPLETE_WITH_LIST(list_TABLEOPTIONS);
  1511. }
  1512. else if (pg_strcasecmp(prev4_wd, "REPLICA") == 0 &&
  1513. pg_strcasecmp(prev3_wd, "IDENTITY") == 0 &&
  1514. pg_strcasecmp(prev2_wd, "USING") == 0 &&
  1515. pg_strcasecmp(prev_wd, "INDEX") == 0)
  1516. {
  1517. completion_info_charp = prev5_wd;
  1518. COMPLETE_WITH_QUERY(Query_for_index_of_table);
  1519. }
  1520. else if (pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
  1521. pg_strcasecmp(prev3_wd, "REPLICA") == 0 &&
  1522. pg_strcasecmp(prev2_wd, "IDENTITY") == 0 &&
  1523. pg_strcasecmp(prev_wd, "USING") == 0)
  1524. {
  1525. COMPLETE_WITH_CONST("INDEX");
  1526. }
  1527. else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
  1528. pg_strcasecmp(prev2_wd, "REPLICA") == 0 &&
  1529. pg_strcasecmp(prev_wd, "IDENTITY") == 0)
  1530. {
  1531. static const char *const list_REPLICAID[] =
  1532. {"FULL", "NOTHING", "DEFAULT", "USING", NULL};
  1533. COMPLETE_WITH_LIST(list_REPLICAID);
  1534. }
  1535. else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  1536. pg_strcasecmp(prev_wd, "REPLICA") == 0)
  1537. {
  1538. COMPLETE_WITH_CONST("IDENTITY");
  1539. }
  1540. /* ALTER TABLESPACE <foo> with RENAME TO, OWNER TO, SET, RESET, MOVE */
  1541. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1542. pg_strcasecmp(prev2_wd, "TABLESPACE") == 0)
  1543. {
  1544. static const char *const list_ALTERTSPC[] =
  1545. {"RENAME TO", "OWNER TO", "SET", "RESET", "MOVE", NULL};
  1546. COMPLETE_WITH_LIST(list_ALTERTSPC);
  1547. }
  1548. /* ALTER TABLESPACE <foo> SET|RESET */
  1549. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1550. pg_strcasecmp(prev3_wd, "TABLESPACE") == 0 &&
  1551. (pg_strcasecmp(prev_wd, "SET") == 0 ||
  1552. pg_strcasecmp(prev_wd, "RESET") == 0))
  1553. COMPLETE_WITH_CONST("(");
  1554. /* ALTER TABLESPACE <foo> SET|RESET ( */
  1555. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1556. pg_strcasecmp(prev4_wd, "TABLESPACE") == 0 &&
  1557. (pg_strcasecmp(prev2_wd, "SET") == 0 ||
  1558. pg_strcasecmp(prev2_wd, "RESET") == 0) &&
  1559. pg_strcasecmp(prev_wd, "(") == 0)
  1560. {
  1561. static const char *const list_TABLESPACEOPTIONS[] =
  1562. {"seq_page_cost", "random_page_cost", NULL};
  1563. COMPLETE_WITH_LIST(list_TABLESPACEOPTIONS);
  1564. }
  1565. /* ALTER TABLESPACE <foo> MOVE ALL|TABLES|INDEXES|MATERIALIZED VIEWS */
  1566. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1567. pg_strcasecmp(prev3_wd, "TABLESPACE") == 0 &&
  1568. pg_strcasecmp(prev_wd, "MOVE") == 0)
  1569. {
  1570. static const char *const list_TABLESPACEMOVETARGETS[] =
  1571. {"ALL", "TABLES", "INDEXES", "MATERIALIZED VIEWS", NULL};
  1572. COMPLETE_WITH_LIST(list_TABLESPACEMOVETARGETS);
  1573. }
  1574. else if ((pg_strcasecmp(prev4_wd, "TABLESPACE") == 0 &&
  1575. pg_strcasecmp(prev2_wd, "MOVE") == 0) ||
  1576. (pg_strcasecmp(prev5_wd, "TABLESPACE") == 0 &&
  1577. pg_strcasecmp(prev3_wd, "MOVE") == 0 &&
  1578. pg_strcasecmp(prev2_wd, "MATERIALIZED") == 0))
  1579. {
  1580. static const char *const list_TABLESPACEMOVEOPTIONS[] =
  1581. {"OWNED BY", "TO", NULL};
  1582. COMPLETE_WITH_LIST(list_TABLESPACEMOVEOPTIONS);
  1583. }
  1584. /* ALTER TEXT SEARCH */
  1585. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1586. pg_strcasecmp(prev2_wd, "TEXT") == 0 &&
  1587. pg_strcasecmp(prev_wd, "SEARCH") == 0)
  1588. {
  1589. static const char *const list_ALTERTEXTSEARCH[] =
  1590. {"CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE", NULL};
  1591. COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH);
  1592. }
  1593. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1594. pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
  1595. pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
  1596. (pg_strcasecmp(prev2_wd, "TEMPLATE") == 0 ||
  1597. pg_strcasecmp(prev2_wd, "PARSER") == 0))
  1598. {
  1599. static const char *const list_ALTERTEXTSEARCH2[] =
  1600. {"RENAME TO", "SET SCHEMA", NULL};
  1601. COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH2);
  1602. }
  1603. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1604. pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
  1605. pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
  1606. pg_strcasecmp(prev2_wd, "DICTIONARY") == 0)
  1607. {
  1608. static const char *const list_ALTERTEXTSEARCH3[] =
  1609. {"OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
  1610. COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH3);
  1611. }
  1612. else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
  1613. pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
  1614. pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
  1615. pg_strcasecmp(prev2_wd, "CONFIGURATION") == 0)
  1616. {
  1617. static const char *const list_ALTERTEXTSEARCH4[] =
  1618. {"ADD MAPPING FOR", "ALTER MAPPING", "DROP MAPPING FOR", "OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
  1619. COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH4);
  1620. }
  1621. /* complete ALTER TYPE <foo> with actions */
  1622. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1623. pg_strcasecmp(prev2_wd, "TYPE") == 0)
  1624. {
  1625. static const char *const list_ALTERTYPE[] =
  1626. {"ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE", "DROP ATTRIBUTE",
  1627. "OWNER TO", "RENAME", "SET SCHEMA", NULL};
  1628. COMPLETE_WITH_LIST(list_ALTERTYPE);
  1629. }
  1630. /* complete ALTER TYPE <foo> ADD with actions */
  1631. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1632. pg_strcasecmp(prev3_wd, "TYPE") == 0 &&
  1633. pg_strcasecmp(prev_wd, "ADD") == 0)
  1634. {
  1635. static const char *const list_ALTERTYPE[] =
  1636. {"ATTRIBUTE", "VALUE", NULL};
  1637. COMPLETE_WITH_LIST(list_ALTERTYPE);
  1638. }
  1639. /* ALTER TYPE <foo> RENAME */
  1640. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1641. pg_strcasecmp(prev3_wd, "TYPE") == 0 &&
  1642. pg_strcasecmp(prev_wd, "RENAME") == 0)
  1643. {
  1644. static const char *const list_ALTERTYPE[] =
  1645. {"ATTRIBUTE", "TO", NULL};
  1646. COMPLETE_WITH_LIST(list_ALTERTYPE);
  1647. }
  1648. /* ALTER TYPE xxx RENAME ATTRIBUTE yyy */
  1649. else if (pg_strcasecmp(prev5_wd, "TYPE") == 0 &&
  1650. pg_strcasecmp(prev3_wd, "RENAME") == 0 &&
  1651. pg_strcasecmp(prev2_wd, "ATTRIBUTE") == 0)
  1652. COMPLETE_WITH_CONST("TO");
  1653. /*
  1654. * If we have TYPE <sth> ALTER/DROP/RENAME ATTRIBUTE, provide list of
  1655. * attributes
  1656. */
  1657. else if (pg_strcasecmp(prev4_wd, "TYPE") == 0 &&
  1658. (pg_strcasecmp(prev2_wd, "ALTER") == 0 ||
  1659. pg_strcasecmp(prev2_wd, "DROP") == 0 ||
  1660. pg_strcasecmp(prev2_wd, "RENAME") == 0) &&
  1661. pg_strcasecmp(prev_wd, "ATTRIBUTE") == 0)
  1662. COMPLETE_WITH_ATTR(prev3_wd, "");
  1663. /* ALTER TYPE ALTER ATTRIBUTE <foo> */
  1664. else if ((pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1665. pg_strcasecmp(prev2_wd, "ATTRIBUTE") == 0))
  1666. {
  1667. COMPLETE_WITH_CONST("TYPE");
  1668. }
  1669. /* complete ALTER GROUP <foo> */
  1670. else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
  1671. pg_strcasecmp(prev2_wd, "GROUP") == 0)
  1672. {
  1673. static const char *const list_ALTERGROUP[] =
  1674. {"ADD USER", "DROP USER", "RENAME TO", NULL};
  1675. COMPLETE_WITH_LIST(list_ALTERGROUP);
  1676. }
  1677. /* complete ALTER GROUP <foo> ADD|DROP with USER */
  1678. else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
  1679. pg_strcasecmp(prev3_wd, "GROUP") == 0 &&
  1680. (pg_strcasecmp(prev_wd, "ADD") == 0 ||
  1681. pg_strcasecmp(prev_wd, "DROP") == 0))
  1682. COMPLETE_WITH_CONST("USER");
  1683. /* complete {ALTER} GROUP <foo> ADD|DROP USER with a user name */
  1684. else if (pg_strcasecmp(prev4_wd, "GROUP") == 0 &&
  1685. (pg_strcasecmp(prev2_wd, "ADD") == 0 ||
  1686. pg_strcasecmp(prev2_wd, "DROP") == 0) &&
  1687. pg_strcasecmp(prev_wd, "USER") == 0)
  1688. COMPLETE_WITH_QUERY(Query_for_list_of_roles);
  1689. /* BEGIN, END, ABORT */
  1690. else if (pg_strcasecmp(prev_wd, "BEGIN") == 0 ||
  1691. pg_strcasecmp(prev_wd, "END") == 0 ||
  1692. pg_strcasecmp(prev_wd, "ABORT") == 0)
  1693. {
  1694. static const char *const list_TRANS[] =
  1695. {"WORK", "TRANSACTION", NULL};
  1696. COMPLETE_WITH_LIST(list_TRANS);
  1697. }
  1698. /* COMMIT */
  1699. else if (pg_strcasecmp(prev_wd, "COMMIT") == 0)
  1700. {
  1701. static const char *const list_COMMIT[] =
  1702. {"WORK", "TRANSACTION", "PREPARED", NULL};
  1703. COMPLETE_WITH_LIST(list_COMMIT);
  1704. }
  1705. /* RELEASE SAVEPOINT */
  1706. else if (pg_strcasecmp(prev_wd, "RELEASE") == 0)
  1707. COMPLETE_WITH_CONST("SAVEPOINT");
  1708. /* ROLLBACK*/
  1709. else if (pg_strcasecmp(prev_wd, "ROLLBACK") == 0)
  1710. {
  1711. static const char *const list_TRANS[] =
  1712. {"WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED", NULL};
  1713. COMPLETE_WITH_LIST(list_TRANS);
  1714. }
  1715. /* CLUSTER */
  1716. /*
  1717. * If the previous word is CLUSTER and not WITHOUT produce list of tables
  1718. */
  1719. else if (pg_strcasecmp(prev_wd, "CLUSTER") == 0 &&
  1720. pg_strcasecmp(prev2_wd, "WITHOUT") != 0)
  1721. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, "UNION SELECT 'VERBOSE'");
  1722. /*
  1723. * If the previous words are CLUSTER VERBOSE produce list of tables
  1724. */
  1725. else if (pg_strcasecmp(prev_wd, "VERBOSE") == 0 &&
  1726. pg_strcasecmp(prev2_wd, "CLUSTER") == 0)
  1727. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL);
  1728. /* If we have CLUSTER <sth>, then add "USING" */
  1729. else if (pg_strcasecmp(prev2_wd, "CLUSTER") == 0 &&
  1730. pg_strcasecmp(prev_wd, "ON") != 0 &&
  1731. pg_strcasecmp(prev_wd, "VERBOSE") != 0)
  1732. {
  1733. COMPLETE_WITH_CONST("USING");
  1734. }
  1735. /* If we have CLUSTER VERBOSE <sth>, then add "USING" */
  1736. else if (pg_strcasecmp(prev3_wd, "CLUSTER") == 0 &&
  1737. pg_strcasecmp(prev2_wd, "VERBOSE") == 0)
  1738. {
  1739. COMPLETE_WITH_CONST("USING");
  1740. }
  1741. /*
  1742. * If we have CLUSTER <sth> USING, then add the index as well.
  1743. */
  1744. else if (pg_strcasecmp(prev3_wd, "CLUSTER") == 0 &&
  1745. pg_strcasecmp(prev_wd, "USING") == 0)
  1746. {
  1747. completion_info_charp = prev2_wd;
  1748. COMPLETE_WITH_QUERY(Query_for_index_of_table);
  1749. }
  1750. /*
  1751. * If we have CLUSTER VERBOSE <sth> USING, then add the index as well.
  1752. */
  1753. else if (pg_strcasecmp(prev4_wd, "CLUSTER") == 0 &&
  1754. pg_strcasecmp(prev3_wd, "VERBOSE") == 0 &&
  1755. pg_strcasecmp(prev_wd, "USING") == 0)
  1756. {
  1757. completion_info_charp = prev2_wd;
  1758. COMPLETE_WITH_QUERY(Query_for_index_of_table);
  1759. }
  1760. /* COMMENT */
  1761. else if (pg_strcasecmp(prev_wd, "COMMENT") == 0)
  1762. COMPLETE_WITH_CONST("ON");
  1763. else if (pg_strcasecmp(prev2_wd, "COMMENT") == 0 &&
  1764. pg_strcasecmp(prev_wd, "ON") == 0)
  1765. {
  1766. static const char *const list_COMMENT[] =
  1767. {"CAST", "COLLATION", "CONVERSION", "DATABASE", "EVENT TRIGGER", "EXTENSION",
  1768. "FOREIGN DATA WRAPPER", "FOREIGN TABLE",
  1769. "SERVER", "INDEX", "LANGUAGE", "RULE", "SCHEMA", "SEQUENCE",
  1770. "TABLE", "TYPE", "VIEW", "MATERIALIZED VIEW", "COLUMN", "AGGREGATE", "FUNCTION",
  1771. "OPERATOR", "TRIGGER", "CONSTRAINT", "DOMAIN", "LARGE OBJECT",
  1772. "TABLESPACE", "TEXT SEARCH", "ROLE", NULL};
  1773. COMPLETE_WITH_LIST(list_COMMENT);
  1774. }
  1775. else if (pg_strcasecmp(prev3_wd, "COMMENT") == 0 &&
  1776. pg_strcasecmp(prev2_wd, "ON") == 0 &&
  1777. pg_strcasecmp(prev_wd, "FOREIGN") == 0)
  1778. {
  1779. static const char *const list_TRANS2[] =
  1780. {"DATA WRAPPER", "TABLE", NULL};
  1781. COMPLETE_WITH_LIST(list_TRANS2);
  1782. }
  1783. else if (pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
  1784. pg_strcasecmp(prev3_wd, "ON") == 0 &&
  1785. pg_strcasecmp(prev2_wd, "TEXT") == 0 &&
  1786. pg_strcasecmp(prev_wd, "SEARCH") == 0)
  1787. {
  1788. static const char *const list_TRANS2[] =
  1789. {"CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE", NULL};
  1790. COMPLETE_WITH_LIST(list_TRANS2);
  1791. }
  1792. else if (pg_strcasecmp(prev3_wd, "COMMENT") == 0 &&
  1793. pg_strcasecmp(prev2_wd, "ON") == 0 &&
  1794. pg_strcasecmp(prev_wd, "CONSTRAINT") == 0)
  1795. {
  1796. COMPLETE_WITH_QUERY(Query_for_all_table_constraints);
  1797. }
  1798. else if (pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
  1799. pg_strcasecmp(prev3_wd, "ON") == 0 &&
  1800. pg_strcasecmp(prev2_wd, "CONSTRAINT") == 0)
  1801. {
  1802. COMPLETE_WITH_CONST("ON");
  1803. }
  1804. else if (pg_strcasecmp(prev5_wd, "COMMENT") == 0 &&
  1805. pg_strcasecmp(prev4_wd, "ON") == 0 &&
  1806. pg_strcasecmp(prev3_wd, "CONSTRAINT") == 0 &&
  1807. pg_strcasecmp(prev_wd, "ON") == 0)
  1808. {
  1809. completion_info_charp = prev2_wd;
  1810. COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_constraint);
  1811. }
  1812. else if (pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
  1813. pg_strcasecmp(prev3_wd, "ON") == 0 &&
  1814. pg_strcasecmp(prev2_wd, "MATERIALIZED") == 0 &&
  1815. pg_strcasecmp(prev_wd, "VIEW") == 0)
  1816. {
  1817. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL);
  1818. }
  1819. else if (pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
  1820. pg_strcasecmp(prev3_wd, "ON") == 0 &&
  1821. pg_strcasecmp(prev2_wd, "EVENT") == 0 &&
  1822. pg_strcasecmp(prev_wd, "TRIGGER") == 0)
  1823. {
  1824. COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
  1825. }
  1826. else if ((pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
  1827. pg_strcasecmp(prev3_wd, "ON") == 0) ||
  1828. (pg_strcasecmp(prev5_wd, "COMMENT") == 0 &&
  1829. pg_strcasecmp(prev4_wd, "ON") == 0) ||
  1830. (pg_strcasecmp(prev6_wd, "COMMENT") == 0 &&
  1831. pg_strcasecmp(prev5_wd, "ON") == 0))
  1832. COMPLETE_WITH_CONST("IS");
  1833. /* COPY */
  1834. /*
  1835. * If we have COPY [BINARY] (which you'd have to type yourself), offer
  1836. * list of tables (Also cover the analogous backslash command)
  1837. */
  1838. else if (pg_strcasecmp(prev_wd, "COPY") == 0 ||
  1839. pg_strcasecmp(prev_wd, "\\copy") == 0 ||
  1840. (pg_strcasecmp(prev2_wd, "COPY") == 0 &&
  1841. pg_strcasecmp(prev_wd, "BINARY") == 0))
  1842. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
  1843. /* If we have COPY|BINARY <sth>, complete it with "TO" or "FROM" */
  1844. else if (pg_strcasecmp(prev2_wd, "COPY") == 0 ||
  1845. pg_strcasecmp(prev2_wd, "\\copy") == 0 ||
  1846. pg_strcasecmp(prev2_wd, "BINARY") == 0)
  1847. {
  1848. static const char *const list_FROMTO[] =
  1849. {"FROM", "TO", NULL};
  1850. COMPLETE_WITH_LIST(list_FROMTO);
  1851. }
  1852. /* If we have COPY|BINARY <sth> FROM|TO, complete with filename */
  1853. else if ((pg_strcasecmp(prev3_wd, "COPY") == 0 ||
  1854. pg_strcasecmp(prev3_wd, "\\copy") == 0 ||
  1855. pg_strcasecmp(prev3_wd, "BINARY") == 0) &&
  1856. (pg_strcasecmp(prev_wd, "FROM") == 0 ||
  1857. pg_strcasecmp(prev_wd, "TO") == 0))
  1858. {
  1859. completion_charp = "";
  1860. matches = completion_matches(text, complete_from_files);
  1861. }
  1862. /* Handle COPY|BINARY <sth> FROM|TO filename */
  1863. else if ((pg_strcasecmp(prev4_wd, "COPY") == 0 ||
  1864. pg_strcasecmp(prev4_wd, "\\copy") == 0 ||
  1865. pg_strcasecmp(prev4_wd, "BINARY") == 0) &&
  1866. (pg_strcasecmp(prev2_wd, "FROM") == 0 ||
  1867. pg_strcasecmp(prev2_wd, "TO") == 0))
  1868. {
  1869. static const char *const list_COPY[] =
  1870. {"BINARY", "OIDS", "DELIMITER", "NULL", "CSV", "ENCODING", NULL};
  1871. COMPLETE_WITH_LIST(list_COPY);
  1872. }
  1873. /* Handle COPY|BINARY <sth> FROM|TO filename CSV */
  1874. else if (pg_strcasecmp(prev_wd, "CSV") == 0 &&
  1875. (pg_strcasecmp(prev3_wd, "FROM") == 0 ||
  1876. pg_strcasecmp(prev3_wd, "TO") == 0))
  1877. {
  1878. static const char *const list_CSV[] =
  1879. {"HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", "FORCE NOT NULL", NULL};
  1880. COMPLETE_WITH_LIST(list_CSV);
  1881. }
  1882. /* CREATE DATABASE */
  1883. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  1884. pg_strcasecmp(prev2_wd, "DATABASE") == 0)
  1885. {
  1886. static const char *const list_DATABASE[] =
  1887. {"OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", "IS_TEMPLATE",
  1888. "ALLOW_CONNECTIONS", "CONNECTION LIMIT", "LC_COLLATE", "LC_CTYPE",
  1889. NULL};
  1890. COMPLETE_WITH_LIST(list_DATABASE);
  1891. }
  1892. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  1893. pg_strcasecmp(prev3_wd, "DATABASE") == 0 &&
  1894. pg_strcasecmp(prev_wd, "TEMPLATE") == 0)
  1895. COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
  1896. /* CREATE EXTENSION */
  1897. /* Complete with available extensions rather than installed ones. */
  1898. else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
  1899. pg_strcasecmp(prev_wd, "EXTENSION") == 0)
  1900. COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions);
  1901. /* CREATE EXTENSION <name> */
  1902. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  1903. pg_strcasecmp(prev2_wd, "EXTENSION") == 0)
  1904. COMPLETE_WITH_CONST("WITH SCHEMA");
  1905. /* CREATE FOREIGN */
  1906. else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
  1907. pg_strcasecmp(prev_wd, "FOREIGN") == 0)
  1908. {
  1909. static const char *const list_CREATE_FOREIGN[] =
  1910. {"DATA WRAPPER", "TABLE", NULL};
  1911. COMPLETE_WITH_LIST(list_CREATE_FOREIGN);
  1912. }
  1913. /* CREATE FOREIGN DATA WRAPPER */
  1914. else if (pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
  1915. pg_strcasecmp(prev4_wd, "FOREIGN") == 0 &&
  1916. pg_strcasecmp(prev3_wd, "DATA") == 0 &&
  1917. pg_strcasecmp(prev2_wd, "WRAPPER") == 0)
  1918. {
  1919. static const char *const list_CREATE_FOREIGN_DATA_WRAPPER[] =
  1920. {"HANDLER", "VALIDATOR", NULL};
  1921. COMPLETE_WITH_LIST(list_CREATE_FOREIGN_DATA_WRAPPER);
  1922. }
  1923. /* CREATE INDEX */
  1924. /* First off we complete CREATE UNIQUE with "INDEX" */
  1925. else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
  1926. pg_strcasecmp(prev_wd, "UNIQUE") == 0)
  1927. COMPLETE_WITH_CONST("INDEX");
  1928. /* If we have CREATE|UNIQUE INDEX, then add "ON" and existing indexes */
  1929. else if (pg_strcasecmp(prev_wd, "INDEX") == 0 &&
  1930. (pg_strcasecmp(prev2_wd, "CREATE") == 0 ||
  1931. pg_strcasecmp(prev2_wd, "UNIQUE") == 0))
  1932. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes,
  1933. " UNION SELECT 'ON'"
  1934. " UNION SELECT 'CONCURRENTLY'");
  1935. /* Complete ... INDEX [<name>] ON with a list of tables */
  1936. else if ((pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
  1937. pg_strcasecmp(prev2_wd, "INDEX") == 0 ||
  1938. pg_strcasecmp(prev2_wd, "CONCURRENTLY") == 0) &&
  1939. pg_strcasecmp(prev_wd, "ON") == 0)
  1940. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL);
  1941. /* If we have CREATE|UNIQUE INDEX <sth> CONCURRENTLY, then add "ON" */
  1942. else if ((pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
  1943. pg_strcasecmp(prev2_wd, "INDEX") == 0) &&
  1944. pg_strcasecmp(prev_wd, "CONCURRENTLY") == 0)
  1945. COMPLETE_WITH_CONST("ON");
  1946. /* If we have CREATE|UNIQUE INDEX <sth>, then add "ON" or "CONCURRENTLY" */
  1947. else if ((pg_strcasecmp(prev3_wd, "CREATE") == 0 ||
  1948. pg_strcasecmp(prev3_wd, "UNIQUE") == 0) &&
  1949. pg_strcasecmp(prev2_wd, "INDEX") == 0)
  1950. {
  1951. static const char *const list_CREATE_INDEX[] =
  1952. {"CONCURRENTLY", "ON", NULL};
  1953. COMPLETE_WITH_LIST(list_CREATE_INDEX);
  1954. }
  1955. /*
  1956. * Complete INDEX <name> ON <table> with a list of table columns (which
  1957. * should really be in parens)
  1958. */
  1959. else if ((pg_strcasecmp(prev4_wd, "INDEX") == 0 ||
  1960. pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
  1961. pg_strcasecmp(prev3_wd, "CONCURRENTLY") == 0) &&
  1962. pg_strcasecmp(prev2_wd, "ON") == 0)
  1963. {
  1964. static const char *const list_CREATE_INDEX2[] =
  1965. {"(", "USING", NULL};
  1966. COMPLETE_WITH_LIST(list_CREATE_INDEX2);
  1967. }
  1968. else if ((pg_strcasecmp(prev5_wd, "INDEX") == 0 ||
  1969. pg_strcasecmp(prev4_wd, "INDEX") == 0 ||
  1970. pg_strcasecmp(prev4_wd, "CONCURRENTLY") == 0) &&
  1971. pg_strcasecmp(prev3_wd, "ON") == 0 &&
  1972. pg_strcasecmp(prev_wd, "(") == 0)
  1973. COMPLETE_WITH_ATTR(prev2_wd, "");
  1974. /* same if you put in USING */
  1975. else if (pg_strcasecmp(prev5_wd, "ON") == 0 &&
  1976. pg_strcasecmp(prev3_wd, "USING") == 0 &&
  1977. pg_strcasecmp(prev_wd, "(") == 0)
  1978. COMPLETE_WITH_ATTR(prev4_wd, "");
  1979. /* Complete USING with an index method */
  1980. else if (pg_strcasecmp(prev_wd, "USING") == 0)
  1981. COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
  1982. else if (pg_strcasecmp(prev4_wd, "ON") == 0 &&
  1983. pg_strcasecmp(prev2_wd, "USING") == 0)
  1984. COMPLETE_WITH_CONST("(");
  1985. /* CREATE RULE */
  1986. /* Complete "CREATE RULE <sth>" with "AS" */
  1987. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  1988. pg_strcasecmp(prev2_wd, "RULE") == 0)
  1989. COMPLETE_WITH_CONST("AS");
  1990. /* Complete "CREATE RULE <sth> AS with "ON" */
  1991. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  1992. pg_strcasecmp(prev3_wd, "RULE") == 0 &&
  1993. pg_strcasecmp(prev_wd, "AS") == 0)
  1994. COMPLETE_WITH_CONST("ON");
  1995. /* Complete "RULE * AS ON" with SELECT|UPDATE|DELETE|INSERT */
  1996. else if (pg_strcasecmp(prev4_wd, "RULE") == 0 &&
  1997. pg_strcasecmp(prev2_wd, "AS") == 0 &&
  1998. pg_strcasecmp(prev_wd, "ON") == 0)
  1999. {
  2000. static const char *const rule_events[] =
  2001. {"SELECT", "UPDATE", "INSERT", "DELETE", NULL};
  2002. COMPLETE_WITH_LIST(rule_events);
  2003. }
  2004. /* Complete "AS ON <sth with a 'T' :)>" with a "TO" */
  2005. else if (pg_strcasecmp(prev3_wd, "AS") == 0 &&
  2006. pg_strcasecmp(prev2_wd, "ON") == 0 &&
  2007. (pg_toupper((unsigned char) prev_wd[4]) == 'T' ||
  2008. pg_toupper((unsigned char) prev_wd[5]) == 'T'))
  2009. COMPLETE_WITH_CONST("TO");
  2010. /* Complete "AS ON <sth> TO" with a table name */
  2011. else if (pg_strcasecmp(prev4_wd, "AS") == 0 &&
  2012. pg_strcasecmp(prev3_wd, "ON") == 0 &&
  2013. pg_strcasecmp(prev_wd, "TO") == 0)
  2014. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
  2015. /* CREATE SERVER <name> */
  2016. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  2017. pg_strcasecmp(prev2_wd, "SERVER") == 0)
  2018. {
  2019. static const char *const list_CREATE_SERVER[] =
  2020. {"TYPE", "VERSION", "FOREIGN DATA WRAPPER", NULL};
  2021. COMPLETE_WITH_LIST(list_CREATE_SERVER);
  2022. }
  2023. /* CREATE TABLE */
  2024. /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */
  2025. else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
  2026. (pg_strcasecmp(prev_wd, "TEMP") == 0 ||
  2027. pg_strcasecmp(prev_wd, "TEMPORARY") == 0))
  2028. {
  2029. static const char *const list_TEMP[] =
  2030. {"SEQUENCE", "TABLE", "VIEW", NULL};
  2031. COMPLETE_WITH_LIST(list_TEMP);
  2032. }
  2033. /* Complete "CREATE UNLOGGED" with TABLE */
  2034. else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
  2035. pg_strcasecmp(prev_wd, "UNLOGGED") == 0)
  2036. {
  2037. static const char *const list_UNLOGGED[] =
  2038. {"TABLE", "MATERIALIZED VIEW", NULL};
  2039. COMPLETE_WITH_LIST(list_UNLOGGED);
  2040. }
  2041. /* CREATE TABLESPACE */
  2042. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  2043. pg_strcasecmp(prev2_wd, "TABLESPACE") == 0)
  2044. {
  2045. static const char *const list_CREATETABLESPACE[] =
  2046. {"OWNER", "LOCATION", NULL};
  2047. COMPLETE_WITH_LIST(list_CREATETABLESPACE);
  2048. }
  2049. /* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */
  2050. else if (pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
  2051. pg_strcasecmp(prev4_wd, "TABLESPACE") == 0 &&
  2052. pg_strcasecmp(prev2_wd, "OWNER") == 0)
  2053. {
  2054. COMPLETE_WITH_CONST("LOCATION");
  2055. }
  2056. /* CREATE TEXT SEARCH */
  2057. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  2058. pg_strcasecmp(prev2_wd, "TEXT") == 0 &&
  2059. pg_strcasecmp(prev_wd, "SEARCH") == 0)
  2060. {
  2061. static const char *const list_CREATETEXTSEARCH[] =
  2062. {"CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE", NULL};
  2063. COMPLETE_WITH_LIST(list_CREATETEXTSEARCH);
  2064. }
  2065. else if (pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
  2066. pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
  2067. pg_strcasecmp(prev2_wd, "CONFIGURATION") == 0)
  2068. COMPLETE_WITH_CONST("(");
  2069. /* CREATE TRIGGER */
  2070. /* complete CREATE TRIGGER <name> with BEFORE,AFTER */
  2071. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  2072. pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
  2073. {
  2074. static const char *const list_CREATETRIGGER[] =
  2075. {"BEFORE", "AFTER", "INSTEAD OF", NULL};
  2076. COMPLETE_WITH_LIST(list_CREATETRIGGER);
  2077. }
  2078. /* complete CREATE TRIGGER <name> BEFORE,AFTER with an event */
  2079. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  2080. pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
  2081. (pg_strcasecmp(prev_wd, "BEFORE") == 0 ||
  2082. pg_strcasecmp(prev_wd, "AFTER") == 0))
  2083. {
  2084. static const char *const list_CREATETRIGGER_EVENTS[] =
  2085. {"INSERT", "DELETE", "UPDATE", "TRUNCATE", NULL};
  2086. COMPLETE_WITH_LIST(list_CREATETRIGGER_EVENTS);
  2087. }
  2088. /* complete CREATE TRIGGER <name> INSTEAD OF with an event */
  2089. else if (pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
  2090. pg_strcasecmp(prev4_wd, "TRIGGER") == 0 &&
  2091. pg_strcasecmp(prev2_wd, "INSTEAD") == 0 &&
  2092. pg_strcasecmp(prev_wd, "OF") == 0)
  2093. {
  2094. static const char *const list_CREATETRIGGER_EVENTS[] =
  2095. {"INSERT", "DELETE", "UPDATE", NULL};
  2096. COMPLETE_WITH_LIST(list_CREATETRIGGER_EVENTS);
  2097. }
  2098. /* complete CREATE TRIGGER <name> BEFORE,AFTER sth with OR,ON */
  2099. else if ((pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
  2100. pg_strcasecmp(prev4_wd, "TRIGGER") == 0 &&
  2101. (pg_strcasecmp(prev2_wd, "BEFORE") == 0 ||
  2102. pg_strcasecmp(prev2_wd, "AFTER") == 0)) ||
  2103. (pg_strcasecmp(prev5_wd, "TRIGGER") == 0 &&
  2104. pg_strcasecmp(prev3_wd, "INSTEAD") == 0 &&
  2105. pg_strcasecmp(prev2_wd, "OF") == 0))
  2106. {
  2107. static const char *const list_CREATETRIGGER2[] =
  2108. {"ON", "OR", NULL};
  2109. COMPLETE_WITH_LIST(list_CREATETRIGGER2);
  2110. }
  2111. /*
  2112. * complete CREATE TRIGGER <name> BEFORE,AFTER event ON with a list of
  2113. * tables
  2114. */
  2115. else if (pg_strcasecmp(prev5_wd, "TRIGGER") == 0 &&
  2116. (pg_strcasecmp(prev3_wd, "BEFORE") == 0 ||
  2117. pg_strcasecmp(prev3_wd, "AFTER") == 0) &&
  2118. pg_strcasecmp(prev_wd, "ON") == 0)
  2119. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
  2120. /* complete CREATE TRIGGER ... INSTEAD OF event ON with a list of views */
  2121. else if (pg_strcasecmp(prev4_wd, "INSTEAD") == 0 &&
  2122. pg_strcasecmp(prev3_wd, "OF") == 0 &&
  2123. pg_strcasecmp(prev_wd, "ON") == 0)
  2124. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL);
  2125. /* complete CREATE TRIGGER ... EXECUTE with PROCEDURE */
  2126. else if (pg_strcasecmp(prev_wd, "EXECUTE") == 0 &&
  2127. prev2_wd[0] != '\0')
  2128. COMPLETE_WITH_CONST("PROCEDURE");
  2129. /* CREATE ROLE,USER,GROUP <name> */
  2130. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  2131. !(pg_strcasecmp(prev2_wd, "USER") == 0 && pg_strcasecmp(prev_wd, "MAPPING") == 0) &&
  2132. (pg_strcasecmp(prev2_wd, "ROLE") == 0 ||
  2133. pg_strcasecmp(prev2_wd, "GROUP") == 0 || pg_strcasecmp(prev2_wd, "USER") == 0))
  2134. {
  2135. static const char *const list_CREATEROLE[] =
  2136. {"ADMIN", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER",
  2137. "ENCRYPTED", "IN", "INHERIT", "LOGIN", "NOCREATEDB",
  2138. "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", "NOLOGIN",
  2139. "NOREPLICATION", "NOSUPERUSER", "REPLICATION", "ROLE",
  2140. "SUPERUSER", "SYSID", "UNENCRYPTED", "VALID UNTIL", "WITH", NULL};
  2141. COMPLETE_WITH_LIST(list_CREATEROLE);
  2142. }
  2143. /* CREATE ROLE,USER,GROUP <name> WITH */
  2144. else if ((pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  2145. (pg_strcasecmp(prev3_wd, "ROLE") == 0 ||
  2146. pg_strcasecmp(prev3_wd, "GROUP") == 0 ||
  2147. pg_strcasecmp(prev3_wd, "USER") == 0) &&
  2148. pg_strcasecmp(prev_wd, "WITH") == 0))
  2149. {
  2150. /* Similar to the above, but don't complete "WITH" again. */
  2151. static const char *const list_CREATEROLE_WITH[] =
  2152. {"ADMIN", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER",
  2153. "ENCRYPTED", "IN", "INHERIT", "LOGIN", "NOCREATEDB",
  2154. "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", "NOLOGIN",
  2155. "NOREPLICATION", "NOSUPERUSER", "REPLICATION", "ROLE",
  2156. "SUPERUSER", "SYSID", "UNENCRYPTED", "VALID UNTIL", NULL};
  2157. COMPLETE_WITH_LIST(list_CREATEROLE_WITH);
  2158. }
  2159. /*
  2160. * complete CREATE ROLE,USER,GROUP <name> ENCRYPTED,UNENCRYPTED with
  2161. * PASSWORD
  2162. */
  2163. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  2164. (pg_strcasecmp(prev3_wd, "ROLE") == 0 ||
  2165. pg_strcasecmp(prev3_wd, "GROUP") == 0 || pg_strcasecmp(prev3_wd, "USER") == 0) &&
  2166. (pg_strcasecmp(prev_wd, "ENCRYPTED") == 0 || pg_strcasecmp(prev_wd, "UNENCRYPTED") == 0))
  2167. {
  2168. COMPLETE_WITH_CONST("PASSWORD");
  2169. }
  2170. /* complete CREATE ROLE,USER,GROUP <name> IN with ROLE,GROUP */
  2171. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  2172. (pg_strcasecmp(prev3_wd, "ROLE") == 0 ||
  2173. pg_strcasecmp(prev3_wd, "GROUP") == 0 || pg_strcasecmp(prev3_wd, "USER") == 0) &&
  2174. pg_strcasecmp(prev_wd, "IN") == 0)
  2175. {
  2176. static const char *const list_CREATEROLE3[] =
  2177. {"GROUP", "ROLE", NULL};
  2178. COMPLETE_WITH_LIST(list_CREATEROLE3);
  2179. }
  2180. /* CREATE VIEW */
  2181. /* Complete CREATE VIEW <name> with AS */
  2182. else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
  2183. pg_strcasecmp(prev2_wd, "VIEW") == 0)
  2184. COMPLETE_WITH_CONST("AS");
  2185. /* Complete "CREATE VIEW <sth> AS with "SELECT" */
  2186. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  2187. pg_strcasecmp(prev3_wd, "VIEW") == 0 &&
  2188. pg_strcasecmp(prev_wd, "AS") == 0)
  2189. COMPLETE_WITH_CONST("SELECT");
  2190. /* CREATE MATERIALIZED VIEW */
  2191. else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
  2192. pg_strcasecmp(prev_wd, "MATERIALIZED") == 0)
  2193. COMPLETE_WITH_CONST("VIEW");
  2194. /* Complete CREATE MATERIALIZED VIEW <name> with AS */
  2195. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  2196. pg_strcasecmp(prev3_wd, "MATERIALIZED") == 0 &&
  2197. pg_strcasecmp(prev2_wd, "VIEW") == 0)
  2198. COMPLETE_WITH_CONST("AS");
  2199. /* Complete "CREATE MATERIALIZED VIEW <sth> AS with "SELECT" */
  2200. else if (pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
  2201. pg_strcasecmp(prev4_wd, "MATERIALIZED") == 0 &&
  2202. pg_strcasecmp(prev3_wd, "VIEW") == 0 &&
  2203. pg_strcasecmp(prev_wd, "AS") == 0)
  2204. COMPLETE_WITH_CONST("SELECT");
  2205. /* CREATE EVENT TRIGGER */
  2206. else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
  2207. pg_strcasecmp(prev_wd, "EVENT") == 0)
  2208. COMPLETE_WITH_CONST("TRIGGER");
  2209. /* Complete CREATE EVENT TRIGGER <name> with ON */
  2210. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  2211. pg_strcasecmp(prev3_wd, "EVENT") == 0 &&
  2212. pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
  2213. COMPLETE_WITH_CONST("ON");
  2214. /* Complete CREATE EVENT TRIGGER <name> ON with event_type */
  2215. else if (pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
  2216. pg_strcasecmp(prev4_wd, "EVENT") == 0 &&
  2217. pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
  2218. pg_strcasecmp(prev_wd, "ON") == 0)
  2219. {
  2220. static const char *const list_CREATE_EVENT_TRIGGER_ON[] =
  2221. {"ddl_command_start", "ddl_command_end", "sql_drop", NULL};
  2222. COMPLETE_WITH_LIST(list_CREATE_EVENT_TRIGGER_ON);
  2223. }
  2224. /* DECLARE */
  2225. else if (pg_strcasecmp(prev2_wd, "DECLARE") == 0)
  2226. {
  2227. static const char *const list_DECLARE[] =
  2228. {"BINARY", "INSENSITIVE", "SCROLL", "NO SCROLL", "CURSOR", NULL};
  2229. COMPLETE_WITH_LIST(list_DECLARE);
  2230. }
  2231. /* CURSOR */
  2232. else if (pg_strcasecmp(prev_wd, "CURSOR") == 0)
  2233. {
  2234. static const char *const list_DECLARECURSOR[] =
  2235. {"WITH HOLD", "WITHOUT HOLD", "FOR", NULL};
  2236. COMPLETE_WITH_LIST(list_DECLARECURSOR);
  2237. }
  2238. /* DELETE */
  2239. /*
  2240. * Complete DELETE with FROM (only if the word before that is not "ON"
  2241. * (cf. rules) or "BEFORE" or "AFTER" (cf. triggers) or GRANT)
  2242. */
  2243. else if (pg_strcasecmp(prev_wd, "DELETE") == 0 &&
  2244. !(pg_strcasecmp(prev2_wd, "ON") == 0 ||
  2245. pg_strcasecmp(prev2_wd, "GRANT") == 0 ||
  2246. pg_strcasecmp(prev2_wd, "BEFORE") == 0 ||
  2247. pg_strcasecmp(prev2_wd, "AFTER") == 0))
  2248. COMPLETE_WITH_CONST("FROM");
  2249. /* Complete DELETE FROM with a list of tables */
  2250. else if (pg_strcasecmp(prev2_wd, "DELETE") == 0 &&
  2251. pg_strcasecmp(prev_wd, "FROM") == 0)
  2252. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL);
  2253. /* Complete DELETE FROM <table> */
  2254. else if (pg_strcasecmp(prev3_wd, "DELETE") == 0 &&
  2255. pg_strcasecmp(prev2_wd, "FROM") == 0)
  2256. {
  2257. static const char *const list_DELETE[] =
  2258. {"USING", "WHERE", "SET", NULL};
  2259. COMPLETE_WITH_LIST(list_DELETE);
  2260. }
  2261. /* XXX: implement tab completion for DELETE ... USING */
  2262. /* DISCARD */
  2263. else if (pg_strcasecmp(prev_wd, "DISCARD") == 0)
  2264. {
  2265. static const char *const list_DISCARD[] =
  2266. {"ALL", "PLANS", "SEQUENCES", "TEMP", NULL};
  2267. COMPLETE_WITH_LIST(list_DISCARD);
  2268. }
  2269. /* DO */
  2270. /*
  2271. * Complete DO with LANGUAGE.
  2272. */
  2273. else if (pg_strcasecmp(prev_wd, "DO") == 0)
  2274. {
  2275. static const char *const list_DO[] =
  2276. {"LANGUAGE", NULL};
  2277. COMPLETE_WITH_LIST(list_DO);
  2278. }
  2279. /* DROP (when not the previous word) */
  2280. /* DROP AGGREGATE */
  2281. else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2282. pg_strcasecmp(prev2_wd, "AGGREGATE") == 0)
  2283. COMPLETE_WITH_CONST("(");
  2284. /* DROP object with CASCADE / RESTRICT */
  2285. else if ((pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2286. (pg_strcasecmp(prev2_wd, "COLLATION") == 0 ||
  2287. pg_strcasecmp(prev2_wd, "CONVERSION") == 0 ||
  2288. pg_strcasecmp(prev2_wd, "DOMAIN") == 0 ||
  2289. pg_strcasecmp(prev2_wd, "EXTENSION") == 0 ||
  2290. pg_strcasecmp(prev2_wd, "FUNCTION") == 0 ||
  2291. pg_strcasecmp(prev2_wd, "INDEX") == 0 ||
  2292. pg_strcasecmp(prev2_wd, "LANGUAGE") == 0 ||
  2293. pg_strcasecmp(prev2_wd, "SCHEMA") == 0 ||
  2294. pg_strcasecmp(prev2_wd, "SEQUENCE") == 0 ||
  2295. pg_strcasecmp(prev2_wd, "SERVER") == 0 ||
  2296. pg_strcasecmp(prev2_wd, "TABLE") == 0 ||
  2297. pg_strcasecmp(prev2_wd, "TYPE") == 0 ||
  2298. pg_strcasecmp(prev2_wd, "VIEW") == 0)) ||
  2299. (pg_strcasecmp(prev4_wd, "DROP") == 0 &&
  2300. pg_strcasecmp(prev3_wd, "AGGREGATE") == 0 &&
  2301. prev_wd[strlen(prev_wd) - 1] == ')') ||
  2302. (pg_strcasecmp(prev4_wd, "DROP") == 0 &&
  2303. pg_strcasecmp(prev3_wd, "EVENT") == 0 &&
  2304. pg_strcasecmp(prev2_wd, "TRIGGER") == 0) ||
  2305. (pg_strcasecmp(prev5_wd, "DROP") == 0 &&
  2306. pg_strcasecmp(prev4_wd, "FOREIGN") == 0 &&
  2307. pg_strcasecmp(prev3_wd, "DATA") == 0 &&
  2308. pg_strcasecmp(prev2_wd, "WRAPPER") == 0) ||
  2309. (pg_strcasecmp(prev5_wd, "DROP") == 0 &&
  2310. pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
  2311. pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
  2312. (pg_strcasecmp(prev2_wd, "CONFIGURATION") == 0 ||
  2313. pg_strcasecmp(prev2_wd, "DICTIONARY") == 0 ||
  2314. pg_strcasecmp(prev2_wd, "PARSER") == 0 ||
  2315. pg_strcasecmp(prev2_wd, "TEMPLATE") == 0))
  2316. )
  2317. {
  2318. if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2319. pg_strcasecmp(prev2_wd, "FUNCTION") == 0)
  2320. {
  2321. COMPLETE_WITH_CONST("(");
  2322. }
  2323. else
  2324. {
  2325. static const char *const list_DROPCR[] =
  2326. {"CASCADE", "RESTRICT", NULL};
  2327. COMPLETE_WITH_LIST(list_DROPCR);
  2328. }
  2329. }
  2330. else if (pg_strcasecmp(prev2_wd, "DROP") == 0 &&
  2331. pg_strcasecmp(prev_wd, "FOREIGN") == 0)
  2332. {
  2333. static const char *const drop_CREATE_FOREIGN[] =
  2334. {"DATA WRAPPER", "TABLE", NULL};
  2335. COMPLETE_WITH_LIST(drop_CREATE_FOREIGN);
  2336. }
  2337. /* DROP MATERIALIZED VIEW */
  2338. else if (pg_strcasecmp(prev2_wd, "DROP") == 0 &&
  2339. pg_strcasecmp(prev_wd, "MATERIALIZED") == 0)
  2340. {
  2341. COMPLETE_WITH_CONST("VIEW");
  2342. }
  2343. else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2344. pg_strcasecmp(prev2_wd, "MATERIALIZED") == 0 &&
  2345. pg_strcasecmp(prev_wd, "VIEW") == 0)
  2346. {
  2347. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL);
  2348. }
  2349. else if (pg_strcasecmp(prev4_wd, "DROP") == 0 &&
  2350. (pg_strcasecmp(prev3_wd, "AGGREGATE") == 0 ||
  2351. pg_strcasecmp(prev3_wd, "FUNCTION") == 0) &&
  2352. pg_strcasecmp(prev_wd, "(") == 0)
  2353. COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
  2354. /* DROP OWNED BY */
  2355. else if (pg_strcasecmp(prev2_wd, "DROP") == 0 &&
  2356. pg_strcasecmp(prev_wd, "OWNED") == 0)
  2357. COMPLETE_WITH_CONST("BY");
  2358. else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2359. pg_strcasecmp(prev2_wd, "OWNED") == 0 &&
  2360. pg_strcasecmp(prev_wd, "BY") == 0)
  2361. COMPLETE_WITH_QUERY(Query_for_list_of_roles);
  2362. else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2363. pg_strcasecmp(prev2_wd, "TEXT") == 0 &&
  2364. pg_strcasecmp(prev_wd, "SEARCH") == 0)
  2365. {
  2366. static const char *const list_ALTERTEXTSEARCH[] =
  2367. {"CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE", NULL};
  2368. COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH);
  2369. }
  2370. /* DROP TRIGGER */
  2371. else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2372. pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
  2373. {
  2374. COMPLETE_WITH_CONST("ON");
  2375. }
  2376. else if (pg_strcasecmp(prev4_wd, "DROP") == 0 &&
  2377. pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
  2378. pg_strcasecmp(prev_wd, "ON") == 0)
  2379. {
  2380. completion_info_charp = prev2_wd;
  2381. COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
  2382. }
  2383. else if (pg_strcasecmp(prev5_wd, "DROP") == 0 &&
  2384. pg_strcasecmp(prev4_wd, "TRIGGER") == 0 &&
  2385. pg_strcasecmp(prev2_wd, "ON") == 0)
  2386. {
  2387. static const char *const list_DROPCR[] =
  2388. {"CASCADE", "RESTRICT", NULL};
  2389. COMPLETE_WITH_LIST(list_DROPCR);
  2390. }
  2391. /* DROP EVENT TRIGGER */
  2392. else if (pg_strcasecmp(prev2_wd, "DROP") == 0 &&
  2393. pg_strcasecmp(prev_wd, "EVENT") == 0)
  2394. {
  2395. COMPLETE_WITH_CONST("TRIGGER");
  2396. }
  2397. else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2398. pg_strcasecmp(prev2_wd, "EVENT") == 0 &&
  2399. pg_strcasecmp(prev_wd, "TRIGGER") == 0)
  2400. {
  2401. COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
  2402. }
  2403. /* DROP RULE */
  2404. else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
  2405. pg_strcasecmp(prev2_wd, "RULE") == 0)
  2406. {
  2407. COMPLETE_WITH_CONST("ON");
  2408. }
  2409. else if (pg_strcasecmp(prev4_wd, "DROP") == 0 &&
  2410. pg_strcasecmp(prev3_wd, "RULE") == 0 &&
  2411. pg_strcasecmp(prev_wd, "ON") == 0)
  2412. {
  2413. completion_info_charp = prev2_wd;
  2414. COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_rule);
  2415. }
  2416. else if (pg_strcasecmp(prev5_wd, "DROP") == 0 &&
  2417. pg_strcasecmp(prev4_wd, "RULE") == 0 &&
  2418. pg_strcasecmp(prev2_wd, "ON") == 0)
  2419. {
  2420. static const char *const list_DROPCR[] =
  2421. {"CASCADE", "RESTRICT", NULL};
  2422. COMPLETE_WITH_LIST(list_DROPCR);
  2423. }
  2424. /* EXECUTE, but not EXECUTE embedded in other commands */
  2425. else if (pg_strcasecmp(prev_wd, "EXECUTE") == 0 &&
  2426. prev2_wd[0] == '\0')
  2427. COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements);
  2428. /* EXPLAIN */
  2429. /*
  2430. * Complete EXPLAIN [ANALYZE] [VERBOSE] with list of EXPLAIN-able commands
  2431. */
  2432. else if (pg_strcasecmp(prev_wd, "EXPLAIN") == 0)
  2433. {
  2434. static const char *const list_EXPLAIN[] =
  2435. {"SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", "ANALYZE", "VERBOSE", NULL};
  2436. COMPLETE_WITH_LIST(list_EXPLAIN);
  2437. }
  2438. else if (pg_strcasecmp(prev2_wd, "EXPLAIN") == 0 &&
  2439. pg_strcasecmp(prev_wd, "ANALYZE") == 0)
  2440. {
  2441. static const char *const list_EXPLAIN[] =
  2442. {"SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", "VERBOSE", NULL};
  2443. COMPLETE_WITH_LIST(list_EXPLAIN);
  2444. }
  2445. else if ((pg_strcasecmp(prev2_wd, "EXPLAIN") == 0 &&
  2446. pg_strcasecmp(prev_wd, "VERBOSE") == 0) ||
  2447. (pg_strcasecmp(prev3_wd, "EXPLAIN") == 0 &&
  2448. pg_strcasecmp(prev2_wd, "ANALYZE") == 0 &&
  2449. pg_strcasecmp(prev_wd, "VERBOSE") == 0))
  2450. {
  2451. static const char *const list_EXPLAIN[] =
  2452. {"SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", NULL};
  2453. COMPLETE_WITH_LIST(list_EXPLAIN);
  2454. }
  2455. /* FETCH && MOVE */
  2456. /* Complete FETCH with one of FORWARD, BACKWARD, RELATIVE */
  2457. else if (pg_strcasecmp(prev_wd, "FETCH") == 0 ||
  2458. pg_strcasecmp(prev_wd, "MOVE") == 0)
  2459. {
  2460. static const char *const list_FETCH1[] =
  2461. {"ABSOLUTE", "BACKWARD", "FORWARD", "RELATIVE", NULL};
  2462. COMPLETE_WITH_LIST(list_FETCH1);
  2463. }
  2464. /* Complete FETCH <sth> with one of ALL, NEXT, PRIOR */
  2465. else if (pg_strcasecmp(prev2_wd, "FETCH") == 0 ||
  2466. pg_strcasecmp(prev2_wd, "MOVE") == 0)
  2467. {
  2468. static const char *const list_FETCH2[] =
  2469. {"ALL", "NEXT", "PRIOR", NULL};
  2470. COMPLETE_WITH_LIST(list_FETCH2);
  2471. }
  2472. /*
  2473. * Complete FETCH <sth1> <sth2> with "FROM" or "IN". These are equivalent,
  2474. * but we may as well tab-complete both: perhaps some users prefer one
  2475. * variant or the other.
  2476. */
  2477. else if ((pg_strcasecmp(prev3_wd, "FETCH") == 0 ||
  2478. pg_strcasecmp(prev3_wd, "MOVE") == 0) &&
  2479. pg_strcasecmp(prev_wd, "TO") != 0)
  2480. {
  2481. static const char *const list_FROMIN[] =
  2482. {"FROM", "IN", NULL};
  2483. COMPLETE_WITH_LIST(list_FROMIN);
  2484. }
  2485. /* FOREIGN DATA WRAPPER */
  2486. /* applies in ALTER/DROP FDW and in CREATE SERVER */
  2487. else if (pg_strcasecmp(prev4_wd, "CREATE") != 0 &&
  2488. pg_strcasecmp(prev3_wd, "FOREIGN") == 0 &&
  2489. pg_strcasecmp(prev2_wd, "DATA") == 0 &&
  2490. pg_strcasecmp(prev_wd, "WRAPPER") == 0)
  2491. COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
  2492. /* FOREIGN TABLE */
  2493. else if (pg_strcasecmp(prev3_wd, "CREATE") != 0 &&
  2494. pg_strcasecmp(prev2_wd, "FOREIGN") == 0 &&
  2495. pg_strcasecmp(prev_wd, "TABLE") == 0)
  2496. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL);
  2497. /* GRANT && REVOKE */
  2498. /* Complete GRANT/REVOKE with a list of roles and privileges */
  2499. else if (pg_strcasecmp(prev_wd, "GRANT") == 0 ||
  2500. pg_strcasecmp(prev_wd, "REVOKE") == 0)
  2501. {
  2502. COMPLETE_WITH_QUERY(Query_for_list_of_roles
  2503. " UNION SELECT 'SELECT'"
  2504. " UNION SELECT 'INSERT'"
  2505. " UNION SELECT 'UPDATE'"
  2506. " UNION SELECT 'DELETE'"
  2507. " UNION SELECT 'TRUNCATE'"
  2508. " UNION SELECT 'REFERENCES'"
  2509. " UNION SELECT 'TRIGGER'"
  2510. " UNION SELECT 'CREATE'"
  2511. " UNION SELECT 'CONNECT'"
  2512. " UNION SELECT 'TEMPORARY'"
  2513. " UNION SELECT 'EXECUTE'"
  2514. " UNION SELECT 'USAGE'"
  2515. " UNION SELECT 'ALL'");
  2516. }
  2517. /*
  2518. * Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with
  2519. * TO/FROM
  2520. */
  2521. else if (pg_strcasecmp(prev2_wd, "GRANT") == 0 ||
  2522. pg_strcasecmp(prev2_wd, "REVOKE") == 0)
  2523. {
  2524. if (pg_strcasecmp(prev_wd, "SELECT") == 0
  2525. || pg_strcasecmp(prev_wd, "INSERT") == 0
  2526. || pg_strcasecmp(prev_wd, "UPDATE") == 0
  2527. || pg_strcasecmp(prev_wd, "DELETE") == 0
  2528. || pg_strcasecmp(prev_wd, "TRUNCATE") == 0
  2529. || pg_strcasecmp(prev_wd, "REFERENCES") == 0
  2530. || pg_strcasecmp(prev_wd, "TRIGGER") == 0
  2531. || pg_strcasecmp(prev_wd, "CREATE") == 0
  2532. || pg_strcasecmp(prev_wd, "CONNECT") == 0
  2533. || pg_strcasecmp(prev_wd, "TEMPORARY") == 0
  2534. || pg_strcasecmp(prev_wd, "TEMP") == 0
  2535. || pg_strcasecmp(prev_wd, "EXECUTE") == 0
  2536. || pg_strcasecmp(prev_wd, "USAGE") == 0
  2537. || pg_strcasecmp(prev_wd, "ALL") == 0)
  2538. COMPLETE_WITH_CONST("ON");
  2539. else
  2540. {
  2541. if (pg_strcasecmp(prev2_wd, "GRANT") == 0)
  2542. COMPLETE_WITH_CONST("TO");
  2543. else
  2544. COMPLETE_WITH_CONST("FROM");
  2545. }
  2546. }
  2547. /*
  2548. * Complete GRANT/REVOKE <sth> ON with a list of tables, views, sequences,
  2549. * and indexes
  2550. *
  2551. * keywords DATABASE, FUNCTION, LANGUAGE, SCHEMA added to query result via
  2552. * UNION; seems to work intuitively
  2553. *
  2554. * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented
  2555. * here will only work if the privilege list contains exactly one
  2556. * privilege
  2557. */
  2558. else if ((pg_strcasecmp(prev3_wd, "GRANT") == 0 ||
  2559. pg_strcasecmp(prev3_wd, "REVOKE") == 0) &&
  2560. pg_strcasecmp(prev_wd, "ON") == 0)
  2561. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf,
  2562. " UNION SELECT 'DATABASE'"
  2563. " UNION SELECT 'DOMAIN'"
  2564. " UNION SELECT 'FOREIGN DATA WRAPPER'"
  2565. " UNION SELECT 'FOREIGN SERVER'"
  2566. " UNION SELECT 'FUNCTION'"
  2567. " UNION SELECT 'LANGUAGE'"
  2568. " UNION SELECT 'LARGE OBJECT'"
  2569. " UNION SELECT 'SCHEMA'"
  2570. " UNION SELECT 'TABLESPACE'"
  2571. " UNION SELECT 'TYPE'");
  2572. else if ((pg_strcasecmp(prev4_wd, "GRANT") == 0 ||
  2573. pg_strcasecmp(prev4_wd, "REVOKE") == 0) &&
  2574. pg_strcasecmp(prev2_wd, "ON") == 0 &&
  2575. pg_strcasecmp(prev_wd, "FOREIGN") == 0)
  2576. {
  2577. static const char *const list_privilege_foreign[] =
  2578. {"DATA WRAPPER", "SERVER", NULL};
  2579. COMPLETE_WITH_LIST(list_privilege_foreign);
  2580. }
  2581. /* Complete "GRANT/REVOKE * ON * " with "TO/FROM" */
  2582. else if ((pg_strcasecmp(prev4_wd, "GRANT") == 0 ||
  2583. pg_strcasecmp(prev4_wd, "REVOKE") == 0) &&
  2584. pg_strcasecmp(prev2_wd, "ON") == 0)
  2585. {
  2586. if (pg_strcasecmp(prev_wd, "DATABASE") == 0)
  2587. COMPLETE_WITH_QUERY(Query_for_list_of_databases);
  2588. else if (pg_strcasecmp(prev_wd, "DOMAIN") == 0)
  2589. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL);
  2590. else if (pg_strcasecmp(prev_wd, "FUNCTION") == 0)
  2591. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
  2592. else if (pg_strcasecmp(prev_wd, "LANGUAGE") == 0)
  2593. COMPLETE_WITH_QUERY(Query_for_list_of_languages);
  2594. else if (pg_strcasecmp(prev_wd, "SCHEMA") == 0)
  2595. COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
  2596. else if (pg_strcasecmp(prev_wd, "TABLESPACE") == 0)
  2597. COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
  2598. else if (pg_strcasecmp(prev_wd, "TYPE") == 0)
  2599. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL);
  2600. else if (pg_strcasecmp(prev4_wd, "GRANT") == 0)
  2601. COMPLETE_WITH_CONST("TO");
  2602. else
  2603. COMPLETE_WITH_CONST("FROM");
  2604. }
  2605. /* Complete "GRANT/REVOKE * ON * TO/FROM" with username, GROUP, or PUBLIC */
  2606. else if (pg_strcasecmp(prev5_wd, "GRANT") == 0 &&
  2607. pg_strcasecmp(prev3_wd, "ON") == 0)
  2608. {
  2609. if (pg_strcasecmp(prev_wd, "TO") == 0)
  2610. COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
  2611. else
  2612. COMPLETE_WITH_CONST("TO");
  2613. }
  2614. else if (pg_strcasecmp(prev5_wd, "REVOKE") == 0 &&
  2615. pg_strcasecmp(prev3_wd, "ON") == 0)
  2616. {
  2617. if (pg_strcasecmp(prev_wd, "FROM") == 0)
  2618. COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
  2619. else
  2620. COMPLETE_WITH_CONST("FROM");
  2621. }
  2622. /* Complete "GRANT/REVOKE * TO/FROM" with username, GROUP, or PUBLIC */
  2623. else if (pg_strcasecmp(prev3_wd, "GRANT") == 0 &&
  2624. pg_strcasecmp(prev_wd, "TO") == 0)
  2625. {
  2626. COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
  2627. }
  2628. else if (pg_strcasecmp(prev3_wd, "REVOKE") == 0 &&
  2629. pg_strcasecmp(prev_wd, "FROM") == 0)
  2630. {
  2631. COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
  2632. }
  2633. /* GROUP BY */
  2634. else if (pg_strcasecmp(prev3_wd, "FROM") == 0 &&
  2635. pg_strcasecmp(prev_wd, "GROUP") == 0)
  2636. COMPLETE_WITH_CONST("BY");
  2637. /* IMPORT FOREIGN SCHEMA */
  2638. else if (pg_strcasecmp(prev_wd, "IMPORT") == 0)
  2639. COMPLETE_WITH_CONST("FOREIGN SCHEMA");
  2640. else if (pg_strcasecmp(prev2_wd, "IMPORT") == 0 &&
  2641. pg_strcasecmp(prev_wd, "FOREIGN") == 0)
  2642. COMPLETE_WITH_CONST("SCHEMA");
  2643. /* INSERT */
  2644. /* Complete INSERT with "INTO" */
  2645. else if (pg_strcasecmp(prev_wd, "INSERT") == 0)
  2646. COMPLETE_WITH_CONST("INTO");
  2647. /* Complete INSERT INTO with table names */
  2648. else if (pg_strcasecmp(prev2_wd, "INSERT") == 0 &&
  2649. pg_strcasecmp(prev_wd, "INTO") == 0)
  2650. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL);
  2651. /* Complete "INSERT INTO <table> (" with attribute names */
  2652. else if (pg_strcasecmp(prev4_wd, "INSERT") == 0 &&
  2653. pg_strcasecmp(prev3_wd, "INTO") == 0 &&
  2654. pg_strcasecmp(prev_wd, "(") == 0)
  2655. COMPLETE_WITH_ATTR(prev2_wd, "");
  2656. /*
  2657. * Complete INSERT INTO <table> with "(" or "VALUES" or "SELECT" or
  2658. * "TABLE" or "DEFAULT VALUES"
  2659. */
  2660. else if (pg_strcasecmp(prev3_wd, "INSERT") == 0 &&
  2661. pg_strcasecmp(prev2_wd, "INTO") == 0)
  2662. {
  2663. static const char *const list_INSERT[] =
  2664. {"(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", NULL};
  2665. COMPLETE_WITH_LIST(list_INSERT);
  2666. }
  2667. /*
  2668. * Complete INSERT INTO <table> (attribs) with "VALUES" or "SELECT" or
  2669. * "TABLE"
  2670. */
  2671. else if (pg_strcasecmp(prev4_wd, "INSERT") == 0 &&
  2672. pg_strcasecmp(prev3_wd, "INTO") == 0 &&
  2673. prev_wd[strlen(prev_wd) - 1] == ')')
  2674. {
  2675. static const char *const list_INSERT[] =
  2676. {"SELECT", "TABLE", "VALUES", NULL};
  2677. COMPLETE_WITH_LIST(list_INSERT);
  2678. }
  2679. /* Insert an open parenthesis after "VALUES" */
  2680. else if (pg_strcasecmp(prev_wd, "VALUES") == 0 &&
  2681. pg_strcasecmp(prev2_wd, "DEFAULT") != 0)
  2682. COMPLETE_WITH_CONST("(");
  2683. /* LOCK */
  2684. /* Complete LOCK [TABLE] with a list of tables */
  2685. else if (pg_strcasecmp(prev_wd, "LOCK") == 0)
  2686. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
  2687. " UNION SELECT 'TABLE'");
  2688. else if (pg_strcasecmp(prev_wd, "TABLE") == 0 &&
  2689. pg_strcasecmp(prev2_wd, "LOCK") == 0)
  2690. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, "");
  2691. /* For the following, handle the case of a single table only for now */
  2692. /* Complete LOCK [TABLE] <table> with "IN" */
  2693. else if ((pg_strcasecmp(prev2_wd, "LOCK") == 0 &&
  2694. pg_strcasecmp(prev_wd, "TABLE") != 0) ||
  2695. (pg_strcasecmp(prev2_wd, "TABLE") == 0 &&
  2696. pg_strcasecmp(prev3_wd, "LOCK") == 0))
  2697. COMPLETE_WITH_CONST("IN");
  2698. /* Complete LOCK [TABLE] <table> IN with a lock mode */
  2699. else if (pg_strcasecmp(prev_wd, "IN") == 0 &&
  2700. (pg_strcasecmp(prev3_wd, "LOCK") == 0 ||
  2701. (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
  2702. pg_strcasecmp(prev4_wd, "LOCK") == 0)))
  2703. {
  2704. static const char *const lock_modes[] =
  2705. {"ACCESS SHARE MODE",
  2706. "ROW SHARE MODE", "ROW EXCLUSIVE MODE",
  2707. "SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE",
  2708. "SHARE ROW EXCLUSIVE MODE",
  2709. "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE", NULL};
  2710. COMPLETE_WITH_LIST(lock_modes);
  2711. }
  2712. /* NOTIFY */
  2713. else if (pg_strcasecmp(prev_wd, "NOTIFY") == 0)
  2714. COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s'");
  2715. /* OPTIONS */
  2716. else if (pg_strcasecmp(prev_wd, "OPTIONS") == 0)
  2717. COMPLETE_WITH_CONST("(");
  2718. /* OWNER TO - complete with available roles */
  2719. else if (pg_strcasecmp(prev2_wd, "OWNER") == 0 &&
  2720. pg_strcasecmp(prev_wd, "TO") == 0)
  2721. COMPLETE_WITH_QUERY(Query_for_list_of_roles);
  2722. /* ORDER BY */
  2723. else if (pg_strcasecmp(prev3_wd, "FROM") == 0 &&
  2724. pg_strcasecmp(prev_wd, "ORDER") == 0)
  2725. COMPLETE_WITH_CONST("BY");
  2726. else if (pg_strcasecmp(prev4_wd, "FROM") == 0 &&
  2727. pg_strcasecmp(prev2_wd, "ORDER") == 0 &&
  2728. pg_strcasecmp(prev_wd, "BY") == 0)
  2729. COMPLETE_WITH_ATTR(prev3_wd, "");
  2730. /* PREPARE xx AS */
  2731. else if (pg_strcasecmp(prev_wd, "AS") == 0 &&
  2732. pg_strcasecmp(prev3_wd, "PREPARE") == 0)
  2733. {
  2734. static const char *const list_PREPARE[] =
  2735. {"SELECT", "UPDATE", "INSERT", "DELETE", NULL};
  2736. COMPLETE_WITH_LIST(list_PREPARE);
  2737. }
  2738. /*
  2739. * PREPARE TRANSACTION is missing on purpose. It's intended for transaction
  2740. * managers, not for manual use in interactive sessions.
  2741. */
  2742. /* REASSIGN OWNED BY xxx TO yyy */
  2743. else if (pg_strcasecmp(prev_wd, "REASSIGN") == 0)
  2744. COMPLETE_WITH_CONST("OWNED");
  2745. else if (pg_strcasecmp(prev_wd, "OWNED") == 0 &&
  2746. pg_strcasecmp(prev2_wd, "REASSIGN") == 0)
  2747. COMPLETE_WITH_CONST("BY");
  2748. else if (pg_strcasecmp(prev_wd, "BY") == 0 &&
  2749. pg_strcasecmp(prev2_wd, "OWNED") == 0 &&
  2750. pg_strcasecmp(prev3_wd, "REASSIGN") == 0)
  2751. COMPLETE_WITH_QUERY(Query_for_list_of_roles);
  2752. else if (pg_strcasecmp(prev2_wd, "BY") == 0 &&
  2753. pg_strcasecmp(prev3_wd, "OWNED") == 0 &&
  2754. pg_strcasecmp(prev4_wd, "REASSIGN") == 0)
  2755. COMPLETE_WITH_CONST("TO");
  2756. else if (pg_strcasecmp(prev_wd, "TO") == 0 &&
  2757. pg_strcasecmp(prev3_wd, "BY") == 0 &&
  2758. pg_strcasecmp(prev4_wd, "OWNED") == 0 &&
  2759. pg_strcasecmp(prev5_wd, "REASSIGN") == 0)
  2760. COMPLETE_WITH_QUERY(Query_for_list_of_roles);
  2761. /* REFRESH MATERIALIZED VIEW */
  2762. else if (pg_strcasecmp(prev_wd, "REFRESH") == 0)
  2763. COMPLETE_WITH_CONST("MATERIALIZED VIEW");
  2764. else if (pg_strcasecmp(prev2_wd, "REFRESH") == 0 &&
  2765. pg_strcasecmp(prev_wd, "MATERIALIZED") == 0)
  2766. COMPLETE_WITH_CONST("VIEW");
  2767. else if (pg_strcasecmp(prev3_wd, "REFRESH") == 0 &&
  2768. pg_strcasecmp(prev2_wd, "MATERIALIZED") == 0 &&
  2769. pg_strcasecmp(prev_wd, "VIEW") == 0)
  2770. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews,
  2771. " UNION SELECT 'CONCURRENTLY'");
  2772. else if (pg_strcasecmp(prev4_wd, "REFRESH") == 0 &&
  2773. pg_strcasecmp(prev3_wd, "MATERIALIZED") == 0 &&
  2774. pg_strcasecmp(prev2_wd, "VIEW") == 0 &&
  2775. pg_strcasecmp(prev_wd, "CONCURRENTLY") == 0)
  2776. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL);
  2777. else if (pg_strcasecmp(prev4_wd, "REFRESH") == 0 &&
  2778. pg_strcasecmp(prev3_wd, "MATERIALIZED") == 0 &&
  2779. pg_strcasecmp(prev2_wd, "VIEW") == 0)
  2780. COMPLETE_WITH_CONST("WITH");
  2781. else if (pg_strcasecmp(prev5_wd, "REFRESH") == 0 &&
  2782. pg_strcasecmp(prev4_wd, "MATERIALIZED") == 0 &&
  2783. pg_strcasecmp(prev3_wd, "VIEW") == 0 &&
  2784. pg_strcasecmp(prev2_wd, "CONCURRENTLY") == 0)
  2785. COMPLETE_WITH_CONST("WITH DATA");
  2786. else if (pg_strcasecmp(prev5_wd, "REFRESH") == 0 &&
  2787. pg_strcasecmp(prev4_wd, "MATERIALIZED") == 0 &&
  2788. pg_strcasecmp(prev3_wd, "VIEW") == 0 &&
  2789. pg_strcasecmp(prev_wd, "WITH") == 0)
  2790. {
  2791. static const char *const list_WITH_DATA[] =
  2792. {"NO DATA", "DATA", NULL};
  2793. COMPLETE_WITH_LIST(list_WITH_DATA);
  2794. }
  2795. else if (pg_strcasecmp(prev6_wd, "REFRESH") == 0 &&
  2796. pg_strcasecmp(prev5_wd, "MATERIALIZED") == 0 &&
  2797. pg_strcasecmp(prev4_wd, "VIEW") == 0 &&
  2798. pg_strcasecmp(prev3_wd, "CONCURRENTLY") == 0 &&
  2799. pg_strcasecmp(prev_wd, "WITH") == 0)
  2800. COMPLETE_WITH_CONST("DATA");
  2801. else if (pg_strcasecmp(prev6_wd, "REFRESH") == 0 &&
  2802. pg_strcasecmp(prev5_wd, "MATERIALIZED") == 0 &&
  2803. pg_strcasecmp(prev4_wd, "VIEW") == 0 &&
  2804. pg_strcasecmp(prev2_wd, "WITH") == 0 &&
  2805. pg_strcasecmp(prev_wd, "NO") == 0)
  2806. COMPLETE_WITH_CONST("DATA");
  2807. /* REINDEX */
  2808. else if (pg_strcasecmp(prev_wd, "REINDEX") == 0)
  2809. {
  2810. static const char *const list_REINDEX[] =
  2811. {"TABLE", "INDEX", "SYSTEM", "DATABASE", NULL};
  2812. COMPLETE_WITH_LIST(list_REINDEX);
  2813. }
  2814. else if (pg_strcasecmp(prev2_wd, "REINDEX") == 0)
  2815. {
  2816. if (pg_strcasecmp(prev_wd, "TABLE") == 0)
  2817. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL);
  2818. else if (pg_strcasecmp(prev_wd, "INDEX") == 0)
  2819. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL);
  2820. else if (pg_strcasecmp(prev_wd, "SYSTEM") == 0 ||
  2821. pg_strcasecmp(prev_wd, "DATABASE") == 0)
  2822. COMPLETE_WITH_QUERY(Query_for_list_of_databases);
  2823. }
  2824. /* SECURITY LABEL */
  2825. else if (pg_strcasecmp(prev_wd, "SECURITY") == 0)
  2826. COMPLETE_WITH_CONST("LABEL");
  2827. else if (pg_strcasecmp(prev2_wd, "SECURITY") == 0 &&
  2828. pg_strcasecmp(prev_wd, "LABEL") == 0)
  2829. {
  2830. static const char *const list_SECURITY_LABEL_preposition[] =
  2831. {"ON", "FOR"};
  2832. COMPLETE_WITH_LIST(list_SECURITY_LABEL_preposition);
  2833. }
  2834. else if (pg_strcasecmp(prev4_wd, "SECURITY") == 0 &&
  2835. pg_strcasecmp(prev3_wd, "LABEL") == 0 &&
  2836. pg_strcasecmp(prev2_wd, "FOR") == 0)
  2837. COMPLETE_WITH_CONST("ON");
  2838. else if ((pg_strcasecmp(prev3_wd, "SECURITY") == 0 &&
  2839. pg_strcasecmp(prev2_wd, "LABEL") == 0 &&
  2840. pg_strcasecmp(prev_wd, "ON") == 0) ||
  2841. (pg_strcasecmp(prev5_wd, "SECURITY") == 0 &&
  2842. pg_strcasecmp(prev4_wd, "LABEL") == 0 &&
  2843. pg_strcasecmp(prev3_wd, "FOR") == 0 &&
  2844. pg_strcasecmp(prev_wd, "ON") == 0))
  2845. {
  2846. static const char *const list_SECURITY_LABEL[] =
  2847. {"LANGUAGE", "SCHEMA", "SEQUENCE", "TABLE", "TYPE", "VIEW",
  2848. "MATERIALIZED VIEW", "COLUMN", "AGGREGATE", "FUNCTION", "DOMAIN",
  2849. "LARGE OBJECT", NULL};
  2850. COMPLETE_WITH_LIST(list_SECURITY_LABEL);
  2851. }
  2852. else if (pg_strcasecmp(prev5_wd, "SECURITY") == 0 &&
  2853. pg_strcasecmp(prev4_wd, "LABEL") == 0 &&
  2854. pg_strcasecmp(prev3_wd, "ON") == 0)
  2855. COMPLETE_WITH_CONST("IS");
  2856. /* SELECT */
  2857. /* naah . . . */
  2858. /* SET, RESET, SHOW */
  2859. /* Complete with a variable name */
  2860. else if ((pg_strcasecmp(prev_wd, "SET") == 0 &&
  2861. pg_strcasecmp(prev3_wd, "UPDATE") != 0) ||
  2862. pg_strcasecmp(prev_wd, "RESET") == 0)
  2863. COMPLETE_WITH_QUERY(Query_for_list_of_set_vars);
  2864. else if (pg_strcasecmp(prev_wd, "SHOW") == 0)
  2865. COMPLETE_WITH_QUERY(Query_for_list_of_show_vars);
  2866. /* Complete "SET TRANSACTION" */
  2867. else if ((pg_strcasecmp(prev2_wd, "SET") == 0 &&
  2868. pg_strcasecmp(prev_wd, "TRANSACTION") == 0)
  2869. || (pg_strcasecmp(prev2_wd, "START") == 0
  2870. && pg_strcasecmp(prev_wd, "TRANSACTION") == 0)
  2871. || (pg_strcasecmp(prev2_wd, "BEGIN") == 0
  2872. && pg_strcasecmp(prev_wd, "WORK") == 0)
  2873. || (pg_strcasecmp(prev2_wd, "BEGIN") == 0
  2874. && pg_strcasecmp(prev_wd, "TRANSACTION") == 0)
  2875. || (pg_strcasecmp(prev4_wd, "SESSION") == 0
  2876. && pg_strcasecmp(prev3_wd, "CHARACTERISTICS") == 0
  2877. && pg_strcasecmp(prev2_wd, "AS") == 0
  2878. && pg_strcasecmp(prev_wd, "TRANSACTION") == 0))
  2879. {
  2880. static const char *const my_list[] =
  2881. {"ISOLATION LEVEL", "READ", NULL};
  2882. COMPLETE_WITH_LIST(my_list);
  2883. }
  2884. else if ((pg_strcasecmp(prev3_wd, "SET") == 0
  2885. || pg_strcasecmp(prev3_wd, "BEGIN") == 0
  2886. || pg_strcasecmp(prev3_wd, "START") == 0
  2887. || (pg_strcasecmp(prev4_wd, "CHARACTERISTICS") == 0
  2888. && pg_strcasecmp(prev3_wd, "AS") == 0))
  2889. && (pg_strcasecmp(prev2_wd, "TRANSACTION") == 0
  2890. || pg_strcasecmp(prev2_wd, "WORK") == 0)
  2891. && pg_strcasecmp(prev_wd, "ISOLATION") == 0)
  2892. COMPLETE_WITH_CONST("LEVEL");
  2893. else if ((pg_strcasecmp(prev4_wd, "SET") == 0
  2894. || pg_strcasecmp(prev4_wd, "BEGIN") == 0
  2895. || pg_strcasecmp(prev4_wd, "START") == 0
  2896. || pg_strcasecmp(prev4_wd, "AS") == 0)
  2897. && (pg_strcasecmp(prev3_wd, "TRANSACTION") == 0
  2898. || pg_strcasecmp(prev3_wd, "WORK") == 0)
  2899. && pg_strcasecmp(prev2_wd, "ISOLATION") == 0
  2900. && pg_strcasecmp(prev_wd, "LEVEL") == 0)
  2901. {
  2902. static const char *const my_list[] =
  2903. {"READ", "REPEATABLE", "SERIALIZABLE", NULL};
  2904. COMPLETE_WITH_LIST(my_list);
  2905. }
  2906. else if ((pg_strcasecmp(prev4_wd, "TRANSACTION") == 0 ||
  2907. pg_strcasecmp(prev4_wd, "WORK") == 0) &&
  2908. pg_strcasecmp(prev3_wd, "ISOLATION") == 0 &&
  2909. pg_strcasecmp(prev2_wd, "LEVEL") == 0 &&
  2910. pg_strcasecmp(prev_wd, "READ") == 0)
  2911. {
  2912. static const char *const my_list[] =
  2913. {"UNCOMMITTED", "COMMITTED", NULL};
  2914. COMPLETE_WITH_LIST(my_list);
  2915. }
  2916. else if ((pg_strcasecmp(prev4_wd, "TRANSACTION") == 0 ||
  2917. pg_strcasecmp(prev4_wd, "WORK") == 0) &&
  2918. pg_strcasecmp(prev3_wd, "ISOLATION") == 0 &&
  2919. pg_strcasecmp(prev2_wd, "LEVEL") == 0 &&
  2920. pg_strcasecmp(prev_wd, "REPEATABLE") == 0)
  2921. COMPLETE_WITH_CONST("READ");
  2922. else if ((pg_strcasecmp(prev3_wd, "SET") == 0 ||
  2923. pg_strcasecmp(prev3_wd, "BEGIN") == 0 ||
  2924. pg_strcasecmp(prev3_wd, "START") == 0 ||
  2925. pg_strcasecmp(prev3_wd, "AS") == 0) &&
  2926. (pg_strcasecmp(prev2_wd, "TRANSACTION") == 0 ||
  2927. pg_strcasecmp(prev2_wd, "WORK") == 0) &&
  2928. pg_strcasecmp(prev_wd, "READ") == 0)
  2929. {
  2930. static const char *const my_list[] =
  2931. {"ONLY", "WRITE", NULL};
  2932. COMPLETE_WITH_LIST(my_list);
  2933. }
  2934. /* SET CONSTRAINTS */
  2935. else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
  2936. pg_strcasecmp(prev_wd, "CONSTRAINTS") == 0)
  2937. {
  2938. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_constraints_with_schema, "UNION SELECT 'ALL'");
  2939. }
  2940. /* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */
  2941. else if (pg_strcasecmp(prev3_wd, "SET") == 0 &&
  2942. pg_strcasecmp(prev2_wd, "CONSTRAINTS") == 0)
  2943. {
  2944. static const char *const constraint_list[] =
  2945. {"DEFERRED", "IMMEDIATE", NULL};
  2946. COMPLETE_WITH_LIST(constraint_list);
  2947. }
  2948. /* Complete SET ROLE */
  2949. else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
  2950. pg_strcasecmp(prev_wd, "ROLE") == 0)
  2951. COMPLETE_WITH_QUERY(Query_for_list_of_roles);
  2952. /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */
  2953. else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
  2954. pg_strcasecmp(prev_wd, "SESSION") == 0)
  2955. {
  2956. static const char *const my_list[] =
  2957. {"AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION", NULL};
  2958. COMPLETE_WITH_LIST(my_list);
  2959. }
  2960. /* Complete SET SESSION AUTHORIZATION with username */
  2961. else if (pg_strcasecmp(prev3_wd, "SET") == 0
  2962. && pg_strcasecmp(prev2_wd, "SESSION") == 0
  2963. && pg_strcasecmp(prev_wd, "AUTHORIZATION") == 0)
  2964. COMPLETE_WITH_QUERY(Query_for_list_of_roles " UNION SELECT 'DEFAULT'");
  2965. /* Complete RESET SESSION with AUTHORIZATION */
  2966. else if (pg_strcasecmp(prev2_wd, "RESET") == 0 &&
  2967. pg_strcasecmp(prev_wd, "SESSION") == 0)
  2968. COMPLETE_WITH_CONST("AUTHORIZATION");
  2969. /* Complete SET <var> with "TO" */
  2970. else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
  2971. pg_strcasecmp(prev4_wd, "UPDATE") != 0 &&
  2972. pg_strcasecmp(prev_wd, "TABLESPACE") != 0 &&
  2973. pg_strcasecmp(prev_wd, "SCHEMA") != 0 &&
  2974. prev_wd[strlen(prev_wd) - 1] != ')' &&
  2975. prev_wd[strlen(prev_wd) - 1] != '=' &&
  2976. pg_strcasecmp(prev4_wd, "DOMAIN") != 0)
  2977. COMPLETE_WITH_CONST("TO");
  2978. /* Suggest possible variable values */
  2979. else if (pg_strcasecmp(prev3_wd, "SET") == 0 &&
  2980. (pg_strcasecmp(prev_wd, "TO") == 0 || strcmp(prev_wd, "=") == 0))
  2981. {
  2982. if (pg_strcasecmp(prev2_wd, "DateStyle") == 0)
  2983. {
  2984. static const char *const my_list[] =
  2985. {"ISO", "SQL", "Postgres", "German",
  2986. "YMD", "DMY", "MDY",
  2987. "US", "European", "NonEuropean",
  2988. "DEFAULT", NULL};
  2989. COMPLETE_WITH_LIST(my_list);
  2990. }
  2991. else if (pg_strcasecmp(prev2_wd, "IntervalStyle") == 0)
  2992. {
  2993. static const char *const my_list[] =
  2994. {"postgres", "postgres_verbose", "sql_standard", "iso_8601", NULL};
  2995. COMPLETE_WITH_LIST(my_list);
  2996. }
  2997. else if (pg_strcasecmp(prev2_wd, "GEQO") == 0)
  2998. {
  2999. static const char *const my_list[] =
  3000. {"ON", "OFF", "DEFAULT", NULL};
  3001. COMPLETE_WITH_LIST(my_list);
  3002. }
  3003. else if (pg_strcasecmp(prev2_wd, "search_path") == 0)
  3004. {
  3005. COMPLETE_WITH_QUERY(Query_for_list_of_schemas
  3006. " AND nspname not like 'pg\\_toast%%' "
  3007. " AND nspname not like 'pg\\_temp%%' "
  3008. " UNION SELECT 'DEFAULT' ");
  3009. }
  3010. else
  3011. {
  3012. static const char *const my_list[] =
  3013. {"DEFAULT", NULL};
  3014. COMPLETE_WITH_LIST(my_list);
  3015. }
  3016. }
  3017. /* START TRANSACTION */
  3018. else if (pg_strcasecmp(prev_wd, "START") == 0)
  3019. COMPLETE_WITH_CONST("TRANSACTION");
  3020. /* TABLE, but not TABLE embedded in other commands */
  3021. else if (pg_strcasecmp(prev_wd, "TABLE") == 0 &&
  3022. prev2_wd[0] == '\0')
  3023. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL);
  3024. /* TRUNCATE */
  3025. else if (pg_strcasecmp(prev_wd, "TRUNCATE") == 0)
  3026. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
  3027. /* UNLISTEN */
  3028. else if (pg_strcasecmp(prev_wd, "UNLISTEN") == 0)
  3029. COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s' UNION SELECT '*'");
  3030. /* UPDATE */
  3031. /* If prev. word is UPDATE suggest a list of tables */
  3032. else if (pg_strcasecmp(prev_wd, "UPDATE") == 0)
  3033. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL);
  3034. /* Complete UPDATE <table> with "SET" */
  3035. else if (pg_strcasecmp(prev2_wd, "UPDATE") == 0)
  3036. COMPLETE_WITH_CONST("SET");
  3037. /*
  3038. * If the previous word is SET (and it wasn't caught above as the _first_
  3039. * word) the word before it was (hopefully) a table name and we'll now
  3040. * make a list of attributes.
  3041. */
  3042. else if (pg_strcasecmp(prev_wd, "SET") == 0)
  3043. COMPLETE_WITH_ATTR(prev2_wd, "");
  3044. /* UPDATE xx SET yy = */
  3045. else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
  3046. pg_strcasecmp(prev4_wd, "UPDATE") == 0)
  3047. COMPLETE_WITH_CONST("=");
  3048. /* USER MAPPING */
  3049. else if ((pg_strcasecmp(prev3_wd, "ALTER") == 0 ||
  3050. pg_strcasecmp(prev3_wd, "CREATE") == 0 ||
  3051. pg_strcasecmp(prev3_wd, "DROP") == 0) &&
  3052. pg_strcasecmp(prev2_wd, "USER") == 0 &&
  3053. pg_strcasecmp(prev_wd, "MAPPING") == 0)
  3054. COMPLETE_WITH_CONST("FOR");
  3055. else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
  3056. pg_strcasecmp(prev3_wd, "USER") == 0 &&
  3057. pg_strcasecmp(prev2_wd, "MAPPING") == 0 &&
  3058. pg_strcasecmp(prev_wd, "FOR") == 0)
  3059. COMPLETE_WITH_QUERY(Query_for_list_of_roles
  3060. " UNION SELECT 'CURRENT_USER'"
  3061. " UNION SELECT 'PUBLIC'"
  3062. " UNION SELECT 'USER'");
  3063. else if ((pg_strcasecmp(prev4_wd, "ALTER") == 0 ||
  3064. pg_strcasecmp(prev4_wd, "DROP") == 0) &&
  3065. pg_strcasecmp(prev3_wd, "USER") == 0 &&
  3066. pg_strcasecmp(prev2_wd, "MAPPING") == 0 &&
  3067. pg_strcasecmp(prev_wd, "FOR") == 0)
  3068. COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
  3069. else if ((pg_strcasecmp(prev5_wd, "CREATE") == 0 ||
  3070. pg_strcasecmp(prev5_wd, "ALTER") == 0 ||
  3071. pg_strcasecmp(prev5_wd, "DROP") == 0) &&
  3072. pg_strcasecmp(prev4_wd, "USER") == 0 &&
  3073. pg_strcasecmp(prev3_wd, "MAPPING") == 0 &&
  3074. pg_strcasecmp(prev2_wd, "FOR") == 0)
  3075. COMPLETE_WITH_CONST("SERVER");
  3076. /*
  3077. * VACUUM [ FULL | FREEZE ] [ VERBOSE ] [ table ]
  3078. * VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]
  3079. */
  3080. else if (pg_strcasecmp(prev_wd, "VACUUM") == 0)
  3081. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm,
  3082. " UNION SELECT 'FULL'"
  3083. " UNION SELECT 'FREEZE'"
  3084. " UNION SELECT 'ANALYZE'"
  3085. " UNION SELECT 'VERBOSE'");
  3086. else if (pg_strcasecmp(prev2_wd, "VACUUM") == 0 &&
  3087. (pg_strcasecmp(prev_wd, "FULL") == 0 ||
  3088. pg_strcasecmp(prev_wd, "FREEZE") == 0))
  3089. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm,
  3090. " UNION SELECT 'ANALYZE'"
  3091. " UNION SELECT 'VERBOSE'");
  3092. else if (pg_strcasecmp(prev3_wd, "VACUUM") == 0 &&
  3093. pg_strcasecmp(prev_wd, "ANALYZE") == 0 &&
  3094. (pg_strcasecmp(prev2_wd, "FULL") == 0 ||
  3095. pg_strcasecmp(prev2_wd, "FREEZE") == 0))
  3096. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm,
  3097. " UNION SELECT 'VERBOSE'");
  3098. else if (pg_strcasecmp(prev3_wd, "VACUUM") == 0 &&
  3099. pg_strcasecmp(prev_wd, "VERBOSE") == 0 &&
  3100. (pg_strcasecmp(prev2_wd, "FULL") == 0 ||
  3101. pg_strcasecmp(prev2_wd, "FREEZE") == 0))
  3102. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm,
  3103. " UNION SELECT 'ANALYZE'");
  3104. else if (pg_strcasecmp(prev2_wd, "VACUUM") == 0 &&
  3105. pg_strcasecmp(prev_wd, "VERBOSE") == 0)
  3106. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm,
  3107. " UNION SELECT 'ANALYZE'");
  3108. else if (pg_strcasecmp(prev2_wd, "VACUUM") == 0 &&
  3109. pg_strcasecmp(prev_wd, "ANALYZE") == 0)
  3110. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm,
  3111. " UNION SELECT 'VERBOSE'");
  3112. else if ((pg_strcasecmp(prev_wd, "ANALYZE") == 0 &&
  3113. pg_strcasecmp(prev2_wd, "VERBOSE") == 0) ||
  3114. (pg_strcasecmp(prev_wd, "VERBOSE") == 0 &&
  3115. pg_strcasecmp(prev2_wd, "ANALYZE") == 0))
  3116. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL);
  3117. /* WITH [RECURSIVE] */
  3118. /*
  3119. * Only match when WITH is the first word, as WITH may appear in many
  3120. * other contexts.
  3121. */
  3122. else if (pg_strcasecmp(prev_wd, "WITH") == 0 &&
  3123. prev2_wd[0] == '\0')
  3124. COMPLETE_WITH_CONST("RECURSIVE");
  3125. /* ANALYZE */
  3126. /* If the previous word is ANALYZE, produce list of tables */
  3127. else if (pg_strcasecmp(prev_wd, "ANALYZE") == 0)
  3128. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tmf, NULL);
  3129. /* WHERE */
  3130. /* Simple case of the word before the where being the table name */
  3131. else if (pg_strcasecmp(prev_wd, "WHERE") == 0)
  3132. COMPLETE_WITH_ATTR(prev2_wd, "");
  3133. /* ... FROM ... */
  3134. /* TODO: also include SRF ? */
  3135. else if (pg_strcasecmp(prev_wd, "FROM") == 0 &&
  3136. pg_strcasecmp(prev3_wd, "COPY") != 0 &&
  3137. pg_strcasecmp(prev3_wd, "\\copy") != 0)
  3138. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL);
  3139. /* ... JOIN ... */
  3140. else if (pg_strcasecmp(prev_wd, "JOIN") == 0)
  3141. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL);
  3142. /* Backslash commands */
  3143. /* TODO: \dc \dd \dl */
  3144. else if (strcmp(prev_wd, "\\connect") == 0 || strcmp(prev_wd, "\\c") == 0)
  3145. COMPLETE_WITH_QUERY(Query_for_list_of_databases);
  3146. else if (strncmp(prev_wd, "\\da", strlen("\\da")) == 0)
  3147. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL);
  3148. else if (strncmp(prev_wd, "\\db", strlen("\\db")) == 0)
  3149. COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
  3150. else if (strncmp(prev_wd, "\\dD", strlen("\\dD")) == 0)
  3151. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL);
  3152. else if (strncmp(prev_wd, "\\des", strlen("\\des")) == 0)
  3153. COMPLETE_WITH_QUERY(Query_for_list_of_servers);
  3154. else if (strncmp(prev_wd, "\\deu", strlen("\\deu")) == 0)
  3155. COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
  3156. else if (strncmp(prev_wd, "\\dew", strlen("\\dew")) == 0)
  3157. COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
  3158. else if (strncmp(prev_wd, "\\df", strlen("\\df")) == 0)
  3159. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
  3160. else if (strncmp(prev_wd, "\\dFd", strlen("\\dFd")) == 0)
  3161. COMPLETE_WITH_QUERY(Query_for_list_of_ts_dictionaries);
  3162. else if (strncmp(prev_wd, "\\dFp", strlen("\\dFp")) == 0)
  3163. COMPLETE_WITH_QUERY(Query_for_list_of_ts_parsers);
  3164. else if (strncmp(prev_wd, "\\dFt", strlen("\\dFt")) == 0)
  3165. COMPLETE_WITH_QUERY(Query_for_list_of_ts_templates);
  3166. /* must be at end of \dF */
  3167. else if (strncmp(prev_wd, "\\dF", strlen("\\dF")) == 0)
  3168. COMPLETE_WITH_QUERY(Query_for_list_of_ts_configurations);
  3169. else if (strncmp(prev_wd, "\\di", strlen("\\di")) == 0)
  3170. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL);
  3171. else if (strncmp(prev_wd, "\\dL", strlen("\\dL")) == 0)
  3172. COMPLETE_WITH_QUERY(Query_for_list_of_languages);
  3173. else if (strncmp(prev_wd, "\\dn", strlen("\\dn")) == 0)
  3174. COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
  3175. else if (strncmp(prev_wd, "\\dp", strlen("\\dp")) == 0
  3176. || strncmp(prev_wd, "\\z", strlen("\\z")) == 0)
  3177. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL);
  3178. else if (strncmp(prev_wd, "\\ds", strlen("\\ds")) == 0)
  3179. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL);
  3180. else if (strncmp(prev_wd, "\\dt", strlen("\\dt")) == 0)
  3181. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
  3182. else if (strncmp(prev_wd, "\\dT", strlen("\\dT")) == 0)
  3183. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL);
  3184. else if (strncmp(prev_wd, "\\du", strlen("\\du")) == 0
  3185. || (strncmp(prev_wd, "\\dg", strlen("\\dg")) == 0))
  3186. COMPLETE_WITH_QUERY(Query_for_list_of_roles);
  3187. else if (strncmp(prev_wd, "\\dv", strlen("\\dv")) == 0)
  3188. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL);
  3189. else if (strncmp(prev_wd, "\\dx", strlen("\\dx")) == 0)
  3190. COMPLETE_WITH_QUERY(Query_for_list_of_extensions);
  3191. else if (strncmp(prev_wd, "\\dm", strlen("\\dm")) == 0)
  3192. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL);
  3193. /* must be at end of \d list */
  3194. else if (strncmp(prev_wd, "\\d", strlen("\\d")) == 0)
  3195. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL);
  3196. else if (strcmp(prev_wd, "\\ef") == 0)
  3197. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
  3198. else if (strcmp(prev_wd, "\\encoding") == 0)
  3199. COMPLETE_WITH_QUERY(Query_for_list_of_encodings);
  3200. else if (strcmp(prev_wd, "\\h") == 0 || strcmp(prev_wd, "\\help") == 0)
  3201. COMPLETE_WITH_LIST(sql_commands);
  3202. else if (strcmp(prev_wd, "\\password") == 0)
  3203. COMPLETE_WITH_QUERY(Query_for_list_of_roles);
  3204. else if (strcmp(prev_wd, "\\pset") == 0)
  3205. {
  3206. static const char *const my_list[] =
  3207. {"border", "columns", "expanded", "fieldsep", "fieldsep_zero",
  3208. "footer", "format", "linestyle", "null", "numericlocale",
  3209. "pager", "recordsep", "recordsep_zero", "tableattr", "title",
  3210. "tuples_only", NULL};
  3211. COMPLETE_WITH_LIST_CS(my_list);
  3212. }
  3213. else if (strcmp(prev2_wd, "\\pset") == 0)
  3214. {
  3215. if (strcmp(prev_wd, "format") == 0)
  3216. {
  3217. static const char *const my_list[] =
  3218. {"unaligned", "aligned", "wrapped", "html", "latex",
  3219. "troff-ms", NULL};
  3220. COMPLETE_WITH_LIST_CS(my_list);
  3221. }
  3222. else if (strcmp(prev_wd, "linestyle") == 0)
  3223. {
  3224. static const char *const my_list[] =
  3225. {"ascii", "old-ascii", "unicode", NULL};
  3226. COMPLETE_WITH_LIST_CS(my_list);
  3227. }
  3228. }
  3229. else if (strcmp(prev_wd, "\\set") == 0)
  3230. {
  3231. matches = complete_from_variables(text, "", "");
  3232. }
  3233. else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
  3234. COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
  3235. else if (strcmp(prev_wd, "\\cd") == 0 ||
  3236. strcmp(prev_wd, "\\e") == 0 || strcmp(prev_wd, "\\edit") == 0 ||
  3237. strcmp(prev_wd, "\\g") == 0 ||
  3238. strcmp(prev_wd, "\\i") == 0 || strcmp(prev_wd, "\\include") == 0 ||
  3239. strcmp(prev_wd, "\\ir") == 0 || strcmp(prev_wd, "\\include_relative") == 0 ||
  3240. strcmp(prev_wd, "\\o") == 0 || strcmp(prev_wd, "\\out") == 0 ||
  3241. strcmp(prev_wd, "\\s") == 0 ||
  3242. strcmp(prev_wd, "\\w") == 0 || strcmp(prev_wd, "\\write") == 0 ||
  3243. strcmp(prev_wd, "\\lo_import") == 0
  3244. )
  3245. {
  3246. completion_charp = "\\";
  3247. matches = completion_matches(text, complete_from_files);
  3248. }
  3249. /*
  3250. * Finally, we look through the list of "things", such as TABLE, INDEX and
  3251. * check if that was the previous word. If so, execute the query to get a
  3252. * list of them.
  3253. */
  3254. else
  3255. {
  3256. int i;
  3257. for (i = 0; words_after_create[i].name; i++)
  3258. {
  3259. if (pg_strcasecmp(prev_wd, words_after_create[i].name) == 0)
  3260. {
  3261. if (words_after_create[i].query)
  3262. COMPLETE_WITH_QUERY(words_after_create[i].query);
  3263. else if (words_after_create[i].squery)
  3264. COMPLETE_WITH_SCHEMA_QUERY(*words_after_create[i].squery,
  3265. NULL);
  3266. break;
  3267. }
  3268. }
  3269. }
  3270. /*
  3271. * If we still don't have anything to match we have to fabricate some sort
  3272. * of default list. If we were to just return NULL, readline automatically
  3273. * attempts filename completion, and that's usually no good.
  3274. */
  3275. if (matches == NULL)
  3276. {
  3277. COMPLETE_WITH_CONST("");
  3278. #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
  3279. rl_completion_append_character = '\0';
  3280. #endif
  3281. }
  3282. /* free storage */
  3283. {
  3284. int i;
  3285. for (i = 0; i < lengthof(previous_words); i++)
  3286. free(previous_words[i]);
  3287. }
  3288. /* Return our Grand List O' Matches */
  3289. return matches;
  3290. }
  3291. /*
  3292. * GENERATOR FUNCTIONS
  3293. *
  3294. * These functions do all the actual work of completing the input. They get
  3295. * passed the text so far and the count how many times they have been called
  3296. * so far with the same text.
  3297. * If you read the above carefully, you'll see that these don't get called
  3298. * directly but through the readline interface.
  3299. * The return value is expected to be the full completion of the text, going
  3300. * through a list each time, or NULL if there are no more matches. The string
  3301. * will be free()'d by readline, so you must run it through strdup() or
  3302. * something of that sort.
  3303. */
  3304. /*
  3305. * Common routine for create_command_generator and drop_command_generator.
  3306. * Entries that have 'excluded' flags are not returned.
  3307. */
  3308. static char *
  3309. create_or_drop_command_generator(const char *text, int state, bits32 excluded)
  3310. {
  3311. static int list_index,
  3312. string_length;
  3313. const char *name;
  3314. /* If this is the first time for this completion, init some values */
  3315. if (state == 0)
  3316. {
  3317. list_index = 0;
  3318. string_length = strlen(text);
  3319. }
  3320. /* find something that matches */
  3321. while ((name = words_after_create[list_index++].name))
  3322. {
  3323. if ((pg_strncasecmp(name, text, string_length) == 0) &&
  3324. !(words_after_create[list_index - 1].flags & excluded))
  3325. return pg_strdup_keyword_case(name, text);
  3326. }
  3327. /* if nothing matches, return NULL */
  3328. return NULL;
  3329. }
  3330. /*
  3331. * This one gives you one from a list of things you can put after CREATE
  3332. * as defined above.
  3333. */
  3334. static char *
  3335. create_command_generator(const char *text, int state)
  3336. {
  3337. return create_or_drop_command_generator(text, state, THING_NO_CREATE);
  3338. }
  3339. /*
  3340. * This function gives you a list of things you can put after a DROP command.
  3341. */
  3342. static char *
  3343. drop_command_generator(const char *text, int state)
  3344. {
  3345. return create_or_drop_command_generator(text, state, THING_NO_DROP);
  3346. }
  3347. /* The following two functions are wrappers for _complete_from_query */
  3348. static char *
  3349. complete_from_query(const char *text, int state)
  3350. {
  3351. return _complete_from_query(0, text, state);
  3352. }
  3353. static char *
  3354. complete_from_schema_query(const char *text, int state)
  3355. {
  3356. return _complete_from_query(1, text, state);
  3357. }
  3358. /*
  3359. * This creates a list of matching things, according to a query pointed to
  3360. * by completion_charp.
  3361. * The query can be one of two kinds:
  3362. *
  3363. * 1. A simple query which must contain a %d and a %s, which will be replaced
  3364. * by the string length of the text and the text itself. The query may also
  3365. * have up to four more %s in it; the first two such will be replaced by the
  3366. * value of completion_info_charp, the next two by the value of
  3367. * completion_info_charp2.
  3368. *
  3369. * 2. A schema query used for completion of both schema and relation names.
  3370. * These are more complex and must contain in the following order:
  3371. * %d %s %d %s %d %s %s %d %s
  3372. * where %d is the string length of the text and %s the text itself.
  3373. *
  3374. * It is assumed that strings should be escaped to become SQL literals
  3375. * (that is, what is in the query is actually ... '%s' ...)
  3376. *
  3377. * See top of file for examples of both kinds of query.
  3378. */
  3379. static char *
  3380. _complete_from_query(int is_schema_query, const char *text, int state)
  3381. {
  3382. static int list_index,
  3383. string_length;
  3384. static PGresult *result = NULL;
  3385. /*
  3386. * If this is the first time for this completion, we fetch a list of our
  3387. * "things" from the backend.
  3388. */
  3389. if (state == 0)
  3390. {
  3391. PQExpBufferData query_buffer;
  3392. char *e_text;
  3393. char *e_info_charp;
  3394. char *e_info_charp2;
  3395. list_index = 0;
  3396. string_length = strlen(text);
  3397. /* Free any prior result */
  3398. PQclear(result);
  3399. result = NULL;
  3400. /* Set up suitably-escaped copies of textual inputs */
  3401. e_text = pg_malloc(string_length * 2 + 1);
  3402. PQescapeString(e_text, text, string_length);
  3403. if (completion_info_charp)
  3404. {
  3405. size_t charp_len;
  3406. charp_len = strlen(completion_info_charp);
  3407. e_info_charp = pg_malloc(charp_len * 2 + 1);
  3408. PQescapeString(e_info_charp, completion_info_charp,
  3409. charp_len);
  3410. }
  3411. else
  3412. e_info_charp = NULL;
  3413. if (completion_info_charp2)
  3414. {
  3415. size_t charp_len;
  3416. charp_len = strlen(completion_info_charp2);
  3417. e_info_charp2 = pg_malloc(charp_len * 2 + 1);
  3418. PQescapeString(e_info_charp2, completion_info_charp2,
  3419. charp_len);
  3420. }
  3421. else
  3422. e_info_charp2 = NULL;
  3423. initPQExpBuffer(&query_buffer);
  3424. if (is_schema_query)
  3425. {
  3426. /* completion_squery gives us the pieces to assemble */
  3427. const char *qualresult = completion_squery->qualresult;
  3428. if (qualresult == NULL)
  3429. qualresult = completion_squery->result;
  3430. /* Get unqualified names matching the input-so-far */
  3431. appendPQExpBuffer(&query_buffer, "SELECT %s FROM %s WHERE ",
  3432. completion_squery->result,
  3433. completion_squery->catname);
  3434. if (completion_squery->selcondition)
  3435. appendPQExpBuffer(&query_buffer, "%s AND ",
  3436. completion_squery->selcondition);
  3437. appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s'",
  3438. completion_squery->result,
  3439. string_length, e_text);
  3440. appendPQExpBuffer(&query_buffer, " AND %s",
  3441. completion_squery->viscondition);
  3442. /*
  3443. * When fetching relation names, suppress system catalogs unless
  3444. * the input-so-far begins with "pg_". This is a compromise
  3445. * between not offering system catalogs for completion at all, and
  3446. * having them swamp the result when the input is just "p".
  3447. */
  3448. if (strcmp(completion_squery->catname,
  3449. "pg_catalog.pg_class c") == 0 &&
  3450. strncmp(text, "pg_", 3) !=0)
  3451. {
  3452. appendPQExpBufferStr(&query_buffer,
  3453. " AND c.relnamespace <> (SELECT oid FROM"
  3454. " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
  3455. }
  3456. /*
  3457. * Add in matching schema names, but only if there is more than
  3458. * one potential match among schema names.
  3459. */
  3460. appendPQExpBuffer(&query_buffer, "\nUNION\n"
  3461. "SELECT pg_catalog.quote_ident(n.nspname) || '.' "
  3462. "FROM pg_catalog.pg_namespace n "
  3463. "WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'",
  3464. string_length, e_text);
  3465. appendPQExpBuffer(&query_buffer,
  3466. " AND (SELECT pg_catalog.count(*)"
  3467. " FROM pg_catalog.pg_namespace"
  3468. " WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
  3469. " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1",
  3470. string_length, e_text);
  3471. /*
  3472. * Add in matching qualified names, but only if there is exactly
  3473. * one schema matching the input-so-far.
  3474. */
  3475. appendPQExpBuffer(&query_buffer, "\nUNION\n"
  3476. "SELECT pg_catalog.quote_ident(n.nspname) || '.' || %s "
  3477. "FROM %s, pg_catalog.pg_namespace n "
  3478. "WHERE %s = n.oid AND ",
  3479. qualresult,
  3480. completion_squery->catname,
  3481. completion_squery->namespace);
  3482. if (completion_squery->selcondition)
  3483. appendPQExpBuffer(&query_buffer, "%s AND ",
  3484. completion_squery->selcondition);
  3485. appendPQExpBuffer(&query_buffer, "substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'",
  3486. qualresult,
  3487. string_length, e_text);
  3488. /*
  3489. * This condition exploits the single-matching-schema rule to
  3490. * speed up the query
  3491. */
  3492. appendPQExpBuffer(&query_buffer,
  3493. " AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) ="
  3494. " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)",
  3495. string_length, e_text);
  3496. appendPQExpBuffer(&query_buffer,
  3497. " AND (SELECT pg_catalog.count(*)"
  3498. " FROM pg_catalog.pg_namespace"
  3499. " WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
  3500. " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1",
  3501. string_length, e_text);
  3502. /* If an addon query was provided, use it */
  3503. if (completion_charp)
  3504. appendPQExpBuffer(&query_buffer, "\n%s", completion_charp);
  3505. }
  3506. else
  3507. {
  3508. /* completion_charp is an sprintf-style format string */
  3509. appendPQExpBuffer(&query_buffer, completion_charp,
  3510. string_length, e_text,
  3511. e_info_charp, e_info_charp,
  3512. e_info_charp2, e_info_charp2);
  3513. }
  3514. /* Limit the number of records in the result */
  3515. appendPQExpBuffer(&query_buffer, "\nLIMIT %d",
  3516. completion_max_records);
  3517. result = exec_query(query_buffer.data);
  3518. termPQExpBuffer(&query_buffer);
  3519. free(e_text);
  3520. if (e_info_charp)
  3521. free(e_info_charp);
  3522. if (e_info_charp2)
  3523. free(e_info_charp2);
  3524. }
  3525. /* Find something that matches */
  3526. if (result && PQresultStatus(result) == PGRES_TUPLES_OK)
  3527. {
  3528. const char *item;
  3529. while (list_index < PQntuples(result) &&
  3530. (item = PQgetvalue(result, list_index++, 0)))
  3531. if (pg_strncasecmp(text, item, string_length) == 0)
  3532. return pg_strdup(item);
  3533. }
  3534. /* If nothing matches, free the db structure and return null */
  3535. PQclear(result);
  3536. result = NULL;
  3537. return NULL;
  3538. }
  3539. /*
  3540. * This function returns in order one of a fixed, NULL pointer terminated list
  3541. * of strings (if matching). This can be used if there are only a fixed number
  3542. * SQL words that can appear at certain spot.
  3543. */
  3544. static char *
  3545. complete_from_list(const char *text, int state)
  3546. {
  3547. static int string_length,
  3548. list_index,
  3549. matches;
  3550. static bool casesensitive;
  3551. const char *item;
  3552. /* need to have a list */
  3553. Assert(completion_charpp != NULL);
  3554. /* Initialization */
  3555. if (state == 0)
  3556. {
  3557. list_index = 0;
  3558. string_length = strlen(text);
  3559. casesensitive = completion_case_sensitive;
  3560. matches = 0;
  3561. }
  3562. while ((item = completion_charpp[list_index++]))
  3563. {
  3564. /* First pass is case sensitive */
  3565. if (casesensitive && strncmp(text, item, string_length) == 0)
  3566. {
  3567. matches++;
  3568. return pg_strdup(item);
  3569. }
  3570. /* Second pass is case insensitive, don't bother counting matches */
  3571. if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0)
  3572. {
  3573. if (completion_case_sensitive)
  3574. return pg_strdup(item);
  3575. else
  3576. /*
  3577. * If case insensitive matching was requested initially,
  3578. * adjust the case according to setting.
  3579. */
  3580. return pg_strdup_keyword_case(item, text);
  3581. }
  3582. }
  3583. /*
  3584. * No matches found. If we're not case insensitive already, lets switch to
  3585. * being case insensitive and try again
  3586. */
  3587. if (casesensitive && matches == 0)
  3588. {
  3589. casesensitive = false;
  3590. list_index = 0;
  3591. state++;
  3592. return complete_from_list(text, state);
  3593. }
  3594. /* If no more matches, return null. */
  3595. return NULL;
  3596. }
  3597. /*
  3598. * This function returns one fixed string the first time even if it doesn't
  3599. * match what's there, and nothing the second time. This should be used if
  3600. * there is only one possibility that can appear at a certain spot, so
  3601. * misspellings will be overwritten. The string to be passed must be in
  3602. * completion_charp.
  3603. */
  3604. static char *
  3605. complete_from_const(const char *text, int state)
  3606. {
  3607. Assert(completion_charp != NULL);
  3608. if (state == 0)
  3609. {
  3610. if (completion_case_sensitive)
  3611. return pg_strdup(completion_charp);
  3612. else
  3613. /*
  3614. * If case insensitive matching was requested initially, adjust
  3615. * the case according to setting.
  3616. */
  3617. return pg_strdup_keyword_case(completion_charp, text);
  3618. }
  3619. else
  3620. return NULL;
  3621. }
  3622. /*
  3623. * This function supports completion with the name of a psql variable.
  3624. * The variable names can be prefixed and suffixed with additional text
  3625. * to support quoting usages.
  3626. */
  3627. static char **
  3628. complete_from_variables(const char *text, const char *prefix, const char *suffix)
  3629. {
  3630. char **matches;
  3631. char **varnames;
  3632. int nvars = 0;
  3633. int maxvars = 100;
  3634. int i;
  3635. struct _variable *ptr;
  3636. varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
  3637. for (ptr = pset.vars->next; ptr; ptr = ptr->next)
  3638. {
  3639. if (nvars >= maxvars)
  3640. {
  3641. maxvars *= 2;
  3642. varnames = (char **) realloc(varnames,
  3643. (maxvars + 1) * sizeof(char *));
  3644. if (!varnames)
  3645. {
  3646. psql_error("out of memory\n");
  3647. exit(EXIT_FAILURE);
  3648. }
  3649. }
  3650. varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
  3651. }
  3652. varnames[nvars] = NULL;
  3653. COMPLETE_WITH_LIST_CS((const char *const *) varnames);
  3654. for (i = 0; i < nvars; i++)
  3655. free(varnames[i]);
  3656. free(varnames);
  3657. return matches;
  3658. }
  3659. /*
  3660. * This function wraps rl_filename_completion_function() to strip quotes from
  3661. * the input before searching for matches and to quote any matches for which
  3662. * the consuming command will require it.
  3663. */
  3664. static char *
  3665. complete_from_files(const char *text, int state)
  3666. {
  3667. static const char *unquoted_text;
  3668. char *unquoted_match;
  3669. char *ret = NULL;
  3670. if (state == 0)
  3671. {
  3672. /* Initialization: stash the unquoted input. */
  3673. unquoted_text = strtokx(text, "", NULL, "'", *completion_charp,
  3674. false, true, pset.encoding);
  3675. /* expect a NULL return for the empty string only */
  3676. if (!unquoted_text)
  3677. {
  3678. Assert(*text == '\0');
  3679. unquoted_text = text;
  3680. }
  3681. }
  3682. unquoted_match = filename_completion_function(unquoted_text, state);
  3683. if (unquoted_match)
  3684. {
  3685. /*
  3686. * Caller sets completion_charp to a zero- or one-character string
  3687. * containing the escape character. This is necessary since \copy has
  3688. * no escape character, but every other backslash command recognizes
  3689. * "\" as an escape character. Since we have only two callers, don't
  3690. * bother providing a macro to simplify this.
  3691. */
  3692. ret = quote_if_needed(unquoted_match, " \t\r\n\"`",
  3693. '\'', *completion_charp, pset.encoding);
  3694. if (ret)
  3695. free(unquoted_match);
  3696. else
  3697. ret = unquoted_match;
  3698. }
  3699. return ret;
  3700. }
  3701. /* HELPER FUNCTIONS */
  3702. /*
  3703. * Make a pg_strdup copy of s and convert the case according to
  3704. * COMP_KEYWORD_CASE variable, using ref as the text that was already entered.
  3705. */
  3706. static char *
  3707. pg_strdup_keyword_case(const char *s, const char *ref)
  3708. {
  3709. char *ret,
  3710. *p;
  3711. unsigned char first = ref[0];
  3712. int tocase;
  3713. const char *varval;
  3714. varval = GetVariable(pset.vars, "COMP_KEYWORD_CASE");
  3715. if (!varval)
  3716. tocase = 0;
  3717. else if (strcmp(varval, "lower") == 0)
  3718. tocase = -2;
  3719. else if (strcmp(varval, "preserve-lower") == 0)
  3720. tocase = -1;
  3721. else if (strcmp(varval, "preserve-upper") == 0)
  3722. tocase = +1;
  3723. else if (strcmp(varval, "upper") == 0)
  3724. tocase = +2;
  3725. else
  3726. tocase = 0;
  3727. /* default */
  3728. if (tocase == 0)
  3729. tocase = +1;
  3730. ret = pg_strdup(s);
  3731. if (tocase == -2
  3732. || ((tocase == -1 || tocase == +1) && islower(first))
  3733. || (tocase == -1 && !isalpha(first))
  3734. )
  3735. for (p = ret; *p; p++)
  3736. *p = pg_tolower((unsigned char) *p);
  3737. else
  3738. for (p = ret; *p; p++)
  3739. *p = pg_toupper((unsigned char) *p);
  3740. return ret;
  3741. }
  3742. /*
  3743. * Execute a query and report any errors. This should be the preferred way of
  3744. * talking to the database in this file.
  3745. */
  3746. static PGresult *
  3747. exec_query(const char *query)
  3748. {
  3749. PGresult *result;
  3750. if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK)
  3751. return NULL;
  3752. result = PQexec(pset.db, query);
  3753. if (PQresultStatus(result) != PGRES_TUPLES_OK)
  3754. {
  3755. #ifdef NOT_USED
  3756. psql_error("tab completion query failed: %s\nQuery was:\n%s\n",
  3757. PQerrorMessage(pset.db), query);
  3758. #endif
  3759. PQclear(result);
  3760. result = NULL;
  3761. }
  3762. return result;
  3763. }
  3764. /*
  3765. * Return the nwords word(s) before point. Words are returned right to left,
  3766. * that is, previous_words[0] gets the last word before point.
  3767. * If we run out of words, remaining array elements are set to empty strings.
  3768. * Each array element is filled with a malloc'd string.
  3769. */
  3770. static void
  3771. get_previous_words(int point, char **previous_words, int nwords)
  3772. {
  3773. const char *buf = rl_line_buffer; /* alias */
  3774. int i;
  3775. /* first we look for a non-word char before the current point */
  3776. for (i = point - 1; i >= 0; i--)
  3777. if (strchr(WORD_BREAKS, buf[i]))
  3778. break;
  3779. point = i;
  3780. while (nwords-- > 0)
  3781. {
  3782. int start,
  3783. end;
  3784. char *s;
  3785. /* now find the first non-space which then constitutes the end */
  3786. end = -1;
  3787. for (i = point; i >= 0; i--)
  3788. {
  3789. if (!isspace((unsigned char) buf[i]))
  3790. {
  3791. end = i;
  3792. break;
  3793. }
  3794. }
  3795. /*
  3796. * If no end found we return an empty string, because there is no word
  3797. * before the point
  3798. */
  3799. if (end < 0)
  3800. {
  3801. point = end;
  3802. s = pg_strdup("");
  3803. }
  3804. else
  3805. {
  3806. /*
  3807. * Otherwise we now look for the start. The start is either the
  3808. * last character before any word-break character going backwards
  3809. * from the end, or it's simply character 0. We also handle open
  3810. * quotes and parentheses.
  3811. */
  3812. bool inquotes = false;
  3813. int parentheses = 0;
  3814. for (start = end; start > 0; start--)
  3815. {
  3816. if (buf[start] == '"')
  3817. inquotes = !inquotes;
  3818. if (!inquotes)
  3819. {
  3820. if (buf[start] == ')')
  3821. parentheses++;
  3822. else if (buf[start] == '(')
  3823. {
  3824. if (--parentheses <= 0)
  3825. break;
  3826. }
  3827. else if (parentheses == 0 &&
  3828. strchr(WORD_BREAKS, buf[start - 1]))
  3829. break;
  3830. }
  3831. }
  3832. point = start - 1;
  3833. /* make a copy of chars from start to end inclusive */
  3834. s = pg_malloc(end - start + 2);
  3835. strlcpy(s, &buf[start], end - start + 2);
  3836. }
  3837. *previous_words++ = s;
  3838. }
  3839. }
  3840. #ifdef NOT_USED
  3841. /*
  3842. * Surround a string with single quotes. This works for both SQL and
  3843. * psql internal. Currently disabled because it is reported not to
  3844. * cooperate with certain versions of readline.
  3845. */
  3846. static char *
  3847. quote_file_name(char *text, int match_type, char *quote_pointer)
  3848. {
  3849. char *s;
  3850. size_t length;
  3851. (void) quote_pointer; /* not used */
  3852. length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2);
  3853. s = pg_malloc(length);
  3854. s[0] = '\'';
  3855. strcpy(s + 1, text);
  3856. if (match_type == SINGLE_MATCH)
  3857. s[length - 2] = '\'';
  3858. s[length - 1] = '\0';
  3859. return s;
  3860. }
  3861. static char *
  3862. dequote_file_name(char *text, char quote_char)
  3863. {
  3864. char *s;
  3865. size_t length;
  3866. if (!quote_char)
  3867. return pg_strdup(text);
  3868. length = strlen(text);
  3869. s = pg_malloc(length - 2 + 1);
  3870. strlcpy(s, text +1, length - 2 + 1);
  3871. return s;
  3872. }
  3873. #endif /* NOT_USED */
  3874. #endif /* USE_READLINE */