Skip to content

Commit 7c08711

Browse files
committed
Add Google Cloud SQL support for App Engine.
It uses Cloud SQL as a database if it runs on Google App Engine with using cloudsql DSN. appengine.go includes a build constraint for Google App Engine (GAE) (http://blog.golang.org/the-app-engine-sdk-and-workspaces-gopath) to avoid an error outside App Engine and provide an exception. Using Google Cloud SQL with App Engine GO SDK is not supported yet. It is a SDK limitation.
1 parent 2d629c0 commit 7c08711

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ TCP on a remote host, e.g. Amazon RDS:
242242
id:password@tcp(your-amazonaws-uri.com:3306)/dbname
243243
```
244244

245+
Google Cloud SQL on App Engine:
246+
```
247+
user@cloudsql(project-id:instance-name)/dbname
248+
```
249+
245250
TCP using default port (3306) on localhost:
246251
```
247252
user:password@tcp/dbname&charset=utf8mb4,utf8&sys_var=esc%40ped

appengine.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
// +build appengine
10+
11+
package mysql
12+
13+
import (
14+
"appengine/cloudsql"
15+
"net"
16+
)
17+
18+
func init() {
19+
if dials == nil {
20+
dials = make(map[string]dialFunc)
21+
}
22+
dials["cloudsql"] = func(cfg *config) (net.Conn, error) {
23+
return cloudsql.Dial(cfg.addr)
24+
}
25+
}

driver.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import (
2626
// In general the driver is used via the database/sql package.
2727
type MySQLDriver struct{}
2828

29+
type dialFunc func(*config) (net.Conn, error)
30+
31+
var dials map[string]dialFunc
32+
2933
// Open new Connection.
3034
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
3135
// the DSN string is formated
@@ -43,8 +47,12 @@ func (d *MySQLDriver) Open(dsn string) (driver.Conn, error) {
4347
}
4448

4549
// Connect to Server
46-
nd := net.Dialer{Timeout: mc.cfg.timeout}
47-
mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr)
50+
if dial, ok := dials[mc.cfg.net]; ok {
51+
mc.netConn, err = dial(mc.cfg)
52+
} else {
53+
nd := net.Dialer{Timeout: mc.cfg.timeout}
54+
mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr)
55+
}
4856
if err != nil {
4957
return nil, err
5058
}

0 commit comments

Comments
 (0)