-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_loader.sh
executable file
·111 lines (93 loc) · 2.83 KB
/
create_loader.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
MODULE=json_flatten
usage()
{
cat <<EOF
$0 TARGET ARTIFACT
TARGET [ debug | release ]
ARTIFACT [ standalone | extension_embed | extension ]
EOF
}
[ $# -ne 2 ] && usage
TARGET="$1"
case "$TARGET" in
debug) ;;
release) ;;
*) usage ;;
esac
ARTIFACT="$2"
case "$ARTIFACT" in
standalone) ;;
extension_embed) ;;
extension) ;;
*) usage ;;
esac
WASM_PATH=target/wasm32-wasi/$TARGET/$MODULE.wasm
WIT_PATH=$MODULE.wit
EXT_PATH=target/$MODULE.tar
WASM_B64=$(base64 -w 0 "${WASM_PATH}")
WIT_B64=$(base64 -w 0 "${WIT_PATH}")
OUTFILE_STANDALONE=load_standalone.sql
OUTFILE_EXTENSION_EMBED=$MODULE.sql
OUTFILE_EXTENSION=load_extension.sql
emit_extension_stmts()
{
EXT_B64=$(base64 -w 0 "${EXT_PATH}")
cat <<EOF >> $OUTFILE_EXTENSION
DROP EXTENSION IF EXISTS $MODULE;
CREATE EXTENSION $MODULE FROM BASE64 '$EXT_B64';
EOF
}
emit_function_stmts()
{
case "$ARTIFACT" in
standalone)
MAYBE_REPLACE="OR REPLACE"
CONTENT_SRC="BASE64 '$WASM_B64'"
OUTFILE="$OUTFILE_STANDALONE"
;;
extension_embed)
MAYBE_REPLACE=""
CONTENT_SRC="LOCAL INFILE '`basename $WASM_PATH`'"
OUTFILE="$OUTFILE_EXTENSION_EMBED"
;;
*)
usage
;;
esac
cat <<EOF > "$OUTFILE"
CREATE $MAYBE_REPLACE FUNCTION json_flatten_json(
json JSON COLLATE utf8mb4_bin NOT NULL,
expr ARRAY(TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL))
RETURNS TABLE(
name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
value LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL)
AS WASM FROM $CONTENT_SRC;
CREATE $MAYBE_REPLACE FUNCTION json_flatten_bigint(
json JSON COLLATE utf8mb4_bin NOT NULL,
expr ARRAY(TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL))
RETURNS TABLE(
name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
value BIGINT NOT NULL)
AS WASM FROM $CONTENT_SRC;
CREATE $MAYBE_REPLACE FUNCTION json_flatten_double(
json JSON COLLATE utf8mb4_bin NOT NULL,
expr ARRAY(TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL))
RETURNS TABLE(
name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
value DOUBLE NOT NULL)
AS WASM FROM $CONTENT_SRC;
CREATE $MAYBE_REPLACE FUNCTION json_flatten_string(
json JSON COLLATE utf8mb4_bin NOT NULL,
expr ARRAY(TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL))
RETURNS TABLE(
name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
value LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL)
AS WASM FROM $CONTENT_SRC;
EOF
}
if [ "$ARTIFACT" = "extension" ] ; then
emit_extension_stmts
else
emit_function_stmts
fi
echo "Loader '$ARTIFACT' created successfully ($TARGET)."