Don't create a database per query
(ctx context.Context, n ast.Node, query string, migrations []string, ps *named.ParamSet)
| 184 | |
| 185 | // Don't create a database per query |
| 186 | func (a *Analyzer) Analyze(ctx context.Context, n ast.Node, query string, migrations []string, ps *named.ParamSet) (*core.Analysis, error) { |
| 187 | extractSqlErr := func(e error) error { |
| 188 | var pgErr *pgconn.PgError |
| 189 | if errors.As(e, &pgErr) { |
| 190 | return &sqlerr.Error{ |
| 191 | Code: pgErr.Code, |
| 192 | Message: pgErr.Message, |
| 193 | Location: max(n.Pos()+int(pgErr.Position)-1, 0), |
| 194 | } |
| 195 | } |
| 196 | return e |
| 197 | } |
| 198 | |
| 199 | if a.pool == nil { |
| 200 | var uri string |
| 201 | if a.db.Managed { |
| 202 | if a.client == nil { |
| 203 | return nil, fmt.Errorf("client is nil") |
| 204 | } |
| 205 | edb, err := a.client.CreateDatabase(ctx, &dbmanager.CreateDatabaseRequest{ |
| 206 | Engine: "postgresql", |
| 207 | Migrations: migrations, |
| 208 | }) |
| 209 | if err != nil { |
| 210 | return nil, err |
| 211 | } |
| 212 | uri = edb.Uri |
| 213 | } else if a.dbg.OnlyManagedDatabases { |
| 214 | return nil, fmt.Errorf("database: connections disabled via SQLCDEBUG=databases=managed") |
| 215 | } else { |
| 216 | uri = a.replacer.Replace(a.db.URI) |
| 217 | } |
| 218 | conf, err := pgxpool.ParseConfig(uri) |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | pool, err := pgxpool.NewWithConfig(ctx, conf) |
| 223 | if err != nil { |
| 224 | return nil, err |
| 225 | } |
| 226 | a.pool = pool |
| 227 | } |
| 228 | |
| 229 | c, err := a.pool.Acquire(ctx) |
| 230 | if err != nil { |
| 231 | return nil, err |
| 232 | } |
| 233 | defer c.Release() |
| 234 | |
| 235 | // TODO: Pick a random name |
| 236 | desc, err := c.Conn().Prepare(ctx, "foo", query) |
| 237 | if err != nil { |
| 238 | return nil, extractSqlErr(err) |
| 239 | } |
| 240 | |
| 241 | if err := c.Conn().Deallocate(ctx, "foo"); err != nil { |
| 242 | return nil, err |
| 243 | } |
nothing calls this directly
no test coverage detected