Project

General

Profile

« Previous | Next » 

Revision 9014

lib/sh/util.sh: split databases utils out into separate db.sh

View differences:

util.sh
315 315

  
316 316
localize_url () { test _"$1" = _"$(hostname -f)" || echo "$1"; }
317 317

  
318
#### databases
319

  
320
# using prefixed connection vars
321
alias use_local='declare prefix=local_; import_vars'
322
alias use_remote='declare prefix=remote_; import_vars'
323
alias use_local_remote='use_local; use_remote'
324

  
325
quote='"'
326

  
327
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
328

  
329
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
330

  
331
alias mk_schema_esc='declare schema_esc; mk_esc_name schema'
332
alias mk_table_esc='declare table_esc; mk_esc_name table'
333

  
334
fi # load new aliases
335
if self_being_included; then
336

  
337
log_sql () { test "$verbosity" -ge 2; }
338

  
339
### MySQL
340

  
341
# auto-adds connection/login opts when specified
342
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; }
343
{
344
	echo_func
345
	local ssh_server="$(localize_url "$ssh_server")"
346
	local server="$(localize_url "$server")"
347
	if test -n "$ssh_server"; then
348
		local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
349
	fi
350
	if test -n "$schema"; then local database="${database-$schema}"; fi
351
	
352
	local var=ssh_dest; local_inv
353
	extern ${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
354
${server:+ --host="$server" }${user:+--user="$user" } --password\
355
${password+="$password"} ${database:+--databases "$database" --tables } "$@"
356
}
357

  
358
mysql () { echo_func; mysql_cmd --verbose "$@"; }
359

  
360
mysql_ANSI ()
361
{
362
	echo_func
363
	(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
364
}
365

  
366
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...]
367
{
368
	echo_func
369
	mysql_cmd --quick --lock-tables=false --set-charset \
370
${postgres_compat:+--compatible=postgresql --add-locks=false }\
371
${schema:+--no-data }${data:+--no-create-info }"$@"
372
}
373

  
374
mysqldump_diffable ()
375
{
376
	echo_func
377
	mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/'
378
}
379

  
380
### PostgreSQL
381

  
382
pg_copy_to ()
383
{
384
	echo_func
385
	if test -z "$source"; then
386
		: "${table:?}"; mk_table_esc
387
		if test -z "$limit"; then local source="$table_esc"
388
		else local source="(SELECT * FROM $table_esc LIMIT $limit)"
389
		fi
390
	fi
391
	local pg_copy_format="${pg_copy_format-CSV HEADER}"
392
	
393
	psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
394
}
395

  
396
pg_header ()
397
{
398
	echo_func
399
	local pg_copy_format="CSV HEADER" limit=0
400
	pg_copy_to "$@"|echo_stdin
401
}
402

  
403
pg_export_table_no_header ()
404
{
405
	echo_func
406
	local pg_copy_format="CSV"
407
	pg_copy_to "$@"
408
}
409

  
410
pg_export_table_to_dir_no_header ()
411
{
412
	echo_func
413
	local table="$1"; shift; mk_table_esc
414
	local cols="$(pg_header)"
415
	pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
416
}
417

  
418 318
fi

Also available in: Unified diff