Skip to content

Commit f43764e

Browse files
committed
cli
1 parent 23ad4c2 commit f43764e

File tree

8 files changed

+429
-22
lines changed

8 files changed

+429
-22
lines changed

README.md

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,56 @@
22

33
SQLMapper is a powerful Go library that enables SQL dump conversion between different database systems. It provides a comprehensive solution for database schema migration, comparison, and conversion tasks.
44

5-
## Features
5+
## Command Line Interface (CLI)
66

7-
- **Multi-Database Support**:
8-
- MySQL
9-
- PostgreSQL
10-
- SQLite
11-
- Oracle
12-
- SQL Server
7+
Convert SQL dumps between different database types with a simple command:
138

14-
- **Core Functionalities**:
15-
- SQL dump parsing and conversion
16-
- Schema migration support
17-
- Database schema comparison
18-
- Structured logging system
19-
- Thread-safe operations
9+
```bash
10+
sqlmapper --file=<path_to_sql_file> --to=<target_database>
11+
```
2012

21-
## Installation
13+
### Installation
2214

2315
```bash
24-
go get github.com/mstgnz/sqlmapper
16+
go install github.com/mstgnz/sqlmapper/cmd/sqlmapper@latest
17+
```
18+
19+
### Examples
20+
21+
Convert PostgreSQL dump to MySQL:
22+
```bash
23+
sqlmapper --file=database.sql --to=mysql
24+
# Output: database_mysql.sql
25+
```
26+
27+
Convert MySQL dump to SQLite:
28+
```bash
29+
sqlmapper --file=dump.sql --to=sqlite
30+
# Output: dump_sqlite.sql
31+
```
32+
33+
Convert Oracle dump to PostgreSQL:
34+
```bash
35+
sqlmapper --file=schema.sql --to=postgres
36+
# Output: schema_postgres.sql
2537
```
2638

27-
## Quick Start
39+
### Auto-detection
40+
41+
SQLMapper automatically detects the source database type based on SQL syntax patterns:
42+
- MySQL: Detects `ENGINE=INNODB`
43+
- SQLite: Detects `AUTOINCREMENT`
44+
- SQL Server: Detects `IDENTITY`
45+
- PostgreSQL: Detects `SERIAL`
46+
- Oracle: Detects `NUMBER(` syntax
47+
48+
## Library Usage
49+
50+
### Installation
51+
52+
```bash
53+
go get github.com/mstgnz/sqlmapper
54+
```
2855

2956
### Basic Usage
3057

@@ -57,6 +84,22 @@ comparer := schema.NewSchemaComparer(sourceTables, targetTables)
5784
differences := comparer.Compare()
5885
```
5986

87+
## Features
88+
89+
- **Multi-Database Support**:
90+
- MySQL
91+
- PostgreSQL
92+
- SQLite
93+
- Oracle
94+
- SQL Server
95+
96+
- **Core Functionalities**:
97+
- SQL dump parsing and conversion
98+
- Schema migration support
99+
- Database schema comparison
100+
- Structured logging system
101+
- Thread-safe operations
102+
60103
## Supported Database Objects
61104

62105
- Tables with columns, indexes, and constraints
@@ -97,12 +140,8 @@ Contributions are welcome! Please feel free to submit a Pull Request.
97140

98141
## License
99142

100-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
143+
This project is licensed under the Apache License - see the [LICENSE](LICENSE) file for details.
101144

102145
## Support
103146

104-
For support and questions, please open an issue in the GitHub repository.
105-
106-
## Documentation
107-
108-
For detailed documentation and examples, visit our [GitHub repository](https://github.com/mstgnz/sqlmapper).
147+
For support and questions, please open an issue in the GitHub repository.

cmd/bin/sqlmapper-darwin-amd64

3.13 MB
Binary file not shown.

cmd/bin/sqlmapper-linux-amd64

69.1 KB
Binary file not shown.
69.4 KB
Binary file not shown.

cmd/sqlmapper/main.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/mstgnz/sqlmapper"
11+
"github.com/mstgnz/sqlmapper/mysql"
12+
"github.com/mstgnz/sqlmapper/oracle"
13+
"github.com/mstgnz/sqlmapper/postgres"
14+
"github.com/mstgnz/sqlmapper/sqlite"
15+
"github.com/mstgnz/sqlmapper/sqlserver"
16+
)
17+
18+
// main fonksiyonu, programın ana giriş noktasıdır
19+
func main() {
20+
// Komut satırı parametrelerini tanımla
21+
filePath := flag.String("file", "", "SQL dump dosyasının yolu")
22+
targetDB := flag.String("to", "", "Hedef veritabanı tipi (mysql, postgres, sqlite, oracle, sqlserver)")
23+
flag.Parse()
24+
25+
// Parametreleri kontrol et
26+
if *filePath == "" || *targetDB == "" {
27+
fmt.Println("Kullanım: sqlmapper --file=<dosya_yolu> --to=<hedef_db>")
28+
fmt.Println("Örnek: sqlmapper --file=postgres.sql --to=mysql")
29+
flag.PrintDefaults()
30+
os.Exit(1)
31+
}
32+
33+
// Dosyayı oku
34+
content, err := os.ReadFile(*filePath)
35+
if err != nil {
36+
fmt.Printf("Dosya okuma hatası: %v\n", err)
37+
os.Exit(1)
38+
}
39+
40+
// Kaynak veritabanı tipini dosya içeriğinden tespit et
41+
sourceType := detectSourceType(string(content))
42+
if sourceType == "" {
43+
fmt.Println("Kaynak veritabanı tipi tespit edilemedi")
44+
os.Exit(1)
45+
}
46+
47+
// Kaynak parser'ı oluştur
48+
sourceParser := createParser(sourceType)
49+
if sourceParser == nil {
50+
fmt.Printf("Desteklenmeyen kaynak veritabanı tipi: %s\n", sourceType)
51+
os.Exit(1)
52+
}
53+
54+
// Hedef parser'ı oluştur
55+
targetParser := createParser(*targetDB)
56+
if targetParser == nil {
57+
fmt.Printf("Desteklenmeyen hedef veritabanı tipi: %s\n", *targetDB)
58+
os.Exit(1)
59+
}
60+
61+
// SQL'i parse et
62+
schema, err := sourceParser.Parse(string(content))
63+
if err != nil {
64+
fmt.Printf("Parse hatası: %v\n", err)
65+
os.Exit(1)
66+
}
67+
68+
// Hedef SQL'i oluştur
69+
result, err := targetParser.Generate(schema)
70+
if err != nil {
71+
fmt.Printf("SQL oluşturma hatası: %v\n", err)
72+
os.Exit(1)
73+
}
74+
75+
// Çıktı dosyasını oluştur
76+
outputPath := createOutputPath(*filePath, *targetDB)
77+
err = os.WriteFile(outputPath, []byte(result), 0644)
78+
if err != nil {
79+
fmt.Printf("Dosya yazma hatası: %v\n", err)
80+
os.Exit(1)
81+
}
82+
83+
fmt.Printf("Dönüşüm başarılı! Çıktı dosyası: %s\n", outputPath)
84+
}
85+
86+
// detectSourceType fonksiyonu, verilen SQL içeriğinden veritabanı tipini tespit eder
87+
func detectSourceType(content string) string {
88+
content = strings.ToUpper(content)
89+
switch {
90+
case strings.Contains(content, "ENGINE=INNODB"):
91+
return "mysql"
92+
case strings.Contains(content, "AUTOINCREMENT"):
93+
return "sqlite"
94+
case strings.Contains(content, "IDENTITY"):
95+
return "sqlserver"
96+
case strings.Contains(content, "SERIAL"):
97+
return "postgres"
98+
case strings.Contains(content, "NUMBER("):
99+
return "oracle"
100+
default:
101+
return ""
102+
}
103+
}
104+
105+
// createParser fonksiyonu, belirtilen veritabanı tipi için uygun parser'ı oluşturur
106+
func createParser(dbType string) sqlmapper.Parser {
107+
switch strings.ToLower(dbType) {
108+
case "mysql":
109+
return mysql.NewMySQL()
110+
case "postgres":
111+
return postgres.NewPostgreSQL()
112+
case "sqlite":
113+
return sqlite.NewSQLite()
114+
case "oracle":
115+
return oracle.NewOracle()
116+
case "sqlserver":
117+
return sqlserver.NewSQLServer()
118+
default:
119+
return nil
120+
}
121+
}
122+
123+
// createOutputPath fonksiyonu, girdi dosyası ve hedef veritabanı tipine göre çıktı dosyası yolunu oluşturur
124+
func createOutputPath(inputPath, targetDB string) string {
125+
dir := filepath.Dir(inputPath)
126+
filename := filepath.Base(inputPath)
127+
ext := filepath.Ext(filename)
128+
name := strings.TrimSuffix(filename, ext)
129+
return filepath.Join(dir, fmt.Sprintf("%s_%s%s", name, targetDB, ext))
130+
}

0 commit comments

Comments
 (0)