@@ -336,94 +336,81 @@ public static AuthRecipeUserInfo signUp(Start start, TenantIdentifier tenantIden
336336 });
337337 }
338338
339- public static void signUpMultiple (Start start , List <EmailPasswordImportUser > usersToSignUp )
340- throws StorageQueryException , StorageTransactionLogicException {
341- start .startTransaction (con -> {
342- Connection sqlCon = (Connection ) con .getConnection ();
343- try {
344- String app_id_to_user_id_QUERY = "INSERT INTO " + getConfig (start ).getAppIdToUserIdTable ()
345- + "(app_id, user_id, primary_or_recipe_user_id, recipe_id)" + " VALUES(?, ?, ?, ?)" ;
339+ public static void signUpMultipleForBulkImport_Transaction (Start start , Connection sqlCon , List <EmailPasswordImportUser > usersToSignUp )
340+ throws StorageQueryException , StorageTransactionLogicException , SQLException {
341+ try {
342+ String app_id_to_user_id_QUERY = "INSERT INTO " + getConfig (start ).getAppIdToUserIdTable ()
343+ + "(app_id, user_id, primary_or_recipe_user_id, recipe_id)" + " VALUES(?, ?, ?, ?)" ;
344+
345+ String all_auth_recipe_users_QUERY = "INSERT INTO " + getConfig (start ).getUsersTable () +
346+ "(app_id, tenant_id, user_id, primary_or_recipe_user_id, recipe_id, time_joined, " +
347+ "primary_or_recipe_user_time_joined)" +
348+ " VALUES(?, ?, ?, ?, ?, ?, ?)" ;
349+
350+ String emailpassword_users_QUERY = "INSERT INTO " + getConfig (start ).getEmailPasswordUsersTable ()
351+ + "(app_id, user_id, email, password_hash, time_joined)" + " VALUES(?, ?, ?, ?, ?)" ;
352+
353+ String emailpassword_users_to_tenant_QUERY =
354+ "INSERT INTO " + getConfig (start ).getEmailPasswordUserToTenantTable ()
355+ + "(app_id, tenant_id, user_id, email)" + " VALUES(?, ?, ?, ?)" ;
346356
347- String all_auth_recipe_users_QUERY = "INSERT INTO " + getConfig (start ).getUsersTable () +
348- "(app_id, tenant_id, user_id, primary_or_recipe_user_id, recipe_id, time_joined, " +
349- "primary_or_recipe_user_time_joined)" +
350- " VALUES(?, ?, ?, ?, ?, ?, ?)" ;
351-
352- String emailpassword_users_QUERY = "INSERT INTO " + getConfig (start ).getEmailPasswordUsersTable ()
353- + "(app_id, user_id, email, password_hash, time_joined)" + " VALUES(?, ?, ?, ?, ?)" ;
354-
355- String emailpassword_users_to_tenant_QUERY = "INSERT INTO " + getConfig (start ).getEmailPasswordUserToTenantTable ()
356- + "(app_id, tenant_id, user_id, email)" + " VALUES(?, ?, ?, ?)" ;
357-
358- PreparedStatement appIdToUserId = sqlCon .prepareStatement (app_id_to_user_id_QUERY );
359- PreparedStatement allAuthRecipeUsers = sqlCon .prepareStatement (all_auth_recipe_users_QUERY );
360- PreparedStatement emailPasswordUsers = sqlCon .prepareStatement (emailpassword_users_QUERY );
361- PreparedStatement emailPasswordUsersToTenant = sqlCon .prepareStatement (emailpassword_users_to_tenant_QUERY );
362-
363- int counter = 0 ;
364- for (EmailPasswordImportUser user : usersToSignUp ) {
365- String userId = user .userId ;
366- TenantIdentifier tenantIdentifier = user .tenantIdentifier ;
367-
368- appIdToUserId .setString (1 , tenantIdentifier .getAppId ());
369- appIdToUserId .setString (2 , userId );
370- appIdToUserId .setString (3 , userId );
371- appIdToUserId .setString (4 , EMAIL_PASSWORD .toString ());
372- appIdToUserId .addBatch ();
373-
374-
375- allAuthRecipeUsers .setString (1 , tenantIdentifier .getAppId ());
376- allAuthRecipeUsers .setString (2 , tenantIdentifier .getTenantId ());
377- allAuthRecipeUsers .setString (3 , userId );
378- allAuthRecipeUsers .setString (4 , userId );
379- allAuthRecipeUsers .setString (5 , EMAIL_PASSWORD .toString ());
380- allAuthRecipeUsers .setLong (6 , user .timeJoinedMSSinceEpoch );
381- allAuthRecipeUsers .setLong (7 , user .timeJoinedMSSinceEpoch );
382- allAuthRecipeUsers .addBatch ();
383-
384- emailPasswordUsers .setString (1 , tenantIdentifier .getAppId ());
385- emailPasswordUsers .setString (2 , userId );
386- emailPasswordUsers .setString (3 , user .email );
387- emailPasswordUsers .setString (4 , user .passwordHash );
388- emailPasswordUsers .setLong (5 , user .timeJoinedMSSinceEpoch );
389- emailPasswordUsers .addBatch ();
390-
391- emailPasswordUsersToTenant .setString (1 , tenantIdentifier .getAppId ());
392- emailPasswordUsersToTenant .setString (2 , tenantIdentifier .getTenantId ());
393- emailPasswordUsersToTenant .setString (3 , userId );
394- emailPasswordUsersToTenant .setString (4 , user .email );
395- emailPasswordUsersToTenant .addBatch ();
396- counter ++;
397- if (counter % 100 == 0 ) {
398- appIdToUserId .executeBatch ();
399- allAuthRecipeUsers .executeBatch ();
400- emailPasswordUsers .executeBatch ();
401- emailPasswordUsersToTenant .executeBatch ();
402- }
357+ PreparedStatement appIdToUserId = sqlCon .prepareStatement (app_id_to_user_id_QUERY );
358+ PreparedStatement allAuthRecipeUsers = sqlCon .prepareStatement (all_auth_recipe_users_QUERY );
359+ PreparedStatement emailPasswordUsers = sqlCon .prepareStatement (emailpassword_users_QUERY );
360+ PreparedStatement emailPasswordUsersToTenant = sqlCon .prepareStatement (emailpassword_users_to_tenant_QUERY );
361+
362+ int counter = 0 ;
363+ for (EmailPasswordImportUser user : usersToSignUp ) {
364+ String userId = user .userId ;
365+ TenantIdentifier tenantIdentifier = user .tenantIdentifier ;
366+
367+ appIdToUserId .setString (1 , tenantIdentifier .getAppId ());
368+ appIdToUserId .setString (2 , userId );
369+ appIdToUserId .setString (3 , userId );
370+ appIdToUserId .setString (4 , EMAIL_PASSWORD .toString ());
371+ appIdToUserId .addBatch ();
372+
373+
374+ allAuthRecipeUsers .setString (1 , tenantIdentifier .getAppId ());
375+ allAuthRecipeUsers .setString (2 , tenantIdentifier .getTenantId ());
376+ allAuthRecipeUsers .setString (3 , userId );
377+ allAuthRecipeUsers .setString (4 , userId );
378+ allAuthRecipeUsers .setString (5 , EMAIL_PASSWORD .toString ());
379+ allAuthRecipeUsers .setLong (6 , user .timeJoinedMSSinceEpoch );
380+ allAuthRecipeUsers .setLong (7 , user .timeJoinedMSSinceEpoch );
381+ allAuthRecipeUsers .addBatch ();
382+
383+ emailPasswordUsers .setString (1 , tenantIdentifier .getAppId ());
384+ emailPasswordUsers .setString (2 , userId );
385+ emailPasswordUsers .setString (3 , user .email );
386+ emailPasswordUsers .setString (4 , user .passwordHash );
387+ emailPasswordUsers .setLong (5 , user .timeJoinedMSSinceEpoch );
388+ emailPasswordUsers .addBatch ();
389+
390+ emailPasswordUsersToTenant .setString (1 , tenantIdentifier .getAppId ());
391+ emailPasswordUsersToTenant .setString (2 , tenantIdentifier .getTenantId ());
392+ emailPasswordUsersToTenant .setString (3 , userId );
393+ emailPasswordUsersToTenant .setString (4 , user .email );
394+ emailPasswordUsersToTenant .addBatch ();
395+ counter ++;
396+ if (counter % 100 == 0 ) {
397+ appIdToUserId .executeBatch ();
398+ allAuthRecipeUsers .executeBatch ();
399+ emailPasswordUsers .executeBatch ();
400+ emailPasswordUsersToTenant .executeBatch ();
403401 }
402+ }
404403
405- //execute the remaining ones
406- appIdToUserId .executeBatch ();
407- allAuthRecipeUsers .executeBatch ();
408- emailPasswordUsers .executeBatch ();
409- emailPasswordUsersToTenant .executeBatch ();
404+ //execute the remaining ones
405+ appIdToUserId .executeBatch ();
406+ allAuthRecipeUsers .executeBatch ();
407+ emailPasswordUsers .executeBatch ();
408+ emailPasswordUsersToTenant .executeBatch ();
410409
411- //UserInfoPartial userInfo = new UserInfoPartial(userId, email, passwordHash, timeJoined);
412- // fillUserInfoWithTenantIds_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo);
413- // fillUserInfoWithVerified_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo);
414- sqlCon .commit ();
415- //return AuthRecipeUserInfo.create(userId, false, userInfo.toLoginMethod());
416- } catch (SQLException throwables ) {
417- throwables .printStackTrace (System .out );
418- SQLException next = throwables .getNextException ();
419- while (next != null ) {
420- next .printStackTrace (System .out );
421- next = next .getNextException ();
422- }
423- throw new StorageTransactionLogicException (throwables );
424- }
425- return null ;
426- });
410+ sqlCon .commit ();
411+ } catch (SQLException throwables ) {
412+ throw new StorageTransactionLogicException (throwables );
413+ }
427414 }
428415
429416 public static void deleteUser_Transaction (Connection sqlCon , Start start , AppIdentifier appIdentifier ,
@@ -569,6 +556,30 @@ public static String lockEmail_Transaction(Start start, Connection con,
569556 });
570557 }
571558
559+ public static List <String > lockEmail_Transaction (Start start , Connection con ,
560+ AppIdentifier appIdentifier ,
561+ List <String > emails )
562+ throws StorageQueryException , SQLException {
563+ if (emails == null || emails .isEmpty ()){
564+ return new ArrayList <>();
565+ }
566+ String QUERY = "SELECT user_id FROM " + getConfig (start ).getEmailPasswordUsersTable () +
567+ " WHERE app_id = ? AND email IN (" + Utils .generateCommaSeperatedQuestionMarks (emails .size ()) + ") FOR UPDATE" ;
568+
569+ return execute (con , QUERY , pst -> {
570+ pst .setString (1 , appIdentifier .getAppId ());
571+ for (int i = 0 ; i < emails .size (); i ++) {
572+ pst .setString (2 + i , emails .get (i ));
573+ }
574+ }, result -> {
575+ List <String > results = new ArrayList <>();
576+ while (result .next ()) {
577+ results .add (result .getString ("user_id" ));
578+ }
579+ return results ;
580+ });
581+ }
582+
572583 public static String getPrimaryUserIdUsingEmail (Start start , TenantIdentifier tenantIdentifier ,
573584 String email )
574585 throws StorageQueryException , SQLException {
@@ -612,6 +623,33 @@ public static List<String> getPrimaryUserIdsUsingEmail_Transaction(Start start,
612623 });
613624 }
614625
626+ public static List <String > getPrimaryUserIdsUsingMultipleEmails_Transaction (Start start , Connection con ,
627+ AppIdentifier appIdentifier ,
628+ List <String > emails )
629+ throws StorageQueryException , SQLException {
630+ if (emails .isEmpty ()){
631+ return new ArrayList <>();
632+ }
633+ String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "
634+ + "FROM " + getConfig (start ).getEmailPasswordUsersTable () + " AS ep" +
635+ " JOIN " + getConfig (start ).getAppIdToUserIdTable () + " AS all_users" +
636+ " ON ep.app_id = all_users.app_id AND ep.user_id = all_users.user_id" +
637+ " WHERE ep.app_id = ? AND ep.email IN ( " + Utils .generateCommaSeperatedQuestionMarks (emails .size ()) + " )" ;
638+
639+ return execute (con , QUERY , pst -> {
640+ pst .setString (1 , appIdentifier .getAppId ());
641+ for (int i = 0 ; i < emails .size (); i ++) {
642+ pst .setString (2 +i , emails .get (i ));
643+ }
644+ }, result -> {
645+ List <String > userIds = new ArrayList <>();
646+ while (result .next ()) {
647+ userIds .add (result .getString ("user_id" ));
648+ }
649+ return userIds ;
650+ });
651+ }
652+
615653 public static boolean addUserIdToTenant_Transaction (Start start , Connection sqlCon ,
616654 TenantIdentifier tenantIdentifier , String userId )
617655 throws SQLException , StorageQueryException , UnknownUserIdException {
0 commit comments