(schemaName, tableName string, table *storepb.TableMetadata)
| 492 | } |
| 493 | |
| 494 | func generateCreateTable(schemaName, tableName string, table *storepb.TableMetadata) string { |
| 495 | var buf strings.Builder |
| 496 | |
| 497 | _, _ = buf.WriteString("CREATE TABLE [") |
| 498 | _, _ = buf.WriteString(schemaName) |
| 499 | _, _ = buf.WriteString("].[") |
| 500 | _, _ = buf.WriteString(tableName) |
| 501 | _, _ = buf.WriteString("] (\n") |
| 502 | |
| 503 | // Add columns |
| 504 | for i, column := range table.Columns { |
| 505 | columnDef := generateColumnDefinition(column) |
| 506 | _, _ = buf.WriteString(" ") |
| 507 | _, _ = buf.WriteString(columnDef) |
| 508 | |
| 509 | // Add comma if not last column or if there are constraints |
| 510 | if i < len(table.Columns)-1 || hasConstraintsInTable(table) || len(table.CheckConstraints) > 0 { |
| 511 | _, _ = buf.WriteString(",") |
| 512 | } |
| 513 | _, _ = buf.WriteString("\n") |
| 514 | } |
| 515 | |
| 516 | // Add constraints (inline with table definition) |
| 517 | constraintAdded := false |
| 518 | for _, idx := range table.Indexes { |
| 519 | if idx.IsConstraint { |
| 520 | if constraintAdded { |
| 521 | _, _ = buf.WriteString(",\n") |
| 522 | } |
| 523 | _, _ = buf.WriteString(" CONSTRAINT [") |
| 524 | _, _ = buf.WriteString(idx.Name) |
| 525 | if idx.Primary { |
| 526 | _, _ = buf.WriteString("] PRIMARY KEY") |
| 527 | } else if idx.Unique { |
| 528 | _, _ = buf.WriteString("] UNIQUE") |
| 529 | } else { |
| 530 | _, _ = buf.WriteString("]") |
| 531 | } |
| 532 | switch idx.Type { |
| 533 | case "CLUSTERED": |
| 534 | _, _ = buf.WriteString(" CLUSTERED") |
| 535 | case "NONCLUSTERED": |
| 536 | _, _ = buf.WriteString(" NONCLUSTERED") |
| 537 | default: |
| 538 | // Default to "" if not specified |
| 539 | } |
| 540 | _, _ = buf.WriteString(" (") |
| 541 | for j, expr := range idx.Expressions { |
| 542 | if j > 0 { |
| 543 | _, _ = buf.WriteString(", ") |
| 544 | } |
| 545 | _, _ = buf.WriteString("[") |
| 546 | _, _ = buf.WriteString(expr) |
| 547 | _, _ = buf.WriteString("]") |
| 548 | } |
| 549 | _, _ = buf.WriteString(")") |
| 550 | constraintAdded = true |
| 551 | } |
no test coverage detected