mirror of
https://github.com/dimitri/pgloader.git
synced 2026-05-04 10:31:02 +02:00
Improve SQL blocks support, fix #265.
It's now possible to use several files in a BEFORE LOAD EXECUTE section, and to mix DO and EXECUTE parts, bringing lots of flexibility in the commands. Also it actually simplifies the parser.
This commit is contained in:
parent
db0f21b5a5
commit
d2a1a5643e
32
pgloader.1
32
pgloader.1
@ -505,8 +505,8 @@ LOAD <source\-type>
|
||||
|
||||
[ SET <postgresql\-settings> ]
|
||||
|
||||
[ BEFORE LOAD DO|EXECUTE [ <sql statements> | <sql file> ]
|
||||
[ AFTER LOAD DO|EXECUTE [ <sql statements> | <sql file> ]
|
||||
[ BEFORE LOAD [ DO <sql statements> | EXECUTE <sql file> ] \.\.\. ]
|
||||
[ AFTER LOAD [ DO <sql statements> | EXECUTE <sql file> ] \.\.\. ]
|
||||
;
|
||||
.
|
||||
.fi
|
||||
@ -1521,29 +1521,11 @@ LOAD ARCHIVE
|
||||
FROM /Users/dim/Downloads/GeoLiteCity\-latest\.zip
|
||||
INTO postgresql:///ip4r
|
||||
|
||||
BEFORE LOAD DO
|
||||
$$ create extension if not exists ip4r; $$,
|
||||
$$ create schema if not exists geolite; $$,
|
||||
$$ create table if not exists geolite\.location
|
||||
(
|
||||
locid integer primary key,
|
||||
country text,
|
||||
region text,
|
||||
city text,
|
||||
postalcode text,
|
||||
location point,
|
||||
metrocode text,
|
||||
areacode text
|
||||
);
|
||||
$$,
|
||||
$$ create table if not exists geolite\.blocks
|
||||
(
|
||||
iprange ip4r,
|
||||
locid integer
|
||||
);
|
||||
$$,
|
||||
$$ drop index if exists geolite\.blocks_ip4r_idx; $$,
|
||||
$$ truncate table geolite\.blocks, geolite\.location cascade; $$
|
||||
BEFORE LOAD
|
||||
DO $$ create extension if not exists ip4r; $$,
|
||||
$$ create schema if not exists geolite; $$,
|
||||
|
||||
EXECUTE \'geolite\.sql\'
|
||||
|
||||
LOAD CSV
|
||||
FROM FILENAME MATCHING ~/GeoLiteCity\-Location\.csv/
|
||||
|
||||
@ -437,8 +437,8 @@ options.
|
||||
|
||||
[ SET <postgresql-settings> ]
|
||||
|
||||
[ BEFORE LOAD DO|EXECUTE [ <sql statements> | <sql file> ]
|
||||
[ AFTER LOAD DO|EXECUTE [ <sql statements> | <sql file> ]
|
||||
[ BEFORE LOAD [ DO <sql statements> | EXECUTE <sql file> ] ... ]
|
||||
[ AFTER LOAD [ DO <sql statements> | EXECUTE <sql file> ] ... ]
|
||||
;
|
||||
|
||||
The main clauses are the `LOAD`, `FROM`, `INTO` and `WITH` clauses that each
|
||||
@ -1294,29 +1294,11 @@ Here's an example:
|
||||
FROM /Users/dim/Downloads/GeoLiteCity-latest.zip
|
||||
INTO postgresql:///ip4r
|
||||
|
||||
BEFORE LOAD DO
|
||||
$$ create extension if not exists ip4r; $$,
|
||||
$$ create schema if not exists geolite; $$,
|
||||
$$ create table if not exists geolite.location
|
||||
(
|
||||
locid integer primary key,
|
||||
country text,
|
||||
region text,
|
||||
city text,
|
||||
postalcode text,
|
||||
location point,
|
||||
metrocode text,
|
||||
areacode text
|
||||
);
|
||||
$$,
|
||||
$$ create table if not exists geolite.blocks
|
||||
(
|
||||
iprange ip4r,
|
||||
locid integer
|
||||
);
|
||||
$$,
|
||||
$$ drop index if exists geolite.blocks_ip4r_idx; $$,
|
||||
$$ truncate table geolite.blocks, geolite.location cascade; $$
|
||||
BEFORE LOAD
|
||||
DO $$ create extension if not exists ip4r; $$,
|
||||
$$ create schema if not exists geolite; $$,
|
||||
|
||||
EXECUTE 'geolite.sql'
|
||||
|
||||
LOAD CSV
|
||||
FROM FILENAME MATCHING ~/GeoLiteCity-Location.csv/
|
||||
|
||||
@ -26,12 +26,6 @@
|
||||
(destructuring-bind (dq1 dqs) source
|
||||
(list* dq1 dqs))))
|
||||
|
||||
(defrule before-load-do (and kw-before kw-load kw-do dollar-quoted-list)
|
||||
(:lambda (bld)
|
||||
(destructuring-bind (before load do quoted) bld
|
||||
(declare (ignore before load do))
|
||||
quoted)))
|
||||
|
||||
(defrule sql-file (or maybe-quoted-filename)
|
||||
(:lambda (filename)
|
||||
(destructuring-bind (kind path) filename
|
||||
@ -39,37 +33,30 @@
|
||||
(:filename
|
||||
(pgloader.sql:read-queries (uiop:merge-pathnames* path *cwd*)))))))
|
||||
|
||||
(defrule before-load-execute (and kw-before kw-load kw-execute sql-file)
|
||||
(defrule load-do (and kw-do dollar-quoted-list)
|
||||
(:lambda (bld)
|
||||
(destructuring-bind (do quoted) bld
|
||||
(declare (ignore do))
|
||||
quoted)))
|
||||
|
||||
(defrule load-execute (and kw-execute sql-file)
|
||||
(:lambda (ble)
|
||||
(bind (((_ _ _ sql) ble)) sql)))
|
||||
(bind (((_ sql) ble)) sql)))
|
||||
|
||||
(defrule before-load (or before-load-do before-load-execute)
|
||||
(defrule before-load (and kw-before kw-load (+ (or load-do load-execute)))
|
||||
(:lambda (before)
|
||||
(cons :before before)))
|
||||
(bind (((_ _ sql-list-of-list) before))
|
||||
(cons :before (apply #'append sql-list-of-list)))))
|
||||
|
||||
(defrule finally-do (and kw-finally kw-do dollar-quoted-list)
|
||||
(:lambda (fd)
|
||||
(bind (((_ _ quoted) fd)) quoted)))
|
||||
|
||||
(defrule finally-execute (and kw-finally kw-execute sql)
|
||||
(:lambda (fe)
|
||||
(bind (((_ _ sql) fe)) sql)))
|
||||
|
||||
(defrule finally (or finally-do finally-execute)
|
||||
(defrule finally (and kw-finally (+ (or load-do load-execute)))
|
||||
(:lambda (finally)
|
||||
(cons :finally finally)))
|
||||
(bind (((_ sql-list-of-list) finally))
|
||||
(cons :finally (apply #'append sql-list-of-list)))))
|
||||
|
||||
(defrule after-load-do (and kw-after kw-load kw-do dollar-quoted-list)
|
||||
(:lambda (fd)
|
||||
(bind (((_ _ _ quoted) fd)) quoted)))
|
||||
|
||||
(defrule after-load-execute (and kw-after kw-load kw-execute sql-file)
|
||||
(:lambda (fd)
|
||||
(bind (((_ _ _ sql) fd)) sql)))
|
||||
|
||||
(defrule after-load (or after-load-do after-load-execute)
|
||||
(defrule after-load (and kw-after kw-load (+ (or load-do load-execute)))
|
||||
(:lambda (after)
|
||||
(cons :after after)))
|
||||
(bind (((_ _ sql-list-of-list) after))
|
||||
(cons :after (apply #'append sql-list-of-list)))))
|
||||
|
||||
(defun sql-code-block (pgconn state commands label)
|
||||
"Return lisp code to run COMMANDS against DBNAME, updating STATE."
|
||||
|
||||
@ -11,7 +11,12 @@ LOAD ARCHIVE
|
||||
FROM http://pgsql.tapoueh.org/temp/foo.zip
|
||||
INTO postgresql:///ip4r
|
||||
|
||||
BEFORE LOAD EXECUTE 'geolite.sql'
|
||||
BEFORE LOAD
|
||||
DO
|
||||
$$ create extension if not exists ip4r; $$,
|
||||
$$ create schema if not exists geolite; $$
|
||||
|
||||
EXECUTE 'geolite.sql'
|
||||
|
||||
LOAD CSV
|
||||
FROM FILENAME MATCHING ~/GeoLiteCity-Location.csv/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user