Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api : fix/optimizes utils.rs #45

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
76 changes: 45 additions & 31 deletions api/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,55 @@ pub fn get_column_names(rows: &[Row]) -> Vec<String> {
}

pub fn rows_to_json(rows: &[Row]) -> Vec<Value> {
if rows.is_empty() {
return vec![];
}

let columns = rows[0].columns();
let column_types: Vec<&Type> = columns.iter().map(|col| col.type_()).collect();
let column_names: Vec<&str> = columns.iter().map(|col| col.name()).collect();

rows.iter()
.map(|row| {
let mut map = serde_json::Map::new();
for (i, column) in row.columns().iter().enumerate() {
let value: Value = match *column.type_() {
Type::INT2 => json!(row.get::<_, i16>(i)),
Type::INT4 => json!(row.get::<_, i32>(i)),
Type::INT8 => json!(row.get::<_, i64>(i)),
Type::FLOAT4 => json!(row.get::<_, f32>(i)),
Type::FLOAT8 => json!(row.get::<_, f64>(i)),
Type::BOOL => json!(row.get::<_, bool>(i)),
Type::VARCHAR | Type::TEXT | Type::BPCHAR => json!(row.get::<_, String>(i)),
Type::TIMESTAMP => {
let ts: NaiveDateTime = row.get(i);
json!(ts.to_string())
}
Type::TIMESTAMPTZ => {
let ts: DateTime<Utc> = row.get(i);
json!(ts.to_rfc3339())
}
Type::DATE => {
let date: NaiveDate = row.get(i);
json!(date.to_string())
}
Type::JSON | Type::JSONB => {
let json_value: serde_json::Value = row.get(i);
json_value
}
Type::UUID => {
let uuid: Uuid = row.get(i);
json!(uuid.to_string())
for (i, &column_type) in column_types.iter().enumerate() {
let column_name = column_names[i];
let value: Value = if row.is_null(i) {
Value::Null
} else {
match *column_type {
Type::INT2 => json!(row.get::<_, i16>(i)),
Type::INT4 => json!(row.get::<_, i32>(i)),
Type::INT8 => json!(row.get::<_, i64>(i)),
Type::FLOAT4 => json!(row.get::<_, f32>(i)),
Type::FLOAT8 => json!(row.get::<_, f64>(i)),
Type::BOOL => json!(row.get::<_, bool>(i)),
Type::VARCHAR | Type::TEXT | Type::BPCHAR => json!(row.get::<_, String>(i)),
Type::TIMESTAMP => {
let ts: NaiveDateTime = row.get(i);
json!(ts.to_string())
}
Type::TIMESTAMPTZ => {
let ts: DateTime<Utc> = row.get(i);
json!(ts.to_rfc3339())
}
Type::DATE => {
let date: NaiveDate = row.get(i);
json!(date.to_string())
}
Type::JSON | Type::JSONB => row.get::<_, Value>(i),
Type::UUID => {
let uuid: Uuid = row.get(i);
json!(uuid.to_string())
}
// Log unhandled types for future improvements
_ => {
log::warn!("Unhandled column type: {:?}", column_type);
Value::Null
}
}
_ => Value::Null,
};
map.insert(column.name().to_string(), value);
map.insert(column_name.to_string(), value);
}
Value::Object(map)
})
Expand All @@ -69,7 +83,7 @@ pub struct Pagination {
impl Pagination {
pub fn new(query: Query<PaginationParams>, total_count: i64) -> Self {
let limit = query.limit.unwrap_or(200).clamp(1, 1000);
let total_pages = (total_count as f64 / limit as f64).ceil() as i64;
let total_pages = ((total_count as f64 / limit as f64).ceil() as i64).max(1);

let page = query.page.unwrap_or(1).clamp(1, total_pages);

Expand Down
Loading