From 4e29d0084fb89bdbeb8f104a2b24d7d051bb5c10 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sat, 21 Jun 2025 10:37:37 +0200 Subject: [PATCH] compiled with the profile to be schemas --- server/src/steel/server/functions.rs | 9 +++++---- .../src/table_script/handlers/post_table_script.rs | 6 +++--- .../src/tables_data/handlers/delete_table_data.rs | 12 ++++++------ server/src/tables_data/handlers/get_table_data.rs | 10 +++++----- .../handlers/get_table_data_by_position.rs | 10 +++++----- .../tables_data/handlers/get_table_data_count.rs | 12 ++++++------ server/src/tables_data/handlers/post_table_data.rs | 13 +++++++------ server/src/tables_data/handlers/put_table_data.rs | 10 +++++----- 8 files changed, 42 insertions(+), 40 deletions(-) diff --git a/server/src/steel/server/functions.rs b/server/src/steel/server/functions.rs index e004e29..7c4ff91 100644 --- a/server/src/steel/server/functions.rs +++ b/server/src/steel/server/functions.rs @@ -21,7 +21,8 @@ pub enum FunctionError { #[derive(Clone)] pub struct SteelContext { pub current_table: String, - pub profile_id: i64, + pub schema_id: i64, + pub schema_name: String, pub row_data: HashMap, pub db_pool: Arc, } @@ -30,8 +31,8 @@ impl SteelContext { pub async fn get_related_table_name(&self, base_name: &str) -> Result { let table_def = sqlx::query!( r#"SELECT table_name FROM table_definitions - WHERE profile_id = $1 AND table_name LIKE $2"#, - self.profile_id, + WHERE schema_id = $1 AND table_name LIKE $2"#, + self.schema_id, format!("%_{}", base_name) ) .fetch_optional(&*self.db_pool) @@ -66,7 +67,7 @@ impl SteelContext { // Add quotes around the table name sqlx::query_scalar::<_, String>( - &format!("SELECT {} FROM \"{}\" WHERE id = $1", column, actual_table) + &format!("SELECT {} FROM \"{}\".\"{}\" WHERE id = $1", column, self.schema_name, actual_table) ) .bind(fk_value.parse::().map_err(|_| SteelVal::StringV("Invalid foreign key format".into()))?) diff --git a/server/src/table_script/handlers/post_table_script.rs b/server/src/table_script/handlers/post_table_script.rs index 4f1406a..f590b92 100644 --- a/server/src/table_script/handlers/post_table_script.rs +++ b/server/src/table_script/handlers/post_table_script.rs @@ -49,7 +49,7 @@ pub async fn post_table_script( ) -> Result { // Fetch the table definition let table_def = sqlx::query!( - r#"SELECT id, table_name, columns, profile_id + r#"SELECT id, table_name, columns, schema_id FROM table_definitions WHERE id = $1"#, request.table_definition_id ) @@ -76,7 +76,7 @@ pub async fn post_table_script( let script_record = sqlx::query!( r#"INSERT INTO table_scripts (table_definitions_id, target_table, target_column, - target_column_type, script, description, profile_id) + target_column_type, script, description, schema_id) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id"#, request.table_definition_id, @@ -85,7 +85,7 @@ pub async fn post_table_script( column_type, parsed_script, request.description, - table_def.profile_id + table_def.schema_id ) .fetch_one(db_pool) .await diff --git a/server/src/tables_data/handlers/delete_table_data.rs b/server/src/tables_data/handlers/delete_table_data.rs index 24cfec1..2f45748 100644 --- a/server/src/tables_data/handlers/delete_table_data.rs +++ b/server/src/tables_data/handlers/delete_table_data.rs @@ -9,24 +9,24 @@ pub async fn delete_table_data( request: DeleteTableDataRequest, ) -> Result { // Lookup profile - let profile = sqlx::query!( - "SELECT id FROM profiles WHERE name = $1", + let schema = sqlx::query!( + "SELECT id FROM schemas WHERE name = $1", request.profile_name ) .fetch_optional(db_pool) .await .map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?; - let profile_id = match profile { - Some(p) => p.id, + let schema_id = match schema { + Some(s) => s.id, None => return Err(Status::not_found("Profile not found")), }; // Verify table exists in profile let table_exists = sqlx::query!( "SELECT 1 AS exists FROM table_definitions - WHERE profile_id = $1 AND table_name = $2", - profile_id, + WHERE schema_id = $1 AND table_name = $2", + schema_id, request.table_name ) .fetch_optional(db_pool) diff --git a/server/src/tables_data/handlers/get_table_data.rs b/server/src/tables_data/handlers/get_table_data.rs index dc0eccf..6eb23dc 100644 --- a/server/src/tables_data/handlers/get_table_data.rs +++ b/server/src/tables_data/handlers/get_table_data.rs @@ -15,21 +15,21 @@ pub async fn get_table_data( let record_id = request.id; // Lookup profile - let profile = sqlx::query!( - "SELECT id FROM profiles WHERE name = $1", + let schema = sqlx::query!( + "SELECT id FROM schemas WHERE name = $1", profile_name ) .fetch_optional(db_pool) .await .map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?; - let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id; + let schema_id = schema.ok_or_else(|| Status::not_found("Profile not found"))?.id; // Lookup table_definition let table_def = sqlx::query!( r#"SELECT id, columns FROM table_definitions - WHERE profile_id = $1 AND table_name = $2"#, - profile_id, + WHERE schema_id = $1 AND table_name = $2"#, + schema_id, table_name ) .fetch_optional(db_pool) diff --git a/server/src/tables_data/handlers/get_table_data_by_position.rs b/server/src/tables_data/handlers/get_table_data_by_position.rs index 18c78f3..addd755 100644 --- a/server/src/tables_data/handlers/get_table_data_by_position.rs +++ b/server/src/tables_data/handlers/get_table_data_by_position.rs @@ -18,22 +18,22 @@ pub async fn get_table_data_by_position( return Err(Status::invalid_argument("Position must be at least 1")); } - let profile = sqlx::query!( - "SELECT id FROM profiles WHERE name = $1", + let schema = sqlx::query!( + "SELECT id FROM schemas WHERE name = $1", profile_name ) .fetch_optional(db_pool) .await .map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?; - let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id; + let schema_id = schema.ok_or_else(|| Status::not_found("Profile not found"))?.id; let table_exists = sqlx::query_scalar!( r#"SELECT EXISTS( SELECT 1 FROM table_definitions - WHERE profile_id = $1 AND table_name = $2 + WHERE schema_id = $1 AND table_name = $2 ) AS "exists!""#, - profile_id, + schema_id, table_name ) .fetch_one(db_pool) diff --git a/server/src/tables_data/handlers/get_table_data_count.rs b/server/src/tables_data/handlers/get_table_data_count.rs index d1d6e2b..0433f53 100644 --- a/server/src/tables_data/handlers/get_table_data_count.rs +++ b/server/src/tables_data/handlers/get_table_data_count.rs @@ -12,15 +12,15 @@ pub async fn get_table_data_count( // We still need to verify that the table is logically defined for the profile. // The schema qualifier handles *how* to access it physically, but this check // ensures the request is valid in the context of the application's definitions. - let profile = sqlx::query!( - "SELECT id FROM profiles WHERE name = $1", + let schema = sqlx::query!( + "SELECT id FROM schemas WHERE name = $1", request.profile_name ) .fetch_optional(db_pool) .await .map_err(|e| Status::internal(format!("Profile lookup error for '{}': {}", request.profile_name, e)))?; - let profile_id = match profile { + let schema_id = match schema { Some(p) => p.id, None => return Err(Status::not_found(format!("Profile '{}' not found", request.profile_name))), }; @@ -28,9 +28,9 @@ pub async fn get_table_data_count( let table_defined_for_profile = sqlx::query_scalar!( r#"SELECT EXISTS( SELECT 1 FROM table_definitions - WHERE profile_id = $1 AND table_name = $2 - ) AS "exists!" "#, // Added AS "exists!" for clarity with sqlx macro - profile_id, + WHERE schema_id = $1 AND table_name = $2 + ) AS "exists!" "#, + schema_id, request.table_name ) .fetch_one(db_pool) diff --git a/server/src/tables_data/handlers/post_table_data.rs b/server/src/tables_data/handlers/post_table_data.rs index fd299ae..c34b9ca 100644 --- a/server/src/tables_data/handlers/post_table_data.rs +++ b/server/src/tables_data/handlers/post_table_data.rs @@ -25,21 +25,21 @@ pub async fn post_table_data( let table_name = request.table_name; // Lookup profile - let profile = sqlx::query!( - "SELECT id FROM profiles WHERE name = $1", + let schema = sqlx::query!( + "SELECT id FROM schemas WHERE name = $1", profile_name ) .fetch_optional(db_pool) .await .map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?; - let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id; + let schema_id = schema.ok_or_else(|| Status::not_found("Profile not found"))?.id; // Lookup table_definition let table_def = sqlx::query!( r#"SELECT id, columns FROM table_definitions - WHERE profile_id = $1 AND table_name = $2"#, - profile_id, + WHERE schema_id = $1 AND table_name = $2"#, + schema_id, table_name ) .fetch_optional(db_pool) @@ -132,7 +132,8 @@ pub async fn post_table_data( let context = SteelContext { current_table: table_name.clone(), - profile_id, + schema_id, + schema_name: profile_name.clone(), row_data: string_data_for_scripts.clone(), db_pool: Arc::new(db_pool.clone()), }; diff --git a/server/src/tables_data/handlers/put_table_data.rs b/server/src/tables_data/handlers/put_table_data.rs index 391800a..5262a24 100644 --- a/server/src/tables_data/handlers/put_table_data.rs +++ b/server/src/tables_data/handlers/put_table_data.rs @@ -21,21 +21,21 @@ pub async fn put_table_data( } // Lookup profile - let profile = sqlx::query!( - "SELECT id FROM profiles WHERE name = $1", + let schema = sqlx::query!( + "SELECT id FROM schemas WHERE name = $1", profile_name ) .fetch_optional(db_pool) .await .map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?; - let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id; + let schema_id = schema.ok_or_else(|| Status::not_found("Profile not found"))?.id; // Lookup table_definition let table_def = sqlx::query!( r#"SELECT id, columns FROM table_definitions - WHERE profile_id = $1 AND table_name = $2"#, - profile_id, + WHERE schema_id = $1 AND table_name = $2"#, + schema_id, table_name ) .fetch_optional(db_pool)