|
|
@ -15,23 +15,41 @@ import ru.simsonic.rscPermissions.API.RowPermission; |
|
|
|
|
|
|
|
|
|
|
|
public class BackendDatabase extends ConnectionMySQL |
|
|
|
public class BackendDatabase extends ConnectionMySQL |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
private final Logger consoleLog; |
|
|
|
public BackendDatabase(Logger logger) |
|
|
|
public BackendDatabase(Logger logger) |
|
|
|
{ |
|
|
|
{ |
|
|
|
super(logger); |
|
|
|
this.consoleLog = logger; |
|
|
|
} |
|
|
|
} |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public synchronized boolean connect() |
|
|
|
public synchronized boolean connect() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return super.connect() && executeUpdateT("Deployment"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return super.connect() && executeUpdateT("Deployment"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void insertExampleRows() |
|
|
|
public synchronized void insertExampleRows() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdateT("ExampleContents"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdateT("ExampleContents"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized DatabaseContents retrieveContents() |
|
|
|
public synchronized DatabaseContents retrieveContents() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdateT("Cleanup"); |
|
|
|
|
|
|
|
final DatabaseContents result = new DatabaseContents(); |
|
|
|
final DatabaseContents result = new DatabaseContents(); |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdateT("Cleanup"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
result.entities = fetchEntities(); |
|
|
|
result.entities = fetchEntities(); |
|
|
|
result.permissions = fetchPermissions(); |
|
|
|
result.permissions = fetchPermissions(); |
|
|
|
result.inheritance = fetchInheritance(); |
|
|
|
result.inheritance = fetchInheritance(); |
|
|
@ -41,8 +59,7 @@ public class BackendDatabase extends ConnectionMySQL |
|
|
|
private RowEntity[] fetchEntities() |
|
|
|
private RowEntity[] fetchEntities() |
|
|
|
{ |
|
|
|
{ |
|
|
|
final ArrayList<RowEntity> result = new ArrayList<>(); |
|
|
|
final ArrayList<RowEntity> result = new ArrayList<>(); |
|
|
|
final ResultSet rs = executeQuery("SELECT * FROM `{DATABASE}`.`{PREFIX}entities`;"); |
|
|
|
try(final ResultSet rs = executeQuery("SELECT * FROM `{DATABASE}`.`{PREFIX}entities`;")) |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
while(rs.next()) |
|
|
|
while(rs.next()) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -57,15 +74,14 @@ public class BackendDatabase extends ConnectionMySQL |
|
|
|
} |
|
|
|
} |
|
|
|
rs.close(); |
|
|
|
rs.close(); |
|
|
|
} catch(SQLException ex) { |
|
|
|
} catch(SQLException ex) { |
|
|
|
logger.log(Level.WARNING, "[rscp] Exception in rs2e(): {0}", ex); |
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
return result.toArray(new RowEntity[result.size()]); |
|
|
|
return result.toArray(new RowEntity[result.size()]); |
|
|
|
} |
|
|
|
} |
|
|
|
private RowPermission[] fetchPermissions() |
|
|
|
private RowPermission[] fetchPermissions() |
|
|
|
{ |
|
|
|
{ |
|
|
|
final ArrayList<RowPermission> result = new ArrayList<>(); |
|
|
|
final ArrayList<RowPermission> result = new ArrayList<>(); |
|
|
|
final ResultSet rs = executeQuery("SELECT * FROM `{DATABASE}`.`{PREFIX}permissions`;"); |
|
|
|
try(final ResultSet rs = executeQuery("SELECT * FROM `{DATABASE}`.`{PREFIX}permissions`;")) |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
while(rs.next()) |
|
|
|
while(rs.next()) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -82,15 +98,14 @@ public class BackendDatabase extends ConnectionMySQL |
|
|
|
} |
|
|
|
} |
|
|
|
rs.close(); |
|
|
|
rs.close(); |
|
|
|
} catch(SQLException ex) { |
|
|
|
} catch(SQLException ex) { |
|
|
|
logger.log(Level.WARNING, "[rscp] Exception in rs2p(): {0}", ex); |
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
return result.toArray(new RowPermission[result.size()]); |
|
|
|
return result.toArray(new RowPermission[result.size()]); |
|
|
|
} |
|
|
|
} |
|
|
|
private RowInheritance[] fetchInheritance() |
|
|
|
private RowInheritance[] fetchInheritance() |
|
|
|
{ |
|
|
|
{ |
|
|
|
final ArrayList<RowInheritance> result = new ArrayList<>(); |
|
|
|
final ArrayList<RowInheritance> result = new ArrayList<>(); |
|
|
|
final ResultSet rs = executeQuery("SELECT * FROM `{DATABASE}`.`{PREFIX}inheritance`;"); |
|
|
|
try(final ResultSet rs = executeQuery("SELECT * FROM `{DATABASE}`.`{PREFIX}inheritance`;")) |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
while(rs.next()) |
|
|
|
while(rs.next()) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -107,76 +122,136 @@ public class BackendDatabase extends ConnectionMySQL |
|
|
|
} |
|
|
|
} |
|
|
|
rs.close(); |
|
|
|
rs.close(); |
|
|
|
} catch(SQLException ex) { |
|
|
|
} catch(SQLException ex) { |
|
|
|
logger.log(Level.WARNING, "[rscp] Exception in rs2i(): {0}", ex); |
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
return result.toArray(new RowInheritance[result.size()]); |
|
|
|
return result.toArray(new RowInheritance[result.size()]); |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void lockTableEntities() |
|
|
|
public synchronized void lockTableEntities() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdate("LOCK TABLES `{DATABASE}`.`{PREFIX}entities`;"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdate("LOCK TABLES `{DATABASE}`.`{PREFIX}entities`;"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void lockTablePermissions() |
|
|
|
public synchronized void lockTablePermissions() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdate("LOCK TABLES `{DATABASE}`.`{PREFIX}permissions`;"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdate("LOCK TABLES `{DATABASE}`.`{PREFIX}permissions`;"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void lockTableInheritance() |
|
|
|
public synchronized void lockTableInheritance() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdate("LOCK TABLES `{DATABASE}`.`{PREFIX}inheritance`;"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdate("LOCK TABLES `{DATABASE}`.`{PREFIX}inheritance`;"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void unlockAllTables() |
|
|
|
public synchronized void unlockAllTables() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdate("UNLOCK TABLES;"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdate("UNLOCK TABLES;"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void transactionStart() |
|
|
|
public synchronized void transactionStart() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdate("START TRANSACTION;"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdate("START TRANSACTION;"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void transactionCommit() |
|
|
|
public synchronized void transactionCommit() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdate("COMMIT;"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdate("COMMIT;"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void transactionCancel() |
|
|
|
public synchronized void transactionCancel() |
|
|
|
{ |
|
|
|
{ |
|
|
|
executeUpdate("ROLLBACK;"); |
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
executeUpdate("ROLLBACK;"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void updateEntityPrefix(String entity, EntityType type, String prefix) |
|
|
|
public synchronized void updateEntityPrefix(String entity, EntityType type, String prefix) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if("".equals(entity)) |
|
|
|
try |
|
|
|
return; |
|
|
|
{ |
|
|
|
if("".equals(prefix) || "\"\"".equals(prefix)) |
|
|
|
if("".equals(entity)) |
|
|
|
prefix = null; |
|
|
|
return; |
|
|
|
setupQueryTemplate("{ENTITY}", entity); |
|
|
|
if("".equals(prefix) || "\"\"".equals(prefix)) |
|
|
|
setupQueryTemplate("{ENTITY_TYPE}", String.valueOf(type.getValue())); |
|
|
|
prefix = null; |
|
|
|
setupQueryTemplate("{TEXT_TYPE}", "prefix"); |
|
|
|
setupQueryTemplate("{ENTITY}", entity); |
|
|
|
setupQueryTemplate("{TEXT}", (prefix != null) ? "'" + prefix + "'" : "NULL"); |
|
|
|
setupQueryTemplate("{ENTITY_TYPE}", String.valueOf(type.getValue())); |
|
|
|
executeUpdateT("UpdateEntity"); |
|
|
|
setupQueryTemplate("{TEXT_TYPE}", "prefix"); |
|
|
|
|
|
|
|
setupQueryTemplate("{TEXT}", (prefix != null) ? "'" + prefix + "'" : "NULL"); |
|
|
|
|
|
|
|
executeUpdateT("UpdateEntity"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void updateEntitySuffix(String entity, EntityType type, String suffix) |
|
|
|
public synchronized void updateEntitySuffix(String entity, EntityType type, String suffix) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if("".equals(entity)) |
|
|
|
try |
|
|
|
return; |
|
|
|
{ |
|
|
|
if("".equals(suffix) || "\"\"".equals(suffix)) |
|
|
|
if("".equals(entity)) |
|
|
|
suffix = null; |
|
|
|
return; |
|
|
|
setupQueryTemplate("{ENTITY}", entity); |
|
|
|
if("".equals(suffix) || "\"\"".equals(suffix)) |
|
|
|
setupQueryTemplate("{ENTITY_TYPE}", String.valueOf(type.getValue())); |
|
|
|
suffix = null; |
|
|
|
setupQueryTemplate("{TEXT_TYPE}", "suffix"); |
|
|
|
setupQueryTemplate("{ENTITY}", entity); |
|
|
|
setupQueryTemplate("{TEXT}", (suffix != null) ? "'" + suffix + "'" : "NULL"); |
|
|
|
setupQueryTemplate("{ENTITY_TYPE}", String.valueOf(type.getValue())); |
|
|
|
executeUpdateT("UpdateEntity"); |
|
|
|
setupQueryTemplate("{TEXT_TYPE}", "suffix"); |
|
|
|
|
|
|
|
setupQueryTemplate("{TEXT}", (suffix != null) ? "'" + suffix + "'" : "NULL"); |
|
|
|
|
|
|
|
executeUpdateT("UpdateEntity"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void removeEntityById(long id) |
|
|
|
public synchronized void removeEntityById(long id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
setupQueryTemplate("{ID}", Long.toString(id)); |
|
|
|
try |
|
|
|
executeUpdate("DELETE FROM `{DATABASE}`.`{PREFIX}entities` WHERE `id` = '{ID}';"); |
|
|
|
{ |
|
|
|
|
|
|
|
setupQueryTemplate("{ID}", Long.toString(id)); |
|
|
|
|
|
|
|
executeUpdate("DELETE FROM `{DATABASE}`.`{PREFIX}entities` WHERE `id` = '{ID}';"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void removePermissionsById(long id) |
|
|
|
public synchronized void removePermissionsById(long id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
setupQueryTemplate("{ID}", Long.toString(id)); |
|
|
|
try |
|
|
|
executeUpdate("DELETE FROM `{DATABASE}`.`{PREFIX}permissions` WHERE `id` = '{ID}';"); |
|
|
|
{ |
|
|
|
|
|
|
|
setupQueryTemplate("{ID}", Long.toString(id)); |
|
|
|
|
|
|
|
executeUpdate("DELETE FROM `{DATABASE}`.`{PREFIX}permissions` WHERE `id` = '{ID}';"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void removeInheritanceById(long id) |
|
|
|
public synchronized void removeInheritanceById(long id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
setupQueryTemplate("{ID}", Long.toString(id)); |
|
|
|
try |
|
|
|
executeUpdate("DELETE FROM `{DATABASE}`.`{PREFIX}inheritance` WHERE `id` = '{ID}';"); |
|
|
|
{ |
|
|
|
|
|
|
|
setupQueryTemplate("{ID}", Long.toString(id)); |
|
|
|
|
|
|
|
executeUpdate("DELETE FROM `{DATABASE}`.`{PREFIX}inheritance` WHERE `id` = '{ID}';"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
public synchronized void insertEntity( |
|
|
|
public synchronized void insertEntity( |
|
|
|
Long id, |
|
|
|
Long id, |
|
|
@ -259,20 +334,25 @@ public class BackendDatabase extends ConnectionMySQL |
|
|
|
} |
|
|
|
} |
|
|
|
private void insertRow(Map<String, String> fields) |
|
|
|
private void insertRow(Map<String, String> fields) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if(fields.isEmpty()) |
|
|
|
try |
|
|
|
return; |
|
|
|
|
|
|
|
final StringBuilder sbf = new StringBuilder(); |
|
|
|
|
|
|
|
final StringBuilder sbv = new StringBuilder(); |
|
|
|
|
|
|
|
final String sep = ", "; |
|
|
|
|
|
|
|
for(Map.Entry<String, String> entry : fields.entrySet()) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
sbf.append("`").append(entry.getKey()).append("`").append(sep); |
|
|
|
if(fields.isEmpty()) |
|
|
|
sbv.append(entry.getValue()).append(sep); |
|
|
|
return; |
|
|
|
|
|
|
|
final StringBuilder sbf = new StringBuilder(); |
|
|
|
|
|
|
|
final StringBuilder sbv = new StringBuilder(); |
|
|
|
|
|
|
|
final String sep = ", "; |
|
|
|
|
|
|
|
for(Map.Entry<String, String> entry : fields.entrySet()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
sbf.append("`").append(entry.getKey()).append("`").append(sep); |
|
|
|
|
|
|
|
sbv.append(entry.getValue()).append(sep); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sbf.setLength(sbf.length() - sep.length()); |
|
|
|
|
|
|
|
sbv.setLength(sbv.length() - sep.length()); |
|
|
|
|
|
|
|
setupQueryTemplate("{FIELDS}", sbf.toString()); |
|
|
|
|
|
|
|
setupQueryTemplate("{VALUES}", sbv.toString()); |
|
|
|
|
|
|
|
executeUpdate("INSERT INTO `{DATABASE}`.`{PREFIX}{TABLE}` ({FIELDS}) VALUES ({VALUES});"); |
|
|
|
|
|
|
|
} catch(SQLException ex) { |
|
|
|
|
|
|
|
consoleLog.warning(ex.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
sbf.setLength(sbf.length() - sep.length()); |
|
|
|
|
|
|
|
sbv.setLength(sbv.length() - sep.length()); |
|
|
|
|
|
|
|
setupQueryTemplate("{FIELDS}", sbf.toString()); |
|
|
|
|
|
|
|
setupQueryTemplate("{VALUES}", sbv.toString()); |
|
|
|
|
|
|
|
executeUpdate("INSERT INTO `{DATABASE}`.`{PREFIX}{TABLE}` ({FIELDS}) VALUES ({VALUES});"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|