1// Copyright 2013 Google Inc. All rights reserved. 2// Use of this source code is governed by the Apache 2.0 3// license that can be found in the LICENSE file. 4 5/* 6Package cloudsql exposes access to Google Cloud SQL databases. 7 8This package does not work in App Engine "flexible environment". 9 10This package is intended for MySQL drivers to make App Engine-specific 11connections. Applications should use this package through database/sql: 12Select a pure Go MySQL driver that supports this package, and use sql.Open 13with protocol "cloudsql" and an address of the Cloud SQL instance. 14 15A Go MySQL driver that has been tested to work well with Cloud SQL 16is the go-sql-driver: 17 import "database/sql" 18 import _ "github.com/go-sql-driver/mysql" 19 20 db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname") 21 22 23Another driver that works well with Cloud SQL is the mymysql driver: 24 import "database/sql" 25 import _ "github.com/ziutek/mymysql/godrv" 26 27 db, err := sql.Open("mymysql", "cloudsql:instance-name*dbname/user/password") 28 29 30Using either of these drivers, you can perform a standard SQL query. 31This example assumes there is a table named 'users' with 32columns 'first_name' and 'last_name': 33 34 rows, err := db.Query("SELECT first_name, last_name FROM users") 35 if err != nil { 36 log.Errorf(ctx, "db.Query: %v", err) 37 } 38 defer rows.Close() 39 40 for rows.Next() { 41 var firstName string 42 var lastName string 43 if err := rows.Scan(&firstName, &lastName); err != nil { 44 log.Errorf(ctx, "rows.Scan: %v", err) 45 continue 46 } 47 log.Infof(ctx, "First: %v - Last: %v", firstName, lastName) 48 } 49 if err := rows.Err(); err != nil { 50 log.Errorf(ctx, "Row error: %v", err) 51 } 52*/ 53package cloudsql 54 55import ( 56 "net" 57) 58 59// Dial connects to the named Cloud SQL instance. 60func Dial(instance string) (net.Conn, error) { 61 return connect(instance) 62} 63