github.com/kotovmak/go-admin@v1.1.1/modules/db/performer.go (about)

     1  // Copyright 2019 GoAdmin Core Team. All rights reserved.
     2  // Use of this source code is governed by a Apache-2.0 style
     3  // license that can be found in the LICENSE file.
     4  
     5  package db
     6  
     7  import (
     8  	"context"
     9  	"database/sql"
    10  	"regexp"
    11  	"strings"
    12  )
    13  
    14  // CommonQuery is a common method of query.
    15  func CommonQuery(db *sql.DB, query string, args ...interface{}) ([]map[string]interface{}, error) {
    16  
    17  	rs, err := db.Query(query, args...)
    18  
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  
    23  	defer func() {
    24  		if rs != nil {
    25  			_ = rs.Close()
    26  		}
    27  	}()
    28  
    29  	col, colErr := rs.Columns()
    30  
    31  	if colErr != nil {
    32  		return nil, colErr
    33  	}
    34  
    35  	typeVal, err := rs.ColumnTypes()
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	// TODO: regular expressions for sqlite, use the dialect module
    41  	// tell the drive to reduce the performance loss
    42  	results := make([]map[string]interface{}, 0)
    43  
    44  	r, _ := regexp.Compile(`\\((.*)\\)`)
    45  	for rs.Next() {
    46  		var colVar = make([]interface{}, len(col))
    47  		for i := 0; i < len(col); i++ {
    48  			typeName := strings.ToUpper(r.ReplaceAllString(typeVal[i].DatabaseTypeName(), ""))
    49  			SetColVarType(&colVar, i, typeName)
    50  		}
    51  		result := make(map[string]interface{})
    52  		if scanErr := rs.Scan(colVar...); scanErr != nil {
    53  			return nil, scanErr
    54  		}
    55  		for j := 0; j < len(col); j++ {
    56  			typeName := strings.ToUpper(r.ReplaceAllString(typeVal[j].DatabaseTypeName(), ""))
    57  			SetResultValue(&result, col[j], colVar[j], typeName)
    58  		}
    59  		results = append(results, result)
    60  	}
    61  	if err := rs.Err(); err != nil {
    62  		return nil, err
    63  	}
    64  	return results, nil
    65  }
    66  
    67  // CommonExec is a common method of exec.
    68  func CommonExec(db *sql.DB, query string, args ...interface{}) (sql.Result, error) {
    69  
    70  	rs, err := db.Exec(query, args...)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return rs, nil
    75  }
    76  
    77  // CommonQueryWithTx is a common method of query.
    78  func CommonQueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error) {
    79  
    80  	rs, err := tx.Query(query, args...)
    81  
    82  	if err != nil {
    83  		panic(err)
    84  	}
    85  
    86  	defer func() {
    87  		if rs != nil {
    88  			_ = rs.Close()
    89  		}
    90  	}()
    91  
    92  	col, colErr := rs.Columns()
    93  
    94  	if colErr != nil {
    95  		return nil, colErr
    96  	}
    97  
    98  	typeVal, err := rs.ColumnTypes()
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	// TODO: regular expressions for sqlite, use the dialect module
   104  	// tell the drive to reduce the performance loss
   105  	results := make([]map[string]interface{}, 0)
   106  
   107  	r, _ := regexp.Compile(`\\((.*)\\)`)
   108  	for rs.Next() {
   109  		var colVar = make([]interface{}, len(col))
   110  		for i := 0; i < len(col); i++ {
   111  			typeName := strings.ToUpper(r.ReplaceAllString(typeVal[i].DatabaseTypeName(), ""))
   112  			SetColVarType(&colVar, i, typeName)
   113  		}
   114  		result := make(map[string]interface{})
   115  		if scanErr := rs.Scan(colVar...); scanErr != nil {
   116  			return nil, scanErr
   117  		}
   118  		for j := 0; j < len(col); j++ {
   119  			typeName := strings.ToUpper(r.ReplaceAllString(typeVal[j].DatabaseTypeName(), ""))
   120  			SetResultValue(&result, col[j], colVar[j], typeName)
   121  		}
   122  		results = append(results, result)
   123  	}
   124  	if err := rs.Err(); err != nil {
   125  		return nil, err
   126  	}
   127  	return results, nil
   128  }
   129  
   130  // CommonExecWithTx is a common method of exec.
   131  func CommonExecWithTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error) {
   132  	rs, err := tx.Exec(query, args...)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	return rs, nil
   137  }
   138  
   139  // CommonBeginTxWithLevel starts a transaction with given transaction isolation level and db connection.
   140  func CommonBeginTxWithLevel(db *sql.DB, level sql.IsolationLevel) *sql.Tx {
   141  	tx, err := db.BeginTx(context.Background(), &sql.TxOptions{Isolation: level})
   142  	if err != nil {
   143  		panic(err)
   144  	}
   145  	return tx
   146  }