chorm

package module
v0.0.0-...-cc25344 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

Golang Clickhouse ORM

Playground, do not use this code yet... It works partially.

Design Choices

  • Reflection caching. Reflection is slow, so we cache it by building reflection manager that is initialized only once and then used for all operations. On this way we can avoid reflection on every insert, select, update, delete operation while still keeping the code clean and simple.

Getting Started

Installation

Todo...

Benchmarks

Insert Benchmarking
go test -benchmem -run=^$ -bench ^BenchmarkNewInsert$ github.com/0x19/go-clickhouse-orm -v

goos: linux
goarch: amd64
pkg: github.com/0x19/go-clickhouse-orm
cpu: AMD Ryzen Threadripper 3960X 24-Core Processor 
BenchmarkNewInsert
BenchmarkNewInsert-48               1194            942314 ns/op            6493 B/op         61 allocs/op
PASS
ok      github.com/0x19/go-clickhouse-orm     1.249s

Documentation

Index

Constants

View Source
const (
	TupleStr    = "Tuple("
	LenTupleStr = len(TupleStr)
	PointStr    = "Point"
)
View Source
const (
	ArrayStr          = "Array("
	LenArrayStr       = len(ArrayStr)
	ArrayTypeStr      = "Array(<type>)"
	NestedStr         = "Nested("
	LenNestedStr      = len(NestedStr)
	NestedToArrayTube = "Array(Nested("
	RingStr           = "Ring"
)
View Source
const (
	Enum8Str              = "Enum8("
	Enum8StrLen           = len(Enum8Str)
	Enum16Str             = "Enum16("
	Enum16StrLen          = len(Enum16Str)
	DateTimeStr           = "DateTime("
	DateTimeStrLen        = len(DateTimeStr)
	DateTime64Str         = "DateTime64("
	DateTime64StrLen      = len(DateTime64Str)
	DecimalStr            = "Decimal("
	DecimalStrLen         = len(DecimalStr)
	FixedStringStr        = "FixedString("
	FixedStringStrLen     = len(FixedStringStr)
	SimpleAggregateStr    = "SimpleAggregateFunction("
	SimpleAggregateStrLen = len(SimpleAggregateStr)
)
View Source
const (
	LowCardinalityStr             = "LowCardinality("
	LenLowCardinalityStr          = len(LowCardinalityStr)
	LowCardinalityTypeStr         = "LowCardinality(<type>)"
	LowCardinalityNullableStr     = "LowCardinality(Nullable("
	LenLowCardinalityNullableStr  = len(LowCardinalityNullableStr)
	LowCardinalityNullableTypeStr = "LowCardinality(Nullable(<type>))"
)
View Source
const (
	MapStr     = "Map("
	LenMapStr  = len(MapStr)
	MapTypeStr = "Map(<key>, <value>)"
)
View Source
const (
	NullableStr     = "Nullable("
	LenNullableStr  = len(NullableStr)
	NullableTypeStr = "Nullable(<type>)"
)
View Source
const MultiPolygonStr = "MultiPolygon"
View Source
const PolygonStr = "Polygon"
View Source
const (
	StringStr = "String"
)

Variables

View Source
var (
	ErrNoConfigProvided   = errors.New("configuration must be provided")
	ErrNoHostProvided     = errors.New("host must be provided")
	ErrNoPortProvided     = errors.New("port must be provided")
	ErrNoUsernameProvided = errors.New("username must be provided")
	ErrNoPasswordProvided = errors.New("password must be provided")
	ErrNoDatabaseProvided = errors.New("database must be provided")
)
View Source
var MultiPolygonMainTypeStr = []byte("Array(Array(Array(Tuple(Float64, Float64))))")
View Source
var PointMainTypeStr = []byte("Tuple(Float64, Float64)")
View Source
var PolygonMainTypeStr = []byte("Array(Array(Tuple(Float64, Float64)))")
View Source
var RingMainTypeStr = []byte("Array(Tuple(Float64, Float64))")

Functions

func ExtractEnum

func ExtractEnum(data []byte) (intToStringMap map[int16]string, stringToIntMap map[string]int16, err error)

func FilterSimpleAggregate

func FilterSimpleAggregate(chType []byte) []byte

func IsArray

func IsArray(chType []byte) bool

func IsDateTime64

func IsDateTime64(chType []byte) bool

func IsDateTimeWithParam

func IsDateTimeWithParam(chType []byte) bool

func IsDecimal

func IsDecimal(chType []byte) bool

func IsEnum16

func IsEnum16(chType []byte) bool

func IsEnum8

func IsEnum8(chType []byte) bool

func IsFixedString

func IsFixedString(chType []byte) bool

func IsLowCardinality

func IsLowCardinality(chType []byte) bool

func IsMap

func IsMap(chType []byte) bool

func IsMultiPolygon

func IsMultiPolygon(chType []byte) bool

func IsNested

func IsNested(chType []byte) bool

func IsNullable

func IsNullable(chType []byte) bool

func IsNullableLowCardinality

func IsNullableLowCardinality(chType []byte) bool

func IsPoint

func IsPoint(chType []byte) bool

func IsPolygon

func IsPolygon(chType []byte) bool

func IsRing

func IsRing(chType []byte) bool

func IsString

func IsString(chType []byte) bool

func IsTuple

func IsTuple(chType []byte) bool

func NestedToArrayType

func NestedToArrayType(chType []byte) []byte

Types

type ColumnData

type ColumnData struct {
	ChType, Name []byte
}

func SplitNameType

func SplitNameType(b []byte) (ColumnData, error)

func TypesInParentheses

func TypesInParentheses(b []byte) ([]ColumnData, error)

type Config

type Config struct {
	Host     string `json:"host" yaml:"host" env:"GCHM_HOST"`
	Port     int    `json:"port" yaml:"port" env:"GCHM_PORT"`
	Username string `json:"username" yaml:"username" env:"GCHM_USERNAME"`
	Password string `json:"password" yaml:"password" env:"GCHM_PASSWORD"`
	Database string `json:"database" yaml:"database" env:"GCHM_DATABASE"`
	SSLMode  string `json:"ssl_mode" yaml:"ssl_mode" env:"GCHM_SSL_MODE"`
	Insecure bool   `json:"insecure" yaml:"insecure" env:"GCHM_INSECURE"`
}

func (*Config) GetDSN

func (c *Config) GetDSN() string

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the configuration provided. TOOD: In the future, this should be replaced with a more robust validation

type DatabaseBuilder

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

func NewCreateDatabase

func NewCreateDatabase(ctx context.Context, orm *ORM, dbName string, useDb bool, queryOptions *chconn.QueryOptions) (*DatabaseBuilder, error)

func NewDropDatabase

func NewDropDatabase(ctx context.Context, orm *ORM, dbName string, queryOptions *chconn.QueryOptions) (*DatabaseBuilder, error)

func (*DatabaseBuilder) Build

func (b *DatabaseBuilder) Build() (string, error)

func (*DatabaseBuilder) ExecContext

func (b *DatabaseBuilder) ExecContext(ctx context.Context, queryOptions *chconn.QueryOptions) error

func (*DatabaseBuilder) SQL

func (b *DatabaseBuilder) SQL() string

type Field

type Field struct {
	Table      string
	PrimaryKey bool
	Name       string
	Default    string
	Type       string
	GoField    reflect.StructField
}

type InsertBuilder

type InsertBuilder[T models.Model] struct {
	// contains filtered or unexported fields
}

func NewInsert

func NewInsert[T models.Model](ctx context.Context, orm *ORM, model T, queryOptions *chconn.QueryOptions) (T, *InsertBuilder[T], error)

func (*InsertBuilder[T]) Build

func (b *InsertBuilder[T]) Build() (string, error)

func (*InsertBuilder[T]) ExecContext

func (b *InsertBuilder[T]) ExecContext(ctx context.Context, queryOptions *chconn.QueryOptions, columns ...column.ColumnBasic) error

func (*InsertBuilder[T]) SQL

func (b *InsertBuilder[T]) SQL() string

type Migration

type Migration struct {
	models.Model `clickhouse:"table:migrations, engine:MergeTree(), order: uuid"`

	UUID      uuid.UUID `clickhouse:"name:uuid, type:UUID, primary:true"`
	Name      string    `clickhouse:"name:name, type:String"`
	Migrated  bool      `clickhouse:"name:migrated, type:Boolean"`
	CreatedAt time.Time `clickhouse:"name:created_at, type:DateTime"`
	UpdatedAt time.Time `clickhouse:"name:updated_at, type:DateTime"`
}

func (*Migration) Settings

func (m *Migration) Settings() []string

func (*Migration) TableName

func (m *Migration) TableName() string

type MigrationRecord

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

type Migrator

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

func NewMigrator

func NewMigrator(orm *ORM) (*Migrator, error)

func (*Migrator) Destroy

func (m *Migrator) Destroy(ctx context.Context, queryOptions *chconn.QueryOptions) error

func (*Migrator) GetMigrations

func (m *Migrator) GetMigrations() map[string]MigrationRecord

func (*Migrator) Migrate

func (m *Migrator) Migrate(ctx context.Context, queryOptions *chconn.QueryOptions) error

func (*Migrator) RegisterMigration

func (m *Migrator) RegisterMigration(
	name string,
	up func(ctx context.Context, orm *ORM, migrator *Migrator) error,
	down func(ctx context.Context, orm *ORM, migrator *Migrator) error,
) error

func (*Migrator) Rollback

func (m *Migrator) Rollback(ctx context.Context, queryOptions *chconn.QueryOptions) error

func (*Migrator) Setup

func (m *Migrator) Setup(ctx context.Context, queryOptions *chconn.QueryOptions) error

type ORM

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

func NewORM

func NewORM(ctx context.Context, cfg *Config) (*ORM, error)

func (*ORM) Close

func (o *ORM) Close()

func (*ORM) Connect

func (o *ORM) Connect() error

func (*ORM) CreateDatabase

func (o *ORM) CreateDatabase(ctx context.Context, dbName string, useDb bool, queryOptions *chconn.QueryOptions) (*DatabaseBuilder, error)

func (*ORM) DropDatabase

func (o *ORM) DropDatabase(ctx context.Context, dbName string, queryOptions *chconn.QueryOptions) (*DatabaseBuilder, error)

func (*ORM) GetConfig

func (o *ORM) GetConfig() *Config

func (*ORM) GetConn

func (o *ORM) GetConn() chpool.Pool

func (*ORM) GetContext

func (o *ORM) GetContext() context.Context

func (*ORM) GetDatabaseName

func (o *ORM) GetDatabaseName() string

func (*ORM) GetManager

func (o *ORM) GetManager() *models.Manager

func (*ORM) GetMigrator

func (o *ORM) GetMigrator() *Migrator

func (*ORM) Insert

func (o *ORM) Insert(ctx context.Context, model models.Model, queryOptions *chconn.QueryOptions) (models.Model, *InsertBuilder[models.Model], error)

type SelectBuilder

type SelectBuilder[T models.Model] struct {
	*sql.SelectBuilder
	// contains filtered or unexported fields
}

func NewSelect

func NewSelect[T models.Model](ctx context.Context, orm *ORM, queryOptions *chconn.QueryOptions) (*SelectBuilder[T], error)

func (*SelectBuilder[T]) Build

func (s *SelectBuilder[T]) Build() (string, error)

func (*SelectBuilder[T]) ColumnByType

func (s *SelectBuilder[T]) ColumnByType(chType []byte, arrayLevel int, nullable, lc bool) (column.ColumnBasic, error)

func (*SelectBuilder[T]) ExecContext

func (s *SelectBuilder[T]) ExecContext(ctx context.Context, queryOptions *chconn.QueryOptions, columns ...column.ColumnBasic) error

func (*SelectBuilder[T]) GetBuilder

func (s *SelectBuilder[T]) GetBuilder() *sql.SelectBuilder

func (*SelectBuilder[T]) GetColumnsByChType

func (s *SelectBuilder[T]) GetColumnsByChType(b models.Model) ([]column.ColumnBasic, error)

func (*SelectBuilder[T]) One

func (s *SelectBuilder[T]) One(ctx context.Context, queryOptions *chconn.QueryOptions, record T) error

func (*SelectBuilder[T]) SQL

func (s *SelectBuilder[T]) SQL() string

func (*SelectBuilder[T]) Scan

func (s *SelectBuilder[T]) Scan(ctx context.Context, queryOptions *chconn.QueryOptions) ([]T, error)

type TableBuilder

type TableBuilder[T models.Model] struct {
	// contains filtered or unexported fields
}

func NewCreateTable

func NewCreateTable[T models.Model](ctx context.Context, orm *ORM, model T, queryOptions *chconn.QueryOptions) (*TableBuilder[T], error)

func NewDropTable

func NewDropTable[T models.Model](ctx context.Context, orm *ORM, model T, queryOptions *chconn.QueryOptions) (*TableBuilder[T], error)

func (*TableBuilder[T]) Build

func (b *TableBuilder[T]) Build() (string, error)

func (*TableBuilder[T]) Exec

func (b *TableBuilder[T]) Exec(ctx context.Context) error

func (*TableBuilder[T]) ExecContext

func (b *TableBuilder[T]) ExecContext(ctx context.Context, queryOptions *chconn.QueryOptions, columns ...column.ColumnBasic) error

func (*TableBuilder[T]) GetBuilder

func (b *TableBuilder[T]) GetBuilder() *sql.TableBuilder

func (*TableBuilder[T]) SQL

func (b *TableBuilder[T]) SQL() string

type UpdateBuilder

type UpdateBuilder[T models.Model] struct {
	// contains filtered or unexported fields
}

func NewUpdate

func NewUpdate[T models.Model](ctx context.Context, orm *ORM, model T, queryOptions *chconn.QueryOptions) (T, *UpdateBuilder[T], error)

func (*UpdateBuilder[T]) Build

func (b *UpdateBuilder[T]) Build() (string, error)

func (*UpdateBuilder[T]) ExecContext

func (b *UpdateBuilder[T]) ExecContext(ctx context.Context, queryOptions *chconn.QueryOptions, columns ...column.ColumnBasic) error

func (*UpdateBuilder[T]) SQL

func (b *UpdateBuilder[T]) SQL() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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