forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_mysql.go
340 lines (291 loc) · 8.89 KB
/
db_mysql.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bookshelf
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"github.com/go-sql-driver/mysql"
)
var createTableStatements = []string{
`CREATE DATABASE IF NOT EXISTS library DEFAULT CHARACTER SET = 'utf8' DEFAULT COLLATE 'utf8_general_ci';`,
`USE library;`,
`CREATE TABLE IF NOT EXISTS books (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NULL,
author VARCHAR(255) NULL,
publishedDate VARCHAR(255) NULL,
imageUrl VARCHAR(255) NULL,
description TEXT NULL,
createdBy VARCHAR(255) NULL,
createdById VARCHAR(255) NULL,
PRIMARY KEY (id)
)`,
}
// mysqlDB persists books to a MySQL instance.
type mysqlDB struct {
conn *sql.DB
list *sql.Stmt
listBy *sql.Stmt
insert *sql.Stmt
get *sql.Stmt
update *sql.Stmt
delete *sql.Stmt
}
// Ensure mysqlDB conforms to the BookDatabase interface.
var _ BookDatabase = &mysqlDB{}
type MySQLConfig struct {
// Optional.
Username, Password string
// Host of the MySQL instance.
//
// If set, UnixSocket should be unset.
Host string
// Port of the MySQL instance.
//
// If set, UnixSocket should be unset.
Port int
// UnixSocket is the filepath to a unix socket.
//
// If set, Host and Port should be unset.
UnixSocket string
}
// dataStoreName returns a connection string suitable for sql.Open.
func (c MySQLConfig) dataStoreName(databaseName string) string {
var cred string
// [username[:password]@]
if c.Username != "" {
cred = c.Username
if c.Password != "" {
cred = cred + ":" + c.Password
}
cred = cred + "@"
}
if c.UnixSocket != "" {
return fmt.Sprintf("%sunix(%s)/%s", cred, c.UnixSocket, databaseName)
}
return fmt.Sprintf("%stcp([%s]:%d)/%s", cred, c.Host, c.Port, databaseName)
}
// newMySQLDB creates a new BookDatabase backed by a given MySQL server.
func newMySQLDB(config MySQLConfig) (BookDatabase, error) {
// Check database and table exists. If not, create it.
if err := config.ensureTableExists(); err != nil {
return nil, err
}
conn, err := sql.Open("mysql", config.dataStoreName("library"))
if err != nil {
return nil, fmt.Errorf("mysql: could not get a connection: %v", err)
}
if err := conn.Ping(); err != nil {
conn.Close()
return nil, fmt.Errorf("mysql: could not establish a good connection: %v", err)
}
db := &mysqlDB{
conn: conn,
}
// Prepared statements. The actual SQL queries are in the code near the
// relevant method (e.g. addBook).
if db.list, err = conn.Prepare(listStatement); err != nil {
return nil, fmt.Errorf("mysql: prepare list: %v", err)
}
if db.listBy, err = conn.Prepare(listByStatement); err != nil {
return nil, fmt.Errorf("mysql: prepare listBy: %v", err)
}
if db.get, err = conn.Prepare(getStatement); err != nil {
return nil, fmt.Errorf("mysql: prepare get: %v", err)
}
if db.insert, err = conn.Prepare(insertStatement); err != nil {
return nil, fmt.Errorf("mysql: prepare insert: %v", err)
}
if db.update, err = conn.Prepare(updateStatement); err != nil {
return nil, fmt.Errorf("mysql: prepare update: %v", err)
}
if db.delete, err = conn.Prepare(deleteStatement); err != nil {
return nil, fmt.Errorf("mysql: prepare delete: %v", err)
}
return db, nil
}
// Close closes the database, freeing up any resources.
func (db *mysqlDB) Close() {
db.conn.Close()
}
// rowScanner is implemented by sql.Row and sql.Rows
type rowScanner interface {
Scan(dest ...interface{}) error
}
// scanBook reads a book from a sql.Row or sql.Rows
func scanBook(s rowScanner) (*Book, error) {
var (
id int64
title sql.NullString
author sql.NullString
publishedDate sql.NullString
imageURL sql.NullString
description sql.NullString
createdBy sql.NullString
createdByID sql.NullString
)
if err := s.Scan(&id, &title, &author, &publishedDate, &imageURL,
&description, &createdBy, &createdByID); err != nil {
return nil, err
}
book := &Book{
ID: id,
Title: title.String,
Author: author.String,
PublishedDate: publishedDate.String,
ImageURL: imageURL.String,
Description: description.String,
CreatedBy: createdBy.String,
CreatedByID: createdByID.String,
}
return book, nil
}
const listStatement = `SELECT * FROM books ORDER BY title`
// ListBooks returns a list of books, ordered by title.
func (db *mysqlDB) ListBooks() ([]*Book, error) {
rows, err := db.list.Query()
if err != nil {
return nil, err
}
defer rows.Close()
var books []*Book
for rows.Next() {
book, err := scanBook(rows)
if err != nil {
return nil, fmt.Errorf("mysql: could not read row: %v", err)
}
books = append(books, book)
}
return books, nil
}
const listByStatement = `
SELECT * FROM books
WHERE createdById = ? ORDER BY title`
// ListBooksCreatedBy returns a list of books, ordered by title, filtered by
// the user who created the book entry.
func (db *mysqlDB) ListBooksCreatedBy(userID string) ([]*Book, error) {
if userID == "" {
return db.ListBooks()
}
rows, err := db.listBy.Query(userID)
if err != nil {
return nil, err
}
defer rows.Close()
var books []*Book
for rows.Next() {
book, err := scanBook(rows)
if err != nil {
return nil, fmt.Errorf("mysql: could not read row: %v", err)
}
books = append(books, book)
}
return books, nil
}
const getStatement = "SELECT * FROM books WHERE id = ?"
// GetBook retrieves a book by its ID.
func (db *mysqlDB) GetBook(id int64) (*Book, error) {
book, err := scanBook(db.get.QueryRow(id))
if err == sql.ErrNoRows {
return nil, fmt.Errorf("mysql: could not find book with id %d", id)
}
if err != nil {
return nil, fmt.Errorf("mysql: could not get book: %v", err)
}
return book, nil
}
const insertStatement = `
INSERT INTO books (
title, author, publishedDate, imageUrl, description, createdBy, createdById
) VALUES (?, ?, ?, ?, ?, ?, ?)`
// AddBook saves a given book, assigning it a new ID.
func (db *mysqlDB) AddBook(b *Book) (id int64, err error) {
r, err := execAffectingOneRow(db.insert, b.Title, b.Author, b.PublishedDate,
b.ImageURL, b.Description, b.CreatedBy, b.CreatedByID)
if err != nil {
return 0, err
}
lastInsertID, err := r.LastInsertId()
if err != nil {
return 0, fmt.Errorf("mysql: could not get last insert ID: %v", err)
}
return lastInsertID, nil
}
const deleteStatement = `DELETE FROM books WHERE id = ?`
// DeleteBook removes a given book by its ID.
func (db *mysqlDB) DeleteBook(id int64) error {
if id == 0 {
return errors.New("mysql: book with unassigned ID passed into deleteBook")
}
_, err := execAffectingOneRow(db.delete, id)
return err
}
const updateStatement = `
UPDATE books
SET title=?, author=?, publishedDate=?, imageUrl=?, description=?,
createdBy=?, createdById=?
WHERE id = ?`
// UpdateBook updates the entry for a given book.
func (db *mysqlDB) UpdateBook(b *Book) error {
if b.ID == 0 {
return errors.New("mysql: book with unassigned ID passed into updateBook")
}
_, err := execAffectingOneRow(db.update, b.Title, b.Author, b.PublishedDate,
b.ImageURL, b.Description, b.CreatedBy, b.CreatedByID, b.ID)
return err
}
// ensureTableExists checks the table exists. If not, it creates it.
func (config MySQLConfig) ensureTableExists() error {
conn, err := sql.Open("mysql", config.dataStoreName(""))
if err != nil {
return fmt.Errorf("mysql: could not get a connection: %v", err)
}
defer conn.Close()
// Check the connection.
if conn.Ping() == driver.ErrBadConn {
return fmt.Errorf("mysql: could not connect to the database. " +
"could be bad address, or this address is not whitelisted for access.")
}
if _, err := conn.Exec("USE library"); err != nil {
// MySQL error 1049 is "database does not exist"
if mErr, ok := err.(*mysql.MySQLError); ok && mErr.Number == 1049 {
return createTable(conn)
}
}
if _, err := conn.Exec("DESCRIBE books"); err != nil {
// MySQL error 1146 is "table does not exist"
if mErr, ok := err.(*mysql.MySQLError); ok && mErr.Number == 1146 {
return createTable(conn)
}
// Unknown error.
return fmt.Errorf("mysql: could not connect to the database: %v", err)
}
return nil
}
// createTable creates the table, and if necessary, the database.
func createTable(conn *sql.DB) error {
for _, stmt := range createTableStatements {
_, err := conn.Exec(stmt)
if err != nil {
return err
}
}
return nil
}
// execAffectingOneRow executes a given statement, expecting one row to be affected.
func execAffectingOneRow(stmt *sql.Stmt, args ...interface{}) (sql.Result, error) {
r, err := stmt.Exec(args...)
if err != nil {
return r, fmt.Errorf("mysql: could not execute statement: %v", err)
}
rowsAffected, err := r.RowsAffected()
if err != nil {
return r, fmt.Errorf("mysql: could not get rows affected: %v", err)
} else if rowsAffected != 1 {
return r, fmt.Errorf("mysql: expected 1 row affected, got %d", rowsAffected)
}
return r, nil
}