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