-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathdatabase.go
195 lines (162 loc) · 4.61 KB
/
database.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Package mysql wraps the github.com/go-sql-driver/mysql MySQL driver. See
// https://github.com/upper/db/adapter/mysql for documentation, particularities and usage
// examples.
package mysql
import (
"reflect"
"strings"
"database/sql"
_ "github.com/go-sql-driver/mysql" // MySQL driver.
db "github.com/upper/db/v4"
"github.com/upper/db/v4/internal/sqladapter"
"github.com/upper/db/v4/internal/sqladapter/exql"
)
// database is the actual implementation of Database
type database struct {
}
func (*database) Template() *exql.Template {
return template
}
func (*database) OpenDSN(sess sqladapter.Session, dsn string) (*sql.DB, error) {
return sql.Open("mysql", dsn)
}
func (*database) Collections(sess sqladapter.Session) (collections []string, err error) {
q := sess.SQL().
Select("table_name").
From("information_schema.tables").
Where("table_schema = ?", sess.Name())
iter := q.Iterator()
defer iter.Close()
for iter.Next() {
var tableName string
if err := iter.Scan(&tableName); err != nil {
return nil, err
}
collections = append(collections, tableName)
}
if err := iter.Err(); err != nil {
return nil, err
}
return collections, nil
}
func (d *database) ConvertValue(in interface{}) interface{} {
switch v := in.(type) {
case *[]uint8:
return v
case []uint8:
return &v
case *map[string]interface{}:
return (*JSONMap)(v)
case map[string]interface{}:
return (*JSONMap)(&v)
}
dv := reflect.ValueOf(in)
if dv.IsValid() {
if dv.Type().Kind() == reflect.Ptr {
dv = dv.Elem()
}
switch dv.Kind() {
case reflect.Map:
if reflect.TypeOf(in).Kind() == reflect.Ptr {
w := reflect.ValueOf(in)
z := reflect.New(w.Elem().Type())
w.Elem().Set(z.Elem())
}
return &JSON{in}
case reflect.Slice:
return &JSON{in}
}
}
return in
}
func (*database) Err(err error) error {
if err != nil {
// This error is not exported so we have to check it by its string value.
s := err.Error()
if strings.Contains(s, `many connections`) {
return db.ErrTooManyClients
}
}
return err
}
func (*database) NewCollection() sqladapter.CollectionAdapter {
return &collectionAdapter{}
}
func (*database) LookupName(sess sqladapter.Session) (string, error) {
q := sess.SQL().
Select(db.Raw("DATABASE() AS name"))
iter := q.Iterator()
defer iter.Close()
if iter.Next() {
var name string
if err := iter.Scan(&name); err != nil {
return "", err
}
return name, nil
}
return "", iter.Err()
}
func (*database) TableExists(sess sqladapter.Session, name string) error {
q := sess.SQL().
Select("table_name").
From("information_schema.tables").
Where("table_schema = ? AND table_name = ?", sess.Name(), name)
iter := q.Iterator()
defer iter.Close()
if iter.Next() {
var name string
if err := iter.Scan(&name); err != nil {
return err
}
return nil
}
if err := iter.Err(); err != nil {
return err
}
return db.ErrCollectionDoesNotExist
}
func (*database) PrimaryKeys(sess sqladapter.Session, tableName string) ([]string, error) {
q := sess.SQL().
Select("k.column_name").
From("information_schema.key_column_usage AS k").
Where(`
k.constraint_name = 'PRIMARY'
AND k.table_schema = ?
AND k.table_name = ?
`, sess.Name(), tableName).
OrderBy("k.ordinal_position")
iter := q.Iterator()
defer iter.Close()
pk := []string{}
for iter.Next() {
var k string
if err := iter.Scan(&k); err != nil {
return nil, err
}
pk = append(pk, k)
}
if err := iter.Err(); err != nil {
return nil, err
}
return pk, nil
}