(ctx context.Context, n ast.Node, query string, migrations []string, ps *named.ParamSet)
| 36 | } |
| 37 | |
| 38 | func (a *Analyzer) Analyze(ctx context.Context, n ast.Node, query string, migrations []string, ps *named.ParamSet) (*core.Analysis, error) { |
| 39 | a.mu.Lock() |
| 40 | defer a.mu.Unlock() |
| 41 | |
| 42 | if a.conn == nil { |
| 43 | var uri string |
| 44 | applyMigrations := a.db.Managed |
| 45 | if a.db.Managed { |
| 46 | // For managed databases, create an in-memory database |
| 47 | uri = ":memory:" |
| 48 | } else if a.dbg.OnlyManagedDatabases { |
| 49 | return nil, fmt.Errorf("database: connections disabled via SQLCDEBUG=databases=managed") |
| 50 | } else { |
| 51 | uri = a.replacer.Replace(a.db.URI) |
| 52 | // For in-memory databases, we need to apply migrations since the database starts empty |
| 53 | if isInMemoryDatabase(uri) { |
| 54 | applyMigrations = true |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | conn, err := sqlite3.Open(uri) |
| 59 | if err != nil { |
| 60 | return nil, fmt.Errorf("failed to open sqlite database: %w", err) |
| 61 | } |
| 62 | a.conn = conn |
| 63 | |
| 64 | // Apply migrations for managed or in-memory databases |
| 65 | if applyMigrations { |
| 66 | for _, m := range migrations { |
| 67 | if len(strings.TrimSpace(m)) == 0 { |
| 68 | continue |
| 69 | } |
| 70 | if err := a.conn.Exec(m); err != nil { |
| 71 | a.conn.Close() |
| 72 | a.conn = nil |
| 73 | return nil, fmt.Errorf("migration failed: %s: %w", m, err) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Prepare the statement to get column and parameter information |
| 80 | stmt, _, err := a.conn.Prepare(query) |
| 81 | if err != nil { |
| 82 | return nil, a.extractSqlErr(n, err) |
| 83 | } |
| 84 | defer stmt.Close() |
| 85 | |
| 86 | var result core.Analysis |
| 87 | |
| 88 | // Get column information |
| 89 | colCount := stmt.ColumnCount() |
| 90 | for i := 0; i < colCount; i++ { |
| 91 | name := stmt.ColumnName(i) |
| 92 | declType := stmt.ColumnDeclType(i) |
| 93 | tableName := stmt.ColumnTableName(i) |
| 94 | originName := stmt.ColumnOriginName(i) |
| 95 | dbName := stmt.ColumnDatabaseName(i) |
nothing calls this directly
no test coverage detected