driver

package
v1.68.5 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

simpleDB driver

driversimpleDB/kernal 的上层封装,提供稳定、统一、易用的 API。

设计目标

  • 定义统一驱动 API
  • 映射核心层事务能力(MVCC)
  • 统一错误模型(DriverError + ErrorCode
  • 支持事务隔离级别

快速开始

package main

import (
    "fmt"

    "github.com/aid297/aid/simpleDB/driver"
)

func main() {
    db, err := driver.New.DB("demo", "users")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Configure(driver.TableSchema{Columns: []driver.Column{
        {Name: "id", Type: "uuid:v7", PrimaryKey: true, AutoIncrement: true},
        {Name: "username", Type: "string", Unique: true, Required: true},
    }})
    if err != nil {
        panic(err)
    }

    tx, err := db.BeginTxWithOptions(driver.TxOptions{Isolation: driver.TxIsolationSnapshot})
    if err != nil {
        panic(err)
    }

    _, err = tx.InsertRow(driver.Row{"username": "alice"})
    if err != nil {
        _ = tx.Rollback()
        panic(err)
    }

    if err = tx.Commit(); err != nil {
        panic(err)
    }

    row, ok, err := db.FindOne(driver.QueryCondition{Field: "username", Operator: driver.QueryOpEQ, Value: "alice"})
    if err != nil {
        panic(err)
    }
    fmt.Println(ok, row)
}

错误模型

所有返回错误都会包装为 DriverError

  • invalid_argument
  • not_found
  • conflict
  • closed
  • read_only
  • internal

可用 errors.As(err, *DriverError) 获取错误码。

Documentation

Index

Constants

View Source
const (
	TableAccessScopeDML = kernal.TableAccessScopeDML
	TableAccessScopeDDL = kernal.TableAccessScopeDDL
)
View Source
const (
	QueryOpEQ         = kernal.QueryOpEQ
	QueryOpNE         = kernal.QueryOpNE
	QueryOpGT         = kernal.QueryOpGT
	QueryOpGTE        = kernal.QueryOpGTE
	QueryOpLT         = kernal.QueryOpLT
	QueryOpLTE        = kernal.QueryOpLTE
	QueryOpIn         = kernal.QueryOpIn
	QueryOpNotIn      = kernal.QueryOpNotIn
	QueryOpBetween    = kernal.QueryOpBetween
	QueryOpNotBetween = kernal.QueryOpNotBetween
)

Variables

View Source
var New app

Functions

This section is empty.

Types

type AlterTablePlan

type AlterTablePlan = kernal.AlterTablePlan

type AuthenticatedUser

type AuthenticatedUser = kernal.AuthenticatedUser

type CascadeInclude

type CascadeInclude = kernal.CascadeInclude

type CascadeQuery

type CascadeQuery = kernal.CascadeQuery

type Column

type Column = kernal.Column

type DB

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

func (*DB) AlterTable

func (db *DB) AlterTable(plan AlterTablePlan) error

AlterTable 按 AlterTablePlan 修改表结构(AddColumn/DropColumn/AddIndex 等)。

func (*DB) AutoMigrate

func (db *DB) AutoMigrate(schema TableSchema) error

AutoMigrate 保守迁移:无表则建,有表则只**增**列/索引,绝不删除(幂等安全)。

func (*DB) BeginReadOnlyTx

func (db *DB) BeginReadOnlyTx() (*Tx, error)

func (*DB) BeginTx

func (db *DB) BeginTx() (*Tx, error)

func (*DB) BeginTxWithOptions

func (db *DB) BeginTxWithOptions(options TxOptions) (*Tx, error)

func (*DB) Close

func (db *DB) Close() error

func (*DB) Compact

func (db *DB) Compact() error

func (*DB) Configure

func (db *DB) Configure(schema TableSchema) error

func (*DB) CreateTable

func (db *DB) CreateTable(schema TableSchema) error

CreateTable 以严格 DDL 语义创建表结构,若已存在 Schema 则返回错误。

func (*DB) Delete

func (db *DB) Delete(key string) error

func (*DB) DeleteRow

func (db *DB) DeleteRow(primaryKey any) error

func (*DB) DeleteRows

func (db *DB) DeleteRows(primaryKeys []any) error

func (*DB) DropTable

func (db *DB) DropTable() error

DropTable 删除所有行数据及 Schema,但不关闭数据库文件。

func (*DB) Find

func (db *DB) Find(conditions ...QueryCondition) ([]Row, error)

func (*DB) FindByConditions

func (db *DB) FindByConditions(conditions []QueryCondition) ([]Row, error)

func (*DB) FindOne

func (db *DB) FindOne(conditions ...QueryCondition) (Row, bool, error)

func (*DB) FindRow

func (db *DB) FindRow(primaryKey any) (Row, bool, error)

func (*DB) Get

func (db *DB) Get(key string) ([]byte, bool, error)

func (*DB) GetConfig

func (db *DB) GetConfig() DatabaseConfig

func (*DB) GetPath

func (db *DB) GetPath() string

func (*DB) GetSchema

func (db *DB) GetSchema() (*TableSchema, error)

func (*DB) HasSchema

func (db *DB) HasSchema() bool

HasSchema 返回表是否已有 Schema 定义。

func (*DB) InsertRow

func (db *DB) InsertRow(values Row) (Row, error)

func (*DB) InsertRows

func (db *DB) InsertRows(values []Row) ([]Row, error)

func (*DB) Keys

func (db *DB) Keys() ([]string, error)

func (*DB) Put

func (db *DB) Put(key string, value []byte) error

func (*DB) Query

func (db *DB) Query(prefix string) (map[string][]byte, error)

func (*DB) QueryCascade

func (db *DB) QueryCascade(query CascadeQuery) ([]map[string]any, error)

func (*DB) QueryCascadeJSON

func (db *DB) QueryCascadeJSON(query CascadeQuery) ([]byte, error)

func (*DB) RemoveByCondition

func (db *DB) RemoveByCondition(conditions ...QueryCondition) (int, error)

func (*DB) RemoveOneByCondition

func (db *DB) RemoveOneByCondition(conditions ...QueryCondition) (bool, error)

func (*DB) SchemaDiff

func (db *DB) SchemaDiff(target TableSchema) (*AlterTablePlan, bool, error)

SchemaDiff 计算从当前 Schema 到目标 Schema 的迁移计划,不执行任何变更。 返回值:(plan, schemaExists, error)

  • plan == nil && !schemaExists → 当前无 Schema,需调用 CreateTable
  • plan == nil && schemaExists → Schema 已与目标一致,无需变更
  • plan != nil → 有差异,plan 描述所需变更

func (*DB) SetAttrs

func (db *DB) SetAttrs(attrs ...kernal.SchemaAttributer) *DB

func (*DB) SetPersistenceConfig

func (db *DB) SetPersistenceConfig(windowSecs int, windowBytes uint64, threshold uint64)

func (*DB) SyncSchema

func (db *DB) SyncSchema(schema TableSchema) error

SyncSchema 完全同步:无表则建,有差异则全量同步(含删列,⚠️ 不可逆)。

func (*DB) TruncateTable

func (db *DB) TruncateTable() error

TruncateTable 清空所有行数据,保留 Schema 定义。

func (*DB) Update

func (db *DB) Update(key string, value []byte) error

func (*DB) UpdateRow

func (db *DB) UpdateRow(primaryKey any, updates Row) (Row, error)

func (*DB) UpdateRows

func (db *DB) UpdateRows(updates []RowUpdate) ([]Row, error)

func (*DB) WithTx

func (db *DB) WithTx(fn func(tx *Tx) error) error

type DatabaseConfig

type DatabaseConfig = kernal.DatabaseConfig

type DriverError

type DriverError struct {
	Code ErrorCode
	Err  error
}

func (*DriverError) Error

func (e *DriverError) Error() string

func (*DriverError) Unwrap

func (e *DriverError) Unwrap() error

type ErrorCode

type ErrorCode string
const (
	ErrorCodeInvalidArgument ErrorCode = "invalid_argument"
	ErrorCodeNotFound        ErrorCode = "not_found"
	ErrorCodeConflict        ErrorCode = "conflict"
	ErrorCodeClosed          ErrorCode = "closed"
	ErrorCodeReadOnly        ErrorCode = "read_only"
	ErrorCodeUnauthorized    ErrorCode = "unauthorized"
	ErrorCodeDDL             ErrorCode = "ddl_error"
	ErrorCodeInternal        ErrorCode = "internal"
)

type ForeignKey

type ForeignKey = kernal.ForeignKey

type QueryCondition

type QueryCondition = kernal.QueryCondition

type Row

type Row = kernal.Row

type RowUpdate

type RowUpdate = kernal.RowUpdate

type TableAccessGrant

type TableAccessGrant = kernal.TableAccessGrant

type TableAccessScope

type TableAccessScope = kernal.TableAccessScope

type TableSchema

type TableSchema = kernal.TableSchema

type Tx

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

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) Delete

func (tx *Tx) Delete(key string) error

func (*Tx) DeleteRow

func (tx *Tx) DeleteRow(primaryKey any) error

func (*Tx) DeleteRows

func (tx *Tx) DeleteRows(primaryKeys []any) error

func (*Tx) Find

func (tx *Tx) Find(conditions ...QueryCondition) ([]Row, error)

func (*Tx) FindByConditions

func (tx *Tx) FindByConditions(conditions []QueryCondition) ([]Row, error)

func (*Tx) FindOne

func (tx *Tx) FindOne(conditions ...QueryCondition) (Row, bool, error)

func (*Tx) FindRow

func (tx *Tx) FindRow(primaryKey any) (Row, bool, error)

func (*Tx) Get

func (tx *Tx) Get(key string) ([]byte, bool, error)

func (*Tx) InsertRow

func (tx *Tx) InsertRow(values Row) (Row, error)

func (*Tx) InsertRows

func (tx *Tx) InsertRows(values []Row) ([]Row, error)

func (*Tx) Keys

func (tx *Tx) Keys() ([]string, error)

func (*Tx) Put

func (tx *Tx) Put(key string, value []byte) error

func (*Tx) Query

func (tx *Tx) Query(prefix string) (map[string][]byte, error)

func (*Tx) RemoveByCondition

func (tx *Tx) RemoveByCondition(conditions ...QueryCondition) (int, error)

func (*Tx) RemoveOneByCondition

func (tx *Tx) RemoveOneByCondition(conditions ...QueryCondition) (bool, error)

func (*Tx) Rollback

func (tx *Tx) Rollback() error

func (*Tx) UpdateRow

func (tx *Tx) UpdateRow(primaryKey any, updates Row) (Row, error)

func (*Tx) UpdateRows

func (tx *Tx) UpdateRows(updates []RowUpdate) ([]Row, error)

type TxIsolation

type TxIsolation string
const (
	TxIsolationSnapshot      TxIsolation = "snapshot"
	TxIsolationReadCommitted TxIsolation = "read_committed"
)

type TxOptions

type TxOptions struct {
	ReadOnly  bool
	Isolation TxIsolation
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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