|
1 |
| -# API Documentation |
| 1 | +# SQLMapper API Documentation |
2 | 2 |
|
3 |
| -## Contents |
| 3 | +## Overview |
| 4 | +SQLMapper provides a comprehensive API for converting SQL schemas between different database systems. This document outlines the main components and their usage. |
| 5 | + |
| 6 | +## Table of Contents |
4 | 7 | - [Parser API](#parser-api)
|
5 | 8 | - [Converter API](#converter-api)
|
6 |
| -- [Migration API](#migration-api) |
7 |
| -- [Database API](#database-api) |
8 |
| -- [Helper Functions](#helper-functions) |
| 9 | +- [Schema API](#schema-api) |
| 10 | +- [Error Handling](#error-handling) |
| 11 | +- [Examples](#examples) |
9 | 12 |
|
10 | 13 | ## Parser API
|
11 | 14 |
|
12 |
| -### NewParser |
13 |
| - |
14 |
| -Creates a new |
| 15 | +### Creating a Parser |
15 | 16 |
|
16 | 17 | ```go
|
17 |
| -func NewParser(dialect string) (Parser, error) |
| 18 | +parser := mysql.NewMySQL() // For MySQL |
| 19 | +parser := postgres.NewPostgreSQL() // For PostgreSQL |
| 20 | +parser := sqlite.NewSQLite() // For SQLite |
| 21 | +parser := oracle.NewOracle() // For Oracle |
| 22 | +parser := sqlserver.NewSQLServer() // For SQL Server |
18 | 23 | ```
|
19 | 24 |
|
20 |
| -**Parameters:** |
21 |
| -- `dialect`: Database dialect ("mysql", "postgres", "sqlite", "oracle", "sqlserver") |
22 |
| - |
23 |
| -**Return Values:** |
24 |
| -- `Parser`: Object implementing the Parser interface |
25 |
| -- `error`: In case of error |
26 |
| - |
27 |
| -### Parse |
28 |
| - |
29 |
| -Parses SQL dump file. |
| 25 | +Each parser implements the `Parser` interface: |
30 | 26 |
|
31 | 27 | ```go
|
32 |
| -func (p *Parser) Parse(sql string) (*Entity, error) |
| 28 | +type Parser interface { |
| 29 | + Parse(content string) (*Schema, error) |
| 30 | + Generate(schema *Schema) (string, error) |
| 31 | +} |
33 | 32 | ```
|
34 | 33 |
|
35 |
| -**Parameters:** |
36 |
| -- `sql`: SQL dump text to parse |
37 |
| - |
38 |
| -**Return Values:** |
39 |
| -- `*Entity`: Parsed data structure |
40 |
| -- `error`: In case of error |
41 |
| - |
42 |
| -## Converter API |
43 |
| - |
44 |
| -### Convert |
45 |
| - |
46 |
| -Converts a database schema to another database format. |
| 34 | +### Parsing SQL |
47 | 35 |
|
48 | 36 | ```go
|
49 |
| -func Convert(sql, sourceDialect, targetDialect string) (string, error) |
| 37 | +// Parse SQL content into a schema |
| 38 | +schema, err := parser.Parse(sqlContent) |
| 39 | +if err != nil { |
| 40 | + log.Fatal(err) |
| 41 | +} |
50 | 42 | ```
|
51 | 43 |
|
52 |
| -**Parameters:** |
53 |
| -- `sql`: SQL text to convert |
54 |
| -- `sourceDialect`: Source database dialect |
55 |
| -- `targetDialect`: Target database dialect |
56 |
| - |
57 |
| -**Return Values:** |
58 |
| -- `string`: Converted SQL text |
59 |
| -- `error`: In case of error |
60 |
| - |
61 |
| -### ConvertEntity |
62 |
| - |
63 |
| -Converts Entity structure to SQL. |
| 44 | +### Generating SQL |
64 | 45 |
|
65 | 46 | ```go
|
66 |
| -func (p *Parser) ConvertEntity(entity *Entity) (string, error) |
| 47 | +// Generate SQL from a schema |
| 48 | +sql, err := parser.Generate(schema) |
| 49 | +if err != nil { |
| 50 | + log.Fatal(err) |
| 51 | +} |
67 | 52 | ```
|
68 | 53 |
|
69 |
| -**Parameters:** |
70 |
| -- `entity`: Entity structure to convert |
71 |
| - |
72 |
| -**Return Values:** |
73 |
| -- `string`: Generated SQL text |
74 |
| -- `error`: In case of error |
75 |
| - |
76 |
| -## Migration API |
77 |
| - |
78 |
| -### NewMigrationManager |
| 54 | +## Schema API |
79 | 55 |
|
80 |
| -Creates a new migration manager. |
| 56 | +The Schema structure represents a complete database schema: |
81 | 57 |
|
82 | 58 | ```go
|
83 |
| -func NewMigrationManager(db *sql.DB) *MigrationManager |
| 59 | +type Schema struct { |
| 60 | + Name string |
| 61 | + Tables []Table |
| 62 | + Views []View |
| 63 | + Triggers []Trigger |
| 64 | + Sequences []Sequence |
| 65 | + Functions []Function |
| 66 | + Procedures []Procedure |
| 67 | + // ... other fields |
| 68 | +} |
84 | 69 | ```
|
85 | 70 |
|
86 |
| -**Parameters:** |
87 |
| -- `db`: Database connection |
88 |
| - |
89 |
| -### Apply |
90 |
| - |
91 |
| -Applies migrations. |
| 71 | +### Table Structure |
92 | 72 |
|
93 | 73 | ```go
|
94 |
| -func (m *MigrationManager) Apply(ctx context.Context) error |
| 74 | +type Table struct { |
| 75 | + Name string |
| 76 | + Schema string |
| 77 | + Columns []Column |
| 78 | + Indexes []Index |
| 79 | + Constraints []Constraint |
| 80 | + Comment string |
| 81 | +} |
95 | 82 | ```
|
96 | 83 |
|
97 |
| -**Parameters:** |
98 |
| -- `ctx`: Context object |
99 |
| - |
100 |
| -**Return Values:** |
101 |
| -- `error`: In case of error |
102 |
| - |
103 |
| -### Rollback |
104 |
| - |
105 |
| -Rolls back the last migration. |
| 84 | +### Column Structure |
106 | 85 |
|
107 | 86 | ```go
|
108 |
| -func (m *MigrationManager) Rollback(ctx context.Context) error |
| 87 | +type Column struct { |
| 88 | + Name string |
| 89 | + DataType string |
| 90 | + Length int |
| 91 | + Scale int |
| 92 | + IsNullable bool |
| 93 | + DefaultValue string |
| 94 | + AutoIncrement bool |
| 95 | + IsPrimaryKey bool |
| 96 | + IsUnique bool |
| 97 | + Comment string |
| 98 | +} |
109 | 99 | ```
|
110 | 100 |
|
111 |
| -**Parameters:** |
112 |
| -- `ctx`: Context object |
113 |
| - |
114 |
| -**Return Values:** |
115 |
| -- `error`: In case of error |
| 101 | +## Error Handling |
116 | 102 |
|
117 |
| -## Database API |
118 |
| - |
119 |
| -### NewConnection |
120 |
| - |
121 |
| -Creates a new database connection. |
| 103 | +SQLMapper provides specific error types for different scenarios: |
122 | 104 |
|
123 | 105 | ```go
|
124 |
| -func NewConnection(config *Config) (*sql.DB, error) |
| 106 | +var ( |
| 107 | + ErrEmptyContent = errors.New("empty content") |
| 108 | + ErrInvalidSQL = errors.New("invalid SQL syntax") |
| 109 | + ErrUnsupportedType = errors.New("unsupported data type") |
| 110 | + ErrParserNotFound = errors.New("parser not found for database type") |
| 111 | +) |
125 | 112 | ```
|
126 | 113 |
|
127 |
| -**Parameters:** |
128 |
| -- `config`: Database configuration |
129 |
| - |
130 |
| -**Return Values:** |
131 |
| -- `*sql.DB`: Database connection |
132 |
| -- `error`: In case of error |
| 114 | +## Examples |
133 | 115 |
|
134 |
| -### Config Structure |
| 116 | +### Basic Usage |
135 | 117 |
|
136 | 118 | ```go
|
137 |
| -type Config struct { |
138 |
| - Driver string |
139 |
| - Host string |
140 |
| - Port int |
141 |
| - Database string |
142 |
| - Username string |
143 |
| - Password string |
144 |
| - SSLMode string |
145 |
| - Options map[string]string |
146 |
| -} |
147 |
| -``` |
148 |
| - |
149 |
| -## Helper Functions |
150 |
| - |
151 |
| -### ValidateSQL |
152 |
| - |
153 |
| -Validates SQL text. |
| 119 | +package main |
154 | 120 |
|
155 |
| -```go |
156 |
| -func ValidateSQL(sql string) error |
157 |
| -``` |
| 121 | +import ( |
| 122 | + "fmt" |
| 123 | + "github.com/mstgnz/sqlmapper/mysql" |
| 124 | + "github.com/mstgnz/sqlmapper/postgres" |
| 125 | +) |
158 | 126 |
|
159 |
| -**Parameters:** |
160 |
| -- `sql`: SQL text to validate |
| 127 | +func main() { |
| 128 | + // MySQL input |
| 129 | + mysqlSQL := ` |
| 130 | + CREATE TABLE users ( |
| 131 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 132 | + name VARCHAR(100) NOT NULL, |
| 133 | + email VARCHAR(255) UNIQUE, |
| 134 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 135 | + );` |
161 | 136 |
|
162 |
| -**Return Values:** |
163 |
| -- `error`: In case of error |
| 137 | + // Create parsers |
| 138 | + mysqlParser := mysql.NewMySQL() |
| 139 | + pgParser := postgres.NewPostgreSQL() |
164 | 140 |
|
165 |
| -### FormatSQL |
| 141 | + // Parse MySQL |
| 142 | + schema, err := mysqlParser.Parse(mysqlSQL) |
| 143 | + if err != nil { |
| 144 | + panic(err) |
| 145 | + } |
166 | 146 |
|
167 |
| -Formats SQL text. |
| 147 | + // Generate PostgreSQL |
| 148 | + pgSQL, err := pgParser.Generate(schema) |
| 149 | + if err != nil { |
| 150 | + panic(err) |
| 151 | + } |
168 | 152 |
|
169 |
| -```go |
170 |
| -func FormatSQL(sql string) string |
| 153 | + fmt.Println(pgSQL) |
| 154 | +} |
171 | 155 | ```
|
172 | 156 |
|
173 |
| -**Parameters:** |
174 |
| -- `sql`: SQL text to format |
175 |
| - |
176 |
| -**Return Values:** |
177 |
| -- `string`: Formatted SQL text |
| 157 | +### CLI Usage |
178 | 158 |
|
179 |
| -## Error Types |
| 159 | +```bash |
| 160 | +# Convert MySQL to PostgreSQL |
| 161 | +sqlmapper --file=dump.sql --to=postgres |
180 | 162 |
|
181 |
| -```go |
182 |
| -var ( |
183 |
| - ErrInvalidDialect = errors.New("invalid database dialect") |
184 |
| - ErrParseFailure = errors.New("SQL parse error") |
185 |
| - ErrConversionFailure = errors.New("conversion error") |
186 |
| - ErrConnectionFailure = errors.New("connection error") |
187 |
| - ErrMigrationFailure = errors.New("migration error") |
188 |
| -) |
| 163 | +# Convert PostgreSQL to SQLite |
| 164 | +sqlmapper --file=schema.sql --to=sqlite |
189 | 165 | ```
|
190 | 166 |
|
191 |
| -## Examples |
192 |
| - |
193 |
| -### Simple Conversion |
| 167 | +### Advanced Usage |
194 | 168 |
|
195 | 169 | ```go
|
196 | 170 | package main
|
197 | 171 |
|
198 | 172 | import (
|
199 | 173 | "github.com/mstgnz/sqlmapper"
|
200 |
| - "log" |
| 174 | + "github.com/mstgnz/sqlmapper/mysql" |
201 | 175 | )
|
202 | 176 |
|
203 | 177 | func main() {
|
204 |
| - mysqlSQL := `CREATE TABLE users ( |
205 |
| - id INT PRIMARY KEY AUTO_INCREMENT, |
206 |
| - name VARCHAR(100) NOT NULL |
207 |
| - );` |
| 178 | + parser := mysql.NewMySQL() |
| 179 | + |
| 180 | + // Parse complex schema |
| 181 | + schema, err := parser.Parse(complexSQL) |
| 182 | + if err != nil { |
| 183 | + panic(err) |
| 184 | + } |
| 185 | + |
| 186 | + // Modify schema |
| 187 | + for i, table := range schema.Tables { |
| 188 | + // Add timestamps to all tables |
| 189 | + schema.Tables[i].Columns = append(table.Columns, |
| 190 | + sqlmapper.Column{ |
| 191 | + Name: "created_at", |
| 192 | + DataType: "TIMESTAMP", |
| 193 | + DefaultValue: "CURRENT_TIMESTAMP", |
| 194 | + }, |
| 195 | + sqlmapper.Column{ |
| 196 | + Name: "updated_at", |
| 197 | + DataType: "TIMESTAMP", |
| 198 | + DefaultValue: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", |
| 199 | + }, |
| 200 | + ) |
| 201 | + } |
208 | 202 |
|
209 |
| - // Convert from MySQL to PostgreSQL |
210 |
| - pgSQL, err := sqlmapper.Convert(mysqlSQL, "mysql", "postgres") |
| 203 | + // Generate modified SQL |
| 204 | + sql, err := parser.Generate(schema) |
211 | 205 | if err != nil {
|
212 |
| - log.Fatal(err) |
| 206 | + panic(err) |
213 | 207 | }
|
214 |
| - |
215 |
| - log.Println(pgSQL) |
216 | 208 | }
|
217 | 209 | ```
|
218 | 210 |
|
219 |
| -### Migration Usage |
| 211 | +## Best Practices |
220 | 212 |
|
221 |
| -```go |
222 |
| -package main |
| 213 | +1. Always check for errors when parsing and generating SQL |
| 214 | +2. Use appropriate parsers for source and target databases |
| 215 | +3. Validate schema modifications before generating SQL |
| 216 | +4. Handle database-specific features carefully |
| 217 | +5. Test conversions with sample data |
223 | 218 |
|
224 |
| -import ( |
225 |
| - "context" |
226 |
| - "github.com/mstgnz/sqlmapper/migration" |
227 |
| - "log" |
228 |
| -) |
| 219 | +## Limitations |
229 | 220 |
|
230 |
| -func main() { |
231 |
| - // Create migration manager |
232 |
| - manager := migration.NewMigrationManager(db) |
233 |
| - |
234 |
| - // Apply migrations |
235 |
| - err := manager.Apply(context.Background()) |
236 |
| - if err != nil { |
237 |
| - log.Fatal(err) |
238 |
| - } |
239 |
| -} |
| 221 | +- Some database-specific features may not be perfectly converted |
| 222 | +- Complex stored procedures might need manual adjustment |
| 223 | +- Custom data types may require special handling |
| 224 | +- Performance may vary with large schemas |
| 225 | +</rewritten_file> |
0 commit comments