Skip to content

Commit

Permalink
Merge pull request #178 from wubin1989/main
Browse files Browse the repository at this point in the history
...
  • Loading branch information
wubin1989 authored Dec 27, 2023
2 parents 51468c4 + 0173c44 commit 53c447b
Show file tree
Hide file tree
Showing 38 changed files with 111,407 additions and 3 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ example/ddl/dao/
example/doudou/dao/
**/cover.out
**/coverage.txt
gen
cmd/testdata/testsvc/cmd/cmd
coverage.out
resources
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/gorilla/handlers v1.5.1
github.com/hashicorp/go-sockaddr v1.0.2
github.com/hashicorp/golang-lru v0.5.4
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/manifoldco/promptui v0.9.0
github.com/mholt/archiver/v3 v3.5.1
github.com/morkid/gocache v1.0.0
Expand Down Expand Up @@ -93,7 +94,7 @@ require (
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1704 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/antlr/antlr4 v0.0.0-20200124162019-2d7f727a00b7 // indirect
github.com/antlr/antlr4 v0.0.0-20200124162019-2d7f727a00b7
github.com/beorn7/perks v1.0.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,8 @@ github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lithammer/shortuuid/v4 v4.0.0 h1:QRbbVkfgNippHOS8PXDkti4NaWeyYfcBTHtw7k08o4c=
github.com/lithammer/shortuuid/v4 v4.0.0/go.mod h1:Zs8puNcrvf2rV9rTH51ZLLcj7ZXqQI3lv67aw4KiB1Y=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down
138 changes: 138 additions & 0 deletions toolkit/dbvendor/ivendor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package dbvendor

import (
"context"
"database/sql"
"fmt"
"github.com/pkg/errors"
"github.com/unionj-cloud/go-doudou/v2/toolkit/errorx"
"github.com/unionj-cloud/go-doudou/v2/toolkit/templateutils"
"github.com/unionj-cloud/go-doudou/v2/toolkit/zlogger"
"gorm.io/gorm"
)

var Registry = &registry{
vendors: map[string]IVendor{},
}

type registry struct {
vendors map[string]IVendor
}

func (receiver *registry) Register(driver string, vendor IVendor) {
receiver.vendors[driver] = vendor
}

func (receiver *registry) GetVendor(driver string) IVendor {
vendor, ok := receiver.vendors[driver]
if !ok {
errorx.Panic(fmt.Sprintf("Unsupported driver %s", driver))
}
return vendor
}

type IVendor interface {
CreateTable(ctx context.Context, db *gorm.DB, t Table) error
DropTable(ctx context.Context, db *gorm.DB, t Table) error
ChangeColumn(ctx context.Context, db *gorm.DB, col Column) error
AddColumn(ctx context.Context, db *gorm.DB, col Column) error
DropColumn(ctx context.Context, db *gorm.DB, col Column) error
ToColumnType(goType string, autoincrementing bool) string

Insert(ctx context.Context, db *gorm.DB, dml DMLSchema, args ...interface{}) (int64, error)
Update(ctx context.Context, db *gorm.DB, dml DMLSchema, args ...interface{}) error
Delete(ctx context.Context, db *gorm.DB, dml DMLSchema, args ...interface{}) error
SelectById(ctx context.Context, db *gorm.DB, dml DMLSchema, args ...interface{}) (map[string]interface{}, error)
GetInsertStatement(dml DMLSchema) (statement string, err error)
GetUpdateStatement(dml DMLSchema) (statement string, err error)
}

type DMLSchema struct {
Schema string
TablePrefix string
TableName string
InsertColumns []Column
UpdateColumns []Column
Pk Column
}

// Column define a column
type Column struct {
TablePrefix string
Table string
Name string
OldName string
Type string
Default *string
Pk bool
Nullable bool
Unsigned bool
Autoincrement bool
Extra string
Comment string
// 关联表名
Foreign string
}

// Table defines a table
type Table struct {
Name string
Columns []Column
BizColumns []Column
Pk string
Joins []string
// 父表
Inherited string
}

func String(tmplname, tmpl string, data interface{}, pf PlaceholderFormat) (string, error) {
result, err := templateutils.String(tmplname, tmpl, data)
if err != nil {
return "", errors.WithStack(err)
}
if pf != nil {
result, err = pf.ReplacePlaceholders(result)
if err != nil {
return "", errors.WithStack(err)
}
}
zlogger.Info().Msg(result)
return result, nil
}

func StringBlock(tmplname, tmpl string, block string, data interface{}, pf PlaceholderFormat) (string, error) {
result, err := templateutils.StringBlock(tmplname, tmpl, block, data)
if err != nil {
return "", errors.WithStack(err)
}
if pf != nil {
result, err = pf.ReplacePlaceholders(result)
if err != nil {
return "", errors.WithStack(err)
}
}
zlogger.Info().Msg(result)
return result, nil
}

const (
// Update used for update_at column
Update = "on update CURRENT_TIMESTAMP"
)

func Scan(rows *sql.Rows, result *[]map[string]interface{}) {
fields, _ := rows.Columns()
for rows.Next() {
scans := make([]interface{}, len(fields))
data := make(map[string]interface{})

for i := range scans {
scans[i] = &scans[i]
}
rows.Scan(scans...)
for i, v := range scans {
data[fields[i]] = v
}
*result = append(*result, data)
}
}
46 changes: 46 additions & 0 deletions toolkit/dbvendor/mysql/columnenum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mysql

const (
// BitType bit
BitType = "BIT"
// TextType text
TextType = "TEXT"
// BlobType blob
BlobType = "BLOB"
// DateType date
DateType = "DATE"
// DatetimeType datatime
DatetimeType = "DATETIME"
// DecimalType decimal
DecimalType = "DECIMAL"
// DoubleType double
DoubleType = "DOUBLE"
// EnumType enum
EnumType = "ENUM"
// FloatType float
FloatType = "FLOAT"
// GeometryType geometry
GeometryType = "GEOMETRY"
// MediumintType medium int
MediumintType = "MEDIUMINT"
// JSONType json
JSONType = "JSON"
// IntType int
IntType = "INT"
// LongtextType long text
LongtextType = "LONGTEXT"
// LongblobType long blob
LongblobType = "LONGBLOB"
// BigintType big int
BigintType = "BIGINT"
// MediumtextType medium text
MediumtextType = "MEDIUMTEXT"
// MediumblobType medium blob
MediumblobType = "MEDIUMBLOB"
// SmallintType small int
SmallintType = "SMALLINT"
// TinyintType tiny int
TinyintType = "TINYINT"
// VarcharType varchar
VarcharType = "VARCHAR(255)"
)
Loading

0 comments on commit 53c447b

Please sign in to comment.