forked from shanemhansen/goper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.go
65 lines (58 loc) · 1.5 KB
/
tables.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
package goper
import (
"bytes"
"regexp"
"strings"
)
var camelingRegex = regexp.MustCompile("[0-9A-Za-z]+")
// A Table represents the metadata associated with a table (columns, datatypes, etc)
type Table struct {
Name string
Columns []Column
}
// A column has a name and a database type (hackily converted to a gotype)
type Column struct {
Name string
DbType string
}
var typemap map[string]string = map[string]string{
"int": "*int64",
"integer": "*int64",
"decimal": "*float64", //fixme
"varchar": "*string",
"text": "*string",
"float": "*float64",
"real": "*float64",
"datetime": "*string",
"timestamp": "*string",
"enum": "*string",
"date": "*string",
"double": "float64",
"double precision": "*float64",
"boolean": "*bool",
"char": "*string",
"bit": "*int64",
"longblob": "*int64",
"blob": "[]byte",
"bytea": "[]byte",
"BIGINT": "*int64",
}
// Return the go type a database column should be mapped to.
// We always use pointers to handle null
func (this *Column) GoType() string {
for key, value := range typemap {
if strings.Contains(strings.ToLower(this.DbType), key) {
return value
}
}
panic("Unknown type" + this.DbType)
}
// A helper function that maps strings like product_variant to ProductVariant
func CamelCase(src string) string {
byteSrc := []byte(src)
chunks := camelingRegex.FindAll(byteSrc, -1)
for idx, val := range chunks {
chunks[idx] = bytes.Title(val)
}
return string(bytes.Join(chunks, nil))
}