Documentation
¶
Overview ¶
Package dalgo2oracle is the Oracle Database adapter of DALgo.
It opens the server with github.com/sijms/go-ora/v3 (pure Go), composes github.com/dal-go/dalgo2sql for the dal.DB surface with the structured-query dialect "oracle", and adds:
- dbschema.SchemaReader over Oracle's ALL_ catalog views, scoped to one schema (the session's CURRENT_SCHEMA unless WithSchema names another), plus ListViews, which tells a caller which collections are views;
- read-only transactions, which go-ora refuses: see NewConnector;
- errors that never carry the connection string, the user, the host or the service: see ConnectionError.
Index ¶
- Constants
- func IsAlreadyExists(err error) bool
- func NewConnector(dsn string) driver.Connector
- type ConnectionError
- type Database
- func (d *Database) Adapter() dal.Adapter
- func (d *Database) BeginReadOnly(ctx context.Context) (*sql.Tx, error)
- 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, parent *dalrecord.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) QueryReadOnly(ctx context.Context, query string, args []any, read func(*sql.Rows) error) error
- func (d *Database) SQLDB() *sql.DB
- func (d *Database) SchemaName() string
- func (d *Database) SupportsConcurrentConnections() bool
- type FailureKind
- type Option
Constants ¶
const StructuredQueryDialect = "oracle"
StructuredQueryDialect is the dalgo2sql dialect the adapter sets.
const Version = "0.1.0"
Version is the adapter version reported by Database.Adapter.
Variables ¶
This section is empty.
Functions ¶
func IsAlreadyExists ¶
IsAlreadyExists reports whether err is a unique-constraint violation (ORA-00001).
func NewConnector ¶
NewConnector returns a driver.Connector for dsn (oracle://user:pass@host:port/service) that a caller can open with sql.OpenDB for native SQL. It differs from go-ora's own connector in two ways:
- A transaction begun with ReadOnly (sql.TxOptions{ReadOnly: true}) is accepted: go-ora refuses one, so it is begun as a plain transaction whose first statement is SET TRANSACTION READ ONLY. Oracle then refuses every write in it (ORA-01456).
- Errors never carry the connection string or the server's address: a failure to connect is a *ConnectionError, and so is a transport failure later on (see ConnectionError).
It also turns go-ora's "fast login" off unless dsn sets it: with it on, go-ora v3.0.1 caches a negotiation cookie per service, and against Oracle 23ai Free every later connection of the process then ends in EOF, so a pool cannot open a second connection.
Types ¶
type ConnectionError ¶
type ConnectionError struct {
// Kind is what failed.
Kind FailureKind
// Code is the ORA number of a server error, for example 1017 for ORA-01017;
// zero when the server sent none.
Code int
// contains filtered or unexported fields
}
ConnectionError is the error a connection that fails is reported as: by NewDatabase when the connection to Oracle cannot be opened or verified, and by any later call whose connection fails (the pool connecting again, a socket that is reset or times out).
Its text is built only from a fixed sentence chosen by Kind and the ORA number in Code. Nothing of the connection string, and nothing of the driver's or the server's own message, is ever copied into it: go-ora and the listener put the user, host, port and service name into theirs, and callers print errors. For the same reason the driver's error is not reachable through errors.Unwrap, errors.Is or errors.As; branch on Kind and Code instead. The one thing kept of it is whether the attempt ended because its context was canceled or its deadline passed: errors.Is finds context.Canceled and context.DeadlineExceeded in such an error.
func (*ConnectionError) Error ¶
func (e *ConnectionError) Error() string
Error returns the text: the step, the fixed sentence and the ORA number only.
func (*ConnectionError) Is ¶
func (e *ConnectionError) Is(target error) bool
Is reports the two context errors of the attempt that failed, and nothing else.
type Database ¶
type Database struct {
dal.ConcurrencyAvailable
dal.DB
// contains filtered or unexported fields
}
Database is an Oracle database opened by NewDatabase. It is a dal.DB (the embedded dalgo2sql database), a dbschema.SchemaReader and an io.Closer.
func NewDatabase ¶
NewDatabase opens the Oracle database dsn names (oracle://user:pass@host:port/service), pings it, and reads the session's CURRENT_SCHEMA unless WithSchema names a schema. A failure to connect is a *ConnectionError, whose text holds nothing of dsn.
func (*Database) BeginReadOnly ¶
BeginReadOnly begins a transaction whose first statement is SET TRANSACTION READ ONLY: Oracle refuses every write in it. The caller ends it; a rollback is the usual end of a read.
func (*Database) DescribeCollection ¶
func (d *Database) DescribeCollection(ctx context.Context, ref *dal.CollectionRef) (*dbschema.CollectionDef, error)
DescribeCollection returns the fields, primary key, indexes and outgoing foreign keys of a table, view or materialized view. A column's default is a dbschema.DefaultLiteral holding the expression as Oracle stores it, except SYSDATE, SYSTIMESTAMP, CURRENT_DATE, CURRENT_TIMESTAMP and LOCALTIMESTAMP, which are dbschema.DefaultCurrentTimestamp; an identity column has AutoIncrement and no default; a virtual column's default is "GENERATED ALWAYS AS (<expression>)". A type with no dbschema counterpart (INTERVAL, XMLTYPE, JSON, ROWID, ...) is reported as a String; SourceDefinition keeps the declared Oracle types.
func (*Database) ListCollections ¶
func (d *Database) ListCollections(ctx context.Context, parent *dalrecord.Key) ([]dal.CollectionRef, error)
ListCollections returns the tables, views and materialized views of the schema in alphabetical order, under the names Oracle stores. The parent key is ignored.
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, typed "primary-key", "unique", "foreign-key" and "check". The check constraints Oracle records for NOT NULL columns are left out: DescribeCollection reports them as non-nullable fields.
func (*Database) ListIndexes ¶
func (d *Database) ListIndexes(ctx context.Context, ref *dal.CollectionRef) ([]dbschema.IndexDef, error)
ListIndexes returns the indexes of a table but the one that backs its primary key: the indexes of unique constraints are unique indexes. A column of a function-based index is named by its expression; a descending column, which Oracle stores as the expression "NAME", by its name.
func (*Database) ListReferrers ¶
func (d *Database) ListReferrers(ctx context.Context, ref *dal.CollectionRef) ([]dbschema.Referrer, error)
ListReferrers returns the tables of the schema whose foreign keys reference the table, each with its referencing columns.
func (*Database) ListViews ¶
ListViews returns the collections of the schema that are views or materialized views, by the references ListCollections gives them. It is the method a schema consumer can look for to tell a view from a table.
func (*Database) QueryReadOnly ¶
func (d *Database) QueryReadOnly(ctx context.Context, query string, args []any, read func(*sql.Rows) error) error
QueryReadOnly runs one native SQL query in a read-only transaction, hands its rows to read, and rolls the transaction back. The error is the first of the begin, the query, read and the rows' own.
func (*Database) SQLDB ¶
SQLDB returns the connection pool, for native SQL. A transaction begun on it with sql.TxOptions{ReadOnly: true} is read-only (see NewConnector).
func (*Database) SchemaName ¶
SchemaName returns the schema (owner) the schema reader inspects.
func (*Database) SupportsConcurrentConnections ¶
SupportsConcurrentConnections reports true: Oracle serves many sessions. The explicit method resolves the ambiguity between the two embedded ones.
type FailureKind ¶
type FailureKind int
FailureKind says what kind of failure a ConnectionError reports. It decides the fixed sentence the error's text uses.
const ( // FailureOther is any failure the kinds below do not describe. It is the zero // value. FailureOther FailureKind = iota // FailureInvalidDSN: the driver cannot parse the connection string. FailureInvalidDSN // FailureNetwork: the server could not be reached, or the connection to it // was lost (name resolution, a refused or reset connection, the end of the // stream). FailureNetwork // FailureTimeout: the attempt timed out or was canceled. FailureTimeout // FailureAuthentication: the server refused the credentials or the account // (ORA-01017, ORA-28000, ORA-28001, ORA-01045). FailureAuthentication // FailureService: the listener does not know the requested service or SID // (ORA-12514, ORA-12505). FailureService // FailureServer: the server answered with another error. [ConnectionError.Code] // holds its ORA number. FailureServer )
func (FailureKind) String ¶
func (k FailureKind) String() string
String returns the fixed sentence of the kind.
type Option ¶
type Option func(*options)
Option configures NewDatabase.
func WithDbOptions ¶
WithDbOptions sets the dalgo2sql options of the dal.DB surface (recordsets, exact numeric values, ...). StructuredQueryDialect is always "oracle", and IsAlreadyExists defaults to IsAlreadyExists.
func WithSchema ¶
WithSchema scopes the schema reader to the named schema (owner), spelled as Oracle stores it (an unquoted name is stored in upper case). Without it the session's CURRENT_SCHEMA is read.