Documentation
¶
Index ¶
- func NewConnection(cfg *config.DatabaseConfig, log logger.Logger) (types.Interface, error)
- type Connection
- func (c *Connection) Begin(ctx context.Context) (types.Tx, error)
- func (c *Connection) BeginTx(ctx context.Context, opts *sql.TxOptions) (types.Tx, error)
- func (c *Connection) Close() error
- func (c *Connection) CreateMigrationTable(ctx context.Context) error
- func (c *Connection) DatabaseType() string
- func (c *Connection) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (c *Connection) Health(ctx context.Context) error
- func (c *Connection) MigrationTable() string
- func (c *Connection) Prepare(ctx context.Context, query string) (types.Statement, error)
- func (c *Connection) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (c *Connection) QueryRow(ctx context.Context, query string, args ...any) types.Row
- func (c *Connection) RegisterType(typeName, arrayTypeName string, typeObj any) error
- func (c *Connection) RegisterTypeWithOwner(owner, typeName, arrayTypeName string, typeObj any) error
- func (c *Connection) Stats() (map[string]any, error)
- type Statement
- type Transaction
- func (t *Transaction) Commit(_ context.Context) error
- func (t *Transaction) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (t *Transaction) Prepare(ctx context.Context, query string) (types.Statement, error)
- func (t *Transaction) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (t *Transaction) QueryRow(ctx context.Context, query string, args ...any) types.Row
- func (t *Transaction) Rollback(_ context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewConnection ¶
NewConnection creates and returns an Oracle-backed types.Interface using the provided database configuration and logger. It returns an error if cfg is nil, if the connection cannot be opened, or if an initial ping to the database fails. The function uses cfg.ConnectionString when present or constructs a DSN from host/port and Oracle service/SID/database, configures the connection pool from cfg.Pool, verifies connectivity with a 10-second timeout, and logs connection details. When cfg.Pool.KeepAlive.Enabled is true, a custom TCP dialer is used to enable keep-alive probes.
Types ¶
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection implements the types.Interface for Oracle
func (*Connection) CreateMigrationTable ¶
func (c *Connection) CreateMigrationTable(ctx context.Context) error
CreateMigrationTable creates the migration table if it doesn't exist
func (*Connection) DatabaseType ¶
func (c *Connection) DatabaseType() string
DatabaseType returns the database type
func (*Connection) Health ¶
func (c *Connection) Health(ctx context.Context) error
Health checks database connectivity
func (*Connection) MigrationTable ¶ added in v0.19.0
func (c *Connection) MigrationTable() string
MigrationTable returns the migration table name for Oracle
func (*Connection) RegisterType ¶ added in v0.16.0
func (c *Connection) RegisterType(typeName, arrayTypeName string, typeObj any) error
RegisterType registers an Oracle User-Defined Type (UDT) for use with stored procedures, functions, and queries that return custom object types.
IMPORTANT: This is for custom types created with CREATE TYPE, NOT for Oracle SEQUENCE objects. Sequences work automatically without registration:
// Oracle SEQUENCE - works without UDT registration var nextID int64 err := conn.QueryRow(ctx, "SELECT SEQ_ID_TABLE.NEXTVAL FROM DUAL").Scan(&nextID)
PRIMARY USE CASE: Collection Types for Bulk Operations
Register object and collection types for efficient bulk operations:
type Product struct {
ID int64 `udt:"ID"`
Name string `udt:"NAME"`
Price float64 `udt:"PRICE"`
}
// Register collection type
conn.RegisterType("PRODUCT_TYPE", "PRODUCT_TABLE", Product{})
// Use in bulk insert
products := []Product{
{ID: 1, Name: "Widget", Price: 19.99},
{ID: 2, Name: "Gadget", Price: 29.99},
}
_, err := conn.Exec(ctx, "BEGIN bulk_insert_products(:1); END;", products)
Parameters:
- typeName: Oracle object type name (e.g., "PRODUCT_TYPE")
- arrayTypeName: Oracle collection type name (e.g., "PRODUCT_TABLE" for TABLE OF) Use empty string "" for single object types only
- typeObj: Go struct instance with udt:"FIELD_NAME" tags matching Oracle type
Oracle type definition example:
CREATE TYPE PRODUCT_TYPE AS OBJECT (
ID NUMBER,
NAME VARCHAR2(100),
PRICE NUMBER(10,2)
);
CREATE TYPE PRODUCT_TABLE AS TABLE OF PRODUCT_TYPE;
The Go struct must use udt tags matching Oracle field names (case-sensitive):
type OrderItem struct {
ItemID int64 `udt:"ITEM_ID"` // Matches Oracle: ITEM_ID
Quantity int `udt:"QUANTITY"` // Matches Oracle: QUANTITY
UnitPrice float64 `udt:"UNIT_PRICE"` // Matches Oracle: UNIT_PRICE
}
Registration must occur before any queries or procedures use the type. Best practice: register all UDTs during application initialization.
func (*Connection) RegisterTypeWithOwner ¶ added in v0.16.0
func (c *Connection) RegisterTypeWithOwner(owner, typeName, arrayTypeName string, typeObj any) error
RegisterTypeWithOwner registers an Oracle User-Defined Type with schema owner.
Use this when UDTs are defined in a specific schema (not the current user's schema).
type Customer struct {
CustomerID int `udt:"CUSTOMER_ID"`
Name string `udt:"NAME"`
}
// Register collection type with schema owner
conn.RegisterTypeWithOwner("SHARED_SCHEMA", "CUSTOMER_TYPE", "CUSTOMER_TABLE", Customer{})
// Use in bulk operations
customers := []Customer{{CustomerID: 1, Name: "ACME"}, ...}
_, err := conn.Exec(ctx, "BEGIN SHARED_SCHEMA.process_customers(:1); END;", customers)
Parameters:
- owner: Schema owner (e.g., "MYSCHEMA", case-sensitive, typically uppercase)
- typeName: Oracle object type name
- arrayTypeName: Oracle collection type name (use "" for single objects)
- typeObj: Go struct instance with udt:"FIELD_NAME" tags
type Statement ¶ added in v0.2.0
type Statement struct {
// contains filtered or unexported fields
}
Statement wraps sql.Stmt to implement types.Statement
type Transaction ¶ added in v0.2.0
type Transaction struct {
// contains filtered or unexported fields
}
Transaction wraps sql.Tx to implement types.Tx
func (*Transaction) Commit ¶ added in v0.2.0
func (t *Transaction) Commit(_ context.Context) error
Commit commits the transaction Note: Oracle's sql.Tx.Commit doesn't accept context; it's atomic and non-cancellable. The context parameter maintains interface consistency for databases that support it.
func (*Transaction) Exec ¶ added in v0.2.0
Exec executes a query without returning rows within the transaction
func (*Transaction) Prepare ¶ added in v0.2.0
Prepare creates a prepared statement within the transaction
func (*Transaction) QueryRow ¶ added in v0.2.0
QueryRow executes a query that returns a single row within the transaction
func (*Transaction) Rollback ¶ added in v0.2.0
func (t *Transaction) Rollback(_ context.Context) error
Rollback rolls back the transaction Note: Oracle's sql.Tx.Rollback doesn't accept context; it's atomic and non-cancellable. The context parameter maintains interface consistency for databases that support it.