optimize database struct
This commit is contained in:
parent
dff74222c6
commit
46ee2a39f4
|
|
@ -24,7 +24,6 @@ func CreateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, topol
|
||||||
UUIDFrom: info.UUIDFrom,
|
UUIDFrom: info.UUIDFrom,
|
||||||
UUIDTo: info.UUIDTo,
|
UUIDTo: info.UUIDTo,
|
||||||
Flag: info.Flag,
|
Flag: info.Flag,
|
||||||
Comment: info.Comment,
|
|
||||||
}
|
}
|
||||||
topologicSlice = append(topologicSlice, topologicInfo)
|
topologicSlice = append(topologicSlice, topologicInfo)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ func QueryLongIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag st
|
||||||
return nil, nil, result.Error
|
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)
|
result = tx.WithContext(cancelCtx).Where(filterMap).Clauses(clause.Locking{Strength: "UPDATE"}).First(&meauserment)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if result.Error == gorm.ErrRecordNotFound {
|
if result.Error == gorm.ErrRecordNotFound {
|
||||||
|
|
@ -127,7 +127,7 @@ func QueryShortIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag s
|
||||||
return nil, nil, result.Error
|
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)
|
result = tx.WithContext(cancelCtx).Where(filterMap).Clauses(clause.Locking{Strength: "UPDATE"}).First(&meauserment)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if result.Error == gorm.ErrRecordNotFound {
|
if result.Error == gorm.ErrRecordNotFound {
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,6 @@ func UpdateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, chang
|
||||||
Flag: changeInfo.Flag,
|
Flag: changeInfo.Flag,
|
||||||
UUIDFrom: changeInfo.NewUUIDFrom,
|
UUIDFrom: changeInfo.NewUUIDFrom,
|
||||||
UUIDTo: changeInfo.NewUUIDTo,
|
UUIDTo: changeInfo.NewUUIDTo,
|
||||||
Comment: changeInfo.Comment,
|
|
||||||
}
|
}
|
||||||
result = tx.WithContext(cancelCtx).Create(&topologic)
|
result = tx.WithContext(cancelCtx).Create(&topologic)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,16 +25,16 @@ type TopologicUUIDCreateInfo struct {
|
||||||
|
|
||||||
// ComponentCreateInfo defines circuit diagram component create index info
|
// ComponentCreateInfo defines circuit diagram component create index info
|
||||||
type ComponentCreateInfo struct {
|
type ComponentCreateInfo struct {
|
||||||
UUID string `json:"uuid"`
|
UUID string `json:"uuid"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Context string `json:"context"`
|
Context map[string]any `json:"context"`
|
||||||
GridID int64 `json:"grid_id"`
|
GridID int64 `json:"grid_id"`
|
||||||
ZoneID int64 `json:"zone_id"`
|
ZoneID int64 `json:"zone_id"`
|
||||||
StationID int64 `json:"station_id"`
|
StationID int64 `json:"station_id"`
|
||||||
PageID int64 `json:"page_id"`
|
PageID int64 `json:"page_id"`
|
||||||
Tag string `json:"tag"`
|
Tag string `json:"tag"`
|
||||||
Params string `json:"params"`
|
Params string `json:"params"`
|
||||||
Op int `json:"op"`
|
Op int `json:"op"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CircuitDiagramCreateRequest defines request params of circuit diagram create api
|
// CircuitDiagramCreateRequest defines request params of circuit diagram create api
|
||||||
|
|
|
||||||
|
|
@ -36,16 +36,16 @@ type TopologicUUIDChangeInfos struct {
|
||||||
|
|
||||||
// ComponentUpdateInfo defines circuit diagram component params info
|
// ComponentUpdateInfo defines circuit diagram component params info
|
||||||
type ComponentUpdateInfo struct {
|
type ComponentUpdateInfo struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
UUID string `json:"uuid"`
|
UUID string `json:"uuid"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Context string `json:"context"`
|
Context map[string]any `json:"context"`
|
||||||
GridID int64 `json:"grid_id"`
|
GridID int64 `json:"grid_id"`
|
||||||
ZoneID int64 `json:"zone_id"`
|
ZoneID int64 `json:"zone_id"`
|
||||||
StationID int64 `json:"station_id"`
|
StationID int64 `json:"station_id"`
|
||||||
Params string `json:"params"`
|
Params string `json:"params"`
|
||||||
Op int `json:"op"`
|
Op int `json:"op"`
|
||||||
Tag string `json:"tag"`
|
Tag string `json:"tag"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CircuitDiagramUpdateRequest defines request params of circuit diagram update api
|
// CircuitDiagramUpdateRequest defines request params of circuit diagram update api
|
||||||
|
|
|
||||||
|
|
@ -9,62 +9,35 @@ import (
|
||||||
|
|
||||||
// Bay structure define abstracted info set of electrical bay
|
// Bay structure define abstracted info set of electrical bay
|
||||||
type Bay struct {
|
type Bay struct {
|
||||||
BayUUID uuid.UUID `gorm:"column:BAY_UUID;type:uuid;primaryKey;default:gen_random_uuid()"`
|
BayUUID uuid.UUID `gorm:"column:bay_uuid;type:uuid;primaryKey;default:gen_random_uuid()"`
|
||||||
Name string `gorm:"column:NAME;size:64;not null;default:''"`
|
Name string `gorm:"column:name;size:64;not null;default:''"`
|
||||||
Type string `gorm:"column:TYPE;size:64;not null;default:''"`
|
Tag string `gorm:"column:tag;size:32;not null;default:''"`
|
||||||
Unom float64 `gorm:"column:UNOM;not null;default:-1"`
|
Type string `gorm:"column:type;size:64;not null;default:''"`
|
||||||
Fla float64 `gorm:"column:FLA;not null;default:-1"`
|
Unom float64 `gorm:"column:unom;not null;default:-1"`
|
||||||
Capacity float64 `gorm:"column:CAPACITY;not null;default:-1"`
|
Fla float64 `gorm:"column:fla;not null;default:-1"`
|
||||||
Description string `gorm:"column:DESCRIPTION;size:512;not null;default:''"`
|
Capacity float64 `gorm:"column:capacity;not null;default:-1"`
|
||||||
InService bool `gorm:"column:IN_SERVICE;not null;default:false"`
|
Description string `gorm:"column:description;size:512;not null;default:''"`
|
||||||
State int `gorm:"column:STATE;not null;default:-1"`
|
InService bool `gorm:"column:in_service;not null;default:false"`
|
||||||
Grid string `gorm:"column:GRID;size:64;not null;default:''"`
|
State int `gorm:"column:state;not null;default:-1"`
|
||||||
Zone string `gorm:"column:ZONE;size:64;not null;default:''"`
|
Grid string `gorm:"column:grid;size:64;not null;default:''"`
|
||||||
Station string `gorm:"column:STATION;size:64;not null;default:''"`
|
Zone string `gorm:"column:zone;size:64;not null;default:''"`
|
||||||
Business map[string]interface{} `gorm:"column:BUSINESS;type:jsonb;not null;default:'{}'"`
|
Station string `gorm:"column:station;size:64;not null;default:''"`
|
||||||
Context map[string]interface{} `gorm:"column:CONTEXT;type:jsonb;not null;default:'{}'"`
|
Business JSONMap `gorm:"column:business;type:jsonb;not null;default:'{}'"`
|
||||||
FromUUIDs []uuid.UUID `gorm:"column:FROM_UUIDS;type:jsonb;not null;default:'[]'"`
|
Context JSONMap `gorm:"column:context;type:jsonb;not null;default:'{}'"`
|
||||||
ToUUIDs []uuid.UUID `gorm:"column:TO_UUIDS;type:jsonb;not null;default:'[]'"`
|
FromUUIDs []uuid.UUID `gorm:"column:from_uuids;type:jsonb;not null;default:'[]'"`
|
||||||
DevProtect []interface{} `gorm:"column:DEV_PROTECT;type:jsonb;not null;default:'[]'"`
|
ToUUIDs []uuid.UUID `gorm:"column:to_uuids;type:jsonb;not null;default:'[]'"`
|
||||||
DevFaultRecord []interface{} `gorm:"column:DEV_FAULT_RECORD;type:jsonb;not null;default:'[]'"`
|
DevProtect JSONMap `gorm:"column:dev_protect;type:jsonb;not null;default:'[]'"`
|
||||||
DevStatus []interface{} `gorm:"column:DEV_STATUS;type:jsonb;not null;default:'[]'"`
|
DevFaultRecord JSONMap `gorm:"column:dev_fault_record;type:jsonb;not null;default:'[]'"`
|
||||||
DevDynSense []interface{} `gorm:"column:DEV_DYN_SENSE;type:jsonb;not null;default:'[]'"`
|
DevStatus JSONMap `gorm:"column:dev_status;type:jsonb;not null;default:'[]'"`
|
||||||
DevInstruct []interface{} `gorm:"column:DEV_INSTRUCT;type:jsonb;not null;default:'[]'"`
|
DevDynSense JSONMap `gorm:"column:dev_dyn_sense;type:jsonb;not null;default:'[]'"`
|
||||||
DevEtc []interface{} `gorm:"column:DEV_ETC;type:jsonb;not null;default:'[]'"`
|
DevInstruct JSONMap `gorm:"column:dev_instruct;type:jsonb;not null;default:'[]'"`
|
||||||
Components []uuid.UUID `gorm:"column:COMPONENTS;type:uuid[];not null;default:'{}'"`
|
DevEtc JSONMap `gorm:"column:dev_etc;type:jsonb;not null;default:'[]'"`
|
||||||
Op int `gorm:"column:OP;not null;default:-1"`
|
Components []uuid.UUID `gorm:"column:components;type:uuid[];not null;default:'{}'"`
|
||||||
Ts time.Time `gorm:"column:TS;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
|
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
|
// TableName func respresent return table name of Bay
|
||||||
func (Bay) TableName() string {
|
func (b *Bay) TableName() string {
|
||||||
return "BAY"
|
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
|
|
||||||
// );
|
|
||||||
|
|
|
||||||
|
|
@ -9,27 +9,27 @@ import (
|
||||||
|
|
||||||
// Component structure define abstracted info set of electrical component
|
// Component structure define abstracted info set of electrical component
|
||||||
type Component struct {
|
type Component struct {
|
||||||
GlobalUUID uuid.UUID `gorm:"column:GLOBAL_UUID;primaryKey"`
|
GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey"`
|
||||||
NsPath string `gorm:"column:NSPATH"`
|
NsPath string `gorm:"column:nspath"`
|
||||||
Tag string `gorm:"column:TAG"`
|
Tag string `gorm:"column:tag"`
|
||||||
Name string `gorm:"column:NAME"`
|
Name string `gorm:"column:name"`
|
||||||
ModelName string `gorm:"column:MODEL_NAME"`
|
ModelName string `gorm:"column:model_name"`
|
||||||
Description string `gorm:"column:DESCRIPTION"`
|
Description string `gorm:"column:description"`
|
||||||
GridTag string `gorm:"column:GRID"`
|
GridTag string `gorm:"column:grid"`
|
||||||
ZoneTag string `gorm:"column:ZONE"`
|
ZoneTag string `gorm:"column:zone"`
|
||||||
StationTag string `gorm:"column:STATION"`
|
StationTag string `gorm:"column:station"`
|
||||||
Type int `gorm:"column:TYPE"`
|
Type int `gorm:"column:type"`
|
||||||
InService bool `gorm:"column:IN_SERVICE"`
|
InService bool `gorm:"column:in_service"`
|
||||||
State int `gorm:"column:STATE"`
|
State int `gorm:"column:state"`
|
||||||
Status int `gorm:"column:STATUS"`
|
Status int `gorm:"column:status"`
|
||||||
Connection map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"`
|
Connection JSONMap `gorm:"column:connection;type:jsonb;default:'{}'"`
|
||||||
Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"`
|
Label JSONMap `gorm:"column:label;type:jsonb;default:'{}'"`
|
||||||
Context string `gorm:"column:CONTEXT"`
|
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
|
||||||
Op int `gorm:"column:OP"`
|
Op int `gorm:"column:op"`
|
||||||
Ts time.Time `gorm:"column:TS"`
|
Ts time.Time `gorm:"column:ts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName func respresent return table name of Component
|
// TableName func respresent return table name of Component
|
||||||
func (c *Component) TableName() string {
|
func (c *Component) TableName() string {
|
||||||
return "COMPONENT"
|
return "component"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,15 @@ import (
|
||||||
|
|
||||||
// Grid structure define abstracted info set of electrical grid
|
// Grid structure define abstracted info set of electrical grid
|
||||||
type Grid struct {
|
type Grid struct {
|
||||||
ID int64 `gorm:"column:ID;primaryKey"`
|
ID int64 `gorm:"column:id;primaryKey"`
|
||||||
TAGNAME string `gorm:"column:TAGNAME"`
|
TAGNAME string `gorm:"column:tagname"`
|
||||||
Name string `gorm:"column:NAME"`
|
Name string `gorm:"column:name"`
|
||||||
Description string `gorm:"column:DESCRIPTION"`
|
Description string `gorm:"column:description"`
|
||||||
Op int `gorm:"column:OP"`
|
Op int `gorm:"column:op"`
|
||||||
Ts time.Time `gorm:"column:TS"`
|
Ts time.Time `gorm:"column:ts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName func respresent return table name of Grid
|
// TableName func respresent return table name of Grid
|
||||||
func (g *Grid) TableName() string {
|
func (g *Grid) TableName() string {
|
||||||
return "GRID"
|
return "grid"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,20 +9,20 @@ import (
|
||||||
|
|
||||||
// Measurement structure define abstracted info set of electrical measurement
|
// Measurement structure define abstracted info set of electrical measurement
|
||||||
type Measurement struct {
|
type Measurement struct {
|
||||||
ID int64 `gorm:"column:ID;primaryKey;autoIncrement"`
|
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||||
Tag string `gorm:"column:TAG;size:64;not null;default:''"`
|
Tag string `gorm:"column:tag;size:64;not null;default:''"`
|
||||||
Name string `gorm:"column:NAME;size:64;not null;default:''"`
|
Name string `gorm:"column:name;size:64;not null;default:''"`
|
||||||
Type int16 `gorm:"column:TYPE;not null;default:-1"`
|
Type int16 `gorm:"column:type;not null;default:-1"`
|
||||||
Size int `gorm:"column:SIZE;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:'{}'"`
|
DataSource JSONMap `gorm:"column:data_source;type:jsonb;not null;default:'{}'"`
|
||||||
EventPlan map[string]any `gorm:"column:EVENT_PLAN;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"`
|
BayUUID uuid.UUID `gorm:"column:bay_uuid;type:uuid;not null"`
|
||||||
ComponentUUID uuid.UUID `gorm:"column:COMPONENT_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"`
|
Op int `gorm:"column:op;not null;default:-1"`
|
||||||
Ts time.Time `gorm:"column:TS;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
|
Ts time.Time `gorm:"column:ts;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName func respresent return table name of Measurement
|
// TableName func respresent return table name of Measurement
|
||||||
func (Measurement) TableName() string {
|
func (Measurement) TableName() string {
|
||||||
return "MEASUREMENT"
|
return "measurement"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,17 @@ import "time"
|
||||||
|
|
||||||
// Page structure define circuit diagram page info set
|
// Page structure define circuit diagram page info set
|
||||||
type Page struct {
|
type Page struct {
|
||||||
ID int64 `gorm:"column:ID;primaryKey"`
|
ID int64 `gorm:"column:id;primaryKey"`
|
||||||
Tag string `gorm:"column:TAG"`
|
Tag string `gorm:"column:tag"`
|
||||||
Name string `gorm:"column:NAME"`
|
Name string `gorm:"column:name"`
|
||||||
Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"`
|
Label JSONMap `gorm:"column:label;type:jsonb;default:'{}'"`
|
||||||
Context map[string]interface{} `gorm:"column:CONTEXT;type:jsonb;default:'{}'"`
|
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
|
||||||
Description string `gorm:"column:DESCRIPTION"`
|
Description string `gorm:"column:description"`
|
||||||
Op int `gorm:"column:OP"`
|
Op int `gorm:"column:op"`
|
||||||
Ts time.Time `gorm:"column:TS"`
|
Ts time.Time `gorm:"column:ts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName func respresent return table name of Page
|
// TableName func respresent return table name of Page
|
||||||
func (p *Page) TableName() string {
|
func (p *Page) TableName() string {
|
||||||
return "PAGE"
|
return "page"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,17 +7,17 @@ import (
|
||||||
|
|
||||||
// Station structure define abstracted info set of electrical Station
|
// Station structure define abstracted info set of electrical Station
|
||||||
type Station struct {
|
type Station struct {
|
||||||
ID int64 `gorm:"column:ID;primaryKey"`
|
ID int64 `gorm:"column:id;primaryKey"`
|
||||||
ZoneID int64 `gorm:"column:ZONE_ID"`
|
ZoneID int64 `gorm:"column:zone_id"`
|
||||||
TAGNAME string `gorm:"column:TAGNAME"`
|
TAGNAME string `gorm:"column:tagname"`
|
||||||
Name string `gorm:"column:NAME"`
|
Name string `gorm:"column:name"`
|
||||||
Description string `gorm:"column:DESCRIPTION"`
|
Description string `gorm:"column:description"`
|
||||||
IsLocal bool `gorm:"column:IS_LOCAL"`
|
IsLocal bool `gorm:"column:is_local"`
|
||||||
Op int `gorm:"column:OP"`
|
Op int `gorm:"column:op"`
|
||||||
Ts time.Time `gorm:"column:TS"`
|
Ts time.Time `gorm:"column:ts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName func respresent return table name of Station
|
// TableName func respresent return table name of Station
|
||||||
func (s *Station) TableName() string {
|
func (s *Station) TableName() string {
|
||||||
return "STATION"
|
return "station"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,25 @@
|
||||||
// Package orm define database data struct
|
// Package orm define database data struct
|
||||||
package orm
|
package orm
|
||||||
|
|
||||||
import "github.com/gofrs/uuid"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gofrs/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
// Topologic structure define topologic info set of circuit diagram
|
// Topologic structure define topologic info set of circuit diagram
|
||||||
type Topologic struct {
|
type Topologic struct {
|
||||||
ID int64 `gorm:"column:id"`
|
ID int64 `gorm:"column:id"`
|
||||||
Flag int `gorm:"column:flag"`
|
UUIDFrom uuid.UUID `gorm:"column:uuid_from"`
|
||||||
UUIDFrom uuid.UUID `gorm:"column:uuid_from"`
|
UUIDTo uuid.UUID `gorm:"column:uuid_to"`
|
||||||
UUIDTo uuid.UUID `gorm:"column:uuid_to"`
|
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
|
||||||
Comment string `gorm:"column:comment"`
|
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
|
// TableName func respresent return table name of Page
|
||||||
func (t *Topologic) TableName() string {
|
func (t *Topologic) TableName() string {
|
||||||
return "Topologic"
|
return "topologic"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,16 @@ import (
|
||||||
|
|
||||||
// Zone structure define abstracted info set of electrical zone
|
// Zone structure define abstracted info set of electrical zone
|
||||||
type Zone struct {
|
type Zone struct {
|
||||||
ID int64 `gorm:"column:ID;primaryKey"`
|
ID int64 `gorm:"column:id;primaryKey"`
|
||||||
GridID int64 `gorm:"column:GRID_ID"`
|
GridID int64 `gorm:"column:grid_id"`
|
||||||
TAGNAME string `gorm:"column:TAGNAME"`
|
TAGNAME string `gorm:"column:tagname"`
|
||||||
Name string `gorm:"column:NAME"`
|
Name string `gorm:"column:name"`
|
||||||
Description string `gorm:"column:DESCRIPTION"`
|
Description string `gorm:"column:description"`
|
||||||
Op int `gorm:"column:OP"`
|
Op int `gorm:"column:op"`
|
||||||
Ts time.Time `gorm:"column:TS"`
|
Ts time.Time `gorm:"column:ts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName func respresent return table name of Zone
|
// TableName func respresent return table name of Zone
|
||||||
func (z *Zone) TableName() string {
|
func (z *Zone) TableName() string {
|
||||||
return "ZONE"
|
return "zone"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
// Package sql define database sql statement
|
// Package sql define database sql statement
|
||||||
package sql
|
package sql
|
||||||
|
|
||||||
// RecursiveSQL define Topologic table recursive query statement
|
// RecursiveSQL define topologic table recursive query statement
|
||||||
var RecursiveSQL = `WITH RECURSIVE recursive_tree as (
|
var RecursiveSQL = `WITH RECURSIVE recursive_tree as (
|
||||||
SELECT uuid_from,uuid_to,flag
|
SELECT uuid_from,uuid_to,flag
|
||||||
FROM "Topologic"
|
FROM "topologic"
|
||||||
WHERE uuid_from = ?
|
WHERE uuid_from = ?
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT t.uuid_from,t.uuid_to,t.flag
|
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
|
JOIN recursive_tree rt ON t.uuid_from = rt.uuid_to
|
||||||
)
|
)
|
||||||
SELECT * FROM recursive_tree;`
|
SELECT * FROM recursive_tree;`
|
||||||
|
|
|
||||||
|
|
@ -43,14 +43,13 @@ func TestUserDao_CreateUser(t *testing.T) {
|
||||||
topologicInfo := &orm.Topologic{
|
topologicInfo := &orm.Topologic{
|
||||||
UUIDFrom: uuid.FromStringOrNil("70c190f2-8a60-42a9-b143-ec5f87e0aa6b"),
|
UUIDFrom: uuid.FromStringOrNil("70c190f2-8a60-42a9-b143-ec5f87e0aa6b"),
|
||||||
UUIDTo: uuid.FromStringOrNil("70c190f2-8a75-42a9-b166-ec5f87e0aa6b"),
|
UUIDTo: uuid.FromStringOrNil("70c190f2-8a75-42a9-b166-ec5f87e0aa6b"),
|
||||||
Comment: "test",
|
|
||||||
Flag: 1,
|
Flag: 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ud := dao2.NewUserDao(context.TODO())
|
// ud := dao2.NewUserDao(context.TODO())
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `Topologic`")).
|
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))
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
mock.ExpectCommit()
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue