|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + |
| 25 | + "github.com/ajitpratap0/GoSQLX/pkg/gosqlx" |
| 26 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 27 | +) |
| 28 | + |
| 29 | +var transpileCmd = &cobra.Command{ |
| 30 | + Use: "transpile [SQL]", |
| 31 | + Short: "Convert SQL from one dialect to another", |
| 32 | + Long: `Transpile SQL between dialects. |
| 33 | +
|
| 34 | +Supported dialect pairs: |
| 35 | + mysql → postgres |
| 36 | + postgres → mysql |
| 37 | + postgres → sqlite |
| 38 | +
|
| 39 | +SQL can be provided as a positional argument or piped via stdin. |
| 40 | +
|
| 41 | +Examples: |
| 42 | + gosqlx transpile --from mysql --to postgres "CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY)" |
| 43 | + echo "SELECT * FROM users WHERE name ILIKE '%alice%'" | gosqlx transpile --from postgres --to mysql |
| 44 | + gosqlx transpile --from postgres --to sqlite "CREATE TABLE t (id SERIAL PRIMARY KEY, tags TEXT)"`, |
| 45 | + Args: cobra.MaximumNArgs(1), |
| 46 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 47 | + fromStr, _ := cmd.Flags().GetString("from") |
| 48 | + toStr, _ := cmd.Flags().GetString("to") |
| 49 | + |
| 50 | + from, err := parseDialectFlag(fromStr) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("--from: %w", err) |
| 53 | + } |
| 54 | + to, err := parseDialectFlag(toStr) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("--to: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + var sql string |
| 60 | + if len(args) > 0 { |
| 61 | + sql = args[0] |
| 62 | + } else { |
| 63 | + // Read from stdin. |
| 64 | + data, readErr := io.ReadAll(os.Stdin) |
| 65 | + if readErr != nil { |
| 66 | + return fmt.Errorf("reading stdin: %w", readErr) |
| 67 | + } |
| 68 | + sql = strings.TrimSpace(string(data)) |
| 69 | + } |
| 70 | + |
| 71 | + if sql == "" { |
| 72 | + return fmt.Errorf("no SQL provided: pass as argument or via stdin") |
| 73 | + } |
| 74 | + |
| 75 | + result, err := gosqlx.Transpile(sql, from, to) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("transpile: %w", err) |
| 78 | + } |
| 79 | + fmt.Println(result) |
| 80 | + return nil |
| 81 | + }, |
| 82 | +} |
| 83 | + |
| 84 | +func init() { |
| 85 | + transpileCmd.Flags().String("from", "mysql", "Source dialect (mysql, postgres, sqlite, sqlserver, oracle, snowflake, clickhouse, mariadb)") |
| 86 | + transpileCmd.Flags().String("to", "postgres", "Target dialect (mysql, postgres, sqlite, sqlserver, oracle, snowflake, clickhouse, mariadb)") |
| 87 | + rootCmd.AddCommand(transpileCmd) |
| 88 | +} |
| 89 | + |
| 90 | +// parseDialectFlag converts a dialect name string to a keywords.SQLDialect value. |
| 91 | +func parseDialectFlag(s string) (keywords.SQLDialect, error) { |
| 92 | + switch strings.ToLower(s) { |
| 93 | + case "mysql": |
| 94 | + return keywords.DialectMySQL, nil |
| 95 | + case "postgres", "postgresql": |
| 96 | + return keywords.DialectPostgreSQL, nil |
| 97 | + case "sqlite": |
| 98 | + return keywords.DialectSQLite, nil |
| 99 | + case "sqlserver", "mssql": |
| 100 | + return keywords.DialectSQLServer, nil |
| 101 | + case "oracle": |
| 102 | + return keywords.DialectOracle, nil |
| 103 | + case "snowflake": |
| 104 | + return keywords.DialectSnowflake, nil |
| 105 | + case "clickhouse": |
| 106 | + return keywords.DialectClickHouse, nil |
| 107 | + case "mariadb": |
| 108 | + return keywords.DialectMariaDB, nil |
| 109 | + default: |
| 110 | + return keywords.DialectGeneric, fmt.Errorf( |
| 111 | + "unknown dialect %q; valid: mysql, postgres, sqlite, sqlserver, oracle, snowflake, clickhouse, mariadb", s) |
| 112 | + } |
| 113 | +} |
0 commit comments