MCPcopy Create free account
hub / github.com/PostHog/duckgres / buildSessionPgDatabaseViewSQL

Function buildSessionPgDatabaseViewSQL

server/sessionmeta/sessionmeta.go:164–218  ·  view source on GitHub ↗
(database string)

Source from the content-addressed store, hash-verified

162 return count > 0, rows.Err()
163}
164
165// buildSessionMetadataSQL returns a single SQL script containing all the
166// per-session catalog/metadata setup statements concatenated with semicolons.
167// The DuckDB driver executes the script as a multi-statement batch in one
168// ExecContext call (one Flight RPC from the control plane to the worker
169// instead of N), which materially cuts session establishment latency on
170// remote-worker setups where each round-trip is non-trivial.
171//
172// All statements are independent: each is CREATE OR REPLACE VIEW or
173// CREATE TABLE IF NOT EXISTS, with no cross-statement dependencies that would
174// require ordering beyond the order they appear in the script.
175func buildSessionMetadataSQL(database string, accessPolicies ...*MetadataAccessPolicy) string {
176 var access *MetadataAccessPolicy
177 if len(accessPolicies) > 0 {
178 access = accessPolicies[0]
179 }
180 parts := []string{
181 sessionColumnMetadataTableSQL(),
182 buildSessionPgDatabaseViewSQL(database),
183 buildSessionPgClassViewSQL(access),
184 buildSessionPgNamespaceViewSQL(access),
185 buildSessionPgAttributeViewSQL(),
186 buildSessionPgIndexViewSQL(),
187 buildSessionPgTablesViewSQL(access),
188 buildSessionPgViewsViewSQL(access),
189 buildSessionPgSequencesViewSQL(access),
190 buildSessionInformationSchemaColumnsViewSQL(access),
191 buildSessionInformationSchemaTablesViewSQL(access),
192 buildSessionInformationSchemaSchemataViewSQL(access),
193 buildSessionInformationSchemaViewsViewSQL(access),
194 buildSessionInformationSchemaSequencesViewSQL(access),
195 }
196 return strings.Join(parts, ";\n") + ";"
197}
198
199func (p *MetadataAccessPolicy) relationPredicate(schemaExpression, relationExpression string) string {
200 if p == nil {
201 return "TRUE"
202 }
203 schemas := normalizedSQLValues(p.AllowedSchemas)
204 relations := normalizedSQLValues(p.AllowedRelations)
205 parts := make([]string, 0, 2)
206 if len(schemas) > 0 {
207 parts = append(parts, fmt.Sprintf("lower(%s) IN (%s)", schemaExpression, quotedSQLValues(schemas)))
208 }
209 if len(relations) > 0 {
210 parts = append(parts, fmt.Sprintf("lower(%s || '.' || %s) IN (%s)", schemaExpression, relationExpression, quotedSQLValues(relations)))
211 }
212 if len(parts) == 0 {
213 return "FALSE"
214 }
215 return "(" + strings.Join(parts, " OR ") + ")"
216}
217
218func (p *MetadataAccessPolicy) schemaPredicate(schemaExpression string) string {
219 if p == nil {
220 return "TRUE"
221 }

Callers 1

buildSessionMetadataSQLFunction · 0.85

Calls 1

quoteSQLStringLiteralFunction · 0.85

Tested by

no test coverage detected