emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jason Riedy <jason@acm.org>
To: emacs-orgmode@gnu.org
Subject: How I'm using these: Generating SQL insert statements.
Date: Sun, 02 Mar 2008 22:06:39 -0800	[thread overview]
Message-ID: <878x108how.fsf_-_@sparse.yi.org> (raw)
In-Reply-To: 87d4qc8idn.fsf_-_@sparse.yi.org

This code is only partially baked, but it's working for me at the
moment.  I'm using my multi-target changes to generate both a
LaTeX description of the values as well as SQL insert statements
in separate noweb chunks.  The code leaves a spare blank line
in place of the header and cannot handle more than one header line,
but it's all I need...

Jason

(defun ejr/orgtbl-to-sqlinsert (table params)
  "Convert the orgtbl-mode TABLE to SQL insert statements.
TABLE is a list, each entry either the symbol `hline' for a horizontal
separator line, or a list of fields for that line.
PARAMS is a property list of parameters that can influence the conversion.
Supports all parameters from `orgtbl-to-generic'.  Most important for
SQL are:

:splice    When set to t, return only insert statements, don't wrap
           them in a transaction.  Default is nil.

:sqlname   The name of the database table; defaults to the name of the
           target region.

:tstart, :tend
           The strings used to begin and commit the transaction.

:fmt       A format to be used to wrap the field value, should contain %s
           for the original field value.  The default wraps the
           value in the non-standard [] quote marks but does not
           protect against ] characters in the header name.  The
           format may also be a function that formats its one
           argument.

:hfmt      A function that gathers the []-quoted header names into a
           dynamically scoped variable HDRLIST.

The general parameters :skip and :skipcols have already been applied when
this function is called."
  (let* (hdrlist
	 (alignment (mapconcat (lambda (x) (if x "r" "l"))
			       org-table-last-alignment ""))
	 (params2
	  (list
	   :sqlname name
	   :tstart "BEGIN TRANSACTION;"
	   :tend "COMMIT;"
	   :fmt "[%s]"
	   :hfmt (lambda (f) (push (concat "[" f "]") hdrlist) "")
	   :hlfmt (lambda (lst) nil)
	   :lstart (lambda () (concat "INSERT INTO "
				      (plist-get params :sqlname) "( "
				      (mapconcat 'identity (reverse hdrlist)
						 ", ")
				      " ) VALUES ( "))
	   :lend " );"
	   :sep " , "
	   :hline nil)))
    (orgtbl-to-generic table (org-combine-plists params2 params))))

      reply	other threads:[~2008-03-03  6:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-03  0:37 [PATCH] Support last-line specializers Jason Riedy
2008-03-03  5:36 ` [respun PATCH 2/3] " jason
2008-03-03  5:51   ` [PATCH 3/3] Allow functions for some orgtbl parameters Jason Riedy
2008-03-03  6:06     ` Jason Riedy [this message]

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=878x108how.fsf_-_@sparse.yi.org \
    --to=jason@acm.org \
    --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).