Skip to main content

oxirs_arq/query/
types.rs

1//! Auto-generated module
2//!
3//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
4
5use crate::algebra::{
6    Aggregate, Algebra, Expression, GroupCondition, Iri, OrderCondition, TriplePattern, Variable,
7};
8use crate::update::UpdateOperation;
9use std::collections::HashMap;
10
11/// SPARQL query types
12#[derive(Debug, Clone, PartialEq)]
13pub enum QueryType {
14    Select,
15    Construct,
16    Ask,
17    Describe,
18}
19/// A single resource named by a `DESCRIBE` query.
20///
21/// `DESCRIBE <iri> ?var …` may mix explicit IRIs (already expanded against the
22/// query prologue) and variables that resolve to bound resources in the WHERE
23/// clause. `DESCRIBE *` is represented out-of-band by [`Query::describe_all`]
24/// and leaves [`Query::describe_targets`] empty.
25#[derive(Debug, Clone, PartialEq)]
26pub enum DescribeTarget {
27    /// A concrete IRI target (prefixed names are expanded before storage).
28    Iri(Iri),
29    /// A variable target whose bindings supply the resources to describe.
30    Variable(Variable),
31}
32/// A single item in a SELECT projection list.
33///
34/// A projection is an ordered list of these. Plain `?x` produces
35/// [`ProjectionItem::Variable`]; a parenthesized `(Expression AS ?v)` produces
36/// [`ProjectionItem::Expression`] for a non-aggregate expression or
37/// [`ProjectionItem::Aggregate`] when the expression is a SPARQL aggregate
38/// (`COUNT`, `SUM`, `MIN`, `MAX`, `AVG`, `SAMPLE`, `GROUP_CONCAT`). The
39/// aggregate variant carries the crate's [`Aggregate`] value directly so a
40/// consumer can assemble an `Algebra::Group { aggregates, .. }` without
41/// re-parsing.
42#[derive(Debug, Clone, PartialEq)]
43pub enum ProjectionItem {
44    /// Plain variable projection: `SELECT ?x`.
45    Variable(Variable),
46    /// Non-aggregate expression projection: `(?a + 1 AS ?b)`.
47    Expression {
48        /// The projected expression.
49        expr: Expression,
50        /// The variable the expression result is bound to.
51        alias: Variable,
52    },
53    /// Aggregate projection: `(COUNT(*) AS ?n)`, `(SUM(?a * ?b) AS ?t)`, ….
54    Aggregate {
55        /// The parsed aggregate (carries DISTINCT / separator / `*` state).
56        aggregate: Aggregate,
57        /// The variable the aggregate result is bound to.
58        alias: Variable,
59    },
60}
61/// Dataset clause for FROM and FROM NAMED
62#[derive(Debug, Clone, Default)]
63pub struct DatasetClause {
64    pub default_graphs: Vec<Iri>,
65    pub named_graphs: Vec<Iri>,
66}
67/// SPARQL UPDATE request representation
68#[derive(Debug, Clone)]
69pub struct UpdateRequest {
70    pub operations: Vec<UpdateOperation>,
71    pub prefixes: HashMap<String, String>,
72    pub base_iri: Option<String>,
73}
74/// Origin of the datatype IRI in a typed RDF literal (`"…"^^dt`).
75///
76/// The lexer must preserve HOW the datatype was written so `^^<urn:x>` (and any
77/// other authority-less scheme such as `tag:`) is honoured as an absolute IRI
78/// rather than mis-resolved as the prefixed name `urn:x`:
79///
80/// * `^^<iri>`         → [`DatatypeRef::Iri`]      — used verbatim as an
81///   absolute IRI, never sent to prefix resolution.
82/// * `^^prefix:local`  → [`DatatypeRef::Prefixed`] — resolved against the
83///   declared prologue prefixes at parse time.
84#[derive(Debug, Clone, PartialEq)]
85pub enum DatatypeRef {
86    /// An absolute IRI written in angle brackets (`^^<…>`), already unwrapped.
87    Iri(String),
88    /// A `prefix:local` (or bare) name (`^^xsd:integer`), resolved via prefixes.
89    Prefixed(String),
90}
91/// Token types for SPARQL parsing
92#[derive(Debug, Clone, PartialEq)]
93pub enum Token {
94    Select,
95    Construct,
96    Ask,
97    Describe,
98    Where,
99    Optional,
100    Union,
101    Minus,
102    Filter,
103    Bind,
104    Service,
105    Graph,
106    From,
107    Named,
108    Prefix,
109    Base,
110    Distinct,
111    Reduced,
112    OrderBy,
113    GroupBy,
114    Having,
115    Limit,
116    Offset,
117    Asc,
118    Desc,
119    As,
120    Values,
121    Exists,
122    NotExists,
123    Insert,
124    Delete,
125    Update,
126    Create,
127    Drop,
128    Clear,
129    Load,
130    Copy,
131    Move,
132    Add,
133    Data,
134    With,
135    Using,
136    Silent,
137    /// The `UNDEF` keyword used in a `VALUES` data block to leave a variable
138    /// unbound for a given row (SPARQL 1.1 §10.2.1 InlineData).
139    Undef,
140    All,
141    Default,
142    To,
143    Equal,
144    NotEqual,
145    Less,
146    LessEqual,
147    Greater,
148    GreaterEqual,
149    And,
150    Or,
151    Not,
152    /// The `IN` operator keyword (`?x IN (…)`, `?x NOT IN (…)`).
153    In,
154    Plus,
155    Minus_,
156    Multiply,
157    Divide,
158    Pipe,
159    Caret,
160    Slash,
161    Question,
162    Star,
163    Bang,
164    LeftParen,
165    RightParen,
166    LeftBrace,
167    RightBrace,
168    LeftBracket,
169    RightBracket,
170    Dot,
171    Semicolon,
172    Comma,
173    Colon,
174    Iri(String),
175    PrefixedName(String, String),
176    /// A SPARQL 1.1 built-in call name (e.g. `LANG`, `isIRI`, `REGEX`),
177    /// carrying its canonical lower-case spelling. Only a *bare* (colon-free)
178    /// keyword is classified as a built-in; a leading-colon default-prefix name
179    /// such as `:lang` stays a [`Token::PrefixedName`] so user functions are
180    /// never mistaken for built-ins.
181    BuiltIn(String),
182    /// The bare `a` keyword: the `rdf:type` predicate shorthand. Only a bare
183    /// (colon-free) lowercase `a` is classified here; `?a`, `:a`, `"a"` and
184    /// `a:` (a prefix name) are never affected.
185    A,
186    Variable(String),
187    StringLiteral(String),
188    /// A string literal carrying a language tag (`"foo"@ja`) or an explicit
189    /// datatype (`"1"^^xsd:integer`, `"5"^^<urn:myint>`). `datatype` carries the
190    /// datatype together with its written origin (see [`DatatypeRef`]) so an
191    /// angle-bracket IRI is never mistaken for a prefixed name.
192    RdfLiteral {
193        value: String,
194        language: Option<String>,
195        datatype: Option<DatatypeRef>,
196    },
197    NumericLiteral(String),
198    BooleanLiteral(bool),
199    BlankNode(String),
200    Eof,
201    Newline,
202}
203/// SPARQL query representation
204#[derive(Debug, Clone)]
205pub struct Query {
206    pub query_type: QueryType,
207    pub select_variables: Vec<Variable>,
208    pub where_clause: Algebra,
209    pub order_by: Vec<OrderCondition>,
210    pub group_by: Vec<GroupCondition>,
211    pub having: Option<Expression>,
212    pub limit: Option<usize>,
213    pub offset: Option<usize>,
214    pub distinct: bool,
215    pub reduced: bool,
216    pub construct_template: Vec<TriplePattern>,
217    pub prefixes: HashMap<String, String>,
218    pub base_iri: Option<String>,
219    pub dataset: DatasetClause,
220    /// Ordered SELECT projection items (parallel to `select_variables`, but
221    /// also carrying `(Expression AS ?v)` / aggregate projections). Empty when
222    /// the query is `SELECT *` or is not a SELECT query.
223    pub projection_items: Vec<ProjectionItem>,
224    /// Explicit `DESCRIBE` targets (IRIs and variables). Empty for non-DESCRIBE
225    /// queries and for `DESCRIBE *` (see `describe_all`).
226    pub describe_targets: Vec<DescribeTarget>,
227    /// `true` for `DESCRIBE *`, which describes every in-scope variable binding
228    /// rather than an explicit target list.
229    pub describe_all: bool,
230}