(database string)
| 162 | buildSessionInformationSchemaTablesViewSQL(access), |
| 163 | buildSessionInformationSchemaSchemataViewSQL(access), |
| 164 | buildSessionInformationSchemaViewsViewSQL(access), |
| 165 | buildSessionInformationSchemaSequencesViewSQL(access), |
| 166 | } |
| 167 | return strings.Join(parts, ";\n") + ";" |
| 168 | } |
| 169 | |
| 170 | func (p *MetadataAccessPolicy) relationPredicate(schemaExpression, relationExpression string) string { |
| 171 | if p == nil { |
| 172 | return "TRUE" |
| 173 | } |
| 174 | schemas := normalizedSQLValues(p.AllowedSchemas) |
| 175 | relations := normalizedSQLValues(p.AllowedRelations) |
| 176 | parts := make([]string, 0, 2) |
| 177 | if len(schemas) > 0 { |
| 178 | parts = append(parts, fmt.Sprintf("lower(%s) IN (%s)", schemaExpression, quotedSQLValues(schemas))) |
| 179 | } |
| 180 | if len(relations) > 0 { |
| 181 | parts = append(parts, fmt.Sprintf("lower(%s || '.' || %s) IN (%s)", schemaExpression, relationExpression, quotedSQLValues(relations))) |
| 182 | } |
| 183 | if len(parts) == 0 { |
| 184 | return "FALSE" |
| 185 | } |
| 186 | return "(" + strings.Join(parts, " OR ") + ")" |
| 187 | } |
| 188 | |
| 189 | func (p *MetadataAccessPolicy) schemaPredicate(schemaExpression string) string { |
| 190 | if p == nil { |
| 191 | return "TRUE" |
| 192 | } |
| 193 | values := append([]string(nil), p.AllowedSchemas...) |
| 194 | for _, relation := range p.AllowedRelations { |
| 195 | if schema, _, ok := strings.Cut(relation, "."); ok { |
| 196 | values = append(values, schema) |
| 197 | } |
| 198 | } |
| 199 | values = normalizedSQLValues(values) |
| 200 | if len(values) == 0 { |
| 201 | return "FALSE" |
| 202 | } |
| 203 | return fmt.Sprintf("lower(%s) IN (%s)", schemaExpression, quotedSQLValues(values)) |
| 204 | } |
| 205 | |
| 206 | func normalizedSQLValues(values []string) []string { |
| 207 | seen := make(map[string]struct{}, len(values)) |
| 208 | for _, value := range values { |
| 209 | value = strings.ToLower(strings.TrimSpace(value)) |
| 210 | if value != "" { |
| 211 | seen[value] = struct{}{} |
| 212 | } |
| 213 | } |
| 214 | result := make([]string, 0, len(seen)) |
| 215 | for value := range seen { |
| 216 | result = append(result, value) |
| 217 | } |
| 218 | sort.Strings(result) |
| 219 | return result |
| 220 | } |
| 221 |
no test coverage detected