@@ -150,43 +150,44 @@ public static void createDevice_Transaction(Start start, Connection sqlCon, AppI
150150 public static void createDevices_Transaction (Start start , Connection sqlCon , AppIdentifier appIdentifier ,
151151 List <TOTPDevice > devices )
152152 throws SQLException , StorageQueryException {
153+ if (devices != null && !devices .isEmpty ()) {
154+ String insert_user_QUERY = "INSERT INTO " + Config .getConfig (start ).getTotpUsersTable ()
155+ + " (app_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING" ;
156+
157+ String insert_device_QUERY = "INSERT INTO " + Config .getConfig (start ).getTotpUserDevicesTable ()
158+ +
159+ " (app_id, user_id, device_name, secret_key, period, skew, verified, created_at) VALUES (?, ?, ?, ?, " +
160+ "?, ?, ?, ?) ON CONFLICT (app_id, user_id, device_name) DO UPDATE SET secret_key = ?, period = ?, skew = ?, created_at = ?, verified = ?" ;
161+
162+ List <PreparedStatementValueSetter > userSetters = new ArrayList <>();
163+ List <PreparedStatementValueSetter > deviceSetters = new ArrayList <>();
164+
165+ for (TOTPDevice device : devices ) {
166+ userSetters .add (pst -> {
167+ pst .setString (1 , appIdentifier .getAppId ());
168+ pst .setString (2 , device .userId );
169+ });
170+
171+ deviceSetters .add (pst -> {
172+ pst .setString (1 , appIdentifier .getAppId ());
173+ pst .setString (2 , device .userId );
174+ pst .setString (3 , device .deviceName );
175+ pst .setString (4 , device .secretKey );
176+ pst .setInt (5 , device .period );
177+ pst .setInt (6 , device .skew );
178+ pst .setBoolean (7 , device .verified );
179+ pst .setLong (8 , device .createdAt );
180+ pst .setString (9 , device .secretKey );
181+ pst .setInt (10 , device .period );
182+ pst .setInt (11 , device .skew );
183+ pst .setLong (12 , device .createdAt );
184+ pst .setBoolean (13 , device .verified );
185+ });
186+ }
153187
154- String insert_user_QUERY = "INSERT INTO " + Config .getConfig (start ).getTotpUsersTable ()
155- + " (app_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING" ;
156-
157- String insert_device_QUERY = "INSERT INTO " + Config .getConfig (start ).getTotpUserDevicesTable ()
158- +
159- " (app_id, user_id, device_name, secret_key, period, skew, verified, created_at) VALUES (?, ?, ?, ?, " +
160- "?, ?, ?, ?) ON CONFLICT (app_id, user_id, device_name) DO UPDATE SET secret_key = ?, period = ?, skew = ?, created_at = ?, verified = ?" ;
161-
162- List <PreparedStatementValueSetter > userSetters = new ArrayList <>();
163- List <PreparedStatementValueSetter > deviceSetters = new ArrayList <>();
164-
165- for (TOTPDevice device : devices ){
166- userSetters .add (pst -> {
167- pst .setString (1 , appIdentifier .getAppId ());
168- pst .setString (2 , device .userId );
169- });
170-
171- deviceSetters .add (pst -> {
172- pst .setString (1 , appIdentifier .getAppId ());
173- pst .setString (2 , device .userId );
174- pst .setString (3 , device .deviceName );
175- pst .setString (4 , device .secretKey );
176- pst .setInt (5 , device .period );
177- pst .setInt (6 , device .skew );
178- pst .setBoolean (7 , device .verified );
179- pst .setLong (8 , device .createdAt );
180- pst .setString (9 , device .secretKey );
181- pst .setInt (10 , device .period );
182- pst .setInt (11 , device .skew );
183- pst .setLong (12 , device .createdAt );
184- pst .setBoolean (13 , device .verified );
185- });
188+ executeBatch (sqlCon , insert_user_QUERY , userSetters );
189+ executeBatch (sqlCon , insert_device_QUERY , deviceSetters );
186190 }
187-
188- executeBatch (sqlCon , insert_user_QUERY , userSetters );
189- executeBatch (sqlCon , insert_device_QUERY , deviceSetters );
190191 }
191192
192193 public static TOTPDevice getDeviceByName_Transaction (Start start , Connection sqlCon , AppIdentifier appIdentifier ,
@@ -290,6 +291,9 @@ public static TOTPDevice[] getDevices(Start start, AppIdentifier appIdentifier,
290291
291292 public static Map <String , List <TOTPDevice >> getDevicesForMultipleUsers (Start start , AppIdentifier appIdentifier , List <String > userIds )
292293 throws StorageQueryException , SQLException {
294+ if (userIds == null || userIds .isEmpty ()){
295+ return new HashMap <>();
296+ }
293297 String QUERY = "SELECT * FROM " + Config .getConfig (start ).getTotpUserDevicesTable ()
294298 + " WHERE app_id = ? AND user_id IN (" + Utils .generateCommaSeperatedQuestionMarks (userIds .size ()) + ");" ;
295299
0 commit comments