engine

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package engine provides an xorm-compatible API layer for hanzo/orm.

Type conversion uses standard encoding/json — no custom parsers, no whitespace bugs. This fixes the xorm v1.1.6 issue where JSON []string with spaces after commas fails to deserialize.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Between

func Between(col string, low, high interface{}) condition

Between creates a BETWEEN condition.

func Eq

func Eq(col string, value interface{}) condition

Eq creates an equality condition: col = value.

func Expr

func Expr(sql string, args ...interface{}) condition

Expr creates a raw SQL expression condition.

func Gt

func Gt(col string, value interface{}) condition

Gt creates a greater-than condition: col > value.

func Gte

func Gte(col string, value interface{}) condition

Gte creates a greater-than-or-equal condition: col >= value.

func In

func In(col string, args ...interface{}) condition

In creates an IN condition for use with Session.Where().

func IsNotNull

func IsNotNull(col string) condition

IsNotNull creates an IS NOT NULL condition.

func IsNull

func IsNull(col string) condition

IsNull creates an IS NULL condition.

func Like

func Like(col string, pattern string) condition

Like creates a LIKE condition.

func Lt

func Lt(col string, value interface{}) condition

Lt creates a less-than condition: col < value.

func Lte

func Lte(col string, value interface{}) condition

Lte creates a less-than-or-equal condition: col <= value.

func Neq

func Neq(col string, value interface{}) condition

Neq creates a not-equal condition: col != value.

func NotIn

func NotIn(col string, args ...interface{}) condition

NotIn creates a NOT IN condition.

Types

type Builder

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

Builder assembles SQL queries with proper parameter binding.

func NewBuilder

func NewBuilder(driver string) *Builder

NewBuilder creates a builder for the given driver.

func (*Builder) Append

func (b *Builder) Append(c byte)

Append appends a single byte.

func (*Builder) Args

func (b *Builder) Args() []interface{}

Args returns the collected arguments.

func (*Builder) Reset

func (b *Builder) Reset()

Reset clears the builder for reuse.

func (*Builder) String

func (b *Builder) String() string

String returns the assembled SQL.

func (*Builder) WriteArg

func (b *Builder) WriteArg(arg interface{})

WriteArg appends a placeholder and records the argument.

func (*Builder) WriteString

func (b *Builder) WriteString(s string)

WriteString appends raw SQL text.

type ColumnMeta

type ColumnMeta struct {
	Name         string
	GoName       string
	GoType       reflect.Type
	SQLType      string
	IsPrimaryKey bool
	IsAutoIncr   bool
	IsNullable   bool
	IsUnique     bool
	IsCreated    bool // auto-set on insert
	IsUpdated    bool // auto-set on update
	IsDeleted    bool // soft-delete
	IsVersion    bool // optimistic lock
	IsJSON       bool // stored as JSON text
	Default      string
	Length       int
	Index        string // index name, empty = no index
}

ColumnMeta holds metadata for a single column.

type Cond

type Cond interface {
	WriteTo(w *Builder) error
}

Cond is the interface for building complex conditions.

type Engine

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

Engine is the xorm-compatible entrypoint. It wraps a *sql.DB and provides table registration, schema sync, and Session creation.

func NewEngine

func NewEngine(driver, dsn string) (*Engine, error)

NewEngine creates an Engine from a driver name and DSN.

func NewEngineWithDB

func NewEngineWithDB(driver string, db *sql.DB) *Engine

NewEngineWithDB creates an Engine from an existing *sql.DB.

func (*Engine) Close

func (e *Engine) Close() error

Close closes the underlying database connection.

func (*Engine) Count

func (e *Engine) Count(bean ...interface{}) (int64, error)

Count counts matching records.

func (*Engine) DB

func (e *Engine) DB() *sql.DB

DB returns the underlying *sql.DB.

func (*Engine) Delete

func (e *Engine) Delete(bean interface{}) (int64, error)

Delete deletes records.

func (*Engine) DriverName

func (e *Engine) DriverName() string

DriverName returns the normalized driver name.

func (*Engine) Exec

func (e *Engine) Exec(sqlOrArgs ...interface{}) (sql.Result, error)

Exec executes raw SQL.

func (*Engine) Find

func (e *Engine) Find(beans interface{}, conds ...interface{}) error

Find retrieves multiple records.

func (*Engine) Get

func (e *Engine) Get(bean interface{}) (bool, error)

Get retrieves a single record by primary key conditions on the bean.

func (*Engine) ID

func (e *Engine) ID(pk interface{}) *Session

ID starts a query by primary key.

func (*Engine) Insert

func (e *Engine) Insert(beans ...interface{}) (int64, error)

Insert inserts one or more records.

func (*Engine) NewSession

func (e *Engine) NewSession() *Session

NewSession creates a new session for this engine.

func (*Engine) Ping

func (e *Engine) Ping() error

Ping tests the database connection.

func (*Engine) Query

func (e *Engine) Query(sqlOrArgs ...interface{}) ([]map[string][]byte, error)

Query executes raw SQL and returns rows.

func (*Engine) SQL

func (e *Engine) SQL(query string, args ...interface{}) *Session

SQL starts a raw SQL query.

func (*Engine) SetColumnMapper

func (e *Engine) SetColumnMapper(mapper names.Mapper)

SetColumnMapper sets the column name mapper.

func (*Engine) SetMapper

func (e *Engine) SetMapper(mapper names.Mapper)

SetMapper sets both column and table mapper.

func (*Engine) SetTableMapper

func (e *Engine) SetTableMapper(mapper names.Mapper)

SetTableMapper sets the table name mapper.

func (*Engine) ShowSQL

func (e *Engine) ShowSQL(show bool)

ShowSQL enables SQL logging.

func (*Engine) Sync2

func (e *Engine) Sync2(beans ...interface{}) error

Sync2 synchronizes table schemas from struct definitions. Creates tables if they don't exist, adds missing columns.

func (*Engine) Table

func (e *Engine) Table(name string) *TableMeta

Table returns the cached TableMeta for a given name.

func (*Engine) Transaction

func (e *Engine) Transaction(fn func(*Session) (interface{}, error)) (interface{}, error)

Transaction runs fn inside a transaction. If fn returns nil the tx is committed; otherwise it is rolled back.

func (*Engine) Update

func (e *Engine) Update(bean interface{}, conds ...interface{}) (int64, error)

Update updates records.

func (*Engine) Where

func (e *Engine) Where(query string, args ...interface{}) *Session

Where starts a conditional query (convenience for Engine.NewSession().Where()).

type PK

type PK []interface{}

PK represents a composite primary key, matching xorm's core.PK.

type Session

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

Session provides the xorm-compatible fluent query/mutation API.

func (*Session) Alias

func (s *Session) Alias(alias string) *Session

Alias sets a table alias.

func (*Session) AllCols

func (s *Session) AllCols() *Session

AllCols includes all columns.

func (*Session) And

func (s *Session) And(query string, args ...interface{}) *Session

And adds an AND condition.

func (*Session) Asc

func (s *Session) Asc(colNames ...string) *Session

Asc sets ascending ORDER BY.

func (*Session) Begin

func (s *Session) Begin() error

Begin starts a transaction.

func (*Session) Close

func (s *Session) Close()

Close is a no-op for session (for xorm compatibility).

func (*Session) Cols

func (s *Session) Cols(cols ...string) *Session

Cols specifies which columns to include.

func (*Session) Commit

func (s *Session) Commit() error

Commit commits the transaction.

func (*Session) Context

func (s *Session) Context(ctx context.Context) *Session

Context sets the context for this session.

func (*Session) Count

func (s *Session) Count(bean ...interface{}) (int64, error)

Count counts matching records.

func (*Session) Delete

func (s *Session) Delete(bean interface{}) (int64, error)

Delete deletes records matching conditions.

func (*Session) Desc

func (s *Session) Desc(colNames ...string) *Session

Desc sets descending ORDER BY.

func (*Session) Exec

func (s *Session) Exec(sqlOrArgs ...interface{}) (sql.Result, error)

Exec executes raw SQL.

func (*Session) Exist

func (s *Session) Exist(bean ...interface{}) (bool, error)

Exist checks if a record exists.

func (*Session) Find

func (s *Session) Find(beans interface{}, conds ...interface{}) error

Find retrieves multiple records into a slice pointer.

func (*Session) Get

func (s *Session) Get(bean interface{}) (bool, error)

Get retrieves a single record into bean. Returns (true, nil) if found.

func (*Session) GroupBy

func (s *Session) GroupBy(keys string) *Session

GroupBy sets GROUP BY.

func (*Session) Having

func (s *Session) Having(cond string) *Session

Having sets the HAVING clause.

func (*Session) ID

func (s *Session) ID(pk interface{}) *Session

ID sets the primary key for the query.

func (*Session) In

func (s *Session) In(col string, args ...interface{}) *Session

In adds an IN condition.

func (*Session) Insert

func (s *Session) Insert(beans ...interface{}) (int64, error)

Insert inserts one or more records.

func (*Session) Join

func (s *Session) Join(joinType, tableName string, cond string, args ...interface{}) *Session

Join adds a JOIN clause.

func (*Session) Limit

func (s *Session) Limit(limit int, start ...int) *Session

Limit sets the LIMIT and optional OFFSET.

func (*Session) NotIn

func (s *Session) NotIn(col string, args ...interface{}) *Session

NotIn adds a NOT IN condition.

func (*Session) Omit

func (s *Session) Omit(cols ...string) *Session

Omit excludes specific columns.

func (*Session) Or

func (s *Session) Or(query string, args ...interface{}) *Session

Or adds an OR condition.

func (*Session) OrderBy

func (s *Session) OrderBy(order string) *Session

OrderBy sets a raw ORDER BY clause.

func (*Session) Prepare

func (s *Session) Prepare() *Session

Prepare is a no-op for compatibility.

func (*Session) QueryBytes

func (s *Session) QueryBytes(sqlOrArgs ...interface{}) ([]map[string][]byte, error)

QueryBytes executes raw SQL and returns []map[string][]byte.

func (*Session) Rollback

func (s *Session) Rollback() error

Rollback rolls back the transaction.

func (*Session) SQL

func (s *Session) SQL(query string, args ...interface{}) *Session

SQL sets a raw SQL query.

func (*Session) Select

func (s *Session) Select(str string) *Session

Select sets a custom SELECT clause.

func (*Session) Table

func (s *Session) Table(name interface{}) *Session

Table sets the table name.

func (*Session) Update

func (s *Session) Update(bean interface{}, conds ...interface{}) (int64, error)

Update updates records matching conditions.

func (*Session) Where

func (s *Session) Where(query string, args ...interface{}) *Session

Where adds a WHERE condition.

type TableMeta

type TableMeta struct {
	Name       string
	Columns    []*ColumnMeta
	PrimaryKey []string // column names
	// contains filtered or unexported fields
}

TableMeta holds metadata for a table.

func (*TableMeta) Column

func (t *TableMeta) Column(name string) *ColumnMeta

Column returns a column by name.

Jump to

Keyboard shortcuts

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