2018-08-07 17:53:48 +02:00
/ *
2022-05-01 10:53:16 +02:00
* Copyright ( C ) 2022 GeorgH93
2018-08-07 17:53:48 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2023-06-15 20:07:38 +02:00
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
2018-08-07 17:53:48 +02:00
* /
package at.pcgamingfreaks.Minepacks.Bukkit.Database.Migration ;
2018-08-10 20:23:38 +02:00
import at.pcgamingfreaks.Minepacks.Bukkit.Database.SQL ;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.SQLite ;
2018-08-07 17:53:48 +02:00
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks ;
import org.intellij.lang.annotations.Language ;
import org.jetbrains.annotations.NotNull ;
import org.jetbrains.annotations.Nullable ;
import java.sql.* ;
2022-05-01 10:53:16 +02:00
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;
2018-08-07 17:53:48 +02:00
import java.util.Date ;
2018-08-10 20:23:38 +02:00
@SuppressWarnings ( " ConstantConditions " )
public class SQLtoSQLMigration extends ToSQLMigration
2018-08-07 17:53:48 +02:00
{
private final @Language ( " SQL " ) String queryInsertUsers , queryInsertBackpacks ;
2018-08-10 20:23:38 +02:00
protected SQLtoSQLMigration ( @NotNull Minepacks plugin , @NotNull SQL oldDb , @NotNull String dbType , boolean global ) throws Exception
2018-08-07 17:53:48 +02:00
{
2018-08-10 20:23:38 +02:00
super ( plugin , oldDb , dbType , global ) ;
2019-12-01 23:25:38 +01:00
queryInsertUsers = replacePlaceholders ( newDb , " INSERT INTO {TablePlayers} ({FieldPlayerID},{FieldName},{FieldUUID}) VALUES (?,?,?); " ) ;
2018-08-07 17:53:48 +02:00
queryInsertBackpacks = replacePlaceholders ( newDb , " INSERT INTO {TableBackpacks} ({FieldBPOwner},{FieldBPITS},{FieldBPVersion},{FieldBPLastUpdate}) VALUES (?,?,?,?); " ) ;
}
@Override
public @Nullable MigrationResult migrate ( ) throws Exception
{
try ( Connection readConnection = ( ( SQL ) oldDb ) . getConnection ( ) ; Connection writeConnection = newDb . getConnection ( ) ; Statement readStatement = readConnection . createStatement ( ) )
{
int users = migrate ( " users " , writeConnection , readStatement , " SELECT * FROM {TablePlayers}; " , queryInsertUsers ) ;
int backpacks = migrate ( " backpacks " , writeConnection , readStatement , " SELECT * FROM {TableBackpacks}; " , queryInsertBackpacks ) ;
return new MigrationResult ( " Migrated " + users + " users and " + backpacks + " backpacks from " + oldDb . getClass ( ) . getSimpleName ( ) + " to " + newDb . getClass ( ) . getSimpleName ( ) + " . " , MigrationResult . MigrationResultType . SUCCESS ) ;
}
finally
{
newDb . close ( ) ;
}
}
2018-08-10 20:23:38 +02:00
private int migrate ( @NotNull String type , @NotNull Connection writeConnection , @NotNull Statement readStatement , @Language ( " SQL " ) String readQuery , @Language ( " SQL " ) String insertQuery ) throws Exception
2018-08-07 17:53:48 +02:00
{
int count = 0 ;
byte mode = ( byte ) ( ( type . equals ( " users " ) ) ? 0 : 1 ) ;
plugin . getLogger ( ) . info ( " Migrate " + type + " ... " ) ;
try ( ResultSet resultSet = readStatement . executeQuery ( replacePlaceholders ( ( SQL ) oldDb , readQuery ) ) ;
PreparedStatement preparedStatement = writeConnection . prepareStatement ( replacePlaceholders ( newDb , insertQuery ) ) )
{
while ( resultSet . next ( ) )
{
switch ( mode )
{
2018-08-10 20:23:38 +02:00
case 0 : migrateUser ( resultSet , preparedStatement ) ; break ;
case 1 : migrateBackpack ( resultSet , preparedStatement ) ; break ;
2018-08-07 17:53:48 +02:00
}
preparedStatement . addBatch ( ) ;
count + + ;
}
preparedStatement . executeBatch ( ) ;
}
plugin . getLogger ( ) . info ( " Migrated " + count + " " + type + " . " ) ;
return count ;
}
private void migrateUser ( @NotNull ResultSet usersResultSet , @NotNull PreparedStatement preparedStatement ) throws Exception
{
int userId = usersResultSet . getInt ( ( String ) FIELD_PLAYER_ID . get ( oldDb ) ) ;
preparedStatement . setInt ( 1 , userId ) ;
preparedStatement . setString ( 2 , usersResultSet . getString ( ( String ) FIELD_PLAYER_NAME . get ( oldDb ) ) ) ;
2019-12-01 23:25:38 +01:00
preparedStatement . setString ( 3 , usersResultSet . getString ( ( String ) FIELD_PLAYER_UUID . get ( oldDb ) ) ) ;
2018-08-07 17:53:48 +02:00
}
private void migrateBackpack ( @NotNull ResultSet backpacksResultSet , @NotNull PreparedStatement preparedStatement ) throws Exception
{
preparedStatement . setInt ( 1 , backpacksResultSet . getInt ( ( String ) FIELD_BP_OWNER . get ( oldDb ) ) ) ;
preparedStatement . setBytes ( 2 , backpacksResultSet . getBytes ( ( String ) FIELD_BP_ITS . get ( oldDb ) ) ) ;
preparedStatement . setInt ( 3 , backpacksResultSet . getInt ( ( String ) FIELD_BP_VERSION . get ( oldDb ) ) ) ;
2022-05-01 10:53:16 +02:00
final DateFormat sqliteDateFormat = new SimpleDateFormat ( " yyyy-MM-dd " ) ;
2018-08-07 17:53:48 +02:00
if ( oldDb instanceof SQLite )
{
2022-05-01 10:53:16 +02:00
preparedStatement . setTimestamp ( 4 , new Timestamp ( sqliteDateFormat . parse ( backpacksResultSet . getString ( ( String ) FIELD_BP_LAST_UPDATE . get ( oldDb ) ) ) . getTime ( ) ) ) ;
2018-08-07 17:53:48 +02:00
}
else
{
2022-05-01 10:53:16 +02:00
preparedStatement . setString ( 4 , sqliteDateFormat . format ( new Date ( backpacksResultSet . getTimestamp ( ( String ) FIELD_BP_LAST_UPDATE . get ( oldDb ) ) . getTime ( ) ) ) ) ;
2018-08-07 17:53:48 +02:00
}
}
}