-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.go
51 lines (42 loc) · 1.08 KB
/
helpers.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
package godatabend
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
)
var (
escaper = strings.NewReplacer(`\`, `\\`, `'`, `\'`)
dateFormat = "2006-01-02"
timeFormat = "2006-01-02 15:04:05"
dateTime64Format = "2006-01-02 15:04:05.999999999"
)
func escape(s string) string {
return escaper.Replace(s)
}
func quote(s string) string {
return "'" + s + "'"
}
func formatTime(value time.Time) string {
return quote(value.Format(timeFormat))
}
func formatDate(value time.Time) string {
return quote(value.Format(dateFormat))
}
func getTableFromInsertQuery(query string) (string, error) {
if !strings.Contains(query, "insert") && !strings.Contains(query, "INSERT") {
return "", errors.New("wrong insert statement")
}
splitQuery := strings.Split(query, " ")
if len(splitQuery) > 2 {
return strings.TrimSpace(splitQuery[2]), nil
}
return "", errors.New("wrong insert")
}
func generateDescTable(query string) (string, error) {
table, err := getTableFromInsertQuery(query)
if err != nil {
return "", err
}
return fmt.Sprintf("DESC %s", table), nil
}