Skip to content

Commit

Permalink
docs : fix/optimizes utils.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
1182658898 committed Jan 1, 2025
1 parent a029212 commit 25207a7
Showing 1 changed file with 97 additions and 22 deletions.
119 changes: 97 additions & 22 deletions api/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,116 @@ 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)),
for (i, column_type) in column_types.iter().enumerate() {
let column_name = column_names[i];
let value: Value = match **column_type {
Type::INT2 => {
if row.is_null(i) {
Value::Null
} else {
json!(row.get::<_, i16>(i))
}
}
Type::INT4 => {
if row.is_null(i) {
Value::Null
} else {
json!(row.get::<_, i32>(i))
}
}
Type::INT8 => {
if row.is_null(i) {
Value::Null
} else {
json!(row.get::<_, i64>(i))
}
}
Type::FLOAT4 => {
if row.is_null(i) {
Value::Null
} else {
json!(row.get::<_, f32>(i))
}
}
Type::FLOAT8 => {
if row.is_null(i) {
Value::Null
} else {
json!(row.get::<_, f64>(i))
}
}
Type::BOOL => {
if row.is_null(i) {
Value::Null
} else {
json!(row.get::<_, bool>(i))
}
}
Type::VARCHAR | Type::TEXT | Type::BPCHAR => {
if row.is_null(i) {
Value::Null
} else {
json!(row.get::<_, String>(i))
}
}
Type::TIMESTAMP => {
let ts: NaiveDateTime = row.get(i);
json!(ts.to_string())
if row.is_null(i) {
Value::Null
} else {
let ts: NaiveDateTime = row.get(i);
json!(ts.to_string())
}
}
Type::TIMESTAMPTZ => {
let ts: DateTime<Utc> = row.get(i);
json!(ts.to_rfc3339())
if row.is_null(i) {
Value::Null
} else {
let ts: DateTime<Utc> = row.get(i);
json!(ts.to_rfc3339())
}
}
Type::DATE => {
let date: NaiveDate = row.get(i);
json!(date.to_string())
if row.is_null(i) {
Value::Null
} else {
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
if row.is_null(i) {
Value::Null
} else {
let json_value: serde_json::Value = row.get(i);
json_value
}
}
Type::UUID => {
let uuid: Uuid = row.get(i);
json!(uuid.to_string())
if row.is_null(i) {
Value::Null
} else {
let uuid: Uuid = row.get(i);
json!(uuid.to_string())
}
}
// Add more type handlers as needed
_ => {
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 +144,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

0 comments on commit 25207a7

Please sign in to comment.