Skip to content

Commit 1d0c37e

Browse files
committed
maintain order of columns
1 parent 102129e commit 1d0c37e

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

be/src/api_sql2.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{
2-
collections::HashMap,
32
convert::Infallible,
43
sync::{Arc, Mutex},
54
time::{self, Duration},
@@ -42,10 +41,16 @@ pub struct Request {
4241
pub query: String,
4342
}
4443

44+
#[derive(Serialize)]
45+
pub struct Column {
46+
pub name: String,
47+
pub pgtype: String,
48+
}
49+
4550
#[derive(Serialize)]
4651
pub struct Response {
4752
pub cursor: query::Cursor,
48-
pub columns: HashMap<String, String>,
53+
pub columns: Vec<Column>,
4954
pub rows: Vec<Vec<Value>>,
5055
}
5156

@@ -229,12 +234,15 @@ async fn update_cursor(
229234
Ok(())
230235
}
231236

232-
fn get_columns(rows: &[tokio_postgres::Row]) -> HashMap<String, String> {
237+
fn get_columns(rows: &[tokio_postgres::Row]) -> Vec<Column> {
233238
rows.first()
234239
.map(|row| {
235240
row.columns()
236241
.iter()
237-
.map(|col| (col.name().to_string(), col.type_().to_string()))
242+
.map(|col| Column {
243+
name: col.name().to_string(),
244+
pgtype: col.type_().to_string(),
245+
})
238246
.collect()
239247
})
240248
.unwrap_or_default()

fe/src/static/docs/index-v2.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ And the response
1515
```
1616
{
1717
"cursor": {8453: 18479546},
18-
"columns": {"from": "bytea", "to": "bytea", "tokens": "numeric"},
18+
"columns": [
19+
{"name": "from", "pgtype": "bytea"},
20+
{"name": "to", "pgtype": "bytea"},
21+
{"name": "tokens", "pgtype": "numeric"}
22+
],
1923
"rows":[[
2024
"0x0000000000000000000000000000000000000000",
2125
"0xdaabdaac8073a7dabdc96f6909e8476ab4001b34",
@@ -111,8 +115,8 @@ The response will contain a cusror indicating that the chain `8453` was at block
111115
[
112116
{
113117
"cursor": {8453: 42},
114-
"columns": {"from": "bytea", "to": "bytea", "value": "numeric"},
115-
"rows": [["0xa...", "0xb...", "1"]]
118+
"columns": [...],
119+
"rows": [[...], ...]
116120
}
117121
]
118122
```
@@ -222,7 +226,7 @@ Regardless of the [Request](#request) there is a single response. The response i
222226
[
223227
{
224228
"cursor": {chain_id: max_block_height},
225-
"columns": {column_name: column_pg_type},
229+
"columns": [{name: string, type: string}],
226230
"rows": [
227231
[col1, col2, colN],
228232
[col1, col2, colN],
@@ -233,7 +237,9 @@ Regardless of the [Request](#request) there is a single response. The response i
233237

234238
The `cursor` object can be copied verbaitum into a subsequent request to query for new data. See [Cursor](#cursor) for more detail.
235239

236-
The `columns` field contains an object mapping the name of the column (derived from the query) to its postgres type (ie `bytea`, `numeric`, `json`, etc.).
240+
The `columns` field contains an array of objects mapping the name of the column (derived from the query) to its postgres type (ie `bytea`, `numeric`, `json`, etc.).
241+
242+
The order of the `columns` array matches the order of the column data in `rows`.
237243

238244
| ABI Type | JSON Type |
239245
|----------|---------------------|

0 commit comments

Comments
 (0)