App-MysqlUtils
view release on metacpan or search on metacpan
script/mysql-fill-csv-columns-from-query view on Meta::CPAN
#!perl
# Note: This script is a CLI for Riap function /App/MysqlUtils/mysql_fill_csv_columns_from_query
# and generated automatically using Perinci::CmdLine::Gen version 0.502
use 5.010001;
use strict;
use warnings;
use Log::ger;
use Perinci::CmdLine::Any;
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2022-10-19'; # DATE
our $DIST = 'App-MysqlUtils'; # DIST
our $VERSION = '0.022'; # VERSION
my $cmdline = Perinci::CmdLine::Any->new(
url => "/App/MysqlUtils/mysql_fill_csv_columns_from_query",
program_name => "mysql-fill-csv-columns-from-query",
log => 1,
log_level => "info",
pass_cmdline_object => 1,
config_filename => "mysqlutils.conf",
);
$cmdline->run;
# ABSTRACT: Fill CSV columns with data from a query
# PODNAME: mysql-fill-csv-columns-from-query
__END__
=pod
=encoding UTF-8
=head1 NAME
mysql-fill-csv-columns-from-query - Fill CSV columns with data from a query
=head1 VERSION
This document describes version 0.022 of mysql-fill-csv-columns-from-query (from Perl distribution App-MysqlUtils), released on 2022-10-19.
=head1 SYNOPSIS
B<mysql-fill-csv-columns-from-query> L<--help|/"--help, -h, -v"> (or L<-h|/"--help, -h, -v">, L<-?|/"--help, -h, -v">)
B<mysql-fill-csv-columns-from-query> L<--version|/"--version, -v"> (or L<-v|/"--version, -v">)
B<mysql-fill-csv-columns-from-query> [(L<--config-path|/"--config-path=s">=I<path>)+|L<--no-config|/"--no-config, -C">|L<-C|/"--no-config, -C">] [L<--config-profile|/"--config-profile=s, -P">=I<profile>|L<-P|/"--config-profile=s, -P">] [L<--count|/"-...
=head1 DESCRIPTION
This utility is handy if you have a partially filled table (in CSV format, which
you can convert from spreadsheet or Google Sheet or whatever), where you have
some unique key already specified in the table (e.g. customer_id) and you want
to fill up other columns (e.g. customer_name, customer_email, last_order_date) from a
query:
% mysql-fill-csv-columns-from-query DBNAME TABLE.csv 'SELECT c.NAME customer_name, c.email customer_email, (SELECT date FROM tblorders WHERE client_id=$customer_id ORDER BY date DESC LIMIT 1) last_order_time FROM tblclients WHERE id=$customer_id'
The C<$NAME> in the query will be replaced by actual CSV column value. SELECT
fields must correspond to the CSV column names. For each row, a new query will
be executed and the first result row is used.
=head1 OPTIONS
C<*> marks required options.
=head2 Main options
=over
=item B<--count>, B<-c>
Instead of returning the CSV rows, just return the count of rows that get filled.
=item B<--database>=I<s>*
(No description)
Can also be specified as the 1st command-line argument.
=item B<--filename>=I<s>*, B<-f>
Input CSV file.
Can also be specified as the 2nd command-line argument.
=item B<--query>=I<s>*
(No description)
Can also be specified as the 3rd command-line argument.
=back
=head2 Configuration options
=over
=item B<--config-path>=I<s>
Set path to configuration file.
Can actually be specified multiple times to instruct application to read from
multiple configuration files (and merge them).
script/mysql-fill-csv-columns-from-query view on Meta::CPAN
C<password> to C<beaver>. When you specify C<--config-profile=production>,
C<username> will be set to C<bar> and C<password> to C<honey>.
=item B<--no-config>, B<-C>
Do not use any configuration file.
If you specify C<--no-config>, the application will not read any configuration
file.
=back
=head2 Connection options
=over
=item B<--host>=I<s>
Default value:
"localhost"
=item B<--password>=I<s>
Will try to get default from C<~/.my.cnf>.
=item B<--port>=I<s>
Default value:
3306
=item B<--username>=I<s>
Will try to get default from C<~/.my.cnf>.
=back
=head2 Environment options
=over
=item B<--no-env>
Do not read environment for default options.
If you specify C<--no-env>, the application wil not read any environment
variable.
=back
=head2 Input options
=over
=item B<--escape-char>=I<s>
Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS.
Defaults to C<\\> (backslash). Overrides C<--tsv> option.
=item B<--no-header>, B<--input-header>
By default (C<--header>), the first row of the CSV will be assumed to contain
field names (and the second row contains the first data row). When you declare
that CSV does not have header row (C<--no-header>), the first row of the CSV is
assumed to contain the first data row. Fields will be named C<field1>, C<field2>,
and so on.
=item B<--quote-char>=I<s>
Specify field quote character in input CSV, will be passed to Text::CSV_XS.
Defaults to C<"> (double quote). Overrides C<--tsv> option.
=item B<--sep-char>=I<s>
Specify field separator character in input CSV, will be passed to Text::CSV_XS.
Defaults to C<,> (comma). Overrides C<--tsv> option.
=item B<--tsv>, B<--input-tsv>
Inform that input file is in TSV (tab-separated) format instead of CSV.
Overriden by C<--sep-char>, C<--quote-char>, C<--escape-char> options. If one of
those options is specified, then C<--tsv> will be ignored.
=back
=head2 Logging options
=over
=item B<--debug>
Shortcut for --log-level=debug.
=item B<--log-level>=I<s>
Set log level.
By default, these log levels are available (in order of increasing level of
importance, from least important to most): C<trace>, C<debug>, C<info>,
C<warn>/C<warning>, C<error>, C<fatal>. By default, the level is usually set to
C<warn>, which means that log statements with level C<info> and less important
levels will not be shown. To increase verbosity, choose C<info>, C<debug>, or
C<trace>.
For more details on log level and logging, as well as how new logging levels can
be defined or existing ones modified, see L<Log::ger>.
=item B<--quiet>
Shortcut for --log-level=error.
=item B<--trace>
Shortcut for --log-level=trace.
=item B<--verbose>
Shortcut for --log-level=info.
=back
=head2 Output options
=over
=item B<--format>=I<s>
Choose output format, e.g. json, text.
Default value:
undef
Output can be displayed in multiple formats, and a suitable default format is
chosen depending on the application and/or whether output destination is
interactive terminal (i.e. whether output is piped). This option specifically
chooses an output format.
script/mysql-fill-csv-columns-from-query view on Meta::CPAN
complete -C mysql-fill-csv-columns-from-query mysql-fill-csv-columns-from-query
in your bash startup (e.g. F<~/.bashrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.
It is recommended, however, that you install modules using L<cpanm-shcompgen>
which can activate shell completion for scripts immediately.
=head2 tcsh
To activate tcsh completion for this script, put:
complete mysql-fill-csv-columns-from-query 'p/*/`mysql-fill-csv-columns-from-query`/'
in your tcsh startup (e.g. F<~/.tcshrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.
It is also recommended to install L<shcompgen> (see above).
=head2 other shells
For fish and zsh, install L<shcompgen> as described above.
=head1 CONFIGURATION FILE
This script can read configuration files. Configuration files are in the format of L<IOD>, which is basically INI with some extra features.
By default, these names are searched for configuration filenames (can be changed using C<--config-path>): F</home/u1/.config/mysqlutils.conf>, F</home/u1/mysqlutils.conf>, or F</etc/mysqlutils.conf>.
All found files will be read and merged.
To disable searching for configuration files, pass C<--no-config>.
You can put multiple profiles in a single file by using section names like C<[profile=SOMENAME]> or C<[SOMESECTION profile=SOMENAME]>. Those sections will only be read if you specify the matching C<--config-profile SOMENAME>.
You can also put configuration for multiple programs inside a single file, and use filter C<program=NAME> in section names, e.g. C<[program=NAME ...]> or C<[SOMESECTION program=NAME]>. The section will then only be used when the reading program match...
You can also filter a section by environment variable using the filter C<env=CONDITION> in section names. For example if you only want a section to be read if a certain environment variable is true: C<[env=SOMEVAR ...]> or C<[SOMESECTION env=SOMEVAR ...
To load and configure plugins, you can use either the C<-plugins> parameter (e.g. C<< -plugins=DumpArgs >> or C<< -plugins=DumpArgs@before_validate_args >>), or use the C<[plugin=NAME ...]> sections, for example:
[plugin=DumpArgs]
-event=before_validate_args
-prio=99
[plugin=Foo]
-event=after_validate_args
arg1=val1
arg2=val2
which is equivalent to setting C<< -plugins=-DumpArgs@before_validate_args@99,-Foo@after_validate_args,arg1,val1,arg2,val2 >>.
List of available configuration parameters:
count (see --count)
database (see --database)
escape_char (see --escape-char)
filename (see --filename)
format (see --format)
header (see --no-header)
host (see --host)
log_level (see --log-level)
naked_res (see --naked-res)
password (see --password)
port (see --port)
query (see --query)
quote_char (see --quote-char)
sep_char (see --sep-char)
tsv (see --tsv)
username (see --username)
=head1 ENVIRONMENT
=head2 MYSQL_FILL_CSV_COLUMNS_FROM_QUERY_OPT
String. Specify additional command-line options.
=head1 FILES
=head2 /home/u1/.config/mysqlutils.conf
=head2 /home/u1/mysqlutils.conf
=head2 /etc/mysqlutils.conf
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/App-MysqlUtils>.
=head1 SOURCE
Source repository is at L<https://github.com/perlancar/perl-App-MysqlUtils>.
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.
Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
that are considered a bug and can be reported to me.
=head1 COPYRIGHT AND LICENSE
( run in 3.125 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )