orgtbl-sqlinsert.el 4.1 KB

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