db

package
v3.3.6 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MySQLResetRootPassword

func MySQLResetRootPassword(ctx context.Context, password, root string) (err error)

MySQLResetRootPassword 重置 MySQL root密码

func MySQLSocket added in v3.3.0

func MySQLSocket(root string) string

MySQLSocket 探测本地 MySQL 的 unix socket 路径

func PostgresPort added in v3.3.2

func PostgresPort(root string) uint

PostgresPort 读取本地 PostgreSQL 端口

Types

type ClickHouse added in v3.2.0

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

ClickHouse 通过 HTTP API 操作 ClickHouse

func NewClickHouse added in v3.2.0

func NewClickHouse(ctx context.Context, username, password, address string) (*ClickHouse, error)

NewClickHouse 创建 ClickHouse 连接(HTTP API)

func (*ClickHouse) Close added in v3.2.0

func (r *ClickHouse) Close()

func (*ClickHouse) DatabaseCreate added in v3.2.0

func (r *ClickHouse) DatabaseCreate(ctx context.Context, name string) error

func (*ClickHouse) DatabaseDrop added in v3.2.0

func (r *ClickHouse) DatabaseDrop(ctx context.Context, name string) error

func (*ClickHouse) DatabaseExists added in v3.2.0

func (r *ClickHouse) DatabaseExists(ctx context.Context, name string) (bool, error)

func (*ClickHouse) DatabaseSize added in v3.2.0

func (r *ClickHouse) DatabaseSize(ctx context.Context, name string) (int64, error)

func (*ClickHouse) Databases added in v3.2.0

func (r *ClickHouse) Databases(ctx context.Context) ([]Database, error)

func (*ClickHouse) Exec added in v3.2.0

func (r *ClickHouse) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*ClickHouse) Ping added in v3.2.0

func (r *ClickHouse) Ping(ctx context.Context) error

func (*ClickHouse) Prepare added in v3.2.0

func (r *ClickHouse) Prepare(ctx context.Context, query string) (*sql.Stmt, error)

func (*ClickHouse) PrivilegesGrant added in v3.2.0

func (r *ClickHouse) PrivilegesGrant(ctx context.Context, user, database string, host ...string) error

func (*ClickHouse) PrivilegesRevoke added in v3.2.0

func (r *ClickHouse) PrivilegesRevoke(ctx context.Context, user, database string, host ...string) error

func (*ClickHouse) Query added in v3.2.0

func (r *ClickHouse) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*ClickHouse) QueryRow added in v3.2.0

func (r *ClickHouse) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

func (*ClickHouse) UserCreate added in v3.2.0

func (r *ClickHouse) UserCreate(ctx context.Context, user, password string, host ...string) error

func (*ClickHouse) UserDrop added in v3.2.0

func (r *ClickHouse) UserDrop(ctx context.Context, user string, host ...string) error

func (*ClickHouse) UserPassword added in v3.2.0

func (r *ClickHouse) UserPassword(ctx context.Context, user, password string, host ...string) error

func (*ClickHouse) UserPrivileges added in v3.2.0

func (r *ClickHouse) UserPrivileges(ctx context.Context, user string, host ...string) ([]string, error)

func (*ClickHouse) Users added in v3.2.0

func (r *ClickHouse) Users(ctx context.Context) ([]User, error)

type Database

type Database struct {
	Name      string `json:"name"`      // 数据库名
	Owner     string `json:"owner"`     // 所有者,MySQL 这个字段为空
	CharSet   string `json:"char_set"`  // 字符集,PG 里面对应 Encoding
	Collation string `json:"collation"` // 校对集,PG 这个字段为空
	Comment   string `json:"comment"`
}

type ESDocument added in v3.2.0

type ESDocument struct {
	ID     string `json:"id"`
	Index  string `json:"index"`
	Source string `json:"source"`
}

ESDocument 文档信息

type ESIndex added in v3.2.0

type ESIndex struct {
	Name      string `json:"name"`
	Health    string `json:"health"`
	Status    string `json:"status"`
	DocsCount string `json:"docs_count"`
	StoreSize string `json:"store_size"`
}

ESIndex 索引信息

type Elasticsearch added in v3.2.0

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

Elasticsearch 通过 REST API 操作 Elasticsearch/OpenSearch

func NewElasticsearch added in v3.2.0

func NewElasticsearch(ctx context.Context, address, username, password string) (*Elasticsearch, error)

NewElasticsearch 创建 Elasticsearch 连接

func (*Elasticsearch) Close added in v3.2.0

func (r *Elasticsearch) Close()

func (*Elasticsearch) DocumentCreate added in v3.2.0

func (r *Elasticsearch) DocumentCreate(ctx context.Context, index, body string) error

DocumentCreate 创建文档(自动生成 ID)

func (*Elasticsearch) DocumentDelete added in v3.2.0

func (r *Elasticsearch) DocumentDelete(ctx context.Context, index, id string) error

DocumentDelete 删除文档

func (*Elasticsearch) DocumentGet added in v3.2.0

func (r *Elasticsearch) DocumentGet(ctx context.Context, index, id string) (*ESDocument, error)

DocumentGet 获取文档

func (*Elasticsearch) DocumentUpdate added in v3.2.0

func (r *Elasticsearch) DocumentUpdate(ctx context.Context, index, id, body string) error

DocumentUpdate 更新文档

func (*Elasticsearch) IndexCreate added in v3.2.0

func (r *Elasticsearch) IndexCreate(ctx context.Context, name string) error

IndexCreate 创建索引

func (*Elasticsearch) IndexDelete added in v3.2.0

func (r *Elasticsearch) IndexDelete(ctx context.Context, name string) error

IndexDelete 删除索引

func (*Elasticsearch) Indices added in v3.2.0

func (r *Elasticsearch) Indices(ctx context.Context) ([]ESIndex, error)

Indices 获取所有索引

func (*Elasticsearch) Ping added in v3.2.0

func (r *Elasticsearch) Ping(ctx context.Context) error

func (*Elasticsearch) Search added in v3.2.0

func (r *Elasticsearch) Search(ctx context.Context, index, query string, page, pageSize int) ([]ESDocument, int64, error)

Search 搜索文档

type MongoDB added in v3.2.0

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

MongoDB 通过 mongosh CLI 操作 MongoDB

func NewMongoDB added in v3.2.0

func NewMongoDB(ctx context.Context, username, password, address string) (*MongoDB, error)

NewMongoDB 创建 MongoDB 连接

func (*MongoDB) Close added in v3.2.0

func (r *MongoDB) Close()

func (*MongoDB) DatabaseCreate added in v3.2.0

func (r *MongoDB) DatabaseCreate(ctx context.Context, name string) error

DatabaseCreate 创建数据库(MongoDB 通过创建集合来显式创建数据库)

func (*MongoDB) DatabaseDrop added in v3.2.0

func (r *MongoDB) DatabaseDrop(ctx context.Context, name string) error

DatabaseDrop 删除数据库

func (*MongoDB) Databases added in v3.2.0

func (r *MongoDB) Databases(ctx context.Context) ([]MongoDatabase, error)

Databases 获取数据库列表

func (*MongoDB) Ping added in v3.2.0

func (r *MongoDB) Ping(ctx context.Context) error

func (*MongoDB) UserCreate added in v3.2.0

func (r *MongoDB) UserCreate(ctx context.Context, user, password, database string) error

UserCreate 创建用户

func (*MongoDB) UserDrop added in v3.2.0

func (r *MongoDB) UserDrop(ctx context.Context, user, database string) error

UserDrop 删除用户

func (*MongoDB) UserPassword added in v3.2.0

func (r *MongoDB) UserPassword(ctx context.Context, user, password string) error

UserPassword 修改用户密码

func (*MongoDB) Users added in v3.2.0

func (r *MongoDB) Users(ctx context.Context) ([]MongoUser, error)

Users 获取用户列表

type MongoDatabase added in v3.2.0

type MongoDatabase struct {
	Name       string `json:"name"`
	SizeOnDisk int64  `json:"size_on_disk"`
}

MongoDatabase MongoDB 数据库信息

type MongoUser added in v3.2.0

type MongoUser struct {
	User  string   `json:"user"`
	DB    string   `json:"db"`
	Roles []string `json:"roles"`
}

MongoUser MongoDB 用户信息

type MySQL

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

func (*MySQL) Close

func (r *MySQL) Close()

func (*MySQL) DatabaseCreate

func (r *MySQL) DatabaseCreate(ctx context.Context, name string) error

func (*MySQL) DatabaseDrop

func (r *MySQL) DatabaseDrop(ctx context.Context, name string) error

func (*MySQL) DatabaseExists

func (r *MySQL) DatabaseExists(ctx context.Context, name string) (bool, error)

func (*MySQL) DatabaseSize

func (r *MySQL) DatabaseSize(ctx context.Context, name string) (int64, error)

func (*MySQL) Databases

func (r *MySQL) Databases(ctx context.Context) ([]Database, error)

func (*MySQL) Exec

func (r *MySQL) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*MySQL) Ping

func (r *MySQL) Ping(ctx context.Context) error

func (*MySQL) Prepare

func (r *MySQL) Prepare(ctx context.Context, query string) (*sql.Stmt, error)

func (*MySQL) PrivilegesGrant

func (r *MySQL) PrivilegesGrant(ctx context.Context, user, database string, host ...string) error

func (*MySQL) PrivilegesRevoke

func (r *MySQL) PrivilegesRevoke(ctx context.Context, user, database string, host ...string) error

func (*MySQL) Query

func (r *MySQL) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*MySQL) QueryRow

func (r *MySQL) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

func (*MySQL) UserCreate

func (r *MySQL) UserCreate(ctx context.Context, user, password string, host ...string) error

func (*MySQL) UserDrop

func (r *MySQL) UserDrop(ctx context.Context, user string, host ...string) error

func (*MySQL) UserPassword

func (r *MySQL) UserPassword(ctx context.Context, user, password string, host ...string) error

func (*MySQL) UserPrivileges

func (r *MySQL) UserPrivileges(ctx context.Context, user string, host ...string) ([]string, error)

func (*MySQL) Users

func (r *MySQL) Users(ctx context.Context) ([]User, error)

type Operator

type Operator interface {
	Close()
	Ping(ctx context.Context) error

	Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRow(ctx context.Context, query string, args ...any) *sql.Row
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
	Prepare(ctx context.Context, query string) (*sql.Stmt, error)

	DatabaseCreate(ctx context.Context, name string) error
	DatabaseDrop(ctx context.Context, name string) error
	DatabaseExists(ctx context.Context, name string) (bool, error)
	DatabaseSize(ctx context.Context, name string) (int64, error)

	UserCreate(ctx context.Context, user, password string, host ...string) error
	UserDrop(ctx context.Context, user string, host ...string) error
	UserPassword(ctx context.Context, user, password string, host ...string) error
	UserPrivileges(ctx context.Context, user string, host ...string) ([]string, error)

	PrivilegesGrant(ctx context.Context, user, database string, host ...string) error
	PrivilegesRevoke(ctx context.Context, user, database string, host ...string) error

	Users(ctx context.Context) ([]User, error)
	Databases(ctx context.Context) ([]Database, error)
}

Operator 数据库操作句柄 所有方法都跟随 ctx 取消,这样 CLI 和停机路径能中断卡在锁等待上的 SQL; 需要整体完成的多步操作由调用方(biz 用例层)在事务边界断开取消链

func NewMySQL

func NewMySQL(ctx context.Context, username, password, address string, typ ...string) (Operator, error)

func NewPostgres

func NewPostgres(ctx context.Context, username, password, address string, port uint, database ...string) (Operator, error)

type Postgres

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

func (*Postgres) Close

func (r *Postgres) Close()

func (*Postgres) DatabaseComment

func (r *Postgres) DatabaseComment(ctx context.Context, name, comment string) error

func (*Postgres) DatabaseCreate

func (r *Postgres) DatabaseCreate(ctx context.Context, name string) error

func (*Postgres) DatabaseDrop

func (r *Postgres) DatabaseDrop(ctx context.Context, name string) error

func (*Postgres) DatabaseExists

func (r *Postgres) DatabaseExists(ctx context.Context, name string) (bool, error)

func (*Postgres) DatabaseSize

func (r *Postgres) DatabaseSize(ctx context.Context, name string) (int64, error)

func (*Postgres) Databases

func (r *Postgres) Databases(ctx context.Context) ([]Database, error)

func (*Postgres) Exec

func (r *Postgres) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*Postgres) Ping

func (r *Postgres) Ping(ctx context.Context) error

func (*Postgres) Prepare

func (r *Postgres) Prepare(ctx context.Context, query string) (*sql.Stmt, error)

func (*Postgres) PrivilegesGrant

func (r *Postgres) PrivilegesGrant(ctx context.Context, user, database string, host ...string) error

func (*Postgres) PrivilegesRevoke

func (r *Postgres) PrivilegesRevoke(ctx context.Context, user, database string, host ...string) error

func (*Postgres) Query

func (r *Postgres) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*Postgres) QueryRow

func (r *Postgres) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

func (*Postgres) UserCreate

func (r *Postgres) UserCreate(ctx context.Context, user, password string, host ...string) error

func (*Postgres) UserDrop

func (r *Postgres) UserDrop(ctx context.Context, user string, host ...string) error

func (*Postgres) UserPassword

func (r *Postgres) UserPassword(ctx context.Context, user, password string, host ...string) error

func (*Postgres) UserPrivileges

func (r *Postgres) UserPrivileges(ctx context.Context, user string, host ...string) ([]string, error)

func (*Postgres) Users

func (r *Postgres) Users(ctx context.Context) ([]User, error)

type Redis

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

func NewRedis

func NewRedis(ctx context.Context, username, password, address string) (*Redis, error)

func (*Redis) Clear

func (r *Redis) Clear() error

func (*Redis) Close

func (r *Redis) Close()

func (*Redis) Data

func (r *Redis) Data(page, pageSize int) ([]RedisKV, int, error)

func (*Redis) Database

func (r *Redis) Database() (int, error)

Database 获取数据库数量

func (*Redis) Del

func (r *Redis) Del(keys ...string) error

Del 删除 key

func (*Redis) Exec

func (r *Redis) Exec(command string, args ...any) (any, error)

func (*Redis) Expire

func (r *Redis) Expire(key string, ttl int64) error

Expire 设置 key 的 TTL,ttl 为 -1 时移除过期时间

func (*Redis) Get

func (r *Redis) Get(key string) (*RedisKV, error)

Get 获取单个 key 的完整信息

func (*Redis) Rename

func (r *Redis) Rename(oldKey, newKey string) error

Rename 重命名 key

func (*Redis) Search

func (r *Redis) Search(pattern string, page, pageSize int) ([]RedisKV, int, error)

Search 搜索匹配的 key,支持分页

func (*Redis) Select

func (r *Redis) Select(db int) error

func (*Redis) SetKey

func (r *Redis) SetKey(key, value, keyType string, ttl int64) error

SetKey 根据类型创建或设置 key

func (*Redis) Size

func (r *Redis) Size() (int, error)

type RedisKV

type RedisKV struct {
	Key       string    `json:"key"`
	Value     string    `json:"value"`
	Type      string    `json:"type"`
	Size      int64     `json:"size"`
	Length    int64     `json:"length"`
	TTL       int64     `json:"ttl"`
	UpdatedAt time.Time `json:"updated_at"`
}

type SQLite added in v3.2.0

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

SQLite 通过 database/sql 操作 SQLite 文件

func NewSQLite added in v3.2.0

func NewSQLite(ctx context.Context, path string) (*SQLite, error)

NewSQLite 打开 SQLite 数据库文件

func (*SQLite) Close added in v3.2.0

func (r *SQLite) Close()

func (*SQLite) Ping added in v3.2.0

func (r *SQLite) Ping(ctx context.Context) error

func (*SQLite) TableInfo added in v3.2.0

func (r *SQLite) TableInfo(ctx context.Context, name string) ([]SQLiteColumn, error)

TableInfo 获取表结构

func (*SQLite) Tables added in v3.2.0

func (r *SQLite) Tables(ctx context.Context) ([]string, error)

Tables 获取所有表

type SQLiteColumn added in v3.2.0

type SQLiteColumn struct {
	CID        int    `json:"cid"`
	Name       string `json:"name"`
	Type       string `json:"type"`
	NotNull    bool   `json:"not_null"`
	Default    string `json:"default"`
	PrimaryKey bool   `json:"primary_key"`
}

SQLiteColumn 表列信息

type User

type User struct {
	User   string   `json:"user"`   // 用户名,PG 里面对应 Role
	Host   string   `json:"host"`   // 主机,PG 这个字段为空
	Grants []string `json:"grants"` // 权限列表
}

Jump to

Keyboard shortcuts

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