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,
|
|
|
|
transactions..) by various criteria, and a parser for query expressions.
|
2011-06-05 22:36:32 +04: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,
|
2012-05-27 22:14:20 +04:00
|
|
|
queryIsDepth,
|
|
|
|
queryIsDate,
|
2012-05-16 11:50:22 +04:00
|
|
|
queryIsStartDateOnly,
|
2012-05-27 22:14:20 +04:00
|
|
|
queryStartDate,
|
|
|
|
queryDateSpan,
|
|
|
|
queryDepth,
|
|
|
|
queryEmpty,
|
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
|
|
|
matchesAccount,
|
2012-05-16 11:50:22 +04:00
|
|
|
matchesPosting,
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesTransaction,
|
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
|
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
|
2011-06-05 22:36:32 +04:00
|
|
|
import Data.Time.Calendar
|
2012-05-28 04:27:55 +04:00
|
|
|
import Safe (readDef, headDef, headMay)
|
2011-06-11 20:00:00 +04:00
|
|
|
import Test.HUnit
|
2011-06-05 22:36:32 +04:00
|
|
|
import Text.ParserCombinators.Parsec
|
|
|
|
|
|
|
|
import Hledger.Utils
|
|
|
|
import Hledger.Data.Types
|
2011-06-14 18:29:31 +04:00
|
|
|
import Hledger.Data.AccountName
|
2013-03-20 20:36:00 +04:00
|
|
|
import Hledger.Data.Amount (nullamt)
|
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
|
2013-03-22 21:59:16 +04:00
|
|
|
| Code String -- ^ match if code matches this regexp
|
2012-05-16 11:12:49 +04:00
|
|
|
| Desc String -- ^ match if description matches this regexp
|
|
|
|
| Acct String -- ^ 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
|
2012-05-16 11:12:49 +04:00
|
|
|
| Status Bool -- ^ match if cleared status has this value
|
|
|
|
| Real Bool -- ^ match if "realness" (involves a real non-virtual account ?) has this value
|
2013-03-20 20:36:00 +04:00
|
|
|
| Amt Ordering Quantity -- ^ match if the amount's numeric quantity is less than/greater than/equal to some value
|
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 ?
|
2012-05-16 11:12:49 +04:00
|
|
|
| Depth Int -- ^ match if account depth is less than or equal to this value
|
2012-05-28 04:27:55 +04:00
|
|
|
| Tag String (Maybe String) -- ^ match if a tag with this exact name, and with value
|
|
|
|
-- matching the regexp if provided, exists
|
2011-06-11 22:35:20 +04:00
|
|
|
deriving (Show, Eq)
|
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
|
2011-06-13 23:46:35 +04:00
|
|
|
deriving (Show, Eq)
|
|
|
|
|
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
|
|
|
|
-- 3. then all terms are AND'd together
|
2012-05-16 11:12:49 +04:00
|
|
|
parseQuery :: Day -> String -> (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
|
|
|
|
(acctpats, otherpats) = partition queryIsAcct pats'
|
|
|
|
q = simplifyQuery $ And $ [Or acctpats, Or descpats] ++ 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.
|
|
|
|
words'' :: [String] -> String -> [String]
|
|
|
|
words'' prefixes = fromparse . parsewith maybeprefixedquotedphrases -- XXX
|
|
|
|
where
|
|
|
|
maybeprefixedquotedphrases = choice' [prefixedQuotedPattern, quotedPattern, pattern] `sepBy` many1 spacenonewline
|
|
|
|
prefixedQuotedPattern = do
|
2011-09-21 04:28:32 +04:00
|
|
|
not' <- fromMaybe "" `fmap` (optionMaybe $ string "not:")
|
|
|
|
let allowednexts | null not' = prefixes
|
|
|
|
| otherwise = prefixes ++ [""]
|
|
|
|
next <- choice' $ map string allowednexts
|
|
|
|
let prefix = not' ++ next
|
2011-06-14 18:27:48 +04:00
|
|
|
p <- quotedPattern
|
2011-09-21 04:28:32 +04:00
|
|
|
return $ prefix ++ stripquotes p
|
2011-06-14 18:27:48 +04:00
|
|
|
quotedPattern = do
|
|
|
|
p <- between (oneOf "'\"") (oneOf "'\"") $ many $ noneOf "'\""
|
|
|
|
return $ stripquotes p
|
2012-05-29 21:02:57 +04:00
|
|
|
pattern = many (noneOf " \n\r")
|
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"
|
|
|
|
prefixes = map (++":") [
|
|
|
|
"inacctonly"
|
|
|
|
,"inacct"
|
2013-03-22 21:59:16 +04:00
|
|
|
,"code"
|
2012-05-29 21:02:18 +04:00
|
|
|
,"desc"
|
|
|
|
,"acct"
|
|
|
|
,"date"
|
|
|
|
,"edate"
|
|
|
|
,"status"
|
|
|
|
,"real"
|
|
|
|
,"empty"
|
|
|
|
,"depth"
|
|
|
|
,"tag"
|
|
|
|
]
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-05-16 11:37:24 +04:00
|
|
|
-- | Parse a single query term as either a query or a query option.
|
2012-05-16 11:12:49 +04:00
|
|
|
parseQueryTerm :: Day -> String -> Either Query QueryOpt
|
2012-05-16 11:37:24 +04:00
|
|
|
parseQueryTerm _ ('i':'n':'a':'c':'c':'t':'o':'n':'l':'y':':':s) = Right $ QueryOptInAcctOnly s
|
|
|
|
parseQueryTerm _ ('i':'n':'a':'c':'c':'t':':':s) = Right $ QueryOptInAcct s
|
|
|
|
parseQueryTerm d ('n':'o':'t':':':s) = case parseQueryTerm d s of
|
2012-05-16 11:12:49 +04:00
|
|
|
Left m -> Left $ Not m
|
|
|
|
Right _ -> Left Any -- not:somequeryoption will be ignored
|
2013-03-22 21:59:16 +04:00
|
|
|
parseQueryTerm _ ('c':'o':'d':'e':':':s) = Left $ Code s
|
2012-05-16 11:12:49 +04:00
|
|
|
parseQueryTerm _ ('d':'e':'s':'c':':':s) = Left $ Desc s
|
|
|
|
parseQueryTerm _ ('a':'c':'c':'t':':':s) = Left $ Acct s
|
2012-05-16 11:37:24 +04:00
|
|
|
parseQueryTerm d ('d':'a':'t':'e':':':s) =
|
2012-05-16 11:12:49 +04:00
|
|
|
case parsePeriodExpr d s of Left _ -> Left None -- XXX should warn
|
|
|
|
Right (_,span) -> Left $ Date span
|
2012-05-16 11:37:24 +04:00
|
|
|
parseQueryTerm d ('e':'d':'a':'t':'e':':':s) =
|
2012-05-16 11:12:49 +04:00
|
|
|
case parsePeriodExpr d s of Left _ -> Left None -- XXX should warn
|
2012-12-06 08:43:41 +04:00
|
|
|
Right (_,span) -> Left $ Date2 span
|
2012-05-16 11:12:49 +04:00
|
|
|
parseQueryTerm _ ('s':'t':'a':'t':'u':'s':':':s) = Left $ Status $ parseStatus s
|
|
|
|
parseQueryTerm _ ('r':'e':'a':'l':':':s) = Left $ Real $ parseBool s
|
2013-03-20 20:36:00 +04:00
|
|
|
parseQueryTerm _ ('a':'m':'t':':':s) = Left $ Amt op q where (op, q) = parseAmountTest s
|
2012-05-16 11:12:49 +04:00
|
|
|
parseQueryTerm _ ('e':'m':'p':'t':'y':':':s) = Left $ Empty $ parseBool s
|
|
|
|
parseQueryTerm _ ('d':'e':'p':'t':'h':':':s) = Left $ Depth $ readDef 0 s
|
2012-05-28 04:27:55 +04:00
|
|
|
parseQueryTerm _ ('t':'a':'g':':':s) = Left $ Tag n v where (n,v) = parseTag s
|
2012-05-16 11:12:49 +04:00
|
|
|
parseQueryTerm _ "" = Left $ Any
|
2012-05-16 11:37:24 +04: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")
|
|
|
|
"status:1" `gives` (Left $ Status True)
|
|
|
|
"status:0" `gives` (Left $ Status False)
|
|
|
|
"status:" `gives` (Left $ Status False)
|
|
|
|
"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)
|
|
|
|
"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
|
|
|
]
|
|
|
|
|
2013-03-20 20:36:00 +04:00
|
|
|
-- can fail
|
|
|
|
parseAmountTest :: String -> (Ordering, Quantity)
|
|
|
|
parseAmountTest s =
|
|
|
|
case s of
|
|
|
|
"" -> err
|
|
|
|
'<':s' -> (LT, readDef err s')
|
|
|
|
'=':s' -> (EQ, readDef err s')
|
|
|
|
'>':s' -> (GT, readDef err s')
|
2013-09-09 22:57:25 +04:00
|
|
|
s' -> (EQ, readDef err s')
|
|
|
|
where
|
|
|
|
err = error' $ "could not parse as '=', '<', or '>' (optional) followed by a numeric quantity: " ++ s
|
2013-03-20 20:36:00 +04:00
|
|
|
|
|
|
|
tests_parseAmountTest = [
|
|
|
|
"parseAmountTest" ~: do
|
|
|
|
let s `gives` r = parseAmountTest s `is` r
|
|
|
|
"<0" `gives` (LT,0)
|
|
|
|
"=0.23" `gives` (EQ,0.23)
|
2013-09-09 22:57:25 +04:00
|
|
|
"0.23" `gives` (EQ,0.23)
|
2013-03-20 20:36:00 +04:00
|
|
|
">10000.10" `gives` (GT,10000.1)
|
|
|
|
]
|
|
|
|
|
2012-05-28 04:27:55 +04:00
|
|
|
parseTag :: String -> (String, Maybe String)
|
|
|
|
parseTag s | '=' `elem` s = (n, Just $ tail v)
|
|
|
|
| otherwise = (s, Nothing)
|
|
|
|
where (n,v) = break (=='=') s
|
|
|
|
|
2012-05-16 11:37:24 +04:00
|
|
|
-- | Parse the boolean value part of a "status:" query, allowing "*" as
|
2011-06-29 04:31:37 +04:00
|
|
|
-- another way to spell True, similar to the journal file format.
|
2011-06-29 03:18:36 +04:00
|
|
|
parseStatus :: String -> Bool
|
|
|
|
parseStatus s = s `elem` (truestrings ++ ["*"])
|
2011-06-13 23:46:35 +04:00
|
|
|
|
2012-05-16 11:37:24 +04:00
|
|
|
-- | Parse the boolean value part of a "status:" query. A true value can
|
2011-06-29 04:31:37 +04:00
|
|
|
-- be spelled as "1", "t" or "true".
|
2011-06-29 03:18:36 +04:00
|
|
|
parseBool :: String -> Bool
|
|
|
|
parseBool s = s `elem` truestrings
|
|
|
|
|
|
|
|
truestrings :: [String]
|
2011-06-29 04:31:37 +04:00
|
|
|
truestrings = ["1","t","true"]
|
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
|
|
|
|
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
|
|
|
|
-- predicate from this query. XXX Semantics not yet clear.
|
|
|
|
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
|
2012-06-30 02:36:30 +04:00
|
|
|
(And [And [Status True,Depth 1]], not . queryIsDepth) `gives` Status True
|
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
|
|
|
|
|
|
|
|
queryIsDesc :: Query -> Bool
|
|
|
|
queryIsDesc (Desc _) = True
|
|
|
|
queryIsDesc _ = False
|
|
|
|
|
|
|
|
queryIsAcct :: Query -> Bool
|
|
|
|
queryIsAcct (Acct _) = True
|
|
|
|
queryIsAcct _ = 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
|
|
|
|
|
|
|
|
queryTermDateSpan (Date span) = Just span
|
|
|
|
queryTermDateSpan _ = Nothing
|
|
|
|
|
2012-12-06 08:43:41 +04:00
|
|
|
-- | What date span (or secondary date span) does this query specify ?
|
2012-05-27 22:14:20 +04:00
|
|
|
-- For OR expressions, use the widest possible span. NOT is ignored.
|
|
|
|
queryDateSpan :: Bool -> Query -> DateSpan
|
2012-12-06 08:43:41 +04:00
|
|
|
queryDateSpan secondary q = spansUnion $ queryDateSpans secondary q
|
2012-05-27 22:14:20 +04:00
|
|
|
|
2012-12-06 08:43:41 +04:00
|
|
|
-- | Extract all date (or secondary date) spans specified in this query.
|
2012-05-27 22:14:20 +04:00
|
|
|
-- NOT is ignored.
|
|
|
|
queryDateSpans :: Bool -> Query -> [DateSpan]
|
2012-12-06 08:43:41 +04:00
|
|
|
queryDateSpans secondary (Or qs) = concatMap (queryDateSpans secondary) qs
|
|
|
|
queryDateSpans secondary (And qs) = concatMap (queryDateSpans secondary) qs
|
2012-05-27 22:14:20 +04:00
|
|
|
queryDateSpans False (Date span) = [span]
|
2012-12-06 08:43:41 +04:00
|
|
|
queryDateSpans True (Date2 span) = [span]
|
2012-05-27 22:14:20 +04:00
|
|
|
queryDateSpans _ _ = []
|
|
|
|
|
2012-05-16 12:28:02 +04:00
|
|
|
-- | What is the earliest of these dates, where Nothing is earliest ?
|
|
|
|
earliestMaybeDate :: [Maybe Day] -> Maybe Day
|
|
|
|
earliestMaybeDate = headDef Nothing . sortBy compareMaybeDates
|
|
|
|
|
|
|
|
-- | What is the latest of these dates, where Nothing is earliest ?
|
|
|
|
latestMaybeDate :: [Maybe Day] -> Maybe Day
|
|
|
|
latestMaybeDate = headDef Nothing . sortBy (flip compareMaybeDates)
|
|
|
|
|
|
|
|
-- | 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' _ = []
|
|
|
|
|
|
|
|
-- | The empty (zero amount) status specified by this query, defaulting to false.
|
|
|
|
queryEmpty :: Query -> Bool
|
|
|
|
queryEmpty = headDef False . queryEmpty'
|
|
|
|
where
|
|
|
|
queryEmpty' (Empty v) = [v]
|
|
|
|
queryEmpty' (Or qs) = concatMap queryEmpty' qs
|
|
|
|
queryEmpty' (And qs) = concatMap queryEmpty' qs
|
|
|
|
queryEmpty' _ = []
|
|
|
|
|
|
|
|
-- -- | The "include empty" option specified by this query, defaulting to false.
|
|
|
|
-- emptyQueryOpt :: [QueryOpt] -> Bool
|
|
|
|
-- emptyQueryOpt = headDef False . emptyQueryOpt'
|
|
|
|
-- where
|
|
|
|
-- emptyQueryOpt' [] = False
|
|
|
|
-- emptyQueryOpt' (QueryOptEmpty v:_) = v
|
|
|
|
-- emptyQueryOpt' (_:vs) = emptyQueryOpt' vs
|
|
|
|
|
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
|
|
|
|
inAccountQuery (QueryOptInAcctOnly a:_) = Just $ Acct $ accountNameToAccountOnlyRegex a
|
|
|
|
inAccountQuery (QueryOptInAcct a:_) = Just $ Acct $ accountNameToAccountRegex a
|
|
|
|
|
|
|
|
-- -- | 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
|
|
|
|
matchesAccount (Acct r) a = regexMatchesCI r a
|
|
|
|
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
|
|
|
]
|
|
|
|
|
2011-06-11 20:00:00 +04:00
|
|
|
-- | Does the match expression match this posting ?
|
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
|
2013-03-22 21:59:16 +04:00
|
|
|
matchesPosting (Code r) p = regexMatchesCI r $ maybe "" tcode $ ptransaction p
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesPosting (Desc r) p = regexMatchesCI r $ maybe "" tdescription $ ptransaction p
|
|
|
|
matchesPosting (Acct r) p = regexMatchesCI r $ paccount p
|
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
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesPosting (Status v) p = v == postingCleared p
|
|
|
|
matchesPosting (Real v) p = v == isReal p
|
2012-05-27 22:14:20 +04:00
|
|
|
matchesPosting (Depth d) Posting{paccount=a} = Depth d `matchesAccount` a
|
2013-03-20 20:36:00 +04:00
|
|
|
matchesPosting (Amt op n) Posting{pamount=a} = compareMixedAmount op n a
|
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
|
2012-05-28 04:27:55 +04:00
|
|
|
matchesPosting (Tag n Nothing) p = isJust $ lookupTagByName n $ postingAllTags p
|
|
|
|
matchesPosting (Tag n (Just v)) p = isJust $ lookupTagByNameAndValue (n,v) $ postingAllTags p
|
2012-05-27 22:14:20 +04:00
|
|
|
-- matchesPosting _ _ = False
|
2011-06-05 22:36:32 +04:00
|
|
|
|
2013-03-20 20:36:00 +04:00
|
|
|
-- | Is this simple mixed amount's quantity less than, equal to, or greater than this number ?
|
|
|
|
-- For complext mixed amounts (with multiple commodities), this is always true.
|
|
|
|
compareMixedAmount :: Ordering -> Quantity -> MixedAmount -> Bool
|
|
|
|
compareMixedAmount op q (Mixed []) = compareMixedAmount op q (Mixed [nullamt])
|
|
|
|
-- compareMixedAmount op q (Mixed [a]) = strace (compare (strace $ aquantity a) (strace q)) == op
|
|
|
|
compareMixedAmount op q (Mixed [a]) = compare (aquantity a) q == op
|
|
|
|
compareMixedAmount _ _ _ = True
|
|
|
|
|
2012-05-17 18:59:38 +04:00
|
|
|
tests_matchesPosting = [
|
|
|
|
"matchesPosting" ~: do
|
|
|
|
-- matching posting status..
|
|
|
|
assertBool "positive match on true posting status" $
|
|
|
|
(Status True) `matchesPosting` nullposting{pstatus=True}
|
|
|
|
assertBool "negative match on true posting status" $
|
|
|
|
not $ (Not $ Status True) `matchesPosting` nullposting{pstatus=True}
|
|
|
|
assertBool "positive match on false posting status" $
|
|
|
|
(Status False) `matchesPosting` nullposting{pstatus=False}
|
|
|
|
assertBool "negative match on false posting status" $
|
|
|
|
not $ (Not $ Status False) `matchesPosting` nullposting{pstatus=False}
|
|
|
|
assertBool "positive match on true posting status acquired from transaction" $
|
|
|
|
(Status True) `matchesPosting` nullposting{pstatus=False,ptransaction=Just nulltransaction{tstatus=True}}
|
|
|
|
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}
|
2012-05-27 22:14:20 +04:00
|
|
|
assertBool "" $ (Acct "'b") `matchesPosting` nullposting{paccount="'b"}
|
2012-05-28 04:27:55 +04:00
|
|
|
assertBool "" $ not $ (Tag "a" (Just "r$")) `matchesPosting` nullposting
|
|
|
|
assertBool "" $ (Tag "foo" Nothing) `matchesPosting` nullposting{ptags=[("foo","")]}
|
|
|
|
assertBool "" $ (Tag "foo" Nothing) `matchesPosting` nullposting{ptags=[("foo","baz")]}
|
|
|
|
assertBool "" $ (Tag "foo" (Just "a")) `matchesPosting` nullposting{ptags=[("foo","bar")]}
|
|
|
|
assertBool "" $ not $ (Tag "foo" (Just "a$")) `matchesPosting` nullposting{ptags=[("foo","bar")]}
|
|
|
|
assertBool "" $ not $ (Tag " foo " (Just "a")) `matchesPosting` nullposting{ptags=[("foo","bar")]}
|
|
|
|
assertBool "" $ not $ (Tag "foo foo" (Just " ar ba ")) `matchesPosting` nullposting{ptags=[("foo foo","bar bar")]}
|
|
|
|
-- a tag match on a posting also sees inherited tags
|
|
|
|
assertBool "" $ (Tag "txntag" Nothing) `matchesPosting` nullposting{ptransaction=Just nulltransaction{ttags=[("txntag","")]}}
|
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
|
2013-03-22 21:59:16 +04:00
|
|
|
matchesTransaction (Code r) t = regexMatchesCI r $ tcode t
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesTransaction (Desc r) t = regexMatchesCI r $ 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
|
2012-05-16 11:12:49 +04:00
|
|
|
matchesTransaction (Status v) t = v == tstatus t
|
|
|
|
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
|
2012-05-28 04:27:55 +04:00
|
|
|
matchesTransaction (Tag n Nothing) t = isJust $ lookupTagByName n $ transactionAllTags t
|
|
|
|
matchesTransaction (Tag n (Just v)) t = isJust $ lookupTagByNameAndValue (n,v) $ transactionAllTags t
|
|
|
|
|
2012-05-27 22:14:20 +04:00
|
|
|
-- matchesTransaction _ _ = False
|
|
|
|
|
|
|
|
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")]}
|
|
|
|
-- a tag match on a transaction usually ignores posting tags
|
|
|
|
assertBool "" $ not $ (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
|
|
|
|
2012-05-28 04:27:55 +04:00
|
|
|
lookupTagByName :: String -> [Tag] -> Maybe Tag
|
|
|
|
lookupTagByName namepat tags = headMay [(n,v) | (n,v) <- tags, matchTagName namepat n]
|
|
|
|
|
|
|
|
lookupTagByNameAndValue :: Tag -> [Tag] -> Maybe Tag
|
|
|
|
lookupTagByNameAndValue (namepat, valpat) tags = headMay [(n,v) | (n,v) <- tags, matchTagName namepat n, matchTagValue valpat v]
|
|
|
|
|
|
|
|
matchTagName :: String -> String -> Bool
|
|
|
|
matchTagName pat name = pat == name
|
|
|
|
|
|
|
|
matchTagValue :: String -> String -> Bool
|
|
|
|
matchTagValue pat value = regexMatchesCI pat value
|
|
|
|
|
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-03-20 20:36:00 +04:00
|
|
|
++ tests_parseAmountTest
|
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
|
|
|
|