Project

General

Profile

1
#!/bin/bash -e
2
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
3
.rel binsearch.sh
4
.rel make.sh
5

    
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
	data_only=1 mysql \
19
<<<"SELECT COUNT(*) FROM $table_esc WHERE $pkey_name = $pkey"|echo_stdout
20
}
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
{ echo_func; local int; int="$(is_pkey_imported__int)"; int2exit "$int"; }
32

    
33

    
34
get_pkey_at_pos() # usage: i=# [quote=...] get_pkey_at_pos
35
{
36
	echo_func; log_local; log++; kw_params i; : "${i?}"; quote='`'; mk_table_esc
37
	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
# stop after first match
41
q
42
}'
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

    
56
### sql_preamble()
57

    
58
sql_preamble() # usage: sql_preamble <file
59
{
60
	echo_func
61
	echo_run sed -n '/^-- (Table structure|Dumping data) for table /{
62
# stop at first match
63
q
64
}
65
# passthru until first match
66
p
67
' "$@"
68
}
69

    
70
preamble_file="${top_file/%.sql/.0.preamble.sql}"
71

    
72
^.preamble.sql/make()
73
{
74
	begin_target
75
	local target="$preamble_file"; check_target_exists
76
	to_target sql_preamble <"$top_file"
77
}
78

    
79

    
80
resume_import() # usage: [pos=#] resume_import
81
{
82
	begin_target; kw_params pos
83
	if ! isset pos; then local pos; pos="$(import_resume_pos)"; fi
84
	
85
	^.preamble.sql/make
86
	use_local_remote
87
	# 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
}
91

    
92
fi
(8-8/11)