Documentation
¶
Index ¶
- Constants
- Variables
- func ValidateDatabaseName(name string) error
- func ValidateQuery(text string) error
- type ConfigProvider
- type DataEngine
- type Database
- type Endpoint
- type Engine
- type Handler
- func (h *Handler) ExtractOperation(*echo.Context) string
- func (h *Handler) ExtractResource(*echo.Context) string
- func (h *Handler) GetSupportedOperations() []string
- func (h *Handler) Handler() echo.HandlerFunc
- func (h *Handler) MatchPriority() int
- func (h *Handler) Name() string
- func (h *Handler) Reset()
- func (h *Handler) Restore(ctx context.Context, data []byte) error
- func (h *Handler) RouteMatcher() service.Matcher
- func (h *Handler) Shutdown(context.Context)
- func (h *Handler) Snapshot(ctx context.Context) []byte
- type InMemoryBackend
- func (b *InMemoryBackend) Close()
- func (b *InMemoryBackend) Databases(id string) ([]string, error)
- func (b *InMemoryBackend) Delete(id string) error
- func (b *InMemoryBackend) DropDatabase(id, name string) error
- func (b *InMemoryBackend) Endpoint(id string) (Endpoint, bool)
- func (b *InMemoryBackend) Ensure(id string, spec Spec) (Endpoint, error)
- func (b *InMemoryBackend) EnsureDatabase(id, name, collation string) error
- func (b *InMemoryBackend) Query(ctx context.Context, id, database, text string) (QueryResult, error)
- func (b *InMemoryBackend) Reset()
- func (b *InMemoryBackend) Restore(ctx context.Context, data []byte) error
- func (b *InMemoryBackend) ServerIDByName(name string) (string, bool)
- func (b *InMemoryBackend) SetLauncher(l Launcher, startup time.Duration)
- func (b *InMemoryBackend) Snapshot(ctx context.Context) []byte
- type LaunchSpec
- type Launcher
- type Provider
- type Querier
- type QueryResult
- type Settings
- type Spec
Constants ¶
const ( MaxQueryLen = 4000 MaxQueryRows = 500 MaxCellLen = 1000 QueryDeadline = 20 * time.Second )
Query limits.
const ( // EngineAuto launches SQL Server containers when a container runtime is reachable. EngineAuto = "auto" // EngineNone never starts containers: servers are state-only. EngineNone = "none" // DefaultImage is the SQL Server image servers run. DefaultImage = "mcr.microsoft.com/mssql/server:2022-CU26-ubuntu-22.04" // DefaultStartupTimeout bounds how long a server container may take to accept connections. DefaultStartupTimeout = 5 * time.Minute )
Variables ¶
var ( // ErrNotFound is returned for an unknown server. ErrNotFound = errors.New("azuresql: server not found") // ErrInvalid is returned for a malformed request. ErrInvalid = errors.New("azuresql: invalid request") // runs state-only. ErrRuntimeUnavailable = errors.New("azuresql: container runtime unavailable") // ErrNilAppContext is returned when Init is called with a nil AppContext. ErrNilAppContext = errors.New("azuresql: nil app context") )
var ErrNoEngine = fmt.Errorf("%w: server has no SQL engine (state-only)", ErrInvalid)
ErrNoEngine is returned when the server has no SQL engine (no container runtime) to run a query.
Functions ¶
func ValidateDatabaseName ¶
ValidateDatabaseName accepts a plain database name.
func ValidateQuery ¶
ValidateQuery accepts one plain SELECT statement: no comments, no batch separators and none of the keywords that write, execute or reach outside the database.
Types ¶
type ConfigProvider ¶
type ConfigProvider interface {
GetAzureSQLSettings() Settings
}
ConfigProvider extracts the Azure SQL settings from the AppContext config.
type DataEngine ¶
type DataEngine interface {
Engine
// BackupDatabase returns a backup of the database; it fails when the backup would exceed maxBytes.
BackupDatabase(ctx context.Context, name string, maxBytes int64) ([]byte, error)
// RestoreDatabase replaces the database with the content of a backup taken by BackupDatabase.
RestoreDatabase(ctx context.Context, name string, data []byte) error
}
DataEngine is an Engine that can export and import the content of a database, so snapshots keep engine data.
type Endpoint ¶
Endpoint is where a server's TDS port listens. Port is 0 when the server is state-only.
type Engine ¶
type Engine interface {
// Endpoint is the host and published TDS port clients connect to.
Endpoint() (string, int)
// SetLogin creates the server-admin login, or resets its password, with sysadmin rights.
SetLogin(ctx context.Context, login, password string) error
CreateDatabase(ctx context.Context, name, collation string) error
DropDatabase(ctx context.Context, name string) error
ListDatabases(ctx context.Context) ([]string, error)
Close(ctx context.Context)
}
Engine is one running SQL Server. Methods are safe for sequential use per server.
type Handler ¶
type Handler struct {
Backend *InMemoryBackend
// contains filtered or unexported fields
}
Handler owns the SQL servers. Azure SQL has no HTTP data plane: ARM manages servers and clients speak TDS to each server's own port, so the handler never matches a request.
func (*Handler) ExtractOperation ¶
ExtractOperation is unused: requests never reach the shared router.
func (*Handler) ExtractResource ¶
ExtractResource is unused: requests never reach the shared router.
func (*Handler) GetSupportedOperations ¶
GetSupportedOperations lists the operations ARM's Microsoft.Sql provider drives.
func (*Handler) Handler ¶
func (h *Handler) Handler() echo.HandlerFunc
Handler returns a 404 handler.
func (*Handler) MatchPriority ¶
MatchPriority is irrelevant because RouteMatcher never matches.
func (*Handler) RouteMatcher ¶
RouteMatcher never matches.
type InMemoryBackend ¶
type InMemoryBackend struct {
// contains filtered or unexported fields
}
InMemoryBackend runs one SQL Server container per logical server, or tracks the server state-only when no container runtime is reachable. mu guards the map; life serialises lifecycle calls so a slow container start does not block reads.
func NewInMemoryBackend ¶
func NewInMemoryBackend() *InMemoryBackend
NewInMemoryBackend creates an empty backend with no launcher: servers are state-only.
func (*InMemoryBackend) Databases ¶
func (b *InMemoryBackend) Databases(id string) ([]string, error)
Databases lists the user databases the engine holds, or the tracked ones when the server is state-only.
func (*InMemoryBackend) Delete ¶
func (b *InMemoryBackend) Delete(id string) error
Delete stops the server and releases its port; a missing server is ErrNotFound.
func (*InMemoryBackend) DropDatabase ¶
func (b *InMemoryBackend) DropDatabase(id, name string) error
DropDatabase drops the database from the engine; a missing database is not an error.
func (*InMemoryBackend) Endpoint ¶
func (b *InMemoryBackend) Endpoint(id string) (Endpoint, bool)
Endpoint reports where the server's TDS port listens; ok is false for an unknown server.
func (*InMemoryBackend) Ensure ¶
func (b *InMemoryBackend) Ensure(id string, spec Spec) (Endpoint, error)
Ensure creates the server's engine, or applies a changed admin login or password to an existing one.
func (*InMemoryBackend) EnsureDatabase ¶
func (b *InMemoryBackend) EnsureDatabase(id, name, collation string) error
EnsureDatabase creates the database in the engine; an existing database is left as it is.
func (*InMemoryBackend) Query ¶
func (b *InMemoryBackend) Query(ctx context.Context, id, database, text string) (QueryResult, error)
Query runs a validated read-only SELECT on database and returns at most MaxQueryRows rows.
func (*InMemoryBackend) Restore ¶
func (b *InMemoryBackend) Restore(ctx context.Context, data []byte) error
Restore loads a snapshot, starting every server again on a fresh port.
func (*InMemoryBackend) ServerIDByName ¶
func (b *InMemoryBackend) ServerIDByName(name string) (string, bool)
ServerIDByName returns the id of the server whose resource name is name.
func (*InMemoryBackend) SetLauncher ¶
func (b *InMemoryBackend) SetLauncher(l Launcher, startup time.Duration)
SetLauncher makes new servers start engines through l.
func (*InMemoryBackend) Snapshot ¶
func (b *InMemoryBackend) Snapshot(ctx context.Context) []byte
Snapshot serialises every server's spec and database list, plus a backup of each database's content when the engine can export one; a database whose backup fails or exceeds the size caps is saved without data.
type LaunchSpec ¶
LaunchSpec describes the engine of one logical server.
type Launcher ¶
type Launcher interface {
Launch(ctx context.Context, spec LaunchSpec) (Engine, error)
}
Launcher starts engines.
type Provider ¶
type Provider struct{}
Provider implements service.Provider for Azure SQL Database.
func (*Provider) Init ¶
func (p *Provider) Init(ctx *service.AppContext) (service.Registerable, error)
Init builds the backend and handler.
type Querier ¶
type Querier interface {
Query(ctx context.Context, database, text string, maxRows int) (QueryResult, error)
}
Querier is implemented by engines that run read-only queries.
type QueryResult ¶
QueryResult is a capped result set; every cell is text.
type Settings ¶
type Settings struct {
Engine string `` //nolint:lll // config struct tags are intentionally verbose
/* 272-byte string literal not displayed */
Image string `` //nolint:lll // config struct tags are intentionally verbose
/* 224-byte string literal not displayed */
StartupTimeout time.Duration `` //nolint:lll // config struct tags are intentionally verbose
/* 243-byte string literal not displayed */
}
Settings is the Kong-embedded configuration of the Azure SQL Database emulator.
func DefaultSettings ¶
func DefaultSettings() Settings
DefaultSettings returns the settings used when no ConfigProvider exists.
type Spec ¶
type Spec struct {
AdminLogin string `json:"adminLogin"`
AdminPassword string `json:"adminPassword"`
}
Spec is the part of a logical server's ARM configuration the engine acts on. An empty AdminLogin (Entra-only server) creates no SQL login; an empty AdminPassword on an existing server keeps the current one.