-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert tables from MyISAM to InnoDB
Fixed #712
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/me/confuser/banmanager/storage/mysql/ConvertMyISAMToInnoDb.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package me.confuser.banmanager.storage.mysql; | ||
|
||
import com.j256.ormlite.field.SqlType; | ||
import com.j256.ormlite.stmt.StatementBuilder; | ||
import com.j256.ormlite.support.CompiledStatement; | ||
import com.j256.ormlite.support.ConnectionSource; | ||
import com.j256.ormlite.support.DatabaseConnection; | ||
import com.j256.ormlite.support.DatabaseResults; | ||
import com.j256.ormlite.table.DatabaseTableConfig; | ||
import me.confuser.banmanager.BanManager; | ||
|
||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ConvertMyISAMToInnoDb { | ||
private BanManager plugin = BanManager.getPlugin(); | ||
|
||
public ConvertMyISAMToInnoDb(ConnectionSource db, HashMap<String, DatabaseTableConfig<?>> tables) { | ||
DatabaseConnection connection = null; | ||
|
||
try { | ||
connection = db.getReadWriteConnection(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
for (Map.Entry<String, DatabaseTableConfig<?>> entry : tables.entrySet()) { | ||
final DatabaseResults result; | ||
final String table = entry.getValue().getTableName(); | ||
|
||
try { | ||
CompiledStatement statement = connection.compileStatement("SHOW TABLE STATUS WHERE Name = ?", StatementBuilder | ||
.StatementType.SELECT, null, DatabaseConnection.DEFAULT_RESULT_FLAGS); | ||
|
||
statement.setObject(0, table, SqlType.STRING); | ||
|
||
result = statement.runQuery(null); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
continue; | ||
} | ||
|
||
try { | ||
while (result.next()) { | ||
final String engine = result.getString(1); | ||
|
||
if (engine.equals("MyISAM")) { | ||
plugin.getLogger().info("Converting " + entry.getKey() + " table from MyISAM to InnoDB"); | ||
|
||
connection.executeStatement("ALTER TABLE " + table + " ENGINE=InnoDB;", DatabaseConnection.DEFAULT_RESULT_FLAGS); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
result.closeQuietly(); | ||
} | ||
|
||
} | ||
|
||
connection.closeQuietly(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters