2018-07-25 23:27:19 +02:00
/ *
* Copyright ( C ) 2018 GeorgH93
*
* 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-07-25 23:27:19 +02:00
* /
package at.pcgamingfreaks.Minepacks.Bukkit.Database.Migration ;
2018-07-25 23:55:28 +02:00
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Files ;
2018-07-25 23:27:19 +02:00
import at.pcgamingfreaks.Minepacks.Bukkit.Database.SQL ;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks ;
import at.pcgamingfreaks.Reflection ;
import org.intellij.lang.annotations.Language ;
import org.jetbrains.annotations.NotNull ;
import org.jetbrains.annotations.Nullable ;
import java.io.File ;
import java.io.FileOutputStream ;
import java.lang.reflect.InvocationTargetException ;
import java.sql.Connection ;
import java.sql.ResultSet ;
import java.sql.Statement ;
public class SQLtoFilesMigration extends Migration
{
private final String sqlQuery ;
private final File saveFolder ;
2018-08-07 17:53:48 +02:00
protected SQLtoFilesMigration ( @NotNull Minepacks plugin , @NotNull SQL oldDb ) throws InvocationTargetException , IllegalAccessException
2018-07-25 23:27:19 +02:00
{
super ( plugin , oldDb ) ;
2019-12-01 23:25:38 +01:00
@Language ( " SQL " ) String query = " SELECT {FieldUUID},{FieldBPITS},{FieldBPVersion} FROM {TablePlayers} INNER JOIN {TableBackpacks} ON {FieldPlayerID}={FieldBPOwner}; " ;
2018-08-10 20:23:38 +02:00
//noinspection ConstantConditions
sqlQuery = ( String ) Reflection . getMethod ( SQL . class , " replacePlaceholders " , String . class ) . invoke ( oldDb , query ) ;
2018-07-25 23:55:28 +02:00
saveFolder = new File ( this . plugin . getDataFolder ( ) , Files . FOLDER_NAME ) ;
if ( ! saveFolder . exists ( ) & & ! saveFolder . mkdirs ( ) ) plugin . getLogger ( ) . warning ( " Failed to create save folder ( " + saveFolder . getAbsolutePath ( ) + " ). " ) ;
2018-07-25 23:27:19 +02:00
}
@Override
public @Nullable MigrationResult migrate ( ) throws Exception
{
int migrated = 0 ;
2018-08-10 20:23:38 +02:00
try ( Connection connection = ( ( SQL ) oldDb ) . getConnection ( ) ; Statement st = connection . createStatement ( ) ; ResultSet rs = st . executeQuery ( sqlQuery ) )
2018-07-25 23:27:19 +02:00
{
while ( rs . next ( ) )
{
2018-07-25 23:55:28 +02:00
try ( FileOutputStream fos = new FileOutputStream ( new File ( saveFolder , rs . getString ( 1 ) + Files . EXT ) ) )
2018-07-25 23:27:19 +02:00
{
fos . write ( rs . getInt ( 3 ) ) ;
fos . write ( rs . getBytes ( 2 ) ) ;
}
migrated + + ;
}
}
2018-08-10 20:23:38 +02:00
return new MigrationResult ( " Migrated " + migrated + " backpacks from " + oldDb . getClass ( ) . getSimpleName ( ) + " to Files. " , MigrationResult . MigrationResultType . SUCCESS ) ;
2018-07-25 23:27:19 +02:00
}
}