1 |
9568
|
aaronmk
|
#!/bin/bash -e
|
2 |
|
|
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
|
3 |
9854
|
aaronmk
|
.rel binsearch.sh
|
4 |
|
|
.rel make.sh
|
5 |
9568
|
aaronmk
|
|
6 |
|
|
if self_not_included; then
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
### is_pkey_imported()
|
10 |
|
|
|
11 |
|
|
is_pkey_imported__int() # usage: pkey_name=... pkey=# is_pkey_imported__int
|
12 |
|
|
{
|
13 |
|
|
echo_func; kw_params pkey_name pkey; : "${pkey_name?}" "${pkey?}"
|
14 |
|
|
test "$pkey" || { log++ echo_run echo 0; return; }
|
15 |
|
|
|
16 |
|
|
mk_table_esc
|
17 |
|
|
use_local_remote
|
18 |
9777
|
aaronmk
|
data_only=1 mysql \
|
19 |
9569
|
aaronmk
|
<<<"SELECT COUNT(*) FROM $table_esc WHERE $pkey_name = $pkey"|echo_stdout
|
20 |
9568
|
aaronmk
|
}
|
21 |
|
|
|
22 |
|
|
func_override is_pkey_imported__int__no_cache
|
23 |
|
|
is_pkey_imported__int() # caches the last result for efficiency
|
24 |
|
|
{
|
25 |
|
|
local cache_key="$(declare -p pkey_name pkey) $*"; load_cache
|
26 |
|
|
if ! cached; then save_cache "$(${FUNCNAME}__no_cache "$@")" || return; fi
|
27 |
|
|
echo_cached_value
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
is_pkey_imported()
|
31 |
9696
|
aaronmk
|
{ echo_func; local int; int="$(is_pkey_imported__int)"; int2exit "$int"; }
|
32 |
9568
|
aaronmk
|
|
33 |
|
|
|
34 |
9579
|
aaronmk
|
get_pkey_at_pos() # usage: i=# [quote=...] get_pkey_at_pos
|
35 |
9568
|
aaronmk
|
{
|
36 |
12828
|
aaronmk
|
echo_func; log_local; log++; kw_params i; : "${i?}"; quote='`'; mk_table_esc
|
37 |
9568
|
aaronmk
|
piped_cmd echo_run tail -c +"$i" "$top_file"\
|
38 |
|
|
|echo_run sed -n '/^INSERT INTO '"$table_esc"' VALUES \(([0-9]+),.*$/{
|
39 |
|
|
s//\1/p
|
40 |
9587
|
aaronmk
|
# stop after first match
|
41 |
|
|
q
|
42 |
9568
|
aaronmk
|
}'
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
is_pkey_at_pos_imported()
|
46 |
|
|
{ echo_func; local pkey; pkey="$(get_pkey_at_pos)"; is_pkey_imported; }
|
47 |
|
|
|
48 |
|
|
import_resume_pos() # usage: [min=#] [max=#] import_resume_pos
|
49 |
|
|
{
|
50 |
|
|
echo_func; kw_params min max; local min="${min-0}"
|
51 |
|
|
if ! isset max; then local max; max="$(file_size "$top_file")"; fi
|
52 |
|
|
binsearch is_pkey_at_pos_imported
|
53 |
|
|
}
|
54 |
|
|
|
55 |
9570
|
aaronmk
|
|
56 |
|
|
### sql_preamble()
|
57 |
|
|
|
58 |
|
|
sql_preamble() # usage: sql_preamble <file
|
59 |
|
|
{
|
60 |
|
|
echo_func
|
61 |
9571
|
aaronmk
|
echo_run sed -n '/^-- (Table structure|Dumping data) for table /{
|
62 |
9587
|
aaronmk
|
# stop at first match
|
63 |
|
|
q
|
64 |
9570
|
aaronmk
|
}
|
65 |
9587
|
aaronmk
|
# passthru until first match
|
66 |
|
|
p
|
67 |
9570
|
aaronmk
|
' "$@"
|
68 |
|
|
}
|
69 |
|
|
|
70 |
9582
|
aaronmk
|
preamble_file="${top_file/%.sql/.0.preamble.sql}"
|
71 |
9570
|
aaronmk
|
|
72 |
|
|
^.preamble.sql/make()
|
73 |
|
|
{
|
74 |
12779
|
aaronmk
|
begin_target
|
75 |
9570
|
aaronmk
|
local target="$preamble_file"; check_target_exists
|
76 |
|
|
to_target sql_preamble <"$top_file"
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
|
80 |
9568
|
aaronmk
|
resume_import() # usage: [pos=#] resume_import
|
81 |
|
|
{
|
82 |
12968
|
aaronmk
|
begin_target; kw_params pos
|
83 |
9568
|
aaronmk
|
if ! isset pos; then local pos; pos="$(import_resume_pos)"; fi
|
84 |
|
|
|
85 |
9570
|
aaronmk
|
^.preamble.sql/make
|
86 |
9568
|
aaronmk
|
use_local_remote
|
87 |
9570
|
aaronmk
|
# run connection preamble (first few lines of dumpfile) before continuing
|
88 |
|
|
# with main file at offset, so that connection setting are reapplied
|
89 |
|
|
(cat "$preamble_file"; echo_run tail -c +"$pos" "$top_file")|mysql
|
90 |
9568
|
aaronmk
|
}
|
91 |
|
|
|
92 |
|
|
fi
|