github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/storeprocfuncs.go (about)

     1  package funcs
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	dbconn "github.com/mdaxf/iac/databases"
     8  )
     9  
    10  type StoreProcFuncs struct {
    11  }
    12  
    13  func (cf *StoreProcFuncs) Execute(f *Funcs) {
    14  	// Execute executes the store procedure function.
    15  	// It measures the performance duration and handles any panics that occur during execution.
    16  	// It also sets the inputs, retrieves the user from the system session, and creates a SELECT clause with aliases.
    17  	// Then, it performs the query operation and sets the outputs.
    18  
    19  	startTime := time.Now()
    20  	defer func() {
    21  		elapsed := time.Since(startTime)
    22  		f.iLog.PerformanceWithDuration("engine.funcs.StoreProcedure.Execute", elapsed)
    23  	}()
    24  	defer func() {
    25  		if err := recover(); err != nil {
    26  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.StoreProcedure.Execute with error: %s", err))
    27  			f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.StoreProcedure.Execute with error: %s", err))
    28  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.StoreProcedure.Execute with error: %s", err)
    29  			return
    30  		}
    31  	}()
    32  
    33  	f.iLog.Debug(fmt.Sprintf("Start process %s : %s", "StoreProcFuncs.Execute", f.Fobj.Name))
    34  
    35  	namelist, _, inputs := f.SetInputs()
    36  
    37  	var user string
    38  
    39  	if f.SystemSession["User"] != nil {
    40  		user = f.SystemSession["User"].(string)
    41  	} else {
    42  		user = "System"
    43  	}
    44  
    45  	// Create SELECT clause with aliases
    46  	dboperation := dbconn.NewDBOperation(user, f.DBTx, "Execute Query Function")
    47  
    48  	outputs, ColumnCount, RowCount, err := dboperation.QuerybyList(f.Fobj.Content, namelist, inputs, f.Fobj.Inputs)
    49  	if err != nil {
    50  		f.iLog.Error(fmt.Sprintf("Error in QueryFuncs.Execute: %s", err.Error()))
    51  		return
    52  	}
    53  	outputs["ColumnCount"] = []interface{}{ColumnCount}
    54  	outputs["RowCount"] = []interface{}{RowCount}
    55  	f.SetOutputs(f.convertMap(outputs))
    56  }
    57  
    58  func (cf *StoreProcFuncs) Validate(f *Funcs) (bool, error) {
    59  	startTime := time.Now()
    60  	defer func() {
    61  		elapsed := time.Since(startTime)
    62  		f.iLog.PerformanceWithDuration("engine.funcs.StoreProcedure.Validate", elapsed)
    63  	}()
    64  	defer func() {
    65  		if err := recover(); err != nil {
    66  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.StoreProcedure.Validate with error: %s", err))
    67  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.StoreProcedure.Validate with error: %s", err)
    68  			return
    69  		}
    70  	}()
    71  
    72  	return true, nil
    73  }
    74  
    75  // Testfunction is a function that performs a test operation.
    76  // It measures the performance of the function and handles any errors that occur.
    77  // It returns a boolean value indicating the success of the test and an error if any.
    78  
    79  func (cf *StoreProcFuncs) Testfunction(f *Funcs) (bool, error) {
    80  	startTime := time.Now()
    81  	defer func() {
    82  		elapsed := time.Since(startTime)
    83  		f.iLog.PerformanceWithDuration("engine.funcs.StoreProcedure.Testfunction", elapsed)
    84  	}()
    85  	/*defer func() {
    86  		if err := recover(); err != nil {
    87  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.StoreProcedure.Testfunction with error: %s", err))
    88  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.StoreProcedure.Testfunction with error: %s", err)
    89  			return
    90  		}
    91  	}() */
    92  
    93  	return true, nil
    94  }