psqlscan.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*-------------------------------------------------------------------------
  2. *
  3. * psqlscan.h
  4. * lexical scanner for SQL commands
  5. *
  6. * This lexer used to be part of psql, and that heritage is reflected in
  7. * the file name as well as function and typedef names, though it can now
  8. * be used by other frontend programs as well. It's also possible to extend
  9. * this lexer with a compatible add-on lexer to handle program-specific
  10. * backslash commands.
  11. *
  12. *
  13. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  14. * Portions Copyright (c) 1994, Regents of the University of California
  15. *
  16. * src/include/fe_utils/psqlscan.h
  17. *
  18. *-------------------------------------------------------------------------
  19. */
  20. #ifndef PSQLSCAN_H
  21. #define PSQLSCAN_H
  22. #include "pqexpbuffer.h"
  23. /* Abstract type for lexer's internal state */
  24. typedef struct PsqlScanStateData *PsqlScanState;
  25. /* Termination states for psql_scan() */
  26. typedef enum
  27. {
  28. PSCAN_SEMICOLON, /* found command-ending semicolon */
  29. PSCAN_BACKSLASH, /* found backslash command */
  30. PSCAN_INCOMPLETE, /* end of line, SQL statement incomplete */
  31. PSCAN_EOL /* end of line, SQL possibly complete */
  32. } PsqlScanResult;
  33. /* Prompt type returned by psql_scan() */
  34. typedef enum _promptStatus
  35. {
  36. PROMPT_READY,
  37. PROMPT_CONTINUE,
  38. PROMPT_COMMENT,
  39. PROMPT_SINGLEQUOTE,
  40. PROMPT_DOUBLEQUOTE,
  41. PROMPT_DOLLARQUOTE,
  42. PROMPT_PAREN,
  43. PROMPT_COPY
  44. } promptStatus_t;
  45. /* Quoting request types for get_variable() callback */
  46. typedef enum
  47. {
  48. PQUOTE_PLAIN, /* just return the actual value */
  49. PQUOTE_SQL_LITERAL, /* add quotes to make a valid SQL literal */
  50. PQUOTE_SQL_IDENT, /* quote if needed to make a SQL identifier */
  51. PQUOTE_SHELL_ARG /* quote if needed to be safe in a shell cmd */
  52. } PsqlScanQuoteType;
  53. /* Callback functions to be used by the lexer */
  54. typedef struct PsqlScanCallbacks
  55. {
  56. /* Fetch value of a variable, as a free'able string; NULL if unknown */
  57. /* This pointer can be NULL if no variable substitution is wanted */
  58. char *(*get_variable) (const char *varname, PsqlScanQuoteType quote,
  59. void *passthrough);
  60. } PsqlScanCallbacks;
  61. extern PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks);
  62. extern void psql_scan_destroy(PsqlScanState state);
  63. extern void psql_scan_set_passthrough(PsqlScanState state, void *passthrough);
  64. extern void psql_scan_setup(PsqlScanState state,
  65. const char *line, int line_len,
  66. int encoding, bool std_strings);
  67. extern void psql_scan_finish(PsqlScanState state);
  68. extern PsqlScanResult psql_scan(PsqlScanState state,
  69. PQExpBuffer query_buf,
  70. promptStatus_t *prompt);
  71. extern void psql_scan_reset(PsqlScanState state);
  72. extern void psql_scan_reselect_sql_lexer(PsqlScanState state);
  73. extern bool psql_scan_in_quote(PsqlScanState state);
  74. #endif /* PSQLSCAN_H */