emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How-to use a source block output as code for another source block?
       [not found] <16ebd54f-9f18-4c27-c667-8637ed6ee658@gaetanandre.eu>
@ 2019-12-06 18:41 ` Gaëtan André
  0 siblings, 0 replies; only message in thread
From: Gaëtan André @ 2019-12-06 18:41 UTC (permalink / raw)
  To: emacs-orgmode


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)

#+name: export-data
#+header: :engine postgresql
#+header: :dbhost localhost
#+header: :dbuser mrwho
#+header: :database whatever
#+header: :noweb no-export
#+begin_src sql

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

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


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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-12-06 18:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <16ebd54f-9f18-4c27-c667-8637ed6ee658@gaetanandre.eu>
2019-12-06 18:41 ` How-to use a source block output as code for another source block? Gaëtan André

Code repositories for project(s) associated with this inbox:


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).