2023-01-17 12:28:49 +01:00
|
|
|
use polars_lazy::prelude::*;
|
|
|
|
use sea_query::table::ColumnType;
|
|
|
|
use sea_query::*;
|
|
|
|
|
|
|
|
pub fn print_schema(ldf: LazyFrame) {
|
|
|
|
let schema = ldf.schema().expect("Could not retreive schema");
|
|
|
|
for f in schema.iter_fields() {
|
2023-01-17 13:36:09 +01:00
|
|
|
let mut unnamed_cols_counter = 0;
|
2023-01-17 12:28:49 +01:00
|
|
|
let d = f.data_type().to_string();
|
2023-01-17 13:36:09 +01:00
|
|
|
let n = if f.name.is_empty() {
|
|
|
|
unnamed_cols_counter += 1;
|
|
|
|
format!("Column{}", unnamed_cols_counter)
|
|
|
|
} else {
|
|
|
|
f.name
|
|
|
|
};
|
|
|
|
|
2023-01-17 12:28:49 +01:00
|
|
|
println!("{n} ({d})");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_create(ldf: LazyFrame, table_name: &str, default_strlen: u32) {
|
|
|
|
let schema = ldf.schema().expect("Could not retreive schema");
|
|
|
|
// Create empty table
|
|
|
|
let mut statements = vec![Table::create()
|
|
|
|
.table(Alias::new(table_name))
|
|
|
|
.if_not_exists()
|
|
|
|
.to_string(PostgresQueryBuilder)];
|
|
|
|
|
|
|
|
// Alter table adding fields one by one
|
2023-01-17 13:34:10 +01:00
|
|
|
let mut unnamed_cols_counter = 0;
|
2023-01-17 12:28:49 +01:00
|
|
|
for f in schema.iter_fields() {
|
|
|
|
let dtype = match f.data_type().to_string().as_str() {
|
|
|
|
"i64" => ColumnType::Integer,
|
|
|
|
"f64" => ColumnType::Float,
|
|
|
|
"str" => ColumnType::String(Some(default_strlen)),
|
|
|
|
"bool" => ColumnType::Boolean,
|
|
|
|
&_ => todo!("Datatype {} not supported", f.data_type().to_string()),
|
|
|
|
};
|
2023-01-17 13:34:10 +01:00
|
|
|
|
|
|
|
let name = if f.name.is_empty() {
|
|
|
|
unnamed_cols_counter += 1;
|
|
|
|
format!("Column{}", unnamed_cols_counter)
|
|
|
|
} else {
|
|
|
|
f.name
|
|
|
|
};
|
|
|
|
|
2023-01-17 12:28:49 +01:00
|
|
|
let table = Table::alter()
|
|
|
|
.table(Alias::new(table_name))
|
2023-01-17 13:34:10 +01:00
|
|
|
.add_column(&mut ColumnDef::new_with_type(Alias::new(&name), dtype))
|
2023-01-17 12:28:49 +01:00
|
|
|
.to_owned();
|
|
|
|
statements.push(table.to_string(PostgresQueryBuilder));
|
|
|
|
}
|
|
|
|
|
2023-01-17 15:48:05 +01:00
|
|
|
// Finallyls print all statements
|
2023-01-17 12:28:49 +01:00
|
|
|
for statement in statements {
|
|
|
|
println!("{};", statement);
|
|
|
|
}
|
|
|
|
}
|