emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Orgmode] How I'm using these: Generating SQL insert statements.


From: Jason Riedy
Subject: [Orgmode] How I'm using these: Generating SQL insert statements.
Date: Sun, 02 Mar 2008 22:06:39 -0800
User-agent: Gnus/5.110007 (No Gnus v0.7) Emacs/23.0.60 (gnu/linux)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]