forked from megaannum/bash_coproc_oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_coproc_oracle.sh
114 lines (94 loc) · 2.5 KB
/
bash_coproc_oracle.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
111
112
113
#!/bin/bash
#
# bash_coproc_oracle.sh
# Example of using a bash coproc to manage an oracle sqlplus connection.
#
###########################################################################
# Functions
###########################################################################
##############################
# sqlplus functions START
##############################
#
# Note: using a bash coproc for launching, communicating with and
# exiting a single sqlplus process results in upto 15 times better
# performance.
#
# The sqlplus_write_with_done command ends by having sqlplus echo "DONE".
# The sqlplus_read_until_done reads lines and echos those line until
# a line equals "DONE" at which point the function returns.
# One can change from "DONE" to some other string, or make it a variable,
# But make sure that Oracle never generates the string except as part
# of the sqlplus_write_with_done function call.
#
# The function sqlplus_check_schema has to be modified if you want to
# only allow a given set of schema to be allowed.
function sqlplus_log_fatal() {
local -r msg="$1"
echo "FATAL: $msg"
exit 1
}
function sqlplus_write() {
echo "$@" >&"${COPROC[1]}"
}
function sqlplus_write_with_done() {
echo "$@" >&"${COPROC[1]}"
echo "prompt DONE;" >&"${COPROC[1]}"
}
function sqlplus_commit() {
sqlplus_write_with_done "
commit;
"
sqlplus_read_until_done
}
function sqlplus_read_until_done() {
while read line; do
if [[ "$line" == "DONE" ]]; then
break;
fi
echo "$line"
done <&"${COPROC[0]}"
}
function sqlplus_check_schema() {
local -r schema="$1"
# allow any schema
case "$schema" in
sys )
;;
"" )
sqlplus_log_fatal "Empty schema name"
;;
* )
;;
esac
}
function sqlplus_init() {
local -r schema="$1"
sqlplus_check_schema "$schema"
case "$schema" in
sys )
coproc sqlplus -s sys/sys as sysdba
;;
*)
coproc sqlplus -s "$schema"/"$schema"
;;
esac
sqlplus_write "set echo off;"
sqlplus_write "set feedback off;"
sqlplus_write "set linesize 10000;"
sqlplus_write "set pagesize 0;"
sqlplus_write "set sqlprompt '';"
sqlplus_write "set trimspool on;"
sqlplus_write "set space 0;"
sqlplus_write "set truncate on;"
sqlplus_write "set verify off;"
}
function sqlplus_exit() {
# only need to exit sqlplus if it is still running
if [[ -n "${COPROC[@]}" ]]; then
sqlplus_write "exit;"
fi
}
##############################
# sqlplus functions END
##############################