Skip to content

Commit

Permalink
fix issues for package contrib/drivers/dm (#3157)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhonghuaxunGM authored Dec 20, 2023
1 parent 7f9467d commit d60d767
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 140 deletions.
17 changes: 14 additions & 3 deletions contrib/drivers/dm/dm.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
}
// Data Source Name of DM8:
// dm://userName:password@ip:port/dbname
// dm://userName:password@DW/dbname?DW=(192.168.1.1:5236,192.168.1.2:5236)
var domain string
if config.Port != "" {
domain = fmt.Sprintf("%s:%s", config.Host, config.Port)
} else {
domain = config.Host
}
source = fmt.Sprintf(
"dm://%s:%s@%s:%s/%s?charset=%s&schema=%s",
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset, config.Name,
"dm://%s:%s@%s/%s?charset=%s&schema=%s",
config.User, config.Pass, domain, config.Name, config.Charset, config.Name,
)
// Demo of timezone setting:
// &loc=Asia/Shanghai
Expand Down Expand Up @@ -210,7 +217,11 @@ func (d *Driver) DoFilter(ctx context.Context, link gdb.Link, sql string, args [
// TODO The current approach is too rough. We should deal with the GROUP_CONCAT function and the parsing of the index field from within the select from match.
// (GROUP_CONCAT DM does not approve; index cannot be used as a query column name, and security characters need to be added, such as "index")
l, r := d.GetChars()
newSql = gstr.ReplaceI(newSql, "INDEX", l+"INDEX"+r)
if strings.Contains(newSql, "INDEX") || strings.Contains(newSql, "index") {
if !(strings.Contains(newSql, "_INDEX") || strings.Contains(newSql, "_index")) {
newSql = gstr.ReplaceI(newSql, "INDEX", l+"INDEX"+r)
}
}

// TODO i tried to do but it never work:
// array, err := gregex.MatchAllString(`SELECT (.*INDEX.*) FROM .*`, newSql)
Expand Down
141 changes: 85 additions & 56 deletions contrib/drivers/dm/dm_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,44 @@ import (
)

var (
db gdb.DB
dblink gdb.DB
dbErr gdb.DB
ctx context.Context
)

const (
db gdb.DB
dblink gdb.DB
dbErr gdb.DB
ctx context.Context
TableSize = 10

// TableName = "inf_group"
// TableNamePrefix = "t_"
// TestSchema = "SYSDBADP"
)

const (
TestDbIP = "127.0.0.1"
TestDbPort = "5236"
TestDbUser = "SYSDBA"
TestDbPass = "SYSDBA001"
TestDbName = "SYSDBA"
TestDbType = "dm"
TestDBHost = "127.0.0.1"
TestDBPort = "5236"
TestDBUser = "SYSDBA"
TestDBPass = "SYSDBA001"
TestDBName = "SYSDBA"
TestDBType = "dm"
TestCharset = "utf8"
)

type User struct {
ID int64 `orm:"id"`
AccountName string `orm:"account_name"`
PwdReset int64 `orm:"pwd_reset"`
AttrIndex int64 `orm:"attr_index"`
Enabled int64 `orm:"enabled"`
Deleted int64 `orm:"deleted"`
CreatedBy string `orm:"created_by"`
CreatedTime time.Time `orm:"created_time"`
UpdatedBy string `orm:"updated_by"`
UpdatedTime time.Time `orm:"updated_time"`
}

func init() {
node := gdb.ConfigNode{
Host: TestDbIP,
Port: TestDbPort,
User: TestDbUser,
Pass: TestDbPass,
Name: TestDbName,
Type: TestDbType,
Host: TestDBHost,
Port: TestDBPort,
User: TestDBUser,
Pass: TestDBPass,
Name: TestDBName,
Type: TestDBType,
Role: "master",
Charset: TestCharset,
Weight: 1,
Expand All @@ -62,22 +68,23 @@ func init() {
UpdatedAt: "updated_time",
}

// todo
nodeLink := gdb.ConfigNode{
Type: TestDbType,
Name: TestDbName,
Type: TestDBType,
Name: TestDBName,
Link: fmt.Sprintf(
"dm:%s:%s@tcp(%s:%s)/%s?charset=%s",
TestDbUser, TestDbPass, TestDbIP, TestDbPort, TestDbName, TestCharset,
TestDBUser, TestDBPass, TestDBHost, TestDBPort, TestDBName, TestCharset,
),
}

nodeErr := gdb.ConfigNode{
Host: TestDbIP,
Port: TestDbPort,
User: TestDbUser,
Host: TestDBHost,
Port: TestDBPort,
User: TestDBUser,
Pass: "1234",
Name: TestDbName,
Type: TestDbType,
Name: TestDBName,
Type: TestDBType,
Role: "master",
Charset: TestCharset,
Weight: 1,
Expand Down Expand Up @@ -107,6 +114,23 @@ func init() {
ctx = context.Background()
}

func dropTable(table string) {
count, err := db.GetCount(
ctx,
"SELECT COUNT(*) FROM all_tables WHERE owner = ? And table_name= ?", TestDBName, strings.ToUpper(table),
)
if err != nil {
gtest.Fatal(err)
}

if count == 0 {
return
}
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
gtest.Fatal(err)
}
}

func createTable(table ...string) (name string) {
if len(table) > 0 {
name = table[0]
Expand All @@ -124,6 +148,7 @@ func createTable(table ...string) (name string) {
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
"ENABLED" INT DEFAULT 1 NOT NULL,
"DELETED" INT DEFAULT 0 NOT NULL,
"ATTR_INDEX" INT DEFAULT 0 ,
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
Expand All @@ -136,18 +161,6 @@ NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
return
}

type User struct {
ID int64 `orm:"id"`
AccountName string `orm:"account_name"`
PwdReset int64 `orm:"pwd_reset"`
Enabled int64 `orm:"enabled"`
Deleted int64 `orm:"deleted"`
CreatedBy string `orm:"created_by"`
CreatedTime time.Time `orm:"created_time"`
UpdatedBy string `orm:"updated_by"`
UpdatedTime time.Time `orm:"updated_time"`
}

func createInitTable(table ...string) (name string) {
name = createTable(table...)
array := garray.New(true)
Expand All @@ -156,10 +169,11 @@ func createInitTable(table ...string) (name string) {
"id": i,
"account_name": fmt.Sprintf(`name_%d`, i),
"pwd_reset": 0,
"attr_index": i,
"create_time": gtime.Now().String(),
})
}
result, err := db.Schema(TestDbName).Insert(context.Background(), name, array.Slice())
result, err := db.Schema(TestDBName).Insert(context.Background(), name, array.Slice())
gtest.Assert(err, nil)

n, e := result.RowsAffected()
Expand All @@ -168,19 +182,34 @@ func createInitTable(table ...string) (name string) {
return
}

func dropTable(table string) {
count, err := db.GetCount(
ctx,
"SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = ?", strings.ToUpper(table),
)
if err != nil {
gtest.Fatal(err)
func createTableFalse(table ...string) (name string, err error) {
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf("random_%d", gtime.Timestamp())
}

if count == 0 {
return
}
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
gtest.Fatal(err)
dropTable(name)

if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE "%s"
(
"ID" BIGINT NOT NULL,
"ACCOUNT_NAME" VARCHAR(128) DEFAULT '' NOT NULL,
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
"ENABLED" INT DEFAULT 1 NOT NULL,
"DELETED" INT DEFAULT 0 NOT NULL,
"INDEX" INT DEFAULT 0 ,
"ATTR_INDEX" INT DEFAULT 0 ,
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
"UPDATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
`, name)); err != nil {
// gtest.Fatal(err)
return name, fmt.Errorf("createTableFalse")
}

return name, nil
}
Loading

0 comments on commit d60d767

Please sign in to comment.