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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
mod anchor;
mod context;
mod dialect;
mod gen_expr;
mod gen_projection;
mod gen_query;
mod preprocess;
mod std;
pub use dialect::Dialect;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use crate::{ast::rq::Query, PRQL_VERSION};
use self::{context::AnchorContext, dialect::DialectHandler};
pub fn compile(query: Query, options: Option<Options>) -> Result<String> {
let options = options.unwrap_or_default();
let sql_ast = gen_query::translate_query(query, options.dialect)?;
let sql = sql_ast.to_string();
let sql = if options.format {
let formatted = sqlformat::format(
&sql,
&sqlformat::QueryParams::default(),
sqlformat::FormatOptions::default(),
);
formatted.replace("{ {", "{{").replace("} }", "}}")
} else {
sql
};
let sql = if options.signature_comment {
let pre = if options.format { "\n\n" } else { " " };
let post = if options.format { "\n" } else { "" };
let signature = format!(
"{pre}-- Generated by PRQL compiler version {} (https://prql-lang.org){post}",
*PRQL_VERSION
);
sql + &signature
} else {
sql
};
Ok(sql)
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Options {
pub format: bool,
pub dialect: Option<Dialect>,
pub signature_comment: bool,
}
impl Default for Options {
fn default() -> Self {
Self {
format: true,
dialect: None,
signature_comment: true,
}
}
}
impl Options {
pub fn no_format(mut self) -> Self {
self.format = false;
self
}
pub fn no_signature(mut self) -> Self {
self.signature_comment = false;
self
}
pub fn with_dialect(mut self, dialect: Dialect) -> Self {
self.dialect = Some(dialect);
self
}
pub fn some(self) -> Option<Self> {
Some(self)
}
}
struct Context {
pub dialect: Box<dyn DialectHandler>,
pub anchor: AnchorContext,
pub omit_ident_prefix: bool,
pub pre_projection: bool,
}