Remove comma after means in many docstrings
[rgr-org-mode.git] / contrib / lisp / orgtbl-sqlinsert.el
blob648e44caac6c49895df6a5af414bb7e2535ee7f6
1 ;;; orgtbl-sqlinsert.el --- orgtbl to SQL insert statements.
3 ;; Copyright (C) 2008 Free Software Foundation
5 ;; Author: Jason Riedy <jason@acm.org>
6 ;; Keywords: org, tables, sql
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; Converts an orgtbl to a sequence of SQL insertion commands.
24 ;; Table cells are quoted and escaped very conservatively.
26 ;;; Code:
28 (defun orgtbl-to-sqlinsert (table params)
29 "Convert the orgtbl-mode TABLE to SQL insert statements.
30 TABLE is a list, each entry either the symbol `hline' for a horizontal
31 separator line, or a list of fields for that line.
32 PARAMS is a property list of parameters that can influence the conversion.
34 Names and strings are modified slightly by default. Single-ticks
35 are doubled as per SQL's standard mechanism. Backslashes and
36 dollar signs are deleted. And tildes are changed to spaces.
37 These modifications were chosed for use with TeX. See
38 ORGTBL-SQL-STRIP-AND-QUOTE.
40 Supports all parameters from ORGTBL-TO-GENERIC. New to this function
41 are:
43 :sqlname The name of the database table; defaults to the name of the
44 target region.
46 :nowebname If not nil, used as a wrapping noweb fragment name.
48 The most important parameters of ORGTBL-TO-GENERIC for SQL are:
50 :splice When set to t, return only insert statements, don't wrap
51 them in a transaction. Default is nil.
53 :tstart, :tend
54 The strings used to begin and commit the transaction.
56 :hfmt A function that gathers the quoted header names into a
57 dynamically scoped variable HDRLIST. Probably should
58 not be changed by the user.
60 The general parameters :skip and :skipcols have already been applied when
61 this function is called."
62 (let* (hdrlist
63 (alignment (mapconcat (lambda (x) (if x "r" "l"))
64 org-table-last-alignment ""))
65 (nowebname (plist-get params :nowebname))
66 (breakvals (plist-get params :breakvals))
67 (firstheader t)
68 (*orgtbl-default-fmt* 'orgtbl-sql-strip-and-quote)
69 (params2
70 (list
71 :sqlname name
72 :tstart (lambda () (concat (if nowebname
73 (format "<<%s>>= \n" nowebname)
74 "")
75 "BEGIN TRANSACTION;"))
76 :tend (lambda () (concat "COMMIT;" (if nowebname "\n@ " "")))
77 :hfmt (lambda (f) (progn (if firstheader (push f hdrlist)) ""))
78 :hlfmt (lambda (lst) (setq firstheader nil))
79 :lstart (lambda () (concat "INSERT INTO "
80 sqlname "( "
81 (mapconcat 'identity (reverse hdrlist)
82 ", ")
83 " )" (if breakvals "\n" " ")
84 "VALUES ( "))
85 :lend " );"
86 :sep " , "
87 :hline nil
88 :remove-nil-lines t))
89 (params (org-combine-plists params2 params))
90 (sqlname (plist-get params :sqlname)))
91 (orgtbl-to-generic table params)))
93 (defun orgtbl-sql-quote (str)
94 "Convert single ticks to doubled single ticks and wrap in single ticks."
95 (concat "'" (mapconcat 'identity (split-string str "'") "''") "'"))
97 (defun orgtbl-sql-strip-dollars-escapes-tildes (str)
98 "Strip dollarsigns and backslash escapes, replace tildes with spaces."
99 (mapconcat 'identity
100 (split-string (mapconcat 'identity
101 (split-string str "\\$\\|\\\\")
103 "~")
104 " "))
106 (defun orgtbl-sql-strip-and-quote (str)
107 "Apply ORGBTL-SQL-QUOTE and ORGTBL-SQL-STRIP-DOLLARS-ESCAPES-TILDES
108 to sanitize STR for use in SQL statements."
109 (cond ((stringp str)
110 (orgtbl-sql-quote (orgtbl-sql-strip-dollars-escapes-tildes str)))
111 ((sequencep str) (mapcar 'orgtbl-sql-strip-and-quote str))
112 (t nil)))
114 (provide 'orgtbl-sqlinsert)
115 ;;; orgtbl-sqlinsert.el ends here