@@ -2,14 +2,23 @@ package mysql
22
33import (
44 "fmt"
5+ "strconv"
56 "strings"
67
7- mysqlc "github.com/ziutek/mymysql/thrsafe"
8+ mysqlc "github.com/ziutek/mymysql/mysql"
9+ mysqlts "github.com/ziutek/mymysql/thrsafe"
810
911 "github.com/hashicorp/terraform/helper/schema"
1012 "github.com/hashicorp/terraform/terraform"
1113)
1214
15+ type providerConfiguration struct {
16+ Conn mysqlc.Conn
17+ VersionMajor uint
18+ VersionMinor uint
19+ VersionPatch uint
20+ }
21+
1322func Provider () terraform.ResourceProvider {
1423 return & schema.Provider {
1524 Schema : map [string ]* schema.Schema {
@@ -69,21 +78,65 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
6978 proto = "unix"
7079 }
7180
72- // mysqlc is the thread-safe implementation of mymysql, so we can
81+ // mysqlts is the thread-safe implementation of mymysql, so we can
7382 // safely re-use the same connection between multiple parallel
7483 // operations.
75- conn := mysqlc .New (proto , "" , endpoint , username , password )
84+ conn := mysqlts .New (proto , "" , endpoint , username , password )
7685
7786 err := conn .Connect ()
7887 if err != nil {
7988 return nil , err
8089 }
8190
82- return conn , nil
91+ major , minor , patch , err := mysqlVersion (conn )
92+ if err != nil {
93+ return nil , err
94+ }
95+
96+ return & providerConfiguration {
97+ Conn : conn ,
98+ VersionMajor : major ,
99+ VersionMinor : minor ,
100+ VersionPatch : patch ,
101+ }, nil
83102}
84103
85104var identQuoteReplacer = strings .NewReplacer ("`" , "``" )
86105
87106func quoteIdentifier (in string ) string {
88107 return fmt .Sprintf ("`%s`" , identQuoteReplacer .Replace (in ))
89108}
109+
110+ func mysqlVersion (conn mysqlc.Conn ) (uint , uint , uint , error ) {
111+ rows , _ , err := conn .Query ("SELECT VERSION()" )
112+ if err != nil {
113+ return 0 , 0 , 0 , err
114+ }
115+ if len (rows ) == 0 {
116+ return 0 , 0 , 0 , fmt .Errorf ("SELECT VERSION() returned an empty set" )
117+ }
118+
119+ versionString := rows [0 ].Str (0 )
120+ version := strings .Split (versionString , "." )
121+ invalidVersionErr := fmt .Errorf ("Invalid major.minor.patch in %q" , versionString )
122+ if len (version ) != 3 {
123+ return 0 , 0 , 0 , invalidVersionErr
124+ }
125+
126+ major , err := strconv .ParseUint (version [0 ], 10 , 32 )
127+ if err != nil {
128+ return 0 , 0 , 0 , invalidVersionErr
129+ }
130+
131+ minor , err := strconv .ParseUint (version [1 ], 10 , 32 )
132+ if err != nil {
133+ return 0 , 0 , 0 , invalidVersionErr
134+ }
135+
136+ patch , err := strconv .ParseUint (version [2 ], 10 , 32 )
137+ if err != nil {
138+ return 0 , 0 , 0 , invalidVersionErr
139+ }
140+
141+ return uint (major ), uint (minor ), uint (patch ), nil
142+ }
0 commit comments