-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialect.go
More file actions
165 lines (146 loc) · 4.01 KB
/
Copy pathdialect.go
File metadata and controls
165 lines (146 loc) · 4.01 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package postgres
import (
"fmt"
"reflect"
"strings"
"time"
_ "github.com/lib/pq"
"github.com/ngorm/common"
"github.com/ngorm/ngorm/dialects"
"github.com/ngorm/ngorm/model"
)
func init() {
dialects.Register(&Postgres{})
}
type Postgres struct {
common.Dialect
}
func (Postgres) GetName() string {
return "postgres"
}
func (Postgres) BindVar(i int) string {
return fmt.Sprintf("$%v", i)
}
func (Postgres) DataTypeOf(field *model.StructField) (string, error) {
dataValue, sqlType, size, additionalType :=
model.ParseFieldStructForDialect(field)
if sqlType == "" {
switch dataValue.Kind() {
case reflect.Bool:
sqlType = "boolean"
case reflect.Int, reflect.Int8,
reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint8,
reflect.Uint16, reflect.Uint32,
reflect.Uintptr:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "serial"
} else {
sqlType = "integer"
}
case reflect.Int64, reflect.Uint64:
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
sqlType = "bigserial"
} else {
sqlType = "bigint"
}
case reflect.Float32, reflect.Float64:
sqlType = "numeric"
case reflect.String:
if _, ok := field.TagSettings["SIZE"]; !ok {
size = 0 // if SIZE haven't been set, use `text` as the default type, as there are no performance different
}
if size > 0 && size < 65532 {
sqlType = fmt.Sprintf("varchar(%d)", size)
} else {
sqlType = "text"
}
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "timestamp with time zone"
}
case reflect.Map:
if dataValue.Type().Name() == "Hstore" {
sqlType = "hstore"
}
default:
if isByteArrayOrSlice(dataValue) {
sqlType = "bytea"
} else if isUUID(dataValue) {
sqlType = "uuid"
}
}
}
if sqlType == "" {
return "", fmt.Errorf("invalid sql type %s (%s) for postgres",
dataValue.Type().Name(), dataValue.Kind().String())
}
if strings.TrimSpace(additionalType) == "" {
return sqlType, nil
}
return fmt.Sprintf("%v %v", sqlType, additionalType), nil
}
func (s Postgres) HasIndex(tableName string, indexName string) bool {
var count int
s.DB.QueryRow(
"SELECT count(*) FROM pg_indexes WHERE tablename = $1 AND indexname = $2",
tableName, indexName).Scan(&count)
return count > 0
}
func (s Postgres) HasForeignKey(tableName string, foreignKeyName string) bool {
var count int
query := `
SELECT Count(con.conname)
FROM pg_constraint con
WHERE $1 :: regclass :: oid = con.conrelid
AND con.conname = $2
AND con.contype = 'f'
`
s.DB.QueryRow(query, tableName, foreignKeyName).Scan(&count)
return count > 0
}
func (s Postgres) HasTable(tableName string) bool {
var count int
query := `
SELECT Count(*)
FROM information_schema.tables
WHERE table_name = $1
AND table_type = 'BASE TABLE'
`
s.DB.QueryRow(query, tableName).Scan(&count)
return count > 0
}
func (s Postgres) HasColumn(tableName string, columnName string) bool {
var count int
query := `
SELECT Count(*)
FROM information_schema.columns
WHERE table_name = $1
AND column_name = $2
`
s.DB.QueryRow(query, tableName, columnName).Scan(&count)
return count > 0
}
func (s Postgres) CurrentDatabase() (name string) {
s.DB.QueryRow("SELECT CURRENT_DATABASE()").Scan(&name)
return
}
func (s Postgres) LastInsertIDReturningSuffix(tableName, key string) string {
return fmt.Sprintf("RETURNING %v.%v", tableName, key)
}
func (Postgres) SupportLastInsertID() bool {
return false
}
func isByteArrayOrSlice(value reflect.Value) bool {
return (value.Kind() == reflect.Array || value.Kind() == reflect.Slice) && value.Type().Elem() == reflect.TypeOf(uint8(0))
}
func isUUID(value reflect.Value) bool {
if value.Kind() != reflect.Array || value.Type().Len() != 16 {
return false
}
typename := value.Type().Name()
lower := strings.ToLower(typename)
return "uuid" == lower || "guid" == lower
}