mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
支持tdengine
This commit is contained in:
@@ -99,7 +99,7 @@ func (p *VisualDataSourceApi) GetDataSourceTables(rc *restfulx.ReqCtx) {
|
||||
one := p.VisualDataSourceApp.FindOne(sourceId)
|
||||
instance := driver.NewDbInstance(one)
|
||||
biz.IsTrue(instance != nil, "获取数据源下所有表失败")
|
||||
rc.ResData = instance.GetMeta().GetTableInfos()
|
||||
rc.ResData = instance.GetMeta().GetTables()
|
||||
}
|
||||
|
||||
// GetDataSourceTableDetails 获取表下面的所有字段
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/XM-GO/PandaKit/cache"
|
||||
"pandax/apps/visual/entity"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/tool"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -74,6 +75,12 @@ func (di *DbInstance) GetMeta() DbMetadata {
|
||||
if dbType == entity.DbTypePostgres {
|
||||
return &PgsqlMetadata{di: di}
|
||||
}
|
||||
if dbType == entity.DbTypeTdengine {
|
||||
return &TDMetadata{di: di}
|
||||
}
|
||||
if dbType == entity.DbTypeClickHouse {
|
||||
return &ClickHouseMetadata{di: di}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -121,11 +128,10 @@ func AddDbInstanceToCache(id string, di *DbInstance) {
|
||||
|
||||
func TestConnection(d *entity.VisualDataSource) error {
|
||||
// 验证第一个库是否可以连接即可
|
||||
DB, err := GetDbConn(d)
|
||||
_, err := GetDbConn(d)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
DB.Close()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -138,8 +144,11 @@ func GetDbConn(d *entity.VisualDataSource) (*sql.DB, error) {
|
||||
DB, err = getMysqlDB(d)
|
||||
} else if d.SourceType == entity.DbTypePostgres {
|
||||
DB, err = getPgsqlDB(d)
|
||||
} else if d.SourceType == entity.DbTypeTdengine {
|
||||
DB, err = getTdDB(d)
|
||||
} else if d.SourceType == entity.DbTypeClickHouse {
|
||||
DB, err = getClickHouseDB(d)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -148,7 +157,6 @@ func GetDbConn(d *entity.VisualDataSource) (*sql.DB, error) {
|
||||
DB.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return DB, nil
|
||||
}
|
||||
|
||||
@@ -195,6 +203,9 @@ func SelectDataByDb(db *sql.DB, selectSql string) ([]string, []map[string]interf
|
||||
if isFirst {
|
||||
colNames = append(colNames, colName)
|
||||
}
|
||||
if strings.Contains(colName, "_") {
|
||||
colName = tool.ToCamelCase(colName)
|
||||
}
|
||||
rowData[colName] = valueConvert(v, colType)
|
||||
}
|
||||
// 放入结果集
|
||||
|
||||
65
apps/visual/driver/click_house_meta.go
Normal file
65
apps/visual/driver/click_house_meta.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"pandax/apps/visual/entity"
|
||||
)
|
||||
|
||||
func getClickHouseDB(d *entity.VisualDataSource) (*sql.DB, error) {
|
||||
// 设置dataSourceName -> 更多参数参考:https://github.com/go-sql-driver/mysql#dsn-data-source-name
|
||||
dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s?timeout=8s",
|
||||
d.Db.Username, d.Db.Password, "tcp", d.Db.Host, d.Db.Port, d.Db.Dbname)
|
||||
if d.Db.Config != "" {
|
||||
dsn = fmt.Sprintf("%s&%s", dsn, d.Db.Config)
|
||||
}
|
||||
return sql.Open("mysql", dsn)
|
||||
}
|
||||
|
||||
// ---------------------------------- mysql元数据 -----------------------------------
|
||||
const (
|
||||
// 表信息元数据
|
||||
ClickHouse_TABLE_MA = `SELECT name tableName, comment tableComment from system.tables WHERE database = '%s'`
|
||||
|
||||
// 表信息
|
||||
ClickHouse_TABLE_INFO = `SELECT name tableName, comment tableComment, total_rows tableRows
|
||||
FROM system.tables WHERE database = '%s'`
|
||||
|
||||
// 列信息元数据
|
||||
ClickHouse_COLUMN_MA = `SELECT table tableName, name columnName, type columnType, comment columnComment from system.columns
|
||||
WHERE database='%s' AND table in (%s) ORDER BY table`
|
||||
)
|
||||
|
||||
type ClickHouseMetadata struct {
|
||||
di *DbInstance
|
||||
}
|
||||
|
||||
// 获取表基础元信息, 如表名等
|
||||
func (mm *ClickHouseMetadata) GetTables() []map[string]interface{} {
|
||||
res, err := mm.di.innerSelect(fmt.Sprintf(ClickHouse_TABLE_MA, mm.di.Info.Db.Dbname))
|
||||
biz.ErrIsNilAppendErr(err, "获取表基本信息失败: %s")
|
||||
return res
|
||||
}
|
||||
|
||||
// 获取列元信息, 如列名等
|
||||
func (mm *ClickHouseMetadata) GetColumns(tableNames ...string) []map[string]interface{} {
|
||||
tableName := ""
|
||||
for i := 0; i < len(tableNames); i++ {
|
||||
if i != 0 {
|
||||
tableName = tableName + ", "
|
||||
}
|
||||
tableName = tableName + "'" + tableNames[i] + "'"
|
||||
}
|
||||
result, err := mm.di.innerSelect(fmt.Sprintf(ClickHouse_COLUMN_MA, mm.di.Info.Db.Dbname, tableName))
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库列信息失败: %s")
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取表信息,比GetTableMetedatas获取更详细的表信息
|
||||
func (mm *ClickHouseMetadata) GetTableInfos() []map[string]interface{} {
|
||||
res, err := mm.di.innerSelect(fmt.Sprintf(ClickHouse_TABLE_INFO, mm.di.Info.Db.Dbname))
|
||||
biz.ErrIsNilAppendErr(err, "获取表信息失败: %s")
|
||||
return res
|
||||
}
|
||||
@@ -3,7 +3,5 @@ package driver
|
||||
type DbMetadata interface {
|
||||
GetTables() []map[string]interface{}
|
||||
GetColumns(tableNames ...string) []map[string]interface{}
|
||||
GetPrimaryKey(tableName string) string
|
||||
GetTableInfos() []map[string]interface{}
|
||||
GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]interface{}, error)
|
||||
}
|
||||
|
||||
@@ -4,14 +4,11 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"pandax/apps/visual/entity"
|
||||
"strings"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"pandax/apps/visual/entity"
|
||||
)
|
||||
|
||||
func getMysqlDB(d *entity.VisualDataSource) (*sql.DB, error) {
|
||||
// SSH Conect
|
||||
// 设置dataSourceName -> 更多参数参考:https://github.com/go-sql-driver/mysql#dsn-data-source-name
|
||||
dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s?timeout=8s",
|
||||
d.Db.Username, d.Db.Password, "tcp", d.Db.Host, d.Db.Port, d.Db.Dbname)
|
||||
@@ -32,25 +29,12 @@ const (
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = (SELECT database())`
|
||||
|
||||
// mysql 索引信息
|
||||
MYSQL_INDEX_INFO = `SELECT index_name indexName, column_name columnName, index_type indexType, non_unique nonUnique,
|
||||
SEQ_IN_INDEX seqInIndex, INDEX_COMMENT indexComment
|
||||
FROM information_schema.STATISTICS
|
||||
WHERE table_schema = (SELECT database()) AND table_name = '%s' ORDER BY index_name asc , SEQ_IN_INDEX asc`
|
||||
|
||||
// mysql 列信息元数据
|
||||
MYSQL_COLUMN_MA = `SELECT table_name tableName, column_name columnName, column_type columnType, column_default columnDefault,
|
||||
column_comment columnComment, column_key columnKey, extra, is_nullable nullable from information_schema.columns
|
||||
MYSQL_COLUMN_MA = `SELECT table_name tableName, column_name columnName, column_type columnType,column_comment columnComment,
|
||||
column_key columnKey, is_nullable nullable from information_schema.columns
|
||||
WHERE table_schema = (SELECT database()) AND table_name in (%s) ORDER BY tableName, ordinal_position`
|
||||
)
|
||||
|
||||
func sqlFix(sql string) string {
|
||||
if strings.LastIndex(sql, ";") == (len(sql) - 1) {
|
||||
sql = sql[0 : len(sql)-1]
|
||||
}
|
||||
return sql
|
||||
}
|
||||
|
||||
type MysqlMetadata struct {
|
||||
di *DbInstance
|
||||
}
|
||||
@@ -76,49 +60,9 @@ func (mm *MysqlMetadata) GetColumns(tableNames ...string) []map[string]interface
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取表主键字段名,不存在主键标识则默认第一个字段
|
||||
func (mm *MysqlMetadata) GetPrimaryKey(tablename string) string {
|
||||
columns := mm.GetColumns(tablename)
|
||||
biz.IsTrue(len(columns) > 0, "[%s] 表不存在", tablename)
|
||||
for _, v := range columns {
|
||||
if v["columnKey"].(string) == "PRI" {
|
||||
return v["columnName"].(string)
|
||||
}
|
||||
}
|
||||
|
||||
return columns[0]["columnName"].(string)
|
||||
}
|
||||
|
||||
// 获取表信息,比GetTableMetedatas获取更详细的表信息
|
||||
func (mm *MysqlMetadata) GetTableInfos() []map[string]interface{} {
|
||||
res, err := mm.di.innerSelect(MYSQL_TABLE_INFO)
|
||||
biz.ErrIsNilAppendErr(err, "获取表信息失败: %s")
|
||||
return res
|
||||
}
|
||||
|
||||
// 获取表索引信息
|
||||
func (mm *MysqlMetadata) GetTableIndex(tableName string) []map[string]interface{} {
|
||||
res, err := mm.di.innerSelect(fmt.Sprintf(MYSQL_INDEX_INFO, tableName))
|
||||
biz.ErrIsNilAppendErr(err, "获取表索引信息失败: %s")
|
||||
// 把查询结果以索引名分组,索引字段以逗号连接
|
||||
result := make([]map[string]interface{}, 0)
|
||||
key := ""
|
||||
for _, v := range res {
|
||||
// 当前的索引名
|
||||
in := v["indexName"].(string)
|
||||
if key == in {
|
||||
// 索引字段已根据名称和顺序排序,故取最后一个即可
|
||||
i := len(result) - 1
|
||||
// 同索引字段以逗号连接
|
||||
result[i]["columnName"] = result[i]["columnName"].(string) + "," + v["columnName"].(string)
|
||||
} else {
|
||||
key = in
|
||||
result = append(result, v)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (mm *MysqlMetadata) GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]interface{}, error) {
|
||||
return mm.di.SelectData(fmt.Sprintf("SELECT * FROM %s LIMIT %d, %d", tableName, (pageNum-1)*pageSize, pageSize))
|
||||
}
|
||||
|
||||
@@ -28,9 +28,6 @@ const (
|
||||
PGSQL_TABLE_INFO = `SELECT obj_description(c.oid) AS "tableComment", c.relname AS "tableName" FROM pg_class c
|
||||
JOIN pg_namespace n ON c.relnamespace = n.oid WHERE n.nspname = (select current_schema()) AND c.reltype > 0`
|
||||
|
||||
PGSQL_INDEX_INFO = `SELECT indexname AS "indexName", indexdef AS "indexComment"
|
||||
FROM pg_indexes WHERE schemaname = (select current_schema()) AND tablename = '%s'`
|
||||
|
||||
PGSQL_COLUMN_MA = `SELECT
|
||||
C.relname AS "tableName",
|
||||
A.attname AS "columnName",
|
||||
@@ -80,18 +77,6 @@ func (pm *PgsqlMetadata) GetColumns(tableNames ...string) []map[string]interface
|
||||
return result
|
||||
}
|
||||
|
||||
func (pm *PgsqlMetadata) GetPrimaryKey(tablename string) string {
|
||||
columns := pm.GetColumns(tablename)
|
||||
biz.IsTrue(len(columns) > 0, "[%s] 表不存在", tablename)
|
||||
for _, v := range columns {
|
||||
if v["columnKey"].(string) == "PRI" {
|
||||
return v["columnName"].(string)
|
||||
}
|
||||
}
|
||||
|
||||
return columns[0]["columnName"].(string)
|
||||
}
|
||||
|
||||
// 获取表信息,比GetTables获取更详细的表信息
|
||||
func (pm *PgsqlMetadata) GetTableInfos() []map[string]interface{} {
|
||||
res, err := pm.di.innerSelect(PGSQL_TABLE_INFO)
|
||||
@@ -99,17 +84,6 @@ func (pm *PgsqlMetadata) GetTableInfos() []map[string]interface{} {
|
||||
return res
|
||||
}
|
||||
|
||||
// 获取表索引信息
|
||||
func (pm *PgsqlMetadata) GetTableIndex(tableName string) []map[string]interface{} {
|
||||
res, err := pm.di.innerSelect(fmt.Sprintf(PGSQL_INDEX_INFO, tableName))
|
||||
biz.ErrIsNilAppendErr(err, "获取表索引信息失败: %s")
|
||||
return res
|
||||
}
|
||||
|
||||
func (pm *PgsqlMetadata) GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]interface{}, error) {
|
||||
return pm.di.SelectData(fmt.Sprintf("SELECT * FROM %s OFFSET %d LIMIT %d", tableName, (pageNum-1)*pageSize, pageSize))
|
||||
}
|
||||
|
||||
// 获取所有Schema
|
||||
func (pm *PgsqlMetadata) GetSchemas() ([]string, []map[string]interface{}, error) {
|
||||
return pm.di.SelectData("SELECT schema_name FROM information_schema.schemata")
|
||||
|
||||
64
apps/visual/driver/td_meta.go
Normal file
64
apps/visual/driver/td_meta.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
_ "github.com/taosdata/driver-go/v3/taosRestful"
|
||||
"pandax/apps/visual/entity"
|
||||
)
|
||||
|
||||
func getTdDB(d *entity.VisualDataSource) (*sql.DB, error) {
|
||||
dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s",
|
||||
d.Db.Username, d.Db.Password, "http", d.Db.Host, d.Db.Port, d.Db.Dbname)
|
||||
if d.Db.Config != "" {
|
||||
dsn = fmt.Sprintf("%s&%s", dsn, d.Db.Config)
|
||||
}
|
||||
return sql.Open("taosRestful", dsn)
|
||||
}
|
||||
|
||||
// ---------------------------------- mysql元数据 -----------------------------------
|
||||
const (
|
||||
// 表信息元数据
|
||||
TD_TABLE_MA = `SELECT table_name table_name,table_comment table_comment from information_schema.INS_TABLES WHERE db_name = '%s'`
|
||||
|
||||
// 表信息
|
||||
TD_TABLE_INFO = `SELECT table_name table_name, table_comment table_comment, columns table_rows, create_time create_time
|
||||
FROM information_schema.INS_TABLES WHERE db_name = '%s'`
|
||||
|
||||
//列信息元数据
|
||||
TD_COLUMN_MA = `SELECT table_name table_name, col_name column_name, col_type column_type,col_nullable nullable from information_schema.INS_COLUMNS
|
||||
WHERE db_name = '%s' AND table_name in (%s) ORDER BY table_name`
|
||||
)
|
||||
|
||||
type TDMetadata struct {
|
||||
di *DbInstance
|
||||
}
|
||||
|
||||
// 获取表基础元信息, 如表名等
|
||||
func (mm *TDMetadata) GetTables() []map[string]interface{} {
|
||||
res, err := mm.di.innerSelect(fmt.Sprintf(TD_TABLE_MA, mm.di.Info.Db.Dbname))
|
||||
biz.ErrIsNilAppendErr(err, "获取表基本信息失败: %s")
|
||||
return res
|
||||
}
|
||||
|
||||
// 获取列元信息, 如列名等
|
||||
func (mm *TDMetadata) GetColumns(tableNames ...string) []map[string]interface{} {
|
||||
tableName := ""
|
||||
for i := 0; i < len(tableNames); i++ {
|
||||
if i != 0 {
|
||||
tableName = tableName + ", "
|
||||
}
|
||||
tableName = tableName + "'" + tableNames[i] + "'"
|
||||
}
|
||||
result, err := mm.di.innerSelect(fmt.Sprintf(TD_COLUMN_MA, mm.di.Info.Db.Dbname, tableName))
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库列信息失败: %s")
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取表信息,比GetTableMetedatas获取更详细的表信息
|
||||
func (mm *TDMetadata) GetTableInfos() []map[string]interface{} {
|
||||
res, err := mm.di.innerSelect(fmt.Sprintf(TD_TABLE_INFO, mm.di.Info.Db.Dbname))
|
||||
biz.ErrIsNilAppendErr(err, "获取表信息失败: %s")
|
||||
return res
|
||||
}
|
||||
@@ -8,8 +8,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
DbTypeMysql = "MySQL"
|
||||
DbTypePostgres = "PostgreSQL"
|
||||
DbTypeMysql = "MySQL"
|
||||
DbTypePostgres = "PostgreSQL"
|
||||
DbTypeTdengine = "Tdengine"
|
||||
DbTypeClickHouse = "ClickHouse"
|
||||
)
|
||||
|
||||
type VisualDataSource struct {
|
||||
|
||||
Reference in New Issue
Block a user