# MSSQL Writer

Generates Microsoft SQL Server DDL (Data Definition Language) from database schema models.

## Features

- **DDL Generation**: Generates complete SQL scripts for creating MSSQL schema
- **Schema Support**: Creates multiple schemas with proper naming
- **Bracket Notation**: Uses [schema].[table] bracket notation for identifiers
- **Identity Columns**: Generates IDENTITY(1,1) for auto-increment columns
- **Constraints**: Generates primary keys, foreign keys, unique, and check constraints
- **Indexes**: Creates indexes with unique support
- **Extended Properties**: Uses sp_addextendedproperty for comments
- **Direct Execution**: Can directly execute DDL on MSSQL database
- **Schema Flattening**: Optional schema flattening for compatibility

## Features by Phase

1. **Phase 1**: Create schemas
2. **Phase 2**: Create tables with columns, identity, and defaults
3. **Phase 3**: Add primary key constraints
4. **Phase 4**: Create indexes
5. **Phase 5**: Add unique constraints
6. **Phase 6**: Add check constraints
7. **Phase 7**: Add foreign key constraints
8. **Phase 8**: Add extended properties (comments)

## Type Mappings

| Canonical Type | MSSQL Type |
|----------------|-----------|
| int | INT |
| int64 | BIGINT |
| int16 | SMALLINT |
| int8 | TINYINT |
| bool | BIT |
| float32 | REAL |
| float64 | FLOAT |
| decimal | NUMERIC |
| string | NVARCHAR(255) |
| text | NVARCHAR(MAX) |
| timestamp | DATETIME2 |
| timestamptz | DATETIMEOFFSET |
| uuid | UNIQUEIDENTIFIER |
| bytea | VARBINARY(MAX) |
| date | DATE |
| time | TIME |

## Usage

### Generate SQL File

```go
import "git.warky.dev/wdevs/relspecgo/pkg/writers/mssql"
import "git.warky.dev/wdevs/relspecgo/pkg/writers"

writer := mssql.NewWriter(&writers.WriterOptions{
    OutputPath: "schema.sql",
    FlattenSchema: false,
})

err := writer.WriteDatabase(db)
if err != nil {
    panic(err)
}
```

### Direct Database Execution

```go
writer := mssql.NewWriter(&writers.WriterOptions{
    OutputPath: "",
    Metadata: map[string]interface{}{
        "connection_string": "sqlserver://sa:password@localhost/newdb",
    },
})

err := writer.WriteDatabase(db)
if err != nil {
    panic(err)
}
```

### CLI Usage

Generate SQL file:
```bash
relspec convert --from json --from-path schema.json \
                --to mssql --to-path schema.sql
```

Execute directly to database:
```bash
relspec convert --from json --from-path schema.json \
                --to mssql \
                --metadata '{"connection_string":"sqlserver://sa:password@localhost/mydb"}'
```

## Default Values

The writer supports several default value patterns:
- Functions: `GETDATE()`, `CURRENT_TIMESTAMP()`
- Literals: strings wrapped in quotes, numbers, booleans (0/1 for BIT)
- CAST expressions

## Comments/Extended Properties

Table and column descriptions are stored as MS_Description extended properties:

```sql
EXEC sp_addextendedproperty
  @name = 'MS_Description',
  @value = 'Table description here',
  @level0type = 'SCHEMA', @level0name = 'dbo',
  @level1type = 'TABLE', @level1name = 'my_table';
```

## Testing

Run tests with:
```bash
go test ./pkg/writers/mssql/...
```

## Limitations

- Views are not currently supported in the writer
- Sequences are not supported (MSSQL uses IDENTITY instead)
- Partitioning and advanced features are not supported
- Generated DDL assumes no triggers or computed columns
