hledger-lib-0.22: Core data types, parsers and utilities for the hledger accounting tool.

Safe Haskell | None

Hledger.Query

Contents

- Query and QueryOpt
- parsing
- accessors
- matching
- tests

Description

A general query system for matching things (accounts, postings, transactions..) by various criteria, and a parser for query expressions.

Synopsis

- data Query
  - = Any
  - | None
  - | Not Query
  - | Or [Query]
  - | And [Query]
  - | Code String
  - | Desc String
  - | Acct String
  - | Date DateSpan
  - | Date2 DateSpan
  - | Status Bool
  - | Real Bool
  - | Amt Ordering Quantity
  - | Sym String
  - | Empty Bool
  - | Depth Int
  - | Tag String (Maybe String)
- data QueryOpt
  - = QueryOptInAcctOnly AccountName
  - | QueryOptInAcct AccountName
- parseQuery :: Day -> String -> (Query, [QueryOpt])
- simplifyQuery :: Query -> Query
- filterQuery :: (Query -> Bool) -> Query -> Query
- queryIsNull :: Query -> Bool
- queryIsDepth :: Query -> Bool
- queryIsDate :: Query -> Bool
- queryIsStartDateOnly :: Bool -> Query -> Bool
- queryStartDate :: Bool -> Query -> Maybe Day
- queryDateSpan :: Bool -> Query -> DateSpan
- queryDepth :: Query -> Int
- queryEmpty :: Query -> Bool
- inAccount :: [QueryOpt] -> Maybe (AccountName, Bool)
- inAccountQuery :: [QueryOpt] -> Maybe Query
- matchesAccount :: Query -> AccountName -> Bool
- matchesPosting :: Query -> Posting -> Bool
- matchesTransaction :: Query -> Transaction -> Bool
- tests_Hledger_Query :: Test

# Query and QueryOpt

data Query Source

A query is a composition of search criteria, which can be used to match postings, transactions, accounts and more.

Constructors

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
Code String | match if code matches this regexp
Desc String | match if description matches this regexp
Acct String | match postings whose account matches this regexp
Date DateSpan | match if primary date in this date span
Date2 DateSpan | match if secondary date in this date span
Status Bool | match if cleared status has this value
Real Bool | match if realness (involves a real non-virtual account ?) has this value
Amt Ordering Quantity | match if the amount's numeric quantity is less thangreater thanequal to some value
Sym String | match if the entire commodity symbol is matched by this regexp
Empty Bool | if true, show zero-amount postings/accounts which are usually not shown more of a query option than a query criteria ?
Depth Int | match if account depth is less than or equal to this value
Tag String (Maybe String) | match if a tag with this exact name, and with value matching the regexp if provided, exists

Instances

Eq Query |
Data Query |
Show Query |
Typeable Query |

data QueryOpt Source

A query option changes a query's/report's behaviour and output in some way.

Constructors

QueryOptInAcctOnly AccountName | show an account register focussed on this account
QueryOptInAcct AccountName | as above but include sub-accounts in the account register | QueryOptCostBasis -- ^ show amounts converted to cost where possible | QueryOptDate2 -- ^ show secondary dates instead of primary dates

Instances

Eq QueryOpt |
Data QueryOpt |
Show QueryOpt |
Typeable QueryOpt |

# parsing

parseQuery :: Day -> String -> (Query, [QueryOpt])Source

Convert a query expression containing zero or more space-separated terms to a query and zero or more query options. A query term is either:

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.

1. a query option, which modifies the reporting behaviour in some way. There is currently one of these, which may appear only once:

inacct:FULLACCTNAME

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.

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

simplifyQuery :: Query -> Query Source

filterQuery :: (Query -> Bool) -> Query -> Query Source

Remove query terms (or whole sub-expressions) not matching the given predicate from this query. XXX Semantics not yet clear.

# accessors

queryIsNull :: Query -> Bool Source

Does this query match everything ?

queryIsDepth :: Query -> Bool Source

queryIsDate :: Query -> Bool Source

queryIsStartDateOnly :: Bool -> Query -> Bool Source

Does this query specify a start date and nothing else (that would filter postings prior to the date) ? When the flag is true, look for a starting secondary date instead.

queryStartDate :: Bool -> Query -> Maybe Day Source

What start date (or secondary date) does this query specify, if any ? For OR expressions, use the earliest of the dates. NOT is ignored.

queryDateSpan :: Bool -> Query -> DateSpan Source

What date span (or secondary date span) does this query specify ? For OR expressions, use the widest possible span. NOT is ignored.

queryDepth :: Query -> Int Source

The depth limit this query specifies, or a large number if none.

queryEmpty :: Query -> Bool Source

The empty (zero amount) status specified by this query, defaulting to false.

inAccount :: [QueryOpt] -> Maybe (AccountName, Bool)Source

The account we are currently focussed on, if any, and whether subaccounts are included. Just looks at the first query option.

inAccountQuery :: [QueryOpt] -> Maybe Query Source

A query for the account(s) we are currently focussed on, if any. Just looks at the first query option.

# matching

matchesAccount :: Query -> AccountName -> Bool Source

Does the match expression match this account ? A matching in: clause is also considered a match.

matchesPosting :: Query -> Posting -> Bool Source

Does the match expression match this posting ?

matchesTransaction :: Query -> Transaction -> Bool Source

Does the match expression match this transaction ?

# tests

tests_Hledger_Query :: Test Source