starrocks.go
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package starrocks
import (
"github.com/linmadan/egglib-go/utils/json"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
//rawlog "log"
//"os"
"reflect"
"time"
)
var DB *gorm.DB
func Init() error {
var err error
newLogger := logger.New(
log.GormLogWriter{Module: "【StarRocks】"}, //rawlog.New(os.Stdout, "\r\n", rawlog.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Info, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)
DB, err = gorm.Open(mysql.Open(constant.STARROCKS_MYSQL_DATA_SOURCE), &gorm.Config{Logger: newLogger, PrepareStmt: false})
//Test1()
return err
}
func Test() {
rows, err := DB.Table("sales_performance_area_categorys").Rows()
if err != nil {
log.Logger.Error(err.Error())
}
defer rows.Close()
cols, err := rows.Columns()
if err != nil {
log.Logger.Error(err.Error())
}
var results [][]interface{}
for rows.Next() {
// 1
var row = make([]interface{}, 0)
generic := reflect.TypeOf(row).Elem()
for _ = range cols {
row = append(row, reflect.New(generic).Interface())
}
err := rows.Scan(row...)
if err != nil {
log.Logger.Error(err.Error())
}
for i := range row {
row[i] = utils.AssertString(*(row[i].(*interface{})))
}
results = append(results, row)
// 2
//var row = make([]interface{}, len(cols))
//var rowp = make([]interface{}, len(cols))
//
//for i := range cols {
// rowp[i] = &row[i]
//}
//err := rows.Scan(rowp...)
//if err != nil {
// log.Logger.Error(err.Error())
//}
//results = append(results, rowp)
// 3
//var row = make([]interface{}, 0)
//var value string
//generic := reflect.TypeOf(value)
//
//for _ = range cols {
// row = append(row, reflect.New(generic).Interface())
//}
//err := rows.Scan(row...)
//if err != nil {
// log.Logger.Error(err.Error())
//}
//results = append(results, row)
}
log.Logger.Debug(json.MarshalToString(results))
}
func Test1() {
rows, err := DB.Table("sales_performance_area_categorys").Rows()
if err != nil {
log.Logger.Error(err.Error())
}
data, _ := ScanRows(rows)
log.Logger.Debug(json.MarshalToString(data))
}