summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDarik Gamble <darik.gamble.spam@gmail.com>2015-10-26 08:30:59 -0400
committerDarik Gamble <darik.gamble@gmail.com>2015-10-28 10:58:29 -0400
commit7adaa498b294059ea9e2e7aa7070feb9876bf161 (patch)
tree79517470a28a08db9344811b1735a9a8a4bd93a1
parent1ea9bf735bd27566bb89e3b1a540ecd229ac6567 (diff)
Update magic to the latest MetaQuery design
-rw-r--r--pgcli/magic.py39
1 files changed, 25 insertions, 14 deletions
diff --git a/pgcli/magic.py b/pgcli/magic.py
index 898e0092..4a52446e 100644
--- a/pgcli/magic.py
+++ b/pgcli/magic.py
@@ -5,30 +5,31 @@ import logging
_logger = logging.getLogger(__name__)
-def load_ipython_extension(ipython):
- #This is called via the ipython command '%load_ext pgcli.magic'
+def load_ipython_extension(ipython):
+ """This is called via the ipython command '%load_ext pgcli.magic'"""
- #first, load the sql magic if it isn't already loaded
+ # first, load the sql magic if it isn't already loaded
if not ipython.find_line_magic('sql'):
ipython.run_line_magic('load_ext', 'sql')
- #register our own magic
+ # register our own magic
ipython.register_magic_function(pgcli_line_magic, 'line', 'pgcli')
+
def pgcli_line_magic(line):
_logger.debug('pgcli magic called: %r', line)
parsed = sql.parse.parse(line, {})
conn = sql.connection.Connection.get(parsed['connection'])
try:
- #A corresponding pgcli object already exists
+ # A corresponding pgcli object already exists
pgcli = conn._pgcli
_logger.debug('Reusing existing pgcli')
except AttributeError:
- #I can't figure out how to get the underylying psycopg2 connection
- #from the sqlalchemy connection, so just grab the url and make a
- #new connection
+ # I can't figure out how to get the underylying psycopg2 connection
+ # from the sqlalchemy connection, so just grab the url and make a
+ # new connection
pgcli = PGCli()
u = conn.session.engine.url
_logger.debug('New pgcli: %r', str(u))
@@ -36,7 +37,7 @@ def pgcli_line_magic(line):
pgcli.connect(u.database, u.host, u.username, u.port, u.password)
conn._pgcli = pgcli
- #For convenience, print the connection alias
+ # For convenience, print the connection alias
print('Connected: {}'.format(conn.name))
try:
@@ -48,11 +49,21 @@ def pgcli_line_magic(line):
return
q = pgcli.query_history[-1]
- if q.mutating:
- _logger.debug('Mutating query detected -- ignoring')
+
+ if not q.successful:
+ _logger.debug('Unsuccessful query - ignoring')
+ return
+
+ if q.meta_changed or q.db_changed or q.path_changed:
+ _logger.debug('Dangerous query detected -- ignoring')
return
- if q.successful:
- ipython = get_ipython()
- return ipython.run_cell_magic('sql', line, q.query)
+
+ ipython = get_ipython()
+ return ipython.run_cell_magic('sql', line, q.query)
+
+
+
+
+