emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Gaëtan André" <rvlander@gaetanandre.eu>
To: emacs-orgmode@gnu.org
Subject: How-to use a source block output as code for another source block?
Date: Fri, 6 Dec 2019 19:41:39 +0100	[thread overview]
Message-ID: <c2ba5c63-4e52-81f6-8a79-2e2bb89d6961@gaetanandre.eu> (raw)
In-Reply-To: <16ebd54f-9f18-4c27-c667-8637ed6ee658@gaetanandre.eu>

Hello,


Yesterday I asked this question on IRC and the community kindly
answered. I was also asked to send an email to the mailing list
explaining my use case. Thus this email.

So, in the scope of my current project at work I need to see if Neo4J would
be a valid alternative to PostgreSQL to store our data. Were hoping to
have better flexibility and better performance on some requests.
In order to document the steps of my exploration I decided to use
org-mode which I discovered a couple of weeks ago (I use it mainly to
keep a diary of my work days).

The first step in my investigation is to migrate the data from Postgres
to Neo4j via CSV files. Since our schema contains quite a bit of tables
and relations I decided to generate corresponding SQL COPY ... TO ...
requests by first putting variable data in a array, second generating
requests with a python source bloc and then feed it to a SQL bloc. Here
is the result:

#+tblname: rels-data
| computer    | id | house      | house_id        | EQUIPS      | 
computer   |
| house       | id | block       | block_id        | IN          | 
house       |
| block        | id | city        | city_id          | IN | block       |
| city         | id | country     | country_id       | IN | city         |
| city         | id | city_group   | city_group_id    | BELONGS_TO | 
city        |

#+name: import-rels
#+begin_src python :var rels=rels-data :results value verbatim
res = ["""COPY (
    SELECT '{0}' || {1} as ":START_ID", '{2}' || {3} as ":END_ID", '{4}' 
as ":TYPE"
    FROM "{5}"
) TO '/data/rels_{0}_{2}.csv' WITH CSV HEADER;""".format(*row) for row 
in rels]
return "\n".join(res)
#+end_src


#+name: export-data
#+header: :engine postgresql
#+header: :dbhost localhost
#+header: :dbuser mrwho
#+header: :database whatever
#+header: :noweb no-export
#+begin_src sql
   <<import-rels()>>
#+end_src

All the magic is in the use of noweb[1] feature.

If you have any questions or remarks don't hesitate.

Regards,

[1] https://orgmode.org/manual/noweb.html

           reply	other threads:[~2019-12-06 18:41 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <16ebd54f-9f18-4c27-c667-8637ed6ee658@gaetanandre.eu>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c2ba5c63-4e52-81f6-8a79-2e2bb89d6961@gaetanandre.eu \
    --to=rvlander@gaetanandre.eu \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).