JdbcMapper/querymapper/genQueryMapper.sh

135 lines
4.9 KiB
Bash
Raw Normal View History

#!/bin/bash
cd "$(dirname "$0")"
function prepareFile(){
path="$1"
tmp_path="$(basename "$path")"
(
sed -n '/CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh/q;p' "$path"
echo -e '\t// DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh\n'
) > "$tmp_path"
echo "$tmp_path"
}
function finishFile(){
path="$1"
tmp_path="$(basename "$path")"
echo -e "}\n" >> "$tmp_path"
mv "$tmp_path" "$path"
}
result="$(prepareFile "src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java")"
# single object types
2017-06-12 22:57:43 -04:00
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | egrep '(toObject|toSingleMap|toResultSetIterable|toStream)\(' | grep ', Calendar cal)' | while read method
do
#echo "method: $method"
method_name=$(echo $method | egrep -o '[^ ]+\(')
echo "ResultSetMapper.$method_name)"
2017-06-12 22:57:43 -04:00
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_START\n' >> "$result"
cat >> "$result" <<EOF
$(echo $method | sed "s/, Calendar cal)/)/")
return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/rs/' -e 's/) {/);/')
}
EOF
2017-06-12 22:57:43 -04:00
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_END\n' >> "$result"
done
#everything else
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | grep '(ResultSet rs' | grep ', int arrayMaxLength, Calendar cal)' | while read method
do
#echo "method: $method"
method_name=$(echo $method | egrep -o '[^ ]+\(')
echo "ResultSetMapper.$method_name)"
for args in '' ', int arrayMaxLength' ', Calendar cal'
do
cat >> "$result" <<EOF
$(echo $method | sed "s/, int arrayMaxLength, Calendar cal)/$args)/")
return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/rs/' -e 's/) {/);/')
}
EOF
done
done
finishFile "src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java"
query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/QueryMapper.java")"
caching_query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java")"
null_query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/NullQueryMapper.java")"
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | grep '(ResultSet rs' | egrep -v '(int arrayMaxLength|Calendar cal)' | while read method
do
#echo "method: $method"
method_name=$(echo $method | egrep -o '[^ ]+\(')
echo "QueryMapper.$method_name)"
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_START\n' | tee -a "$query" "$caching_query" "$null_query" >/dev/null
2017-06-12 22:57:43 -04:00
# QueryMapper.java
cat >> "$query" <<EOF
$(echo $method | sed -e 's/ResultSet rs/PreparedStatement ps/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
return cm.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/bindExecute(ps, bindObjects)/' -e 's/) {/);/')
}
EOF
# handle this specially in QueryMapper because we need it to hold open PreparedStatement until the ResultSetIterable is closed
if [ "$method_name" != 'toResultSetIterable(' -a "$method_name" != 'toStream(' -a "$method_name" != 'toType(' ]; then
cat >> "$query" <<EOF
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
PreparedStatement ps = null;
try {
ps = getPreparedStatement(sql, bindObjects);
return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/ps/' -e 's/) {/, bindObjects);/')
} finally {
tryClose(ps);
}
}
EOF
fi # end special case toResultSetIterable/toStream/toType
# CachingQueryMapper.java
cat >> "$caching_query" <<EOF
@Override
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
return super.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/getPreparedStatement(sql, bindObjects)/' -e 's/) {/, bindObjects);/')
}
EOF
# NullQueryMapper.java
for type in PreparedStatement String
do
cat <<EOF
@Override
$(echo $method | sed -e "s/ResultSet rs/$type query/" -e 's/) {/, final Object... bindObjects) {/')
try {
return delegate.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/query/' -e 's/) {/, bindObjects);/')
} catch (Throwable e) {
2018-10-10 00:10:00 -04:00
handler.handle(e);
}
return null;
}
EOF
done >> "$null_query"
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_END\n' | tee -a "$query" "$caching_query" "$null_query" >/dev/null
2017-06-12 22:57:43 -04:00
done
finishFile "src/main/java/com/moparisthebest/jdbc/QueryMapper.java"
finishFile "src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java"
finishFile "src/main/java/com/moparisthebest/jdbc/NullQueryMapper.java"