2014-04-22 14:40:18 -04:00
|
|
|
#!/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
|
2014-04-22 14:40:18 -04:00
|
|
|
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"
|
|
|
|
|
2014-04-22 14:40:18 -04:00
|
|
|
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"
|
2014-04-22 14:40:18 -04:00
|
|
|
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)"
|
|
|
|
|
2019-01-15 23:30:00 -05:00
|
|
|
[ "$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
|
|
|
|
2014-04-22 14:40:18 -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/) {/);/')
|
|
|
|
}
|
|
|
|
|
2017-06-08 23:02:12 -04:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# handle this specially in QueryMapper because we need it to hold open PreparedStatement until the ResultSetIterable is closed
|
2017-06-29 00:00:46 -04:00
|
|
|
if [ "$method_name" != 'toResultSetIterable(' -a "$method_name" != 'toStream(' -a "$method_name" != 'toType(' ]; then
|
2017-06-08 23:02:12 -04:00
|
|
|
|
|
|
|
cat >> "$query" <<EOF
|
2014-04-22 14:40:18 -04:00
|
|
|
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
|
|
|
|
PreparedStatement ps = null;
|
|
|
|
try {
|
2019-01-15 23:30:00 -05:00
|
|
|
ps = getPreparedStatement(sql, bindObjects);
|
2014-04-22 14:40:18 -04:00
|
|
|
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
|
|
|
|
|
2017-06-29 00:00:46 -04:00
|
|
|
fi # end special case toResultSetIterable/toStream/toType
|
2017-06-08 23:02:12 -04:00
|
|
|
|
2014-04-22 14:40:18 -04:00
|
|
|
# CachingQueryMapper.java
|
|
|
|
cat >> "$caching_query" <<EOF
|
|
|
|
@Override
|
|
|
|
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
|
2019-01-15 23:30:00 -05:00
|
|
|
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);/')
|
2014-04-22 14:40:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-04-22 14:40:18 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
EOF
|
2019-01-15 23:30:00 -05:00
|
|
|
done >> "$null_query"
|
2015-04-29 12:03:35 -04:00
|
|
|
|
2019-01-15 23:30:00 -05:00
|
|
|
[ "$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
|
|
|
|
2014-04-22 14:40:18 -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"
|