Commit da1d1d418f9eca11760bcc47115cac849d4b86b5
1 parent
9033286899
Exists in
master
and in
1 other branch
SQL EqualString formater
Showing
1 changed file
with
7 additions
and
0 deletions
Show diff stats
format_utility.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "time" | 4 | "time" |
5 | "fmt" | 5 | "fmt" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | // UnixToDate converts given Unix time to local time in format and returns result: | 8 | // UnixToDate converts given Unix time to local time in format and returns result: |
9 | // YYYY-MM-DD hh:mm:ss +zzzz UTC | 9 | // YYYY-MM-DD hh:mm:ss +zzzz UTC |
10 | func UnixToDate(unix int64) time.Time { | 10 | func UnixToDate(unix int64) time.Time { |
11 | return time.Unix(unix, 0) | 11 | return time.Unix(unix, 0) |
12 | } | 12 | } |
13 | 13 | ||
14 | // DateToUnix converts given date in Unix timestamp. | 14 | // DateToUnix converts given date in Unix timestamp. |
15 | func DateToUnix(date interface{}) int64 { | 15 | func DateToUnix(date interface{}) int64 { |
16 | if date != nil { | 16 | if date != nil { |
17 | t := date.(time.Time) | 17 | t := date.(time.Time) |
18 | return t.Unix() | 18 | return t.Unix() |
19 | 19 | ||
20 | } | 20 | } |
21 | return 0 | 21 | return 0 |
22 | } | 22 | } |
23 | 23 | ||
24 | // EqualQuotes encapsulates given string in SQL 'equal' statement and returns result. | 24 | // EqualQuotes encapsulates given string in SQL 'equal' statement and returns result. |
25 | // Example: "hello" -> " = 'hello'" | 25 | // Example: "hello" -> " = 'hello'" |
26 | func EqualQuotes(stmt string) string { | 26 | func EqualQuotes(stmt string) string { |
27 | if stmt != "" { | 27 | if stmt != "" { |
28 | stmt = fmt.Sprintf(" = '%s'", stmt) | 28 | stmt = fmt.Sprintf(" = '%s'", stmt) |
29 | } | 29 | } |
30 | return stmt | 30 | return stmt |
31 | } | 31 | } |
32 | 32 | ||
33 | func EqualString(stmt string) string { | ||
34 | if stmt != "" { | ||
35 | stmt = fmt.Sprintf(" = %s", stmt) | ||
36 | } | ||
37 | return stmt | ||
38 | } | ||
39 | |||
33 | // LikeQuotes encapsulates given string in SQL 'like' statement and returns result. | 40 | // LikeQuotes encapsulates given string in SQL 'like' statement and returns result. |
34 | // Example: "hello" -> " LIKE UPPER('%hello%')" | 41 | // Example: "hello" -> " LIKE UPPER('%hello%')" |
35 | func LikeQuotes(stmt string) string { | 42 | func LikeQuotes(stmt string) string { |
36 | if stmt != "" { | 43 | if stmt != "" { |
37 | stmt = fmt.Sprintf(" LIKE UPPER('%s%s%s')", "%", stmt, "%") | 44 | stmt = fmt.Sprintf(" LIKE UPPER('%s%s%s')", "%", stmt, "%") |
38 | } | 45 | } |
39 | return stmt | 46 | return stmt |
40 | } | 47 | } |
41 | 48 | ||
42 | 49 |