HORIZON HASKELLDocslts/ghc-10.0.x8c2f8e82026-10-19Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8c2f8e8 · 2026-10-19

Modulehledger-lib-1.52.4GHC2021

Hledger.Query

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

  • 3 types
  • 52 values

Query and QueryOpt

6 declarations
datadata Query
#

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 data queries (in "standard" order, roughly as they appear in a transaction)

  • Date DateSpan

    match primary dates in this date span

  • Date2 DateSpan

    match secondary dates in this date span

  • StatusQ Status

    match this txn/posting status

  • Code Regexp

    match txn codes infix-matched by this regexp

  • Desc Regexp

    match txn descriptions infix-matched by this regexp

  • Tag Regexp (Maybe Regexp)

    match if a tag's name, and optionally its value, is infix-matched by the respective regexps

  • Acct Regexp

    match account names infix-matched by this regexp

  • Type [AccountType]

    match accounts whose type is one of these (or with no types, any account)

  • Depth Int

    match if account depth is less than or equal to this value (or, sometimes used as a display option)

  • DepthAcct Regexp Int

    match if the account matches and account depth is less than or equal to this value (usually used as a display option)

  • Real Bool

    match postings with this "realness" value

  • Amt OrdPlus Quantity

    match if the amount's numeric quantity is less thangreater thanequal to/unsignedly equal to some value

  • Sym Regexp

    match if the commodity symbol is fully-matched by this regexp compound queries (expr:)

  • Not Query

    negate this match

  • And [Query]

    match if all of these match

  • Or [Query]

    match if any of these match compound queries for transactions (any:, all:) If used in a non transaction-matching context, these are equivalent to And.

  • AnyPosting [Query]

    match if any one posting is matched by all of these

  • AllPostings [Query]

    match if all of one or more postings are matched by all of these

Instances3Eq, Show, Default
  • Eq QueryDefined in hledger-lib-1.52.4 · Hledger.Query
  • Show QueryDefined in hledger-lib-1.52.4 · Hledger.Query
  • Default QueryDefined in hledger-lib-1.52.4 · Hledger.Query
datadata QueryOpt
#

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

Constructors

Instances2Eq, Show
  • Eq QueryOptDefined in hledger-lib-1.52.4 · Hledger.Query
  • Show QueryOptDefined in hledger-lib-1.52.4 · Hledger.Query

parsing

5 declarations
valueparseQuery :: Day -> Text -> Either String (Query, [QueryOpt])
#

A version of parseQueryList which acts on a single Text of space-separated terms.

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.

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

Period expressions may contain relative dates, so a reference date is required to fully parse these.

Example1 expression
parseQuery nulldate "expenses:dining out"Right (Or [Acct (RegexpCI "expenses:dining"),Acct (RegexpCI "out")],[])
Example1 expression
parseQuery nulldate "\"expenses:dining out\""Right (Acct (RegexpCI "expenses:dining out"),[])
valueparseQueryList :: Day -> [Text] -> Either String (Query, [QueryOpt])
#

Convert a list of space-separated queries to a single query

Multiple terms are combined as follows: 1. multiple account patterns are OR'd together 2. multiple description patterns are OR'd together 3. multiple status patterns are OR'd together 4. then all terms are AND'd together

modifying

4 declarations
valuefilterQuery :: (Query -> Bool) -> Query -> Query
#

Remove query terms (or whole sub-expressions) from this query which do not match the given predicate. XXX Semantics not completely clear. Also calls simplifyQuery on the result.

valuefilterQueryOrNotQuery :: (Query -> Bool) -> Query -> Query
#

Remove query terms (or whole sub-expressions) from this query which match neither the given predicate nor that predicate negated (eg, if predicate is queryIsAcct, this will keep both "acct:" and "not:acct:" terms). Also calls simplifyQuery on the result. (Since 1.24.1, might be merged into filterQuery in future.) XXX Semantics not completely clear.

predicates

17 declarations
valuequeryIsDate :: Query -> Bool
#

Is this a simple query of this type (date:) ? Does not match a compound query involving andornot. Likewise for the following functions.

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.

accessors

7 declarations
valuequeryStartDate :: Bool -> Query -> Maybe Day
#

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

valuequeryEndDate :: Bool -> Query -> Maybe Day
#

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

valuequeryDateSpan :: Bool -> Query -> DateSpan
#

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.

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.

matching things with queries

15 declarations
valuematchesDescription :: Query -> Text -> Bool
#

Does the query match this transaction description ? Non-desc: query terms are ignored (this might disrupt some boolean queries).

valuematchesPayeeWIP :: Query -> Payee -> Bool
#

Does the query match this transaction payee ? Tests desc: (and payee: ?) terms, any other terms are ignored. XXX Currently an alias for matchDescription. I'm not sure if more is needed, There's some shenanigan with payee: and "payeeTag" to figure out.

valuematchesPosting :: Query -> Posting -> Bool
#

Does the match expression match this posting ? When matching account name, and the posting has been transformed in some way, we will match either the original or transformed name.

Like matchesAccount, but with optional extra matching features:

  • If the account's type is provided, any type: terms in the query must match it (and any negated type: terms must not match it).

  • If the account's tags are provided, any tag: terms must match at least one of them (and any negated tag: terms must match none).

valuematchesTag :: Query -> Tag -> Bool
#

Does the query match the name and optionally the value of this tag ? Non-tag: query terms are ignored (this might disrupt some boolean queries).

valuewords'' :: [Text] -> Text -> [Text]
#

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.

tests

1 declaration