2011-06-05 22:36:32 +04:00
|
|
|
{-|
|
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
A general query system for matching things (accounts, postings,
|
2017-07-27 14:59:55 +03:00
|
|
|
transactions..) by various criteria, and a SimpleTextParser for query expressions.
|
2011-06-05 22:36:32 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
{-# LANGUAGE DeriveDataTypeable, OverloadedStrings, ViewPatterns #-}
|
2018-03-25 01:51:56 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
|
2012-05-16 11:57:10 +04:00
|
|
|
module Hledger.Query (
|
2012-05-16 11:50:22 +04:00
|
|
|
-- * Query and QueryOpt
|
2012-05-16 11:12:49 +04:00
|
|
|
Query(..),
|
2012-05-16 11:50:22 +04:00
|
|
|
QueryOpt(..),
|
2012-05-16 12:28:02 +04:00
|
|
|
-- * parsing
|
|
|
|
parseQuery,
|
2012-05-17 20:02:22 +04:00
|
|
|
simplifyQuery,
|
2012-05-27 22:14:20 +04:00
|
|
|
filterQuery,
|
2012-05-16 12:28:02 +04:00
|
|
|
-- * accessors
|
2012-05-16 11:37:24 +04:00
|
|
|
queryIsNull,
|
2014-02-28 05:47:47 +04:00
|
|
|
queryIsAcct,
|
2012-05-27 22:14:20 +04:00
|
|
|
queryIsDepth,
|
|
|
|
queryIsDate,
|
2014-12-25 03:11:30 +03:00
|
|
|
queryIsDate2,
|
|
|
|
queryIsDateOrDate2,
|
2012-05-16 11:50:22 +04:00
|
|
|
queryIsStartDateOnly,
|
2014-02-28 05:47:47 +04:00
|
|
|
queryIsSym,
|
2016-06-01 20:48:57 +03:00
|
|
|
queryIsReal,
|
2016-06-04 03:51:10 +03:00
|
|
|
queryIsStatus,
|
2016-08-09 03:24:37 +03:00
|
|
|
queryIsEmpty,
|
2012-05-27 22:14:20 +04:00
|
|
|
queryStartDate,
|
2014-07-15 18:01:01 +04:00
|
|
|
queryEndDate,
|
2012-05-27 22:14:20 +04:00
|
|
|
queryDateSpan,
|
2014-12-25 03:11:30 +03:00
|
|
|
queryDateSpan',
|
2012-05-27 22:14:20 +04:00
|
|
|
queryDepth,
|
2012-05-16 10:43:13 +04:00
|
|
|
inAccount,
|
2012-05-16 11:37:24 +04:00
|
|
|
inAccountQuery,
|
2012-05-16 11:50:22 +04:00
|
|
|
-- * matching
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesTransaction,
|
2014-02-28 05:47:47 +04:00
|
|
|
matchesPosting,
|
|
|
|
matchesAccount,
|
2014-04-06 06:33:44 +04:00
|
|
|
matchesMixedAmount,
|
2014-02-28 05:47:47 +04:00
|
|
|
matchesAmount,
|
2014-04-15 22:45:30 +04:00
|
|
|
words'',
|
2012-05-16 11:50:22 +04:00
|
|
|
-- * tests
|
2012-05-16 11:57:10 +04:00
|
|
|
tests_Hledger_Query
|
2012-05-16 10:43:13 +04:00
|
|
|
)
|
2011-06-05 22:36:32 +04:00
|
|
|
where
|
2013-12-07 01:19:43 +04:00
|
|
|
import Data.Data
|
2011-06-13 23:46:35 +04:00
|
|
|
import Data.Either
|
2011-06-11 20:00:00 +04:00
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
2018-03-25 01:51:56 +03:00
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
2016-07-29 18:57:10 +03:00
|
|
|
import Data.Monoid ((<>))
|
2018-03-25 01:51:56 +03:00
|
|
|
#endif
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
-- import Data.Text (Text)
|
|
|
|
import qualified Data.Text as T
|
2011-06-05 22:36:32 +04:00
|
|
|
import Data.Time.Calendar
|
2015-05-22 02:24:20 +03:00
|
|
|
import Safe (readDef, headDef)
|
2011-06-11 20:00:00 +04:00
|
|
|
import Test.HUnit
|
2018-05-22 01:47:56 +03:00
|
|
|
import Text.Megaparsec
|
|
|
|
import Text.Megaparsec.Char
|
2011-06-05 22:36:32 +04:00
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
import Hledger.Utils hiding (words')
|
2011-06-05 22:36:32 +04:00
|
|
|
import Hledger.Data.Types
|
2011-06-14 18:29:31 +04:00
|
|
|
import Hledger.Data.AccountName
|
2014-04-06 18:43:04 +04:00
|
|
|
import Hledger.Data.Amount (amount, nullamt, usd)
|
2011-06-05 22:36:32 +04:00
|
|
|
import Hledger.Data.Dates
|
2011-06-29 03:18:36 +04:00
|
|
|
import Hledger.Data.Posting
|
|
|
|
import Hledger.Data.Transaction
|
2011-06-05 22:36:32 +04:00
|
|
|
|
2012-05-16 11:50:22 +04:00
|
|
|
|
2012-05-16 11:37:24 +04:00
|
|
|
-- | A query is a composition of search criteria, which can be used to
|
|
|
|
-- match postings, transactions, accounts and more.
|
2012-05-16 11:12:49 +04:00
|
|
|
data Query = Any -- ^ always match
|
|
|
|
| None -- ^ never match
|
|
|
|
| Not Query -- ^ negate this match
|
|
|
|
| Or [Query] -- ^ match if any of these match
|
|
|
|
| And [Query] -- ^ match if all of these match
|
2015-05-22 02:24:20 +03:00
|
|
|
| Code Regexp -- ^ match if code matches this regexp
|
|
|
|
| Desc Regexp -- ^ match if description matches this regexp
|
|
|
|
| Acct Regexp -- ^ match postings whose account matches this regexp
|
2012-12-06 08:43:41 +04:00
|
|
|
| Date DateSpan -- ^ match if primary date in this date span
|
|
|
|
| Date2 DateSpan -- ^ match if secondary date in this date span
|
2017-06-16 02:54:34 +03:00
|
|
|
| StatusQ Status -- ^ match txns/postings with this status
|
2012-05-16 11:12:49 +04:00
|
|
|
| Real Bool -- ^ match if "realness" (involves a real non-virtual account ?) has this value
|
2014-03-21 06:10:48 +04:00
|
|
|
| Amt OrdPlus Quantity -- ^ match if the amount's numeric quantity is less than/greater than/equal to/unsignedly equal to some value
|
2015-05-22 02:24:20 +03:00
|
|
|
| Sym Regexp -- ^ match if the entire commodity symbol is matched by this regexp
|
2012-05-27 22:14:20 +04:00
|
|
|
| Empty Bool -- ^ if true, show zero-amount postings/accounts which are usually not shown
|
|
|
|
-- more of a query option than a query criteria ?
|
2015-08-28 18:04:54 +03:00
|
|
|
| Depth Int -- ^ match if account depth is less than or equal to this value.
|
|
|
|
-- Depth is sometimes used like a query (for filtering report data)
|
|
|
|
-- and sometimes like a query option (for controlling display)
|
2015-05-22 02:24:20 +03:00
|
|
|
| Tag Regexp (Maybe Regexp) -- ^ match if a tag's name, and optionally its value, is matched by these respective regexps
|
2012-05-28 04:27:55 +04:00
|
|
|
-- matching the regexp if provided, exists
|
2013-12-07 01:19:43 +04:00
|
|
|
deriving (Eq,Data,Typeable)
|
2013-09-23 22:50:20 +04:00
|
|
|
|
|
|
|
-- custom Show implementation to show strings more accurately, eg for debugging regexps
|
|
|
|
instance Show Query where
|
|
|
|
show Any = "Any"
|
|
|
|
show None = "None"
|
|
|
|
show (Not q) = "Not (" ++ show q ++ ")"
|
|
|
|
show (Or qs) = "Or (" ++ show qs ++ ")"
|
|
|
|
show (And qs) = "And (" ++ show qs ++ ")"
|
|
|
|
show (Code r) = "Code " ++ show r
|
|
|
|
show (Desc r) = "Desc " ++ show r
|
|
|
|
show (Acct r) = "Acct " ++ show r
|
|
|
|
show (Date ds) = "Date (" ++ show ds ++ ")"
|
|
|
|
show (Date2 ds) = "Date2 (" ++ show ds ++ ")"
|
2017-06-16 02:52:58 +03:00
|
|
|
show (StatusQ b) = "StatusQ " ++ show b
|
2013-09-23 22:50:20 +04:00
|
|
|
show (Real b) = "Real " ++ show b
|
|
|
|
show (Amt ord qty) = "Amt " ++ show ord ++ " " ++ show qty
|
|
|
|
show (Sym r) = "Sym " ++ show r
|
|
|
|
show (Empty b) = "Empty " ++ show b
|
|
|
|
show (Depth n) = "Depth " ++ show n
|
|
|
|
show (Tag s ms) = "Tag " ++ show s ++ " (" ++ show ms ++ ")"
|
2011-06-05 22:36:32 +04:00
|
|
|
|
2011-06-13 23:46:35 +04:00
|
|
|
-- | A query option changes a query's/report's behaviour and output in some way.
|
2011-07-01 04:32:09 +04:00
|
|
|
data QueryOpt = QueryOptInAcctOnly AccountName -- ^ show an account register focussed on this account
|
|
|
|
| QueryOptInAcct AccountName -- ^ as above but include sub-accounts in the account register
|
2011-06-13 23:46:35 +04:00
|
|
|
-- | QueryOptCostBasis -- ^ show amounts converted to cost where possible
|
2012-12-06 08:43:41 +04:00
|
|
|
-- | QueryOptDate2 -- ^ show secondary dates instead of primary dates
|
2013-12-07 01:19:43 +04:00
|
|
|
deriving (Show, Eq, Data, Typeable)
|
2011-06-13 23:46:35 +04:00
|
|
|
|
2012-05-16 12:28:02 +04:00
|
|
|
-- parsing
|
2012-05-16 11:50:22 +04:00
|
|
|
|
2012-05-16 11:37:24 +04:00
|
|
|
-- -- | A query restricting the account(s) to be shown in the sidebar, if any.
|
2011-07-01 04:32:09 +04:00
|
|
|
-- -- Just looks at the first query option.
|
2012-05-16 11:12:49 +04:00
|
|
|
-- showAccountMatcher :: [QueryOpt] -> Maybe Query
|
|
|
|
-- showAccountMatcher (QueryOptInAcctSubsOnly a:_) = Just $ Acct True $ accountNameToAccountRegex a
|
2011-07-01 04:32:09 +04:00
|
|
|
-- showAccountMatcher _ = Nothing
|
2011-06-13 23:46:35 +04:00
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
|
2011-06-13 02:35:54 +04:00
|
|
|
-- | Convert a query expression containing zero or more space-separated
|
2012-05-16 11:37:24 +04:00
|
|
|
-- terms to a query and zero or more query options. A query term is either:
|
2011-06-13 02:35:54 +04:00
|
|
|
--
|
2012-05-27 22:14:20 +04:00
|
|
|
-- 1. a search pattern, which matches on one or more fields, eg:
|
|
|
|
--
|
|
|
|
-- acct:REGEXP - match the account name with a regular expression
|
|
|
|
-- desc:REGEXP - match the transaction description
|
|
|
|
-- date:PERIODEXP - match the date with a period expression
|
|
|
|
--
|
|
|
|
-- The prefix indicates the field to match, or if there is no prefix
|
|
|
|
-- account name is assumed.
|
|
|
|
--
|
|
|
|
-- 2. a query option, which modifies the reporting behaviour in some
|
|
|
|
-- way. There is currently one of these, which may appear only once:
|
2011-06-13 02:35:54 +04:00
|
|
|
--
|
2012-05-27 22:14:20 +04:00
|
|
|
-- inacct:FULLACCTNAME
|
2011-06-13 02:35:54 +04:00
|
|
|
--
|
2012-05-27 22:14:20 +04:00
|
|
|
-- The usual shell quoting rules are assumed. When a pattern contains
|
|
|
|
-- whitespace, it (or the whole term including prefix) should be enclosed
|
|
|
|
-- in single or double quotes.
|
2011-06-13 02:35:54 +04:00
|
|
|
--
|
2012-05-27 22:14:20 +04:00
|
|
|
-- Period expressions may contain relative dates, so a reference date is
|
|
|
|
-- required to fully parse these.
|
|
|
|
--
|
|
|
|
-- Multiple terms are combined as follows:
|
|
|
|
-- 1. multiple account patterns are OR'd together
|
|
|
|
-- 2. multiple description patterns are OR'd together
|
2017-06-10 23:31:43 +03:00
|
|
|
-- 3. multiple status patterns are OR'd together
|
|
|
|
-- 4. then all terms are AND'd together
|
2016-07-29 18:57:10 +03:00
|
|
|
parseQuery :: Day -> T.Text -> (Query,[QueryOpt])
|
2012-05-27 22:14:20 +04:00
|
|
|
parseQuery d s = (q, opts)
|
2011-06-05 22:36:32 +04:00
|
|
|
where
|
2011-06-13 23:46:35 +04:00
|
|
|
terms = words'' prefixes s
|
2012-05-27 22:14:20 +04:00
|
|
|
(pats, opts) = partitionEithers $ map (parseQueryTerm d) terms
|
|
|
|
(descpats, pats') = partition queryIsDesc pats
|
2017-06-10 23:31:43 +03:00
|
|
|
(acctpats, pats'') = partition queryIsAcct pats'
|
|
|
|
(statuspats, otherpats) = partition queryIsStatus pats''
|
|
|
|
q = simplifyQuery $ And $ [Or acctpats, Or descpats, Or statuspats] ++ otherpats
|
2011-06-13 23:46:35 +04:00
|
|
|
|
2012-05-17 18:59:38 +04:00
|
|
|
tests_parseQuery = [
|
|
|
|
"parseQuery" ~: do
|
2012-05-27 22:14:20 +04:00
|
|
|
let d = nulldate -- parsedate "2011/1/1"
|
2012-05-17 18:59:38 +04:00
|
|
|
parseQuery d "acct:'expenses:autres d\233penses' desc:b" `is` (And [Acct "expenses:autres d\233penses", Desc "b"], [])
|
|
|
|
parseQuery d "inacct:a desc:\"b b\"" `is` (Desc "b b", [QueryOptInAcct "a"])
|
|
|
|
parseQuery d "inacct:a inacct:b" `is` (Any, [QueryOptInAcct "a", QueryOptInAcct "b"])
|
2012-05-27 22:14:20 +04:00
|
|
|
parseQuery d "desc:'x x'" `is` (Desc "x x", [])
|
|
|
|
parseQuery d "'a a' 'b" `is` (Or [Acct "a a",Acct "'b"], [])
|
2012-05-29 21:02:57 +04:00
|
|
|
parseQuery d "\"" `is` (Acct "\"", [])
|
2012-05-17 18:59:38 +04:00
|
|
|
]
|
|
|
|
|
2012-05-29 21:02:18 +04:00
|
|
|
-- XXX
|
2011-06-14 18:27:48 +04:00
|
|
|
-- | Quote-and-prefix-aware version of words - don't split on spaces which
|
|
|
|
-- are inside quotes, including quotes which may have one of the specified
|
|
|
|
-- prefixes in front, and maybe an additional not: prefix in front of that.
|
2016-07-29 18:57:10 +03:00
|
|
|
words'' :: [T.Text] -> T.Text -> [T.Text]
|
2011-06-14 18:27:48 +04:00
|
|
|
words'' prefixes = fromparse . parsewith maybeprefixedquotedphrases -- XXX
|
|
|
|
where
|
2017-07-27 14:59:55 +03:00
|
|
|
maybeprefixedquotedphrases :: SimpleTextParser [T.Text]
|
2018-03-25 16:53:44 +03:00
|
|
|
maybeprefixedquotedphrases = choice' [prefixedQuotedPattern, singleQuotedPattern, doubleQuotedPattern, pattern] `sepBy` skipSome spacenonewline
|
2017-07-27 14:59:55 +03:00
|
|
|
prefixedQuotedPattern :: SimpleTextParser T.Text
|
2011-06-14 18:27:48 +04:00
|
|
|
prefixedQuotedPattern = do
|
2018-05-22 01:47:56 +03:00
|
|
|
not' <- fromMaybe "" `fmap` (optional $ string "not:")
|
2017-07-27 14:59:55 +03:00
|
|
|
let allowednexts | T.null not' = prefixes
|
|
|
|
| otherwise = prefixes ++ [""]
|
2018-05-22 01:47:56 +03:00
|
|
|
next <- choice' $ map string allowednexts
|
2016-07-29 18:57:10 +03:00
|
|
|
let prefix :: T.Text
|
2017-07-27 14:59:55 +03:00
|
|
|
prefix = not' <> next
|
2014-04-15 22:45:30 +04:00
|
|
|
p <- singleQuotedPattern <|> doubleQuotedPattern
|
2016-07-29 18:57:10 +03:00
|
|
|
return $ prefix <> stripquotes p
|
2017-07-27 14:59:55 +03:00
|
|
|
singleQuotedPattern :: SimpleTextParser T.Text
|
2016-07-29 18:57:10 +03:00
|
|
|
singleQuotedPattern = between (char '\'') (char '\'') (many $ noneOf ("'" :: [Char])) >>= return . stripquotes . T.pack
|
2017-07-27 14:59:55 +03:00
|
|
|
doubleQuotedPattern :: SimpleTextParser T.Text
|
2016-07-29 18:57:10 +03:00
|
|
|
doubleQuotedPattern = between (char '"') (char '"') (many $ noneOf ("\"" :: [Char])) >>= return . stripquotes . T.pack
|
2017-07-27 14:59:55 +03:00
|
|
|
pattern :: SimpleTextParser T.Text
|
2016-07-29 18:57:10 +03:00
|
|
|
pattern = fmap T.pack $ many (noneOf (" \n\r" :: [Char]))
|
2011-06-14 18:27:48 +04:00
|
|
|
|
2012-05-17 18:59:38 +04:00
|
|
|
tests_words'' = [
|
|
|
|
"words''" ~: do
|
|
|
|
assertEqual "1" ["a","b"] (words'' [] "a b")
|
|
|
|
assertEqual "2" ["a b"] (words'' [] "'a b'")
|
|
|
|
assertEqual "3" ["not:a","b"] (words'' [] "not:a b")
|
|
|
|
assertEqual "4" ["not:a b"] (words'' [] "not:'a b'")
|
|
|
|
assertEqual "5" ["not:a b"] (words'' [] "'not:a b'")
|
|
|
|
assertEqual "6" ["not:desc:a b"] (words'' ["desc:"] "not:desc:'a b'")
|
|
|
|
let s `gives` r = assertEqual "" r (words'' prefixes s)
|
|
|
|
"\"acct:expenses:autres d\233penses\"" `gives` ["acct:expenses:autres d\233penses"]
|
2012-05-29 21:02:57 +04:00
|
|
|
"\"" `gives` ["\""]
|
2012-05-17 18:59:38 +04:00
|
|
|
]
|
|
|
|
|
2012-05-29 21:02:18 +04:00
|
|
|
-- XXX
|
|
|
|
-- keep synced with patterns below, excluding "not"
|
2016-07-29 18:57:10 +03:00
|
|
|
prefixes :: [T.Text]
|
|
|
|
prefixes = map (<>":") [
|
2012-05-29 21:02:18 +04:00
|
|
|
"inacctonly"
|
|
|
|
,"inacct"
|
2013-09-10 02:04:43 +04:00
|
|
|
,"amt"
|
2013-03-22 21:59:16 +04:00
|
|
|
,"code"
|
2012-05-29 21:02:18 +04:00
|
|
|
,"desc"
|
2017-08-31 00:21:01 +03:00
|
|
|
,"payee"
|
|
|
|
,"note"
|
2012-05-29 21:02:18 +04:00
|
|
|
,"acct"
|
|
|
|
,"date"
|
2014-12-16 22:06:21 +03:00
|
|
|
,"date2"
|
2012-05-29 21:02:18 +04:00
|
|
|
,"status"
|
2014-03-16 20:43:15 +04:00
|
|
|
,"cur"
|
2012-05-29 21:02:18 +04:00
|
|
|
,"real"
|
|
|
|
,"empty"
|
|
|
|
,"depth"
|
|
|
|
,"tag"
|
|
|
|
]
|
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
defaultprefix :: T.Text
|
2012-05-29 21:02:18 +04:00
|
|
|
defaultprefix = "acct"
|
|
|
|
|
2011-06-14 18:27:48 +04:00
|
|
|
-- -- | Parse the query string as a boolean tree of match patterns.
|
2012-05-16 11:12:49 +04:00
|
|
|
-- parseQueryTerm :: String -> Query
|
|
|
|
-- parseQueryTerm s = either (const (Any)) id $ runParser query () "" $ lexmatcher s
|
2011-06-14 18:27:48 +04:00
|
|
|
|
|
|
|
-- lexmatcher :: String -> [String]
|
|
|
|
-- lexmatcher s = words' s
|
|
|
|
|
2012-05-16 11:12:49 +04:00
|
|
|
-- query :: GenParser String () Query
|
2012-05-16 11:37:24 +04:00
|
|
|
-- query = undefined
|
2011-06-14 18:27:48 +04:00
|
|
|
|
2014-08-08 04:26:08 +04:00
|
|
|
-- | Parse a single query term as either a query or a query option,
|
|
|
|
-- or raise an error if it has invalid syntax.
|
2016-07-29 18:57:10 +03:00
|
|
|
parseQueryTerm :: Day -> T.Text -> Either Query QueryOpt
|
|
|
|
parseQueryTerm _ (T.stripPrefix "inacctonly:" -> Just s) = Right $ QueryOptInAcctOnly s
|
|
|
|
parseQueryTerm _ (T.stripPrefix "inacct:" -> Just s) = Right $ QueryOptInAcct s
|
|
|
|
parseQueryTerm d (T.stripPrefix "not:" -> Just s) =
|
|
|
|
case parseQueryTerm d s of
|
|
|
|
Left m -> Left $ Not m
|
|
|
|
Right _ -> Left Any -- not:somequeryoption will be ignored
|
|
|
|
parseQueryTerm _ (T.stripPrefix "code:" -> Just s) = Left $ Code $ T.unpack s
|
|
|
|
parseQueryTerm _ (T.stripPrefix "desc:" -> Just s) = Left $ Desc $ T.unpack s
|
2017-08-31 00:21:01 +03:00
|
|
|
parseQueryTerm _ (T.stripPrefix "payee:" -> Just s) = Left $ Tag "payee" $ Just $ T.unpack s
|
|
|
|
parseQueryTerm _ (T.stripPrefix "note:" -> Just s) = Left $ Tag "note" $ Just $ T.unpack s
|
2016-07-29 18:57:10 +03:00
|
|
|
parseQueryTerm _ (T.stripPrefix "acct:" -> Just s) = Left $ Acct $ T.unpack s
|
|
|
|
parseQueryTerm d (T.stripPrefix "date2:" -> Just s) =
|
|
|
|
case parsePeriodExpr d s of Left e -> error' $ "\"date2:"++T.unpack s++"\" gave a "++showDateParseError e
|
2014-12-16 22:06:21 +03:00
|
|
|
Right (_,span) -> Left $ Date2 span
|
2016-07-29 18:57:10 +03:00
|
|
|
parseQueryTerm d (T.stripPrefix "date:" -> Just s) =
|
|
|
|
case parsePeriodExpr d s of Left e -> error' $ "\"date:"++T.unpack s++"\" gave a "++showDateParseError e
|
2012-05-16 11:12:49 +04:00
|
|
|
Right (_,span) -> Left $ Date span
|
2016-07-29 18:57:10 +03:00
|
|
|
parseQueryTerm _ (T.stripPrefix "status:" -> Just s) =
|
|
|
|
case parseStatus s of Left e -> error' $ "\"status:"++T.unpack s++"\" gave a parse error: " ++ e
|
2017-06-16 02:52:58 +03:00
|
|
|
Right st -> Left $ StatusQ st
|
2016-07-29 18:57:10 +03:00
|
|
|
parseQueryTerm _ (T.stripPrefix "real:" -> Just s) = Left $ Real $ parseBool s || T.null s
|
|
|
|
parseQueryTerm _ (T.stripPrefix "amt:" -> Just s) = Left $ Amt ord q where (ord, q) = parseAmountQueryTerm s
|
|
|
|
parseQueryTerm _ (T.stripPrefix "empty:" -> Just s) = Left $ Empty $ parseBool s
|
|
|
|
parseQueryTerm _ (T.stripPrefix "depth:" -> Just s)
|
2015-08-28 19:57:30 +03:00
|
|
|
| n >= 0 = Left $ Depth n
|
|
|
|
| otherwise = error' "depth: should have a positive number"
|
2016-07-29 18:57:10 +03:00
|
|
|
where n = readDef 0 (T.unpack s)
|
2015-08-28 19:57:30 +03:00
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
parseQueryTerm _ (T.stripPrefix "cur:" -> Just s) = Left $ Sym (T.unpack s) -- support cur: as an alias
|
|
|
|
parseQueryTerm _ (T.stripPrefix "tag:" -> Just s) = Left $ Tag n v where (n,v) = parseTag s
|
2012-05-16 11:12:49 +04:00
|
|
|
parseQueryTerm _ "" = Left $ Any
|
2016-07-29 18:57:10 +03:00
|
|
|
parseQueryTerm d s = parseQueryTerm d $ defaultprefix<>":"<>s
|
2011-06-13 23:46:35 +04:00
|
|
|
|
2012-05-17 18:59:38 +04:00
|
|
|
tests_parseQueryTerm = [
|
|
|
|
"parseQueryTerm" ~: do
|
|
|
|
let s `gives` r = parseQueryTerm nulldate s `is` r
|
|
|
|
"a" `gives` (Left $ Acct "a")
|
|
|
|
"acct:expenses:autres d\233penses" `gives` (Left $ Acct "expenses:autres d\233penses")
|
|
|
|
"not:desc:a b" `gives` (Left $ Not $ Desc "a b")
|
2017-06-16 02:52:58 +03:00
|
|
|
"status:1" `gives` (Left $ StatusQ Cleared)
|
|
|
|
"status:*" `gives` (Left $ StatusQ Cleared)
|
|
|
|
"status:!" `gives` (Left $ StatusQ Pending)
|
|
|
|
"status:0" `gives` (Left $ StatusQ Unmarked)
|
|
|
|
"status:" `gives` (Left $ StatusQ Unmarked)
|
2017-08-31 00:21:01 +03:00
|
|
|
"payee:x" `gives` (Left $ Tag "payee" (Just "x"))
|
|
|
|
"note:x" `gives` (Left $ Tag "note" (Just "x"))
|
2012-05-17 18:59:38 +04:00
|
|
|
"real:1" `gives` (Left $ Real True)
|
|
|
|
"date:2008" `gives` (Left $ Date $ DateSpan (Just $ parsedate "2008/01/01") (Just $ parsedate "2009/01/01"))
|
|
|
|
"date:from 2012/5/17" `gives` (Left $ Date $ DateSpan (Just $ parsedate "2012/05/17") Nothing)
|
2018-04-04 19:45:23 +03:00
|
|
|
"date:20180101-201804" `gives` (Left $ Date $ DateSpan (Just $ parsedate "2018/01/01") (Just $ parsedate "2018/04/01"))
|
2012-05-17 18:59:38 +04:00
|
|
|
"inacct:a" `gives` (Right $ QueryOptInAcct "a")
|
2012-05-28 04:27:55 +04:00
|
|
|
"tag:a" `gives` (Left $ Tag "a" Nothing)
|
|
|
|
"tag:a=some value" `gives` (Left $ Tag "a" (Just "some value"))
|
2013-03-20 20:36:00 +04:00
|
|
|
-- "amt:<0" `gives` (Left $ Amt LT 0)
|
|
|
|
-- "amt:=.23" `gives` (Left $ Amt EQ 0.23)
|
|
|
|
-- "amt:>10000.10" `gives` (Left $ Amt GT 10000.1)
|
2012-05-17 18:59:38 +04:00
|
|
|
]
|
|
|
|
|
2014-03-21 06:10:48 +04:00
|
|
|
|
2014-06-29 22:09:13 +04:00
|
|
|
data OrdPlus = Lt | LtEq | Gt | GtEq | Eq | AbsLt | AbsLtEq | AbsGt | AbsGtEq | AbsEq
|
2014-03-21 06:10:48 +04:00
|
|
|
deriving (Show,Eq,Data,Typeable)
|
|
|
|
|
2013-03-20 20:36:00 +04:00
|
|
|
-- can fail
|
2016-07-29 18:57:10 +03:00
|
|
|
parseAmountQueryTerm :: T.Text -> (OrdPlus, Quantity)
|
2014-03-21 06:10:48 +04:00
|
|
|
parseAmountQueryTerm s' =
|
|
|
|
case s' of
|
|
|
|
-- feel free to do this a smarter way
|
2014-06-29 22:09:13 +04:00
|
|
|
"" -> err
|
2016-07-29 18:57:10 +03:00
|
|
|
(T.stripPrefix "<+" -> Just s) -> (Lt, readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix "<=+" -> Just s) -> (LtEq, readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix ">+" -> Just s) -> (Gt, readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix ">=+" -> Just s) -> (GtEq, readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix "=+" -> Just s) -> (Eq, readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix "+" -> Just s) -> (Eq, readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix "<-" -> Just s) -> (Lt, negate $ readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix "<=-" -> Just s) -> (LtEq, negate $ readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix ">-" -> Just s) -> (Gt, negate $ readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix ">=-" -> Just s) -> (GtEq, negate $ readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix "=-" -> Just s) -> (Eq, negate $ readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix "-" -> Just s) -> (Eq, negate $ readDef err (T.unpack s))
|
|
|
|
(T.stripPrefix "<=" -> Just s) -> let n = readDef err (T.unpack s) in
|
|
|
|
case n of
|
|
|
|
0 -> (LtEq, 0)
|
|
|
|
_ -> (AbsLtEq, n)
|
|
|
|
(T.stripPrefix "<" -> Just s) -> let n = readDef err (T.unpack s) in
|
|
|
|
case n of 0 -> (Lt, 0)
|
|
|
|
_ -> (AbsLt, n)
|
|
|
|
(T.stripPrefix ">=" -> Just s) -> let n = readDef err (T.unpack s) in
|
|
|
|
case n of 0 -> (GtEq, 0)
|
|
|
|
_ -> (AbsGtEq, n)
|
|
|
|
(T.stripPrefix ">" -> Just s) -> let n = readDef err (T.unpack s) in
|
|
|
|
case n of 0 -> (Gt, 0)
|
|
|
|
_ -> (AbsGt, n)
|
|
|
|
(T.stripPrefix "=" -> Just s) -> (AbsEq, readDef err (T.unpack s))
|
|
|
|
s -> (AbsEq, readDef err (T.unpack s))
|
2013-09-09 22:57:25 +04:00
|
|
|
where
|
2016-07-29 18:57:10 +03:00
|
|
|
err = error' $ "could not parse as '=', '<', or '>' (optional) followed by a (optionally signed) numeric quantity: " ++ T.unpack s'
|
2013-03-20 20:36:00 +04:00
|
|
|
|
2013-09-10 04:41:29 +04:00
|
|
|
tests_parseAmountQueryTerm = [
|
|
|
|
"parseAmountQueryTerm" ~: do
|
|
|
|
let s `gives` r = parseAmountQueryTerm s `is` r
|
2014-04-28 10:59:11 +04:00
|
|
|
"<0" `gives` (Lt,0) -- special case for convenience, since AbsLt 0 would be always false
|
|
|
|
">0" `gives` (Gt,0) -- special case for convenience and consistency with above
|
2014-03-21 06:10:48 +04:00
|
|
|
">10000.10" `gives` (AbsGt,10000.1)
|
|
|
|
"=0.23" `gives` (AbsEq,0.23)
|
|
|
|
"0.23" `gives` (AbsEq,0.23)
|
2014-06-29 22:09:13 +04:00
|
|
|
"<=+0.23" `gives` (LtEq,0.23)
|
2014-03-21 06:10:48 +04:00
|
|
|
"-0.23" `gives` (Eq,(-0.23))
|
2013-03-20 20:36:00 +04:00
|
|
|
]
|
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
parseTag :: T.Text -> (Regexp, Maybe Regexp)
|
|
|
|
parseTag s | "=" `T.isInfixOf` s = (T.unpack n, Just $ tail $ T.unpack v)
|
|
|
|
| otherwise = (T.unpack s, Nothing)
|
|
|
|
where (n,v) = T.break (=='=') s
|
2012-05-28 04:27:55 +04:00
|
|
|
|
2015-05-16 22:21:50 +03:00
|
|
|
-- | Parse the value part of a "status:" query, or return an error.
|
2017-06-16 02:54:34 +03:00
|
|
|
parseStatus :: T.Text -> Either String Status
|
2015-05-16 22:21:50 +03:00
|
|
|
parseStatus s | s `elem` ["*","1"] = Right Cleared
|
|
|
|
| s `elem` ["!"] = Right Pending
|
2017-06-16 02:25:37 +03:00
|
|
|
| s `elem` ["","0"] = Right Unmarked
|
2015-05-16 22:21:50 +03:00
|
|
|
| otherwise = Left $ "could not parse "++show s++" as a status (should be *, ! or empty)"
|
2011-06-13 23:46:35 +04:00
|
|
|
|
2014-12-25 10:48:23 +03:00
|
|
|
-- | Parse the boolean value part of a "status:" query. "1" means true,
|
|
|
|
-- anything else will be parsed as false without error.
|
2016-07-29 18:57:10 +03:00
|
|
|
parseBool :: T.Text -> Bool
|
2011-06-29 03:18:36 +04:00
|
|
|
parseBool s = s `elem` truestrings
|
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
truestrings :: [T.Text]
|
2014-12-25 10:48:23 +03:00
|
|
|
truestrings = ["1"]
|
2011-06-05 22:36:32 +04:00
|
|
|
|
2012-05-17 20:02:22 +04:00
|
|
|
simplifyQuery :: Query -> Query
|
2012-05-27 22:14:20 +04:00
|
|
|
simplifyQuery q =
|
|
|
|
let q' = simplify q
|
|
|
|
in if q' == q then q else simplifyQuery q'
|
|
|
|
where
|
|
|
|
simplify (And []) = Any
|
|
|
|
simplify (And [q]) = simplify q
|
|
|
|
simplify (And qs) | same qs = simplify $ head qs
|
|
|
|
| any (==None) qs = None
|
|
|
|
| all queryIsDate qs = Date $ spansIntersect $ mapMaybe queryTermDateSpan qs
|
|
|
|
| otherwise = And $ concat $ [map simplify dateqs, map simplify otherqs]
|
|
|
|
where (dateqs, otherqs) = partition queryIsDate $ filter (/=Any) qs
|
|
|
|
simplify (Or []) = Any
|
|
|
|
simplify (Or [q]) = simplifyQuery q
|
|
|
|
simplify (Or qs) | same qs = simplify $ head qs
|
|
|
|
| any (==Any) qs = Any
|
|
|
|
-- all queryIsDate qs = Date $ spansUnion $ mapMaybe queryTermDateSpan qs ?
|
|
|
|
| otherwise = Or $ map simplify $ filter (/=None) qs
|
|
|
|
simplify (Date (DateSpan Nothing Nothing)) = Any
|
2014-12-25 01:43:49 +03:00
|
|
|
simplify (Date2 (DateSpan Nothing Nothing)) = Any
|
2012-05-27 22:14:20 +04:00
|
|
|
simplify q = q
|
|
|
|
|
|
|
|
tests_simplifyQuery = [
|
|
|
|
"simplifyQuery" ~: do
|
|
|
|
let q `gives` r = assertEqual "" r (simplifyQuery q)
|
|
|
|
Or [Acct "a"] `gives` Acct "a"
|
|
|
|
Or [Any,None] `gives` Any
|
|
|
|
And [Any,None] `gives` None
|
|
|
|
And [Any,Any] `gives` Any
|
|
|
|
And [Acct "b",Any] `gives` Acct "b"
|
|
|
|
And [Any,And [Date (DateSpan Nothing Nothing)]] `gives` Any
|
|
|
|
And [Date (DateSpan Nothing (Just $ parsedate "2013-01-01")), Date (DateSpan (Just $ parsedate "2012-01-01") Nothing)]
|
|
|
|
`gives` Date (DateSpan (Just $ parsedate "2012-01-01") (Just $ parsedate "2013-01-01"))
|
|
|
|
And [Or [],Or [Desc "b b"]] `gives` Desc "b b"
|
|
|
|
]
|
|
|
|
|
|
|
|
same [] = True
|
|
|
|
same (a:as) = all (a==) as
|
|
|
|
|
|
|
|
-- | Remove query terms (or whole sub-expressions) not matching the given
|
2016-05-07 03:19:23 +03:00
|
|
|
-- predicate from this query. XXX Semantics not completely clear.
|
2012-05-27 22:14:20 +04:00
|
|
|
filterQuery :: (Query -> Bool) -> Query -> Query
|
2012-06-30 02:36:30 +04:00
|
|
|
filterQuery p = simplifyQuery . filterQuery' p
|
|
|
|
|
|
|
|
filterQuery' :: (Query -> Bool) -> Query -> Query
|
|
|
|
filterQuery' p (And qs) = And $ map (filterQuery p) qs
|
|
|
|
filterQuery' p (Or qs) = Or $ map (filterQuery p) qs
|
|
|
|
-- filterQuery' p (Not q) = Not $ filterQuery p q
|
|
|
|
filterQuery' p q = if p q then q else Any
|
2012-05-27 22:14:20 +04:00
|
|
|
|
|
|
|
tests_filterQuery = [
|
|
|
|
"filterQuery" ~: do
|
|
|
|
let (q,p) `gives` r = assertEqual "" r (filterQuery p q)
|
|
|
|
(Any, queryIsDepth) `gives` Any
|
|
|
|
(Depth 1, queryIsDepth) `gives` Depth 1
|
2017-06-16 02:52:58 +03:00
|
|
|
(And [And [StatusQ Cleared,Depth 1]], not . queryIsDepth) `gives` StatusQ Cleared
|
2012-05-27 22:14:20 +04:00
|
|
|
-- (And [Date nulldatespan, Not (Or [Any, Depth 1])], queryIsDepth) `gives` And [Not (Or [Depth 1])]
|
|
|
|
]
|
2012-05-17 20:02:22 +04:00
|
|
|
|
2012-05-16 12:28:02 +04:00
|
|
|
-- * accessors
|
|
|
|
|
|
|
|
-- | Does this query match everything ?
|
2012-05-27 22:14:20 +04:00
|
|
|
queryIsNull :: Query -> Bool
|
2012-05-16 12:28:02 +04:00
|
|
|
queryIsNull Any = True
|
|
|
|
queryIsNull (And []) = True
|
|
|
|
queryIsNull (Not (Or [])) = True
|
|
|
|
queryIsNull _ = False
|
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
queryIsDepth :: Query -> Bool
|
|
|
|
queryIsDepth (Depth _) = True
|
|
|
|
queryIsDepth _ = False
|
|
|
|
|
|
|
|
queryIsDate :: Query -> Bool
|
|
|
|
queryIsDate (Date _) = True
|
|
|
|
queryIsDate _ = False
|
|
|
|
|
2014-12-25 03:11:30 +03:00
|
|
|
queryIsDate2 :: Query -> Bool
|
|
|
|
queryIsDate2 (Date2 _) = True
|
|
|
|
queryIsDate2 _ = False
|
|
|
|
|
|
|
|
queryIsDateOrDate2 :: Query -> Bool
|
|
|
|
queryIsDateOrDate2 (Date _) = True
|
|
|
|
queryIsDateOrDate2 (Date2 _) = True
|
|
|
|
queryIsDateOrDate2 _ = False
|
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
queryIsDesc :: Query -> Bool
|
|
|
|
queryIsDesc (Desc _) = True
|
|
|
|
queryIsDesc _ = False
|
|
|
|
|
|
|
|
queryIsAcct :: Query -> Bool
|
|
|
|
queryIsAcct (Acct _) = True
|
|
|
|
queryIsAcct _ = False
|
2012-05-16 12:28:02 +04:00
|
|
|
|
2014-02-28 05:47:47 +04:00
|
|
|
queryIsSym :: Query -> Bool
|
|
|
|
queryIsSym (Sym _) = True
|
|
|
|
queryIsSym _ = False
|
|
|
|
|
2016-06-01 20:48:57 +03:00
|
|
|
queryIsReal :: Query -> Bool
|
|
|
|
queryIsReal (Real _) = True
|
|
|
|
queryIsReal _ = False
|
|
|
|
|
2016-06-04 03:51:10 +03:00
|
|
|
queryIsStatus :: Query -> Bool
|
2017-06-16 02:52:58 +03:00
|
|
|
queryIsStatus (StatusQ _) = True
|
2016-06-04 03:51:10 +03:00
|
|
|
queryIsStatus _ = False
|
|
|
|
|
2016-08-09 03:24:37 +03:00
|
|
|
queryIsEmpty :: Query -> Bool
|
|
|
|
queryIsEmpty (Empty _) = True
|
|
|
|
queryIsEmpty _ = False
|
|
|
|
|
2012-05-16 12:28:02 +04:00
|
|
|
-- | Does this query specify a start date and nothing else (that would
|
|
|
|
-- filter postings prior to the date) ?
|
2012-12-06 08:43:41 +04:00
|
|
|
-- When the flag is true, look for a starting secondary date instead.
|
2012-05-16 12:28:02 +04:00
|
|
|
queryIsStartDateOnly :: Bool -> Query -> Bool
|
|
|
|
queryIsStartDateOnly _ Any = False
|
|
|
|
queryIsStartDateOnly _ None = False
|
2012-12-06 08:43:41 +04:00
|
|
|
queryIsStartDateOnly secondary (Or ms) = and $ map (queryIsStartDateOnly secondary) ms
|
|
|
|
queryIsStartDateOnly secondary (And ms) = and $ map (queryIsStartDateOnly secondary) ms
|
2012-05-16 12:28:02 +04:00
|
|
|
queryIsStartDateOnly False (Date (DateSpan (Just _) _)) = True
|
2012-12-06 08:43:41 +04:00
|
|
|
queryIsStartDateOnly True (Date2 (DateSpan (Just _) _)) = True
|
2012-05-16 12:28:02 +04:00
|
|
|
queryIsStartDateOnly _ _ = False
|
|
|
|
|
2012-12-06 08:43:41 +04:00
|
|
|
-- | What start date (or secondary date) does this query specify, if any ?
|
2012-05-27 22:14:20 +04:00
|
|
|
-- For OR expressions, use the earliest of the dates. NOT is ignored.
|
|
|
|
queryStartDate :: Bool -> Query -> Maybe Day
|
2012-12-06 08:43:41 +04:00
|
|
|
queryStartDate secondary (Or ms) = earliestMaybeDate $ map (queryStartDate secondary) ms
|
|
|
|
queryStartDate secondary (And ms) = latestMaybeDate $ map (queryStartDate secondary) ms
|
2012-05-27 22:14:20 +04:00
|
|
|
queryStartDate False (Date (DateSpan (Just d) _)) = Just d
|
2012-12-06 08:43:41 +04:00
|
|
|
queryStartDate True (Date2 (DateSpan (Just d) _)) = Just d
|
2012-05-27 22:14:20 +04:00
|
|
|
queryStartDate _ _ = Nothing
|
|
|
|
|
2014-07-15 18:01:01 +04:00
|
|
|
-- | What end date (or secondary date) does this query specify, if any ?
|
|
|
|
-- For OR expressions, use the latest of the dates. NOT is ignored.
|
|
|
|
queryEndDate :: Bool -> Query -> Maybe Day
|
|
|
|
queryEndDate secondary (Or ms) = latestMaybeDate' $ map (queryEndDate secondary) ms
|
|
|
|
queryEndDate secondary (And ms) = earliestMaybeDate' $ map (queryEndDate secondary) ms
|
|
|
|
queryEndDate False (Date (DateSpan _ (Just d))) = Just d
|
|
|
|
queryEndDate True (Date2 (DateSpan _ (Just d))) = Just d
|
|
|
|
queryEndDate _ _ = Nothing
|
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
queryTermDateSpan (Date span) = Just span
|
|
|
|
queryTermDateSpan _ = Nothing
|
|
|
|
|
2018-07-14 13:10:16 +03:00
|
|
|
-- | What date span (or with a true argument, what secondary date span) does this query specify ?
|
|
|
|
-- OR clauses specifying multiple spans return their union (the span enclosing all of them).
|
|
|
|
-- AND clauses specifying multiple spans return their intersection.
|
|
|
|
-- NOT clauses are ignored.
|
2012-05-27 22:14:20 +04:00
|
|
|
queryDateSpan :: Bool -> Query -> DateSpan
|
2018-07-14 13:10:16 +03:00
|
|
|
queryDateSpan secondary (Or qs) = spansUnion $ map (queryDateSpan secondary) qs
|
|
|
|
queryDateSpan secondary (And qs) = spansIntersect $ map (queryDateSpan secondary) qs
|
|
|
|
queryDateSpan False (Date span) = span
|
|
|
|
queryDateSpan True (Date2 span) = span
|
|
|
|
queryDateSpan _ _ = nulldatespan
|
|
|
|
|
|
|
|
-- | What date span does this query specify, treating primary and secondary dates as equivalent ?
|
|
|
|
-- OR clauses specifying multiple spans return their union (the span enclosing all of them).
|
|
|
|
-- AND clauses specifying multiple spans return their intersection.
|
|
|
|
-- NOT clauses are ignored.
|
2014-12-25 03:11:30 +03:00
|
|
|
queryDateSpan' :: Query -> DateSpan
|
2018-07-14 13:10:16 +03:00
|
|
|
queryDateSpan' (Or qs) = spansUnion $ map queryDateSpan' qs
|
|
|
|
queryDateSpan' (And qs) = spansIntersect $ map queryDateSpan' qs
|
|
|
|
queryDateSpan' (Date span) = span
|
|
|
|
queryDateSpan' (Date2 span) = span
|
|
|
|
queryDateSpan' _ = nulldatespan
|
2014-12-25 03:11:30 +03:00
|
|
|
|
2014-07-15 18:01:01 +04:00
|
|
|
-- | What is the earliest of these dates, where Nothing is latest ?
|
2012-05-16 12:28:02 +04:00
|
|
|
earliestMaybeDate :: [Maybe Day] -> Maybe Day
|
2014-07-15 18:01:01 +04:00
|
|
|
earliestMaybeDate mds = head $ sortBy compareMaybeDates mds ++ [Nothing]
|
2012-05-16 12:28:02 +04:00
|
|
|
|
|
|
|
-- | What is the latest of these dates, where Nothing is earliest ?
|
|
|
|
latestMaybeDate :: [Maybe Day] -> Maybe Day
|
|
|
|
latestMaybeDate = headDef Nothing . sortBy (flip compareMaybeDates)
|
|
|
|
|
2014-07-15 18:01:01 +04:00
|
|
|
-- | What is the earliest of these dates, ignoring Nothings ?
|
|
|
|
earliestMaybeDate' :: [Maybe Day] -> Maybe Day
|
|
|
|
earliestMaybeDate' = headDef Nothing . sortBy compareMaybeDates . filter isJust
|
|
|
|
|
|
|
|
-- | What is the latest of these dates, ignoring Nothings ?
|
|
|
|
latestMaybeDate' :: [Maybe Day] -> Maybe Day
|
|
|
|
latestMaybeDate' = headDef Nothing . sortBy (flip compareMaybeDates) . filter isJust
|
|
|
|
|
2012-05-16 12:28:02 +04:00
|
|
|
-- | Compare two maybe dates, Nothing is earliest.
|
|
|
|
compareMaybeDates :: Maybe Day -> Maybe Day -> Ordering
|
|
|
|
compareMaybeDates Nothing Nothing = EQ
|
|
|
|
compareMaybeDates Nothing (Just _) = LT
|
|
|
|
compareMaybeDates (Just _) Nothing = GT
|
|
|
|
compareMaybeDates (Just a) (Just b) = compare a b
|
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
-- | The depth limit this query specifies, or a large number if none.
|
|
|
|
queryDepth :: Query -> Int
|
|
|
|
queryDepth q = case queryDepth' q of [] -> 99999
|
|
|
|
ds -> minimum ds
|
|
|
|
where
|
|
|
|
queryDepth' (Depth d) = [d]
|
|
|
|
queryDepth' (Or qs) = concatMap queryDepth' qs
|
|
|
|
queryDepth' (And qs) = concatMap queryDepth' qs
|
|
|
|
queryDepth' _ = []
|
|
|
|
|
2012-05-16 12:28:02 +04:00
|
|
|
-- | The account we are currently focussed on, if any, and whether subaccounts are included.
|
|
|
|
-- Just looks at the first query option.
|
|
|
|
inAccount :: [QueryOpt] -> Maybe (AccountName,Bool)
|
|
|
|
inAccount [] = Nothing
|
|
|
|
inAccount (QueryOptInAcctOnly a:_) = Just (a,False)
|
|
|
|
inAccount (QueryOptInAcct a:_) = Just (a,True)
|
|
|
|
|
|
|
|
-- | A query for the account(s) we are currently focussed on, if any.
|
|
|
|
-- Just looks at the first query option.
|
|
|
|
inAccountQuery :: [QueryOpt] -> Maybe Query
|
|
|
|
inAccountQuery [] = Nothing
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
inAccountQuery (QueryOptInAcctOnly a : _) = Just $ Acct $ accountNameToAccountOnlyRegex a
|
|
|
|
inAccountQuery (QueryOptInAcct a : _) = Just $ Acct $ accountNameToAccountRegex a
|
2012-05-16 12:28:02 +04:00
|
|
|
|
|
|
|
-- -- | Convert a query to its inverse.
|
|
|
|
-- negateQuery :: Query -> Query
|
|
|
|
-- negateQuery = Not
|
|
|
|
|
|
|
|
-- matching
|
2011-06-11 20:00:00 +04:00
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
-- | Does the match expression match this account ?
|
|
|
|
-- A matching in: clause is also considered a match.
|
|
|
|
matchesAccount :: Query -> AccountName -> Bool
|
|
|
|
matchesAccount (None) _ = False
|
|
|
|
matchesAccount (Not m) a = not $ matchesAccount m a
|
|
|
|
matchesAccount (Or ms) a = any (`matchesAccount` a) ms
|
|
|
|
matchesAccount (And ms) a = all (`matchesAccount` a) ms
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
matchesAccount (Acct r) a = regexMatchesCI r (T.unpack a) -- XXX pack
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesAccount (Depth d) a = accountNameLevel a <= d
|
2012-05-28 04:27:55 +04:00
|
|
|
matchesAccount (Tag _ _) _ = False
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesAccount _ _ = True
|
|
|
|
|
|
|
|
tests_matchesAccount = [
|
|
|
|
"matchesAccount" ~: do
|
|
|
|
assertBool "positive acct match" $ matchesAccount (Acct "b:c") "a:bb:c:d"
|
|
|
|
-- assertBool "acct should match at beginning" $ not $ matchesAccount (Acct True "a:b") "c:a:b"
|
|
|
|
let q `matches` a = assertBool "" $ q `matchesAccount` a
|
|
|
|
Depth 2 `matches` "a:b"
|
|
|
|
assertBool "" $ Depth 2 `matchesAccount` "a"
|
|
|
|
assertBool "" $ Depth 2 `matchesAccount` "a:b"
|
|
|
|
assertBool "" $ not $ Depth 2 `matchesAccount` "a:b:c"
|
|
|
|
assertBool "" $ Date nulldatespan `matchesAccount` "a"
|
2012-12-06 08:43:41 +04:00
|
|
|
assertBool "" $ Date2 nulldatespan `matchesAccount` "a"
|
2012-05-28 04:27:55 +04:00
|
|
|
assertBool "" $ not $ (Tag "a" Nothing) `matchesAccount` "a"
|
2012-05-27 22:14:20 +04:00
|
|
|
]
|
|
|
|
|
2014-04-06 06:33:44 +04:00
|
|
|
matchesMixedAmount :: Query -> MixedAmount -> Bool
|
|
|
|
matchesMixedAmount q (Mixed []) = q `matchesAmount` nullamt
|
|
|
|
matchesMixedAmount q (Mixed as) = any (q `matchesAmount`) as
|
|
|
|
|
2014-02-28 05:47:47 +04:00
|
|
|
-- | Does the match expression match this (simple) amount ?
|
|
|
|
matchesAmount :: Query -> Amount -> Bool
|
|
|
|
matchesAmount (Not q) a = not $ q `matchesAmount` a
|
|
|
|
matchesAmount (Any) _ = True
|
|
|
|
matchesAmount (None) _ = False
|
|
|
|
matchesAmount (Or qs) a = any (`matchesAmount` a) qs
|
|
|
|
matchesAmount (And qs) a = all (`matchesAmount` a) qs
|
2014-04-06 06:33:44 +04:00
|
|
|
--
|
|
|
|
matchesAmount (Amt ord n) a = compareAmount ord n a
|
lib: textification: commodity symbols
hledger -f data/100x100x10.journal stats
<<ghc: 39288536 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.028 elapsed), 0.009 GC (0.012 elapsed) :ghc>>
<<ghc: 39290808 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.003 elapsed), 0.015 MUT (0.021 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
<<ghc: 314268960 bytes, 612 GCs, 2143219/6826152 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.009 elapsed), 0.135 MUT (0.151 elapsed), 0.065 GC (0.178 elapsed) :ghc>>
<<ghc: 314254512 bytes, 612 GCs, 2072377/6628024 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.130 MUT (0.134 elapsed), 0.064 GC (0.075 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
<<ghc: 3070016592 bytes, 5965 GCs, 13138220/64266016 avg/max bytes residency (10 samples), 128M in use, 0.000 INIT (0.000 elapsed), 1.272 MUT (1.322 elapsed), 0.527 GC (0.595 elapsed) :ghc>>
<<ghc: 3069989896 bytes, 5973 GCs, 12687877/62848920 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.002 elapsed), 1.295 MUT (1.324 elapsed), 0.511 GC (0.570 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
<<ghc: 30753448072 bytes, 59763 GCs, 121502982/673169248 avg/max bytes residency (14 samples), 1640M in use, 0.000 INIT (0.007 elapsed), 12.421 MUT (12.672 elapsed), 6.240 GC (7.812 elapsed) :ghc>>
<<ghc: 30753350528 bytes, 59811 GCs, 117616668/666703600 avg/max bytes residency (14 samples), 1588M in use, 0.001 INIT (0.011 elapsed), 13.209 MUT (13.683 elapsed), 6.137 GC (7.117 elapsed) :ghc>>
2016-05-24 05:13:43 +03:00
|
|
|
matchesAmount (Sym r) a = regexMatchesCI ("^" ++ r ++ "$") $ T.unpack $ acommodity a
|
2014-04-06 06:33:44 +04:00
|
|
|
--
|
2014-02-28 05:47:47 +04:00
|
|
|
matchesAmount _ _ = True
|
|
|
|
|
2014-03-21 06:10:48 +04:00
|
|
|
-- | Is this simple (single-amount) mixed amount's quantity less than, greater than, equal to, or unsignedly equal to this number ?
|
|
|
|
-- For multi-amount (multiple commodities, or just unsimplified) mixed amounts this is always true.
|
2014-04-06 06:33:44 +04:00
|
|
|
|
|
|
|
-- | Is this amount's quantity less than, greater than, equal to, or unsignedly equal to this number ?
|
|
|
|
compareAmount :: OrdPlus -> Quantity -> Amount -> Bool
|
2014-06-29 22:09:13 +04:00
|
|
|
compareAmount ord q Amount{aquantity=aq} = case ord of Lt -> aq < q
|
|
|
|
LtEq -> aq <= q
|
|
|
|
Gt -> aq > q
|
|
|
|
GtEq -> aq >= q
|
|
|
|
Eq -> aq == q
|
|
|
|
AbsLt -> abs aq < abs q
|
|
|
|
AbsLtEq -> abs aq <= abs q
|
|
|
|
AbsGt -> abs aq > abs q
|
|
|
|
AbsGtEq -> abs aq >= abs q
|
|
|
|
AbsEq -> abs aq == abs q
|
2014-02-28 05:47:47 +04:00
|
|
|
|
2011-06-11 20:00:00 +04:00
|
|
|
-- | Does the match expression match this posting ?
|
2017-01-13 19:02:11 +03:00
|
|
|
--
|
|
|
|
-- Note that for account match we try both original and effective account
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesPosting :: Query -> Posting -> Bool
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesPosting (Not q) p = not $ q `matchesPosting` p
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesPosting (Any) _ = True
|
|
|
|
matchesPosting (None) _ = False
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesPosting (Or qs) p = any (`matchesPosting` p) qs
|
|
|
|
matchesPosting (And qs) p = all (`matchesPosting` p) qs
|
lib: textification: descriptions & codes
Slightly higher (with small files) and lower (with large files) maximum
residency, and slightly quicker for all.
hledger -f data/100x100x10.journal stats
<<ghc: 42858472 bytes, 84 GCs, 193712/269608 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.015 elapsed), 0.016 MUT (0.042 elapsed), 0.011 GC (0.119 elapsed) :ghc>>
<<ghc: 42891776 bytes, 84 GCs, 190816/260920 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.004 elapsed), 0.017 MUT (0.025 elapsed), 0.010 GC (0.015 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 349575240 bytes, 681 GCs, 1396425/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.137 MUT (0.146 elapsed), 0.050 GC (0.057 elapsed) :ghc>>
<<ghc: 349927568 bytes, 681 GCs, 1397825/4097248 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.133 elapsed), 0.050 GC (0.057 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3424029496 bytes, 6658 GCs, 11403141/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.278 MUT (1.310 elapsed), 0.493 GC (0.546 elapsed) :ghc>>
<<ghc: 3427418064 bytes, 6665 GCs, 11127869/37790168 avg/max bytes residency (11 samples), 109M in use, 0.000 INIT (0.001 elapsed), 1.212 MUT (1.229 elapsed), 0.466 GC (0.519 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 34306546248 bytes, 66727 GCs, 77030638/414617944 avg/max bytes residency (14 samples), 1012M in use, 0.000 INIT (0.000 elapsed), 12.965 MUT (13.164 elapsed), 4.771 GC (5.447 elapsed) :ghc>>
<<ghc: 34340246056 bytes, 66779 GCs, 76983178/416011480 avg/max bytes residency (14 samples), 1011M in use, 0.000 INIT (0.008 elapsed), 12.666 MUT (12.836 elapsed), 4.595 GC (5.175 elapsed) :ghc>>
2016-05-25 04:51:52 +03:00
|
|
|
matchesPosting (Code r) p = regexMatchesCI r $ maybe "" (T.unpack . tcode) $ ptransaction p
|
|
|
|
matchesPosting (Desc r) p = regexMatchesCI r $ maybe "" (T.unpack . tdescription) $ ptransaction p
|
2017-01-13 19:02:11 +03:00
|
|
|
matchesPosting (Acct r) p = matchesPosting p || matchesPosting (originalPosting p)
|
|
|
|
where matchesPosting p = regexMatchesCI r $ T.unpack $ paccount p -- XXX pack
|
2012-12-06 05:10:15 +04:00
|
|
|
matchesPosting (Date span) p = span `spanContainsDate` postingDate p
|
2012-12-06 08:43:41 +04:00
|
|
|
matchesPosting (Date2 span) p = span `spanContainsDate` postingDate2 p
|
2017-06-16 02:52:58 +03:00
|
|
|
matchesPosting (StatusQ s) p = postingStatus p == s
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesPosting (Real v) p = v == isReal p
|
2014-04-06 06:33:44 +04:00
|
|
|
matchesPosting q@(Depth _) Posting{paccount=a} = q `matchesAccount` a
|
|
|
|
matchesPosting q@(Amt _ _) Posting{pamount=amt} = q `matchesMixedAmount` amt
|
|
|
|
-- matchesPosting q@(Amt _ _) Posting{pamount=amt} = q `matchesMixedAmount` amt
|
2012-05-27 22:14:20 +04:00
|
|
|
-- matchesPosting (Empty v) Posting{pamount=a} = v == isZeroMixedAmount a
|
|
|
|
-- matchesPosting (Empty False) Posting{pamount=a} = True
|
|
|
|
-- matchesPosting (Empty True) Posting{pamount=a} = isZeroMixedAmount a
|
|
|
|
matchesPosting (Empty _) _ = True
|
lib: textification: commodity symbols
hledger -f data/100x100x10.journal stats
<<ghc: 39288536 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.028 elapsed), 0.009 GC (0.012 elapsed) :ghc>>
<<ghc: 39290808 bytes, 77 GCs, 196608/269560 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.003 elapsed), 0.015 MUT (0.021 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
<<ghc: 314268960 bytes, 612 GCs, 2143219/6826152 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.009 elapsed), 0.135 MUT (0.151 elapsed), 0.065 GC (0.178 elapsed) :ghc>>
<<ghc: 314254512 bytes, 612 GCs, 2072377/6628024 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.000 elapsed), 0.130 MUT (0.134 elapsed), 0.064 GC (0.075 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
<<ghc: 3070016592 bytes, 5965 GCs, 13138220/64266016 avg/max bytes residency (10 samples), 128M in use, 0.000 INIT (0.000 elapsed), 1.272 MUT (1.322 elapsed), 0.527 GC (0.595 elapsed) :ghc>>
<<ghc: 3069989896 bytes, 5973 GCs, 12687877/62848920 avg/max bytes residency (10 samples), 124M in use, 0.000 INIT (0.002 elapsed), 1.295 MUT (1.324 elapsed), 0.511 GC (0.570 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
<<ghc: 30753448072 bytes, 59763 GCs, 121502982/673169248 avg/max bytes residency (14 samples), 1640M in use, 0.000 INIT (0.007 elapsed), 12.421 MUT (12.672 elapsed), 6.240 GC (7.812 elapsed) :ghc>>
<<ghc: 30753350528 bytes, 59811 GCs, 117616668/666703600 avg/max bytes residency (14 samples), 1588M in use, 0.001 INIT (0.011 elapsed), 13.209 MUT (13.683 elapsed), 6.137 GC (7.117 elapsed) :ghc>>
2016-05-24 05:13:43 +03:00
|
|
|
matchesPosting (Sym r) Posting{pamount=Mixed as} = any (regexMatchesCI $ "^" ++ r ++ "$") $ map (T.unpack . acommodity) as
|
2017-08-31 00:21:01 +03:00
|
|
|
matchesPosting (Tag n v) p = case (n, v) of
|
|
|
|
("payee", Just v) -> maybe False (regexMatchesCI v . T.unpack . transactionPayee) $ ptransaction p
|
|
|
|
("note", Just v) -> maybe False (regexMatchesCI v . T.unpack . transactionNote) $ ptransaction p
|
|
|
|
(n, v) -> matchesTags n v $ postingAllTags p
|
2011-06-05 22:36:32 +04:00
|
|
|
|
2012-05-17 18:59:38 +04:00
|
|
|
tests_matchesPosting = [
|
|
|
|
"matchesPosting" ~: do
|
|
|
|
-- matching posting status..
|
2015-05-16 21:51:35 +03:00
|
|
|
assertBool "positive match on cleared posting status" $
|
2017-06-16 02:52:58 +03:00
|
|
|
(StatusQ Cleared) `matchesPosting` nullposting{pstatus=Cleared}
|
2015-05-16 21:51:35 +03:00
|
|
|
assertBool "negative match on cleared posting status" $
|
2017-06-16 02:52:58 +03:00
|
|
|
not $ (Not $ StatusQ Cleared) `matchesPosting` nullposting{pstatus=Cleared}
|
2017-06-16 02:25:37 +03:00
|
|
|
assertBool "positive match on unmarked posting status" $
|
2017-06-16 02:52:58 +03:00
|
|
|
(StatusQ Unmarked) `matchesPosting` nullposting{pstatus=Unmarked}
|
2017-06-16 02:25:37 +03:00
|
|
|
assertBool "negative match on unmarked posting status" $
|
2017-06-16 02:52:58 +03:00
|
|
|
not $ (Not $ StatusQ Unmarked) `matchesPosting` nullposting{pstatus=Unmarked}
|
2012-05-17 18:59:38 +04:00
|
|
|
assertBool "positive match on true posting status acquired from transaction" $
|
2017-06-16 02:52:58 +03:00
|
|
|
(StatusQ Cleared) `matchesPosting` nullposting{pstatus=Unmarked,ptransaction=Just nulltransaction{tstatus=Cleared}}
|
2012-05-17 18:59:38 +04:00
|
|
|
assertBool "real:1 on real posting" $ (Real True) `matchesPosting` nullposting{ptype=RegularPosting}
|
|
|
|
assertBool "real:1 on virtual posting fails" $ not $ (Real True) `matchesPosting` nullposting{ptype=VirtualPosting}
|
|
|
|
assertBool "real:1 on balanced virtual posting fails" $ not $ (Real True) `matchesPosting` nullposting{ptype=BalancedVirtualPosting}
|
2013-09-10 02:26:45 +04:00
|
|
|
assertBool "a" $ (Acct "'b") `matchesPosting` nullposting{paccount="'b"}
|
|
|
|
assertBool "b" $ not $ (Tag "a" (Just "r$")) `matchesPosting` nullposting
|
|
|
|
assertBool "c" $ (Tag "foo" Nothing) `matchesPosting` nullposting{ptags=[("foo","")]}
|
|
|
|
assertBool "d" $ (Tag "foo" Nothing) `matchesPosting` nullposting{ptags=[("foo","baz")]}
|
|
|
|
assertBool "e" $ (Tag "foo" (Just "a")) `matchesPosting` nullposting{ptags=[("foo","bar")]}
|
|
|
|
assertBool "f" $ not $ (Tag "foo" (Just "a$")) `matchesPosting` nullposting{ptags=[("foo","bar")]}
|
|
|
|
assertBool "g" $ not $ (Tag " foo " (Just "a")) `matchesPosting` nullposting{ptags=[("foo","bar")]}
|
|
|
|
assertBool "h" $ not $ (Tag "foo foo" (Just " ar ba ")) `matchesPosting` nullposting{ptags=[("foo foo","bar bar")]}
|
2012-05-28 04:27:55 +04:00
|
|
|
-- a tag match on a posting also sees inherited tags
|
2013-09-10 02:26:45 +04:00
|
|
|
assertBool "i" $ (Tag "txntag" Nothing) `matchesPosting` nullposting{ptransaction=Just nulltransaction{ttags=[("txntag","")]}}
|
|
|
|
assertBool "j" $ not $ (Sym "$") `matchesPosting` nullposting{pamount=Mixed [usd 1]} -- becomes "^$$", ie testing for null symbol
|
|
|
|
assertBool "k" $ (Sym "\\$") `matchesPosting` nullposting{pamount=Mixed [usd 1]} -- have to quote $ for regexpr
|
|
|
|
assertBool "l" $ (Sym "shekels") `matchesPosting` nullposting{pamount=Mixed [amount{acommodity="shekels"}]}
|
|
|
|
assertBool "m" $ not $ (Sym "shek") `matchesPosting` nullposting{pamount=Mixed [amount{acommodity="shekels"}]}
|
2012-05-17 18:59:38 +04:00
|
|
|
]
|
|
|
|
|
2011-06-11 20:00:00 +04:00
|
|
|
-- | Does the match expression match this transaction ?
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesTransaction :: Query -> Transaction -> Bool
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesTransaction (Not q) t = not $ q `matchesTransaction` t
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesTransaction (Any) _ = True
|
|
|
|
matchesTransaction (None) _ = False
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesTransaction (Or qs) t = any (`matchesTransaction` t) qs
|
|
|
|
matchesTransaction (And qs) t = all (`matchesTransaction` t) qs
|
lib: textification: descriptions & codes
Slightly higher (with small files) and lower (with large files) maximum
residency, and slightly quicker for all.
hledger -f data/100x100x10.journal stats
<<ghc: 42858472 bytes, 84 GCs, 193712/269608 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.015 elapsed), 0.016 MUT (0.042 elapsed), 0.011 GC (0.119 elapsed) :ghc>>
<<ghc: 42891776 bytes, 84 GCs, 190816/260920 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.004 elapsed), 0.017 MUT (0.025 elapsed), 0.010 GC (0.015 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 349575240 bytes, 681 GCs, 1396425/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.137 MUT (0.146 elapsed), 0.050 GC (0.057 elapsed) :ghc>>
<<ghc: 349927568 bytes, 681 GCs, 1397825/4097248 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.133 elapsed), 0.050 GC (0.057 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3424029496 bytes, 6658 GCs, 11403141/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.278 MUT (1.310 elapsed), 0.493 GC (0.546 elapsed) :ghc>>
<<ghc: 3427418064 bytes, 6665 GCs, 11127869/37790168 avg/max bytes residency (11 samples), 109M in use, 0.000 INIT (0.001 elapsed), 1.212 MUT (1.229 elapsed), 0.466 GC (0.519 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 34306546248 bytes, 66727 GCs, 77030638/414617944 avg/max bytes residency (14 samples), 1012M in use, 0.000 INIT (0.000 elapsed), 12.965 MUT (13.164 elapsed), 4.771 GC (5.447 elapsed) :ghc>>
<<ghc: 34340246056 bytes, 66779 GCs, 76983178/416011480 avg/max bytes residency (14 samples), 1011M in use, 0.000 INIT (0.008 elapsed), 12.666 MUT (12.836 elapsed), 4.595 GC (5.175 elapsed) :ghc>>
2016-05-25 04:51:52 +03:00
|
|
|
matchesTransaction (Code r) t = regexMatchesCI r $ T.unpack $ tcode t
|
|
|
|
matchesTransaction (Desc r) t = regexMatchesCI r $ T.unpack $ tdescription t
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesTransaction q@(Acct _) t = any (q `matchesPosting`) $ tpostings t
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesTransaction (Date span) t = spanContainsDate span $ tdate t
|
2012-12-06 08:43:41 +04:00
|
|
|
matchesTransaction (Date2 span) t = spanContainsDate span $ transactionDate2 t
|
2017-06-16 02:52:58 +03:00
|
|
|
matchesTransaction (StatusQ s) t = tstatus t == s
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesTransaction (Real v) t = v == hasRealPostings t
|
2013-03-20 20:36:00 +04:00
|
|
|
matchesTransaction q@(Amt _ _) t = any (q `matchesPosting`) $ tpostings t
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesTransaction (Empty _) _ = True
|
|
|
|
matchesTransaction (Depth d) t = any (Depth d `matchesPosting`) $ tpostings t
|
2013-09-10 02:26:45 +04:00
|
|
|
matchesTransaction q@(Sym _) t = any (q `matchesPosting`) $ tpostings t
|
2017-08-31 00:21:01 +03:00
|
|
|
matchesTransaction (Tag n v) t = case (n, v) of
|
|
|
|
("payee", Just v) -> regexMatchesCI v . T.unpack . transactionPayee $ t
|
|
|
|
("note", Just v) -> regexMatchesCI v . T.unpack . transactionNote $ t
|
|
|
|
(n, v) -> matchesTags n v $ transactionAllTags t
|
2012-05-27 22:14:20 +04:00
|
|
|
|
|
|
|
tests_matchesTransaction = [
|
|
|
|
"matchesTransaction" ~: do
|
|
|
|
let q `matches` t = assertBool "" $ q `matchesTransaction` t
|
|
|
|
Any `matches` nulltransaction
|
|
|
|
assertBool "" $ not $ (Desc "x x") `matchesTransaction` nulltransaction{tdescription="x"}
|
|
|
|
assertBool "" $ (Desc "x x") `matchesTransaction` nulltransaction{tdescription="x x"}
|
2012-05-28 04:27:55 +04:00
|
|
|
-- see posting for more tag tests
|
|
|
|
assertBool "" $ (Tag "foo" (Just "a")) `matchesTransaction` nulltransaction{ttags=[("foo","bar")]}
|
2017-08-31 00:21:01 +03:00
|
|
|
assertBool "" $ (Tag "payee" (Just "payee")) `matchesTransaction` nulltransaction{tdescription="payee|note"}
|
|
|
|
assertBool "" $ (Tag "note" (Just "note")) `matchesTransaction` nulltransaction{tdescription="payee|note"}
|
2014-03-02 05:42:13 +04:00
|
|
|
-- a tag match on a transaction also matches posting tags
|
|
|
|
assertBool "" $ (Tag "postingtag" Nothing) `matchesTransaction` nulltransaction{tpostings=[nullposting{ptags=[("postingtag","")]}]}
|
2012-05-27 22:14:20 +04:00
|
|
|
]
|
2011-06-05 22:36:32 +04:00
|
|
|
|
2015-05-22 02:24:20 +03:00
|
|
|
-- | Filter a list of tags by matching against their names and
|
|
|
|
-- optionally also their values.
|
2017-08-31 00:21:01 +03:00
|
|
|
matchesTags :: Regexp -> Maybe Regexp -> [Tag] -> Bool
|
|
|
|
matchesTags namepat valuepat = not . null . filter (match namepat valuepat)
|
2015-05-22 02:24:20 +03:00
|
|
|
where
|
lib: textification: comments and tags
No change.
hledger -f data/100x100x10.journal stats
<<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.020 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
<<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.018 elapsed), 0.009 GC (0.013 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 349576344 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.124 MUT (0.130 elapsed), 0.047 GC (0.055 elapsed) :ghc>>
<<ghc: 349576280 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.132 elapsed), 0.049 GC (0.058 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3424030664 bytes, 6658 GCs, 11403359/41071624 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.207 MUT (1.228 elapsed), 0.473 GC (0.528 elapsed) :ghc>>
<<ghc: 3424030760 bytes, 6658 GCs, 11403874/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.002 elapsed), 1.234 MUT (1.256 elapsed), 0.470 GC (0.520 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 34306547448 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.003 elapsed), 12.615 MUT (12.813 elapsed), 4.656 GC (5.291 elapsed) :ghc>>
<<ghc: 34306547320 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.009 elapsed), 12.802 MUT (13.065 elapsed), 4.774 GC (5.441 elapsed) :ghc>>
2016-05-25 03:09:20 +03:00
|
|
|
match npat Nothing (n,_) = regexMatchesCI npat (T.unpack n) -- XXX
|
|
|
|
match npat (Just vpat) (n,v) = regexMatchesCI npat (T.unpack n) && regexMatchesCI vpat (T.unpack v)
|
2012-05-28 04:27:55 +04:00
|
|
|
|
2012-05-17 18:59:38 +04:00
|
|
|
-- tests
|
2011-06-29 03:18:36 +04:00
|
|
|
|
2012-05-16 11:57:10 +04:00
|
|
|
tests_Hledger_Query :: Test
|
|
|
|
tests_Hledger_Query = TestList $
|
2012-05-27 22:14:20 +04:00
|
|
|
tests_simplifyQuery
|
|
|
|
++ tests_words''
|
|
|
|
++ tests_filterQuery
|
2012-05-17 18:59:38 +04:00
|
|
|
++ tests_parseQueryTerm
|
2013-09-10 04:41:29 +04:00
|
|
|
++ tests_parseAmountQueryTerm
|
2012-05-17 18:59:38 +04:00
|
|
|
++ tests_parseQuery
|
|
|
|
++ tests_matchesAccount
|
|
|
|
++ tests_matchesPosting
|
2012-05-27 22:14:20 +04:00
|
|
|
++ tests_matchesTransaction
|
2011-09-21 04:28:32 +04:00
|
|
|
|