by berntb · 10y ago · 0 comments
alias sqlcmdtocsv="perl -nE 'chomp; s/\\t/;/g; say \$_;'"
alias sqlcmdtoperl="perl -MData::Dump=dump -nE 'chomp; @r=split(/\\t/); if (@titles == 0) { @titles=@r; next; } \$row={}; for(\$i=0; \$i < @r; \$i++) { \$row->{\$titles[\$i]} = \$r[\$i]; } push @rows, \$row; END { say dump \\@rows };'"
# Quick and dirty for moving from the real DB to the test DB.
# Use: mysql_generates_a_row | sqlcmdtoperl | dumptosqlinsert
alias dumptosqlinsert="perl -E 'my \$txt; { local \$/; \$txt=<>; } my \$rows= eval \$txt; \$q=chr(39); for my \$r (@\$rows) { @cols=map { \$v=\$r->{\$_}; if (\$v eq \"NULL\") {\"\$_=NULL\"} else {\"\$_=\${q}\$v\$q\"} } keys %\$r; say join(\", \", @cols);}'"
I have a bunch of aliases for conversions, to generate common SQL that dependend on parameters and so on.But it would have been better to build off a well tested module as a basis for my set of tools, sigh. I could have used yaml as an intermediary.
A favorite saying is from chemistry -- with a few weeks of hard work in the lab, you can save hours in the library...