github.com/kaydxh/golang@v0.0.131/pkg/database/mysql/sql_exec.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package mysql 23 24 import ( 25 "context" 26 "database/sql" 27 "fmt" 28 29 runtime_ "github.com/kaydxh/golang/go/runtime" 30 time_ "github.com/kaydxh/golang/go/time" 31 "github.com/sirupsen/logrus" 32 33 "github.com/jmoiron/sqlx" 34 ) 35 36 /* 37 func ExecContext( 38 ctx context.Context, 39 timeout time.Duration, 40 db *sqlx.DB, 41 query string, 42 h func(ctx context.Context, db *sqlx.DB, query string) error, 43 ) error { 44 if h == nil { 45 return nil 46 } 47 ctx, cancel := context_.WithTimeout(ctx, timeout) 48 defer cancel() 49 50 return h(ctx, db, query) 51 } 52 */ 53 54 func ExecContext( 55 ctx context.Context, 56 query string, 57 arg interface{}, 58 tx *sqlx.Tx, 59 db *sqlx.DB, 60 ) (rows int64, err error) { 61 tc := time_.New(true) 62 caller := runtime_.GetShortCaller() 63 logger := logrus.WithField("caller", caller) 64 65 clean := func() { 66 tc.Tick(caller) 67 logger.WithField("cost", tc.String()).Infof("SQL EXECL") 68 if err != nil { 69 logger.WithError(err).Errorf("sql: %s", query) 70 } 71 } 72 defer clean() 73 74 result, err := NamedExecContext(ctx, query, arg, tx, db) 75 if err != nil { 76 return 0, err 77 } 78 79 rows, err = result.RowsAffected() 80 if err != nil { 81 return 0, err 82 } 83 logger.Infof("affected rows: %v", rows) 84 85 return rows, nil 86 } 87 88 // pointer struct model for dest 89 func SelectNamedContext( 90 ctx context.Context, 91 query string, 92 arg interface{}, 93 dest interface{}, 94 db *sqlx.DB, 95 ) (err error) { 96 tc := time_.New(true) 97 caller := runtime_.GetShortCaller() 98 logger := logrus.WithField("caller", caller) 99 100 clean := func() { 101 tc.Tick(caller) 102 logger.WithField("cost", tc.String()).Infof("SQL EXECL") 103 if err != nil { 104 logger.WithError(err).Errorf("sql: %s", query) 105 } 106 } 107 defer clean() 108 109 // prepare 110 ns, err := db.PrepareNamedContext(ctx, query) 111 if err != nil { 112 return err 113 } 114 defer ns.Close() 115 116 // query 117 return ns.SelectContext(ctx, dest, arg) 118 } 119 120 func GetCountContext(ctx context.Context, query string, arg interface{}, db *sqlx.DB) (count uint32, err error) { 121 tc := time_.New(true) 122 caller := runtime_.GetShortCaller() 123 logger := logrus.WithField("caller", caller) 124 125 clean := func() { 126 tc.Tick(caller) 127 logger.WithField("cost", tc.String()).Infof("SQL EXECL") 128 if err != nil { 129 logger.WithError(err).Errorf("sql: %s", query) 130 } 131 } 132 defer clean() 133 134 ns, err := db.PrepareNamedContext(ctx, query) 135 if err != nil { 136 return 0, err 137 } 138 defer ns.Close() 139 140 err = ns.QueryRowContext(ctx, arg).Scan(&count) 141 if err != nil { 142 return 0, err 143 } 144 145 return count, nil 146 147 } 148 149 func NamedExecContext( 150 ctx context.Context, 151 query string, 152 arg interface{}, 153 tx *sqlx.Tx, 154 db *sqlx.DB, 155 ) (sql.Result, error) { 156 157 if tx != nil { 158 return tx.NamedExecContext(ctx, query, arg) 159 } 160 161 if db != nil { 162 return db.NamedExecContext(ctx, query, arg) 163 } 164 165 return nil, fmt.Errorf("db is nil") 166 } 167 168 func PrepareNamedContext(ctx context.Context, 169 query string, 170 tx *sqlx.Tx, 171 db *sqlx.DB, 172 ) (*sqlx.NamedStmt, error) { 173 174 if tx != nil { 175 return tx.PrepareNamedContext(ctx, query) 176 } 177 178 if db != nil { 179 return db.PrepareNamedContext(ctx, query) 180 } 181 182 return nil, fmt.Errorf("db is nil") 183 }