-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from wubin1989/main
...
- Loading branch information
Showing
38 changed files
with
111,407 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ®istry{ | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
) |
Oops, something went wrong.