2018-10-19 23:43:36 +00:00
package xyz.etztech.mixtape ;
import org.bukkit.plugin.java.JavaPlugin ;
import xyz.etztech.core.db.DataSource ;
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.util.ArrayList ;
import java.util.List ;
public class Database extends DataSource {
public enum AliasType {
CHAT , COMMAND
}
public Database ( JavaPlugin plugin ) {
super ( plugin ) ;
create ( ) ;
}
private void create ( ) {
2022-02-21 05:50:44 +00:00
try ( Connection connection = getConnection ( ) ) {
2018-10-20 18:22:32 +00:00
final String CREATE = "CREATE TABLE IF NOT EXISTS `aliases` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `uuid` VARCHAR(36) NOT NULL, `from` VARCHAR(200) NOT NULL, `to` VARCHAR(200) NOT NULL, `type` VARCHAR(7) NOT NULL );" ;
2018-10-19 23:43:36 +00:00
PreparedStatement pst = connection . prepareStatement ( CREATE ) ;
pst . execute ( ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
}
public void insert ( String uuid , String from , String to , AliasType type ) {
2022-02-21 05:50:44 +00:00
try ( Connection connection = getConnection ( ) ) {
2018-10-19 23:43:36 +00:00
int existingId = exists ( uuid , from ) ;
if ( existingId > 0 ) {
delete ( existingId ) ;
}
final String MERGE = "INSERT INTO `aliases` (`uuid`, `from`, `to`, `type`) VALUES (?, ?, ?, ?);" ;
PreparedStatement pst = connection . prepareStatement ( MERGE ) ;
pst . setString ( 1 , uuid ) ;
pst . setString ( 2 , from . toLowerCase ( ) ) ;
pst . setString ( 3 , to ) ;
pst . setString ( 4 , type . name ( ) ) ;
pst . executeUpdate ( ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
}
public void purge ( String uuid ) {
2022-02-21 05:50:44 +00:00
try ( Connection connection = getConnection ( ) ) {
2018-10-19 23:43:36 +00:00
final String PURGE = "DELETE FROM `aliases` WHERE `uuid` = ?;" ;
PreparedStatement pst = connection . prepareStatement ( PURGE ) ;
pst . setString ( 1 , uuid ) ;
pst . executeUpdate ( ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
}
public List < Alias > getAliases ( String uuid ) {
List < Alias > results = new ArrayList < > ( ) ;
2022-02-21 05:50:44 +00:00
try ( Connection connection = getConnection ( ) ) {
2018-10-19 23:43:36 +00:00
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ?;" ;
PreparedStatement pst = connection . prepareStatement ( SELECT ) ;
pst . setString ( 1 , uuid ) ;
ResultSet rs = pst . executeQuery ( ) ;
while ( rs . next ( ) ) {
Alias alias = new Alias ( ) ;
alias . setUuid ( uuid ) ;
alias . setFrom ( rs . getString ( "from" ) ) ;
alias . setTo ( rs . getString ( "to" ) ) ;
alias . setType ( AliasType . valueOf ( rs . getString ( "type" ) ) ) ;
results . add ( alias ) ;
}
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
return results ;
}
2018-10-20 18:22:32 +00:00
public List < Alias > getAliases ( String uuid , AliasType type ) {
List < Alias > results = new ArrayList < > ( ) ;
2022-02-21 05:50:44 +00:00
try ( Connection connection = getConnection ( ) ) {
2018-10-20 18:22:32 +00:00
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ? AND `type` = ?;" ;
PreparedStatement pst = connection . prepareStatement ( SELECT ) ;
pst . setString ( 1 , uuid ) ;
pst . setString ( 2 , type . name ( ) ) ;
ResultSet rs = pst . executeQuery ( ) ;
while ( rs . next ( ) ) {
Alias alias = new Alias ( ) ;
alias . setUuid ( uuid ) ;
alias . setFrom ( rs . getString ( "from" ) ) ;
alias . setTo ( rs . getString ( "to" ) ) ;
alias . setType ( AliasType . valueOf ( rs . getString ( "type" ) ) ) ;
results . add ( alias ) ;
}
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
return results ;
}
public Alias getAlias ( String uuid , String from ) {
2022-02-21 05:50:44 +00:00
try ( Connection connection = getConnection ( ) ) {
2018-10-20 18:22:32 +00:00
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ? AND `from` = ?;" ;
PreparedStatement pst = connection . prepareStatement ( SELECT ) ;
pst . setString ( 1 , uuid ) ;
pst . setString ( 2 , from ) ;
ResultSet rs = pst . executeQuery ( ) ;
if ( rs . next ( ) ) {
Alias alias = new Alias ( ) ;
alias . setUuid ( uuid ) ;
alias . setFrom ( rs . getString ( "from" ) ) ;
alias . setTo ( rs . getString ( "to" ) ) ;
alias . setType ( AliasType . valueOf ( rs . getString ( "type" ) ) ) ;
return alias ;
}
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
return null ;
}
2018-10-19 23:43:36 +00:00
public int exists ( String uuid , String from ) {
2022-02-21 05:50:44 +00:00
try ( Connection connection = getConnection ( ) ) {
2018-10-19 23:43:36 +00:00
final String SELECT = "SELECT `id` FROM `aliases` WHERE `uuid` = ? AND `from` = ?;" ;
PreparedStatement pst = connection . prepareStatement ( SELECT ) ;
pst . setString ( 1 , uuid ) ;
pst . setString ( 2 , from . toLowerCase ( ) ) ;
ResultSet rs = pst . executeQuery ( ) ;
if ( rs . next ( ) ) {
return rs . getInt ( "id" ) ;
}
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
return 0 ;
}
public void delete ( int id ) {
2022-02-21 05:50:44 +00:00
try ( Connection connection = getConnection ( ) ) {
2018-10-19 23:43:36 +00:00
final String DELETE = "DELETE FROM `aliases` WHERE `id` = ?;" ;
PreparedStatement pst = connection . prepareStatement ( DELETE ) ;
pst . setInt ( 1 , id ) ;
pst . executeUpdate ( ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
}
}