cli: print -O sql
This commit is contained in:
parent
d2e0312ab1
commit
c8a84e3c96
@ -17,6 +17,7 @@ where
|
||||
|
||||
import Data.Maybe (isJust)
|
||||
import Data.Text (Text)
|
||||
import Data.List (intercalate)
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Lazy as TL
|
||||
import System.Console.CmdArgs.Explicit
|
||||
@ -37,7 +38,7 @@ printmode = hledgerCommandMode
|
||||
"show all amounts explicitly"
|
||||
,flagNone ["new"] (setboolopt "new")
|
||||
"show only newer-dated transactions added in each file since last run"
|
||||
,outputFormatFlag ["txt","csv","json"]
|
||||
,outputFormatFlag ["txt","csv","json","sql"]
|
||||
,outputFileFlag
|
||||
])
|
||||
[generalflagsgroup1]
|
||||
@ -60,6 +61,7 @@ printEntries opts@CliOpts{reportopts_=ropts} j = do
|
||||
"txt" -> entriesReportAsText opts
|
||||
"csv" -> (++"\n") . printCSV . entriesReportAsCsv
|
||||
"json" -> (++"\n") . TL.unpack . toJsonText
|
||||
"sql" -> entriesReportAsSql
|
||||
_ -> const $ error' $ unsupportedOutputFormatError fmt
|
||||
writeOutput opts $ render $ entriesReport ropts q j
|
||||
|
||||
@ -125,6 +127,20 @@ originalPostingPreservingAccount p = (originalPosting p) { paccount = paccount p
|
||||
-- ]
|
||||
-- ]
|
||||
|
||||
entriesReportAsSql :: EntriesReport -> String
|
||||
entriesReportAsSql txns =
|
||||
"create table if not exists postings(id serial,txnidx int,date1 date,date2 date,status text,code text,description text,comment text,account text,amount numeric,commodity text,credit numeric,debit numeric,posting_status text,posting_comment text);\n"++
|
||||
"insert into postings(txnidx,date1,date2,status,code,description,comment,account,amount,commodity,credit,debit,posting_status,posting_comment) values\n"++
|
||||
(intercalate "," (map values csv))
|
||||
++";\n"
|
||||
where
|
||||
values vs = "(" ++ (intercalate "," $ map toSql vs) ++ ")\n"
|
||||
toSql "" = "NULL"
|
||||
toSql s = "'" ++ (concatMap quoteChar s) ++ "'"
|
||||
quoteChar '\'' = "''"
|
||||
quoteChar c = [c]
|
||||
csv = concatMap transactionToCSV txns
|
||||
|
||||
entriesReportAsCsv :: EntriesReport -> CSV
|
||||
entriesReportAsCsv txns =
|
||||
["txnidx","date","date2","status","code","description","comment","account","amount","commodity","credit","debit","posting-status","posting-comment"] :
|
||||
|
||||
@ -75,7 +75,7 @@ This command also supports the
|
||||
[output destination](hledger.html#output-destination) and
|
||||
[output format](hledger.html#output-format) options
|
||||
The output formats supported are
|
||||
`txt`, `csv`, and (experimental) `json`.
|
||||
`txt`, `csv`, and (experimental) `json` and `sql`.
|
||||
|
||||
Here's an example of print's CSV output:
|
||||
|
||||
|
||||
@ -774,7 +774,7 @@ $ hledger print -o - # write to stdout (the default)
|
||||
|
||||
Some commands (print, register, the balance commands) offer a choice of output format.
|
||||
In addition to the usual plain text format (`txt`), there are
|
||||
CSV (`csv`), HTML (`html`) and JSON (`json`).
|
||||
CSV (`csv`), HTML (`html`), JSON (`json`) and SQL (`sql`).
|
||||
This is controlled by the `-O/--output-format` option:
|
||||
```shell
|
||||
$ hledger print -O csv
|
||||
@ -819,6 +819,19 @@ Some notes about JSON output:
|
||||
find otherwise, please let us know.
|
||||
(Cf [#1195](https://github.com/simonmichael/hledger/issues/1195))
|
||||
|
||||
Notes about SQL output:
|
||||
|
||||
- SQL output is also marked experimental, and much like JSON could use
|
||||
real-world feedback.
|
||||
|
||||
- SQL output is expected to work with sqlite, MySQL and PostgreSQL
|
||||
|
||||
- SQL output is structured with the expectations that statements will
|
||||
be executed in the empty database. If you already have tables created
|
||||
via SQL output of hledger, you would probably want to either clear tables
|
||||
of existing data (via `delete` or `truncate` SQL statements) or drop
|
||||
tables completely as otherwise your postings will be duped.
|
||||
|
||||
## Regular expressions
|
||||
|
||||
hledger uses [regular expressions](http://www.regular-expressions.info) in a number of places:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user