Project

General

Profile

1
#!/bin/bash -e
2
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
3
. "$(dirname "${BASH_SOURCE[0]}")"/binsearch.sh
4
. "$(dirname "${BASH_SOURCE[0]}")"/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_ANSI \
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)"; int2bool "$int"; }
32

    
33

    
34
get_pkey_at_pos() # usage: i=# [quote=...] get_pkey_at_pos
35
{
36
	echo_func; 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
q # stop after first match
41
}'
42
}
43

    
44
is_pkey_at_pos_imported()
45
{ echo_func; local pkey; pkey="$(get_pkey_at_pos)"; is_pkey_imported; }
46

    
47
import_resume_pos() # usage: [min=#] [max=#] import_resume_pos
48
{
49
	echo_func; kw_params min max; local min="${min-0}"
50
	if ! isset max; then local max; max="$(file_size "$top_file")"; fi
51
	binsearch is_pkey_at_pos_imported
52
}
53

    
54

    
55
### sql_preamble()
56

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

    
67
preamble_file="${top_file/%.sql/.0.preamble.sql}"
68

    
69
^.preamble.sql/make()
70
{
71
	echo_func; set_make_vars
72
	local target="$preamble_file"; check_target_exists
73
	to_target sql_preamble <"$top_file"
74
}
75

    
76

    
77
resume_import() # usage: [pos=#] resume_import
78
{
79
	echo_func; kw_params pos
80
	if ! isset pos; then local pos; pos="$(import_resume_pos)"; fi
81
	
82
	^.preamble.sql/make
83
	use_local_remote
84
	# run connection preamble (first few lines of dumpfile) before continuing
85
	# with main file at offset, so that connection setting are reapplied
86
	(cat "$preamble_file"; echo_run tail -c +"$pos" "$top_file")|mysql
87
}
88

    
89
fi
(6-6/7)