Documentation
¶
Index ¶
- Constants
- Variables
- type AttrCascadeMaxDepth
- type AttrMaxCPUCores
- type AttrMaxMemoryBytes
- type AttrMaxMemoryGB
- type AttrMaxMemoryKB
- type AttrMaxMemoryMB
- type AttrUUIDUpper
- type AttrUUIDVersion
- type AttrUUIDWithHyphen
- type CascadeInclude
- type CascadeQuery
- type Column
- type ColumnCheck
- type ColumnType
- type DatabaseConfig
- type ForeignKey
- type QueryCondition
- type Row
- type RowUpdate
- type SchemaAttributer
- type SimpleDB
- func (db *SimpleDB) BeginTx() (*Tx, error)
- func (db *SimpleDB) Close() error
- func (db *SimpleDB) Compact() error
- func (db *SimpleDB) Configure(schema TableSchema) (err error)
- func (db *SimpleDB) Delete(key string) (err error)
- func (db *SimpleDB) DeleteRow(primaryKey any) error
- func (db *SimpleDB) DeleteRows(primaryKeys []any) error
- func (db *SimpleDB) Find(conditions ...QueryCondition) ([]Row, error)
- func (db *SimpleDB) FindByConditions(conditions []QueryCondition) ([]Row, error)
- func (db *SimpleDB) FindByConditionsJSON(conditions []QueryCondition) ([]byte, error)
- func (db *SimpleDB) FindByIndex(field string, value any) ([]Row, error)
- func (db *SimpleDB) FindByUnique(field string, value any) (Row, bool, error)
- func (db *SimpleDB) FindOne(conditions ...QueryCondition) (Row, bool, error)
- func (db *SimpleDB) FindRow(primaryKey any) (Row, bool, error)
- func (db *SimpleDB) Get(key string) ([]byte, bool, error)
- func (db *SimpleDB) GetConfig() DatabaseConfig
- func (db *SimpleDB) GetSchema() (*TableSchema, error)
- func (db *SimpleDB) InsertRow(values Row) (row Row, err error)
- func (db *SimpleDB) InsertRows(values []Row) ([]Row, error)
- func (db *SimpleDB) Keys() ([]string, error)
- func (db *SimpleDB) Put(key string, value []byte) (err error)
- func (db *SimpleDB) Query(prefix string) (map[string][]byte, error)
- func (db *SimpleDB) QueryCascadeJSON(query CascadeQuery) ([]byte, error)
- func (db *SimpleDB) RemoveByCondition(conditions ...QueryCondition) (int, error)
- func (db *SimpleDB) RemoveOneByCondition(conditions ...QueryCondition) (bool, error)
- func (db *SimpleDB) SetAttrs(attrs ...SchemaAttributer) *SimpleDB
- func (db *SimpleDB) Update(key string, value []byte) (err error)
- func (db *SimpleDB) UpdateRow(primaryKey any, updates Row) (Row, error)
- func (db *SimpleDB) UpdateRows(updates []RowUpdate) ([]Row, error)
- type TableSchema
- type Tx
Constants ¶
View Source
const ( DefaultUUIDVersion = 6 DefaultCascadeMaxDepth = 6 HardCascadeMaxDepthLimit = 6 DefaultUUIDWithHyphen = true DefaultUUIDUppercase = true )
View Source
const ( ColumnExprCurrentTime = "current_time" ColumnExprCurrentTimestamp = "current_timestamp" )
View Source
const ( ColumnCheckGT = "gt" ColumnCheckGTE = "gte" ColumnCheckLT = "lt" ColumnCheckLTE = "lte" ColumnCheckLenGT = "len_gt" ColumnCheckLenGTE = "len_gte" ColumnCheckLenLT = "len_lt" ColumnCheckLenLTE = "len_lte" ColumnCheckRegex = "regex" )
View Source
const ( QueryOpEQ = "eq" QueryOpNE = "ne" QueryOpGT = "gt" QueryOpGTE = "gte" QueryOpLT = "lt" QueryOpLTE = "lte" QueryOpIn = "in" QueryOpNotIn = "not_in" QueryOpBetween = "between" QueryOpNotBetween = "not_between" )
Variables ¶
View Source
var ( ErrEmptyKey = fmt.Errorf("%s 读取错误:key 为空", dbLogTitle) ErrKeyNotFound = fmt.Errorf("%s 读取错误:key 对应数据不存在", dbLogTitle) ErrKeyDeleted = fmt.Errorf("%s 读取错误:key 已经被删除", dbLogTitle) ErrDatabaseClosed = fmt.Errorf("%s 读取错误:数据库已经被关闭", dbLogTitle) ErrDatabaseLocked = fmt.Errorf("%s 打开数据库错误:数据库文件已被锁定", dbLogTitle) ErrCorruptedRecord = fmt.Errorf("%s 读取错误:数据记录损坏", dbLogTitle) ErrDBPathEmpty = fmt.Errorf("%s 打开数据库错误:目录为空", dbLogTitle) ErrUnkownOperation = fmt.Errorf("%s 未知操作:", dbLogTitle) ErrInitDB = fmt.Errorf("初始化数据库错误") ErrSchemaNotConfigured = fmt.Errorf("%s 结构错误:表结构尚未配置", dbLogTitle) ErrSchemaAlreadyExists = fmt.Errorf("%s 结构错误:表结构已经存在", dbLogTitle) ErrInvalidSchema = fmt.Errorf("%s 结构错误:表结构无效", dbLogTitle) ErrFieldNotDefined = fmt.Errorf("%s 结构错误:字段未定义", dbLogTitle) ErrPrimaryKeyMissing = fmt.Errorf("%s 写入错误:主键缺失", dbLogTitle) ErrPrimaryKeyConflict = fmt.Errorf("%s 写入错误:主键冲突", dbLogTitle) ErrPrimaryKeyImmutable = fmt.Errorf("%s 更新错误:不允许修改主键", dbLogTitle) ErrUniqueConflict = fmt.Errorf("%s 写入错误:唯一索引冲突", dbLogTitle) ErrFieldNotIndexed = fmt.Errorf("%s 查询错误:字段未建立索引", dbLogTitle) ErrUnsupportedFieldType = fmt.Errorf("%s 结构错误:字段类型不支持", dbLogTitle) ErrFieldTypeMismatch = fmt.Errorf("%s 写入错误:字段类型不匹配", dbLogTitle) ErrFieldRequired = fmt.Errorf("%s 写入错误:字段必填", dbLogTitle) ErrFieldNotNullable = fmt.Errorf("%s 写入错误:字段不允许为 null", dbLogTitle) ErrFieldLengthViolation = fmt.Errorf("%s 写入错误:字段长度不合法", dbLogTitle) ErrFieldEnumViolation = fmt.Errorf("%s 写入错误:字段值不在枚举范围内", dbLogTitle) ErrFieldCheckViolation = fmt.Errorf("%s 写入错误:字段 check 约束失败", dbLogTitle) ErrInvalidQueryCondition = fmt.Errorf("%s 查询错误:查询条件无效", dbLogTitle) ErrInvalidForeignKey = fmt.Errorf("%s 结构错误:外键定义无效", dbLogTitle) ErrRelationNotFound = fmt.Errorf("%s 查询错误:关联关系不存在", dbLogTitle) ErrCascadeDepthExceeded = fmt.Errorf("%s 查询错误:级联查询深度超限", dbLogTitle) ErrCascadeCycleNotAllow = fmt.Errorf("%s 查询错误:不允许级联环", dbLogTitle) ErrBatchEmpty = fmt.Errorf("%s 写入错误:批量操作数据为空", dbLogTitle) ErrTxConflict = fmt.Errorf("%s 事务错误:检测到写冲突", dbLogTitle) ErrTxClosed = fmt.Errorf("%s 事务错误:事务已关闭", dbLogTitle) )
View Source
var New app
Functions ¶
This section is empty.
Types ¶
type AttrCascadeMaxDepth ¶
type AttrCascadeMaxDepth struct{ CascadeMaxDepth int }
func CascadeMaxDepth ¶
func CascadeMaxDepth(depth int) AttrCascadeMaxDepth
func (AttrCascadeMaxDepth) RegisterAttr ¶
func (my AttrCascadeMaxDepth) RegisterAttr(db *SimpleDB)
type AttrMaxCPUCores ¶
type AttrMaxCPUCores struct{ MaxCPUCores uint8 }
func MaxCPUCores ¶
func MaxCPUCores(cores uint8) AttrMaxCPUCores
func (AttrMaxCPUCores) RegisterAttr ¶
func (my AttrMaxCPUCores) RegisterAttr(db *SimpleDB)
type AttrMaxMemoryBytes ¶
type AttrMaxMemoryBytes struct{ MaxMemoryBytes uint64 }
func MaxMemoryBytes ¶
func MaxMemoryBytes(volume uint64) AttrMaxMemoryBytes
func (AttrMaxMemoryBytes) RegisterAttr ¶
func (my AttrMaxMemoryBytes) RegisterAttr(db *SimpleDB)
type AttrMaxMemoryGB ¶
type AttrMaxMemoryGB struct{ MaxMemoryGB uint64 }
func MaxMemoryGB ¶
func MaxMemoryGB(volume uint64) AttrMaxMemoryGB
func (AttrMaxMemoryGB) RegisterAttr ¶
func (my AttrMaxMemoryGB) RegisterAttr(db *SimpleDB)
type AttrMaxMemoryKB ¶
type AttrMaxMemoryKB struct{ MaxMemoryKB uint64 }
func MaxMemoryKB ¶
func MaxMemoryKB(volume uint64) AttrMaxMemoryKB
func (AttrMaxMemoryKB) RegisterAttr ¶
func (my AttrMaxMemoryKB) RegisterAttr(db *SimpleDB)
type AttrMaxMemoryMB ¶
type AttrMaxMemoryMB struct{ MaxMemoryMB uint64 }
func MaxMemoryMB ¶
func MaxMemoryMB(volume uint64) AttrMaxMemoryMB
func (AttrMaxMemoryMB) RegisterAttr ¶
func (my AttrMaxMemoryMB) RegisterAttr(db *SimpleDB)
type AttrUUIDUpper ¶
type AttrUUIDUpper struct{ UUIDUpper *bool }
func UUIDUpper ¶
func UUIDUpper(withUpper bool) AttrUUIDUpper
func (AttrUUIDUpper) RegisterAttr ¶
func (my AttrUUIDUpper) RegisterAttr(db *SimpleDB)
type AttrUUIDVersion ¶
type AttrUUIDVersion struct{ UUIDVersion uint8 }
func UUIDVersion ¶
func UUIDVersion(version uint8) AttrUUIDVersion
func (AttrUUIDVersion) RegisterAttr ¶
func (my AttrUUIDVersion) RegisterAttr(db *SimpleDB)
type AttrUUIDWithHyphen ¶
type AttrUUIDWithHyphen struct{ UUIDWithHyphen *bool }
func UUIDWithHyphen ¶
func UUIDWithHyphen(withHyphen bool) AttrUUIDWithHyphen
func (AttrUUIDWithHyphen) RegisterAttr ¶
func (my AttrUUIDWithHyphen) RegisterAttr(db *SimpleDB)
type CascadeInclude ¶
type CascadeInclude struct {
Table string `json:"table"`
Alias string `json:"alias,omitempty"`
ForeignKey string `json:"foreignKey,omitempty"`
Conditions []QueryCondition `json:"conditions,omitempty"`
Includes []CascadeInclude `json:"includes,omitempty"`
}
type CascadeQuery ¶
type CascadeQuery struct {
Conditions []QueryCondition `json:"conditions,omitempty"`
Includes []CascadeInclude `json:"includes,omitempty"`
MaxDepth int `json:"maxDepth,omitempty"`
}
type Column ¶
type Column struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Default any `json:"default,omitempty"`
DefaultExpr string `json:"defaultExpr,omitempty"`
OnUpdateExpr string `json:"onUpdateExpr,omitempty"`
MinLength int `json:"minLength,omitempty"`
MaxLength int `json:"maxLength,omitempty"`
Enum []any `json:"enum,omitempty"`
Checks []ColumnCheck `json:"checks,omitempty"`
Nullable *bool `json:"nullable,omitempty"`
Required bool `json:"required,omitempty"`
PrimaryKey bool `json:"primaryKey,omitempty"`
AutoIncrement bool `json:"autoIncrement,omitempty"`
Unique bool `json:"unique,omitempty"`
Indexed bool `json:"indexed,omitempty"`
}
type ColumnCheck ¶
type ColumnType ¶
type ColumnType string
const ( ColumnTypeAny ColumnType = "any" ColumnTypeString ColumnType = "string" ColumnTypeInt ColumnType = "int" ColumnTypeFloat ColumnType = "float" ColumnTypeBool ColumnType = "bool" ColumnTypeObject ColumnType = "object" ColumnTypeArray ColumnType = "array" ColumnTypeUUID ColumnType = "uuid" ColumnTypeTime ColumnType = "time" ColumnTypeTimestamp ColumnType = "timestamp" )
type DatabaseConfig ¶
type DatabaseConfig struct {
DefaultUUIDVersion int `json:"defaultUUIDVersion,omitempty"`
DefaultUUIDWithHyphen *bool `json:"defaultUUIDWithHyphen,omitempty"`
DefaultUUIDUppercase *bool `json:"defaultUUIDUppercase,omitempty"`
DefaultCascadeMaxDepth int `json:"defaultCascadeMaxDepth,omitempty"`
MaxCPUCores int `json:"maxCpuCores,omitempty"`
MaxMemoryBytes uint64 `json:"maxMemoryBytes,omitempty"`
}
type ForeignKey ¶
type QueryCondition ¶
type SchemaAttributer ¶
type SchemaAttributer interface{ RegisterAttr(db *SimpleDB) }
type SimpleDB ¶
type SimpleDB struct {
// contains filtered or unexported fields
}
func (*SimpleDB) Configure ¶
func (db *SimpleDB) Configure(schema TableSchema) (err error)
func (*SimpleDB) DeleteRows ¶
func (*SimpleDB) Find ¶
func (db *SimpleDB) Find(conditions ...QueryCondition) ([]Row, error)
Find is the unified query entry. It accepts arbitrary conditions and automatically chooses index-based candidates when possible, then falls back to row scan for non-indexed predicates.
func (*SimpleDB) FindByConditions ¶
func (db *SimpleDB) FindByConditions(conditions []QueryCondition) ([]Row, error)
func (*SimpleDB) FindByConditionsJSON ¶
func (db *SimpleDB) FindByConditionsJSON(conditions []QueryCondition) ([]byte, error)
func (*SimpleDB) FindByIndex ¶
func (*SimpleDB) FindByUnique ¶
func (*SimpleDB) FindOne ¶
func (db *SimpleDB) FindOne(conditions ...QueryCondition) (Row, bool, error)
FindOne returns the first matched row for a given condition set. The second returned value indicates whether a row is found.
func (*SimpleDB) GetConfig ¶
func (db *SimpleDB) GetConfig() DatabaseConfig
func (*SimpleDB) GetSchema ¶
func (db *SimpleDB) GetSchema() (*TableSchema, error)
func (*SimpleDB) QueryCascadeJSON ¶
func (db *SimpleDB) QueryCascadeJSON(query CascadeQuery) ([]byte, error)
func (*SimpleDB) RemoveByCondition ¶
func (db *SimpleDB) RemoveByCondition(conditions ...QueryCondition) (int, error)
RemoveByCondition removes all rows matching the given conditions. It returns the count of deleted rows.
func (*SimpleDB) RemoveOneByCondition ¶
func (db *SimpleDB) RemoveOneByCondition(conditions ...QueryCondition) (bool, error)
RemoveOneByCondition removes the first row matching the given conditions. It returns a boolean indicating whether a row was deleted.
func (*SimpleDB) SetAttrs ¶
func (db *SimpleDB) SetAttrs(attrs ...SchemaAttributer) *SimpleDB
type TableSchema ¶
type TableSchema struct {
Columns []Column `json:"columns"`
ForeignKeys []ForeignKey `json:"foreignKeys,omitempty"`
PrimaryKey string `json:"primaryKey"`
AutoIncrement bool `json:"autoIncrement,omitempty"`
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.