working creation of profiles and tables for each profile
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
use tonic::Status;
|
||||
use sqlx::PgPool;
|
||||
use serde_json::json;
|
||||
use time::OffsetDateTime;
|
||||
use common::proto::multieko2::table_definition::{PostTableDefinitionRequest, TableDefinitionResponse};
|
||||
|
||||
const VALID_DATA_TYPES: &[&str] = &["TEXT", "INTEGER", "BIGINT", "BOOLEAN", "TIMESTAMPTZ", "NUMERIC"];
|
||||
@@ -11,16 +12,23 @@ fn is_valid_data_type(dt: &str) -> bool {
|
||||
}
|
||||
|
||||
fn is_valid_identifier(s: &str) -> bool {
|
||||
!s.is_empty() &&
|
||||
s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') &&
|
||||
!s.starts_with('_') &&
|
||||
!s.chars().next().unwrap().is_ascii_digit()
|
||||
let parts: Vec<&str> = s.split('_').collect();
|
||||
|
||||
parts.len() >= 3 &&
|
||||
parts[0] == "ud" &&
|
||||
parts[1].len() == 4 &&
|
||||
parts[1].chars().all(|c| c.is_ascii_digit()) &&
|
||||
parts[2..].iter().all(|p| !p.is_empty()) &&
|
||||
s.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
|
||||
}
|
||||
|
||||
fn sanitize_identifier(s: &str) -> String {
|
||||
s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "")
|
||||
let year = OffsetDateTime::now_utc().year();
|
||||
let cleaned = s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "")
|
||||
.trim()
|
||||
.to_lowercase()
|
||||
.to_lowercase();
|
||||
|
||||
format!("ud_{}_{}", year, cleaned)
|
||||
}
|
||||
|
||||
pub async fn post_table_definition(
|
||||
@@ -49,10 +57,10 @@ pub async fn post_table_definition(
|
||||
let linked_table_name = request.linked_table_name
|
||||
.as_ref()
|
||||
.map(|lt| sanitize_identifier(lt));
|
||||
|
||||
|
||||
if let Some(lt_name) = &linked_table_name {
|
||||
let lt_record = sqlx::query!(
|
||||
"SELECT id FROM table_definitions
|
||||
"SELECT id FROM table_definitions
|
||||
WHERE profile_id = $1 AND table_name = $2",
|
||||
profile.id,
|
||||
lt_name
|
||||
@@ -153,8 +161,15 @@ fn generate_table_sql(
|
||||
];
|
||||
|
||||
if let Some(linked) = linked_table {
|
||||
// Extract base name without prefix for relationship
|
||||
let parts: Vec<&str> = linked.splitn(3, '_').collect();
|
||||
let base_name = parts.get(2).unwrap_or(&linked);
|
||||
|
||||
system_columns.push(
|
||||
format!("\"{}_id\" BIGINT NOT NULL REFERENCES \"{}\"(id)", linked, linked)
|
||||
format!("\"{}_id\" BIGINT NOT NULL REFERENCES \"{}\"(id)",
|
||||
base_name,
|
||||
linked
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -184,7 +199,7 @@ fn generate_table_sql(
|
||||
let all_indexes = system_indexes
|
||||
.into_iter()
|
||||
.chain(indexes.iter().map(|idx| {
|
||||
format!("CREATE INDEX idx_{}_{} ON \"{}\" (\"{}\")",
|
||||
format!("CREATE INDEX idx_{}_{} ON \"{}\" (\"{}\")",
|
||||
table_name, idx, table_name, idx)
|
||||
}))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Reference in New Issue
Block a user