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

feat(sql): preserve column order from query with indexmap #1305

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/sql-column-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sql": patch
---

Preserve column order from SELECT query with indexmap
21 changes: 11 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ futures-core = "0.3"
sqlx = { version = "0.7", features = [ "json", "time" ] }
time = "0.3"
tokio = { version = "1", features = [ "sync" ] }
indexmap = { version = "2.2.6", features = ["serde"] }

[features]
sqlite = [ "sqlx/sqlite", "sqlx/runtime-tokio" ]
Expand Down
5 changes: 3 additions & 2 deletions plugins/sql/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tauri::{
};
use tokio::sync::Mutex;

use indexmap::IndexMap;
use std::collections::HashMap;

#[cfg(feature = "sqlite")]
Expand Down Expand Up @@ -233,7 +234,7 @@ async fn select(
db: String,
query: String,
values: Vec<JsonValue>,
) -> Result<Vec<HashMap<String, JsonValue>>> {
) -> Result<Vec<IndexMap<String, JsonValue>>> {
let mut instances = db_instances.0.lock().await;
let db = instances.get_mut(&db).ok_or(Error::DatabaseNotLoaded(db))?;
let mut query = sqlx::query(&query);
Expand All @@ -251,7 +252,7 @@ async fn select(
let rows = query.fetch_all(&*db).await?;
let mut values = Vec::new();
for row in rows {
let mut value = HashMap::default();
let mut value = IndexMap::default();
for (i, column) in row.columns().iter().enumerate() {
let v = row.try_get_raw(i)?;

Expand Down
Loading