-
Notifications
You must be signed in to change notification settings - Fork 0
/
column.go
102 lines (84 loc) · 1.79 KB
/
column.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
package sqllite
import (
"fmt"
"strings"
pangu "github.com/eshu0/pangu/pkg/structures"
_ "github.com/mattn/go-sqlite3"
)
type CType int
const (
Unknown CType = iota
Integer
Text
Numeric
VarChar
)
/*
func (d CType) String() string {
return [...]string{"INTEGER", "TEXT", "NUMERIC", "VARCHAR"}[d]
}
*/
func CTypeFromString(typestring string) CType {
if strings.Contains(typestring, "VARCHAR") {
return VarChar
} else {
switch typestring {
case "INTEGER":
return Integer
case "TEXT":
return Text
case "VARCHAR":
return VarChar
case "NUMERIC":
return Numeric
}
}
return Unknown
}
type Column struct {
PTableName string
Name string
CType CType
NotNull int
Default string
PrimaryKey int
DefaultValue []byte
}
func (col *Column) ToProperty(table *Table) *pangu.Property {
prop := &pangu.Property{}
// Comment for the proptyer
prop.Comment = fmt.Sprintf("%s (SQL TYPE: %s)", col.Name, col.CType)
// property name
prop.Name = strings.Title(col.Name)
// if column is not null we cwill always return a json reponse
if col.NotNull == 1 {
prop.Json = "`json:\"" + strings.ToLower(col.Name) + "\"`"
} else {
prop.Json = "`json:\"" + strings.ToLower(col.Name) + ",omitempty\"`"
}
// this is not an identifier
prop.IsIdentifier = false
prop.Constant = table.CreateConstant(col)
col.SetTypeFromCType(&prop)
return prop
}
func (col *Column) SetTypeFromCType(prop *pangu.Property) {
switch col.CType {
case Integer:
prop.GType = pangu.GTint64
prop.UpdateValue = "11"
break
case Text:
prop.GType = pangu.GTstring
prop.UpdateValue = "\"Updated\""
break
case VarChar:
prop.GType = pangu.GTstring
prop.UpdateValue = "\"Updated\""
break
case Numeric:
prop.GType = pangu.GTfloat64
prop.UpdateValue = "1.11"
break
}
}