Compare commits
2 Commits
database/c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c2726db8a1 | |||
|
|
47c72651db |
@@ -191,6 +191,7 @@ type Logger_Logrus struct {
|
||||
TimestampFormat string `protobuf:"bytes,3,opt,name=timestamp_format,json=timestampFormat,proto3" json:"timestamp_format,omitempty"` // 定义时间戳格式,例如:"2006-01-02 15:04:05"
|
||||
DisableColors bool `protobuf:"varint,4,opt,name=disable_colors,json=disableColors,proto3" json:"disable_colors,omitempty"` // 不需要彩色日志
|
||||
DisableTimestamp bool `protobuf:"varint,5,opt,name=disable_timestamp,json=disableTimestamp,proto3" json:"disable_timestamp,omitempty"` // 不需要时间戳
|
||||
ForceColors bool `protobuf:"varint,6,opt,name=force_colors,json=forceColors,proto3" json:"force_colors,omitempty"` // 是否开启彩色日志
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -260,6 +261,13 @@ func (x *Logger_Logrus) GetDisableTimestamp() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (x *Logger_Logrus) GetForceColors() bool {
|
||||
if x != nil {
|
||||
return x.ForceColors
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Fluent
|
||||
type Logger_Fluent struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
@@ -447,7 +455,7 @@ var File_conf_v1_kratos_conf_logger_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_conf_v1_kratos_conf_logger_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
" conf/v1/kratos_conf_logger.proto\x12\x04conf\"\xc4\a\n" +
|
||||
" conf/v1/kratos_conf_logger.proto\x12\x04conf\"\xe7\a\n" +
|
||||
"\x06Logger\x12\x12\n" +
|
||||
"\x04type\x18\x01 \x01(\tR\x04type\x12'\n" +
|
||||
"\x03zap\x18\x02 \x01(\v2\x10.conf.Logger.ZapH\x00R\x03zap\x88\x01\x01\x120\n" +
|
||||
@@ -461,13 +469,14 @@ const file_conf_v1_kratos_conf_logger_proto_rawDesc = "" +
|
||||
"\bmax_size\x18\x03 \x01(\x05R\amaxSize\x12\x17\n" +
|
||||
"\amax_age\x18\x04 \x01(\x05R\x06maxAge\x12\x1f\n" +
|
||||
"\vmax_backups\x18\x05 \x01(\x05R\n" +
|
||||
"maxBackups\x1a\xbb\x01\n" +
|
||||
"maxBackups\x1a\xde\x01\n" +
|
||||
"\x06Logrus\x12\x14\n" +
|
||||
"\x05level\x18\x01 \x01(\tR\x05level\x12\x1c\n" +
|
||||
"\tformatter\x18\x02 \x01(\tR\tformatter\x12)\n" +
|
||||
"\x10timestamp_format\x18\x03 \x01(\tR\x0ftimestampFormat\x12%\n" +
|
||||
"\x0edisable_colors\x18\x04 \x01(\bR\rdisableColors\x12+\n" +
|
||||
"\x11disable_timestamp\x18\x05 \x01(\bR\x10disableTimestamp\x1a$\n" +
|
||||
"\x11disable_timestamp\x18\x05 \x01(\bR\x10disableTimestamp\x12!\n" +
|
||||
"\fforce_colors\x18\x06 \x01(\bR\vforceColors\x1a$\n" +
|
||||
"\x06Fluent\x12\x1a\n" +
|
||||
"\bendpoint\x18\x01 \x01(\tR\bendpoint\x1a\x82\x01\n" +
|
||||
"\x06Aliyun\x12\x1a\n" +
|
||||
|
||||
@@ -22,6 +22,7 @@ message Logger {
|
||||
string timestamp_format = 3; // 定义时间戳格式,例如:"2006-01-02 15:04:05"
|
||||
bool disable_colors = 4; // 不需要彩色日志
|
||||
bool disable_timestamp = 5; // 不需要时间戳
|
||||
bool force_colors = 6; // 是否开启彩色日志
|
||||
}
|
||||
|
||||
// Fluent
|
||||
|
||||
@@ -167,14 +167,15 @@ func appendStructToBatch(batch driverV2.Batch, obj interface{}, columns []string
|
||||
field := t.Field(j)
|
||||
|
||||
// 检查ch标签
|
||||
if tag := field.Tag.Get("ch"); tag == col {
|
||||
if tag := field.Tag.Get("ch"); strings.TrimSpace(tag) == col {
|
||||
values[i] = v.Field(j).Interface()
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
// 检查json标签
|
||||
if tag := field.Tag.Get("json"); tag == col {
|
||||
jsonTags := strings.Split(field.Tag.Get("json"), ",")
|
||||
if len(jsonTags) > 0 && strings.TrimSpace(jsonTags[0]) == col {
|
||||
values[i] = v.Field(j).Interface()
|
||||
found = true
|
||||
break
|
||||
|
||||
@@ -335,14 +335,21 @@ func (c *Client) prepareInsertData(data any) (string, string, []any, error) {
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
|
||||
// 优先获取 `cn` 标签,其次获取 `json` 标签,最后使用字段名
|
||||
columnName := field.Tag.Get("cn")
|
||||
// 优先获取 `ch` 标签,其次获取 `json` 标签,最后使用字段名
|
||||
columnName := field.Tag.Get("ch")
|
||||
if columnName == "" {
|
||||
columnName = field.Tag.Get("json")
|
||||
jsonTag := field.Tag.Get("json")
|
||||
if jsonTag != "" {
|
||||
tags := strings.Split(jsonTag, ",") // 只取逗号前的部分
|
||||
if len(tags) > 0 {
|
||||
columnName = tags[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
if columnName == "" {
|
||||
columnName = field.Name
|
||||
}
|
||||
//columnName = strings.TrimSpace(columnName)
|
||||
|
||||
columns = append(columns, columnName)
|
||||
placeholders = append(placeholders, "?")
|
||||
@@ -352,15 +359,15 @@ func (c *Client) prepareInsertData(data any) (string, string, []any, error) {
|
||||
}
|
||||
|
||||
// Insert 插入数据到指定表
|
||||
func (c *Client) Insert(ctx context.Context, tableName string, data any) error {
|
||||
func (c *Client) Insert(ctx context.Context, tableName string, in any) error {
|
||||
if c.conn == nil {
|
||||
c.log.Error("clickhouse client is not initialized")
|
||||
return ErrClientNotInitialized
|
||||
}
|
||||
|
||||
columns, placeholders, values, err := c.prepareInsertData(data)
|
||||
columns, placeholders, values, err := c.prepareInsertData(in)
|
||||
if err != nil {
|
||||
c.log.Errorf("prepare insert data failed: %v", err)
|
||||
c.log.Errorf("prepare insert in failed: %v", err)
|
||||
return ErrPrepareInsertDataFailed
|
||||
}
|
||||
|
||||
@@ -372,7 +379,7 @@ func (c *Client) Insert(ctx context.Context, tableName string, data any) error {
|
||||
)
|
||||
|
||||
// 执行插入操作
|
||||
if err := c.conn.Exec(ctx, query, values...); err != nil {
|
||||
if err = c.conn.Exec(ctx, query, values...); err != nil {
|
||||
c.log.Errorf("insert failed: %v", err)
|
||||
return ErrInsertFailed
|
||||
}
|
||||
@@ -414,7 +421,7 @@ func (c *Client) InsertMany(ctx context.Context, tableName string, data []any) e
|
||||
}
|
||||
|
||||
// 构造 SQL 语句
|
||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s",
|
||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)",
|
||||
tableName,
|
||||
columns,
|
||||
strings.Join(placeholders, ", "),
|
||||
@@ -444,10 +451,14 @@ func (c *Client) AsyncInsert(ctx context.Context, tableName string, data any, wa
|
||||
}
|
||||
|
||||
// 构造 SQL 语句
|
||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", tableName, columns, placeholders)
|
||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)",
|
||||
tableName,
|
||||
columns,
|
||||
placeholders,
|
||||
)
|
||||
|
||||
// 执行异步插入
|
||||
if err := c.asyncInsert(ctx, query, wait, values...); err != nil {
|
||||
if err = c.asyncInsert(ctx, query, wait, values...); err != nil {
|
||||
c.log.Errorf("async insert failed: %v", err)
|
||||
return ErrAsyncInsertFailed
|
||||
}
|
||||
|
||||
@@ -109,6 +109,34 @@ func structToValueArray(input any) []any {
|
||||
values = append(values, nil) // 如果时间为零值,插入 NULL
|
||||
}
|
||||
|
||||
case []any:
|
||||
// 处理切片类型
|
||||
if len(v) > 0 {
|
||||
for _, item := range v {
|
||||
if item == nil {
|
||||
values = append(values, nil)
|
||||
} else {
|
||||
values = append(values, item)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
values = append(values, nil) // 如果切片为空,插入 NULL
|
||||
}
|
||||
|
||||
case [][]any:
|
||||
// 处理二维切片类型
|
||||
if len(v) > 0 {
|
||||
for _, item := range v {
|
||||
if len(item) > 0 {
|
||||
values = append(values, item)
|
||||
} else {
|
||||
values = append(values, nil) // 如果子切片为空,插入 NULL
|
||||
}
|
||||
}
|
||||
} else {
|
||||
values = append(values, nil) // 如果二维切片为空,插入 NULL
|
||||
}
|
||||
|
||||
default:
|
||||
values = append(values, v)
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ func NewLogger(cfg *conf.Logger) log.Logger {
|
||||
fallthrough
|
||||
case "text":
|
||||
loggerFormatter = &logrus.TextFormatter{
|
||||
ForceColors: cfg.Logrus.ForceColors,
|
||||
DisableColors: cfg.Logrus.DisableColors,
|
||||
DisableTimestamp: cfg.Logrus.DisableTimestamp,
|
||||
TimestampFormat: cfg.Logrus.TimestampFormat,
|
||||
|
||||
Reference in New Issue
Block a user