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")"
|
2015-04-29 12:03:35 -04:00
|
|
|
list_query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/ListQueryMapper.java")"
|
2014-04-22 14:40:18 -04:00
|
|
|
|
|
|
|
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)"
|
|
|
|
|
2017-06-12 22:57:43 -04:00
|
|
|
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_START\n' | tee -a "$query" "$caching_query" "$null_query" "$list_query" >/dev/null
|
|
|
|
|
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-12 22:57:43 -04:00
|
|
|
if [ "$method_name" != 'toResultSetIterable(' -a "$method_name" != 'toStream(' ]; 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 {
|
|
|
|
ps = conn.prepareStatement(sql);
|
|
|
|
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-12 22:57:43 -04:00
|
|
|
fi # end special case toResultSetIterable/toStream
|
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 {/')
|
|
|
|
return super.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/getPreparedStatement(sql)/' -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) {
|
|
|
|
if (verbose) e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
EOF
|
|
|
|
done >> "$null_query"
|
2015-04-29 12:03:35 -04:00
|
|
|
|
|
|
|
# ListQueryMapper.java
|
|
|
|
cat >> "$list_query" <<EOF
|
|
|
|
@Override
|
|
|
|
$(echo $method | sed -e 's/ResultSet rs/PreparedStatement ps/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
|
|
|
|
return delegate.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/ps/' -e 's/) {/, bindObjects);/')
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
|
|
|
|
return delegate.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/prepareSql(sql, bindObjects)/' -e 's/) {/, bindObjects);/')
|
|
|
|
}
|
|
|
|
|
|
|
|
EOF
|
2017-06-12 22:57:43 -04:00
|
|
|
|
|
|
|
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_END\n' | tee -a "$query" "$caching_query" "$null_query" "$list_query" >/dev/null
|
|
|
|
|
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"
|
2015-04-29 12:03:35 -04:00
|
|
|
finishFile "src/main/java/com/moparisthebest/jdbc/ListQueryMapper.java"
|