Skip to content

Instantly share code, notes, and snippets.

@jay16
Last active August 29, 2015 14:06
Show Gist options
  • Save jay16/ae829abb1d7c13faa6e6 to your computer and use it in GitHub Desktop.
Save jay16/ae829abb1d7c13faa6e6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
#
# filename prefix
# fs_|ft_ : function return string or table
# p_ : procedure
# t_ : table
# dt_ : table data
# check_ : check create sql data with selct
# task_ : timing task
#
# point
# 1. drop sql before create sql, so head -n 20 is ok.
# 2. function/table/procedure must drop before create,
# so show WARNGIN when not found drop sql
# 3. only fs_|ft_|p_|t_ prefix file need be checked.
#
# shell
# 1. wc -l : count rows
# 2. grep
# -E : regexp
# -A : show more rows after founded row
# -B : show more row before founded row
# -m : only show the first found result
# \b : word boundary, \bok\b, ok => yes, itsok => no
# 3. basename/dirname
# 4. && : if true then continue
# || : if false then continue
# eg: test 1 -eq 1 && echo '==' || echo '!='
# 5. \ : one line shell break row
#
# main function:
extract ()
{
sqlpath=$1;
sqlfile=$2;
keyword=$3;
num=$(ls ${sqlpath}/*.sql | grep -E "${keyword}" | wc -l)
echo "----- ${keyword} \n----- [${num}] \n\n" >> ${sqlfile}
ls ${sqlpath}/*.sql | grep -E "${keyword}" | \
while read file;
do
filename=$(basename "${file}")
dropsql=$(cat "${file}" | head -n 20 | grep -A 1 -B 1 -m 1 "\bdrop\b" || echo "notfound")
if [ "${dropsql}" = "notfound" ]; then
echo "WARNGING: not found drop sql in first 20 rows - ${filename}"
else
echo "-- ${filename}\n${dropsql}" >> ${sqlfile}
fi;
done
#cat ${sqlpath}/*.sql | grep -A 1 -B 1 -m 1 "${keyword}" >> ${sqlfile}
}
sqlpath=$(pwd)/sql
sqlfile=$(pwd)/drop.sql
# clear sql file
true > ${sqlfile}
# extract table
extract ${sqlpath} ${sqlfile} "\<t_"
# extract function
extract ${sqlpath} ${sqlfile} "\<fs_|\<ft_"
# extract procedure
extract ${sqlpath} ${sqlfile} "\<p_"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment