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