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

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