Skip to content

cubrid-lab/gorm-cubrid

GORM CUBRID Driver

Go Reference v0.1.0 Go 1.21+ ci workflow license GitHub stars

CUBRID database driver for GORM.

flowchart TD
    A[Application] --> B[GORM]
    B --> C[gorm-cubrid Dialector]
    C --> D[cubrid-go database/sql]
    D --> E[CUBRID Server]
Loading

Requirements

  • Go 1.21+
  • CUBRID 10.2+ database server

Installation

go get github.com/cubrid-labs/gorm-cubrid

The CUBRID Go SQL driver must also be installed:

go get github.com/cubrid-labs/cubrid-go

cubrid-go is a pure Go driver — no CGO or native libraries required.

Usage

package main

import (
    _ "github.com/cubrid-labs/cubrid-go" // register CUBRID SQL driver

    cubrid "github.com/cubrid-labs/gorm-cubrid"
    "gorm.io/gorm"
)

func main() {
    // DSN format: cubrid://[user[:password]]@host[:port]/database[?autocommit=true&timeout=30s]
    dsn := "cubrid://public:@localhost:33000/demodb"

    db, err := gorm.Open(cubrid.Open(dsn), &gorm.Config{})
    if err != nil {
        panic(err)
    }

    // AutoMigrate example
    type Product struct {
        gorm.Model
        Name  string
        Price float64
    }
    db.AutoMigrate(&Product{})
}

Advanced Configuration

db, err := gorm.Open(cubrid.New(cubrid.Config{
    DSN:               "cubrid://public:@localhost:33000/demodb",
    DefaultStringSize: 512, // default VARCHAR size (default: 256)
}), &gorm.Config{})

Using an Existing Connection

import "database/sql"

sqlDB, err := sql.Open("cubrid", "cubrid://public:@localhost:33000/demodb")
if err != nil {
    panic(err)
}

db, err := gorm.Open(cubrid.New(cubrid.Config{Conn: sqlDB}), &gorm.Config{})

Data Type Mapping

Go / GORM Type CUBRID Type
bool TINYINT(1)
int8, uint8 TINYINT
int16, uint16 SMALLINT
int32, uint32 INT
int64, uint64 BIGINT
float32 FLOAT
float64 DOUBLE
float with precision NUMERIC(p, s)
string (size < 65536) VARCHAR(n)
string (size ≥ 65536) CLOB
time.Time DATETIME
[]byte BLOB

DSN Format

cubrid://[user[:password]]@host[:port]/database[?autocommit=true&timeout=30s]
Field Description Example
host CUBRID server hostname or IP (required) localhost
port CUBRID broker port (default: 33000) 33000
database Database name (required) demodb
user Database user (optional) public
password User password (optional) ``

Example:

cubrid://dba:password@localhost:33000/demodb

Supported Features

  • AutoMigrate — create and alter tables automatically
  • CreateTable / DropTable
  • HasTable / HasColumn / HasIndex
  • GetTables / ColumnTypes
  • Standard GORM CRUD operations
  • Associations (HasOne, HasMany, BelongsTo, ManyToMany)
  • Transactions
  • Hooks

AutoMigrate with CUBRID

Basic usage:

type User struct {
    ID   uint
    Name string
}

type Product struct {
    ID    uint
    Name  string
    Price float64
}

if err := db.AutoMigrate(&User{}, &Product{}); err != nil {
    panic(err)
}

Best practices and CUBRID-specific notes:

  • Treat bool as an integer-backed type (SMALLINT semantics; emitted as TINYINT(1)), because CUBRID has no native BOOLEAN type.
  • CUBRID has no native JSON type. Store JSON payloads as VARCHAR/CLOB and validate in the application layer.
  • CUBRID DDL statements auto-commit. Schema changes from AutoMigrate cannot be rolled back inside a transaction.
  • Keep index definitions explicit (gorm:"index", gorm:"uniqueIndex") and review generated indexes for large tables before production rollout.
  • AutoMigrate handles table creation and additive changes well, but complex column type changes and some ALTER TABLE patterns may require manual SQL.
  • For production-critical systems, prefer versioned/manual migration files (or a dedicated migration tool) for deterministic review and rollback planning.

Soft Delete Support

GORM soft delete works with CUBRID when a model includes gorm.DeletedAt.

import "gorm.io/gorm"

type User struct {
    ID        uint
    Name      string
    DeletedAt gorm.DeletedAt `gorm:"index"`
}

// Soft delete: sets deleted_at instead of physical delete.
db.Delete(&user)

// Normal queries automatically exclude rows where deleted_at is not NULL.
var users []User
db.Find(&users)

// Include soft-deleted rows.
db.Unscoped().Find(&users)

// Permanently delete rows.
db.Unscoped().Delete(&user)

Notes:

  • This works because CUBRID supports DATETIME columns and WHERE ... IS NULL filters used by GORM soft delete.
  • Soft delete in GORM is application-level behavior (query/update conventions), not a database-native soft-delete feature.

Notes

  • CUBRID does not support unsigned integer types. uint fields are mapped to their signed equivalents of the next larger size.
  • Schema introspection (HasTable, HasColumn, ColumnTypes, etc.) requires INFORMATION_SCHEMA support, available in CUBRID 11.2+.
  • AUTO_INCREMENT columns use CUBRID's native AUTO_INCREMENT attribute.

Ecosystem

Package Description
cubrid-go database/sql driver + GORM dialector
gorm-cubrid GORM dialect for CUBRID (this package)

Roadmap

See ROADMAP.md for this project's direction and next milestones.

For the ecosystem-wide view, see the CUBRID Labs Ecosystem Roadmap and Project Board.

License

MIT