|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the Apache License, Version 2.0 (the |
| 5 | + * "License") as published by the Apache Software Foundation. |
| 6 | + * |
| 7 | + * You may not use this file except in compliance with the License. You may |
| 8 | + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.supertokens.storage.postgresql.queries; |
| 18 | + |
| 19 | +import io.supertokens.pluginInterface.RowMapper; |
| 20 | +import io.supertokens.pluginInterface.exceptions.StorageQueryException; |
| 21 | +import io.supertokens.pluginInterface.useridmapping.UserIdMapping; |
| 22 | +import io.supertokens.storage.postgresql.Start; |
| 23 | +import io.supertokens.storage.postgresql.config.Config; |
| 24 | +import io.supertokens.storage.postgresql.utils.Utils; |
| 25 | + |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import java.sql.ResultSet; |
| 28 | +import java.sql.SQLException; |
| 29 | +import java.util.ArrayList; |
| 30 | + |
| 31 | +import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute; |
| 32 | +import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update; |
| 33 | + |
| 34 | +public class UserIdMappingQueries { |
| 35 | + |
| 36 | + public static String getQueryToCreateUserIdMappingTable(Start start) { |
| 37 | + String schema = Config.getConfig(start).getTableSchema(); |
| 38 | + String userIdMappingTable = Config.getConfig(start).getUserIdMappingTable(); |
| 39 | + // @formatter:off |
| 40 | + return "CREATE TABLE IF NOT EXISTS " + userIdMappingTable + " (" |
| 41 | + + "supertokens_user_id CHAR(36) NOT NULL " |
| 42 | + + "CONSTRAINT " + Utils.getConstraintName(schema, userIdMappingTable, "supertokens_user_id", "key") + " UNIQUE," |
| 43 | + + "external_user_id VARCHAR(128) NOT NULL" |
| 44 | + + " CONSTRAINT " + Utils.getConstraintName(schema, userIdMappingTable, "external_user_id", "key") + " UNIQUE," |
| 45 | + + "external_user_id_info TEXT," |
| 46 | + + " CONSTRAINT " + Utils.getConstraintName(schema, userIdMappingTable, null, "pkey") + |
| 47 | + " PRIMARY KEY(supertokens_user_id, external_user_id)," |
| 48 | + + ("CONSTRAINT " + Utils.getConstraintName(schema, userIdMappingTable, "supertokens_user_id", "fkey") + |
| 49 | + " FOREIGN KEY (supertokens_user_id)" |
| 50 | + + " REFERENCES " + Config.getConfig(start).getUsersTable() + "(user_id)" |
| 51 | + + " ON DELETE CASCADE);"); |
| 52 | + // @formatter:on |
| 53 | + } |
| 54 | + |
| 55 | + public static void createUserIdMapping(Start start, String superTokensUserId, String externalUserId, |
| 56 | + String externalUserIdInfo) throws SQLException, StorageQueryException { |
| 57 | + String QUERY = "INSERT INTO " + Config.getConfig(start).getUserIdMappingTable() |
| 58 | + + " (supertokens_user_id, external_user_id, external_user_id_info)" + " VALUES(?, ?, ?)"; |
| 59 | + |
| 60 | + update(start, QUERY, pst -> { |
| 61 | + pst.setString(1, superTokensUserId); |
| 62 | + pst.setString(2, externalUserId); |
| 63 | + pst.setString(3, externalUserIdInfo); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + public static UserIdMapping getuseraIdMappingWithSuperTokensUserId(Start start, String userId) |
| 68 | + throws SQLException, StorageQueryException { |
| 69 | + String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() |
| 70 | + + " WHERE supertokens_user_id = ?"; |
| 71 | + return execute(start, QUERY, pst -> pst.setString(1, userId), result -> { |
| 72 | + if (result.next()) { |
| 73 | + return UserIdMappingRowMapper.getInstance().mapOrThrow(result); |
| 74 | + } |
| 75 | + return null; |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + public static UserIdMapping getUserIdMappingWithExternalUserId(Start start, String userId) |
| 80 | + throws SQLException, StorageQueryException { |
| 81 | + String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() |
| 82 | + + " WHERE external_user_id = ?"; |
| 83 | + |
| 84 | + return execute(start, QUERY, pst -> pst.setString(1, userId), result -> { |
| 85 | + if (result.next()) { |
| 86 | + return UserIdMappingRowMapper.getInstance().mapOrThrow(result); |
| 87 | + } |
| 88 | + return null; |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExternalUserId(Start start, |
| 93 | + String userId) throws SQLException, StorageQueryException { |
| 94 | + String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() |
| 95 | + + " WHERE supertokens_user_id = ? OR external_user_id = ? "; |
| 96 | + |
| 97 | + return execute(start, QUERY, pst -> { |
| 98 | + pst.setString(1, userId); |
| 99 | + pst.setString(2, userId); |
| 100 | + }, result -> { |
| 101 | + ArrayList<UserIdMapping> userIdMappingArray = new ArrayList<>(); |
| 102 | + while (result.next()) { |
| 103 | + userIdMappingArray.add(UserIdMappingRowMapper.getInstance().mapOrThrow(result)); |
| 104 | + } |
| 105 | + return userIdMappingArray.toArray(UserIdMapping[]::new); |
| 106 | + }); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + public static boolean deleteUserIdMappingWithSuperTokensUserId(Start start, String userId) |
| 111 | + throws SQLException, StorageQueryException { |
| 112 | + String QUERY = "DELETE FROM " + Config.getConfig(start).getUserIdMappingTable() |
| 113 | + + " WHERE supertokens_user_id = ?"; |
| 114 | + |
| 115 | + // store the number of rows updated |
| 116 | + int rowUpdatedCount = update(start, QUERY, pst -> pst.setString(1, userId)); |
| 117 | + |
| 118 | + return rowUpdatedCount > 0; |
| 119 | + } |
| 120 | + |
| 121 | + public static boolean deleteUserIdMappingWithExternalUserId(Start start, String userId) |
| 122 | + throws SQLException, StorageQueryException { |
| 123 | + String QUERY = "DELETE FROM " + Config.getConfig(start).getUserIdMappingTable() + " WHERE external_user_id = ?"; |
| 124 | + |
| 125 | + // store the number of rows updated |
| 126 | + int rowUpdatedCount = update(start, QUERY, pst -> pst.setString(1, userId)); |
| 127 | + |
| 128 | + return rowUpdatedCount > 0; |
| 129 | + } |
| 130 | + |
| 131 | + public static boolean updateOrDeleteExternalUserIdInfoWithSuperTokensUserId(Start start, String userId, |
| 132 | + @Nullable String externalUserIdInfo) throws SQLException, StorageQueryException { |
| 133 | + String QUERY = "UPDATE " + Config.getConfig(start).getUserIdMappingTable() |
| 134 | + + " SET external_user_id_info = ? WHERE supertokens_user_id = ?"; |
| 135 | + |
| 136 | + int rowUpdated = update(start, QUERY, pst -> { |
| 137 | + pst.setString(1, externalUserIdInfo); |
| 138 | + pst.setString(2, userId); |
| 139 | + }); |
| 140 | + |
| 141 | + return rowUpdated > 0; |
| 142 | + } |
| 143 | + |
| 144 | + public static boolean updateOrDeleteExternalUserIdInfoWithExternalUserId(Start start, String userId, |
| 145 | + @Nullable String externalUserIdInfo) throws SQLException, StorageQueryException { |
| 146 | + String QUERY = "UPDATE " + Config.getConfig(start).getUserIdMappingTable() |
| 147 | + + " SET external_user_id_info = ? WHERE external_user_id = ?"; |
| 148 | + |
| 149 | + int rowUpdated = update(start, QUERY, pst -> { |
| 150 | + pst.setString(1, externalUserIdInfo); |
| 151 | + pst.setString(2, userId); |
| 152 | + }); |
| 153 | + |
| 154 | + return rowUpdated > 0; |
| 155 | + } |
| 156 | + |
| 157 | + private static class UserIdMappingRowMapper implements RowMapper<UserIdMapping, ResultSet> { |
| 158 | + private static final UserIdMappingRowMapper INSTANCE = new UserIdMappingRowMapper(); |
| 159 | + |
| 160 | + private UserIdMappingRowMapper() { |
| 161 | + } |
| 162 | + |
| 163 | + private static UserIdMappingRowMapper getInstance() { |
| 164 | + return INSTANCE; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public UserIdMapping map(ResultSet rs) throws Exception { |
| 169 | + return new UserIdMapping(rs.getString("supertokens_user_id"), rs.getString("external_user_id"), |
| 170 | + rs.getString("external_user_id_info")); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments