-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
103 lines (90 loc) · 3.23 KB
/
helper.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
//
// //// SQLite Cloud
// //////////// ///
// /// /// /// Product : SQLite Cloud GO SDK
// /// /// /// Version : 1.0.0
// // /// /// /// Date : 2021/08/31
// /// /// /// /// Author : Andreas Pfeil
// /// /// /// ///
// /// ////////// /// /// Description : GO Functions for parsing
// //// /// /// SQLite Cloud values and
// //// ////////// /// enquoting strings.
// //// ////
// //// /////
// /// Copyright : 2021 by SQLite Cloud Inc.
//
// -----------------------------------------------------------------------TAB=2
package sqlitecloud
import (
"errors"
"fmt"
"strings"
)
// Helper functions
func escapeString(s, quote string) string {
repeatedquote := quote + quote
s = strings.Replace(s, repeatedquote, quote, -1)
s = strings.Replace(s, quote, repeatedquote, -1)
return s
}
// SQCloudEnquoteString enquotes the given string if necessary and returns the result as a news created string.
// If the given string contains a '"', the '"' is properly escaped.
// If the given string contains one or more spaces, the whole string is enquoted with '"'
func SQCloudEnquoteString(s string) string {
s = strings.TrimSpace(s)
singlequote := "'"
doublequote := "\""
if !strings.Contains(s, singlequote) && !strings.Contains(s, doublequote) {
if strings.Contains(s, " ") {
return fmt.Sprintf("'%s'", s)
}
return s
}
if l := len(s); l > 1 {
for _, q := range []string{singlequote, doublequote} {
if strings.HasPrefix(s, q) && strings.HasSuffix(s, q) {
unquoted := s[1 : l-1]
if !strings.Contains(unquoted, q) {
return s
}
return q + escapeString(unquoted, q) + q
}
}
}
for _, q := range []string{singlequote, doublequote} {
if !strings.Contains(s, q) {
return q + s + q
}
}
return singlequote + escapeString(s, singlequote) + singlequote
}
/*
// SQCloudEnquoteString enquotes the given string if necessary and returns the result as a news created string.
// If the given string contains a '"', the '"' is properly escaped.
// If the given string contains one or more spaces, the whole string is enquoted with '"'
func SQCloudEnquoteString(Token string) string {
Token = strings.Replace(Token, "\"", "\"\"", -1)
Token = strings.Replace(Token, "'", "''", -1)
if strings.Contains(Token, " ") {
return fmt.Sprintf("\"%s\"", Token)
}
return Token
}
*/
// parseBool parses the given string value and tries to interpret the value as a bool.
// true is returned if the value is "TRUE", "ENABLED" or 1.
// false is returned if the value is "FALSE", "DISABLED" or 0.
// The specified defaultValue is returned, if the given string value was an emptry string.
// An error is returned in any other case.
func parseBool(value string, defaultValue bool) (bool, error) {
switch strings.ToUpper(strings.TrimSpace(value)) {
case "FALSE", "DISABLED", "0":
return false, nil
case "TRUE", "ENABLED", "1":
return true, nil
case "":
return defaultValue, nil
default:
return false, errors.New("ERROR: Not a Boolean value")
}
}