2019-10-23 21:11:44 +00:00
|
|
|
import logging
|
2018-09-28 21:18:40 +00:00
|
|
|
|
|
|
|
from prompt_toolkit.enums import DEFAULT_BUFFER
|
2015-05-02 15:01:57 +00:00
|
|
|
from prompt_toolkit.filters import Condition
|
2018-09-28 21:18:40 +00:00
|
|
|
from prompt_toolkit.application import get_app
|
2016-07-22 11:09:17 +00:00
|
|
|
from .packages.parseutils.utils import is_open_quote
|
2015-08-18 00:42:12 +00:00
|
|
|
|
2019-10-23 21:11:44 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2014-12-02 05:55:13 +00:00
|
|
|
|
2015-08-18 00:42:12 +00:00
|
|
|
def _is_complete(sql):
|
|
|
|
# A complete command is an sql statement that ends with a semicolon, unless
|
|
|
|
# there's an open quote surrounding it, as is common when writing a
|
|
|
|
# CREATE FUNCTION command
|
2019-05-25 20:08:56 +00:00
|
|
|
return sql.endswith(";") and not is_open_quote(sql)
|
2015-08-18 00:42:12 +00:00
|
|
|
|
|
|
|
|
2019-10-13 00:07:57 +00:00
|
|
|
"""
|
2019-10-16 21:26:25 +00:00
|
|
|
Returns True if the buffer contents should be handled (i.e. the query/command
|
|
|
|
executed) immediately. This is necessary as we use prompt_toolkit in multiline
|
|
|
|
mode, which by default will insert new lines on Enter.
|
2019-10-13 00:07:57 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2022-09-08 22:14:42 +00:00
|
|
|
def safe_multi_line_mode(pgcli):
|
|
|
|
@Condition
|
|
|
|
def cond():
|
2022-09-14 21:58:45 +00:00
|
|
|
_logger.debug(
|
|
|
|
'Multi-line mode state: "%s" / "%s"', pgcli.multi_line, pgcli.multiline_mode
|
|
|
|
)
|
2022-09-08 22:14:42 +00:00
|
|
|
return pgcli.multi_line and (pgcli.multiline_mode == "safe")
|
2022-09-14 21:58:45 +00:00
|
|
|
|
2022-09-08 22:14:42 +00:00
|
|
|
return cond
|
|
|
|
|
|
|
|
|
2019-10-16 21:26:25 +00:00
|
|
|
def buffer_should_be_handled(pgcli):
|
2019-10-13 00:07:57 +00:00
|
|
|
@Condition
|
|
|
|
def cond():
|
2019-10-16 21:26:25 +00:00
|
|
|
if not pgcli.multi_line:
|
2019-10-23 21:11:44 +00:00
|
|
|
_logger.debug("Not in multi-line mode. Handle the buffer.")
|
2019-10-16 21:26:25 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
if pgcli.multiline_mode == "safe":
|
2019-10-23 21:11:44 +00:00
|
|
|
_logger.debug("Multi-line mode is set to 'safe'. Do NOT handle the buffer.")
|
2019-10-13 00:07:57 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
doc = get_app().layout.get_buffer_by_name(DEFAULT_BUFFER).document
|
|
|
|
text = doc.text.strip()
|
|
|
|
|
|
|
|
return (
|
|
|
|
text.startswith("\\") # Special Command
|
|
|
|
or text.endswith(r"\e") # Special Command
|
|
|
|
or text.endswith(r"\G") # Ended with \e which should launch the editor
|
|
|
|
or _is_complete(text) # A complete SQL command
|
|
|
|
or (text == "exit") # Exit doesn't need semi-colon
|
|
|
|
or (text == "quit") # Quit doesn't need semi-colon
|
|
|
|
or (text == ":q") # To all the vim fans out there
|
|
|
|
or (text == "") # Just a plain enter without any text
|
|
|
|
)
|
|
|
|
|
|
|
|
return cond
|