Documentation
¶
Index ¶
- func NewConnection(cfg *config.DatabaseConfig, log logger.Logger) (types.Interface, error)
- type Connection
- func (c *Connection) CreateMigrationTable(ctx context.Context) error
- func (c *Connection) DatabaseType() string
- func (c *Connection) MigrationTable() string
- func (c *Connection) RegisterType(typeName, arrayTypeName string, typeObj any) error
- func (c *Connection) RegisterTypeWithOwner(owner, typeName, arrayTypeName string, typeObj any) error
- type Statement
- type Transaction
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. When cfg.Timezone is set (non-empty and not "-"), every new physical connection runs ALTER SESSION SET TIME_ZONE before being handed to the pool, guaranteeing pool-wide consistency.
Types ¶
type Connection ¶
type Connection struct {
*wrapper.Connection
}
Connection implements the types.Interface for Oracle. Embeds the vendor-agnostic wrapper.Connection which carries the byte-identical delegation methods (Query, Exec, Prepare, Begin, Health, Stats, Close, etc.) — see database/internal/wrapper. Oracle-only methods (DatabaseType, MigrationTable, CreateMigrationTable PL/SQL block) stay defined here.
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) 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
Oracle re-exports the vendor-agnostic wrappers; see database/internal/wrapper.
type Transaction ¶ added in v0.2.0
type Transaction = wrapper.Transaction