diff options
author | catherinedevlin <catherine.devlin@gmail.com> | 2018-05-16 01:14:22 -0400 |
---|---|---|
committer | Irina Truong <i.chernyavska@gmail.com> | 2018-06-16 21:46:31 -0700 |
commit | bb8cbac3de146f7503d23e9994522a7a8b610b7b (patch) | |
tree | b08d088b6f0764b6e13887bdaf15eaca8e9468c5 | |
parent | 675e1dbff26c42722493703c7a792bae97c7d538 (diff) |
Added \ef
-rw-r--r-- | pgcli/main.py | 8 | ||||
-rw-r--r-- | pgcli/pgexecute.py | 27 |
2 files changed, 30 insertions, 5 deletions
diff --git a/pgcli/main.py b/pgcli/main.py index e4f0e396..97d27202 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -489,10 +489,10 @@ class PGCli(object): else: # \ev or \ef filename = None spec = document.text.split()[1] - query = self.pgexecute.view_definitions(spec) - if not query: - raise RuntimeError( - 'View {} does not exist.'.format(spec)) + if editor_command == '\\ev': + query = self.pgexecute.view_definitions(spec) + elif editor_command == '\\ef': + query = self.pgexecute.function_definition(spec) sql, message = special.open_external_editor( filename, sql=query) if message: diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index 9ab3d413..917d8f4f 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -162,12 +162,19 @@ class PGExecute(object): FROM pg_settings WHERE name = 'unix_socket_directories' ''' + view_definition_query = ''' SELECT table_schema, table_name, view_definition FROM information_schema.views WHERE table_schema LIKE %s AND table_name LIKE %s''' + function_definition_query = ''' + WITH f AS + (SELECT %s::pg_catalog.regproc::pg_catalog.oid AS f_oid) + SELECT pg_catalog.pg_get_functiondef(f.f_oid) + FROM f''' + def __init__(self, database, user, password, host, port, dsn, **kwargs): self.dbname = database self.user = user @@ -400,7 +407,25 @@ class PGExecute(object): _logger.debug('View Definition Query. sql: %r\nschema: %r\nview: %r', sql, schema_pattern, view_pattern) cur.execute(sql, (schema_pattern, view_pattern)) - return ';\n\n'.join(template.format(*row) for row in cur) + result = ';\n\n'.join(template.format(*row) for row in cur) + if result: + return result + else: + raise RuntimeError('View {} does not exist.'.format(spec)) + + def function_definition(self, spec): + """Returns the SQL defining functions described by `spec` """ + + with self.conn.cursor() as cur: + sql = self.function_definition_query + _logger.debug('Function Definition Query. sql: %r\nspec: %r', + sql, spec) + cur.execute(sql, (spec, )) + result = cur.fetchone() + if result: + return result[0] + else: + raise RuntimeError('Function {} does not exist.'.format(spec)) def schemata(self): """Returns a list of schema names in the database""" |