transpiler

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package transpiler - DML statement handling Converts T-SQL DML operations to Go code targeting different backends: - SQL backends (PostgreSQL, MySQL, SQLite): generates database/sql calls - gRPC backend: generates gRPC client calls - Mock backend: generates mock store calls

Package transpiler - unified error handling

Package transpiler converts T-SQL source code to Go.

Package transpiler - UDF/TVF validation and error handling

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Transpile

func Transpile(source string, packageName string) (string, error)

Transpile converts T-SQL source code to Go source code. This version only handles procedural code (no DML statements). GO statements are stripped by default as they have no semantic meaning.

func TranspileWithDML

func TranspileWithDML(source string, packageName string, dmlConfig DMLConfig) (string, error)

TranspileWithDML converts T-SQL source code to Go, including DML statements. DML statements (SELECT, INSERT, UPDATE, DELETE, EXEC) are converted to the appropriate backend calls based on the DMLConfig. GO statements are stripped by default unless PreserveGo is set in config.

Types

type BackendType

type BackendType string

BackendType identifies the target backend for DML code generation.

const (
	BackendSQL    BackendType = "sql"    // Generic SQL (uses dialect)
	BackendGRPC   BackendType = "grpc"   // gRPC client calls
	BackendMock   BackendType = "mock"   // Mock store calls
	BackendInline BackendType = "inline" // Inline SQL strings (for migration)
)

type DMLConfig

type DMLConfig struct {
	// Target backend
	Backend BackendType

	// Fallback backend for operations that don't map to primary backend
	// e.g., temp table operations when Backend=grpc should fall back to sql
	FallbackBackend  BackendType
	FallbackExplicit bool // True if user explicitly set --fallback-backend

	// SQL dialect (postgres, mysql, sqlite, sqlserver)
	SQLDialect string

	// Repository/store variable name (e.g., "r.db", "r.store", "r.client")
	StoreVar string

	// Receiver configuration for generated functions
	Receiver     string // Receiver variable name (e.g., "r") - empty means no receiver
	ReceiverType string // Receiver type (e.g., "*Repository", "*Service")

	// GO statement handling
	PreserveGo bool // If true, don't strip GO statements (default: false, strip them)

	// Sequence handling mode
	// "db" - use database features (RETURNING id for Postgres, LAST_INSERT_ID() for MySQL)
	// "uuid" - generate uuid.New() application-side
	// "stub" - generate TODO placeholder
	SequenceMode string

	// NEWID() handling mode
	// "app" - generate uuid.New() application-side (default, recommended)
	// "db" - use database-specific UUID function
	// "grpc" - call gRPC ID service
	// "mock" - generate predictable sequential UUIDs for testing
	// "stub" - generate TODO placeholder
	NewidMode string

	// gRPC client variable for --newid=grpc mode
	IDServiceVar string

	// DDL handling
	// SkipDDL: skip CREATE TABLE/VIEW/INDEX/SEQUENCE with warning (default: true)
	// StrictDDL: fail on any DDL statement
	// ExtractDDL: file path to extract skipped DDL
	SkipDDL    bool
	StrictDDL  bool
	ExtractDDL string

	// Whether to use transactions
	UseTransactions bool

	// gRPC backend options
	GRPCClientVar    string            // gRPC client variable name (e.g., "client", "svc")
	GRPCMappings     map[string]string // procedure -> service.method
	ProtoPackage     string            // Proto package for gRPC
	TableToService   map[string]string // table -> service name (e.g., "Products" -> "CatalogService")
	TableToClient    map[string]string // table -> client variable (e.g., "Products" -> "catalogClient")
	ServiceToPackage map[string]string // service -> proto package (e.g., "CatalogService" -> "catalogpb")

	// Mock backend options
	MockStoreVar string // Mock store variable name (e.g., "store", "mockDB")

	// SPLogger configuration
	UseSPLogger    bool   // Use SPLogger for CATCH blocks
	SPLoggerVar    string // Variable name for logger (e.g., "spLogger", "r.logger")
	SPLoggerType   string // Logger type: slog, db, file, multi, nop
	SPLoggerTable  string // Table name for db logger
	SPLoggerFile   string // File path for file logger
	SPLoggerFormat string // Format for file logger: json, text
	GenLoggerInit  bool   // Generate logger initialization code

	// Annotation level: none, minimal, standard, verbose
	// minimal: TODO markers for patterns needing attention
	// standard: TODOs + Original SQL comments
	// verbose: All of the above + type annotations + section markers
	AnnotateLevel string

	// LocalProcedures is a set of procedure names that are being transpiled
	// in the same compilation unit. When an EXEC targets one of these,
	// generate a method call instead of a stub function call.
	// Key format: lowercase procedure name (e.g., "helperproc", "dbo.helperproc")
	LocalProcedures map[string]*LocalProcInfo
}

DMLConfig configures DML transpilation.

func DefaultDMLConfig

func DefaultDMLConfig() DMLConfig

DefaultDMLConfig returns sensible defaults.

type LocalProcInfo added in v0.5.0

type LocalProcInfo struct {
	GoName       string   // Go function name (e.g., "HelperProc")
	IsMethod     bool     // True if it's a method on Repository
	HasContext   bool     // True if first param is context.Context
	InputParams  []string // Names of input parameters
	OutputParams []string // Names of OUTPUT parameters
	HasError     bool     // True if returns error
	IsExternal   bool     // True if this is an external stub (not locally defined)
}

LocalProcInfo holds information about a locally transpiled procedure

type TranspileError added in v0.5.0

type TranspileError struct {
	Message    string
	Line       int
	Column     int
	SourceLine string // The actual line of SQL that caused the error
	Hint       string
	Category   string // "parse", "type", "unsupported", "semantic"
}

TranspileError represents a transpilation error with full context

func (*TranspileError) Error added in v0.5.0

func (e *TranspileError) Error() string

type TranspileResult

type TranspileResult struct {
	Code              string   // Generated Go code
	DDLWarnings       []string // Warnings about skipped DDL statements
	ExtractedDDL      []string // DDL statements collected for extraction
	TempTablesUsed    []string // Temp tables encountered (for fallback backend info)
	TempTableWarnings []string // Warnings about temp tables with non-SQL backends
}

TranspileResult contains the transpilation output and metadata

func TranspileWithDMLEx

func TranspileWithDMLEx(source string, packageName string, dmlConfig DMLConfig) (*TranspileResult, error)

TranspileWithDMLEx is like TranspileWithDML but returns extended results

type UDFError added in v0.4.0

type UDFError struct {
	Message  string
	Line     int
	Column   int
	Hint     string
	Category string // "unsupported", "partial", "warning"
}

UDFError represents an error in UDF/TVF handling with position info

func (*UDFError) Error added in v0.4.0

func (e *UDFError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL