// 2019 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. package raxSQL import ( "database/sql" "fmt" _ "github.com/denisenkom/go-mssqldb" _ "github.com/go-sql-driver/mysql" ) // NewMySQL takes a MySql database name and a passwordsafe Credential type. // It will initialize a MySql connection to the DB and test connectivity. func NewMySQL(h Host, dbName string, c user, opts ...Option) (*Client, error) { if c == nil { return nil, ErrNoKey } // Create the client and apply any options client := &Client{ port: mySqlPort, scheme: mySqlScheme, timeout: 240, } for _, opt := range opts { opt(client) } user, pass := c.Creds() var err error client.DB, err = sql.Open(client.scheme, fmt.Sprintf("%v:%v@tcp(%v:%v)/%v", user, pass, h, client.port, dbName)) if err != nil { return nil, ErrSQLOpen } // The Open sql method doesn't actually open a connection to the DB // To validate DSN data, we can use the Ping method with a sql.DB receiver if err = client.DB.Ping(); err != nil { return nil, ErrSQLPing } return client, nil }