Documentation
¶
Overview ¶
Package dalgo2mssql is the Microsoft SQL Server adapter of DALgo: it reads a SQL Server schema and runs structured queries.
NewDatabase opens the server with github.com/microsoft/go-mssqldb (driver name "sqlserver", DSN form sqlserver://user:pass@host:1433?database=shop), pings it, and wraps the *sql.DB with dalgo2sql.NewDatabase under the "sqlserver" structured-query dialect, so a *Database is a dal.DB. It also is a dbschema.SchemaReader and an io.Closer, and it lists views (ListViews), which lets a schema browser tell a view from a table.
Scope ¶
A Database reads one schema: the login's default schema (SCHEMA_NAME(), usually dbo) unless WithSchema names another. It reads user tables and views; objects SQL Server ships (is_ms_shipped) are left out. A collection reference that names a schema of its own (dal.NewQualifiedRootCollectionRef) is read in that schema.
Read-only ¶
SQL Server has no read-only session and no read-only transaction, and this package does not pretend to have one: nothing here stops a statement sent through the embedded dal.DB from writing, and a transaction begun read-only (RunReadonlyTransaction) fails with ErrReadOnlyTransaction. Safety comes from outside this package: the caller sends only SELECT text, and the login in the DSN should be a read-only one (for example a user that is only a member of db_datareader and has VIEW DEFINITION for the schema scan).
The key read/write surface of the embedded dal.DB (Get, Set, Insert, Delete) writes "?" parameter markers, which the "sqlserver" driver does not accept; structured reads compiled by dalgo2sql's "sqlserver" dialect use @p1, @p2, … and are the supported way to read data. A dalgo2sql release older than the one that adds the "sqlserver" dialect refuses structured reads with its unsupported-dialect error; the schema reader does not depend on it.
Errors ¶
No error of this package carries the DSN, the password, the user or the host. The pool is opened over NewConnector, which sends every error of the driver through one function, so this holds for the query, read and transaction methods of the embedded dal.DB too. A failure to open, ping or resolve the schema is a *ConnectionError; so is a failed reconnect, a lost connection or a login or database error on a later call. A catalog query the server rejects is a *CatalogError with the server's error number; another error of the driver is a *DriverError. Each has fixed text, and the driver's error stays reachable through Unwrap for errors.Is and errors.As. An error the server returns for a statement the caller sent is go-mssqldb's mssql.Error, unchanged: its message describes the statement and can name its objects, including their database.
Index ¶
- Constants
- Variables
- func NewConnector(dsn string) (driver.Connector, error)
- type CatalogError
- type ConnectionError
- type ConnectionErrorKind
- type Database
- func (d *Database) Adapter() dal.Adapter
- func (d *Database) Close() error
- func (d *Database) DescribeCollection(ctx context.Context, ref *dal.CollectionRef) (*dbschema.CollectionDef, error)
- func (d *Database) ListCollections(ctx context.Context, _ *record.Key) ([]dal.CollectionRef, error)
- func (d *Database) ListConstraints(ctx context.Context, ref *dal.CollectionRef) ([]dbschema.ConstraintDef, error)
- func (d *Database) ListIndexes(ctx context.Context, ref *dal.CollectionRef) ([]dbschema.IndexDef, error)
- func (d *Database) ListReferrers(ctx context.Context, ref *dal.CollectionRef) ([]dbschema.Referrer, error)
- func (d *Database) ListViews(ctx context.Context) ([]dal.CollectionRef, error)
- func (d *Database) SchemaName() string
- type DriverError
- type Option
Constants ¶
const DriverName = "sqlserver"
DriverName is the database/sql driver name go-mssqldb registers. The package itself opens the pool with sql.OpenDB over NewConnector, so that every error of the driver is sanitised.
const StructuredQueryDialect = "sqlserver"
StructuredQueryDialect is the dalgo2sql structured-query dialect the package sets on the dalgo2sql options.
const Version = "0.1.0"
Version is the dalgo2mssql adapter version, reported by Adapter.
Variables ¶
var ErrCollectionNotFound = errors.New("collection not found")
ErrCollectionNotFound is matched (errors.Is) by the error a reader method returns for a table or view that is not in the schema.
var ErrReadOnlyTransaction = errors.New("dalgo2mssql: SQL Server has no read-only transactions; use a read-only login")
ErrReadOnlyTransaction is returned when a transaction is begun read-only (sql.TxOptions{ReadOnly: true}, dal's RunReadonlyTransaction): SQL Server has no read-only transaction, and this package does not fake one.
var ErrSchemaNotFound = errors.New("schema not found")
ErrSchemaNotFound is matched (errors.Is) by the error NewDatabase returns when the schema named with WithSchema does not exist.
Functions ¶
func NewConnector ¶
NewConnector returns a driver.Connector for dsn (sqlserver://user:pass@host:1433?database=shop) that a caller can open with sql.OpenDB. It is go-mssqldb's connector with every error it returns sanitised (see the package documentation, Errors):
- a failure to connect, a login or database error the server returns (18456, 4060 and the like) and a failure of the transport are a *ConnectionError;
- an error the server returns for a statement is go-mssqldb's own mssql.Error, unchanged;
- any other error of the driver is a *DriverError;
- database/sql's driver.ErrBadConn and the context errors are returned bare, so database/sql still retries on a bad connection.
A transaction begun read-only fails with ErrReadOnlyTransaction.
A DSN the driver refuses is a *ConnectionError of kind KindOpen.
Types ¶
type CatalogError ¶
CatalogError reports a catalog query that SQL Server rejected (for example for lack of VIEW DEFINITION permission). Its text holds the call and the server's error number only: the server's message can name the database. The driver's error is reachable only through Unwrap.
func (*CatalogError) Error ¶
func (e *CatalogError) Error() string
func (*CatalogError) Unwrap ¶
func (e *CatalogError) Unwrap() error
Unwrap returns the driver's error.
type ConnectionError ¶
type ConnectionError struct {
Kind ConnectionErrorKind
// Op is the call that failed, for example "ListCollections"; empty for the
// steps of NewDatabase.
Op string
// contains filtered or unexported fields
}
ConnectionError reports a failure to reach SQL Server. Its text is fixed by Kind (and Op, a name of this package's own call); it never holds the DSN, the user, the password, the host, the database name or the driver's message. The driver's error is reachable only through Unwrap.
func (*ConnectionError) Error ¶
func (e *ConnectionError) Error() string
func (*ConnectionError) Unwrap ¶
func (e *ConnectionError) Unwrap() error
Unwrap returns the driver's error.
type ConnectionErrorKind ¶
type ConnectionErrorKind string
ConnectionErrorKind says which step of reaching the server failed. It decides the fixed text of a ConnectionError.
const ( // KindOpen: the driver refused the DSN (sql.Open failed). KindOpen ConnectionErrorKind = "open" // KindPing: the server could not be reached, refused the login, or the ping // timed out. KindPing ConnectionErrorKind = "ping" // KindSchema: the schema to read could not be resolved after connecting. KindSchema ConnectionErrorKind = "schema" // KindConnection: a later call failed with something other than an error // the server returned for its statement: a lost connection, a refused // login on reconnect, a timeout or a canceled context. KindConnection ConnectionErrorKind = "connection" )
type Database ¶
Database is a SQL Server database: a dal.DB (the embedded dalgo2sql database) that also reads the schema of one SQL Server schema.
func NewDatabase ¶
NewDatabase opens the SQL Server named by dsn (sqlserver://… form), pings it with ctx and resolves the schema to read. Failures are *ConnectionError values whose text never holds any part of dsn; a schema named with WithSchema that does not exist matches ErrSchemaNotFound.
func (*Database) DescribeCollection ¶
func (d *Database) DescribeCollection(ctx context.Context, ref *dal.CollectionRef) (*dbschema.CollectionDef, error)
DescribeCollection returns the columns (portable type, length, precision, nullability, default expression text, identity), the primary key in key order, the indexes and the foreign keys of a table or view.
func (*Database) ListCollections ¶
ListCollections returns the user tables and views of the schema, by name. parent is ignored: a schema is flat.
func (*Database) ListConstraints ¶
func (d *Database) ListConstraints(ctx context.Context, ref *dal.CollectionRef) ([]dbschema.ConstraintDef, error)
ListConstraints returns the PRIMARY KEY, UNIQUE, FOREIGN KEY and CHECK constraints of a table, by name within type (types "check", "foreign-key", "primary-key", "unique"). A view has none.
func (*Database) ListIndexes ¶
func (d *Database) ListIndexes(ctx context.Context, ref *dal.CollectionRef) ([]dbschema.IndexDef, error)
ListIndexes returns the indexes of a table or view other than its primary key, each with its key columns in key order (included columns are left out). The index behind a UNIQUE constraint is listed under the constraint's name, unique.
func (*Database) ListReferrers ¶
func (d *Database) ListReferrers(ctx context.Context, ref *dal.CollectionRef) ([]dbschema.Referrer, error)
ListReferrers returns one entry per foreign key that references the table, naming the referencing table and its columns in key order. Only referencing tables of the same schema are listed: a reference carries no schema.
func (*Database) ListViews ¶
ListViews returns the views of the schema, by name; every one of them is also among what ListCollections returns. It lets a schema browser tell a view from a table.
func (*Database) SchemaName ¶
SchemaName returns the SQL Server schema the reader reads.
type DriverError ¶
type DriverError struct {
Op string
// contains filtered or unexported fields
}
DriverError reports an error of the SQL Server driver that is neither a server's error for a statement nor a failure of the connection, for example a malformed reply. Its text holds the call only: the driver's message can name the server. The driver's error is reachable only through Unwrap.
func (*DriverError) Error ¶
func (e *DriverError) Error() string
func (*DriverError) Unwrap ¶
func (e *DriverError) Unwrap() error
Unwrap returns the driver's error.
type Option ¶
type Option func(*options)
Option configures NewDatabase.
func WithDalSchema ¶
WithDalSchema sets the dal.Schema of the embedded dal.DB (an empty one by default).
func WithDbOptions ¶
WithDbOptions sets the dalgo2sql options of the embedded dal.DB. Its StructuredQueryDialect is always replaced with "sqlserver".
func WithSchema ¶
WithSchema names the SQL Server schema to read, instead of the default schema of the login (SCHEMA_NAME()).