gorm_plugin

package
v0.0.0-...-78d783c Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMinMaxToConstraints

func ApplyMinMaxToConstraints(constraints map[string]any, result MinMaxResult)

ApplyMinMaxToConstraints applies MinMaxResult values to a constraint map

func EnsureConstraintEntry

func EnsureConstraintEntry(constraints map[string]map[string]any, columnName string) map[string]any

EnsureConstraintEntry initializes a constraint map entry for a column if it doesn't exist.

func ExtractColumnNameFromClause

func ExtractColumnNameFromClause(clause string) string

ExtractColumnNameFromClause extracts the column name from a CHECK constraint clause. Handles various database-specific formats:

  • PostgreSQL: (column)::text = ... or column >= 0
  • PostgreSQL with functions: length((password)::text) >= 8
  • MySQL: `column` IN (...) or column >= 0
  • SQLite: column IN (...) or column >= 0

func GetDefault

func GetDefault(constraint map[string]any) string

GetDefault returns the default value for the column, or empty string if none.

func HasDefault

func HasDefault(constraint map[string]any) bool

HasDefault returns whether the column has a default value.

func IsAutoIncrement

func IsAutoIncrement(constraint map[string]any) bool

IsAutoIncrement returns whether the column is auto-incremented.

func IsGenerated

func IsGenerated(constraint map[string]any) bool

IsGenerated returns whether the column is a generated/computed column.

func IsNullable

func IsNullable(constraint map[string]any) bool

IsNullable returns whether the column allows NULL values. Defaults to true if the constraint is not set.

func IsPrimary

func IsPrimary(constraint map[string]any) bool

IsPrimary returns whether the column is a primary key based on constraint data.

func ParseINClauseValues

func ParseINClauseValues(clause string) []string

ParseINClauseValues extracts values from a SQL IN clause. Handles formats like:

  • column IN ('val1', 'val2', 'val3')
  • [column] IN (N'val1', N'val2') (MSSQL Unicode)
  • column IN ("val1", "val2")
  • Multi-line CHECK constraints with whitespace/newlines

func ParseORClauseValues

func ParseORClauseValues(clause string) []string

ParseORClauseValues extracts values from OR equality constraints. Handles formats like:

  • ([status]='Pending' OR [status]='Shipped' OR [status]='Delivered')
  • (col='a' OR col='b')
  • ([col]=N'value1' OR [col]=N'value2') (MSSQL Unicode)

func ParseValueList

func ParseValueList(content string) []string

ParseValueList parses a comma-separated list of quoted values. Handles:

  • Single quotes: 'value'
  • Double quotes: "value"
  • MySQL charset prefix: _utf8mb4'value'
  • PostgreSQL type casts: 'value'::text

func SanitizeConstraintValue

func SanitizeConstraintValue(value string) string

SanitizeConstraintValue removes type casts and normalizes a constraint value. Handles PostgreSQL type casts like ::text, ::integer, etc.

Types

type BatchConfig

type BatchConfig struct {
	BatchSize             int      // Number of records per batch
	UseBulkInsert         bool     // Use database-specific bulk insert when available
	FailOnError           bool     // Stop on first error or continue
	LogProgress           bool     // Log progress during batch operations
	UpsertPKColumns       []string // PK columns for ON CONFLICT DO UPDATE; non-nil = upsert mode
	SkipConflictPKColumns []string // PK columns for ON CONFLICT with identity update (append mode — skip duplicates)
}

BatchConfig holds configuration for batch operations

func DefaultBatchConfig

func DefaultBatchConfig() *BatchConfig

DefaultBatchConfig returns default batch configuration

type BatchProcessor

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

BatchProcessor handles batch operations for the plugin

func NewBatchProcessor

func NewBatchProcessor(plugin GormPluginFunctions, dbType engine.DatabaseType, config *BatchConfig) *BatchProcessor

NewBatchProcessor creates a new batch processor

func (*BatchProcessor) ExportInBatches

func (b *BatchProcessor) ExportInBatches(db *gorm.DB, schema, tableName string, columns []string, writer func([]map[string]any) error) error

ExportInBatches exports data in batches to avoid memory issues

func (*BatchProcessor) InsertBatch

func (b *BatchProcessor) InsertBatch(db *gorm.DB, schema, tableName string, records []map[string]any) error

InsertBatch inserts multiple rows in batches

type ColumnDef

type ColumnDef struct {
	Name     string
	Type     string
	Primary  bool
	Nullable bool
	NotNull  bool   // Explicit NOT NULL flag (opposite of Nullable)
	Extra    string // Additional column modifiers (e.g., AUTO_INCREMENT, DEFAULT)
}

ColumnDef represents a column definition for CREATE TABLE

func RecordsToColumnDefs

func RecordsToColumnDefs(columns []engine.Record, decorator PrimaryKeyDecorator) []ColumnDef

RecordsToColumnDefs converts engine.Record columns to ColumnDef slices. The decorator callback handles database-specific primary key decoration (e.g., AUTO_INCREMENT for MySQL, GENERATED ALWAYS AS IDENTITY for Postgres).

type ColumnTypeInfo

type ColumnTypeInfo struct {
	Type       string
	IsNullable bool
}

ColumnTypeInfo holds a column's type string alongside its nullability. Used by CRUD operations that need both pieces of information for value conversion.

type ConnectionInput

type ConnectionInput struct {
	//common
	Username string `validate:"required"`
	Password string `validate:"required"`
	Database string `validate:"required"`
	Hostname string `validate:"required"`
	Port     int    `validate:"required"`

	//mysql/mariadb
	ParseTime               bool           `validate:"boolean"`
	Loc                     *time.Location `validate:"required"`
	AllowClearTextPasswords bool           `validate:"boolean"`

	//postgres
	SearchPath string

	//clickhouse
	HTTPProtocol string
	ReadOnly     string
	Debug        string

	ConnectionTimeout int

	SSLConfig *ssl.SSLConfig

	ExtraOptions map[string]string `validate:"omitnil"`
}

type ErrorHandler

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

ErrorHandler provides centralized error handling for GORM operations

func NewErrorHandler

func NewErrorHandler(plugin GormPluginFunctions) *ErrorHandler

NewErrorHandler creates a new error handler

func (*ErrorHandler) HandleError

func (h *ErrorHandler) HandleError(err error, operation string, details map[string]any) error

HandleError processes GORM errors and returns user-friendly messages

type GormPlugin

type GormPlugin struct {
	engine.Plugin
	GormPluginFunctions
	// contains filtered or unexported fields
}

func (*GormPlugin) AddRow

func (p *GormPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (bool, error)

func (*GormPlugin) AddRowReturningID

func (p *GormPlugin) AddRowReturningID(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (int64, error)

AddRowReturningID inserts a row and returns the auto-generated ID. Returns 0 if the table has no auto-increment column or the database doesn't support it.

func (*GormPlugin) AddStorageUnit

func (p *GormPlugin) AddStorageUnit(config *engine.PluginConfig, schema string, storageUnit string, fields []engine.Record) (bool, error)

func (*GormPlugin) ApplyWhereConditions

func (p *GormPlugin) ApplyWhereConditions(query *gorm.DB, condition *queryast.WhereCondition, columnTypes map[string]ColumnTypeInfo) (*gorm.DB, error)

func (*GormPlugin) BuildSkipConflictClause

func (p *GormPlugin) BuildSkipConflictClause(pkColumns []string) clause.OnConflict

BuildSkipConflictClause returns ON CONFLICT (pk) DO NOTHING — works for Postgres, SQLite, ClickHouse. MySQL/MariaDB plugins override this with identity assignments.

func (*GormPlugin) BulkAddRows

func (p *GormPlugin) BulkAddRows(config *engine.PluginConfig, schema string, storageUnit string, rows [][]engine.Record) (bool, error)

BulkAddRows adds multiple rows using batch processing

func (*GormPlugin) Chat

func (p *GormPlugin) Chat(config *engine.PluginConfig, schema string, previousConversation string, query string) ([]*engine.ChatMessage, error)

func (*GormPlugin) ClearTableData

func (p *GormPlugin) ClearTableData(config *engine.PluginConfig, schema string, storageUnit string) (bool, error)

ClearTableData clears all data from a table

func (*GormPlugin) ConvertArrayValue

func (p *GormPlugin) ConvertArrayValue(value string, columnType string) (any, error)

ConvertArrayValue parses array literal values (e.g., "[1, 2, 3]") into Go slices. Used by ClickHouse for ARRAY() type conversion.

func (*GormPlugin) ConvertRawToRows

func (p *GormPlugin) ConvertRawToRows(rows *sql.Rows) (*engine.GetRowsResult, error)

ConvertRawToRows converts raw SQL rows into the engine's GetRowsResult format. Handles column metadata extraction (type, length, precision) and per-row value formatting including binary, geometry, time, and plugin-specific custom types.

func (*GormPlugin) ConvertRecordValuesToMap

func (p *GormPlugin) ConvertRecordValuesToMap(values []engine.Record) (map[string]any, error)

func (*GormPlugin) ConvertStringValue

func (p *GormPlugin) ConvertStringValue(value, columnType string, isNullable bool) (any, error)

ConvertStringValue converts a string value to the appropriate Go type based on column type.

func (*GormPlugin) CreateSQLBuilder

func (p *GormPlugin) CreateSQLBuilder(db *gorm.DB) SQLBuilderInterface

CreateSQLBuilder creates a SQL builder instance - default implementation Can be overridden by specific database plugins (e.g., MySQL)

func (*GormPlugin) DeleteRow

func (p *GormPlugin) DeleteRow(config *engine.PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error)

func (*GormPlugin) ExecuteRawSQL

func (p *GormPlugin) ExecuteRawSQL(config *engine.PluginConfig, openMultiStatementDB func(*engine.PluginConfig) (*gorm.DB, error), query string, params ...any) (*engine.GetRowsResult, error)

ExecuteRawSQL handles raw SQL execution with optional multi-statement support. openMultiStatementDB is called when config.MultiStatement is true to get a DB connection that supports multiple statements. Pass nil if multi-statement is not supported.

func (*GormPlugin) ExportData

func (p *GormPlugin) ExportData(config *engine.PluginConfig, schema string, storageUnit string, writer func([]string) error, selectedRows []map[string]any) error

ExportData exports data to tabular format (CSV/Excel). If selectedRows is nil/empty, exports all rows from the table.

func (*GormPlugin) FindMissingDataType

func (p *GormPlugin) FindMissingDataType(db *gorm.DB, columnType string) string

FindMissingDataType resolves unknown column types by querying system catalogs.

func (*GormPlugin) FormTableName

func (p *GormPlugin) FormTableName(schema string, storageUnit string) string

FormTableName returns the qualified table name for a given schema and storage unit.

func (*GormPlugin) FormatColumnValue

func (p *GormPlugin) FormatColumnValue(columnType string, scanner any) (string, error)

FormatColumnValue returns empty string by default.

func (*GormPlugin) FormatGeometryValue

func (p *GormPlugin) FormatGeometryValue(rawBytes []byte, columnType string) string

FormatGeometryValue returns empty string by default (use hex formatting).

func (*GormPlugin) FormatValue

func (p *GormPlugin) FormatValue(val any) string

FormatValue converts any values to strings appropriately for CSV

func (*GormPlugin) GetAllSchemas

func (p *GormPlugin) GetAllSchemas(config *engine.PluginConfig) ([]string, error)

func (*GormPlugin) GetColumnConstraints

func (p *GormPlugin) GetColumnConstraints(config *engine.PluginConfig, schema string, storageUnit string) (map[string]map[string]any, error)

GetColumnConstraints gets column constraints using GORM's Migrator

func (*GormPlugin) GetColumnScanner

func (p *GormPlugin) GetColumnScanner(columnType string) any

GetColumnScanner returns nil by default.

func (*GormPlugin) GetColumnTypes

func (p *GormPlugin) GetColumnTypes(db *gorm.DB, schema, tableName string) (map[string]ColumnTypeInfo, error)

GetColumnTypes uses GORM's Migrator when possible, falls back to raw SQL

func (*GormPlugin) GetColumnsForTable

func (p *GormPlugin) GetColumnsForTable(config *engine.PluginConfig, schema string, storageUnit string) ([]engine.Column, error)

func (*GormPlugin) GetCustomColumnTypeName

func (p *GormPlugin) GetCustomColumnTypeName(columnName string, defaultTypeName string) string

GetCustomColumnTypeName returns empty string by default.

func (*GormPlugin) GetDatabaseMetadata

func (p *GormPlugin) GetDatabaseMetadata() *engine.DatabaseMetadata

GetDatabaseMetadata returns nil by default. Database plugins should override this to provide metadata for frontend configuration.

func (*GormPlugin) GetDatabaseType

func (p *GormPlugin) GetDatabaseType() engine.DatabaseType

GetDatabaseType returns the database type

func (*GormPlugin) GetForeignKeyRelationships

func (p *GormPlugin) GetForeignKeyRelationships(config *engine.PluginConfig, schema string, storageUnit string) (map[string]*engine.ForeignKeyRelationship, error)

GetForeignKeyRelationships returns foreign key relationships for a table (default empty implementation)

func (*GormPlugin) GetGraph

func (p *GormPlugin) GetGraph(config *engine.PluginConfig, schema string) ([]engine.GraphUnit, error)

func (*GormPlugin) GetLastInsertID

func (p *GormPlugin) GetLastInsertID(db *gorm.DB) (int64, error)

GetLastInsertID returns the most recently auto-generated ID. The default implementation returns 0 (unsupported). Database plugins that support this should override it.

func (*GormPlugin) GetMaxBulkInsertParameters

func (p *GormPlugin) GetMaxBulkInsertParameters() int

GetMaxBulkInsertParameters returns the default limit of 65535 parameters.

func (*GormPlugin) GetPlaceholder

func (p *GormPlugin) GetPlaceholder(index int) string

GetPlaceholder returns the placeholder for prepared statements Override this in database-specific implementations

func (*GormPlugin) GetPrimaryKeyColumns

func (p *GormPlugin) GetPrimaryKeyColumns(db *gorm.DB, schema string, tableName string) ([]string, error)

GetPrimaryKeyColumns returns the primary key columns for a table using raw SQL

func (*GormPlugin) GetRowCount

func (p *GormPlugin) GetRowCount(config *engine.PluginConfig, schema string, storageUnit string, where *queryast.WhereCondition) (int64, error)

func (*GormPlugin) GetRows

func (*GormPlugin) GetRowsOrderBy

func (p *GormPlugin) GetRowsOrderBy(db *gorm.DB, schema string, storageUnit string) string

GetRowsOrderBy returns the ORDER BY clause for pagination queries. Default implementation returns empty string (no ordering).

func (*GormPlugin) GetStorageUnits

func (p *GormPlugin) GetStorageUnits(config *engine.PluginConfig, schema string) ([]engine.StorageUnit, error)

func (*GormPlugin) HandleCustomDataType

func (p *GormPlugin) HandleCustomDataType(value string, columnType string, isNullable bool) (any, bool, error)

HandleCustomDataType returns false by default (no custom handling).

func (*GormPlugin) InitPlugin

func (p *GormPlugin) InitPlugin()

InitPlugin initializes the plugin with an error handler

func (*GormPlugin) IsArrayType

func (p *GormPlugin) IsArrayType(columnType string) bool

IsArrayType returns false by default. PostgreSQL overrides this to detect underscore-prefixed array types.

func (*GormPlugin) IsAvailable

func (p *GormPlugin) IsAvailable(ctx context.Context, config *engine.PluginConfig) bool

func (*GormPlugin) IsGeometryType

func (p *GormPlugin) IsGeometryType(columnType string) bool

IsGeometryType returns true for common geometry type names.

func (*GormPlugin) MarkGeneratedColumns

func (p *GormPlugin) MarkGeneratedColumns(config *engine.PluginConfig, schema string, storageUnit string, columns []engine.Column) error

MarkGeneratedColumns is a no-op base implementation. Database plugins should override this to detect auto-increment and computed columns.

func (*GormPlugin) NormalizeType

func (p *GormPlugin) NormalizeType(typeName string) string

NormalizeType returns the type unchanged by default. Database plugins should override this to normalize aliases to canonical types.

func (*GormPlugin) NullifyFKColumn

func (p *GormPlugin) NullifyFKColumn(config *engine.PluginConfig, schema string, storageUnit string, column string) error

NullifyFKColumn sets a column to NULL for all rows where it is not already NULL. Used to break circular FK constraints during mock data overwrite.

func (*GormPlugin) ParseConnectionConfig

func (p *GormPlugin) ParseConnectionConfig(config *engine.PluginConfig) (*ConnectionInput, error)

func (*GormPlugin) QueryComputedColumns

func (p *GormPlugin) QueryComputedColumns(config *engine.PluginConfig, query string, params ...any) (map[string]bool, error)

QueryComputedColumns executes a query and returns a set of computed column names. This is a helper for SQL plugins that query system catalogs for generated/computed columns. The query must return exactly 1 column: column_name.

func (*GormPlugin) QueryForeignKeyRelationships

func (p *GormPlugin) QueryForeignKeyRelationships(config *engine.PluginConfig, query string, params ...any) (map[string]*engine.ForeignKeyRelationship, error)

QueryForeignKeyRelationships executes a foreign key query and returns the relationships map. This is a helper for SQL plugins that query system catalogs for FK information. The query must return exactly 3 columns: column_name, referenced_table, referenced_column.

func (*GormPlugin) ResolveGraphSchema

func (p *GormPlugin) ResolveGraphSchema(config *engine.PluginConfig, schema string) string

ResolveGraphSchema returns the schema parameter unchanged by default. ClickHouse overrides this to return the database name.

func (*GormPlugin) ShouldCheckRowsAffected

func (p *GormPlugin) ShouldCheckRowsAffected() bool

ShouldCheckRowsAffected returns true by default. ClickHouse overrides this to return false since its GORM driver doesn't report affected rows for mutations.

func (*GormPlugin) ShouldHandleColumnType

func (p *GormPlugin) ShouldHandleColumnType(columnType string) bool

ShouldHandleColumnType returns false by default.

func (*GormPlugin) StorageUnitExists

func (p *GormPlugin) StorageUnitExists(config *engine.PluginConfig, schema string, storageUnit string) (bool, error)

func (*GormPlugin) StreamRawExecute

func (p *GormPlugin) StreamRawExecute(config *engine.PluginConfig, query string, writer engine.QueryStreamWriter, params ...any) error

StreamRawExecute streams a raw SQL result row by row without materializing the entire result set in memory first.

func (*GormPlugin) UpdateStorageUnit

func (p *GormPlugin) UpdateStorageUnit(config *engine.PluginConfig, schema string, storageUnit string, values map[string]string, updatedColumns []string) (bool, error)

func (*GormPlugin) WithTransaction

func (p *GormPlugin) WithTransaction(config *engine.PluginConfig, operation func(tx any) error) error

WithTransaction executes the given operation within a database transaction

type GormPluginFunctions

type GormPluginFunctions interface {
	// these below are meant to be generic-ish implementations by the base gorm plugin
	ParseConnectionConfig(config *engine.PluginConfig) (*ConnectionInput, error)

	ConvertStringValue(value, columnType string, isNullable bool) (any, error)
	ConvertRawToRows(raw *sql.Rows) (*engine.GetRowsResult, error)
	ConvertRecordValuesToMap(values []engine.Record) (map[string]any, error)

	// CreateSQLBuilder creates a SQL builder instance - can be overridden by specific plugins
	CreateSQLBuilder(db *gorm.DB) SQLBuilderInterface

	// these below are meant to be implemented by the specific database plugins
	DB(config *engine.PluginConfig) (*gorm.DB, error)
	GetPlaceholder(index int) string

	GetTableInfoQuery() string
	GetStorageUnitExistsQuery() string
	GetPrimaryKeyColQuery() string
	GetAllSchemasQuery() string
	GetCreateTableQuery(db *gorm.DB, schema string, storageUnit string, columns []engine.Record) string

	FormTableName(schema string, storageUnit string) string

	GetSupportedOperators() map[string]string

	GetGraphQueryDB(db *gorm.DB, schema string) *gorm.DB
	GetTableNameAndAttributes(rows *sql.Rows) (string, []engine.Record)

	// GetRowsOrderBy returns the ORDER BY clause for pagination queries
	GetRowsOrderBy(db *gorm.DB, schema string, storageUnit string) string

	// ShouldHandleColumnType returns true if the plugin wants to handle a specific column type
	ShouldHandleColumnType(columnType string) bool

	// GetColumnScanner returns a scanner for a specific column type
	// This is called when ShouldHandleColumnType returns true
	GetColumnScanner(columnType string) any

	// FormatColumnValue formats a scanned value for a specific column type
	// This is called when ShouldHandleColumnType returns true
	FormatColumnValue(columnType string, scanner any) (string, error)

	// GetCustomColumnTypeName returns a custom column type name for display
	// Return empty string to use the default type name
	GetCustomColumnTypeName(columnName string, defaultTypeName string) string

	// IsGeometryType returns true if the column type represents spatial/geometry data
	IsGeometryType(columnType string) bool

	// FormatGeometryValue formats geometry data for display
	// Return empty string to use default hex formatting
	FormatGeometryValue(rawBytes []byte, columnType string) string

	// HandleCustomDataType allows plugins to handle their own data type conversions
	// Return (value, true) if handled, or (nil, false) to use default handling
	HandleCustomDataType(value string, columnType string, isNullable bool) (any, bool, error)

	// IsArrayType returns true if the column type represents an array type.
	// Used to format array type names for display (e.g., PostgreSQL _int4 -> []int4).
	IsArrayType(columnType string) bool

	// ResolveGraphSchema returns the schema to use for graph queries.
	// Most databases use the schema parameter as-is; ClickHouse uses the database name instead.
	ResolveGraphSchema(config *engine.PluginConfig, schema string) string

	// ShouldCheckRowsAffected returns whether the plugin supports checking RowsAffected
	// after UPDATE/DELETE mutations. Returns false for databases like ClickHouse where
	// the GORM driver doesn't report affected rows.
	ShouldCheckRowsAffected() bool

	// GetPrimaryKeyColumns returns the primary key columns for a table
	GetPrimaryKeyColumns(db *gorm.DB, schema string, tableName string) ([]string, error)

	// GetForeignKeyRelationships returns foreign key relationships for a table
	GetForeignKeyRelationships(config *engine.PluginConfig, schema string, storageUnit string) (map[string]*engine.ForeignKeyRelationship, error)

	// GetDatabaseType returns the database type
	GetDatabaseType() engine.DatabaseType

	// GetColumnConstraints retrieves column constraints for a table
	GetColumnConstraints(config *engine.PluginConfig, schema string, storageUnit string) (map[string]map[string]any, error)

	// NormalizeType converts a type alias to its canonical form for this database.
	// For example, PostgreSQL: "INT4" -> "INTEGER", "VARCHAR" -> "CHARACTER VARYING"
	// Returns the input unchanged if no mapping exists.
	NormalizeType(typeName string) string

	// GetColumnTypes returns a map of column names to their type info (type + nullability).
	// Used for type conversion during CRUD operations.
	GetColumnTypes(db *gorm.DB, schema, tableName string) (map[string]ColumnTypeInfo, error)

	// GetLastInsertID returns the most recently auto-generated ID after an INSERT.
	// This is used by the mock data generator to track PKs for FK references.
	// Returns 0 if the database doesn't support this or no ID was generated.
	GetLastInsertID(db *gorm.DB) (int64, error)

	// GetMaxBulkInsertParameters returns the maximum number of parameters supported
	// for bulk insert operations. Used to calculate appropriate batch sizes.
	// Default: 65535 (PostgreSQL/MySQL limit). Override for databases with lower limits.
	GetMaxBulkInsertParameters() int

	// BuildSkipConflictClause returns an OnConflict clause that skips duplicate rows
	// during append-mode imports. Dialect-specific because Postgres uses DO NOTHING
	// while MySQL needs identity assignments (pk = pk) since GORM can't generate the
	// fallback without schema info when using .Table() with map records.
	BuildSkipConflictClause(pkColumns []string) clause.OnConflict
}

type MigratorHelper

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

MigratorHelper provides schema operations using GORM's Migrator interface NOTE: Most methods are not yet used but are prepared for future schema modification features

func NewMigratorHelper

func NewMigratorHelper(db *gorm.DB, plugin GormPluginFunctions) *MigratorHelper

NewMigratorHelper creates a new migrator helper

func (*MigratorHelper) GetColumnTypes

func (m *MigratorHelper) GetColumnTypes(tableName string) (map[string]ColumnTypeInfo, error)

GetColumnTypes gets column types and nullability using Migrator's ColumnTypes. Returns types with length info when available (e.g., "VARCHAR(255)").

func (*MigratorHelper) GetConstraints

func (m *MigratorHelper) GetConstraints(tableName string) (map[string][]gorm.ColumnType, error)

GetConstraints gets table constraints using Migrator

func (*MigratorHelper) GetOrderedColumns

func (m *MigratorHelper) GetOrderedColumns(tableName string) ([]engine.Column, error)

GetOrderedColumns returns columns in their definition order. Returns types with length info when available and normalized to canonical form.

func (*MigratorHelper) TableExists

func (m *MigratorHelper) TableExists(tableName string) bool

TableExists checks if a table exists using Migrator

type MinMaxResult

type MinMaxResult struct {
	Min    float64
	Max    float64
	HasMin bool
	HasMax bool
}

MinMaxResult holds the result of parsing min/max constraints

func ParseMinMaxConstraints

func ParseMinMaxConstraints(clause string) MinMaxResult

ParseMinMaxConstraints extracts min/max values from CHECK constraint clauses. Handles patterns like:

  • column >= 0
  • column > 0
  • column <= 100
  • column < 100
  • column BETWEEN 0 AND 100
  • ([column]>=(0)), ([column]>(0))

type PrimaryKeyDecorator

type PrimaryKeyDecorator func(def ColumnDef, column engine.Record) ColumnDef

PrimaryKeyDecorator customizes how a primary key column is decorated. It receives the column def (with Name and Type already set) and the original engine.Record, and returns the modified def.

type SQLBuilder

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

SQLBuilder provides SQL query building functionality This builder prioritizes GORM's native methods for all operations where possible.

func NewSQLBuilder

func NewSQLBuilder(db *gorm.DB, plugin GormPluginFunctions) *SQLBuilder

NewSQLBuilder creates a new SQL builder

func (*SQLBuilder) BuildFullTableName

func (sb *SQLBuilder) BuildFullTableName(schema, table string) string

BuildFullTableName builds a fully qualified table name for GORM operations GORM's dialector handles identifier quoting internally via QuoteTo method.

func (*SQLBuilder) BuildOrderBy

func (sb *SQLBuilder) BuildOrderBy(query *gorm.DB, sortList []plugins.Sort) *gorm.DB

BuildOrderBy builds ORDER BY clause using GORM's Order method GORM handles column name escaping automatically

func (*SQLBuilder) CountQuery

func (sb *SQLBuilder) CountQuery(schema, table string) (int64, error)

CountQuery builds a COUNT query using GORM's Count method GORM handles all escaping automatically

func (*SQLBuilder) CreateTableQuery

func (sb *SQLBuilder) CreateTableQuery(schema, table string, columns []ColumnDef) string

CreateTableQuery builds a CREATE TABLE statement for DDL operations DDL requires manual SQL building as GORM doesn't support dynamic table creation

func (*SQLBuilder) CreateTableQueryWithSuffix

func (sb *SQLBuilder) CreateTableQueryWithSuffix(schema, table string, columns []ColumnDef, suffix string) string

CreateTableQueryWithSuffix builds a CREATE TABLE statement with a suffix (for ClickHouse ENGINE, etc)

func (*SQLBuilder) DeleteQuery

func (sb *SQLBuilder) DeleteQuery(schema, table string, conditions map[string]any) *gorm.DB

DeleteQuery builds a DELETE query using GORM's Delete method GORM handles all escaping automatically

func (*SQLBuilder) GetDB

func (sb *SQLBuilder) GetDB() *gorm.DB

GetDB returns the underlying GORM DB instance

func (*SQLBuilder) GetTableQuery

func (sb *SQLBuilder) GetTableQuery(schema, table string) *gorm.DB

GetTableQuery creates a GORM query with the appropriate table reference This method can be overridden by database-specific implementations

func (*SQLBuilder) InsertRow

func (sb *SQLBuilder) InsertRow(schema, table string, data map[string]any) error

InsertRow inserts a row using GORM's Create method GORM handles all escaping automatically

func (*SQLBuilder) QuoteIdentifier

func (sb *SQLBuilder) QuoteIdentifier(identifier string) string

QuoteIdentifier quotes an identifier (column/table name) ONLY for DDL operations For DML operations (SELECT, INSERT, UPDATE, DELETE), use GORM's native methods which handle escaping automatically

func (*SQLBuilder) SelectQuery

func (sb *SQLBuilder) SelectQuery(schema, table string, columns []string, conditions map[string]any) *gorm.DB

SelectQuery builds a SELECT query using GORM's query builder GORM handles all escaping automatically

func (*SQLBuilder) SetSelf

func (sb *SQLBuilder) SetSelf(self SQLBuilderInterface)

SetSelf sets the reference to the actual implementation (used by subclasses)

func (*SQLBuilder) UpdateQuery

func (sb *SQLBuilder) UpdateQuery(schema, table string, updates map[string]any, conditions map[string]any) *gorm.DB

UpdateQuery builds an UPDATE query using GORM's Update methods GORM handles all escaping automatically

type SQLBuilderInterface

type SQLBuilderInterface interface {
	QuoteIdentifier(identifier string) string
	BuildFullTableName(schema, table string) string
	GetTableQuery(schema, table string) *gorm.DB
	SelectQuery(schema, table string, columns []string, conditions map[string]any) *gorm.DB
	BuildOrderBy(query *gorm.DB, sortList []plugins.Sort) *gorm.DB
	CreateTableQuery(schema, table string, columns []ColumnDef) string
	CreateTableQueryWithSuffix(schema, table string, columns []ColumnDef, suffix string) string
	InsertRow(schema, table string, data map[string]any) error
	UpdateQuery(schema, table string, updates map[string]any, conditions map[string]any) *gorm.DB
	DeleteQuery(schema, table string, conditions map[string]any) *gorm.DB
	CountQuery(schema, table string) (int64, error)
}

SQLBuilderInterface defines the methods that can be overridden by database-specific implementations

Jump to

Keyboard shortcuts

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