Skip to content

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

Merged
merged 1 commit into from
Nov 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ TCP on a remote host, e.g. Amazon RDS:
id:password@tcp(your-amazonaws-uri.com:3306)/dbname
```

Google Cloud SQL on App Engine:
```
user@cloudsql(project-id:instance-name)/dbname
```

TCP using default port (3306) on localhost:
```
user:password@tcp/dbname&charset=utf8mb4,utf8&sys_var=esc%40ped
Expand Down
25 changes: 25 additions & 0 deletions appengine.go
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)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func init() {
        if dials == nil {
                dials = make(map[string]dialFunc)
        }
        dials["cloudsql"] = dialFunc {
                return cloudsql.Dial(cfg.addr)
        }
}

Copy link
Contributor Author

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 and dials["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?

func init() {
        if dials == nil {
                dials = make(map[string]dialFunc)
        }
        dials["cloudsql"] = func(cfg *config) (net.Conn, error) {
                return cloudsql.Dial(cfg.addr)
        }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

12 changes: 10 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Copy link
Member

Choose a reason for hiding this comment

The 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)
}

Copy link
Member

Choose a reason for hiding this comment

The 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
http://play.golang.org/p/tBq3LMI0hc

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}
Expand Down