OSDN Git Service

5f96c363a0d5b23a19f9549fb33f5225a7d52fd5
[bytom/vapor.git] / vendor / github.com / jinzhu / gorm / dialect_sqlite3.go
1 package gorm
2
3 import (
4         "fmt"
5         "reflect"
6         "strings"
7         "time"
8 )
9
10 type sqlite3 struct {
11         commonDialect
12 }
13
14 func init() {
15         RegisterDialect("sqlite3", &sqlite3{})
16 }
17
18 func (sqlite3) GetName() string {
19         return "sqlite3"
20 }
21
22 // Get Data Type for Sqlite Dialect
23 func (s *sqlite3) DataTypeOf(field *StructField) string {
24         var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s)
25
26         if sqlType == "" {
27                 switch dataValue.Kind() {
28                 case reflect.Bool:
29                         sqlType = "bool"
30                 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
31                         if s.fieldCanAutoIncrement(field) {
32                                 field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
33                                 sqlType = "integer primary key autoincrement"
34                         } else {
35                                 sqlType = "integer"
36                         }
37                 case reflect.Int64, reflect.Uint64:
38                         if s.fieldCanAutoIncrement(field) {
39                                 field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
40                                 sqlType = "integer primary key autoincrement"
41                         } else {
42                                 sqlType = "bigint"
43                         }
44                 case reflect.Float32, reflect.Float64:
45                         sqlType = "real"
46                 case reflect.String:
47                         if size > 0 && size < 65532 {
48                                 sqlType = fmt.Sprintf("varchar(%d)", size)
49                         } else {
50                                 sqlType = "text"
51                         }
52                 case reflect.Struct:
53                         if _, ok := dataValue.Interface().(time.Time); ok {
54                                 sqlType = "datetime"
55                         }
56                 default:
57                         if IsByteArrayOrSlice(dataValue) {
58                                 sqlType = "blob"
59                         }
60                 }
61         }
62
63         if sqlType == "" {
64                 panic(fmt.Sprintf("invalid sql type %s (%s) for sqlite3", dataValue.Type().Name(), dataValue.Kind().String()))
65         }
66
67         if strings.TrimSpace(additionalType) == "" {
68                 return sqlType
69         }
70         return fmt.Sprintf("%v %v", sqlType, additionalType)
71 }
72
73 func (s sqlite3) HasIndex(tableName string, indexName string) bool {
74         var count int
75         s.db.QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = ? AND sql LIKE '%%INDEX %v ON%%'", indexName), tableName).Scan(&count)
76         return count > 0
77 }
78
79 func (s sqlite3) HasTable(tableName string) bool {
80         var count int
81         s.db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", tableName).Scan(&count)
82         return count > 0
83 }
84
85 func (s sqlite3) HasColumn(tableName string, columnName string) bool {
86         var count int
87         s.db.QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = ? AND (sql LIKE '%%\"%v\" %%' OR sql LIKE '%%%v %%');\n", columnName, columnName), tableName).Scan(&count)
88         return count > 0
89 }
90
91 func (s sqlite3) CurrentDatabase() (name string) {
92         var (
93                 ifaces   = make([]interface{}, 3)
94                 pointers = make([]*string, 3)
95                 i        int
96         )
97         for i = 0; i < 3; i++ {
98                 ifaces[i] = &pointers[i]
99         }
100         if err := s.db.QueryRow("PRAGMA database_list").Scan(ifaces...); err != nil {
101                 return
102         }
103         if pointers[1] != nil {
104                 name = *pointers[1]
105         }
106         return
107 }