diff --git a/pgloader.1 b/pgloader.1 index d9bca88..b14876d 100644 --- a/pgloader.1 +++ b/pgloader.1 @@ -505,8 +505,8 @@ LOAD [ SET ] -[ BEFORE LOAD DO|EXECUTE [ | ] -[ AFTER LOAD DO|EXECUTE [ | ] +[ BEFORE LOAD [ DO | EXECUTE ] \.\.\. ] +[ AFTER LOAD [ DO | EXECUTE ] \.\.\. ] ; . .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/ diff --git a/pgloader.1.md b/pgloader.1.md index 0e9febb..792b424 100644 --- a/pgloader.1.md +++ b/pgloader.1.md @@ -437,8 +437,8 @@ options. [ SET ] - [ BEFORE LOAD DO|EXECUTE [ | ] - [ AFTER LOAD DO|EXECUTE [ | ] + [ BEFORE LOAD [ DO | EXECUTE ] ... ] + [ AFTER LOAD [ DO | EXECUTE ] ... ] ; 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/ diff --git a/src/parsers/command-sql-block.lisp b/src/parsers/command-sql-block.lisp index f58f35f..3c254fd 100644 --- a/src/parsers/command-sql-block.lisp +++ b/src/parsers/command-sql-block.lisp @@ -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." diff --git a/test/archive.load b/test/archive.load index 8f39d0b..90ccc6e 100644 --- a/test/archive.load +++ b/test/archive.load @@ -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/