shapes_converter/shex_to_sparql/
select_query.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::fmt::Display;

use iri_s::IriS;
use prefixmap::PrefixMap;

use crate::shex_to_sparql::TriplePattern;

pub struct SelectQuery {
    prefixmap: Option<PrefixMap>,
    base: Option<IriS>,
    patterns: Vec<TriplePattern>,
}

impl SelectQuery {
    pub fn new() -> SelectQuery {
        SelectQuery {
            prefixmap: None,
            base: None,
            patterns: Vec::new(),
        }
    }

    pub fn with_prefixmap(mut self, prefixmap: Option<PrefixMap>) -> Self {
        self.prefixmap = prefixmap;
        self
    }

    pub fn without_prefixmap(mut self) -> Self {
        self.prefixmap = None;
        self
    }

    pub fn with_base(mut self, base: Option<IriS>) -> Self {
        self.base = base;
        self
    }

    pub fn with_patterns(mut self, patterns: Vec<TriplePattern>) -> Self {
        self.patterns = patterns;
        self
    }
}

impl Display for SelectQuery {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(base) = &self.base {
            writeln!(f, "{}", base)?
        };
        // TODO: Unify these 2 branches in one...it was giving an move error on prefixmap that I wanted to bypass quickly...
        if let Some(prefixmap) = &self.prefixmap {
            writeln!(f, "{}", prefixmap)?;
            writeln!(f, "SELECT * WHERE {{")?;
            for pattern in &self.patterns {
                write!(f, " ")?;
                pattern.show_qualified(f, prefixmap).map_err(|_e|
                        // TODO: The following is a hack to make the type checker happy...
                        std::fmt::Error)?;
                writeln!(f)?;
            }
        } else {
            let prefixmap = PrefixMap::new();
            for pattern in &self.patterns {
                pattern.show_qualified(f, &prefixmap).map_err(|_e|
                        // TODO: The following is a hack to make the type checker happy...
                        std::fmt::Error)?;
            }
        }
        writeln!(f, "}}")?;
        Ok(())
    }
}

impl Default for SelectQuery {
    fn default() -> Self {
        Self::new()
    }
}