|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/davecgh/go-spew/spew" |
| 8 | + "github.com/kanmu/go-sqlfmt/sqlfmt" |
| 9 | + "github.com/specterops/dawgs/cypher/models/pgsql/format" |
| 10 | + "github.com/specterops/dawgs/cypher/models/pgsql/translate" |
| 11 | + "github.com/specterops/dawgs/graph" |
| 12 | + |
| 13 | + "github.com/specterops/dawgs/tools/dawgrun/pkg/stubs" |
| 14 | +) |
| 15 | + |
| 16 | +func parseCmd() CommandDesc { |
| 17 | + return CommandDesc{ |
| 18 | + args: []string{"<...query>"}, |
| 19 | + help: "Parses and dumps a Cypher query to AST form.", |
| 20 | + |
| 21 | + Fn: func(ctx *CommandContext, fields []string) error { |
| 22 | + query, err := parseQueryArray(fields) |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("error trying to parse query '%s': %w", fields, err) |
| 25 | + } |
| 26 | + |
| 27 | + ctx.output.WriteHighlighted(spew.Sdump(query), "golang", "monokai") |
| 28 | + return nil |
| 29 | + }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func translateToPsqlCmd() CommandDesc { |
| 34 | + flagSet := flag.NewFlagSet("translate-psql", flag.ContinueOnError) |
| 35 | + |
| 36 | + var ( |
| 37 | + kindMapperConnRef = "" |
| 38 | + dumpTranslatedAst = false |
| 39 | + ) |
| 40 | + flagSet.StringVar(&kindMapperConnRef, "conn", "", "Connection reference for choosing a kind mapper") |
| 41 | + flagSet.BoolVar(&dumpTranslatedAst, "dump-pg-ast", false, "Whether to dump the translator's constructed AST") |
| 42 | + |
| 43 | + return CommandDesc{ |
| 44 | + args: []string{"[flags]", "<...query>"}, |
| 45 | + help: "Parses a query and converts it to the underlying PostgreSQL query", |
| 46 | + desc: "Does a bunch of magic to fully translate a Cypher query into a PostgreSQL query", |
| 47 | + flags: flagSet, |
| 48 | + |
| 49 | + Fn: func(ctx *CommandContext, fields []string) error { |
| 50 | + if err := flagSet.Parse(fields); err != nil { |
| 51 | + return fmt.Errorf("could not parse flags: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + fields = flagSet.Args() |
| 55 | + query, err := parseQueryArray(fields) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("error trying to parse query '%s': %w", fields, err) |
| 58 | + } |
| 59 | + |
| 60 | + kindMapper := stubs.EmptyMapper() |
| 61 | + if kindMapperConnRef != "" { |
| 62 | + // Fetch kinds regardless of if it's already loaded. |
| 63 | + kindMap, err := loadKindMap(ctx, kindMapperConnRef) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("could not load kind map for explain: %w", err) |
| 66 | + } |
| 67 | + kindMapper = stubs.MapperFromKindMap(kindMap) |
| 68 | + } |
| 69 | + |
| 70 | + result, err := translate.Translate(ctx, query, kindMapper, nil) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("could not translate cypher query to pgsql: %w", err) |
| 73 | + } |
| 74 | + if dumpTranslatedAst { |
| 75 | + fmt.Fprintf(ctx.output, "TRANSLATOR AST\n\n") |
| 76 | + ctx.output.WriteHighlighted(spew.Sdump(result.Statement), "golang", "monokai") |
| 77 | + fmt.Fprintf(ctx.output, "\n") |
| 78 | + } |
| 79 | + |
| 80 | + sqlQuery, err := format.SyntaxNode(result.Statement) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("could not format translated statement into a string query: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + formattedQuery, err := sqlfmt.Format(sqlQuery, &sqlfmt.Options{ |
| 86 | + Distance: 0, |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + ctx.output.Warnf("could not format query: %s", err.Error()) |
| 90 | + formattedQuery = sqlQuery |
| 91 | + } |
| 92 | + |
| 93 | + ctx.output.WriteHighlighted(formattedQuery, "postgres", "monokai") |
| 94 | + return nil |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func explainAsPsqlCmd() CommandDesc { |
| 100 | + return CommandDesc{ |
| 101 | + args: []string{"<conn>", "<...query>"}, |
| 102 | + help: "Explains a translated query over an active PG connection", |
| 103 | + desc: "Asks the PG query planner to explain the (translated) Cypher query in PG terms", |
| 104 | + |
| 105 | + Fn: func(ctx *CommandContext, fields []string) error { |
| 106 | + if len(fields) < 2 { |
| 107 | + return fmt.Errorf("invalid usage, requires: <connection name> <query>") |
| 108 | + } |
| 109 | + |
| 110 | + connName := fields[0] |
| 111 | + conn, ok := ctx.scope.connections[connName] |
| 112 | + if !ok { |
| 113 | + return fmt.Errorf("connection %s not found; did you `open` it?", connName) |
| 114 | + } |
| 115 | + |
| 116 | + // Fetch kinds regardless of if it's already loaded. |
| 117 | + kindMap, err := loadKindMap(ctx, connName) |
| 118 | + if err != nil { |
| 119 | + return fmt.Errorf("could not load kind map for explain: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + query, err := parseQueryArray(fields[1:]) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("could not parse query: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + // Populate a DumbKindMapper from the database's kinds table |
| 128 | + kindMapper := stubs.MapperFromKindMap(kindMap) |
| 129 | + result, err := translate.Translate(ctx, query, kindMapper, nil) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("could not translate cypher query to pgsql: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + sqlQuery, err := format.SyntaxNode(result.Statement) |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("could not format translated statement into a string query: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + formattedQuery, err := sqlfmt.Format(sqlQuery, &sqlfmt.Options{ |
| 140 | + Distance: 2, |
| 141 | + }) |
| 142 | + if err != nil { |
| 143 | + ctx.output.Warnf("could not format query: %s", err.Error()) |
| 144 | + formattedQuery = sqlQuery |
| 145 | + } |
| 146 | + explainSQLQuery := fmt.Sprintf("EXPLAIN %s", formattedQuery) |
| 147 | + ctx.output.WriteHighlighted(explainSQLQuery, "postgres", "monokai") |
| 148 | + fmt.Fprint(ctx.output, "\n\n") |
| 149 | + |
| 150 | + err = conn.ReadTransaction(ctx, func(tx graph.Transaction) error { |
| 151 | + result := tx.Raw(explainSQLQuery, nil) |
| 152 | + if err := result.Error(); err != nil { |
| 153 | + return fmt.Errorf("error running raw query: '%s': %w", explainSQLQuery, err) |
| 154 | + } |
| 155 | + defer result.Close() |
| 156 | + |
| 157 | + var value string |
| 158 | + for result.Next() { |
| 159 | + graph.ScanNextResult(result, &value) |
| 160 | + fmt.Fprintf(ctx.output, " %s\n", value) |
| 161 | + } |
| 162 | + |
| 163 | + return nil |
| 164 | + }) |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("could not run EXPLAIN query: %w", err) |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | + }, |
| 171 | + } |
| 172 | +} |
0 commit comments