Files
my-rook-config/tests/framework/utils/mysql_helper.go
T

153 lines
3.4 KiB
Go
Raw Normal View History

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"
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
)
2017-07-05 12:26:18 -07:00
//MySQLHelper contains pointer to MySqlDB and wrappers for basic object on mySql database
type MySQLHelper struct {
2017-05-25 10:26:03 -07:00
DB *sql.DB
}
2017-07-05 12:26:18 -07:00
// CreateNewMySQLHelper creates a s3 client for specfied endpoint and creds
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
//CloseConnection function closes mysql connection
func (h *MySQLHelper) CloseConnection() {
2017-06-08 12:19:48 -07:00
h.DB.Close()
}
2017-07-05 12:26:18 -07:00
//PingSuccess function is used check connection to a database
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
}
2017-07-05 12:26:18 -07:00
//CreateTable func create sample Table
func (h *MySQLHelper) CreateTable() sql.Result {
2017-05-25 10:26:03 -07:00
result, err := h.DB.Exec("CREATE TABLE LONGHAUL (id int NOT NULL AUTO_INCREMENT,number int,data varchar(32)" +
", PRIMARY KEY (id) )")
if err != nil {
panic(err)
}
return result
}
2017-07-05 12:26:18 -07:00
//InsertRandomData Inserts random Data into the table
func (h *MySQLHelper) InsertRandomData() sql.Result {
2017-05-25 10:26:03 -07:00
stmtIns, err := h.DB.Prepare("INSERT INTO LONGHAUL (number, data) VALUES ( ?, ? )") // ? = placeholder
if err != nil {
panic(err)
}
defer stmtIns.Close()
result, err := stmtIns.Exec(rand.Intn(100000000), fake.CharactersN(20))
if err != nil {
panic(err)
}
return result
}
2017-09-25 15:40:28 -07:00
//TableRowCount gets row count of table
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
}
2017-07-05 12:26:18 -07:00
//TableRowCount gets row count of table
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
}
2017-07-05 12:26:18 -07:00
//TableExists checks if a table exists
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
}
2017-07-05 12:26:18 -07:00
//DeleteRandomRow deletes a random row
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
}