Documentation
¶
Index ¶
- Constants
- Variables
- type AlterTablePlan
- type AttrCascadeMaxDepth
- type AttrMaxCPUCores
- type AttrMaxMemoryBytes
- type AttrMaxMemoryGB
- type AttrMaxMemoryKB
- type AttrMaxMemoryMB
- type AttrUUIDUpper
- type AttrUUIDVersion
- type AttrUUIDWithHyphen
- type AuthenticatedUser
- 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) AlterTable(plan AlterTablePlan) error
- func (db *SimpleDB) AutoMigrate(schema TableSchema) error
- func (db *SimpleDB) BeginReadOnlyTx() (*Tx, error)
- func (db *SimpleDB) BeginTx() (*Tx, error)
- func (db *SimpleDB) BeginTxWithOptions(options TxOptions) (*Tx, error)
- func (db *SimpleDB) Close() error
- func (db *SimpleDB) Compact() error
- func (db *SimpleDB) Configure(schema TableSchema) (err error)
- func (db *SimpleDB) CreateTable(schema TableSchema) 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) DropTable() 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) HasSchema() bool
- 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) QueryCascade(query CascadeQuery) ([]map[string]any, 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) SchemaDiff(target TableSchema) (*AlterTablePlan, bool, error)
- func (db *SimpleDB) SetAttrs(attrs ...SchemaAttributer) *SimpleDB
- func (db *SimpleDB) SyncSchema(schema TableSchema) error
- func (db *SimpleDB) TruncateTable() error
- 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)
- func (db *SimpleDB) WithTx(fn func(tx *Tx) error) error
- type TableAccessGrant
- type TableAccessScope
- type TableSchema
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) Delete(key string) error
- func (tx *Tx) DeleteRow(primaryKey any) error
- func (tx *Tx) DeleteRows(primaryKeys []any) error
- func (tx *Tx) Find(conditions ...QueryCondition) ([]Row, error)
- func (tx *Tx) FindByConditions(conditions []QueryCondition) ([]Row, error)
- func (tx *Tx) FindOne(conditions ...QueryCondition) (Row, bool, error)
- func (tx *Tx) FindRow(primaryKey any) (Row, bool, error)
- func (tx *Tx) Get(key string) ([]byte, bool, error)
- func (tx *Tx) InsertRow(values Row) (Row, error)
- func (tx *Tx) InsertRows(values []Row) ([]Row, error)
- func (tx *Tx) Keys() ([]string, error)
- func (tx *Tx) Put(key string, value []byte) error
- func (tx *Tx) Query(prefix string) (map[string][]byte, error)
- func (tx *Tx) RemoveByCondition(conditions ...QueryCondition) (int, error)
- func (tx *Tx) RemoveOneByCondition(conditions ...QueryCondition) (bool, error)
- func (tx *Tx) Rollback() error
- func (tx *Tx) UpdateRow(primaryKey any, updates Row) (Row, error)
- func (tx *Tx) UpdateRows(updates []RowUpdate) ([]Row, error)
- type TxIsolationLevel
- type TxOptions
Constants ¶
const ( DefaultUUIDVersion = 6 DefaultCascadeMaxDepth = 6 HardCascadeMaxDepthLimit = 6 DefaultUUIDWithHyphen = true DefaultUUIDUppercase = true )
const ( ColumnExprCurrentTime = "current_time" ColumnExprCurrentTimestamp = "current_timestamp" )
const ( ColumnCheckGT = "gt" ColumnCheckGTE = "gte" ColumnCheckLT = "lt" ColumnCheckLTE = "lte" ColumnCheckLenGT = "len_gt" ColumnCheckLenGTE = "len_gte" ColumnCheckLenLT = "len_lt" ColumnCheckLenLTE = "len_lte" ColumnCheckRegex = "regex" )
const ( QueryOpEQ = "eq" QueryOpNE = "ne" QueryOpGT = "gt" QueryOpGTE = "gte" QueryOpLT = "lt" QueryOpLTE = "lte" QueryOpIn = "in" QueryOpNotIn = "not_in" QueryOpBetween = "between" QueryOpNotBetween = "not_between" )
Variables ¶
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) ErrTxReadOnly = fmt.Errorf("%s 事务错误:只读事务不允许写入", dbLogTitle) ErrAlterTableInvalid = fmt.Errorf("%s DDL 错误:AlterTable 操作无效", dbLogTitle) ErrColumnAlreadyExists = fmt.Errorf("%s DDL 错误:列已存在", dbLogTitle) ErrColumnNotFound = fmt.Errorf("%s DDL 错误:列不存在", dbLogTitle) ErrCannotDropPrimaryKey = fmt.Errorf("%s DDL 错误:不允许删除主键列", dbLogTitle) ErrSystemBootstrap = fmt.Errorf("%s 系统表错误:初始化失败", dbLogTitle) ErrSystemTableSchema = fmt.Errorf("%s 系统表错误:表结构异常", dbLogTitle) ErrInvalidCredentials = fmt.Errorf("%s 认证错误:用户名或密码错误", dbLogTitle) ErrInvalidRegistration = fmt.Errorf("%s 注册错误:用户名和密码不能为空", dbLogTitle) ErrUserAlreadyExists = fmt.Errorf("%s 注册错误:用户名已存在", dbLogTitle) ErrUserNotFound = fmt.Errorf("%s 用户错误:用户不存在", dbLogTitle) ErrRoleNotFound = fmt.Errorf("%s 角色错误:角色不存在", dbLogTitle) ErrInvalidPermissionAssignment = fmt.Errorf("%s 权限错误:角色和权限不能为空", dbLogTitle) ErrSuperAdminRoleReserved = fmt.Errorf("%s 角色错误:super_admin 仅允许 sdb 使用", dbLogTitle) ErrInvalidInitPassword = fmt.Errorf("%s 初始化错误:初始化口令无效", dbLogTitle) ErrUserInactive = fmt.Errorf("%s 认证错误:用户未激活", dbLogTitle) ErrTableAccessDenied = fmt.Errorf("%s 权限错误:无访问权限", dbLogTitle) ErrTableOwnerAlreadyAssigned = fmt.Errorf("%s 权限错误:表已有 owner", dbLogTitle) ErrTableOwnerNotFound = fmt.Errorf("%s 权限错误:表 owner 不存在", dbLogTitle) ErrInvalidTableAccessGrant = fmt.Errorf("%s 权限错误:授权参数无效", dbLogTitle) )
var New app
Functions ¶
This section is empty.
Types ¶
type AlterTablePlan ¶
type AlterTablePlan struct {
// AddColumns 向表添加新列。新列可携带 Default 值,对已有行自动回填。
// 若列名已存在则返回 ErrColumnAlreadyExists。
AddColumns []Column `json:"addColumns,omitempty"`
// DropColumns 按列名删除列,已有行的对应字段也会被清除。
// 不允许删除主键列(返回 ErrCannotDropPrimaryKey)。
// 若列名不存在则返回 ErrColumnNotFound。
DropColumns []string `json:"dropColumns,omitempty"`
// AddIndexes 为已有列添加普通索引(将列的 Indexed 置为 true)。
AddIndexes []string `json:"addIndexes,omitempty"`
// DropIndexes 移除已有列的普通索引(将列的 Indexed 置为 false)。
// 若该列同时是唯一索引,唯一索引不受影响。
DropIndexes []string `json:"dropIndexes,omitempty"`
// AddUniques 为已有列添加唯一索引(同时隐式将 Indexed 置为 true)。
AddUniques []string `json:"addUniques,omitempty"`
// DropUniques 移除已有列的唯一索引(将列的 Unique 置为 false)。
// 普通索引标志 Indexed 不受影响。
DropUniques []string `json:"dropUniques,omitempty"`
// AddForeignKeys 新增外键链路定义。
// 会自动将外键字段标记为 Indexed(由 normalizeSchema 处理)。
AddForeignKeys []ForeignKey `json:"addForeignKeys,omitempty"`
// DropForeignKeys 删除外键链路,支持通过 Name / Field / Alias 指定。
DropForeignKeys []string `json:"dropForeignKeys,omitempty"`
}
AlterTablePlan 描述一次 ALTER TABLE 操作的完整计划。 所有字段均可按需组合,同一次调用可包含多个操作。
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 AuthenticatedUser ¶
type AuthenticatedUser struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"displayName,omitempty"`
Status string `json:"status,omitempty"`
IsAdmin bool `json:"isAdmin"`
Roles []string `json:"roles,omitempty"`
Permissions []string `json:"permissions,omitempty"`
}
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) AlterTable ¶
func (db *SimpleDB) AlterTable(plan AlterTablePlan) error
AlterTable 根据 AlterTablePlan 修改表结构,操作按以下顺序执行:
- DropColumns → 2. AddColumns → 3. DropUniques → 4. DropIndexes →
- AddIndexes → 6. AddUniques
所有操作均在同一个锁保护下原子完成。已有行会按计划迁移(删列/补默认值)。 操作完成后自动重建二级索引。
func (*SimpleDB) AutoMigrate ¶
func (db *SimpleDB) AutoMigrate(schema TableSchema) error
AutoMigrate 以**保守策略**将表结构迁移到目标 Schema:
- 若当前无 Schema → 等同于 CreateTable(严格建表)
- 若当前 Schema 与目标完全一致 → 幂等,无操作
- 若当前 Schema 与目标不同 → **只新增**列/索引,绝不删除任何列或索引
适合应用启动时安全地追加字段,不会破坏现有数据。 若需要删除列,请手动调用 AlterTable 或使用 SyncSchema。
func (*SimpleDB) BeginReadOnlyTx ¶
func (*SimpleDB) BeginTxWithOptions ¶
func (*SimpleDB) Configure ¶
func (db *SimpleDB) Configure(schema TableSchema) (err error)
func (*SimpleDB) CreateTable ¶
func (db *SimpleDB) CreateTable(schema TableSchema) error
CreateTable 以严格模式创建表结构。 与 Configure 的区别:若表已存在 Schema,即使完全相同也返回 ErrSchemaAlreadyExists。 适用于明确的 DDL 创建语义(CREATE TABLE IF NOT EXISTS 请先调用 HasSchema 判断)。
func (*SimpleDB) DeleteRows ¶
func (*SimpleDB) DropTable ¶
DropTable 删除表的所有行数据以及 Schema 元信息,但不关闭数据库文件。 调用后该 SimpleDB 实例进入"无 Schema"状态,可再次调用 CreateTable 重建。
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) QueryCascade ¶
func (db *SimpleDB) QueryCascade(query CascadeQuery) ([]map[string]any, error)
QueryCascade 返回级联查询的对象结构(非 JSON)。 如需序列化输出,可使用 QueryCascadeJSON。
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) SchemaDiff ¶
func (db *SimpleDB) SchemaDiff(target TableSchema) (*AlterTablePlan, bool, error)
SchemaDiff 计算从当前 Schema 迁移到目标 Schema 所需的最小 AlterTablePlan。 若当前无 Schema,返回 (nil, false, nil),表示需要 CreateTable 而非 Alter。 若当前 Schema 与目标完全一致,返回 (nil, true, nil)(第二个值表示 Schema 已存在)。 否则返回 (plan, true, nil),plan 包含所有需要执行的变更。
注意:SchemaDiff 检测以下变更:
- 新增列(target 有、current 无)
- 删除列(current 有、target 无,主键列除外)
- 索引变更(Indexed / Unique 标志的开启与关闭)
- 外键变更(ForeignKeys 增删)
SchemaDiff 不检测列类型变更、列约束变更、主键变更。 如需这些变更,请直接调用 AlterTable 或重建表(DropTable + CreateTable)。
func (*SimpleDB) SetAttrs ¶
func (db *SimpleDB) SetAttrs(attrs ...SchemaAttributer) *SimpleDB
func (*SimpleDB) SyncSchema ¶
func (db *SimpleDB) SyncSchema(schema TableSchema) error
SyncSchema 以**完全同步策略**将表结构精确对齐到目标 Schema:
- 若当前无 Schema → 等同于 CreateTable
- 若当前 Schema 与目标完全一致 → 幂等,无操作
- 若当前 Schema 与目标不同 → 全量同步,包括**删除**目标中已移除的列/索引
⚠️ 删列是不可逆操作,会永久丢失该列的所有数据,请谨慎使用。 若只需安全追加字段,请使用 AutoMigrate。
func (*SimpleDB) TruncateTable ¶
TruncateTable 清空表中所有行数据,保留 Schema 定义及索引结构。 若表配置了自增主键,自增序列计数器也会重置为 0。
type TableAccessGrant ¶
type TableAccessGrant struct {
TableName string `json:"tableName"`
GranteeUserID string `json:"granteeUserId"`
Scope TableAccessScope `json:"scope"`
OwnerApproved bool `json:"ownerApproved"`
AdminApproved bool `json:"adminApproved"`
}
type TableAccessScope ¶
type TableAccessScope string
const ( TableAccessScopeDML TableAccessScope = "dml" TableAccessScopeDDL TableAccessScope = "ddl" )
type TableSchema ¶
type TableSchema struct {
Columns []Column `json:"columns"`
ForeignKeys []ForeignKey `json:"foreignKeys,omitempty"`
PrimaryKey string `json:"primaryKey"`
AutoIncrement bool `json:"autoIncrement,omitempty"`
}
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
func (*Tx) DeleteRows ¶
func (*Tx) FindByConditions ¶
func (tx *Tx) FindByConditions(conditions []QueryCondition) ([]Row, error)
func (*Tx) RemoveByCondition ¶
func (tx *Tx) RemoveByCondition(conditions ...QueryCondition) (int, error)
func (*Tx) RemoveOneByCondition ¶
func (tx *Tx) RemoveOneByCondition(conditions ...QueryCondition) (bool, error)
type TxIsolationLevel ¶
type TxIsolationLevel string
const ( TxIsolationSnapshot TxIsolationLevel = "snapshot" TxIsolationReadCommitted TxIsolationLevel = "read_committed" )
type TxOptions ¶
type TxOptions struct {
ReadOnly bool
Isolation TxIsolationLevel
}