// 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" "net/url" _ "github.com/denisenkom/go-mssqldb" _ "github.com/go-sql-driver/mysql" "github.com/sirupsen/logrus" ) // NewMSSQL takes the MSSQL driver and connects to the GDCI data-store. func NewMSSQL(h Host, c user, opts ...Option) (*Client, error) { if c == nil { return nil, ErrNoKey } // Create the client and apply any options client := &Client{ port: msSqlPort, scheme: msSqlScheme, timeout: 240, } for _, opt := range opts { opt(client) } query := url.Values{} query.Add("parseTime", "true") query.Add("connection timeout", fmt.Sprintf("%d", client.timeout)) u := &url.URL{ Scheme: client.scheme, User: url.UserPassword(c.Creds()), Host: fmt.Sprintf("%s:%d", h, client.port), RawQuery: query.Encode(), } var err error client.DB, err = sql.Open(u.Scheme, u.String()) if err != nil { logrus.Error(err) 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 { logrus.Error(err) return nil, ErrSQLPing } return client, nil }