starrocks.go 2.5 KB
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))
}