From: Steven Lembark <lembark@wrkhors.com>
To: pgsql-general@lists.postgresql.org
Cc: lembark@wrkhors.com
Subject: Re: reduce number of multiple values to be inserted
Date: Fri, 1 Jun 2018 08:35:44 -0500
Message-ID: <20180601083544.5be9d57d@wrkhors.com> (raw)
In-Reply-To: <CAA6wQLLRSCkVrxmo23pc1sbyW0rr5SQX8bTfMXA3WpgmH=48rQ@mail.gmail.com>
References: <CAA6wQLLRSCkVrxmo23pc1sbyW0rr5SQX8bTfMXA3WpgmH=48rQ@mail.gmail.com>
On Wed, 30 May 2018 08:10:05 +0800
tango ward <tangoward15@gmail.com> wrote:
> curr_pgsql.execute('''
> INSERT INTO student (created, modified, name,
> address, age,
> level )
> VALUES (current_timezone, current_timezone,
> 'Scott', 'Malayan Village', 21, 2),
> (current_timezone, current_timezone,
> 'Ben', 'Malayan Village', 21, 2),
> (current_timezone, current_timezone,
> 'Scott', 'Malayan Village', 21, 2),
>
> (current_timezone, current_timezone,
> 'Andrew', 'Malayan Village', 25, 2),
> (current_timezone, current_timezone,
> 'Larry', 'Malayan Village', 25, 2),
> (current_timezone, current_timezone,
> 'Adam', 'Malayan Village', 25, 2),
>
> (current_timezone, current_timezone,
> 'Elisse', 'Malayan Village', 27, 2),
> (current_timezone, current_timezone,
> 'Xena', 'Malayan Village', 27, 2),
> (current_timezone, current_timezone,
> 'Karen', 'Malayan Village', 27, 2)
>
> I will repeat the same process for 13 villages so that will be 117 of
> values. I would like to know if there's a way to reduce the script?
> This has to be done strictly via script.
Rather than try to cut+paste SQL it may be easier to put the data
into a flat file and have some code spit the SQL out for you:
#!/usr/bin/env perl
########################################################################
# housekeeping
########################################################################
use v5.22;
use YAML::XS qw( Load );
########################################################################
# package variables
########################################################################
my $data
= do
{
# slurp the data from named files or stdin.
local $/;
Load <ARGV>
};
my $prefix = <<'SQL';
/*
* Input data for vx.y.z of student data schema.
*/
insert into student
(
created,
modified,
name,
address,
age,
level
)
values
(
SQL
my $suffix = <<'SQL';
);
/*
* end of input
*/
SQL
########################################################################
# output the SQL
########################################################################
say $prefix;
while( my ( $place, $place_valz ) = each %$data )
{
while( my ( $nums, $namz ) = each %$place_valz )
{
for my $name ( @$namz )
{
say <<"SQL";
(
current_timezone,
current_timezone,
'$name' ,
'$place' ,
'$nums'
)
}
SQL
}
}
say $suffix;
# this is not a module
0
__END__
=head1 NAME
output_sql - dump insert ... values ... from YAML
=head1 SYNOPSIS
Inputs arrive via stdin, literal, or glob-ed paths:
output_sql << /path/to/foobar.yaml;
output_sql /path/to/foobar.yaml;
output_sql /path/to/*.yaml;
gzip -dc < /path/to/bigfile.yaml | output_sql;
gzip -dc < /path/to/bigfile.yaml | output_sql | psql;
Your data file could look like this if you want a single flat file
for all of it:
---
Malayan Village :
21, 2 :
- Ben
- Scott
25, 2 :
- Anderew
- Larry
- Adam
...
Another Village :
...
Or your could break it into chunks using multiple documents within
the YAML file (notice the extra '---'):
---
Malayan Village :
21, 2 :
- Ben
- Scott
25, 2 :
- Anderew
- Larry
- Adam
...
---
Another Village :
...
At which point $data, above, is an array and you get:
for my $chunk ( @$data )
{
while( my ( $place, $place_valz ) = each %$chunk )
{
...
}
}
with the added flexibility of breaking the input data into
multiple files if needed.
--
Steven Lembark 1505 National Ave
Workhorse Computing Rockford, IL 61103
lembark@wrkhors.com +1 888 359 3508
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: lembark@wrkhors.com
Subject: Re: reduce number of multiple values to be inserted
In-Reply-To: <20180601083544.5be9d57d@wrkhors.com>
References: <CAA6wQLLRSCkVrxmo23pc1sbyW0rr5SQX8bTfMXA3WpgmH=48rQ@mail.gmail.com> <20180601083544.5be9d57d@wrkhors.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox