-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqlLite.go
65 lines (52 loc) · 1.37 KB
/
sqlLite.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
package dbdriver
/*
sqlite 驱动单元,封装了pgears的数据库中特殊处理部分
*/
import(
"database/sql"
"reflect"
_ "github.com/mattn/go-sqlite3"
"errors"
)
func SqliteConnection(url string) (*sql.DB, error){
conn, err := sql.Open("sqlite3_custom",url)
if err != nil {
return nil ,err
}
return conn ,err
}
/**
确认tag的dbfieldType是否正确
*/
func CheckType(GoType *reflect.Type , GoName string,RealType string) (string,bool){
str := getSqlite3DbFieldTypeName(GoType,GoName)
if str != RealType{
return "",false
}
return str,true
}
/*通过反射实体字段类型翻译成数据库字段类型*/
func getSqlite3DbFieldTypeName(Gotype *reflect.Type,GoName string) string{
if fldTyp,ok := (*Gotype).FieldByName(GoName) ; ok{
if fldTyp.Type.Name() == "Time" {
return ("timestamp")
}
kd := fldTyp.Type.Kind()
switch kd{
default :
break
case reflect.Invalid:
break
case reflect.Int,reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64 ,reflect.Uintptr,reflect.Bool :{
return ("integer")
}
case reflect.String ,reflect.Interface, reflect.Map,reflect.Ptr:{
return ("text")
}
case reflect.Float32,reflect.Float64,reflect.Complex64,reflect.Complex128:{
return ("float")
}
}
}
panic(errors.New("this field is not in Type") )
}