@@ -5,7 +5,7 @@ use std::str::FromStr;
55#[ cfg( not( target_os = "macos" ) ) ]
66use defguard_wireguard_rs:: { key:: Key , net:: IpAddrMask , peer:: Peer , InterfaceConfiguration } ;
77use 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" ) ) ]
1111use 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
403420impl 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 {
0 commit comments