Skip to content

Commit 662e0ac

Browse files
committed
Fix clippy and add quick db query
1 parent 88528d9 commit 662e0ac

8 files changed

Lines changed: 62 additions & 27 deletions

src-tauri/.sqlx/query-1c996712f62a1005990733cd9eee7a94bdcf2ef01b559304aea1d642fab7ae22.json renamed to src-tauri/.sqlx/query-31e1b340bfbdf29dde642b657124a0705b638769ce6631c1b455cbf45e82bb14.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/.sqlx/query-3bedd8a0e3a8d4b76330ba0f81d82cf1590e6d15ba30360c41b0a5a3482df3df.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/.sqlx/query-f88f92313f52f0b2c584f48b40e6edb4bc14d96f3000bd58a3ca68eecb8e4a88.json renamed to src-tauri/.sqlx/query-97a52a8bbf020b77afe5dc427efb66abfdc6b571d1631a4f77fbf4fa5cfbe7e7.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/.sqlx/query-ba54bef9b71c2add858203b7be2c0e87d3a54d536ef96a923a6b949e16b9746e.json renamed to src-tauri/.sqlx/query-af70b9b18d8452a03d4d5624c2f3a11ab0d2e123989e97dfceb57e472523398c.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/.sqlx/query-49039d91cfdefbb32284d15af739a2090ba7baf9dee8cf00e8800b9a5f891fab.json renamed to src-tauri/.sqlx/query-c6a5e793cccc520039e28da8b4fb73e0c79c6a8d671c300ec2ea3eb0d58342b5.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/src/bin/defguard-client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ async fn startup(app_handle: &AppHandle) {
155155
}
156156

157157
/// Open the appropriate window, either the old or the new UI, depending if there are locations.
158+
#[cfg(not(target_os = "linux"))]
158159
fn open_appropriate_window(app_handle: &AppHandle) {
159160
let has_locations = async_runtime::block_on(has_non_service_locations());
160161
if has_locations {

src-tauri/src/database/models/location.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::str::FromStr;
55
#[cfg(not(target_os = "macos"))]
66
use defguard_wireguard_rs::{key::Key, net::IpAddrMask, peer::Peer, InterfaceConfiguration};
77
use serde::{Deserialize, Serialize};
8-
use sqlx::{prelude::Type, query, query_as, query_scalar, Error as SqlxError, SqliteExecutor};
8+
use sqlx::{prelude::Type, query, query_as, query_scalar, SqliteExecutor};
99

1010
#[cfg(not(target_os = "macos"))]
1111
use super::wireguard_keys::WireguardKeys;
@@ -129,7 +129,7 @@ impl Location<Id> {
129129
pub(crate) async fn all<'e, E>(
130130
executor: E,
131131
include_service_locations: bool,
132-
) -> Result<Vec<Self>, SqlxError>
132+
) -> sqlx::Result<Vec<Self>>
133133
where
134134
E: SqliteExecutor<'e>,
135135
{
@@ -143,14 +143,34 @@ impl Location<Id> {
143143
service_location_mode \"service_location_mode: ServiceLocationMode\", \
144144
mfa_method \"mfa_method: _\", posture_check_required \
145145
FROM location WHERE service_location_mode <= $1 \
146-
ORDER BY name ASC;",
146+
ORDER BY name ASC",
147147
max_service_location_mode
148148
)
149149
.fetch_all(executor)
150150
.await
151151
}
152152

153-
pub(crate) async fn save<'e, E>(&mut self, executor: E) -> Result<(), SqlxError>
153+
#[cfg(any(windows, target_os = "macos"))]
154+
pub(crate) async fn exist<'e, E>(
155+
executor: E,
156+
include_service_locations: bool,
157+
) -> sqlx::Result<bool>
158+
where
159+
E: SqliteExecutor<'e>,
160+
{
161+
let max_service_location_mode =
162+
Self::get_service_location_mode_filter(include_service_locations);
163+
let result = query_scalar!(
164+
"SELECT EXISTS (SELECT 1 FROM location WHERE service_location_mode <= $1)",
165+
max_service_location_mode
166+
)
167+
.fetch_one(executor)
168+
.await?;
169+
170+
Ok(result != 0)
171+
}
172+
173+
pub(crate) async fn save<'e, E>(&mut self, executor: E) -> sqlx::Result<()>
154174
where
155175
E: SqliteExecutor<'e>,
156176
{
@@ -186,7 +206,7 @@ impl Location<Id> {
186206
pub(crate) async fn find_by_id<'e, E>(
187207
executor: E,
188208
location_id: Id,
189-
) -> Result<Option<Self>, SqlxError>
209+
) -> sqlx::Result<Option<Self>>
190210
where
191211
E: SqliteExecutor<'e>,
192212
{
@@ -208,7 +228,7 @@ impl Location<Id> {
208228
executor: E,
209229
instance_id: Id,
210230
include_service_locations: bool,
211-
) -> Result<Vec<Self>, SqlxError>
231+
) -> sqlx::Result<Vec<Self>>
212232
where
213233
E: SqliteExecutor<'e>,
214234
{
@@ -230,10 +250,7 @@ impl Location<Id> {
230250
.await
231251
}
232252

233-
pub(crate) async fn find_by_public_key<'e, E>(
234-
executor: E,
235-
pubkey: &str,
236-
) -> Result<Self, SqlxError>
253+
pub(crate) async fn find_by_public_key<'e, E>(executor: E, pubkey: &str) -> sqlx::Result<Self>
237254
where
238255
E: SqliteExecutor<'e>,
239256
{
@@ -244,18 +261,18 @@ impl Location<Id> {
244261
location_mfa_mode \"location_mfa_mode: LocationMfaMode\", \
245262
service_location_mode \"service_location_mode: ServiceLocationMode\",
246263
mfa_method \"mfa_method: _\", posture_check_required \
247-
FROM location WHERE pubkey = $1;",
264+
FROM location WHERE pubkey = $1",
248265
pubkey
249266
)
250267
.fetch_one(executor)
251268
.await
252269
}
253270

254-
pub(crate) async fn delete<'e, E>(&self, executor: E) -> Result<(), SqlxError>
271+
pub(crate) async fn delete<'e, E>(&self, executor: E) -> sqlx::Result<()>
255272
where
256273
E: SqliteExecutor<'e>,
257274
{
258-
query!("DELETE FROM location WHERE id = $1;", self.id)
275+
query!("DELETE FROM location WHERE id = $1", self.id)
259276
.execute(executor)
260277
.await?;
261278
Ok(())
@@ -270,7 +287,7 @@ impl Location<Id> {
270287
E: SqliteExecutor<'e>,
271288
{
272289
query!(
273-
"UPDATE location SET route_all_traffic = 0 WHERE instance_id = $1;",
290+
"UPDATE location SET route_all_traffic = 0 WHERE instance_id = $1",
274291
instance_id
275292
)
276293
.execute(executor)
@@ -401,7 +418,7 @@ impl Location<Id> {
401418
}
402419

403420
impl Location<NoId> {
404-
pub(crate) async fn save<'e, E>(self, executor: E) -> Result<Location<Id>, SqlxError>
421+
pub(crate) async fn save<'e, E>(self, executor: E) -> sqlx::Result<Location<Id>>
405422
where
406423
E: SqliteExecutor<'e>,
407424
{

src-tauri/src/window_manager/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ use crate::database::{models::location::Location, DB_POOL};
88
/// Returns `true` if there are any non-service locations in the database.
99
#[cfg(not(target_os = "linux"))]
1010
pub async fn has_non_service_locations() -> bool {
11-
match Location::all(&*DB_POOL, false).await {
12-
Ok(locations) => !locations.is_empty(),
13-
Err(_) => false,
14-
}
11+
Location::exist(&*DB_POOL, false).await.unwrap_or_default()
1512
}
1613

1714
pub const NEW_UI_WINDOW_ID: &str = "new-ui";

0 commit comments

Comments
 (0)