Returns the SQL defining views described by `spec`
(self, spec)
| 493 | return cur.fetchone()[0] |
| 494 | |
| 495 | def view_definition(self, spec): |
| 496 | """Returns the SQL defining views described by `spec`""" |
| 497 | |
| 498 | # 2: relkind, v or m (materialized) |
| 499 | # 4: reloptions, null |
| 500 | # 5: checkoption: local or cascaded |
| 501 | with self.conn.cursor() as cur: |
| 502 | sql = self.view_definition_query |
| 503 | _logger.debug("View Definition Query. sql: %r\nspec: %r", sql, spec) |
| 504 | try: |
| 505 | cur.execute(sql, (spec,)) |
| 506 | except psycopg.ProgrammingError: |
| 507 | raise RuntimeError(f"View {spec} does not exist.") |
| 508 | result = ViewDef(*cur.fetchone()) |
| 509 | if result.relkind == "m": |
| 510 | template = "CREATE OR REPLACE MATERIALIZED VIEW {name} AS \n{stmt}" |
| 511 | else: |
| 512 | template = "CREATE OR REPLACE VIEW {name} AS \n{stmt}" |
| 513 | return ( |
| 514 | psycopg.sql |
| 515 | .SQL(template) |
| 516 | .format( |
| 517 | name=psycopg.sql.Identifier(result.nspname, result.relname), |
| 518 | stmt=psycopg.sql.SQL(result.viewdef), |
| 519 | ) |
| 520 | .as_string(self.conn) |
| 521 | ) |
| 522 | |
| 523 | def function_definition(self, spec): |
| 524 | """Returns the SQL defining functions described by `spec`""" |