// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. package clickhouse import ( "context" "fmt" "git.magicany.cc/black1552/gin-base/database" "github.com/gogf/gf/v2/text/gregex" "github.com/gogf/gf/v2/util/gutil" ) const ( tableFieldsColumns = `name,position,default_expression,comment,type,is_in_partition_key,is_in_sorting_key,is_in_primary_key,is_in_sampling_key` ) // TableFields retrieves and returns the fields' information of specified table of current schema. // Also see DriverMysql.TableFields. func (d *Driver) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*database.TableField, err error) { var ( result database.Result link database.Link usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...) ) if link, err = d.SlaveLink(usedSchema); err != nil { return nil, err } var ( getColumnsSql = fmt.Sprintf( "select %s from `system`.columns c where `table` = '%s'", tableFieldsColumns, table, ) ) result, err = d.DoSelect(ctx, link, getColumnsSql) if err != nil { return nil, err } fields = make(map[string]*database.TableField) for _, m := range result { var ( isNull = false fieldType = m["type"].String() ) // in clickhouse , field type like is Nullable(int) fieldsResult, _ := gregex.MatchString(`^Nullable\((.*?)\)`, fieldType) if len(fieldsResult) == 2 { isNull = true fieldType = fieldsResult[1] } position := m["position"].Int() if result[0]["position"].Int() != 0 { position -= 1 } fields[m["name"].String()] = &database.TableField{ Index: position, Name: m["name"].String(), Default: m["default_expression"].Val(), Comment: m["comment"].String(), // Key: m["Key"].String(), Type: fieldType, Null: isNull, } } return fields, nil }