// 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" "errors" "fmt" ) const ( // HostEBIDataMart is the DNS name for the Datamart SQL instance // ebi-datamart.rackspace.corp // - Because we can't resolve .corp on the containers HostEBIDataMart Host = "10.10.97.94" // HostODSCore is the DNS name for the ODS-CORE SQL instance // ebi-ods-core.rackspace.corp // - Because we can't resolve .corp on the containers HostODSCore Host = "10.10.97.8" // HostEBIGDCI is the DNS name for the GDCI EBI SQL instance HostEBIGDCI Host = "10.10.252.20" mySqlPort = 3306 mySqlScheme = "mysql" msSqlPort = 1433 msSqlScheme = "sqlserver" ) var ( // ErrNoKey is the error type received when a call to the New* methods does not contain // a passwordsafe credential ErrNoKey = errors.New("cannot create MSSQL connection without a passwordsafe project") // ErrSQLOpen is the error type returned when we encounter an error building the connection // information for a SQL connection. ErrSQLOpen = errors.New("error establishing SQL connection") // ErrSQLPing is the error type returned when we have successfully created the connection // details, but we are unable to reach the database. ErrSQLPing = errors.New("error reaching the SQL database") ) type ( // Host is a package specific string type to identify a // SQL Host connection name. Host string // Client struct represents the MySql connection // type client for the mySql library Client struct { DB *sql.DB port int timeout int scheme string } // AuthSQL type is used for local SQL authentication. This type satisfies the // User interface. AuthSQL struct { Username string Password string } // AuthWindows type is used for Windows Domain authentication. This type satisfies the // User interface. AuthWindows struct { Username string Password string Domain string } // Option defines a signature for a basic option passed into the // SQL clients. Option func(c *Client) // user interface is used to abstract the type of user // that is required for SQL authentication. user interface { Creds() (string, string) } ) // Creds method to satisfy the User interface for AuthSQL func (a AuthSQL) Creds() (string, string) { return a.Username, a.Password } // Creds method to satisfy the User interface for AuthWindows func (a AuthWindows) Creds() (string, string) { return fmt.Sprintf("%s\\%s", a.Domain, a.Username), a.Password } // WithPort allows for a custom port number to be passed into the // client configuration. func WithPort(p int) Option { return func(c *Client) { c.port = p } } // WithPort allows for a custom timeout (in seconds) to be passed into the // client configuration. func WithTimeout(t int) Option { return func(c *Client) { c.timeout = t } } // WithScheme allows for a custom scheme to be passed into the // client configuration. func WithScheme(s string) Option { return func(c *Client) { c.scheme = s } } // String will convert the Host type back into // a regular string. func (h Host) String() string { return string(h) }