server table structure response is now generalized

This commit is contained in:
filipriec
2025-05-25 18:57:13 +02:00
parent bd7c97ca91
commit 685361a11a
7 changed files with 193 additions and 359 deletions

View File

@@ -4,18 +4,22 @@ package multieko2.table_structure;
import "common.proto"; import "common.proto";
message GetTableStructureRequest {
string profile_name = 1; // e.g., "default"
string table_name = 2; // e.g., "2025_adresar6"
}
message TableStructureResponse { message TableStructureResponse {
repeated TableColumn columns = 1; repeated TableColumn columns = 1;
} }
message TableColumn { message TableColumn {
string name = 1; string name = 1;
string data_type = 2; string data_type = 2; // e.g., "TEXT", "BIGINT", "VARCHAR(255)", "TIMESTAMPTZ"
bool is_nullable = 3; bool is_nullable = 3;
bool is_primary_key = 4; bool is_primary_key = 4;
} }
service TableStructureService { service TableStructureService {
rpc GetAdresarTableStructure (common.Empty) returns (TableStructureResponse); rpc GetTableStructure (GetTableStructureRequest) returns (TableStructureResponse);
rpc GetUctovnictvoTableStructure (common.Empty) returns (TableStructureResponse);
} }

Binary file not shown.

View File

@@ -1,5 +1,14 @@
// This file is @generated by prost-build. // This file is @generated by prost-build.
#[derive(Clone, PartialEq, ::prost::Message)] #[derive(Clone, PartialEq, ::prost::Message)]
pub struct GetTableStructureRequest {
/// e.g., "default"
#[prost(string, tag = "1")]
pub profile_name: ::prost::alloc::string::String,
/// e.g., "2025_adresar6"
#[prost(string, tag = "2")]
pub table_name: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TableStructureResponse { pub struct TableStructureResponse {
#[prost(message, repeated, tag = "1")] #[prost(message, repeated, tag = "1")]
pub columns: ::prost::alloc::vec::Vec<TableColumn>, pub columns: ::prost::alloc::vec::Vec<TableColumn>,
@@ -8,6 +17,7 @@ pub struct TableStructureResponse {
pub struct TableColumn { pub struct TableColumn {
#[prost(string, tag = "1")] #[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String, pub name: ::prost::alloc::string::String,
/// e.g., "TEXT", "BIGINT", "VARCHAR(255)", "TIMESTAMPTZ"
#[prost(string, tag = "2")] #[prost(string, tag = "2")]
pub data_type: ::prost::alloc::string::String, pub data_type: ::prost::alloc::string::String,
#[prost(bool, tag = "3")] #[prost(bool, tag = "3")]
@@ -106,9 +116,9 @@ pub mod table_structure_service_client {
self.inner = self.inner.max_encoding_message_size(limit); self.inner = self.inner.max_encoding_message_size(limit);
self self
} }
pub async fn get_adresar_table_structure( pub async fn get_table_structure(
&mut self, &mut self,
request: impl tonic::IntoRequest<super::super::common::Empty>, request: impl tonic::IntoRequest<super::GetTableStructureRequest>,
) -> std::result::Result< ) -> std::result::Result<
tonic::Response<super::TableStructureResponse>, tonic::Response<super::TableStructureResponse>,
tonic::Status, tonic::Status,
@@ -123,43 +133,14 @@ pub mod table_structure_service_client {
})?; })?;
let codec = tonic::codec::ProstCodec::default(); let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static( let path = http::uri::PathAndQuery::from_static(
"/multieko2.table_structure.TableStructureService/GetAdresarTableStructure", "/multieko2.table_structure.TableStructureService/GetTableStructure",
); );
let mut req = request.into_request(); let mut req = request.into_request();
req.extensions_mut() req.extensions_mut()
.insert( .insert(
GrpcMethod::new( GrpcMethod::new(
"multieko2.table_structure.TableStructureService", "multieko2.table_structure.TableStructureService",
"GetAdresarTableStructure", "GetTableStructure",
),
);
self.inner.unary(req, path, codec).await
}
pub async fn get_uctovnictvo_table_structure(
&mut self,
request: impl tonic::IntoRequest<super::super::common::Empty>,
) -> std::result::Result<
tonic::Response<super::TableStructureResponse>,
tonic::Status,
> {
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::unknown(
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/multieko2.table_structure.TableStructureService/GetUctovnictvoTableStructure",
);
let mut req = request.into_request();
req.extensions_mut()
.insert(
GrpcMethod::new(
"multieko2.table_structure.TableStructureService",
"GetUctovnictvoTableStructure",
), ),
); );
self.inner.unary(req, path, codec).await self.inner.unary(req, path, codec).await
@@ -179,16 +160,9 @@ pub mod table_structure_service_server {
/// Generated trait containing gRPC methods that should be implemented for use with TableStructureServiceServer. /// Generated trait containing gRPC methods that should be implemented for use with TableStructureServiceServer.
#[async_trait] #[async_trait]
pub trait TableStructureService: std::marker::Send + std::marker::Sync + 'static { pub trait TableStructureService: std::marker::Send + std::marker::Sync + 'static {
async fn get_adresar_table_structure( async fn get_table_structure(
&self, &self,
request: tonic::Request<super::super::common::Empty>, request: tonic::Request<super::GetTableStructureRequest>,
) -> std::result::Result<
tonic::Response<super::TableStructureResponse>,
tonic::Status,
>;
async fn get_uctovnictvo_table_structure(
&self,
request: tonic::Request<super::super::common::Empty>,
) -> std::result::Result< ) -> std::result::Result<
tonic::Response<super::TableStructureResponse>, tonic::Response<super::TableStructureResponse>,
tonic::Status, tonic::Status,
@@ -271,15 +245,13 @@ pub mod table_structure_service_server {
} }
fn call(&mut self, req: http::Request<B>) -> Self::Future { fn call(&mut self, req: http::Request<B>) -> Self::Future {
match req.uri().path() { match req.uri().path() {
"/multieko2.table_structure.TableStructureService/GetAdresarTableStructure" => { "/multieko2.table_structure.TableStructureService/GetTableStructure" => {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
struct GetAdresarTableStructureSvc<T: TableStructureService>( struct GetTableStructureSvc<T: TableStructureService>(pub Arc<T>);
pub Arc<T>,
);
impl< impl<
T: TableStructureService, T: TableStructureService,
> tonic::server::UnaryService<super::super::common::Empty> > tonic::server::UnaryService<super::GetTableStructureRequest>
for GetAdresarTableStructureSvc<T> { for GetTableStructureSvc<T> {
type Response = super::TableStructureResponse; type Response = super::TableStructureResponse;
type Future = BoxFuture< type Future = BoxFuture<
tonic::Response<Self::Response>, tonic::Response<Self::Response>,
@@ -287,11 +259,11 @@ pub mod table_structure_service_server {
>; >;
fn call( fn call(
&mut self, &mut self,
request: tonic::Request<super::super::common::Empty>, request: tonic::Request<super::GetTableStructureRequest>,
) -> Self::Future { ) -> Self::Future {
let inner = Arc::clone(&self.0); let inner = Arc::clone(&self.0);
let fut = async move { let fut = async move {
<T as TableStructureService>::get_adresar_table_structure( <T as TableStructureService>::get_table_structure(
&inner, &inner,
request, request,
) )
@@ -306,58 +278,7 @@ pub mod table_structure_service_server {
let max_encoding_message_size = self.max_encoding_message_size; let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone(); let inner = self.inner.clone();
let fut = async move { let fut = async move {
let method = GetAdresarTableStructureSvc(inner); let method = GetTableStructureSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
"/multieko2.table_structure.TableStructureService/GetUctovnictvoTableStructure" => {
#[allow(non_camel_case_types)]
struct GetUctovnictvoTableStructureSvc<T: TableStructureService>(
pub Arc<T>,
);
impl<
T: TableStructureService,
> tonic::server::UnaryService<super::super::common::Empty>
for GetUctovnictvoTableStructureSvc<T> {
type Response = super::TableStructureResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::super::common::Empty>,
) -> Self::Future {
let inner = Arc::clone(&self.0);
let fut = async move {
<T as TableStructureService>::get_uctovnictvo_table_structure(
&inner,
request,
)
.await
};
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let method = GetUctovnictvoTableStructureSvc(inner);
let codec = tonic::codec::ProstCodec::default(); let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec) let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config( .apply_compression_config(

View File

@@ -1,11 +1,12 @@
// src/server/services/table_structure_service.rs // src/server/services/table_structure_service.rs
use tonic::{Request, Response, Status}; use tonic::{Request, Response, Status};
// Correct the import path for the TableStructureService trait
use common::proto::multieko2::table_structure::table_structure_service_server::TableStructureService; use common::proto::multieko2::table_structure::table_structure_service_server::TableStructureService;
use common::proto::multieko2::table_structure::TableStructureResponse; use common::proto::multieko2::table_structure::{
use common::proto::multieko2::common::Empty; GetTableStructureRequest,
use crate::table_structure::handlers::{ TableStructureResponse,
get_adresar_table_structure, get_uctovnictvo_table_structure,
}; };
use crate::table_structure::handlers::get_table_structure;
use sqlx::PgPool; use sqlx::PgPool;
#[derive(Debug)] #[derive(Debug)]
@@ -13,22 +14,21 @@ pub struct TableStructureHandler {
pub db_pool: PgPool, pub db_pool: PgPool,
} }
#[tonic::async_trait] impl TableStructureHandler {
impl TableStructureService for TableStructureHandler { pub fn new(db_pool: PgPool) -> Self {
async fn get_adresar_table_structure( Self { db_pool }
&self,
request: Request<Empty>,
) -> Result<Response<TableStructureResponse>, Status> {
let response = get_adresar_table_structure(&self.db_pool, request.into_inner())
.await?;
Ok(Response::new(response))
} }
}
async fn get_uctovnictvo_table_structure( #[tonic::async_trait]
impl TableStructureService for TableStructureHandler { // This line should now be correct
async fn get_table_structure(
&self, &self,
request: Request<Empty>, request: Request<GetTableStructureRequest>,
) -> Result<Response<TableStructureResponse>, Status> { ) -> Result<Response<TableStructureResponse>, Status> {
let response = get_uctovnictvo_table_structure(&self.db_pool, request.into_inner()).await?; let req_payload = request.into_inner();
let response =
get_table_structure(&self.db_pool, req_payload).await?;
Ok(Response::new(response)) Ok(Response::new(response))
} }
} }

View File

@@ -1,83 +1,39 @@
Adresar response: grpcurl -plaintext \
grpcurl -plaintext \ -d '{
-proto proto/table_structure.proto \ "profile_name": "default",
-import-path proto \ "table_name": "2025_customer"
}' \
localhost:50051 \ localhost:50051 \
multieko2.table_structure.TableStructureService/GetAdresarTableStructure multieko2.table_structure.TableStructureService/GetTableStructure
{ {
"columns": [ "columns": [
{ {
"name": "firma", "name": "id",
"dataType": "TEXT" "dataType": "INT8",
"isPrimaryKey": true
}, },
{ {
"name": "kz", "name": "deleted",
"dataType": "BOOL"
},
{
"name": "full_name",
"dataType": "TEXT", "dataType": "TEXT",
"isNullable": true "isNullable": true
}, },
{ {
"name": "drc", "name": "email",
"dataType": "TEXT", "dataType": "VARCHAR(255)",
"isNullable": true "isNullable": true
}, },
{ {
"name": "ulica", "name": "loyalty_status",
"dataType": "TEXT", "dataType": "BOOL",
"isNullable": true "isNullable": true
}, },
{ {
"name": "psc", "name": "created_at",
"dataType": "TEXT", "dataType": "TIMESTAMPTZ",
"isNullable": true
},
{
"name": "mesto",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "stat",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "banka",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "ucet",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "skladm",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "ico",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "kontakt",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "telefon",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "skladu",
"dataType": "TEXT",
"isNullable": true
},
{
"name": "fax",
"dataType": "TEXT",
"isNullable": true "isNullable": true
} }
] ]

View File

@@ -1,4 +1,4 @@
// src/table_structure/handlers.rs // src/table_structure/handlers.rs
pub mod table_structure; pub mod table_structure;
pub use table_structure::{get_adresar_table_structure, get_uctovnictvo_table_structure}; pub use table_structure::get_table_structure;

View File

@@ -1,181 +1,134 @@
// src/table_structure/handlers/table_structure.rs // src/table_structure/handlers/table_structure.rs
use tonic::Status; use common::proto::multieko2::table_structure::{
use sqlx::PgPool; GetTableStructureRequest, TableColumn, TableStructureResponse,
use common::proto::multieko2::{
table_structure::{TableStructureResponse, TableColumn},
common::Empty
}; };
use sqlx::{PgPool, Row};
use tonic::Status;
pub async fn get_adresar_table_structure( // Helper struct to map query results
_db_pool: &PgPool, #[derive(sqlx::FromRow, Debug)]
_request: Empty, struct DbColumnInfo {
) -> Result<TableStructureResponse, Status> { column_name: String,
let columns = vec![ formatted_data_type: String,
TableColumn { is_nullable: bool,
name: "firma".to_string(), is_primary_key: bool,
data_type: "TEXT".to_string(),
is_nullable: false,
is_primary_key: false,
},
TableColumn {
name: "kz".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "drc".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "ulica".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "psc".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "mesto".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "stat".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "banka".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "ucet".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "skladm".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "ico".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "kontakt".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "telefon".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "skladu".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "fax".to_string(),
data_type: "TEXT".to_string(),
is_nullable: true,
is_primary_key: false,
},
];
Ok(TableStructureResponse { columns })
} }
pub async fn get_uctovnictvo_table_structure( pub async fn get_table_structure(
_db_pool: &PgPool, db_pool: &PgPool,
_request: Empty, request: GetTableStructureRequest,
) -> Result<TableStructureResponse, Status> { ) -> Result<TableStructureResponse, Status> {
let columns = vec![ let profile_name = request.profile_name;
TableColumn { let table_name = request.table_name; // This should be the full table name, e.g., "2025_adresar6"
name: "adresar_id".to_string(), let table_schema = "public"; // Assuming tables are in the 'public' schema
data_type: "BIGINT".to_string(),
is_nullable: false, // 1. Validate Profile
is_primary_key: false, let profile = sqlx::query!(
}, "SELECT id FROM profiles WHERE name = $1",
TableColumn { profile_name
name: "c_dokladu".to_string(), )
data_type: "TEXT".to_string(), .fetch_optional(db_pool)
is_nullable: false, .await
is_primary_key: false, .map_err(|e| {
}, Status::internal(format!(
TableColumn { "Failed to query profile '{}': {}",
name: "datum".to_string(), profile_name, e
data_type: "DATE".to_string(), ))
is_nullable: false, })?;
is_primary_key: false,
}, let profile_id = match profile {
TableColumn { Some(p) => p.id,
name: "c_faktury".to_string(), None => {
data_type: "TEXT".to_string(), return Err(Status::not_found(format!(
is_nullable: false, "Profile '{}' not found",
is_primary_key: false, profile_name
}, )));
TableColumn { }
name: "obsah".to_string(), };
data_type: "TEXT".to_string(),
is_nullable: true, // 2. Validate Table within Profile
is_primary_key: false, sqlx::query!(
}, "SELECT id FROM table_definitions WHERE profile_id = $1 AND table_name = $2",
TableColumn { profile_id,
name: "stredisko".to_string(), table_name
data_type: "TEXT".to_string(), )
is_nullable: true, .fetch_optional(db_pool)
is_primary_key: false, .await
}, .map_err(|e| Status::internal(format!("Failed to query table_definitions: {}", e)))?
TableColumn { .ok_or_else(|| Status::not_found(format!(
name: "c_uctu".to_string(), "Table '{}' not found in profile '{}'",
data_type: "TEXT".to_string(), table_name,
is_nullable: true, profile_name
is_primary_key: false, )))?;
},
TableColumn { // 3. Query information_schema for column details
name: "md".to_string(), let query_str = r#"
data_type: "TEXT".to_string(), SELECT
is_nullable: true, c.column_name,
is_primary_key: false, CASE
}, WHEN c.udt_name = 'varchar' AND c.character_maximum_length IS NOT NULL THEN
TableColumn { 'VARCHAR(' || c.character_maximum_length || ')'
name: "identif".to_string(), WHEN c.udt_name = 'bpchar' AND c.character_maximum_length IS NOT NULL THEN
data_type: "TEXT".to_string(), 'CHAR(' || c.character_maximum_length || ')'
is_nullable: true, WHEN c.udt_name = 'numeric' AND c.numeric_precision IS NOT NULL AND c.numeric_scale IS NOT NULL THEN
is_primary_key: false, 'NUMERIC(' || c.numeric_precision || ',' || c.numeric_scale || ')'
}, WHEN c.udt_name = 'numeric' AND c.numeric_precision IS NOT NULL THEN
TableColumn { 'NUMERIC(' || c.numeric_precision || ')'
name: "poznanka".to_string(), WHEN STARTS_WITH(c.udt_name, '_') THEN
data_type: "TEXT".to_string(), UPPER(SUBSTRING(c.udt_name FROM 2)) || '[]'
is_nullable: true, ELSE
is_primary_key: false, UPPER(c.udt_name)
}, END AS formatted_data_type,
TableColumn { c.is_nullable = 'YES' AS is_nullable,
name: "firma".to_string(), EXISTS (
data_type: "TEXT".to_string(), SELECT 1
is_nullable: false, FROM information_schema.key_column_usage kcu
is_primary_key: false, JOIN information_schema.table_constraints tc
}, ON kcu.constraint_name = tc.constraint_name
]; AND kcu.table_schema = tc.table_schema
AND kcu.table_name = tc.table_name
WHERE tc.table_schema = c.table_schema
AND tc.table_name = c.table_name
AND tc.constraint_type = 'PRIMARY KEY'
AND kcu.column_name = c.column_name
) AS is_primary_key
FROM
information_schema.columns c
WHERE
c.table_schema = $1
AND c.table_name = $2
ORDER BY
c.ordinal_position;
"#;
let db_columns = sqlx::query_as::<_, DbColumnInfo>(query_str)
.bind(table_schema)
.bind(&table_name) // Use the validated table_name
.fetch_all(db_pool)
.await
.map_err(|e| {
Status::internal(format!(
"Failed to query column information for table '{}': {}",
table_name, e
))
})?;
if db_columns.is_empty() {
// This could mean the table exists in table_definitions but not in information_schema,
// or it has no columns. The latter is unlikely for a created table.
// Depending on desired behavior, you could return an error or an empty list.
// For now, returning an empty list if the table was validated.
}
let columns = db_columns
.into_iter()
.map(|db_col| TableColumn {
name: db_col.column_name,
data_type: db_col.formatted_data_type,
is_nullable: db_col.is_nullable,
is_primary_key: db_col.is_primary_key,
})
.collect();
Ok(TableStructureResponse { columns }) Ok(TableStructureResponse { columns })
} }