psqlscan.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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-2016, 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. /* Callback functions to be used by the lexer */
  46. typedef struct PsqlScanCallbacks
  47. {
  48. /* Fetch value of a variable, as a pfree'able string; NULL if unknown */
  49. /* This pointer can be NULL if no variable substitution is wanted */
  50. char *(*get_variable) (const char *varname, bool escape, bool as_ident);
  51. /* Print an error message someplace appropriate */
  52. /* (very old gcc versions don't support attributes on function pointers) */
  53. #if defined(__GNUC__) && __GNUC__ < 4
  54. void (*write_error) (const char *fmt,...);
  55. #else
  56. void (*write_error) (const char *fmt,...) pg_attribute_printf(1, 2);
  57. #endif
  58. } PsqlScanCallbacks;
  59. extern PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks);
  60. extern void psql_scan_destroy(PsqlScanState state);
  61. extern void psql_scan_setup(PsqlScanState state,
  62. const char *line, int line_len,
  63. int encoding, bool std_strings);
  64. extern void psql_scan_finish(PsqlScanState state);
  65. extern PsqlScanResult psql_scan(PsqlScanState state,
  66. PQExpBuffer query_buf,
  67. promptStatus_t *prompt);
  68. extern void psql_scan_reset(PsqlScanState state);
  69. extern void psql_scan_reselect_sql_lexer(PsqlScanState state);
  70. extern bool psql_scan_in_quote(PsqlScanState state);
  71. #endif /* PSQLSCAN_H */