oracle

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConnection

func NewConnection(cfg *config.DatabaseConfig, log logger.Logger) (types.Interface, error)

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) Begin

func (c *Connection) Begin(ctx context.Context) (types.Tx, error)

Begin starts a transaction

func (*Connection) BeginTx

func (c *Connection) BeginTx(ctx context.Context, opts *sql.TxOptions) (types.Tx, error)

BeginTx starts a transaction with options

func (*Connection) Close

func (c *Connection) Close() error

Close closes the database connection

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) Exec

func (c *Connection) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows

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) Prepare

func (c *Connection) Prepare(ctx context.Context, query string) (types.Statement, error)

Prepare creates a prepared statement for later queries or executions

func (*Connection) Query

func (c *Connection) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows

func (*Connection) QueryRow

func (c *Connection) QueryRow(ctx context.Context, query string, args ...any) types.Row

QueryRow executes a query that returns at most one row

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

func (*Connection) Stats

func (c *Connection) Stats() (map[string]any, error)

Stats returns database connection statistics

type Statement added in v0.2.0

type Statement struct {
	// contains filtered or unexported fields
}

Statement wraps sql.Stmt to implement types.Statement

func (*Statement) Close added in v0.2.0

func (s *Statement) Close() error

Close closes the prepared statement

func (*Statement) Exec added in v0.2.0

func (s *Statement) Exec(ctx context.Context, args ...any) (sql.Result, error)

Exec executes a prepared statement with arguments

func (*Statement) Query added in v0.2.0

func (s *Statement) Query(ctx context.Context, args ...any) (*sql.Rows, error)

Query executes a prepared query with arguments

func (*Statement) QueryRow added in v0.2.0

func (s *Statement) QueryRow(ctx context.Context, args ...any) types.Row

QueryRow executes a prepared query that returns a single row

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

func (t *Transaction) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a query without returning rows within the transaction

func (*Transaction) Prepare added in v0.2.0

func (t *Transaction) Prepare(ctx context.Context, query string) (types.Statement, error)

Prepare creates a prepared statement within the transaction

func (*Transaction) Query added in v0.2.0

func (t *Transaction) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query within the transaction

func (*Transaction) QueryRow added in v0.2.0

func (t *Transaction) QueryRow(ctx context.Context, query string, args ...any) types.Row

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL