optimize database struct

This commit is contained in:
douxu 2025-11-19 17:44:08 +08:00
parent dff74222c6
commit 46ee2a39f4
16 changed files with 170 additions and 155 deletions

View File

@ -24,7 +24,6 @@ func CreateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, topol
UUIDFrom: info.UUIDFrom,
UUIDTo: info.UUIDTo,
Flag: info.Flag,
Comment: info.Comment,
}
topologicSlice = append(topologicSlice, topologicInfo)
}

View File

@ -100,7 +100,7 @@ func QueryLongIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag st
return nil, nil, result.Error
}
filterMap := map[string]any{"COMPONENT_UUID": resultComp.GlobalUUID, "TAG": measTag}
filterMap := map[string]any{"component_uuid": resultComp.GlobalUUID, "tag": measTag}
result = tx.WithContext(cancelCtx).Where(filterMap).Clauses(clause.Locking{Strength: "UPDATE"}).First(&meauserment)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
@ -127,7 +127,7 @@ func QueryShortIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag s
return nil, nil, result.Error
}
filterMap := map[string]any{"COMPONENT_UUID": resultComp.GlobalUUID, "TAG": measTag}
filterMap := map[string]any{"component_uuid": resultComp.GlobalUUID, "tag": measTag}
result = tx.WithContext(cancelCtx).Where(filterMap).Clauses(clause.Locking{Strength: "UPDATE"}).First(&meauserment)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {

View File

@ -51,7 +51,6 @@ func UpdateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, chang
Flag: changeInfo.Flag,
UUIDFrom: changeInfo.NewUUIDFrom,
UUIDTo: changeInfo.NewUUIDTo,
Comment: changeInfo.Comment,
}
result = tx.WithContext(cancelCtx).Create(&topologic)
}

View File

@ -25,16 +25,16 @@ type TopologicUUIDCreateInfo struct {
// ComponentCreateInfo defines circuit diagram component create index info
type ComponentCreateInfo struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Context string `json:"context"`
GridID int64 `json:"grid_id"`
ZoneID int64 `json:"zone_id"`
StationID int64 `json:"station_id"`
PageID int64 `json:"page_id"`
Tag string `json:"tag"`
Params string `json:"params"`
Op int `json:"op"`
UUID string `json:"uuid"`
Name string `json:"name"`
Context map[string]any `json:"context"`
GridID int64 `json:"grid_id"`
ZoneID int64 `json:"zone_id"`
StationID int64 `json:"station_id"`
PageID int64 `json:"page_id"`
Tag string `json:"tag"`
Params string `json:"params"`
Op int `json:"op"`
}
// CircuitDiagramCreateRequest defines request params of circuit diagram create api

View File

@ -36,16 +36,16 @@ type TopologicUUIDChangeInfos struct {
// ComponentUpdateInfo defines circuit diagram component params info
type ComponentUpdateInfo struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`
Name string `json:"name"`
Context string `json:"context"`
GridID int64 `json:"grid_id"`
ZoneID int64 `json:"zone_id"`
StationID int64 `json:"station_id"`
Params string `json:"params"`
Op int `json:"op"`
Tag string `json:"tag"`
ID int64 `json:"id"`
UUID string `json:"uuid"`
Name string `json:"name"`
Context map[string]any `json:"context"`
GridID int64 `json:"grid_id"`
ZoneID int64 `json:"zone_id"`
StationID int64 `json:"station_id"`
Params string `json:"params"`
Op int `json:"op"`
Tag string `json:"tag"`
}
// CircuitDiagramUpdateRequest defines request params of circuit diagram update api

View File

@ -9,62 +9,35 @@ import (
// Bay structure define abstracted info set of electrical bay
type Bay struct {
BayUUID uuid.UUID `gorm:"column:BAY_UUID;type:uuid;primaryKey;default:gen_random_uuid()"`
Name string `gorm:"column:NAME;size:64;not null;default:''"`
Type string `gorm:"column:TYPE;size:64;not null;default:''"`
Unom float64 `gorm:"column:UNOM;not null;default:-1"`
Fla float64 `gorm:"column:FLA;not null;default:-1"`
Capacity float64 `gorm:"column:CAPACITY;not null;default:-1"`
Description string `gorm:"column:DESCRIPTION;size:512;not null;default:''"`
InService bool `gorm:"column:IN_SERVICE;not null;default:false"`
State int `gorm:"column:STATE;not null;default:-1"`
Grid string `gorm:"column:GRID;size:64;not null;default:''"`
Zone string `gorm:"column:ZONE;size:64;not null;default:''"`
Station string `gorm:"column:STATION;size:64;not null;default:''"`
Business map[string]interface{} `gorm:"column:BUSINESS;type:jsonb;not null;default:'{}'"`
Context map[string]interface{} `gorm:"column:CONTEXT;type:jsonb;not null;default:'{}'"`
FromUUIDs []uuid.UUID `gorm:"column:FROM_UUIDS;type:jsonb;not null;default:'[]'"`
ToUUIDs []uuid.UUID `gorm:"column:TO_UUIDS;type:jsonb;not null;default:'[]'"`
DevProtect []interface{} `gorm:"column:DEV_PROTECT;type:jsonb;not null;default:'[]'"`
DevFaultRecord []interface{} `gorm:"column:DEV_FAULT_RECORD;type:jsonb;not null;default:'[]'"`
DevStatus []interface{} `gorm:"column:DEV_STATUS;type:jsonb;not null;default:'[]'"`
DevDynSense []interface{} `gorm:"column:DEV_DYN_SENSE;type:jsonb;not null;default:'[]'"`
DevInstruct []interface{} `gorm:"column:DEV_INSTRUCT;type:jsonb;not null;default:'[]'"`
DevEtc []interface{} `gorm:"column:DEV_ETC;type:jsonb;not null;default:'[]'"`
Components []uuid.UUID `gorm:"column:COMPONENTS;type:uuid[];not null;default:'{}'"`
Op int `gorm:"column:OP;not null;default:-1"`
Ts time.Time `gorm:"column:TS;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
BayUUID uuid.UUID `gorm:"column:bay_uuid;type:uuid;primaryKey;default:gen_random_uuid()"`
Name string `gorm:"column:name;size:64;not null;default:''"`
Tag string `gorm:"column:tag;size:32;not null;default:''"`
Type string `gorm:"column:type;size:64;not null;default:''"`
Unom float64 `gorm:"column:unom;not null;default:-1"`
Fla float64 `gorm:"column:fla;not null;default:-1"`
Capacity float64 `gorm:"column:capacity;not null;default:-1"`
Description string `gorm:"column:description;size:512;not null;default:''"`
InService bool `gorm:"column:in_service;not null;default:false"`
State int `gorm:"column:state;not null;default:-1"`
Grid string `gorm:"column:grid;size:64;not null;default:''"`
Zone string `gorm:"column:zone;size:64;not null;default:''"`
Station string `gorm:"column:station;size:64;not null;default:''"`
Business JSONMap `gorm:"column:business;type:jsonb;not null;default:'{}'"`
Context JSONMap `gorm:"column:context;type:jsonb;not null;default:'{}'"`
FromUUIDs []uuid.UUID `gorm:"column:from_uuids;type:jsonb;not null;default:'[]'"`
ToUUIDs []uuid.UUID `gorm:"column:to_uuids;type:jsonb;not null;default:'[]'"`
DevProtect JSONMap `gorm:"column:dev_protect;type:jsonb;not null;default:'[]'"`
DevFaultRecord JSONMap `gorm:"column:dev_fault_record;type:jsonb;not null;default:'[]'"`
DevStatus JSONMap `gorm:"column:dev_status;type:jsonb;not null;default:'[]'"`
DevDynSense JSONMap `gorm:"column:dev_dyn_sense;type:jsonb;not null;default:'[]'"`
DevInstruct JSONMap `gorm:"column:dev_instruct;type:jsonb;not null;default:'[]'"`
DevEtc JSONMap `gorm:"column:dev_etc;type:jsonb;not null;default:'[]'"`
Components []uuid.UUID `gorm:"column:components;type:uuid[];not null;default:'{}'"`
Op int `gorm:"column:op;not null;default:-1"`
Ts time.Time `gorm:"column:ts;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
}
// TableName func respresent return table name of Bay
func (Bay) TableName() string {
return "BAY"
func (b *Bay) TableName() string {
return "bay"
}
// CREATE TABLE PUBLIC.BAY (
// BAY_UUID UUID PRIMARY KEY DEFAULT GEN_RANDOM_UUID(),
// NAME VARCHAR(64) NOT NULL DEFAULT '',
// TYPE VARCHAR(64) NOT NULL DEFAULT '',
// UNOM DOUBLE PRECISION NOT NULL DEFAULT -1,
// FLA DOUBLE PRECISION NOT NULL DEFAULT -1,
// CAPACITY DOUBLE PRECISION NOT NULL DEFAULT -1,
// DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
// IN_SERVICE BOOLEAN NOT NULL DEFAULT FALSE,
// STATE INTEGER NOT NULL DEFAULT -1,
// GRID VARCHAR(64) NOT NULL DEFAULT '',
// ZONE VARCHAR(64) NOT NULL DEFAULT '',
// STATION VARCHAR(64) NOT NULL DEFAULT '',
// BUSINESS JSONB NOT NULL DEFAULT '{}', -- for Server
// CONTEXT JSONB NOT NULL DEFAULT '{}', -- for UI
// FROM_UUIDS JSONB NOT NULL DEFAULT '[]', -- uuids
// TO_UUIDS JSONB NOT NULL DEFAULT '[]', -- uuids
// DEV_PROTECT JSONB NOT NULL DEFAULT '[]', -- devices
// DEV_FAULT_RECORD JSONB NOT NULL DEFAULT '[]', -- devices
// DEV_STATUS JSONB NOT NULL DEFAULT '[]', -- devices
// DEV_DYN_SENSE JSONB NOT NULL DEFAULT '[]', -- devices
// DEV_INSTRUCT JSONB NOT NULL DEFAULT '[]', -- devices
// DEV_ETC JSONB NOT NULL DEFAULT '[]', -- devices
// COMPONENTS UUID[] NOT NULL DEFAULT '{}',
// OP INTEGER NOT NULL DEFAULT -1,
// TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
// );

View File

@ -9,27 +9,27 @@ import (
// Component structure define abstracted info set of electrical component
type Component struct {
GlobalUUID uuid.UUID `gorm:"column:GLOBAL_UUID;primaryKey"`
NsPath string `gorm:"column:NSPATH"`
Tag string `gorm:"column:TAG"`
Name string `gorm:"column:NAME"`
ModelName string `gorm:"column:MODEL_NAME"`
Description string `gorm:"column:DESCRIPTION"`
GridTag string `gorm:"column:GRID"`
ZoneTag string `gorm:"column:ZONE"`
StationTag string `gorm:"column:STATION"`
Type int `gorm:"column:TYPE"`
InService bool `gorm:"column:IN_SERVICE"`
State int `gorm:"column:STATE"`
Status int `gorm:"column:STATUS"`
Connection map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"`
Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"`
Context string `gorm:"column:CONTEXT"`
Op int `gorm:"column:OP"`
Ts time.Time `gorm:"column:TS"`
GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey"`
NsPath string `gorm:"column:nspath"`
Tag string `gorm:"column:tag"`
Name string `gorm:"column:name"`
ModelName string `gorm:"column:model_name"`
Description string `gorm:"column:description"`
GridTag string `gorm:"column:grid"`
ZoneTag string `gorm:"column:zone"`
StationTag string `gorm:"column:station"`
Type int `gorm:"column:type"`
InService bool `gorm:"column:in_service"`
State int `gorm:"column:state"`
Status int `gorm:"column:status"`
Connection JSONMap `gorm:"column:connection;type:jsonb;default:'{}'"`
Label JSONMap `gorm:"column:label;type:jsonb;default:'{}'"`
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
Op int `gorm:"column:op"`
Ts time.Time `gorm:"column:ts"`
}
// TableName func respresent return table name of Component
func (c *Component) TableName() string {
return "COMPONENT"
return "component"
}

View File

@ -7,15 +7,15 @@ import (
// Grid structure define abstracted info set of electrical grid
type Grid struct {
ID int64 `gorm:"column:ID;primaryKey"`
TAGNAME string `gorm:"column:TAGNAME"`
Name string `gorm:"column:NAME"`
Description string `gorm:"column:DESCRIPTION"`
Op int `gorm:"column:OP"`
Ts time.Time `gorm:"column:TS"`
ID int64 `gorm:"column:id;primaryKey"`
TAGNAME string `gorm:"column:tagname"`
Name string `gorm:"column:name"`
Description string `gorm:"column:description"`
Op int `gorm:"column:op"`
Ts time.Time `gorm:"column:ts"`
}
// TableName func respresent return table name of Grid
func (g *Grid) TableName() string {
return "GRID"
return "grid"
}

View File

@ -9,20 +9,20 @@ import (
// Measurement structure define abstracted info set of electrical measurement
type Measurement struct {
ID int64 `gorm:"column:ID;primaryKey;autoIncrement"`
Tag string `gorm:"column:TAG;size:64;not null;default:''"`
Name string `gorm:"column:NAME;size:64;not null;default:''"`
Type int16 `gorm:"column:TYPE;not null;default:-1"`
Size int `gorm:"column:SIZE;not null;default:-1"`
DataSource map[string]any `gorm:"column:DATA_SOURCE;type:jsonb;not null;default:'{}'"`
EventPlan map[string]any `gorm:"column:EVENT_PLAN;type:jsonb;not null;default:'{}'"`
BayUUID uuid.UUID `gorm:"column:BAY_UUID;type:uuid;not null"`
ComponentUUID uuid.UUID `gorm:"column:COMPONENT_UUID;type:uuid;not null"`
Op int `gorm:"column:OP;not null;default:-1"`
Ts time.Time `gorm:"column:TS;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
Tag string `gorm:"column:tag;size:64;not null;default:''"`
Name string `gorm:"column:name;size:64;not null;default:''"`
Type int16 `gorm:"column:type;not null;default:-1"`
Size int `gorm:"column:size;not null;default:-1"`
DataSource JSONMap `gorm:"column:data_source;type:jsonb;not null;default:'{}'"`
EventPlan JSONMap `gorm:"column:event_plan;type:jsonb;not null;default:'{}'"`
BayUUID uuid.UUID `gorm:"column:bay_uuid;type:uuid;not null"`
ComponentUUID uuid.UUID `gorm:"column:component_uuid;type:uuid;not null"`
Op int `gorm:"column:op;not null;default:-1"`
Ts time.Time `gorm:"column:ts;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
}
// TableName func respresent return table name of Measurement
func (Measurement) TableName() string {
return "MEASUREMENT"
return "measurement"
}

View File

@ -5,17 +5,17 @@ import "time"
// Page structure define circuit diagram page info set
type Page struct {
ID int64 `gorm:"column:ID;primaryKey"`
Tag string `gorm:"column:TAG"`
Name string `gorm:"column:NAME"`
Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"`
Context map[string]interface{} `gorm:"column:CONTEXT;type:jsonb;default:'{}'"`
Description string `gorm:"column:DESCRIPTION"`
Op int `gorm:"column:OP"`
Ts time.Time `gorm:"column:TS"`
ID int64 `gorm:"column:id;primaryKey"`
Tag string `gorm:"column:tag"`
Name string `gorm:"column:name"`
Label JSONMap `gorm:"column:label;type:jsonb;default:'{}'"`
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
Description string `gorm:"column:description"`
Op int `gorm:"column:op"`
Ts time.Time `gorm:"column:ts"`
}
// TableName func respresent return table name of Page
func (p *Page) TableName() string {
return "PAGE"
return "page"
}

View File

@ -7,17 +7,17 @@ import (
// Station structure define abstracted info set of electrical Station
type Station struct {
ID int64 `gorm:"column:ID;primaryKey"`
ZoneID int64 `gorm:"column:ZONE_ID"`
TAGNAME string `gorm:"column:TAGNAME"`
Name string `gorm:"column:NAME"`
Description string `gorm:"column:DESCRIPTION"`
IsLocal bool `gorm:"column:IS_LOCAL"`
Op int `gorm:"column:OP"`
Ts time.Time `gorm:"column:TS"`
ID int64 `gorm:"column:id;primaryKey"`
ZoneID int64 `gorm:"column:zone_id"`
TAGNAME string `gorm:"column:tagname"`
Name string `gorm:"column:name"`
Description string `gorm:"column:description"`
IsLocal bool `gorm:"column:is_local"`
Op int `gorm:"column:op"`
Ts time.Time `gorm:"column:ts"`
}
// TableName func respresent return table name of Station
func (s *Station) TableName() string {
return "STATION"
return "station"
}

View File

@ -1,18 +1,25 @@
// Package orm define database data struct
package orm
import "github.com/gofrs/uuid"
import (
"time"
"github.com/gofrs/uuid"
)
// Topologic structure define topologic info set of circuit diagram
type Topologic struct {
ID int64 `gorm:"column:id"`
Flag int `gorm:"column:flag"`
UUIDFrom uuid.UUID `gorm:"column:uuid_from"`
UUIDTo uuid.UUID `gorm:"column:uuid_to"`
Comment string `gorm:"column:comment"`
ID int64 `gorm:"column:id"`
UUIDFrom uuid.UUID `gorm:"column:uuid_from"`
UUIDTo uuid.UUID `gorm:"column:uuid_to"`
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
Flag int `gorm:"column:flag"`
Description string `gorm:"column:description;size:512;not null;default:''"`
Op int `gorm:"column:op;not null;default:-1"`
Ts time.Time `gorm:"column:ts;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
}
// TableName func respresent return table name of Page
func (t *Topologic) TableName() string {
return "Topologic"
return "topologic"
}

View File

@ -7,16 +7,16 @@ import (
// Zone structure define abstracted info set of electrical zone
type Zone struct {
ID int64 `gorm:"column:ID;primaryKey"`
GridID int64 `gorm:"column:GRID_ID"`
TAGNAME string `gorm:"column:TAGNAME"`
Name string `gorm:"column:NAME"`
Description string `gorm:"column:DESCRIPTION"`
Op int `gorm:"column:OP"`
Ts time.Time `gorm:"column:TS"`
ID int64 `gorm:"column:id;primaryKey"`
GridID int64 `gorm:"column:grid_id"`
TAGNAME string `gorm:"column:tagname"`
Name string `gorm:"column:name"`
Description string `gorm:"column:description"`
Op int `gorm:"column:op"`
Ts time.Time `gorm:"column:ts"`
}
// TableName func respresent return table name of Zone
func (z *Zone) TableName() string {
return "ZONE"
return "zone"
}

38
orm/jsonb_serializer.go Normal file
View File

@ -0,0 +1,38 @@
// Package orm define database data struct
package orm
import (
"database/sql/driver"
"encoding/json"
"errors"
)
// JSONMap define struct of implements the sql.Scanner and driver.Valuer interfaces for handling JSONB fields
type JSONMap map[string]any
// Value define func to convert the JSONMap to driver.Value([]byte) for writing to the database
func (j JSONMap) Value() (driver.Value, error) {
if j == nil {
return nil, nil
}
return json.Marshal(j)
}
// Scan define to scanned values([]bytes) in the database and parsed into a JSONMap for data retrieval
func (j *JSONMap) Scan(value any) error {
if value == nil {
*j = nil
return nil
}
var source []byte
switch v := value.(type) {
case []byte:
source = v
case string:
source = []byte(v)
default:
return errors.New("unsupported data type for JSONMap Scan")
}
return json.Unmarshal(source, j)
}

View File

@ -1,14 +1,14 @@
// Package sql define database sql statement
package sql
// RecursiveSQL define Topologic table recursive query statement
// RecursiveSQL define topologic table recursive query statement
var RecursiveSQL = `WITH RECURSIVE recursive_tree as (
SELECT uuid_from,uuid_to,flag
FROM "Topologic"
FROM "topologic"
WHERE uuid_from = ?
UNION ALL
SELECT t.uuid_from,t.uuid_to,t.flag
FROM "Topologic" t
FROM "topologic" t
JOIN recursive_tree rt ON t.uuid_from = rt.uuid_to
)
SELECT * FROM recursive_tree;`

View File

@ -43,14 +43,13 @@ func TestUserDao_CreateUser(t *testing.T) {
topologicInfo := &orm.Topologic{
UUIDFrom: uuid.FromStringOrNil("70c190f2-8a60-42a9-b143-ec5f87e0aa6b"),
UUIDTo: uuid.FromStringOrNil("70c190f2-8a75-42a9-b166-ec5f87e0aa6b"),
Comment: "test",
Flag: 1,
}
// ud := dao2.NewUserDao(context.TODO())
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `Topologic`")).
WithArgs(topologicInfo.Flag, topologicInfo.UUIDFrom, topologicInfo.UUIDTo, topologicInfo.Comment).
WithArgs(topologicInfo.Flag, topologicInfo.UUIDFrom, topologicInfo.UUIDTo).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()