Compare commits
No commits in common. "6f4d44dc3b90d158b3765fb0e86a228c93c95de2" and "b2136e170e397d2d141d1421571315928617b4d2" have entirely different histories.
6f4d44dc3b
...
b2136e170e
63
DB.go
63
DB.go
@ -349,69 +349,6 @@ func GetDB(name string, logger *log.Logger) *DB {
|
||||
return getDB(name, logger, true)
|
||||
}
|
||||
|
||||
// Sync 同步数据库结构 (使用默认实例 "default")
|
||||
func Sync(desc string) error {
|
||||
d := GetDB("default", nil)
|
||||
if d == nil {
|
||||
return errors.New("default db not configured")
|
||||
}
|
||||
return d.Sync(desc)
|
||||
}
|
||||
|
||||
// Insert 插入数据 (使用默认实例 "default")
|
||||
func Insert(table string, data any) *ExecResult {
|
||||
d := GetDB("default", nil)
|
||||
if d == nil {
|
||||
return &ExecResult{Error: errors.New("default db not configured")}
|
||||
}
|
||||
return d.Insert(table, data)
|
||||
}
|
||||
|
||||
// Update 更新数据 (使用默认实例 "default")
|
||||
func Update(table string, data any, conditions string, args ...any) *ExecResult {
|
||||
d := GetDB("default", nil)
|
||||
if d == nil {
|
||||
return &ExecResult{Error: errors.New("default db not configured")}
|
||||
}
|
||||
return d.Update(table, data, conditions, args...)
|
||||
}
|
||||
|
||||
// Delete 删除数据 (使用默认实例 "default")
|
||||
func Delete(table string, conditions string, args ...any) *ExecResult {
|
||||
d := GetDB("default", nil)
|
||||
if d == nil {
|
||||
return &ExecResult{Error: errors.New("default db not configured")}
|
||||
}
|
||||
return d.Delete(table, conditions, args...)
|
||||
}
|
||||
|
||||
// Query 查询数据 (使用默认实例 "default")
|
||||
func Query(query string, args ...any) *QueryResult {
|
||||
d := GetDB("default", nil)
|
||||
if d == nil {
|
||||
return &QueryResult{Error: errors.New("default db not configured")}
|
||||
}
|
||||
return d.Query(query, args...)
|
||||
}
|
||||
|
||||
// Exec 执行 SQL (使用默认实例 "default")
|
||||
func Exec(query string, args ...any) *ExecResult {
|
||||
d := GetDB("default", nil)
|
||||
if d == nil {
|
||||
return &ExecResult{Error: errors.New("default db not configured")}
|
||||
}
|
||||
return d.Exec(query, args...)
|
||||
}
|
||||
|
||||
// Begin 开始事务 (使用默认实例 "default")
|
||||
func Begin() *Tx {
|
||||
d := GetDB("default", nil)
|
||||
if d == nil {
|
||||
return &Tx{Error: errors.New("default db not configured")}
|
||||
}
|
||||
return d.Begin()
|
||||
}
|
||||
|
||||
func getDB(name string, logger *log.Logger, useCache bool) *DB {
|
||||
if logger == nil {
|
||||
logger = log.DefaultLogger
|
||||
|
||||
10
Log.go
10
Log.go
@ -6,11 +6,11 @@ import (
|
||||
)
|
||||
|
||||
type DBLog struct {
|
||||
DbType string `log:"pos:7,color:blue"`
|
||||
Dsn string `log:"pos:8,color:gray,withoutkey:true"`
|
||||
Query string `log:"pos:9,color:cyan"`
|
||||
QueryArgs string `log:"pos:10,color:gray"`
|
||||
UsedTime float32 `log:"pos:11,format:%.2fms"`
|
||||
DbType string `log:"pos:6,color:blue"`
|
||||
Dsn string `log:"pos:7,color:gray,withoutkey:true"`
|
||||
Query string `log:"pos:8,color:cyan"`
|
||||
QueryArgs string `log:"pos:9,color:gray"`
|
||||
UsedTime float32 `log:"pos:10,format:%.2fms"`
|
||||
}
|
||||
|
||||
func (l *DBLog) Reset() {
|
||||
|
||||
25
Schema.go
25
Schema.go
@ -424,40 +424,34 @@ func (db *DB) CheckTable(table *TableStruct) error {
|
||||
tmpFields := []struct {
|
||||
Name string
|
||||
Type string
|
||||
Notnull int
|
||||
Notnull bool
|
||||
Dflt_value any
|
||||
Pk int
|
||||
Pk bool
|
||||
}{}
|
||||
if err := db.Query("PRAGMA table_info(" + db.Quote(table.Name) + ")").To(&tmpFields); err != nil {
|
||||
return err
|
||||
}
|
||||
db.Query("PRAGMA table_info(" + db.Quote(table.Name) + ")").To(&tmpFields)
|
||||
for _, f := range tmpFields {
|
||||
oldFieldList = append(oldFieldList, &tableFieldDesc{
|
||||
Field: f.Name,
|
||||
Type: f.Type,
|
||||
Null: cast.If(f.Notnull != 0, "NO", "YES"),
|
||||
Key: cast.If(f.Pk != 0, "PRI", ""),
|
||||
Null: cast.If(f.Notnull, "NO", "YES"),
|
||||
Key: cast.If(f.Pk, "PRI", ""),
|
||||
Default: cast.String(f.Dflt_value),
|
||||
})
|
||||
}
|
||||
tmpIndexes := []struct {
|
||||
Name string
|
||||
Unique int
|
||||
Unique bool
|
||||
Origin string
|
||||
Partial int
|
||||
}{}
|
||||
if err := db.Query("PRAGMA index_list(" + db.Quote(table.Name) + ")").To(&tmpIndexes); err != nil {
|
||||
return err
|
||||
}
|
||||
db.Query("PRAGMA index_list(" + db.Quote(table.Name) + ")").To(&tmpIndexes)
|
||||
for _, i := range tmpIndexes {
|
||||
tmpIndexInfo := []struct {
|
||||
Name string
|
||||
Seqno int
|
||||
Cid int
|
||||
}{}
|
||||
if err := db.Query("PRAGMA index_info(" + db.Quote(i.Name) + ")").To(&tmpIndexInfo); err != nil {
|
||||
return err
|
||||
}
|
||||
db.Query("PRAGMA index_info(" + db.Quote(i.Name) + ")").To(&tmpIndexInfo)
|
||||
if len(tmpIndexInfo) > 0 {
|
||||
oldIndexInfos = append(oldIndexInfos, &tableKeyDesc{
|
||||
Key_name: i.Name,
|
||||
@ -521,9 +515,6 @@ func (db *DB) CheckTable(table *TableStruct) error {
|
||||
for keyId := range oldIndexes {
|
||||
if keyId != "PRIMARY" && !isPostgres && strings.ToLower(keySetFields[keyId]) != strings.ToLower(oldIndexes[keyId]) {
|
||||
if strings.HasPrefix(db.Config.Type, "sqlite") {
|
||||
if strings.HasPrefix(keyId, "sqlite_autoindex_") {
|
||||
continue
|
||||
}
|
||||
actions = append(actions, "DROP INDEX "+db.Quote(keyId))
|
||||
} else {
|
||||
actions = append(actions, "DROP KEY "+db.Quote(keyId))
|
||||
|
||||
4
go.mod
4
go.mod
@ -3,10 +3,9 @@ module apigo.cc/go/db
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
apigo.cc/go/cast v1.3.2
|
||||
apigo.cc/go/cast v1.3.0
|
||||
apigo.cc/go/config v1.3.0
|
||||
apigo.cc/go/crypto v1.3.0
|
||||
apigo.cc/go/file v1.3.1
|
||||
apigo.cc/go/id v1.3.0
|
||||
apigo.cc/go/log v1.3.2
|
||||
apigo.cc/go/rand v1.3.0
|
||||
@ -21,6 +20,7 @@ require (
|
||||
|
||||
require (
|
||||
apigo.cc/go/encoding v1.3.0 // indirect
|
||||
apigo.cc/go/file v1.3.0 // indirect
|
||||
filippo.io/edwards25519 v1.2.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/gomodule/redigo v2.0.0+incompatible // indirect
|
||||
|
||||
8
go.sum
8
go.sum
@ -1,13 +1,13 @@
|
||||
apigo.cc/go/cast v1.3.2 h1:hh9MWDSwh3T/kQdCHjFpjDwHrh2A05Q4wt1AAWs8NBI=
|
||||
apigo.cc/go/cast v1.3.2/go.mod h1:lGlwImiOvHxG7buyMWhFzcdvQzmSaoKbmr7bcDfUpHk=
|
||||
apigo.cc/go/cast v1.3.0 h1:ZTcLYijkqZjSWSCSpJUWMfzJYeJKbwKxquKkPrFsROQ=
|
||||
apigo.cc/go/cast v1.3.0/go.mod h1:lGlwImiOvHxG7buyMWhFzcdvQzmSaoKbmr7bcDfUpHk=
|
||||
apigo.cc/go/config v1.3.0 h1:TwI3bv3D+BJrAnFx+o62HQo3FarY2Ge3SCGsKchFYGg=
|
||||
apigo.cc/go/config v1.3.0/go.mod h1:88lqKEBXlIExFKt1geLONVLYyM+QhRVpBe0ok3OEvjI=
|
||||
apigo.cc/go/crypto v1.3.0 h1:rGRrrb5O+4M50X5hVUmJQbXx3l87zzlcgzGtUvZrZL8=
|
||||
apigo.cc/go/crypto v1.3.0/go.mod h1:uSCcmbcFoiltUPMQTSuqmU9nfKEH/lRs7nQ7aa3Z4Mc=
|
||||
apigo.cc/go/encoding v1.3.0 h1:8jqNHoZBR8vOU/BGsLFebfp1Txa1UxDRpd7YwzIFLJs=
|
||||
apigo.cc/go/encoding v1.3.0/go.mod h1:kT/uUJiuAOkZ4LzUWrUtk/I0iL1D8aatvD+59bDnHBo=
|
||||
apigo.cc/go/file v1.3.1 h1:qHgiJsn1K9DazWRrPoHVnXtp6hDGGsUpAE/4G1bFXqY=
|
||||
apigo.cc/go/file v1.3.1/go.mod h1:pYHBlB/XwsrnWpEh7GIFpbiqobrExfiB+rEN8V2d2kY=
|
||||
apigo.cc/go/file v1.3.0 h1:xG9FcY3Rv6Br83r9pq9QsIXFrplx4g8ITOkHSzfzXRg=
|
||||
apigo.cc/go/file v1.3.0/go.mod h1:pYHBlB/XwsrnWpEh7GIFpbiqobrExfiB+rEN8V2d2kY=
|
||||
apigo.cc/go/id v1.3.0 h1:Tr2Yj0Rl19lfwW5wBTJ407o/zgo2oVRLE20WWEgJzdE=
|
||||
apigo.cc/go/id v1.3.0/go.mod h1:AFH3kMFwENfXNyijnAFWEhSF1o3y++UBPem1IUlrcxA=
|
||||
apigo.cc/go/log v1.3.2 h1:/m3V4MnlYnCG4XPHpWDsa4cw5suMaDVY1SgaVyjnBSo=
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user