connector

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SanitizeDSN

func SanitizeDSN(driver, dsn string) string

SanitizeDSN ensures that URL-style DSNs (postgres://, sqlserver://) have their userinfo (especially the password) properly percent-encoded. Raw passwords containing @, #, %, or other URL-special characters cause the Go URL parser to mis-split the authority component, leading to connection failures that surface as "Service not found" because the connector never registers in the live registry.

MySQL DSNs are normalized to use the tcp() wrapper required by go-sql-driver. Snowflake uses its own non-URL DSN format and is returned unchanged.

Types

type ConnectionConfig

type ConnectionConfig struct {
	Driver          string
	DSN             string
	SchemaName      string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
	PrivateKeyPath  string // Path to PEM-encoded private key file (Snowflake JWT auth)
}

ConnectionConfig holds database connection parameters.

type Connector

type Connector interface {
	// Connection management
	Connect(cfg ConnectionConfig) error
	Disconnect() error
	Ping(ctx context.Context) error
	DB() *sqlx.DB
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)

	// Schema introspection
	IntrospectSchema(ctx context.Context) (*model.Schema, error)
	IntrospectTable(ctx context.Context, tableName string) (*model.TableSchema, error)
	GetTableNames(ctx context.Context) ([]string, error)
	GetStoredProcedures(ctx context.Context) ([]model.StoredProcedure, error)

	// Query building (database-specific SQL dialect)
	BuildSelect(ctx context.Context, req SelectRequest) (string, []interface{}, error)
	BuildInsert(ctx context.Context, req InsertRequest) (string, []interface{}, error)
	BuildUpdate(ctx context.Context, req UpdateRequest) (string, []interface{}, error)
	BuildDelete(ctx context.Context, req DeleteRequest) (string, []interface{}, error)
	BuildCount(ctx context.Context, req CountRequest) (string, []interface{}, error)

	// Schema modification
	CreateTable(ctx context.Context, def model.TableSchema) error
	AlterTable(ctx context.Context, tableName string, changes []SchemaChange) error
	DropTable(ctx context.Context, tableName string) error

	// Stored procedures
	CallProcedure(ctx context.Context, name string, params map[string]interface{}) ([]map[string]interface{}, error)

	// Metadata
	DriverName() string
	QuoteIdentifier(name string) string
	SupportsReturning() bool
	SupportsUpsert() bool
	ParameterPlaceholder(index int) string
}

Connector is the interface that all database connectors must implement.

type CountRequest

type CountRequest struct {
	Table      string
	Filter     string
	FilterArgs []interface{}
}

CountRequest represents a count query.

type DeleteRequest

type DeleteRequest struct {
	Table      string
	Filter     string
	FilterArgs []interface{}
	IDs        []interface{}
}

DeleteRequest represents a delete operation.

type Factory

type Factory func() Connector

Factory is a function that creates a new Connector instance.

type InsertRequest

type InsertRequest struct {
	Table   string
	Records []map[string]interface{}
}

InsertRequest represents an insert operation.

type QueryExecutor added in v0.1.5

type QueryExecutor interface {
	QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
}

QueryExecutor is the common interface satisfied by both *sqlx.DB and *sqlx.Tx, allowing handlers to execute queries transparently inside or outside a transaction.

type Registry

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

Registry manages connector factories and active connections.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new empty Registry.

func (*Registry) CloseAll

func (r *Registry) CloseAll()

CloseAll disconnects all services.

func (*Registry) Connect

func (r *Registry) Connect(serviceName string, cfg ConnectionConfig) error

Connect creates a new connector for the given driver and connects it.

func (*Registry) Disconnect

func (r *Registry) Disconnect(serviceName string) error

Disconnect removes and disconnects a service.

func (*Registry) Get

func (r *Registry) Get(serviceName string) (Connector, error)

Get returns the connector for a service.

func (*Registry) ListServices

func (r *Registry) ListServices() []string

ListServices returns active service names.

func (*Registry) RegisterDriver

func (r *Registry) RegisterDriver(driver string, factory Factory)

RegisterDriver registers a connector factory for a driver type.

type SchemaChange

type SchemaChange struct {
	Type       string // "add_column", "drop_column", "rename_column", "modify_column"
	Column     string
	NewName    string        // for rename
	Definition *model.Column // for add/modify
}

SchemaChange represents a table alteration.

type SelectRequest

type SelectRequest struct {
	Table      string
	Fields     []string
	Filter     string
	FilterArgs []interface{}
	Order      string
	Limit      int
	Offset     int
	Cursor     string
}

SelectRequest represents a query for records.

type UpdateRequest

type UpdateRequest struct {
	Table      string
	Filter     string
	FilterArgs []interface{}
	Record     map[string]interface{}
	IDs        []interface{} // for updating by primary key
}

UpdateRequest represents an update operation.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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