-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtranslate_wildcard_expr.rs
More file actions
88 lines (78 loc) · 2.77 KB
/
translate_wildcard_expr.rs
File metadata and controls
88 lines (78 loc) · 2.77 KB
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
use crate::common::lazy::DB_SCHEMA;
use crate::common::logger::warning;
use crate::core::connection::DBConn;
use crate::ts_generator::errors::TsGeneratorError;
use crate::ts_generator::sql_parser::quoted_strings::DisplayObjectName;
use crate::ts_generator::types::ts_query::TsFieldType;
use crate::ts_generator::types::ts_query::TsQuery;
use color_eyre::eyre::Result;
use sqlparser::ast::{Join, Select, TableFactor};
pub fn get_all_table_names_from_select(select: &Select) -> Result<Vec<String>, TsGeneratorError> {
let table_with_joins = select
.from
.first()
.ok_or(TsGeneratorError::WildcardStatementWithoutTargetTables(
select.to_string(),
))?
.to_owned();
let primary_table_name = match table_with_joins.relation {
TableFactor::Table { name, .. } => {
let name = DisplayObjectName(&name).to_string();
Ok(name)
}
TableFactor::Function { .. } => {
// Wildcard queries with table-valued functions are not supported
// because we cannot query the database schema for function result types
Err(TsGeneratorError::WildcardStatementUnsupportedTableExpr(
select.to_string(),
))
}
_ => Err(TsGeneratorError::WildcardStatementUnsupportedTableExpr(
select.to_string(),
)),
}?;
let tables = &mut vec![primary_table_name];
for join in &table_with_joins.joins {
let Join { relation, .. } = join;
match relation {
TableFactor::Table { name, .. } => {
let name = DisplayObjectName(name).to_string();
tables.push(name);
}
_ => {
return Err(TsGeneratorError::WildcardStatementDeadendExpression(
relation.to_string(),
))
}
}
}
Ok(tables.clone())
}
/// Translates a wildcard expression of a SQL statement
/// @example
/// SELECT * FROM items
///
/// and it appends result into the hashmap for type generation
pub async fn translate_wildcard_expr(
select: &Select,
ts_query: &mut TsQuery,
db_conn: &DBConn,
) -> Result<(), TsGeneratorError> {
let table_with_joins = get_all_table_names_from_select(select)?;
if table_with_joins.len() > 1 {
warning!("Impossible to calculate appropriate field names of a wildcard query with multiple tables. Please use explicit field names instead. Query: {}", select.to_string());
}
let table_with_joins = table_with_joins.iter().map(|s| s.as_ref()).collect();
let all_fields = DB_SCHEMA.lock().await.fetch_table(&table_with_joins, db_conn).await;
if let Some(all_fields) = all_fields {
for key in all_fields.keys() {
let field = all_fields.get(key).unwrap();
let mut field_types = vec![field.field_type.clone()];
if field.is_nullable {
field_types.push(TsFieldType::Null);
}
ts_query.result.insert(key.to_owned(), field_types);
}
}
Ok(())
}