From 41013e13d10bf4be4790670105615b2900cb1944 Mon Sep 17 00:00:00 2001 From: Thierry Banel Date: Sat, 4 Jan 2014 15:45:39 +0100 Subject: [PATCH] convert org-date to sql-date in orgtbl-sqlinsert.el --- contrib/lisp/orgtbl-sqlinsert.el | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/contrib/lisp/orgtbl-sqlinsert.el b/contrib/lisp/orgtbl-sqlinsert.el index b00c93d..82e9e6a 100644 --- a/contrib/lisp/orgtbl-sqlinsert.el +++ b/contrib/lisp/orgtbl-sqlinsert.el @@ -2,7 +2,8 @@ ;; Copyright (C) 2008-2013 Free Software Foundation -;; Author: Jason Riedy +;; Author: Jason Riedy , +;; modified by Thierry Banel tbanelwebmin at free dot fr ;; Keywords: org, tables, sql ;; This file is not part of GNU Emacs. @@ -25,6 +26,32 @@ ;; Converts an orgtbl to a sequence of SQL insertion commands. ;; Table cells are quoted and escaped very conservatively. +;; Example: +;; Using this table as source, +;; adding somewhere in the same file a block like this: +;; -- BEGIN RECEIVE ORGTBL faraway +;; -- END RECEIVE ORGTBL faraway +;; and typing C-c C-c on the first "pipe" of the table, +;; fills the BEGIN RECEIVE ORGTBL block +;; +;; #+ORGTBL: SEND faraway orgtbl-to-sqlinsert :splice nil :skipcols (5) :datefmt "'%Y-%m-%d %H:%M:%S'" :sqlname "thetable" +;; | date | quty | color | status | useless | +;; |-------------------------+-------+--------+-------------+---------| +;; | [2013-04-17 wed. 11:34] | 123.4 | red | yes ok nogo | a | +;; | [2014-04-18 fri.] | 75.3 | yellow | no ko go | bb | +;; | <2015-04-19 sun. 11:35> | 355.1 | blue | no ok go | ccc | +;; | <2016-04-20 wed.> | 233.7 | white | yes ko go | dddd | +;; +;; +;; -- BEGIN RECEIVE ORGTBL faraway +;; BEGIN TRANSACTION; +;; INSERT INTO thetable( date, quty, color, status ) VALUES ( '2013-04-17 11:34:00' , '123.4' , 'red' , 'yes ok nogo' ); +;; INSERT INTO thetable( date, quty, color, status ) VALUES ( '2014-04-18 00:00:00' , '75.3' , 'yellow' , 'no ko go' ); +;; INSERT INTO thetable( date, quty, color, status ) VALUES ( '2015-04-19 11:35:00' , '355.1' , 'blue' , 'no ok go' ); +;; INSERT INTO thetable( date, quty, color, status ) VALUES ( '2016-04-20 00:00:00' , '233.7' , 'white' , 'yes ko go' ); +;; COMMIT; +;; -- END RECEIVE ORGTBL faraway + ;;; Code: (defun orgtbl-to-sqlinsert (table params) @@ -47,6 +74,9 @@ are: :nowebname If not nil, used as a wrapping noweb fragment name. +:datefmt The format used for converting Ord dates + by defaut it is '%Y-%m-%d %H:%M:%S', suitable for Sqlite + The most important parameters of ORGTBL-TO-GENERIC for SQL are: :splice When set to t, return only insert statements, don't wrap @@ -87,6 +117,7 @@ this function is called." :lend " );" :sep " , " :hline nil + :datefmt "'%Y-%m-%d %H:%M:%S'" :remove-nil-lines t)) (params (org-combine-plists params2 params)) (sqlname (plist-get params :sqlname))) @@ -108,7 +139,9 @@ this function is called." (defun orgtbl-sql-strip-and-quote (str) "Apply ORGBTL-SQL-QUOTE and ORGTBL-SQL-STRIP-DOLLARS-ESCAPES-TILDES to sanitize STR for use in SQL statements." - (cond ((stringp str) + (cond ((string-match org-ts-regexp0 str) + (format-time-string (plist-get params :datefmt) (org-time-string-to-time str))) + ((stringp str) (orgtbl-sql-quote (orgtbl-sql-strip-dollars-escapes-tildes str))) ((sequencep str) (mapcar 'orgtbl-sql-strip-and-quote str)) (t nil))) -- 1.7.9.5