diff --git a/internal/consts/database.go b/internal/consts/database.go index 2852ccd..f4a5ea5 100644 --- a/internal/consts/database.go +++ b/internal/consts/database.go @@ -5,3 +5,12 @@ const ( DatabaseTypeMysql = "mysql" // mysql DatabaseTypePostgresql = "pgsql" // postgresql ) + +const ( + TdEngineDbNameKey = "tsd.tdengine.dbName" + TdEngineTypeKey = "tsd.tdengine.type" + TdEngineDsnKey = "tsd.tdengine.dsn" + TdEngineMaxIdleConnsKey = "tsd.tdengine.maxIdleConns" + TdEngineMaxOpenConnsKey = "tsd.tdengine.maxOpenConns" + TdEngineDbName = "sagoo_iot" +) diff --git a/internal/logic/product/dev_init.go b/internal/logic/product/dev_init.go index 5362393..d771365 100644 --- a/internal/logic/product/dev_init.go +++ b/internal/logic/product/dev_init.go @@ -101,9 +101,9 @@ func (s *sDevInit) InitDeviceForTd(ctx context.Context) (err error) { for _, d := range list { // 检测设备表是否创建TD表的标识 - if d.MetadataTable == 1 { + /*if d.MetadataTable == 1 { continue - } + }*/ pd, err := service.DevProduct().Detail(ctx, d.ProductKey) if err != nil { diff --git a/internal/logic/tdengine/td_engine.go b/internal/logic/tdengine/td_engine.go index e86e890..3152ba7 100644 --- a/internal/logic/tdengine/td_engine.go +++ b/internal/logic/tdengine/td_engine.go @@ -3,6 +3,7 @@ package tdengine import ( "context" "database/sql" + "sagooiot/internal/consts" "sagooiot/internal/service" "sync" "time" @@ -26,13 +27,13 @@ func tdEngineNew() *sTdEngine { return _instance } -var dbName = "sagoo_iot" +var dbName = consts.TdEngineDbName // 初始化函数 func init() { service.RegisterTdEngine(tdEngineNew()) // 简化配置获取过程 - dbName = g.Cfg().MustGet(context.Background(), "tsd.tdengine.dbName", "sagoo_iot").String() + dbName = g.Cfg().MustGet(context.Background(), consts.TdEngineDbNameKey, consts.TdEngineDbName).String() } type connections struct { @@ -67,8 +68,8 @@ func (s *sTdEngine) GetConn(ctx context.Context, dbName string) (*sql.DB, error) // 初始化连接映射 func initConnectionMap(ctx context.Context) *connections { - driver := g.Cfg().MustGet(ctx, "tsd.tdengine.type") - dsn := g.Cfg().MustGet(ctx, "tsd.tdengine.dsn") + driver := g.Cfg().MustGet(ctx, consts.TdEngineTypeKey) + dsn := g.Cfg().MustGet(ctx, consts.TdEngineDsnKey) return &connections{ tdEngineType: driver.String(), tdDsn: dsn.String(), @@ -92,8 +93,9 @@ func createNewConnection(ctx context.Context, dbName string) (*sql.DB, error) { } // 配置连接池设置 - connection.SetMaxIdleConns(g.Cfg().MustGet(ctx, "tdengine.maxIdleConns").Int()) - connection.SetMaxOpenConns(g.Cfg().MustGet(ctx, "tdengine.maxOpenConns").Int()) + // 配置连接池设置 + connection.SetMaxIdleConns(g.Cfg().MustGet(ctx, consts.TdEngineMaxIdleConnsKey).Int()) + connection.SetMaxOpenConns(g.Cfg().MustGet(ctx, consts.TdEngineMaxOpenConnsKey).Int()) connectionMap.ConnectionMap[dbName] = connection return connection, nil @@ -101,7 +103,7 @@ func createNewConnection(ctx context.Context, dbName string) (*sql.DB, error) { // Time REST连接时区处理 func (s *sTdEngine) Time(v *g.Var) (rs *g.Var) { - driver := g.Cfg().MustGet(context.TODO(), "tdengine.type") + driver := g.Cfg().MustGet(context.TODO(), consts.TdEngineTypeKey) if driver.String() == "taosRestful" { if t, err := time.Parse("2006-01-02 15:04:05 +0000 UTC", v.String()); err == nil { rs = gvar.New(t.Local().Format("2006-01-02 15:04:05")) diff --git a/internal/logic/tdengine/tsl_table.go b/internal/logic/tdengine/tsl_table.go index 2f1bbff..145187a 100644 --- a/internal/logic/tdengine/tsl_table.go +++ b/internal/logic/tdengine/tsl_table.go @@ -245,7 +245,7 @@ func (s *sTSLTable) CheckStable(ctx context.Context, stable string) (b bool, err } var name string - if err = taos.QueryRow("SELECT stable_name FROM information_schema.ins_stables WHERE stable_name = '?' LIMIT 1", stable).Scan(&name); err != nil { + if err = taos.QueryRow("SELECT stable_name FROM information_schema.ins_stables WHERE db_name = '?' AND stable_name = '?' LIMIT 1", dbName, stable).Scan(&name); err != nil { return } if name == "" { @@ -265,7 +265,7 @@ func (s *sTSLTable) CheckTable(ctx context.Context, table string) (b bool, err e } var name string - if err = taos.QueryRowContext(ctx, "SELECT table_name FROM information_schema.ins_tables WHERE table_name = '?' LIMIT 1", table).Scan(&name); err != nil { + if err = taos.QueryRowContext(ctx, "SELECT table_name FROM information_schema.ins_tables WHERE db_name = '?' AND table_name = '?' LIMIT 1", dbName, table).Scan(&name); err != nil { return } if name == "" { diff --git a/manifest/docker-compose/iot-open/sagooiot b/manifest/docker-compose/iot-open/sagooiot index b79df9d..c0bc639 100755 Binary files a/manifest/docker-compose/iot-open/sagooiot and b/manifest/docker-compose/iot-open/sagooiot differ