Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Dec 26, 2024
1 parent 83350d1 commit 421f21d
Show file tree
Hide file tree
Showing 5 changed files with 729 additions and 60 deletions.
239 changes: 239 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
# API Documentation

## Contents
- [Parser API](#parser-api)
- [Converter API](#converter-api)
- [Migration API](#migration-api)
- [Database API](#database-api)
- [Helper Functions](#helper-functions)

## Parser API

### NewParser

Creates a new parser.

```go
func NewParser(dialect string) (Parser, error)
```

**Parameters:**
- `dialect`: Database dialect ("mysql", "postgres", "sqlite", "oracle", "sqlserver")

**Return Values:**
- `Parser`: Object implementing the Parser interface
- `error`: In case of error

### Parse

Parses SQL dump file.

```go
func (p *Parser) Parse(sql string) (*Entity, error)
```

**Parameters:**
- `sql`: SQL dump text to parse

**Return Values:**
- `*Entity`: Parsed data structure
- `error`: In case of error

## Converter API

### Convert

Converts a database schema to another database format.

```go
func Convert(sql, sourceDialect, targetDialect string) (string, error)
```

**Parameters:**
- `sql`: SQL text to convert
- `sourceDialect`: Source database dialect
- `targetDialect`: Target database dialect

**Return Values:**
- `string`: Converted SQL text
- `error`: In case of error

### ConvertEntity

Converts Entity structure to SQL.

```go
func (p *Parser) ConvertEntity(entity *Entity) (string, error)
```

**Parameters:**
- `entity`: Entity structure to convert

**Return Values:**
- `string`: Generated SQL text
- `error`: In case of error

## Migration API

### NewMigrationManager

Creates a new migration manager.

```go
func NewMigrationManager(db *sql.DB) *MigrationManager
```

**Parameters:**
- `db`: Database connection

### Apply

Applies migrations.

```go
func (m *MigrationManager) Apply(ctx context.Context) error
```

**Parameters:**
- `ctx`: Context object

**Return Values:**
- `error`: In case of error

### Rollback

Rolls back the last migration.

```go
func (m *MigrationManager) Rollback(ctx context.Context) error
```

**Parameters:**
- `ctx`: Context object

**Return Values:**
- `error`: In case of error

## Database API

### NewConnection

Creates a new database connection.

```go
func NewConnection(config *Config) (*sql.DB, error)
```

**Parameters:**
- `config`: Database configuration

**Return Values:**
- `*sql.DB`: Database connection
- `error`: In case of error

### Config Structure

```go
type Config struct {
Driver string
Host string
Port int
Database string
Username string
Password string
SSLMode string
Options map[string]string
}
```

## Helper Functions

### ValidateSQL

Validates SQL text.

```go
func ValidateSQL(sql string) error
```

**Parameters:**
- `sql`: SQL text to validate

**Return Values:**
- `error`: In case of error

### FormatSQL

Formats SQL text.

```go
func FormatSQL(sql string) string
```

**Parameters:**
- `sql`: SQL text to format

**Return Values:**
- `string`: Formatted SQL text

## Error Types

```go
var (
ErrInvalidDialect = errors.New("invalid database dialect")
ErrParseFailure = errors.New("SQL parse error")
ErrConversionFailure = errors.New("conversion error")
ErrConnectionFailure = errors.New("connection error")
ErrMigrationFailure = errors.New("migration error")
)
```

## Examples

### Simple Conversion

```go
package main

import (
"github.com/mstgnz/sdc"
"log"
)

func main() {
mysqlSQL := `CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);`

// Convert from MySQL to PostgreSQL
pgSQL, err := sdc.Convert(mysqlSQL, "mysql", "postgres")
if err != nil {
log.Fatal(err)
}

log.Println(pgSQL)
}
```

### Migration Usage

```go
package main

import (
"context"
"github.com/mstgnz/sdc/migration"
"log"
)

func main() {
// Create migration manager
manager := migration.NewMigrationManager(db)

// Apply migrations
err := manager.Apply(context.Background())
if err != nil {
log.Fatal(err)
}
}
111 changes: 111 additions & 0 deletions docs/mysql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# MySQL Kullanım Kılavuzu

## İçindekiler
- [Bağlantı Yapılandırması](#bağlantı-yapılandırması)
- [Temel Kullanım](#temel-kullanım)
- [Veri Tipleri](#veri-tipleri)
- [Özel Durumlar](#özel-durumlar)
- [Örnek Senaryolar](#örnek-senaryolar)

## Bağlantı Yapılandırması

MySQL bağlantısı için aşağıdaki yapılandırmayı kullanabilirsiniz:

```go
config := db.Config{
Driver: "mysql",
Host: "localhost",
Port: 3306,
Database: "mydb",
Username: "user",
Password: "pass",
}
```

## Temel Kullanım

MySQL dump dosyasını dönüştürmek için:

```go
// MySQL parser oluştur
parser := sdc.NewMySQLParser()

// MySQL dump'ı parse et
entity, err := parser.Parse(mysqlDump)
if err != nil {
log.Error("Parse hatası", err)
return
}

// PostgreSQL'e dönüştür
pgParser := sdc.NewPostgresParser()
pgSQL, err := pgParser.Convert(entity)
if err != nil {
log.Error("Dönüştürme hatası", err)
return
}
```

## Veri Tipleri

MySQL'den diğer veritabanlarına dönüşüm yaparken veri tipi eşleştirmeleri:

| MySQL Veri Tipi | PostgreSQL | SQLite | Oracle | SQL Server |
|-----------------|------------|---------|---------|------------|
| TINYINT | SMALLINT | INTEGER | NUMBER | TINYINT |
| INT | INTEGER | INTEGER | NUMBER | INT |
| BIGINT | BIGINT | INTEGER | NUMBER | BIGINT |
| VARCHAR | VARCHAR | TEXT | VARCHAR2| VARCHAR |
| TEXT | TEXT | TEXT | CLOB | TEXT |
| DATETIME | TIMESTAMP | TEXT | DATE | DATETIME |
| DECIMAL | DECIMAL | REAL | NUMBER | DECIMAL |

## Özel Durumlar

### AUTO_INCREMENT

MySQL'in AUTO_INCREMENT özelliği diğer veritabanlarında şu şekilde karşılık bulur:
- PostgreSQL: SERIAL
- SQLite: AUTOINCREMENT
- Oracle: SEQUENCE
- SQL Server: IDENTITY

### Karakter Seti ve Collation

MySQL'de karakter seti ve collation belirtimi:
```sql
CREATE TABLE users (
name VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
);
```

## Örnek Senaryolar

### 1. Tablo Oluşturma ve Dönüştürme

```go
mysqlDump := `CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);`

// MySQL'den PostgreSQL'e dönüştürme
pgSQL, err := Convert(mysqlDump, "mysql", "postgres")
```

### 2. İndeks ve Kısıtlamalar

```go
mysqlDump := `CREATE TABLE orders (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
total DECIMAL(10,2) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id),
INDEX idx_user_id (user_id)
);`

// MySQL'den Oracle'a dönüştürme
oracleSQL, err := Convert(mysqlDump, "mysql", "oracle")
```
Loading

0 comments on commit 421f21d

Please sign in to comment.