MCPcopy
hub / github.com/sqldef/sqldef / getPolicyDefsForTables

Method getPolicyDefsForTables

database/postgres/database.go:1606–1645  ·  view source on GitHub ↗
(tableNames []string)

Source from the content-addressed store, hash-verified

1604}
1605
1606func (d *PostgresDatabase) getPolicyDefsForTables(tableNames []string) (map[string][]string, error) {
1607 const query = `
1608 SELECT schemaname || '.' || tablename AS qualified_table_name, schemaname, tablename, policyname, permissive, roles, cmd, qual, with_check
1609 FROM pg_policies
1610 WHERE schemaname || '.' || tablename = ANY($1::text[])
1611 `
1612 rows, err := d.db.Query(query, pq.Array(tableNames))
1613 if err != nil {
1614 return nil, err
1615 }
1616 defer rows.Close()
1617
1618 result := make(map[string][]string, len(tableNames))
1619 for rows.Next() {
1620 var (
1621 qualifiedTableName, schemaName, tableName, policyName, permissive, roles, cmd string
1622 using, withCheck sql.NullString
1623 )
1624 err = rows.Scan(&qualifiedTableName, &schemaName, &tableName, &policyName, &permissive, &roles, &cmd, &using, &withCheck)
1625 if err != nil {
1626 return nil, err
1627 }
1628 roles = policyRolesPrefixRegex.ReplaceAllString(roles, "")
1629 roles = policyRolesSuffixRegex.ReplaceAllString(roles, "")
1630 // Qualify the table name with its schema; otherwise policies on tables
1631 // outside the default schema resolve to the wrong table when re-parsed
1632 def := fmt.Sprintf(
1633 "CREATE POLICY %s ON %s.%s AS %s FOR %s TO %s",
1634 policyName, d.quoteIdentifierIfNeeded(schemaName), d.quoteIdentifierIfNeeded(tableName), permissive, cmd, roles,
1635 )
1636 if using.Valid {
1637 def += fmt.Sprintf(" USING (%s)", using.String)
1638 }
1639 if withCheck.Valid {
1640 def += fmt.Sprintf(" WITH CHECK %s", withCheck.String)
1641 }
1642 result[qualifiedTableName] = append(result[qualifiedTableName], def+";")
1643 }
1644 return result, nil
1645}
1646
1647func (d *PostgresDatabase) getCheckConstraintsForTables(tableNames []string) (map[string][]CheckConstraint, error) {
1648 const query = `SELECT nsp.nspname || '.' || cls.relname AS qualified_table_name, con.conname, pg_get_constraintdef(con.oid, true)

Callers 1

Calls 5

QueryMethod · 0.80
NextMethod · 0.80
ScanMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected