1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
package com.calculateur.warhammer.create.database.sql;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.calculateur.warhammer.base.bdd.IExecuteSQL;
import com.calculateur.warhammer.base.exception.DAOException;
import com.calculateur.warhammer.base.server.IDatabaseConfiguration;
public class ExecuteFichiersSQL implements IExecuteSQL {
private static final Logger LOGGER = Logger.getLogger(ExecuteFichiersSQL.class.getName());
private static final String SEPARATOR_SQL_FILE = "_";
private static final String SQL_COMMENTAIRE = "--";
private static final int INDEX_NUMERO = 0;
private final File folderSQL;
private final IDatabaseConfiguration configuration;
private final Map<Integer, File> mapExecution;
public ExecuteFichiersSQL(File folderSQL, IDatabaseConfiguration configuration) {
this.folderSQL = folderSQL;
this.configuration = configuration;
mapExecution = new TreeMap<>();
}
@Override
public void executeSQL() throws DAOException {
try {
determinerOrdreExecution();
executeSQLFiles();
} catch (Exception e) {
throw new DAOException(e);
}
}
private void determinerOrdreExecution() {
String fileName;
String[] tab;
Integer order;
for (File sqlFile : folderSQL.listFiles()) {
fileName = sqlFile.getName();
tab = fileName.split(SEPARATOR_SQL_FILE);
order = Integer.parseInt(tab[INDEX_NUMERO]);
mapExecution.put(order, sqlFile);
}
}
private void executeSQLFiles() throws ClassNotFoundException, SQLException, IOException {
Class.forName(configuration.getClassDriver());
try (Connection connection = DriverManager.getConnection(configuration.getJDBCUrl(), configuration.getUser(),
configuration.getPassword())) {
for (Entry<Integer, File> entry : mapExecution.entrySet()) {
LOGGER.info("Execution ficnier n°" + entry.getKey() + " " + entry.getValue().getAbsolutePath());
executeSQLFile(connection, entry.getValue());
LOGGER.info("Le ficnier n°" + entry.getKey() + " " + entry.getValue().getAbsolutePath()+" a été executé entièrement");
}
}
}
private void executeSQLFile(Connection connection, File file) throws IOException, SQLException {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String sql;
while ((sql = br.readLine()) != null) {
if(!sql.isEmpty() && !sql.startsWith(SQL_COMMENTAIRE)) {
executeUpdate(connection, sql);
}
}
}
}
private void executeUpdate(Connection connection,String sql)throws SQLException{
try(PreparedStatement ps =connection.prepareStatement(sql)){
LOGGER.log(Level.INFO,"Execute SQL : {0}",sql);
ps.executeUpdate();
}
}
} |
Partager