import type { Vault } from "./vault.js"; /** * The `FROM` clause of a parsed DQL query. `"all"` means scan every note; * `"folder"` restricts to a vault-relative subtree; `"tag"` restricts to * notes carrying the given tag (frontmatter or inline). */ export type Source = { type: "all"; } | { type: "folder"; path: string; } | { type: "tag"; tag: string; }; /** Supported predicate operators in a DQL `WHERE` clause. `like` uses * SQL-LIKE-style `*` wildcards (case-insensitive). */ export type Op = "=" | "!=" | "contains" | "like"; /** A single `WHERE` predicate (`field op value`). */ export interface Predicate { /** Field path — either a `file.*` virtual field or a frontmatter key. */ field: string; /** Operator. See {@link Op}. */ op: Op; /** RHS literal. Coerced at parse time: bare numbers → number, `true`/`false` * → boolean, `null` → null, quoted strings → string, bare ident → string. */ value: string | number | boolean | null; } /** A WHERE clause is a disjunction of conjunctions: (A AND B) OR (C AND D). */ export type WhereGroups = Predicate[][]; /** * Fully parsed Dataview-Query-Language (DQL) query, ready for * {@link runDql}. */ export interface DataviewQuery { /** Result shape: `LIST` returns one row per note; `TABLE` returns named columns. */ kind: "LIST" | "TABLE"; /** Column expressions (TABLE only). Empty for LIST. */ columns: string[]; /** Source restriction — see {@link Source}. */ source: Source; /** Disjunction-of-conjunctions WHERE clause. Empty array means no filter. */ where: WhereGroups; /** Optional sort spec. Default is unsorted (source order). */ sort?: { field: string; dir: "ASC" | "DESC"; }; /** Optional row cap. Falls back to {@link DEFAULT_DQL_ROW_LIMIT} when undefined. */ limit?: number; } /** Thrown by {@link parseDql} when input is not a valid DQL query. * Error message describes the failure point. */ export declare class DqlParseError extends Error { } /** * Parse a Dataview-Query-Language string into a {@link DataviewQuery}. * Supports `LIST` / `TABLE` queries with `FROM`, `WHERE`, `SORT`, `LIMIT` * clauses. WHERE supports `AND` / `OR` and the {@link Op} operators. * * Quoted strings are recognized via a simple quote-aware tokenizer so * folder names with spaces or operator keywords survive. * * @param input - Raw DQL text. Whitespace-only input throws. * @returns Parsed query ready for {@link runDql}. * @throws {DqlParseError} On syntax errors (with a message pointing at the failing clause). * @example * ```ts * const q = parseDql('TABLE file.name FROM "Inbox" WHERE status = "open" SORT mtime DESC LIMIT 10'); * q.kind; // "TABLE" * q.columns; // ["file.name"] * q.source; // { type: "folder", path: "Inbox" } * ``` */ /** * Max DQL query length (chars). v3.10.0-rc.57 (DQL-PARSE-QUADRATIC-DOS) — the * `obsidian_dataview_query` tool is always-registered and read-only (no CLI gate), * so an unbounded query string fed to the char-by-char clause tokenizer is a * main-event-loop CPU-DoS reachable by any serve-http client. This boundary cap * (mirroring {@link MAX_LIKE_PATTERN_LEN} / `MAX_QUESTION_PATTERN_LEN`) bounds the * input so even a superlinear parser stage stays trivial; the tokenizer is also * linearized (no whole-tail `slice`/`toUpperCase` per position). */ export declare const MAX_DQL_QUERY_LEN = 4096; export declare function parseDql(input: string): DataviewQuery; /** * Default row cap for {@link runDql} when the query has no `LIMIT` clause. * Prevents accidental "TABLE FROM" returning thousands of rows on a * large vault. */ export declare const DEFAULT_DQL_ROW_LIMIT = 1000; /** * Execute a parsed DQL query against a vault. Iterates the source * restriction, evaluates the WHERE filter per note, sorts (if requested), * and applies the row cap. Each result row carries `file.path`, * `file.name`, `file.mtime` plus any TABLE-requested columns. * * @param vault - The vault to query. * @param query - Parsed query (from {@link parseDql}). * @param opts - Optional overrides; `defaultLimit` replaces * {@link DEFAULT_DQL_ROW_LIMIT} when the query has no `LIMIT`. * @returns Result rows in sort order (or source order when unsorted), * truncated to the limit. * @example * ```ts * const q = parseDql('LIST FROM "Inbox" WHERE status = "open"'); * const rows = await runDql(vault, q); * for (const row of rows) console.log(row["file.path"]); * ``` */ export declare function runDql(vault: Vault, query: DataviewQuery, opts?: { defaultLimit?: number; }): Promise>>; /** * Defensive cap on a DQL `like` pattern length (v3.9.0-rc.9 audit). Bounds the * tokenize + match work on an absurdly long user-supplied LIKE value from a * `.base` / DQL query. (Matching is already O(tokens × value.length) — see * {@link compileLike} — so this is a cheap secondary bound, not the ReDoS guard.) * * v3.10.0-rc.71 (post-rc.66 re-sweep, ReDoS class): the ReDoS guard is structural — * {@link compileLike} matches via a NON-backtracking DP ({@link matchWildcardTokens}), * NOT a `RegExp`. The pre-rc.71 {@link compileLike} compiled `*`→`.*` and (rc.63) * collapsed only ADJACENT `*` runs; a LIKE value with wildcards SEPARATED BY LITERALS * (`*a*a*…` → `^.*a.*a…$`) was still catastrophic (measured 110 s for `*a`×14), since * the catastrophe scales with the SUBJECT length and so cannot be bounded by any * wildcard count cap. The linear matcher removes the backtracking engine entirely. */ export declare const MAX_LIKE_PATTERN_LEN = 512; /** * Compile a DQL `like` pattern into a NON-backtracking matcher (case-insensitive, * consistent with the file's other string ops which `.toLowerCase()`). `*` is the * only wildcard (any run of chars); see {@link compileLikeTokens} for the escape * rules (`\*` literal asterisk, etc.). The returned object exposes `.test(value)` * so call sites read like the old `likeToRegex(...).test(...)`. * * @internal exported for unit tests; not part of the package `exports` map. */ export declare function compileLike(pattern: string): { test(value: string): boolean; }; //# sourceMappingURL=dql.d.ts.map