@@ -50,7 +50,7 @@ public MysqlUserBackend(String baseTableName, MysqlConfig config, SqlUserSchema
5050
5151 @ Override
5252 public SqlUserStorage user (UUID uuid ) {
53- requireOpen ();
53+ requireAdmissionOpen ();
5454 JdbcSqlUserStorage .Dialect dialect = JdbcSqlUserStorage .Dialect .fromDbType (table .getMysql ().getConnectionManager ().getDbType ());
5555 SqlUserStorage delegate = new JdbcSqlUserStorage (UserStorage .MYSQL , uuid , table .getTableName (), schema ,
5656 () -> table .getMysql ().getConnectionManager ().getConnection (), dialect , logger );
@@ -63,46 +63,37 @@ public SqlUserStorage user(UUID uuid) {
6363 };
6464 }
6565
66- @ Override
67- public List <UUID > enumerateUsers () {
66+ @ Override public List <UUID > enumerateUsers () {
6867 ArrayList <UUID > users = new ArrayList <>();
6968 forEachUser (uuid -> {
70- if (users .size () >= MAX_MATERIALIZED_USERS ) throw new IllegalStateException ("User enumeration exceeds "
71- + MAX_MATERIALIZED_USERS + " entries; use forEachUser for streaming access" );
69+ if (users .size () >= MAX_MATERIALIZED_USERS ) throw new IllegalStateException ("User enumeration exceeds " + MAX_MATERIALIZED_USERS + " entries; use forEachUser for streaming access" );
7270 users .add (uuid );
7371 });
7472 return users ;
7573 }
7674
77- @ Override
78- public void forEachUser (Consumer <UUID > consumer ) {
75+ @ Override public void forEachUser (Consumer <UUID > consumer ) {
7976 Objects .requireNonNull (consumer , "consumer" );
8077 withOperation (() -> {
8178 String cursor = null ;
8279 while (true ) {
8380 List <UserPageEntry > page = readUserPage (cursor );
8481 if (page .isEmpty ()) return null ;
8582 cursor = page .get (page .size () - 1 ).cursor ();
86- for (UserPageEntry entry : page ) {
87- if (entry .uuid () != null ) consumer .accept (entry .uuid ());
88- }
83+ for (UserPageEntry entry : page ) if (entry .uuid () != null ) consumer .accept (entry .uuid ());
8984 if (page .size () < USER_PAGE_SIZE ) return null ;
9085 }
9186 });
9287 }
9388
9489 private List <UserPageEntry > readUserPage (String cursor ) {
9590 String uuidColumn = table .quote (SqlUserSchema .UUID_COLUMN );
96- String sql = "SELECT " + uuidColumn + " FROM " + table .quote (table .getTableName ())
97- + (cursor == null ? "" : " WHERE " + uuidColumn + " > ?" )
98- + " ORDER BY " + uuidColumn + " ASC LIMIT ?" ;
91+ String sql = "SELECT " + uuidColumn + " FROM " + table .quote (table .getTableName ()) + (cursor == null ? "" : " WHERE " + uuidColumn + " > ?" ) + " ORDER BY " + uuidColumn + " ASC LIMIT ?" ;
9992 JdbcSqlUserStorage .Dialect dialect = JdbcSqlUserStorage .Dialect .fromDbType (table .getDbType ());
100- try (Connection connection = table .getMysql ().getConnectionManager ().getConnection ();
101- PreparedStatement statement = connection .prepareStatement (sql )) {
93+ try (Connection connection = table .getMysql ().getConnectionManager ().getConnection (); PreparedStatement statement = connection .prepareStatement (sql )) {
10294 int index = 1 ;
10395 if (cursor != null ) {
104- if (dialect == JdbcSqlUserStorage .Dialect .POSTGRESQL ) dialect .bindUuid (statement , index ++, UUID .fromString (cursor ));
105- else statement .setString (index ++, cursor );
96+ if (dialect == JdbcSqlUserStorage .Dialect .POSTGRESQL ) dialect .bindUuid (statement , index ++, UUID .fromString (cursor )); else statement .setString (index ++, cursor );
10697 }
10798 statement .setInt (index , USER_PAGE_SIZE );
10899 ArrayList <UserPageEntry > page = new ArrayList <>(USER_PAGE_SIZE );
@@ -117,48 +108,39 @@ private List<UserPageEntry> readUserPage(String cursor) {
117108 }
118109 }
119110 return page ;
120- } catch (IllegalArgumentException invalidCursor ) {
121- throw new IllegalStateException ("Failed to advance SQL user enumeration cursor" , invalidCursor );
122- } catch (SQLException failure ) {
123- throw new IllegalStateException ("Failed to enumerate MySQL users" , failure );
124- }
111+ } catch (IllegalArgumentException invalidCursor ) { throw new IllegalStateException ("Failed to advance SQL user enumeration cursor" , invalidCursor ); }
112+ catch (SQLException failure ) { throw new IllegalStateException ("Failed to enumerate MySQL users" , failure ); }
125113 }
126114
127115 private record UserPageEntry (String cursor , UUID uuid ) {}
128116
129117 @ Override public boolean isOpen () { return open .get (); }
130118
131- @ Override
132- public void close () {
119+ @ Override public void close () {
133120 if (operations .getReadHoldCount () != 0 ) throw new IllegalStateException ("Cannot close MySQL from inside an active storage operation" );
134121 open .set (false );
135122 operations .writeLock ().lock ();
136123 try {
137- if (!tableClosed ) {
138- table .close ();
139- tableClosed = true ;
140- }
141- } finally {
142- operations .writeLock ().unlock ();
143- }
124+ if (!tableClosed ) { table .close (); tableClosed = true ; }
125+ } finally { operations .writeLock ().unlock (); }
144126 }
145127
146128 private <T > T withOperation (Supplier <T > operation ) {
147- requireOpen ();
129+ requireAdmissionOpen ();
148130 operations .readLock ().lock ();
149- try { requireOpen (); return operation .get (); }
150- finally { operations .readLock ().unlock (); }
131+ try {
132+ requireAdmissionOpen ();
133+ return operation .get ();
134+ } finally { operations .readLock ().unlock (); }
151135 }
152136
153137 private void ensureRegisteredColumns () {
154138 table .ensureUuidType ();
155- for (SqlUserSchema .ColumnDefinition column : schema .columns ()) {
156- if (!SqlUserSchema .UUID_COLUMN .equalsIgnoreCase (column .name ())) table .ensureColumn (column );
157- }
139+ for (SqlUserSchema .ColumnDefinition column : schema .columns ()) if (!SqlUserSchema .UUID_COLUMN .equalsIgnoreCase (column .name ())) table .ensureColumn (column );
158140 }
159141
160- private void requireOpen () {
161- if (!open .get () && operations . getReadHoldCount () == 0 ) throw new IllegalStateException ("MySQL user backend is closed" );
142+ private void requireAdmissionOpen () {
143+ if (!open .get ()) throw new IllegalStateException ("MySQL user backend is closed" );
162144 }
163145
164146 private static final class HeadlessUserTable extends AbstractSqlTable {
@@ -178,14 +160,11 @@ private static final class HeadlessUserTable extends AbstractSqlTable {
178160 }
179161
180162 @ Override public String getPrimaryKeyColumn () { return SqlUserSchema .UUID_COLUMN ; }
181-
182- @ Override
183- public String buildCreateTableSql (DbType dbType ) {
163+ @ Override public String buildCreateTableSql (DbType dbType ) {
184164 StringBuilder sql = new StringBuilder ("CREATE TABLE IF NOT EXISTS " ).append (quote (tableName )).append (" (" );
185165 boolean first = true ;
186166 for (SqlUserSchema .ColumnDefinition column : schema .columns ()) {
187- if (!first ) sql .append (", " );
188- first = false ;
167+ if (!first ) sql .append (", " ); first = false ;
189168 String type = SqlUserSchema .UUID_COLUMN .equalsIgnoreCase (column .name ()) ? bestUuidType () : normaliseTypeForDb (column .sqlType ());
190169 sql .append (quote (column .name ())).append (' ' ).append (type );
191170 }
@@ -204,10 +183,8 @@ void ensureUuidType() {
204183 String uuidType = bestUuidType ();
205184 if (!columnNeedsAlter (SqlUserSchema .UUID_COLUMN , uuidType )) return ;
206185 String uuidColumn = quote (SqlUserSchema .UUID_COLUMN );
207- String sql = "ALTER TABLE " + quote (tableName ) + " ALTER COLUMN " + uuidColumn
208- + " TYPE " + uuidType + " USING NULLIF(" + uuidColumn + ", '')::uuid;" ;
209- try (Connection connection = getMysql ().getConnectionManager ().getConnection ();
210- PreparedStatement statement = connection .prepareStatement (sql )) { statement .executeUpdate (); }
186+ String sql = "ALTER TABLE " + quote (tableName ) + " ALTER COLUMN " + uuidColumn + " TYPE " + uuidType + " USING NULLIF(" + uuidColumn + ", '')::uuid;" ;
187+ try (Connection connection = getMysql ().getConnectionManager ().getConnection (); PreparedStatement statement = connection .prepareStatement (sql )) { statement .executeUpdate (); }
211188 catch (SQLException ddlFailure ) {
212189 try { if (columnNeedsAlter (SqlUserSchema .UUID_COLUMN , uuidType )) throw ddlFailure ; }
213190 catch (SQLException inspectionFailure ) {
@@ -224,13 +201,10 @@ void ensureColumn(SqlUserSchema.ColumnDefinition column) {
224201 String storedName = findRegisteredColumn (column .name ());
225202 if (storedName != null ) {
226203 if (getDbType () == DbType .POSTGRESQL && !storedName .equals (column .name ())) renamePostgresColumn (storedName , column .name ());
227- rememberColumn (column );
228- return ;
204+ rememberColumn (column ); return ;
229205 }
230- String sql = "ALTER TABLE " + quote (tableName ) + " ADD COLUMN " + quote (column .name ())
231- + " " + normaliseTypeForDb (column .sqlType ()) + ";" ;
232- try (Connection connection = getMysql ().getConnectionManager ().getConnection ();
233- PreparedStatement statement = connection .prepareStatement (sql )) { statement .executeUpdate (); }
206+ String sql = "ALTER TABLE " + quote (tableName ) + " ADD COLUMN " + quote (column .name ()) + " " + normaliseTypeForDb (column .sqlType ()) + ";" ;
207+ try (Connection connection = getMysql ().getConnectionManager ().getConnection (); PreparedStatement statement = connection .prepareStatement (sql )) { statement .executeUpdate (); }
234208 catch (SQLException ddlFailure ) {
235209 if (!isDuplicateColumn (ddlFailure )) throw ddlFailure ;
236210 try {
@@ -249,27 +223,18 @@ void ensureColumn(SqlUserSchema.ColumnDefinition column) {
249223
250224 private void renamePostgresColumn (String storedName , String requestedName ) throws SQLException {
251225 String sql = "ALTER TABLE " + quote (tableName ) + " RENAME COLUMN " + quote (storedName ) + " TO " + quote (requestedName ) + ";" ;
252- try (Connection connection = getMysql ().getConnectionManager ().getConnection (); PreparedStatement statement = connection .prepareStatement (sql )) {
253- statement .executeUpdate ();
254- } catch (SQLException renameFailure ) {
255- String current = findRegisteredColumn (requestedName );
256- if (!requestedName .equals (current )) throw renameFailure ;
257- }
226+ try (Connection connection = getMysql ().getConnectionManager ().getConnection (); PreparedStatement statement = connection .prepareStatement (sql )) { statement .executeUpdate (); }
227+ catch (SQLException renameFailure ) { String current = findRegisteredColumn (requestedName ); if (!requestedName .equals (current )) throw renameFailure ; }
258228 }
259229
260230 private void rememberColumn (SqlUserSchema .ColumnDefinition column ) {
261- columns .removeIf (existing -> existing .equalsIgnoreCase (column .name ()));
262- columns .add (column .name ());
263- intColumns .removeIf (existing -> existing .equalsIgnoreCase (column .name ()));
264- if (column .dataType () == DataType .INTEGER ) intColumns .add (column .name ());
231+ columns .removeIf (existing -> existing .equalsIgnoreCase (column .name ())); columns .add (column .name ());
232+ intColumns .removeIf (existing -> existing .equalsIgnoreCase (column .name ())); if (column .dataType () == DataType .INTEGER ) intColumns .add (column .name ());
265233 }
266234
267235 private String findRegisteredColumn (String name ) throws SQLException {
268- try (Connection connection = getMysql ().getConnectionManager ().getConnection ();
269- PreparedStatement statement = connection .prepareStatement ("SELECT * FROM " + quote (tableName ) + " WHERE 1=0" );
270- ResultSet result = statement .executeQuery ()) {
271- ResultSetMetaData metadata = result .getMetaData ();
272- String foldedMatch = null ;
236+ try (Connection connection = getMysql ().getConnectionManager ().getConnection (); PreparedStatement statement = connection .prepareStatement ("SELECT * FROM " + quote (tableName ) + " WHERE 1=0" ); ResultSet result = statement .executeQuery ()) {
237+ ResultSetMetaData metadata = result .getMetaData (); String foldedMatch = null ;
273238 for (int i = 1 ; i <= metadata .getColumnCount (); i ++) {
274239 String storedName = metadata .getColumnName (i );
275240 if (name .equals (storedName )) return storedName ;
@@ -279,11 +244,7 @@ private String findRegisteredColumn(String name) throws SQLException {
279244 }
280245 }
281246
282- private boolean isDuplicateColumn (SQLException failure ) {
283- return getDbType () == DbType .POSTGRESQL ? "42701" .equals (failure .getSQLState ())
284- : failure .getErrorCode () == 1060 && "42S21" .equals (failure .getSQLState ());
285- }
286-
247+ private boolean isDuplicateColumn (SQLException failure ) { return getDbType () == DbType .POSTGRESQL ? "42701" .equals (failure .getSQLState ()) : failure .getErrorCode () == 1060 && "42S21" .equals (failure .getSQLState ()); }
287248 String quote (String identifier ) { return qi (identifier ); }
288249 }
289250}
0 commit comments