#!/bin/bash
# Created by Roel Van de Paar, Percona LLC

# This script interleaves an SQL statement into the main.sql file on every 2nd line.

# Interleaving is handy for checking various status counters etc. For example, if you saw a sudden spike in "Queries" in SHOW GLOBAL STATUS output, you could
# interleave every 2nd line in the sql trace with "SHOW GLOBAL STATUS LIKE 'Queries';" and implement an assert (see ../adding_asserts.txt) in the code which 
# would halt the server on seeing a very large value in Queries. If the tests would run for quite a short duration, this would not happen (since "Queries" would
# still be small), unless there was a bug which made "Queries" spike.

# Note that the output file generated by this script can be quite large. For a more advanced version, use sql-interleave-switch.sh instead!
# Note also that each independent SQL statement can take up only a single line (as pquery randomly takes lines to execute). As an example, a CREATE TABLE ... ;
# statement (which is customary written accross several lines) should not take up more then one line. Also, each line needs to be terminated by ';'

#INPUT_FILE=./main-merged.sql  # Previous main SQL file
INPUT_FILE=./main-ms-ps-md.sql
OUTPUT_FILE=./main-interleaved.sql
SQL_TO_INTERLEAVE="create table t1 (a int not null, b int not null auto_increment, primary key(a,b)) engine=tokudb;"  # May contain \n seperators to seperate fully-independent SQL statements. The resulting output file will have each '\n' seperated line on a seperate line.
#SQL_TO_INTERLEAVE="SHOW GLOBAL STATUS LIKE 'Queries';\nSHOW GLOBAL STATUS LIKE 'Questions';"  # May contain \n seperators to seperate fully-independent SQL statements. The resulting output file will have each '\n' seperated line on a seperate line.

if [ ! -r ${INPUT_FILE} ]; then
  echo "Assert: ${INPUT_FILE} was not found. Please check. Terminating."
  exit 1
fi

# Syntax:
# * Use ";" as a EOL/terminating character. You also need to use '\n' if you want to list 2 statements per option. Do not put '\n' at the end of a string/line.
# * Note that if you put multiple statements on one line with '\n', then it does not mean these statements are executed sequentially. They are rather listed
#   in the file sequentially, and as pquery uses a single line randomly from this file, either statement may be executed independently at any time.
#   There is currently no workaround/way to execute multiple statements in sequence, but feel free to code it/add it.
# 2 Examples:
# SQL_TO_INTERLEAVE="DROP TABLE t1;\nCREATE TABLE t1 (c1 INT PRIMARY KEY);"
# SQL_TO_INTERLEAVE="SHOW GLOBAL STATUS LIKE 'Queries';"

cat ${INPUT_FILE} | sed "s|$|\n${SQL_TO_INTERLEAVE}|" > ${OUTPUT_FILE}
