types

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModelNameToTableName

func ModelNameToTableName(modelName string) string

ModelNameToTableName converts model name to default table name (pluralized, snake_case)

Types

type AggregationCondition

type AggregationCondition struct {
	BaseCondition
	AggregationType string // "sum", "avg", "min", "max", "count"
	FieldName       string
	Operator        string
	Value           any
}

AggregationCondition represents a condition on an aggregated value

func NewAggregationCondition

func NewAggregationCondition(aggType, fieldName, operator string, value any) *AggregationCondition

NewAggregationCondition creates a new aggregation condition

func (*AggregationCondition) ToSQL

func (a *AggregationCondition) ToSQL(ctx *ConditionContext) (string, []any)

ToSQL generates the SQL for this aggregation condition

type AggregationQuery

type AggregationQuery interface {
	// Grouping
	GroupBy(fieldNames ...string) AggregationQuery
	Having(condition Condition) AggregationQuery

	// Aggregation functions
	Count(fieldName string, alias string) AggregationQuery
	CountAll(alias string) AggregationQuery
	Sum(fieldName string, alias string) AggregationQuery
	Avg(fieldName string, alias string) AggregationQuery
	Min(fieldName string, alias string) AggregationQuery
	Max(fieldName string, alias string) AggregationQuery

	// Selection of grouped fields
	Select(fieldNames ...string) AggregationQuery

	// Conditions and ordering
	Where(fieldName string) FieldCondition
	WhereCondition(condition Condition) AggregationQuery
	OrderBy(fieldName string, direction Order) AggregationQuery
	OrderByAggregation(aggregationType string, fieldName string, direction Order) AggregationQuery
	Limit(limit int) AggregationQuery
	Offset(offset int) AggregationQuery

	// Execution
	Exec(ctx context.Context, dest any) error

	// Internal methods
	BuildSQL() (string, []any, error)
	GetModelName() string
}

AggregationQuery interface for aggregation operations

type AndCondition

type AndCondition struct {
	Conditions []Condition
}

AndCondition represents AND logic

func NewAndCondition

func NewAndCondition(conditions ...Condition) *AndCondition

func (*AndCondition) And

func (c *AndCondition) And(condition Condition) Condition

func (*AndCondition) Not

func (c *AndCondition) Not() Condition

func (*AndCondition) Or

func (c *AndCondition) Or(condition Condition) Condition

func (*AndCondition) ToSQL

func (c *AndCondition) ToSQL(ctx *ConditionContext) (string, []any)

type BaseCondition

type BaseCondition struct {
	SQL  string
	Args []any
}

BaseCondition implements common condition functionality

func NewBaseCondition

func NewBaseCondition(sql string, args ...any) *BaseCondition

Helper function to create BaseCondition

func (BaseCondition) And

func (c BaseCondition) And(condition Condition) Condition

func (BaseCondition) Not

func (c BaseCondition) Not() Condition

func (BaseCondition) Or

func (c BaseCondition) Or(condition Condition) Condition

func (BaseCondition) ToSQL

func (c BaseCondition) ToSQL(ctx *ConditionContext) (string, []any)

type ChangeType

type ChangeType string

ChangeType represents the type of schema change

const (
	ChangeTypeCreateTable ChangeType = "CREATE_TABLE"
	ChangeTypeDropTable   ChangeType = "DROP_TABLE"
	ChangeTypeAddColumn   ChangeType = "ADD_COLUMN"
	ChangeTypeDropColumn  ChangeType = "DROP_COLUMN"
	ChangeTypeAlterColumn ChangeType = "ALTER_COLUMN"
	ChangeTypeAddIndex    ChangeType = "ADD_INDEX"
	ChangeTypeDropIndex   ChangeType = "DROP_INDEX"
	ChangeTypeAddFK       ChangeType = "ADD_FOREIGN_KEY"
	ChangeTypeDropFK      ChangeType = "DROP_FOREIGN_KEY"
)

type ColumnChange

type ColumnChange struct {
	TableName  string
	ColumnName string
	OldColumn  *ColumnInfo // nil for additions
	NewColumn  *ColumnInfo // nil for deletions
}

type ColumnInfo

type ColumnInfo struct {
	Name          string
	Type          string
	Nullable      bool
	Default       any
	PrimaryKey    bool
	AutoIncrement bool
	Unique        bool
}

type Condition

type Condition interface {
	ToSQL(ctx *ConditionContext) (string, []any)
	And(condition Condition) Condition
	Or(condition Condition) Condition
	Not() Condition
}

Condition interface for query conditions

func And

func And(conditions ...Condition) Condition

Utility functions for building conditions

func Not

func Not(condition Condition) Condition

func Or

func Or(conditions ...Condition) Condition

func Raw

func Raw(sql string, args ...any) Condition

type ConditionContext

type ConditionContext struct {
	FieldMapper     FieldMapper
	ModelName       string
	TableAlias      string
	JoinedTables    map[string]JoinInfo // For complex queries with joins
	QuoteIdentifier func(string) string // Function to quote identifiers
}

ConditionContext provides context information for SQL generation

func NewConditionContext

func NewConditionContext(fieldMapper FieldMapper, modelName string, tableAlias string) *ConditionContext

NewConditionContext creates a new condition context

func (*ConditionContext) MapFieldToColumn

func (ctx *ConditionContext) MapFieldToColumn(fieldName string) (string, error)

MapFieldToColumn maps a field name to its column name with proper table alias

type ConflictAction

type ConflictAction int

ConflictAction represents action to take on insert conflicts

const (
	ConflictIgnore ConflictAction = iota
	ConflictReplace
	ConflictUpdate
)

type Database

type Database interface {
	// Connection management
	Connect(ctx context.Context) error
	Close() error
	Ping(ctx context.Context) error

	// Schema management
	RegisterSchema(modelName string, schema *schema.Schema) error
	GetSchema(modelName string) (*schema.Schema, error)
	CreateModel(ctx context.Context, modelName string) error
	DropModel(ctx context.Context, modelName string) error

	// Schema loading with auto-migration
	LoadSchema(ctx context.Context, schemaContent string) error
	LoadSchemaFrom(ctx context.Context, filename string) error
	SyncSchemas(ctx context.Context) error

	// Model query builder (uses modelName)
	Model(modelName string) ModelQuery
	Raw(sql string, args ...any) RawQuery

	// Transaction management
	Begin(ctx context.Context) (Transaction, error)
	Transaction(ctx context.Context, fn func(tx Transaction) error) error

	// Metadata
	GetModels() []string
	GetModelSchema(modelName string) (*schema.Schema, error)
	GetDriverType() string
	GetCapabilities() DriverCapabilities

	// Internal field mapping (used by driver implementations)
	ResolveTableName(modelName string) (string, error)
	ResolveFieldName(modelName, fieldName string) (string, error)
	ResolveFieldNames(modelName string, fieldNames []string) ([]string, error)

	// Raw query support
	Exec(query string, args ...any) (sql.Result, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row

	// Migration support
	GetMigrator() DatabaseMigrator

	// Logger configuration
	SetLogger(l logger.Logger)
	GetLogger() logger.Logger
}

Database interface defines all database operations

type DatabaseMigrator

type DatabaseMigrator interface {
	// Introspection
	GetTables() ([]string, error)
	GetTableInfo(tableName string) (*TableInfo, error)
	IsSystemTable(tableName string) bool

	// SQL Generation
	GenerateCreateTableSQL(schema any) (string, error)
	GenerateDropTableSQL(tableName string) string
	GenerateAddColumnSQL(tableName string, field any) (string, error)
	GenerateModifyColumnSQL(change ColumnChange) ([]string, error)
	GenerateDropColumnSQL(tableName, columnName string) ([]string, error)
	GenerateCreateIndexSQL(tableName, indexName string, columns []string, unique bool) string
	GenerateDropIndexSQL(indexName string) string

	// Migration execution
	ApplyMigration(sql string) error
	GetDatabaseType() string

	// Schema comparison and migration planning
	CompareSchema(existingTable *TableInfo, desiredSchema any) (*MigrationPlan, error)
	GenerateMigrationSQL(plan *MigrationPlan) ([]string, error)
}

type DatabaseSpecificMigrator

type DatabaseSpecificMigrator interface {
	// Database introspection
	GetTables() ([]string, error)
	GetTableInfo(tableName string) (*TableInfo, error)

	// SQL generation
	GenerateCreateTableSQL(s *schema.Schema) (string, error)
	GenerateDropTableSQL(tableName string) string
	GenerateAddColumnSQL(tableName string, field any) (string, error)
	GenerateModifyColumnSQL(change ColumnChange) ([]string, error)
	GenerateDropColumnSQL(tableName, columnName string) ([]string, error)
	GenerateCreateIndexSQL(tableName, indexName string, columns []string, unique bool) string
	GenerateDropIndexSQL(indexName string) string

	// Migration execution
	ApplyMigration(sql string) error
	GetDatabaseType() string

	// Type mapping and column generation
	MapFieldType(field schema.Field) string
	FormatDefaultValue(value any) string
	GenerateColumnDefinitionFromColumnInfo(col ColumnInfo) string
	ConvertFieldToColumnInfo(field schema.Field) *ColumnInfo

	// Default value handling
	ParseDefaultValue(value any, fieldType schema.FieldType) any
	NormalizeDefaultToPrismaFunction(value any, fieldType schema.FieldType) (string, bool)

	// Index management
	IsSystemIndex(indexName string) bool
	IsPrimaryKeyIndex(indexName string) bool

	// Table management
	IsSystemTable(tableName string) bool

	// Type conversion from database to schema
	MapDatabaseTypeToFieldType(dbType string) schema.FieldType
}

DatabaseSpecificMigrator defines database-specific operations that each driver must implement

type DefaultFieldMapper

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

DefaultFieldMapper provides default field mapping implementation

func NewDefaultFieldMapper

func NewDefaultFieldMapper() *DefaultFieldMapper

NewDefaultFieldMapper creates a new default field mapper

func (*DefaultFieldMapper) ColumnFieldsToSchema

func (m *DefaultFieldMapper) ColumnFieldsToSchema(modelName string, columnNames []string) ([]string, error)

ColumnFieldsToSchema converts multiple column names to schema field names

func (*DefaultFieldMapper) ColumnToSchema

func (m *DefaultFieldMapper) ColumnToSchema(modelName, columnName string) (string, error)

ColumnToSchema converts a database column name to schema field name

func (*DefaultFieldMapper) GetSchema

func (m *DefaultFieldMapper) GetSchema(modelName string) (*schema.Schema, error)

GetSchema returns a registered schema

func (*DefaultFieldMapper) MapColumnToSchemaData

func (m *DefaultFieldMapper) MapColumnToSchemaData(modelName string, data map[string]any) (map[string]any, error)

MapColumnToSchemaData converts data map from column names to schema field names

func (*DefaultFieldMapper) MapSchemaToColumnData

func (m *DefaultFieldMapper) MapSchemaToColumnData(modelName string, data map[string]any) (map[string]any, error)

MapSchemaToColumnData converts data map from schema field names to column names

func (*DefaultFieldMapper) ModelToTable

func (m *DefaultFieldMapper) ModelToTable(modelName string) (string, error)

ModelToTable converts model name to table name

func (*DefaultFieldMapper) RegisterSchema

func (m *DefaultFieldMapper) RegisterSchema(modelName string, s *schema.Schema)

RegisterSchema registers a schema for field mapping

func (*DefaultFieldMapper) SchemaFieldsToColumns

func (m *DefaultFieldMapper) SchemaFieldsToColumns(modelName string, fieldNames []string) ([]string, error)

SchemaFieldsToColumns converts multiple schema field names to column names

func (*DefaultFieldMapper) SchemaToColumn

func (m *DefaultFieldMapper) SchemaToColumn(modelName, fieldName string) (string, error)

SchemaToColumn converts a schema field name to database column name

type DeleteQuery

type DeleteQuery interface {
	Where(fieldName string) FieldCondition
	WhereCondition(condition Condition) DeleteQuery
	Returning(fieldNames ...string) DeleteQuery

	// Execution
	Exec(ctx context.Context) (Result, error)

	// Internal methods
	BuildSQL() (string, []any, error)
	GetModelName() string
}

DeleteQuery interface for delete operations

type DriverCapabilities

type DriverCapabilities interface {
	// SQL dialect features
	SupportsReturning() bool
	SupportsDefaultValues() bool
	RequiresLimitForOffset() bool
	SupportsDistinctOn() bool
	SupportsForeignKeys() bool

	// Identifier quoting
	QuoteIdentifier(name string) string
	GetPlaceholder(index int) string

	// Type conversion
	GetBooleanLiteral(value bool) string
	NeedsTypeConversion() bool
	GetNullsOrderingSQL(direction Order, nullsFirst bool) string

	// Index/Table detection
	IsSystemIndex(indexName string) bool
	IsSystemTable(tableName string) bool

	// Driver identification
	GetDriverType() DriverType
	GetSupportedSchemes() []string

	// NoSQL features
	IsNoSQL() bool
	SupportsTransactions() bool
	SupportsNestedDocuments() bool
	SupportsArrayFields() bool
	SupportsAggregationPipeline() bool
}

DriverCapabilities defines what a driver supports

type DriverType

type DriverType string

DriverType represents a database driver type It's defined as a string to allow extensibility for new database drivers

const (
	DriverSQLite     DriverType = "sqlite"
	DriverMySQL      DriverType = "mysql"
	DriverPostgreSQL DriverType = "postgresql"
	DriverMongoDB    DriverType = "mongodb"
)

Well-known driver types (for convenience and documentation)

func ParseDriverType

func ParseDriverType(s string) (DriverType, error)

ParseDriverType parses a string into a DriverType This is primarily used for parsing configuration and maintaining backward compatibility

func (DriverType) String

func (d DriverType) String() string

String returns the string representation of the driver type

type FieldCondition

type FieldCondition interface {
	// Basic comparisons
	Equals(value any) Condition
	NotEquals(value any) Condition
	GreaterThan(value any) Condition
	GreaterThanOrEqual(value any) Condition
	LessThan(value any) Condition
	LessThanOrEqual(value any) Condition

	// Collection operations
	In(values ...any) Condition
	NotIn(values ...any) Condition

	// String operations
	Contains(value string) Condition
	StartsWith(value string) Condition
	EndsWith(value string) Condition
	Like(pattern string) Condition

	// Null checks
	IsNull() Condition
	IsNotNull() Condition

	// Range operations
	Between(min, max any) Condition

	// Internal methods (for driver implementation)
	GetFieldName() string
	GetModelName() string
}

FieldCondition interface for field-specific conditions

type FieldConditionImpl

type FieldConditionImpl struct {
	ModelName string
	FieldName string
}

FieldConditionImpl implements FieldCondition interface

func NewFieldCondition

func NewFieldCondition(modelName, fieldName string) *FieldConditionImpl

func (*FieldConditionImpl) Between

func (f *FieldConditionImpl) Between(min, max any) Condition

func (*FieldConditionImpl) Contains

func (f *FieldConditionImpl) Contains(value string) Condition

func (*FieldConditionImpl) EndsWith

func (f *FieldConditionImpl) EndsWith(value string) Condition

func (*FieldConditionImpl) Equals

func (f *FieldConditionImpl) Equals(value any) Condition

func (*FieldConditionImpl) GetFieldName

func (f *FieldConditionImpl) GetFieldName() string

func (*FieldConditionImpl) GetModelName

func (f *FieldConditionImpl) GetModelName() string

func (*FieldConditionImpl) GreaterThan

func (f *FieldConditionImpl) GreaterThan(value any) Condition

func (*FieldConditionImpl) GreaterThanOrEqual

func (f *FieldConditionImpl) GreaterThanOrEqual(value any) Condition

func (*FieldConditionImpl) In

func (f *FieldConditionImpl) In(values ...any) Condition

func (*FieldConditionImpl) IsNotNull

func (f *FieldConditionImpl) IsNotNull() Condition

func (*FieldConditionImpl) IsNull

func (f *FieldConditionImpl) IsNull() Condition

func (*FieldConditionImpl) LessThan

func (f *FieldConditionImpl) LessThan(value any) Condition

func (*FieldConditionImpl) LessThanOrEqual

func (f *FieldConditionImpl) LessThanOrEqual(value any) Condition

func (*FieldConditionImpl) Like

func (f *FieldConditionImpl) Like(pattern string) Condition

func (*FieldConditionImpl) NotEquals

func (f *FieldConditionImpl) NotEquals(value any) Condition

func (*FieldConditionImpl) NotIn

func (f *FieldConditionImpl) NotIn(values ...any) Condition

func (*FieldConditionImpl) StartsWith

func (f *FieldConditionImpl) StartsWith(value string) Condition

type FieldMapper

type FieldMapper interface {
	// Field name mapping
	SchemaToColumn(modelName, fieldName string) (string, error)
	ColumnToSchema(modelName, columnName string) (string, error)

	// Batch mapping
	SchemaFieldsToColumns(modelName string, fieldNames []string) ([]string, error)
	ColumnFieldsToSchema(modelName string, columnNames []string) ([]string, error)

	// Data mapping
	MapSchemaToColumnData(modelName string, data map[string]any) (map[string]any, error)
	MapColumnToSchemaData(modelName string, data map[string]any) (map[string]any, error)

	// Table name mapping
	ModelToTable(modelName string) (string, error)
}

FieldMapper interface for field name mapping between schema and database

type ForeignKeyInfo

type ForeignKeyInfo struct {
	Name             string
	Column           string
	ReferencedTable  string
	ReferencedColumn string
	OnDelete         string
	OnUpdate         string
}

type IncludeOption

type IncludeOption struct {
	// The relation path (e.g., "posts" or "posts.comments")
	Path string

	// Fields to select from the related model (nil means all fields)
	Select []string

	// Where conditions to filter the related records
	Where Condition

	// Order by for the related records
	OrderBy []OrderByOption

	// Limit for the related records
	Limit *int

	// Offset for the related records
	Offset *int
}

IncludeOption represents options for including relations

type IncludeOptions

type IncludeOptions map[string]*IncludeOption

IncludeOptions represents a collection of include options

type IndexChange

type IndexChange struct {
	TableName string
	IndexName string
	OldIndex  *IndexInfo // nil for additions
	NewIndex  *IndexInfo // nil for deletions
}

type IndexDefinition

type IndexDefinition struct {
	Name    string   `json:"name"`
	Columns []string `json:"columns"`
	Unique  bool     `json:"unique"`
}

IndexDefinition stores the definition of an index

type IndexInfo

type IndexInfo struct {
	Name    string
	Columns []string
	Unique  bool
}

type InsertQuery

type InsertQuery interface {
	// Data uses schema field names
	Values(data ...any) InsertQuery
	OnConflict(action ConflictAction) InsertQuery
	Returning(fieldNames ...string) InsertQuery

	// Execution
	Exec(ctx context.Context) (Result, error)
	ExecAndReturn(ctx context.Context, dest any) error

	// Internal methods
	BuildSQL() (string, []any, error)
	GetModelName() string
}

InsertQuery interface for insert operations

type JoinInfo

type JoinInfo struct {
	ModelName  string
	TableAlias string
}

JoinInfo contains information about a joined table

type MappedFieldCondition

type MappedFieldCondition struct {
	BaseCondition
	// contains filtered or unexported fields
}

MappedFieldCondition wraps a base condition with field mapping support

func (*MappedFieldCondition) And

func (f *MappedFieldCondition) And(condition Condition) Condition

And combines this condition with another using AND logic

func (*MappedFieldCondition) GetArgs

func (f *MappedFieldCondition) GetArgs() []any

GetArgs returns the SQL arguments

func (*MappedFieldCondition) GetFieldName

func (f *MappedFieldCondition) GetFieldName() string

GetFieldName returns the field name

func (*MappedFieldCondition) GetModelName

func (f *MappedFieldCondition) GetModelName() string

GetModelName returns the model name

func (*MappedFieldCondition) GetSQL

func (f *MappedFieldCondition) GetSQL() string

GetSQL returns the SQL string

func (*MappedFieldCondition) Not

Not negates this condition

func (*MappedFieldCondition) Or

func (f *MappedFieldCondition) Or(condition Condition) Condition

Or combines this condition with another using OR logic

func (*MappedFieldCondition) ToSQL

func (f *MappedFieldCondition) ToSQL(ctx *ConditionContext) (string, []any)

ToSQL generates SQL with proper field mapping

type Migration

type Migration struct {
	ID        int
	Version   string
	Name      string
	AppliedAt time.Time
	Checksum  string
}

Migration represents a database migration

type MigrationFile

type MigrationFile struct {
	Version  string
	Name     string
	UpSQL    string
	DownSQL  string
	Metadata MigrationMetadata
}

MigrationFile represents a migration file on disk

type MigrationMetadata

type MigrationMetadata struct {
	Version     string            `json:"version"`
	Name        string            `json:"name"`
	Checksum    string            `json:"checksum"`
	CreatedAt   time.Time         `json:"created_at"`
	Description string            `json:"description"`
	Changes     []SchemaChange    `json:"changes"`
	Schemas     map[string]string `json:"schemas"` // Model name -> schema hash
}

MigrationMetadata contains metadata for a migration file

type MigrationMode

type MigrationMode string

MigrationMode represents the migration execution mode

const (
	MigrationModeAuto MigrationMode = "auto" // Auto-migrate (development)
	MigrationModeFile MigrationMode = "file" // File-based migrations (production)
)

type MigrationOptions

type MigrationOptions struct {
	AutoMigrate   bool
	DryRun        bool
	Force         bool          // Force destructive changes without confirmation
	Mode          MigrationMode // Migration mode (auto or file)
	MigrationsDir string        // Directory containing migration files
}

MigrationOptions contains options for migration operations

type MigrationPlan

type MigrationPlan struct {
	CreateTables  []string       // Tables to be created
	AddColumns    []ColumnChange // Columns to be added
	ModifyColumns []ColumnChange // Columns to be modified
	DropColumns   []ColumnChange // Columns to be dropped
	AddIndexes    []IndexChange  // Indexes to be added
	DropIndexes   []IndexChange  // Indexes to be dropped
}

type ModelQuery

type ModelQuery interface {
	// Query building (uses schema field names)
	Select(fields ...string) SelectQuery
	Insert(data any) InsertQuery
	Update(data any) UpdateQuery
	Delete() DeleteQuery
	Aggregate() AggregationQuery

	// Condition building (uses schema field names)
	Where(fieldName string) FieldCondition
	WhereCondition(condition Condition) ModelQuery
	WhereRaw(sql string, args ...any) ModelQuery

	// Relation queries (uses relation names)
	Include(relations ...string) ModelQuery
	With(relations ...string) ModelQuery

	// Sorting and pagination (uses schema field names)
	OrderBy(fieldName string, direction Order) ModelQuery
	GroupBy(fieldNames ...string) ModelQuery
	Having(condition Condition) ModelQuery
	Limit(limit int) ModelQuery
	Offset(offset int) ModelQuery

	// Execution
	FindMany(ctx context.Context, dest any) error
	FindUnique(ctx context.Context, dest any) error
	FindFirst(ctx context.Context, dest any) error
	Count(ctx context.Context) (int64, error)
	Exists(ctx context.Context) (bool, error)

	// Aggregation (uses schema field names)
	Sum(ctx context.Context, fieldName string) (float64, error)
	Avg(ctx context.Context, fieldName string) (float64, error)
	Max(ctx context.Context, fieldName string) (any, error)
	Min(ctx context.Context, fieldName string) (any, error)

	// Internal
	GetModelName() string
}

ModelQuery interface for model-based queries

type NotCondition

type NotCondition struct {
	Condition Condition
}

NotCondition represents NOT logic

func NewNotCondition

func NewNotCondition(condition Condition) *NotCondition

func (*NotCondition) And

func (c *NotCondition) And(condition Condition) Condition

func (*NotCondition) Not

func (c *NotCondition) Not() Condition

func (*NotCondition) Or

func (c *NotCondition) Or(condition Condition) Condition

func (*NotCondition) ToSQL

func (c *NotCondition) ToSQL(ctx *ConditionContext) (string, []any)

type OrCondition

type OrCondition struct {
	Conditions []Condition
}

OrCondition represents OR logic

func NewOrCondition

func NewOrCondition(conditions ...Condition) *OrCondition

func (*OrCondition) And

func (c *OrCondition) And(condition Condition) Condition

func (*OrCondition) Not

func (c *OrCondition) Not() Condition

func (*OrCondition) Or

func (c *OrCondition) Or(condition Condition) Condition

func (*OrCondition) ToSQL

func (c *OrCondition) ToSQL(ctx *ConditionContext) (string, []any)

type Order

type Order int

Order represents sorting direction

const (
	ASC Order = iota
	DESC
)

type OrderByClause

type OrderByClause struct {
	Field     string
	Direction Order
}

OrderByClause represents an ORDER BY clause

type OrderByOption

type OrderByOption struct {
	Field     string
	Direction Order
}

OrderByOption represents ordering option for includes

type RawCondition

type RawCondition struct {
	BaseCondition
}

RawCondition for raw SQL conditions

func NewRawCondition

func NewRawCondition(sql string, args ...any) *RawCondition

type RawQuery

type RawQuery interface {
	Exec(ctx context.Context) (Result, error)
	Find(ctx context.Context, dest any) error
	FindOne(ctx context.Context, dest any) error
}

RawQuery interface for raw SQL queries

type Result

type Result struct {
	LastInsertID int64
	RowsAffected int64
}

Result represents operation result

type SchemaChange

type SchemaChange struct {
	Type       ChangeType
	TableName  string
	ColumnName string
	IndexName  string
	SQL        string
	// IndexDef stores index definition for DROP_INDEX changes
	// This allows recreating the index during rollback
	IndexDef *IndexDefinition `json:"index_def,omitempty"`
}

SchemaChange represents a single schema change

type SelectQuery

type SelectQuery interface {
	// Condition building
	Where(fieldName string) FieldCondition
	WhereCondition(condition Condition) SelectQuery
	Include(relations ...string) SelectQuery
	IncludeWithOptions(path string, opt *IncludeOption) SelectQuery
	OrderBy(fieldName string, direction Order) SelectQuery
	GroupBy(fieldNames ...string) SelectQuery
	Having(condition Condition) SelectQuery
	Limit(limit int) SelectQuery
	Offset(offset int) SelectQuery
	Distinct() SelectQuery
	DistinctOn(fieldNames ...string) SelectQuery

	// Execution
	FindMany(ctx context.Context, dest any) error
	FindFirst(ctx context.Context, dest any) error
	Count(ctx context.Context) (int64, error)

	// Internal methods (for driver implementation)
	BuildSQL() (string, []any, error)
	GetModelName() string
}

SelectQuery interface for select operations

type TableInfo

type TableInfo struct {
	Name        string
	Columns     []ColumnInfo
	Indexes     []IndexInfo
	ForeignKeys []ForeignKeyInfo
}

Migration types for backward compatibility

type Transaction

type Transaction interface {
	// Inherit all model query capabilities
	Model(modelName string) ModelQuery
	Raw(sql string, args ...any) RawQuery

	// Transaction control
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error

	// Savepoints
	Savepoint(ctx context.Context, name string) error
	RollbackTo(ctx context.Context, name string) error

	// Batch operations (uses schema field names)
	CreateMany(ctx context.Context, modelName string, data []any) (Result, error)
	UpdateMany(ctx context.Context, modelName string, condition Condition, data any) (Result, error)
	DeleteMany(ctx context.Context, modelName string, condition Condition) (Result, error)
}

Transaction interface for database transactions

type URIParser

type URIParser interface {
	// ParseURI parses a database URI and returns a native database URI/DSN if the URI is supported by this driver
	// Returns an error if the URI format is not supported or invalid
	ParseURI(uri string) (string, error)

	// GetSupportedSchemes returns the URI schemes this parser supports (e.g., ["sqlite"])
	GetSupportedSchemes() []string

	// GetDriverType returns the driver type this parser is for
	GetDriverType() string
}

URIParser defines the interface for database-specific URI parsing

type UpdateQuery

type UpdateQuery interface {
	// Data uses schema field names
	Set(data any) UpdateQuery
	Where(fieldName string) FieldCondition
	WhereCondition(condition Condition) UpdateQuery
	Returning(fieldNames ...string) UpdateQuery

	// Atomic operations (uses schema field names)
	Increment(fieldName string, value int64) UpdateQuery
	Decrement(fieldName string, value int64) UpdateQuery

	// Execution
	Exec(ctx context.Context) (Result, error)
	ExecAndReturn(ctx context.Context, dest any) error

	// Internal methods
	BuildSQL() (string, []any, error)
	GetModelName() string
}

UpdateQuery interface for update operations

Jump to

Keyboard shortcuts

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