orgtbl-sqlinsert.el 4.2 KB

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