github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/TableInsertFuncs.go (about) 1 package funcs 2 3 import ( 4 "fmt" 5 "time" 6 7 dbconn "github.com/mdaxf/iac/databases" 8 ) 9 10 type TableInsertFuncs struct { 11 } 12 13 // Execute executes the TableInsertFuncs function. 14 // It retrieves the input values, sets up the necessary variables, 15 // and performs the table insert operation using the provided database connection. 16 // The execution can be canceled if an error occurs during the process. 17 // The function also logs debug and error messages for troubleshooting purposes. 18 // Finally, it sets the output values for further processing. 19 20 func (cf *TableInsertFuncs) Execute(f *Funcs) { 21 startTime := time.Now() 22 defer func() { 23 elapsed := time.Since(startTime) 24 f.iLog.PerformanceWithDuration("engine.funcs.TableInsertFuncs.Execute", elapsed) 25 }() 26 defer func() { 27 if err := recover(); err != nil { 28 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.TableInsertFuncs.Execute with error: %s", err)) 29 f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.TableInsertFuncs.Execute with error: %s", err)) 30 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.TableInsertFuncs.Execute with error: %s", err) 31 return 32 } 33 }() 34 35 f.iLog.Debug(fmt.Sprintf("Start process %s : %s", "TableInsertFuncs.Execute", f.Fobj.Name)) 36 37 namelist, valuelist, _ := f.SetInputs() 38 39 columnList := []string{} 40 columnvalueList := []string{} 41 columndatatypeList := []int{} 42 43 var execution bool 44 execution = true 45 46 TableName := "" 47 for i, name := range namelist { 48 if name == "TableName" { 49 TableName = valuelist[i] 50 } else if name == "Execution" { 51 if valuelist[i] == "false" { 52 execution = false 53 } 54 } else { 55 columnList = append(columnList, name) 56 columnvalueList = append(columnvalueList, valuelist[i]) 57 columndatatypeList = append(columndatatypeList, int(f.Fobj.Inputs[i].Datatype)) 58 } 59 60 } 61 62 f.iLog.Debug(fmt.Sprintf("TableInsertFuncs columnList: %s", columnList)) 63 f.iLog.Debug(fmt.Sprintf("TableInsertFuncs columnvalueList: %s", columnvalueList)) 64 f.iLog.Debug(fmt.Sprintf("TableInsertFuncs columndatatypeList: %v", columndatatypeList)) 65 f.iLog.Debug(fmt.Sprintf("TableInsertFuncs TableName: %s", TableName)) 66 67 if execution == false { 68 f.iLog.Debug(fmt.Sprintf("TableInsertFuncs Execution is false, skip execution")) 69 return 70 } 71 72 if TableName == "" { 73 f.iLog.Error(fmt.Sprintf("Error in TableInsertFuncs.Execute: %s", "TableName is empty")) 74 return 75 } 76 77 if len(columnList) == 0 { 78 f.iLog.Error(fmt.Sprintf("Error in TableInsertFuncs.Execute: %s", "columnList is empty")) 79 return 80 } 81 82 var user string 83 84 if f.SystemSession["User"] != nil { 85 user = f.SystemSession["User"].(string) 86 } else { 87 user = "System" 88 } 89 90 // Create SELECT clause with aliases 91 dboperation := dbconn.NewDBOperation(user, f.DBTx, "TableInsert Function") 92 93 output, err := dboperation.TableInsert(TableName, columnList, columnvalueList) 94 if err != nil { 95 f.iLog.Error(fmt.Sprintf("Error in TableInsert Execute: %s", err.Error())) 96 return 97 } 98 f.iLog.Debug(fmt.Sprintf("TableInsert Execution Result: %v", output)) 99 100 outputs := make(map[string]interface{}) 101 outputs["Identify"] = output 102 f.SetOutputs(outputs) 103 } 104 105 // Validate validates the TableInsertFuncs. 106 // It measures the performance of the function and logs the duration. 107 // It returns true if the validation is successful, otherwise it returns an error. 108 // It also logs the performance of the function. 109 func (cf *TableInsertFuncs) Validate(f *Funcs) (bool, error) { 110 startTime := time.Now() 111 defer func() { 112 elapsed := time.Since(startTime) 113 f.iLog.PerformanceWithDuration("engine.funcs.TableInsertFuncs.Validate", elapsed) 114 }() 115 /*defer func() { 116 if err := recover(); err != nil { 117 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.TableInsertFuncs.Validate with error: %s", err)) 118 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.TableInsertFuncs.Validate with error: %s", err) 119 return 120 } 121 }() */ 122 123 return true, nil 124 } 125 126 // Testfunction is a function that performs a test operation. 127 // It measures the performance of the function and logs the duration. 128 // It returns a boolean value indicating the success of the test and an error if any. 129 130 func (cf *TableInsertFuncs) Testfunction(f *Funcs) (bool, error) { 131 startTime := time.Now() 132 defer func() { 133 elapsed := time.Since(startTime) 134 f.iLog.PerformanceWithDuration("engine.funcs.TableInsertFuncs.Testfunction", elapsed) 135 }() 136 /*defer func() { 137 if err := recover(); err != nil { 138 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.TableInsertFuncs.Testfunction with error: %s", err)) 139 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.TableInsertFuncs.Testfunction with error: %s", err) 140 return 141 } 142 }() */ 143 144 return true, nil 145 }