github.com/aldelo/common@v1.5.1/helper-db.go (about) 1 package helper 2 3 /* 4 * Copyright 2020-2023 Aldelo, LP 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 import ( 20 "database/sql" 21 "time" 22 ) 23 24 // FromNullString casts sql null string variable to string variable, if null, blank string is returned 25 func FromNullString(s sql.NullString) string { 26 if !s.Valid { 27 return "" 28 } 29 30 return s.String 31 } 32 33 // ToNullString sets string value into NullString output 34 func ToNullString(s string, emptyAsNull bool) sql.NullString { 35 if emptyAsNull == true { 36 if LenTrim(s) > 0 { 37 return sql.NullString{Valid: true, String: s} 38 } 39 40 return sql.NullString{Valid: false, String: ""} 41 } 42 43 // if not emptyAsNull 44 return sql.NullString{Valid: true, String: s} 45 } 46 47 // LenTrimNullString returns string length 48 func LenTrimNullString(s sql.NullString) int { 49 if !s.Valid { 50 return 0 51 } 52 53 return LenTrim(s.String) 54 } 55 56 // FromNullInt64 casts sql null int64 variable to int64 variable, if null, 0 is returned 57 func FromNullInt64(d sql.NullInt64) int64 { 58 if !d.Valid { 59 return 0 60 } 61 62 return d.Int64 63 } 64 65 // ToNullInt64 sets int64 value into NullInt64 output 66 func ToNullInt64(d int64, emptyAsNull bool) sql.NullInt64 { 67 if emptyAsNull == true { 68 if d == 0 { 69 return sql.NullInt64{Valid: false, Int64: 0} 70 } 71 72 return sql.NullInt64{Valid: true, Int64: d} 73 } 74 75 // not using emptyAsNull 76 return sql.NullInt64{Valid: true, Int64: d} 77 } 78 79 // FromNullInt casts sql NullInt32 into int variable, if null, 0 is returned 80 func FromNullInt(d sql.NullInt32) int { 81 if !d.Valid { 82 return 0 83 } 84 85 return int(d.Int32) 86 } 87 88 // ToNullInt sets int value into NullInt32 output 89 func ToNullInt(d int, emptyAsNull bool) sql.NullInt32 { 90 if emptyAsNull == true { 91 if d == 0 { 92 return sql.NullInt32{Valid: false, Int32: 0} 93 } 94 95 return sql.NullInt32{Valid: true, Int32: int32(d)} 96 } 97 98 // not using emptyAsNull 99 return sql.NullInt32{Valid: true, Int32: int32(d)} 100 } 101 102 // FromNullFloat64 casts sql null float64 variable to float64 variable, if null, 0.00 is returned 103 func FromNullFloat64(d sql.NullFloat64) float64 { 104 if !d.Valid { 105 return 0.00 106 } 107 108 return d.Float64 109 } 110 111 // ToNullFloat64 sets float64 into NullFloat64 output 112 func ToNullFloat64(d float64, emptyAsNull bool) sql.NullFloat64 { 113 if emptyAsNull == true { 114 if d == 0.00 { 115 return sql.NullFloat64{Valid: false, Float64: 0.00} 116 } 117 118 return sql.NullFloat64{Valid: true, Float64: d} 119 } 120 121 // not using emptyAsNull 122 return sql.NullFloat64{Valid: true, Float64: d} 123 } 124 125 // FromNullFloat32 casts sql null float64 into float32 variable 126 func FromNullFloat32(d sql.NullFloat64) float32 { 127 return float32(FromNullFloat64(d)) 128 } 129 130 // ToNullFloat32 sets float32 into NullFloat64 output 131 func ToNullFloat32(d float32, emptyAsNull bool) sql.NullFloat64 { 132 return ToNullFloat64(float64(d), emptyAsNull) 133 } 134 135 // FromNullBool casts sql null bool variable to bool variable, if null, false is returned 136 func FromNullBool(b sql.NullBool) bool { 137 if !b.Valid { 138 return false 139 } 140 141 return b.Bool 142 } 143 144 // ToNullBool sets bool into NullBool output 145 func ToNullBool(b bool) sql.NullBool { 146 return sql.NullBool{Valid: true, Bool: b} 147 } 148 149 // FromNullTime parses string into time.Time 150 func FromNullTime(t sql.NullTime) time.Time { 151 if t.Valid == false { 152 return time.Time{} 153 } 154 155 return t.Time 156 } 157 158 // ToNullTime sets time.Time into NullTime output 159 func ToNullTime(t time.Time) sql.NullTime { 160 if t.IsZero() == true { 161 return sql.NullTime{Valid: false, Time: time.Time{}} 162 } 163 164 return sql.NullTime{Valid: true, Time: t} 165 }