2017-06-26 21:52:06 -07:00
/*
Copyright 2016 The Rook Authors. All rights reserved.
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.
*/
2017-05-25 10:26:03 -07:00
package utils
import (
"database/sql"
2017-09-25 15:40:28 -07:00
"fmt"
2017-05-25 10:26:03 -07:00
"math/rand"
"strconv"
"time"
2018-07-26 10:52:19 -06:00
2017-07-05 12:26:18 -07:00
// import mysql driver
2017-06-26 21:52:06 -07:00
_ "github.com/go-sql-driver/mysql"
"github.com/icrowley/fake"
2017-05-25 10:26:03 -07:00
)
2018-10-14 12:10:08 +02:00
// MySQLHelper contains pointer to MySqlDB and wrappers for basic object on mySql database
2017-07-05 12:26:18 -07:00
type MySQLHelper struct {
2017-05-25 10:26:03 -07:00
DB * sql . DB
}
2018-07-26 10:52:19 -06:00
// CreateNewMySQLHelper creates a s3 client for specified endpoint and creds
2017-07-05 12:26:18 -07:00
func CreateNewMySQLHelper ( username string , password string , url string , dbname string ) * MySQLHelper {
2017-05-25 10:26:03 -07:00
dataSourceName := username + ":" + password + "@tcp(" + url + ")/" + dbname
db , err := sql . Open ( "mysql" , dataSourceName )
if err != nil {
panic ( err )
}
2017-07-05 12:26:18 -07:00
return & MySQLHelper { DB : db }
2017-05-25 10:26:03 -07:00
}
2017-07-05 12:26:18 -07:00
2018-10-14 12:10:08 +02:00
// CloseConnection function closes mysql connection
2017-07-05 12:26:18 -07:00
func ( h * MySQLHelper ) CloseConnection () {
2017-06-08 12:19:48 -07:00
h . DB . Close ()
}
2017-07-05 12:26:18 -07:00
2018-10-14 12:10:08 +02:00
// PingSuccess function is used check connection to a database
2017-07-05 12:26:18 -07:00
func ( h * MySQLHelper ) PingSuccess () bool {
2017-05-25 10:26:03 -07:00
inc := 0
2017-06-08 12:19:48 -07:00
for inc < 30 {
2017-05-25 10:26:03 -07:00
err := h . DB . Ping ()
if err == nil {
return true
}
inc ++
2017-06-08 12:19:48 -07:00
time . Sleep ( 3 * time . Second )
2017-05-25 10:26:03 -07:00
}
return false
}
2018-10-14 12:10:08 +02:00
// CreateTable func create sample Table
2017-07-05 12:26:18 -07:00
func ( h * MySQLHelper ) CreateTable () sql . Result {
2018-01-05 12:24:12 -08:00
result , err := h . DB . Exec ( "CREATE TABLE LONGHAUL (id int NOT NULL AUTO_INCREMENT,number int,data1 varchar(10000),data2 varchar(10000),data3 varchar(10000),data4 varchar(10000),data5 varchar(10000)" +
2017-05-25 10:26:03 -07:00
", PRIMARY KEY (id) )" )
if err != nil {
panic ( err )
}
return result
}
2018-10-14 12:10:08 +02:00
// InsertRandomData Inserts random Data into the table
2018-01-05 12:24:12 -08:00
func ( h * MySQLHelper ) InsertRandomData ( dataSize int ) sql . Result {
stmtIns , err := h . DB . Prepare ( "INSERT INTO LONGHAUL (number, data1, data2, data3, data4, data5) VALUES ( ?, ?, ?, ?, ? )" ) // ? = placeholder
2017-05-25 10:26:03 -07:00
if err != nil {
panic ( err )
}
defer stmtIns . Close ()
2018-01-05 12:24:12 -08:00
result , err := stmtIns . Exec ( rand . Intn ( 100000000 ), fake . CharactersN ( dataSize ), fake . CharactersN ( dataSize ), fake . CharactersN ( dataSize ), fake . CharactersN ( dataSize ), fake . CharactersN ( dataSize ))
2017-05-25 10:26:03 -07:00
if err != nil {
panic ( err )
}
return result
}
2018-10-14 12:10:08 +02:00
// TableRowCount gets row count of table
2017-09-25 15:40:28 -07:00
func ( h * MySQLHelper ) SelectRandomData ( limit int ) * sql . Rows {
query := fmt . Sprintf ( "SELECT * FROM LONGHAUL ORDER BY RAND() LIMIT %d" , limit )
rows , err := h . DB . Query ( query )
if err != nil {
panic ( err )
}
defer rows . Close ()
return rows
}
2018-10-14 12:10:08 +02:00
// TableRowCount gets row count of table
2017-07-05 12:26:18 -07:00
func ( h * MySQLHelper ) TableRowCount () ( count int ) {
2017-05-25 10:26:03 -07:00
rows , err := h . DB . Query ( "SELECT COUNT(*) as count FROM LONGHAUL" )
if err != nil {
panic ( err )
}
2017-09-25 15:40:28 -07:00
defer rows . Close ()
2017-05-25 10:26:03 -07:00
for rows . Next () {
err := rows . Scan ( & count )
if err != nil {
panic ( err )
}
}
return count
}
2018-10-14 12:10:08 +02:00
// TableExists checks if a table exists
2017-07-05 12:26:18 -07:00
func ( h * MySQLHelper ) TableExists () bool {
2017-05-25 10:26:03 -07:00
_ , err := h . DB . Query ( "SELECT 1 FROM LONGHAUL LIMIT 1 " )
if err != nil {
return false
}
return true
}
2018-10-14 12:10:08 +02:00
// DeleteRandomRow deletes a random row
2017-07-05 12:26:18 -07:00
func ( h * MySQLHelper ) DeleteRandomRow () sql . Result {
2017-05-25 10:26:03 -07:00
var id int
rows , err := h . DB . Query ( "SELECT id FROM LONGHAUL ORDER BY RAND() LIMIT 1" )
if err != nil {
panic ( err )
}
2017-09-25 15:40:28 -07:00
defer rows . Close ()
2017-05-25 10:26:03 -07:00
for rows . Next () {
err := rows . Scan ( & id )
if err != nil {
panic ( err )
}
}
result1 , err := h . DB . Exec ( "DELETE FROM LONGHAUL WHERE id= " + strconv . Itoa ( id ))
if err != nil {
panic ( err )
}
return result1
}