Skip to content

Commit 34176ee

Browse files
committed
docs
1 parent f43764e commit 34176ee

File tree

5 files changed

+672
-656
lines changed

5 files changed

+672
-656
lines changed

docs/api.md

+152-166
Original file line numberDiff line numberDiff line change
@@ -1,239 +1,225 @@
1-
# API Documentation
1+
# SQLMapper API Documentation
22

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
47
- [Parser API](#parser-api)
58
- [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)
912

1013
## Parser API
1114

12-
### NewParser
13-
14-
Creates a new
15+
### Creating a Parser
1516

1617
```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
1823
```
1924

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:
3026

3127
```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+
}
3332
```
3433

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
4735

4836
```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+
}
5042
```
5143

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
6445

6546
```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+
}
6752
```
6853

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
7955

80-
Creates a new migration manager.
56+
The Schema structure represents a complete database schema:
8157

8258
```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+
}
8469
```
8570

86-
**Parameters:**
87-
- `db`: Database connection
88-
89-
### Apply
90-
91-
Applies migrations.
71+
### Table Structure
9272

9373
```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+
}
9582
```
9683

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
10685

10786
```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+
}
10999
```
110100

111-
**Parameters:**
112-
- `ctx`: Context object
113-
114-
**Return Values:**
115-
- `error`: In case of error
101+
## Error Handling
116102

117-
## Database API
118-
119-
### NewConnection
120-
121-
Creates a new database connection.
103+
SQLMapper provides specific error types for different scenarios:
122104

123105
```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+
)
125112
```
126113

127-
**Parameters:**
128-
- `config`: Database configuration
129-
130-
**Return Values:**
131-
- `*sql.DB`: Database connection
132-
- `error`: In case of error
114+
## Examples
133115

134-
### Config Structure
116+
### Basic Usage
135117

136118
```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
154120

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+
)
158126

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+
);`
161136

162-
**Return Values:**
163-
- `error`: In case of error
137+
// Create parsers
138+
mysqlParser := mysql.NewMySQL()
139+
pgParser := postgres.NewPostgreSQL()
164140

165-
### FormatSQL
141+
// Parse MySQL
142+
schema, err := mysqlParser.Parse(mysqlSQL)
143+
if err != nil {
144+
panic(err)
145+
}
166146

167-
Formats SQL text.
147+
// Generate PostgreSQL
148+
pgSQL, err := pgParser.Generate(schema)
149+
if err != nil {
150+
panic(err)
151+
}
168152

169-
```go
170-
func FormatSQL(sql string) string
153+
fmt.Println(pgSQL)
154+
}
171155
```
172156

173-
**Parameters:**
174-
- `sql`: SQL text to format
175-
176-
**Return Values:**
177-
- `string`: Formatted SQL text
157+
### CLI Usage
178158

179-
## Error Types
159+
```bash
160+
# Convert MySQL to PostgreSQL
161+
sqlmapper --file=dump.sql --to=postgres
180162

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
189165
```
190166

191-
## Examples
192-
193-
### Simple Conversion
167+
### Advanced Usage
194168

195169
```go
196170
package main
197171

198172
import (
199173
"github.com/mstgnz/sqlmapper"
200-
"log"
174+
"github.com/mstgnz/sqlmapper/mysql"
201175
)
202176

203177
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+
}
208202

209-
// Convert from MySQL to PostgreSQL
210-
pgSQL, err := sqlmapper.Convert(mysqlSQL, "mysql", "postgres")
203+
// Generate modified SQL
204+
sql, err := parser.Generate(schema)
211205
if err != nil {
212-
log.Fatal(err)
206+
panic(err)
213207
}
214-
215-
log.Println(pgSQL)
216208
}
217209
```
218210

219-
### Migration Usage
211+
## Best Practices
220212

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
223218

224-
import (
225-
"context"
226-
"github.com/mstgnz/sqlmapper/migration"
227-
"log"
228-
)
219+
## Limitations
229220

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

Comments
 (0)