-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add Google Cloud SQL support for App Engine. #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
// | ||
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// +build appengine | ||
|
||
package mysql | ||
|
||
import ( | ||
"appengine/cloudsql" | ||
"net" | ||
) | ||
|
||
func init() { | ||
if dials == nil { | ||
dials = make(map[string]dialFunc) | ||
} | ||
dials["cloudsql"] = func(cfg *config) (net.Conn, error) { | ||
return cloudsql.Dial(cfg.addr) | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,10 @@ import ( | |
// In general the driver is used via the database/sql package. | ||
type MySQLDriver struct{} | ||
|
||
type dialFunc func(*config) (net.Conn, error) | ||
|
||
var dials map[string]dialFunc | ||
|
||
// Open new Connection. | ||
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how | ||
// the DSN string is formated | ||
|
@@ -43,8 +47,12 @@ func (d *MySQLDriver) Open(dsn string) (driver.Conn, error) { | |
} | ||
|
||
// Connect to Server | ||
nd := net.Dialer{Timeout: mc.cfg.timeout} | ||
mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr) | ||
if dial, ok := dials[mc.cfg.net]; ok { | ||
mc.netConn, err = dial(mc.cfg) | ||
} else { | ||
nd := net.Dialer{Timeout: mc.cfg.timeout} | ||
mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if dial, ok := dials[mc.cfg.net]; ok {
mc.netConn, err = dial(mc.cfg)
} else {
nd := net.Dialer{Timeout: mc.cfg.timeout}
mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading from a nil-map is fine, you don't need to check if the map is nil first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know this technique. It's quite interesting. I have to apply it. |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dials["cloudsql"] = func(cfg *config) (net.Conn, error) {
seems valid anddials["cloudsql"] = dialFunc {
doesn' seem valid.I have tested using your code snippets.
http://play.golang.org/p/YhzUL3x96O
http://play.golang.org/p/j6nTx75jPx
Should I use following code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes