formatsql

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package formatsql implements SQL export formatting for query results. It generates INSERT, INSERT OR IGNORE, and INSERT OR UPDATE statements that can be used for database migration, backup/restore, and test data generation.

This package registers its formatters with the format package's registry, allowing it to be used as a plugin without the format package depending on memefish.

Current Design Constraints: - Values are expected to be pre-formatted as SQL literals using spanvalue.LiteralFormatConfig - The formatter receives []Cell (Row) but uses RawText() to extract plain string values - Format decision is made early in execute_sql.go, not at formatting time

The implementation uses memefish's ast.Path for correct identifier handling.

Index

Constants

View Source
const (
	ModeSQLInsert         format.Mode = "SQL_INSERT"
	ModeSQLInsertOrIgnore format.Mode = "SQL_INSERT_OR_IGNORE"
	ModeSQLInsertOrUpdate format.Mode = "SQL_INSERT_OR_UPDATE"
)

SQL export mode constants matching enums.DisplayMode string values.

Variables

This section is empty.

Functions

func ExtractTableNameFromQuery

func ExtractTableNameFromQuery(sql string) (string, error)

ExtractTableNameFromQuery attempts to extract a table name from a simple SELECT query. It supports simple SELECT patterns including:

  • SELECT * FROM table_name
  • SELECT columns FROM table_name
  • SELECT * FROM table_name WHERE ...
  • SELECT * FROM table_name ORDER BY ...
  • SELECT * FROM table_name LIMIT ...
  • SELECT DISTINCT columns FROM table_name (DISTINCT is allowed since Spanner tables always have PKs)
  • Combinations of the above

NOT supported:

  • GROUP BY / HAVING (aggregations change the result set structure)
  • JOINs (combine multiple tables)
  • Subqueries, CTEs, UNIONs (complex structures)

Note: DISTINCT is allowed because Spanner tables always have primary keys, making SELECT * results inherently unique. DISTINCT on unique results is a no-op.

Returns:

  • (tableName, nil) when extraction succeeds
  • ("", error) when extraction fails with a reason

The error messages are intended for debug logging to help understand why auto-detection failed, allowing users to adjust their queries or use explicit CLI_SQL_TABLE_NAME setting.

func FormatSQL

func FormatSQL(mode format.Mode) format.FormatFunc

FormatSQL is the non-streaming formatter for SQL export.

func ParseSimpleTablePath

func ParseSimpleTablePath(input string) (*ast.Path, error)

ParseSimpleTablePath converts a simple table path string from CLI input to an ast.Path. This function handles user-friendly input where reserved words don't need quoting. Examples: "Users", "Order" (reserved word OK), "myschema.Users" The function does NOT parse SQL expressions - it simply splits on dots. Quoting for reserved words is handled automatically by ast.Ident.SQL() during output.

Types

type SQLFormatter

type SQLFormatter struct {
	// contains filtered or unexported fields
}

SQLFormatter handles SQL export formatting for different INSERT variants. It supports both single-row and multi-row INSERT statements based on batchSize. The formatter buffers rows when batchSize > 1 to generate multi-row INSERTs.

func NewSQLFormatter

func NewSQLFormatter(out io.Writer, mode format.Mode, tableName string, batchSize int64) (*SQLFormatter, error)

NewSQLFormatter creates a new SQL formatter for streaming output.

func (*SQLFormatter) Finish

func (f *SQLFormatter) Finish() error

Finish flushes any remaining rows.

func (*SQLFormatter) WriteHeader

func (f *SQLFormatter) WriteHeader(columnNames []string) error

WriteHeader sets up column names for the formatter.

func (*SQLFormatter) WriteRow

func (f *SQLFormatter) WriteRow(values []string) error

WriteRow processes a single row and outputs SQL when batch is full.

type SQLStreamingFormatter

type SQLStreamingFormatter struct {
	// contains filtered or unexported fields
}

SQLStreamingFormatter implements StreamingFormatter for SQL export.

func NewSQLStreamingFormatter

func NewSQLStreamingFormatter(out io.Writer, config format.FormatConfig, mode format.Mode) (*SQLStreamingFormatter, error)

NewSQLStreamingFormatter creates a new streaming SQL formatter.

func (*SQLStreamingFormatter) FinishFormat

func (s *SQLStreamingFormatter) FinishFormat() error

FinishFormat completes the SQL export.

func (*SQLStreamingFormatter) InitFormat

func (s *SQLStreamingFormatter) InitFormat(columnNames []string, config format.FormatConfig, previewRows []format.Row) error

InitFormat initializes the formatter with column information.

func (*SQLStreamingFormatter) WriteRow

func (s *SQLStreamingFormatter) WriteRow(row format.Row) error

WriteRow outputs a single row.

Jump to

Keyboard shortcuts

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