cloudsql.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. Package cloudsql exposes access to Google Cloud SQL databases.
  6. This package does not work on Managed VMs.
  7. This package is intended for MySQL drivers to make App Engine-specific
  8. connections. Applications should use this package through database/sql:
  9. Select a pure Go MySQL driver that supports this package, and use sql.Open
  10. with protocol "cloudsql" and an address of the Cloud SQL instance.
  11. A Go MySQL driver that has been tested to work well with Cloud SQL
  12. is the go-sql-driver:
  13. import "database/sql"
  14. import _ "github.com/go-sql-driver/mysql"
  15. db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname")
  16. Another driver that works well with Cloud SQL is the mymysql driver:
  17. import "database/sql"
  18. import _ "github.com/ziutek/mymysql/godrv"
  19. db, err := sql.Open("mymysql", "cloudsql:instance-name*dbname/user/password")
  20. Using either of these drivers, you can perform a standard SQL query.
  21. This example assumes there is a table named 'users' with
  22. columns 'first_name' and 'last_name':
  23. rows, err := db.Query("SELECT first_name, last_name FROM users")
  24. if err != nil {
  25. log.Errorf(ctx, "db.Query: %v", err)
  26. }
  27. defer rows.Close()
  28. for rows.Next() {
  29. var firstName string
  30. var lastName string
  31. if err := rows.Scan(&firstName, &lastName); err != nil {
  32. log.Errorf(ctx, "rows.Scan: %v", err)
  33. continue
  34. }
  35. log.Infof(ctx, "First: %v - Last: %v", firstName, lastName)
  36. }
  37. if err := rows.Err(); err != nil {
  38. log.Errorf(ctx, "Row error: %v", err)
  39. }
  40. */
  41. package cloudsql
  42. import (
  43. "net"
  44. )
  45. // Dial connects to the named Cloud SQL instance.
  46. func Dial(instance string) (net.Conn, error) {
  47. return connect(instance)
  48. }