|
|
@@ -0,0 +1,411 @@
|
|
|
+package data;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.AbstractMap;
|
|
|
+import java.util.AbstractMap.SimpleEntry;
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+import com.google.common.base.Strings;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+
|
|
|
+import mysql.Mysql;
|
|
|
+import objects.OverUnder;
|
|
|
+import objects.SoccerMatch;
|
|
|
+import objects.Team;
|
|
|
+import objects.TeamResults;
|
|
|
+import objects.TeamStanding;
|
|
|
+
|
|
|
+public class GuiMysql extends Mysql {
|
|
|
+
|
|
|
+ private static final BigDecimal INCREMENT = new BigDecimal(0.2);
|
|
|
+ private static final GuiMysql instance = new GuiMysql();
|
|
|
+ private final Connection conn;
|
|
|
+
|
|
|
+
|
|
|
+ private GuiMysql() {
|
|
|
+ super();
|
|
|
+ conn = this.getConnection();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static GuiMysql getInstance() {
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<SimpleEntry<Integer, String>> getSports() {
|
|
|
+
|
|
|
+ final ArrayList<AbstractMap.SimpleEntry<Integer, String>> sports = Lists.newArrayList();
|
|
|
+ try {
|
|
|
+ final String sql = "SELECT id, name FROM Sport";
|
|
|
+ PreparedStatement stmt;
|
|
|
+ stmt = conn.prepareStatement(sql);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ final SimpleEntry<Integer, String> entry = new SimpleEntry<>(rs.getInt("id"), rs.getString("name"));
|
|
|
+ sports.add(entry);
|
|
|
+ }
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return sports;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<SimpleEntry<Integer, String>> getCountries() {
|
|
|
+
|
|
|
+ final ArrayList<AbstractMap.SimpleEntry<Integer, String>> countries = Lists.newArrayList();
|
|
|
+ try {
|
|
|
+ final String sql = "SELECT id, name FROM Country";
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ final SimpleEntry<Integer, String> entry = new SimpleEntry<>(rs.getInt("id"), rs.getString("name"));
|
|
|
+ countries.add(entry);
|
|
|
+ }
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return countries;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<SimpleEntry<Integer, String>> getLeagues(int sportId, int countryId) {
|
|
|
+
|
|
|
+ final ArrayList<AbstractMap.SimpleEntry<Integer, String>> leagues = Lists.newArrayList();
|
|
|
+ try {
|
|
|
+ final String sql = "SELECT id, name FROM League WHERE sportId = ? AND countryId = ? ORDER BY name ASC";
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+ stmt.setInt(1, sportId);
|
|
|
+ stmt.setInt(2, countryId);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ final SimpleEntry<Integer, String> entry = new SimpleEntry<>(rs.getInt("id"), rs.getString("name"));
|
|
|
+ leagues.add(entry);
|
|
|
+ }
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return leagues;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<SoccerMatch> getTodaysMatches(String sportResultTable, int sportId) {
|
|
|
+ final ArrayList<SoccerMatch> matches = Lists.newArrayList();
|
|
|
+ final String sql = "SELECT res.id, homeTeam, awayTeam, homeScore, awayScore, overtime, odds1, oddsX, odds2, gameDate, season, res.leagueId, res.countryId, " +
|
|
|
+ "hTeam.name as homeTeamName, aTeam.name as awayTeamName, " +
|
|
|
+ "league.name as leagueName, " +
|
|
|
+ "country.name as countryName " +
|
|
|
+ "FROM " + sportResultTable + " as res " +
|
|
|
+ "Join Team as hTeam ON res.homeTeam = hTeam.id " +
|
|
|
+ "Join Team as aTeam ON res.awayTeam = aTeam.id " +
|
|
|
+ "Join League as league ON res.leagueId = league.id " +
|
|
|
+ "Join Country as country ON res.countryId = country.id " +
|
|
|
+ "where homeScore = -1 AND DATE(gameDate) = DATE(now()) AND league.name NOT LIKE '%cup%' AND league.name NOT LIKE '%group%'";
|
|
|
+
|
|
|
+ try {
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ final SoccerMatch sm = new SoccerMatch();
|
|
|
+ final Team homeTeam = new Team();
|
|
|
+ final Team awayTeam = new Team();
|
|
|
+
|
|
|
+ homeTeam.setTeamId(rs.getInt("homeTeam"));
|
|
|
+ awayTeam.setTeamId(rs.getInt("awayTeam"));
|
|
|
+ homeTeam.setTeamName(rs.getString("homeTeamName"));
|
|
|
+ awayTeam.setTeamName(rs.getString("awayTeamName"));
|
|
|
+ homeTeam.setTeamLeagueId(rs.getInt("leagueId"));
|
|
|
+ awayTeam.setTeamLeagueId(rs.getInt("leagueId"));
|
|
|
+ homeTeam.setTeamLeague(rs.getString("leagueName"));
|
|
|
+ awayTeam.setTeamLeague(rs.getString("leagueName"));
|
|
|
+ homeTeam.setCountryId(rs.getInt("countryId"));
|
|
|
+ awayTeam.setCountryId(rs.getInt("countryId"));
|
|
|
+ homeTeam.setCountryName(rs.getString("countryName"));
|
|
|
+ awayTeam.setCountryName(rs.getString("countryName"));
|
|
|
+
|
|
|
+ sm.setAwayScore(rs.getInt("awayScore"));
|
|
|
+ sm.setHomeScore(rs.getInt("homeScore"));
|
|
|
+ sm.setHomeTeam(homeTeam);
|
|
|
+ sm.setAwayTeam(awayTeam);
|
|
|
+ sm.setMatchId(rs.getInt("id"));
|
|
|
+ sm.setOdds1(rs.getFloat("odds1"));
|
|
|
+ sm.setOddsX(rs.getFloat("oddsX"));
|
|
|
+ sm.setOdds2(rs.getFloat("odds2"));
|
|
|
+ sm.setGameDate(LocalDateTime.parse(rs.getString("gameDate")));
|
|
|
+
|
|
|
+ matches.add(sm);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return matches;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<Float> getAvgHomeScore(int teamId) {
|
|
|
+ final ArrayList<Float> returnValue = Lists.newArrayList();
|
|
|
+ final String sql = "SELECT AVG(homeScore) as avgScored, AVG(awayScore) as avgConceded FROM SoccerResults WHERE homeScore != -1 && homeTeam = ?";
|
|
|
+ try {
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+ stmt.setInt(1, teamId);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ returnValue.add(rs.getFloat("avgScored"));
|
|
|
+ returnValue.add(rs.getFloat("avgConceded"));
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return returnValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<Float> getAvgAwayScore(int teamId) {
|
|
|
+ final ArrayList<Float> returnValue = Lists.newArrayList();
|
|
|
+ final String sql = "SELECT AVG(homeScore) as avgConceded, AVG(awayScore) as avgScored FROM SoccerResults WHERE awayScore != -1 && awayTeam = ?";
|
|
|
+ try {
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+ stmt.setInt(1, teamId);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ returnValue.add(rs.getFloat("avgScored"));
|
|
|
+ returnValue.add(rs.getFloat("avgConceded"));
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return returnValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<Float> getLeagueAvarages(int leagueId, int countryId) {
|
|
|
+ final ArrayList<Float> returnValue = Lists.newArrayList();
|
|
|
+ final String sql = "SELECT AVG(homeScore) avgHomeScore, AVG(awayScore) as avgAwayScore"
|
|
|
+ + " FROM SoccerResults WHERE leagueId = ? AND countryId = ?";
|
|
|
+
|
|
|
+ final String goalsSql = "SELECT (homeScore + awayScore) as totalGoals, count(*) as count FROM SoccerResults WHERE leagueId = ? AND countryId = ? AND season = ? GROUP BY totalGoals ORDER BY totalGoals asc";
|
|
|
+ try {
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+ stmt.setInt(1, leagueId);
|
|
|
+ stmt.setInt(2, countryId);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ returnValue.add(rs.getFloat("avgHomeScore"));
|
|
|
+ returnValue.add(rs.getFloat("avgAwayScore"));
|
|
|
+ }
|
|
|
+
|
|
|
+ final PreparedStatement goalStmt = conn.prepareStatement(goalsSql);
|
|
|
+ goalStmt.setInt(1, leagueId);
|
|
|
+ goalStmt.setInt(2, countryId);
|
|
|
+ goalStmt.setString(3, getLastSeason(countryId, leagueId));
|
|
|
+
|
|
|
+ final ResultSet goalRs = goalStmt.executeQuery();
|
|
|
+ while (goalRs.next()) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return returnValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<OverUnder> getStatsOverUnder(int leagueId) {
|
|
|
+
|
|
|
+ final DecimalFormat df = new DecimalFormat("##.##");
|
|
|
+ df.setRoundingMode(RoundingMode.HALF_DOWN);
|
|
|
+ final ArrayList<OverUnder> result = Lists.newArrayList();
|
|
|
+
|
|
|
+ final String sql = "SELECT ((sHome.avgScored + sAway.avgConceded) - (sAway.avgScored + sHome.avgConceded)) as diff, (homeScore + awayScore) as numGoals "
|
|
|
+ + "FROM SoccerResults "
|
|
|
+ + "INNER JOIN (SELECT homeTeam, AVG(homeScore) as avgScored, AVG(awayScore) as avgConceded FROM SoccerResults WHERE homeScore != -1 && homeTeam = SoccerResults.homeTeam GROUP BY homeTeam) as sHome ON SoccerResults.homeTeam = sHome.homeTeam "
|
|
|
+ + "INNER JOIN (SELECT awayTeam, AVG(homeScore) as avgConceded, AVG(awayScore) as avgScored FROM SoccerResults WHERE awayScore != -1 && awayTeam = SoccerResults.awayTeam GROUP BY awayTeam) as sAway ON SoccerResults.awayTeam = sAway.awayTeam "
|
|
|
+ + "WHERE homeScore != -1 AND awayScore != -1 AND leagueId = ? "
|
|
|
+ + "ORDER BY diff ASC";
|
|
|
+ try {
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+ stmt.setInt(1, leagueId);
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ final float diff = rs.getFloat("diff");
|
|
|
+ final int numGoals = rs.getInt("numGoals");
|
|
|
+ // final Float formatted = Float.valueOf(df.format(diff));
|
|
|
+ // final Float formatted = BigDecimal.valueOf(diff).setScale(1, BigDecimal.ROUND_HALF_DOWN).floatValue();
|
|
|
+ final Float formatted = round(new BigDecimal(diff), INCREMENT, RoundingMode.HALF_UP).floatValue();
|
|
|
+
|
|
|
+ final OverUnder entry = result.stream().filter(ou -> ou.getKey().compareTo(formatted) == 0).findFirst().orElse(new OverUnder(formatted));
|
|
|
+ entry.addGoalStat(numGoals);
|
|
|
+ result.add(entry);
|
|
|
+ }
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public BigDecimal round(BigDecimal value, BigDecimal increment,
|
|
|
+ RoundingMode roundingMode) {
|
|
|
+ if (increment.signum() == 0) {
|
|
|
+ // 0 increment does not make much sense, but prevent division by 0
|
|
|
+ return value;
|
|
|
+ } else {
|
|
|
+ final BigDecimal divided = value.divide(increment, 0, roundingMode);
|
|
|
+ final BigDecimal result = divided.multiply(increment);
|
|
|
+ return result.setScale(2, RoundingMode.HALF_UP);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BigDecimal getIncrement() {
|
|
|
+ return INCREMENT;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<TeamStanding> getLeagueTable(int leagueId, String season, int countryId) {
|
|
|
+ final ArrayList<TeamStanding> result = Lists.newArrayList();
|
|
|
+
|
|
|
+ final String sql = "SELECT teamName.name as teamName, count(*) played, count(case when homeScore > awayScore then 1 end) wins, "
|
|
|
+ + "count(case when awayScore> homeScore then 1 end) lost, "
|
|
|
+ + "count(case when homeScore = awayScore then 1 end) draws, "
|
|
|
+ + "sum(homeScore) homeScore, "
|
|
|
+ + "sum(awayScore) awayScore, "
|
|
|
+ + "sum(homeScore) - sum(awayScore) goal_diff, "
|
|
|
+ + "sum(case when homeScore > awayScore then 3 else 0 end + case "
|
|
|
+ + "WHEN homeScore = awayScore then 1 else 0 end) score, "
|
|
|
+ + "season FROM "
|
|
|
+ + "(select hometeam team, homeScore, awayScore, season, leagueId as league, countryId as country FROM SoccerResults "
|
|
|
+ + "union all SELECT awayteam, awayScore, homeScore, season, leagueId as league, countryId as country FROM SoccerResults) a "
|
|
|
+ + "INNER JOIN Team teamName ON team = teamName.id "
|
|
|
+ + "WHERE season = ? "
|
|
|
+ + "AND league = ? "
|
|
|
+ + "AND country = ? "
|
|
|
+ + "AND homeScore != -1 "
|
|
|
+ + "AND awayScore != -1 "
|
|
|
+ + "group by team "
|
|
|
+ + "order by score desc, goal_diff desc";
|
|
|
+ try {
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+
|
|
|
+ stmt.setString(1, season);
|
|
|
+ stmt.setInt(2, leagueId);
|
|
|
+ stmt.setInt(3, countryId);
|
|
|
+
|
|
|
+ final String tempSql = sql.replaceAll("\\?", "%s");
|
|
|
+ System.out.println(String.format(tempSql, season, leagueId, countryId));
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ final TeamStanding ts = new TeamStanding(rs.getString("teamName"), rs.getInt("wins"), rs.getInt("lost"), rs.getInt("draws"),
|
|
|
+ rs.getInt("score"), rs.getFloat("homeScore"), rs.getFloat("awayScore"), rs.getFloat("goal_diff"));
|
|
|
+ result.add(ts);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("Sql vid fel: " + sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean getParsingStarted(int countryId, int leagueId) {
|
|
|
+ boolean returnValue = false;
|
|
|
+ final String sql = "SELECT parsedYear FROM League WHERE id = ? AND countryId = ?";
|
|
|
+ try {
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+ stmt.setInt(1, leagueId);
|
|
|
+ stmt.setInt(2, countryId);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ final String parsedYear = rs.getString("parsedYear");
|
|
|
+ if (!Strings.isNullOrEmpty(parsedYear)) {
|
|
|
+ returnValue = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return returnValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getLastSeason(Integer countryId, Integer leagueId) {
|
|
|
+ String season = "";
|
|
|
+ final String sql = "SELECT season FROM SoccerResults WHERE leagueId = ? AND countryId = ? ORDER BY season DESC limit 1";
|
|
|
+ try {
|
|
|
+ final PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
+ stmt.setInt(1, leagueId);
|
|
|
+ stmt.setInt(2, countryId);
|
|
|
+
|
|
|
+ final ResultSet rs = stmt.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ season = rs.getString("season");
|
|
|
+ }
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return season;
|
|
|
+ }
|
|
|
+
|
|
|
+ public TeamResults getTeamResults(int teamId, int numResults, boolean isHomeTeam) {
|
|
|
+ final String sql;
|
|
|
+ final TeamResults tr = new TeamResults();
|
|
|
+ if (isHomeTeam) {
|
|
|
+ sql = "SELECT count(case when homeScore > awayScore then 1 end) wins, " +
|
|
|
+ "count(case when awayScore > homeScore then 1 end) lost, " +
|
|
|
+ "count(case when homeScore = awayScore then 1 end) draws " +
|
|
|
+ "FROM SoccerResults WHERE homeTeam = ? AND " +
|
|
|
+ "HomeScore >= 0 AND awayScore >= 0 LIMIT ?";
|
|
|
+ } else {
|
|
|
+ sql = "SELECT count(case when homeScore < awayScore then 1 end) wins, " +
|
|
|
+ "count(case when awayScore < homeScore then 1 end) lost, " +
|
|
|
+ "count(case when homeScore = awayScore then 1 end) draws " +
|
|
|
+ "FROM SoccerResults WHERE awayTeam = ? AND " +
|
|
|
+ "HomeScore >= 0 AND awayScore >= 0 LIMIT ?";
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ final PreparedStatement stat = conn.prepareStatement(sql);
|
|
|
+ stat.setInt(1, teamId);
|
|
|
+ stat.setInt(2, numResults);
|
|
|
+ final ResultSet rs = stat.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ final int draws = rs.getInt("draws");
|
|
|
+ final int wins = rs.getInt("wins");
|
|
|
+ final int lost = rs.getInt("lost");
|
|
|
+ tr.setDraws(draws);
|
|
|
+ tr.setWins(wins);
|
|
|
+ tr.setLosses(lost);
|
|
|
+ tr.setCount(wins+draws+lost);
|
|
|
+ }
|
|
|
+ } catch (final SQLException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return tr;
|
|
|
+ }
|
|
|
+}
|