github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/TableUpdateFuncs.go (about) 1 package funcs 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 dbconn "github.com/mdaxf/iac/databases" 9 ) 10 11 type TableUpdateFuncs struct { 12 } 13 14 // Execute executes the TableUpdateFuncs function. 15 // It retrieves the inputs, sets up the necessary variables, and performs the table update operation. 16 // If any errors occur during the execution, it logs the error and returns. 17 // Finally, it sets the output values and returns. 18 19 func (cf *TableUpdateFuncs) Execute(f *Funcs) { 20 startTime := time.Now() 21 defer func() { 22 elapsed := time.Since(startTime) 23 f.iLog.PerformanceWithDuration("engine.funcs.TableUpdateFuncs.Execute", elapsed) 24 }() 25 defer func() { 26 if err := recover(); err != nil { 27 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.TableUpdateFuncs.Execute with error: %s", err)) 28 f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.TableUpdateFuncs.Execute with error: %s", err)) 29 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.TableUpdateFuncs.Execute with error: %s", err) 30 return 31 } 32 }() 33 34 f.iLog.Debug(fmt.Sprintf("Start process %s : %s", "TableUpdateFuncs.Execute", f.Fobj.Name)) 35 36 namelist, valuelist, _ := f.SetInputs() 37 38 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs content: %s", f.Fobj.Content)) 39 40 columnList := []string{} 41 columnvalueList := []string{} 42 columndatatypeList := []int{} 43 keycolumnList := []string{} 44 keycolumnvalueList := []string{} 45 keycolumndatatypeList := []int{} 46 TableName := "" 47 48 for i, name := range namelist { 49 if name == "TableName" { 50 TableName = valuelist[i] 51 } else if strings.HasSuffix(name, "KEY") { 52 name = strings.Replace(name+"_|", "KEY_|", "", -1) 53 keycolumnList = append(keycolumnList, name) 54 keycolumnvalueList = append(keycolumnvalueList, valuelist[i]) 55 keycolumndatatypeList = append(keycolumndatatypeList, int(f.Fobj.Inputs[i].Datatype)) 56 } else { 57 columnList = append(columnList, name) 58 columnvalueList = append(columnvalueList, valuelist[i]) 59 columndatatypeList = append(columndatatypeList, int(f.Fobj.Inputs[i].Datatype)) 60 } 61 62 } 63 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs columnList: %s", columnList)) 64 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs columnvalueList: %s", columnvalueList)) 65 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs columndatatypeList: %v", columndatatypeList)) 66 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs keycolumnList: %s", keycolumnList)) 67 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs keycolumnvalueList: %s", keycolumnvalueList)) 68 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs keycolumndatatypeList: %v", keycolumndatatypeList)) 69 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs TableName: %s", TableName)) 70 71 if TableName == "" { 72 f.iLog.Error(fmt.Sprintf("Error in TableInsertFuncs.Execute: %s", "TableName is empty")) 73 return 74 } 75 76 if len(columnList) == 0 { 77 f.iLog.Error(fmt.Sprintf("Error in TableInsertFuncs.Execute: %s", "columnList is empty")) 78 return 79 } 80 81 if len(keycolumnList) == 0 { 82 f.iLog.Error(fmt.Sprintf("Error in TableInsertFuncs.Execute: %s", "keycolumnList is empty")) 83 return 84 } 85 86 Where := "" 87 for i, column := range keycolumnList { 88 if Where != "" { 89 Where = fmt.Sprintf("%s AND ", Where) 90 } 91 // f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs Column: %s Value: %s keycolumndatatypeList: %s", column, keycolumnvalueList[i], keycolumndatatypeList[i])) 92 /*switch keycolumndatatypeList[i] { 93 case int(types.String): 94 case int(types.DateTime): 95 Where = fmt.Sprintf("%s %s ='%s'", Where, column, keycolumnvalueList[i]) 96 default: 97 Where = fmt.Sprintf("%s %s =%s", Where, column, keycolumnvalueList[i]) 98 } */ 99 value := strings.Replace(keycolumnvalueList[i], "'", "", -1) 100 101 Where = fmt.Sprintf("%s %s ='%s'", Where, column, value) 102 // f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs Where: %s", Where)) 103 } 104 f.iLog.Debug(fmt.Sprintf("TableUpdateFuncs Where: %s", Where)) 105 var user string 106 107 if f.SystemSession["User"] != nil { 108 user = f.SystemSession["User"].(string) 109 } else { 110 user = "System" 111 } 112 113 dboperation := dbconn.NewDBOperation(user, f.DBTx, "TableUpdate Function") 114 115 output, err := dboperation.TableUpdate(TableName, columnList, columnvalueList, columndatatypeList, Where) 116 if err != nil { 117 f.iLog.Error(fmt.Sprintf("Error in TableUpdate Execute: %s", err.Error())) 118 return 119 } 120 f.iLog.Debug(fmt.Sprintf("TableUpdate Execution Result: %v", output)) 121 122 outputs := make(map[string]interface{}) 123 outputs["RowCount"] = output 124 f.SetOutputs(outputs) 125 } 126 127 // Validate is a method of the TableUpdateFuncs struct that validates the function. 128 // It measures the performance of the function and logs the duration. 129 // It returns a boolean value indicating the validation result and an error if any. 130 131 func (cf *TableUpdateFuncs) Validate(f *Funcs) (bool, error) { 132 startTime := time.Now() 133 defer func() { 134 elapsed := time.Since(startTime) 135 f.iLog.PerformanceWithDuration("engine.funcs.TableUpdateFuncs.Validate", elapsed) 136 }() 137 /*defer func() { 138 if err := recover(); err != nil { 139 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.TableUpdateFuncs.Validate with error: %s", err)) 140 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.TableUpdateFuncs.Validate with error: %s", err) 141 return 142 } 143 }() */ 144 145 return true, nil 146 } 147 148 // Testfunction is a function that performs a test operation. 149 // It measures the performance of the function and handles any errors that occur. 150 // It returns a boolean value indicating the success of the test and an error if any. 151 152 func (cf *TableUpdateFuncs) Testfunction(f *Funcs) (bool, error) { 153 startTime := time.Now() 154 defer func() { 155 elapsed := time.Since(startTime) 156 f.iLog.PerformanceWithDuration("engine.funcs.TableUpdateFuncs.Testfunction", elapsed) 157 }() 158 defer func() { 159 if err := recover(); err != nil { 160 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.TableUpdateFuncs.Testfunction with error: %s", err)) 161 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.TableUpdateFuncs.Testfunction with error: %s", err) 162 return 163 } 164 }() 165 166 return true, nil 167 }