Skip to content

Commit

Permalink
redesign database variable naming, add saving of pokedex lists
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Aug 2, 2024
1 parent 9c1714d commit 070763e
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@ public class DatabaseSetup {

public static final String CreateDexes = """
CREATE TABLE IF NOT EXISTS `dexes` (
`apiName` tinytext PRIMARY KEY NOT NULL,
`apiDexName` tinytext PRIMARY KEY NOT NULL,
`uiName` tinytext NOT NULL,
`apiUrl` tinytext NOT NULL
);""";

public static final String CreateDexPokemon = """
CREATE TABLE IF NOT EXISTS `dexPokemon` (
public static final String CreateNatDexToName = """
CREATE TABLE IF NOT EXISTS `natDexToName` (
`nationalDexNumber` int PRIMARY KEY NOT NULL,
`apiDexName` tinytext UNIQUE NOT NULL
`apiMonName` tinytext UNIQUE NOT NULL
);""";

public static final String CreatePokemonNamesToDex = """
CREATE TABLE IF NOT EXISTS `pokemonNamesToDex` (
`apiDexName` tinytext PRIMARY KEY NOT NULL,
public static final String CreateNameToNatDex = """
CREATE TABLE IF NOT EXISTS `nameToNatDex` (
`apiMonName` tinytext PRIMARY KEY NOT NULL,
`nationalDexNumber` int UNIQUE NOT NULL,
FOREIGN KEY (`apiDexName`) REFERENCES `dexPokemon` (`apiDexName`),
FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexPokemon` (`nationalDexNumber`)
FOREIGN KEY (`apiMonName`) REFERENCES `pokemonByDex` (`apiMonName`),
FOREIGN KEY (`nationalDexNumber`) REFERENCES `natDexToName` (`nationalDexNumber`)
);""";

public static final String CreatePokemonNamesByDex = """
CREATE TABLE IF NOT EXISTS `pokemonByDex` (
`apiDexName` tinytext NOT NULL,
`apiMonName` tinytext NOT NULL,
`dexNumber` int NOT NULL,
PRIMARY KEY(apiDexName, apiMonName),
FOREIGN KEY (`apiDexName`) REFERENCES `dexes` (`apiDexName`),
FOREIGN KEY (`apiMonName`) REFERENCES `natDexToName` (`apiMonName`)
);""";

public static final String CreateDexEntries = """
Expand All @@ -30,8 +40,8 @@ FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexPokemon` (`nationalDexNumber`)
`speciesUrl` tinytext NOT NULL,
`genus` tinytext NOT NULL,
`generation` int NOT NULL,
FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexPokemon` (`nationalDexNumber`),
FOREIGN KEY (`name`) REFERENCES `dexPokemon` (`apiDexName`)
FOREIGN KEY (`nationalDexNumber`) REFERENCES `natDexToName` (`nationalDexNumber`),
FOREIGN KEY (`name`) REFERENCES `natDexToName` (`apiMonName`)
);""";

public static final String CreateEggGroups = """
Expand All @@ -48,14 +58,15 @@ FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexEntries` (`nationalDexNumber`)
`regionText` tinytext NOT NULL,
PRIMARY KEY (`nationalDexNumber`, `region`),
FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexEntries` (`nationalDexNumber`),
FOREIGN KEY (`region`) REFERENCES `dexes` (`apiName`)
FOREIGN KEY (`region`) REFERENCES `dexes` (`apiDexName`)
);""";

public static String[] getStartupStatements() {
return new String[]{
CreateDexes,
CreateDexPokemon,
CreatePokemonNamesToDex,
CreateNatDexToName,
CreateNameToNatDex,
CreatePokemonNamesByDex,
CreateDexEntries,
CreateEggGroups,
CreateFlavorTexts
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/io/github/lucasstarsz/fxdex/model/JsonDexItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.json.JSONObject;

import java.sql.ResultSet;
import java.sql.SQLException;

public class JsonDexItem {

private final String apiDexName;
private final String apiMonName;
private final String apiDexUrl;
private final String uiName;

public JsonDexItem(JSONObject dex) {
this.apiDexName = dex.getString("name");
this.apiMonName = dex.getString("name");
this.apiDexUrl = dex.getString("url");
this.uiName = null;
}

public JsonDexItem(ResultSet dexSql) throws SQLException {
this.apiMonName = dexSql.getString("apiDexName");
this.uiName = dexSql.getString("uiName");
this.apiDexUrl = dexSql.getString("apiUrl");
}

public String getApiDexName() {
return apiDexName;
return apiMonName;
}

public String getApiDexUrl() {
return apiDexUrl;
}

public String getUiName() {
return uiName;
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
Expand All @@ -31,23 +46,24 @@ public boolean equals(Object other) {
JsonDexItem dexItem = (JsonDexItem) other;

return new EqualsBuilder()
.append(apiDexName, dexItem.apiDexName)
.append(apiMonName, dexItem.apiMonName)
.append(apiDexUrl, dexItem.apiDexUrl)
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.append(apiDexName)
.append(apiMonName)
.append(apiDexUrl)
.toHashCode();
}

@Override
public String toString() {
return new ToStringBuilder(this)
.append("apiDexName", apiDexName)
.append("apiMonName", apiMonName)
.append("uiName", uiName)
.append("apiDexUrl", apiDexUrl)
.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.json.JSONObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

public class JsonDexListItem {
private final int dexNumber;
private final String apiPokemonName;
Expand All @@ -15,6 +19,12 @@ public JsonDexListItem(JSONObject dexEntryFromInfoList) {
this.apiPokemonName = pokemonSpecies.getString("name");
}

public JsonDexListItem(ResultSet dexItemFromDatabase) throws SQLException {
this.apiPokemonName = Objects.requireNonNull(dexItemFromDatabase.getString("apiMonName"));
this.dexNumber = dexItemFromDatabase.getInt("dexNumber");
System.out.println(this);
}

public int getDexNumber() {
return dexNumber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
import java.util.List;

public interface DexInfoHandler {
void saveDexItems(List<JsonDexItem> jsonDexItems);
void saveDexMenuList(List<JsonDexItem> jsonDexItems);

void saveDexEntry(JsonDexEntryItem jsonDexEntry, String dexEntryLink);

JsonDexEntryItem loadDexEntry(int nationalDexNumber) throws SQLException;

void saveDexPokemon(List<JsonDexListItem> dexEntries);
void saveNatDexList(List<JsonDexListItem> dexList);

void saveDexList(String apiDexName, List<JsonDexListItem> dexList);

JsonDexItem loadDexItem(String apiDexName) throws SQLException;

int loadPokemonNumber(String apiPokemonName);

List<JsonDexListItem> loadDexList(String apiDexName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class SqlDexInfoHandler implements DexInfoHandler {

private static final String SaveDexPokemon = """
INSERT INTO dexPokemon(nationalDexNumber, apiDexName) VALUES (?, ?)
INSERT INTO natDexToName(nationalDexNumber, apiMonName) VALUES (?, ?)
ON CONFLICT(nationalDexNumber) DO NOTHING
""";

private static final String SaveDexPokemonName = """
INSERT INTO pokemonNamesToDex(apiDexName, nationalDexNumber) VALUES (?, ?)
ON CONFLICT(apiDexName) DO NOTHING
INSERT INTO nameToNatDex(apiMonName, nationalDexNumber) VALUES (?, ?)
ON CONFLICT(apiMonName) DO NOTHING
""";

private static final String SaveDexItems = """
Expand Down Expand Up @@ -60,46 +58,110 @@ INSERT INTO flavorTexts(nationalDexNumber, region, regionText) VALUES (?, ?, ?)
""";

private static final String LoadDexPokemonByName = """
SELECT * FROM pokemonNamesToDex WHERE pokemonNamesToDex.apiDexName=(?)
SELECT * FROM nameToNatDex WHERE nameToNatDex.apiMonName=(?)
""";

private static final String LoadDexList = """
SELECT * FROM pokemonByDex WHERE pokemonByDex.apiDexName=(?) ORDER BY pokemonByDex.dexNumber ASC
""";

private static final String LoadDexItem = """
SELECT * FROM dexes WHERE dexes.apiDexName=(?)
""";

@Override
public void saveDexList(String apiDexName, List<JsonDexListItem> dexList) {
String monByDexQuery = SqlDexInfoHandler.createInsertQuery(QueryType.SaveDexList, dexList.size());

try (var dbConnection = DriverManager.getConnection(FileLinks.JDBCConnectionUrl);
var monByDexStatement = dbConnection.prepareStatement(monByDexQuery)) {

int monByDexCount = QueryType.SaveDexList.getData().length;
for (int i = 1; i <= dexList.size() * monByDexCount; i += monByDexCount) {
int itemIndex = ((i + monByDexCount - 1) / monByDexCount) - 1;
JsonDexListItem dexEntryFromList = dexList.get(itemIndex);
monByDexStatement.setString(i, apiDexName);
monByDexStatement.setString(i + 1, dexEntryFromList.getApiPokemonName());
monByDexStatement.setInt(i + 2, dexEntryFromList.getDexNumber());
}

System.out.println("mons by dex list:\n" + monByDexStatement.toString());
monByDexStatement.executeUpdate();
} catch (SQLException e) {
UiService.createErrorAlert("Unable to save pok\u00e9mon", e).showAndWait();
}
}

@Override
public void saveDexPokemon(List<JsonDexListItem> dexEntries) {
String dexToNameQuery = SqlDexInfoHandler.createInsertQuery(QueryType.SaveDexToName, dexEntries.size());
String nameToDexQuery = SqlDexInfoHandler.createInsertQuery(QueryType.SaveNameToDex, dexEntries.size());
public void saveNatDexList(List<JsonDexListItem> dexList) {
String dexToNameQuery = SqlDexInfoHandler.createInsertQuery(QueryType.SaveDexToName, dexList.size());
String nameToDexQuery = SqlDexInfoHandler.createInsertQuery(QueryType.SaveNameToDex, dexList.size());

System.out.println(dexToNameQuery);
try (var dbConnection = DriverManager.getConnection(FileLinks.JDBCConnectionUrl);
var dexToNameStatement = dbConnection.prepareStatement(dexToNameQuery);
var nameToDexStatement = dbConnection.prepareStatement(nameToDexQuery)) {

int dexToNameCount = QueryType.SaveDexToName.getData().length;

for (int i = 1; i <= dexEntries.size() * dexToNameCount; i += dexToNameCount) {
for (int i = 1; i <= dexList.size() * dexToNameCount; i += dexToNameCount) {
int itemIndex = ((i + dexToNameCount - 1) / dexToNameCount) - 1;
JsonDexListItem dexEntryFromList = dexEntries.get(itemIndex);
JsonDexListItem dexEntryFromList = dexList.get(itemIndex);
dexToNameStatement.setInt(i, dexEntryFromList.getDexNumber());
dexToNameStatement.setString(i + 1, dexEntryFromList.getApiPokemonName());
}

System.out.println("dex to name list:\n" + dexToNameStatement.toString());
dexToNameStatement.executeUpdate();

int nameToDexCount = QueryType.SaveDexToName.getData().length;

System.out.println(nameToDexQuery);
for (int i = 1; i <= dexEntries.size() * nameToDexCount; i += nameToDexCount) {
int itemIndex = ((i + dexToNameCount - 1) / dexToNameCount) - 1;
JsonDexListItem dexEntryFromList = dexEntries.get(itemIndex);
for (int i = 1; i <= dexList.size() * nameToDexCount; i += nameToDexCount) {
int itemIndex = ((i + nameToDexCount - 1) / nameToDexCount) - 1;
JsonDexListItem dexEntryFromList = dexList.get(itemIndex);
nameToDexStatement.setString(i, dexEntryFromList.getApiPokemonName());
nameToDexStatement.setInt(i + 1, dexEntryFromList.getDexNumber());
}

System.out.println("name to dex list:\n" + nameToDexStatement.toString());
nameToDexStatement.executeUpdate();
} catch (SQLException e) {
UiService.createErrorAlert("Unable to save pok\u00e9mon", e).showAndWait();
}
}

@Override
public List<JsonDexListItem> loadDexList(String apiDexName) {
try (var dbConnection = DriverManager.getConnection(FileLinks.JDBCConnectionUrl);
var loadDexListStatement = dbConnection.prepareStatement(LoadDexList)) {
System.out.println("search for " + apiDexName);
loadDexListStatement.setString(1, apiDexName);
var result = loadDexListStatement.executeQuery();

List<JsonDexListItem> dexItems = new ArrayList<>();
System.out.println("finding...");
do {
System.out.println("found 1");
dexItems.add(new JsonDexListItem(result));
} while (result.next());

return dexItems;
} catch (SQLException e) {
UiService.createErrorAlert("Unable to load pok\u00e9mon", e).showAndWait();
}

return List.of();
}

@Override
public JsonDexItem loadDexItem(String apiDexName) throws SQLException {
try (var dbConnection = DriverManager.getConnection(FileLinks.JDBCConnectionUrl);
var loadDexNameStatement = dbConnection.prepareStatement(LoadDexItem)) {
System.out.println("search for " + apiDexName);
loadDexNameStatement.setString(1, apiDexName);
var result = loadDexNameStatement.executeQuery();

return new JsonDexItem(result);
}
}

@Override
public int loadPokemonNumber(String apiPokemonName) {
try (var dbConnection = DriverManager.getConnection(FileLinks.JDBCConnectionUrl);
Expand All @@ -117,7 +179,7 @@ public int loadPokemonNumber(String apiPokemonName) {
}

@Override
public void saveDexItems(List<JsonDexItem> jsonDexItems) {
public void saveDexMenuList(List<JsonDexItem> jsonDexItems) {
String saveDexItems = SqlDexInfoHandler.createInsertQuery(QueryType.SaveDexItems, jsonDexItems.size());
try (var dbConnection = DriverManager.getConnection(FileLinks.JDBCConnectionUrl);
var saveDexItemsStatement = dbConnection.prepareStatement(saveDexItems)) {
Expand All @@ -129,7 +191,7 @@ public void saveDexItems(List<JsonDexItem> jsonDexItems) {
int itemIndex = ((i + saveDexItemsCount - 1) / saveDexItemsCount) - 1;
JsonDexItem jsonDexItem = jsonDexItems.get(itemIndex);
saveDexItemsStatement.setString(i, jsonDexItem.getApiDexName());
saveDexItemsStatement.setString(i + 1, ApiConversionTables.DexNameMap.get(jsonDexItem.getApiDexName().toLowerCase()));
saveDexItemsStatement.setString(i + 1, Objects.requireNonNullElse(jsonDexItem.getUiName(), ApiConversionTables.DexNameMap.get(jsonDexItem.getApiDexName().toLowerCase())));
saveDexItemsStatement.setString(i + 2, jsonDexItem.getApiDexUrl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static io.github.lucasstarsz.fxdex.misc.ApiConversionTables.DexNameMap;

Expand All @@ -38,12 +39,18 @@ public DefaultUIService(JsonParserService jsonParserService, DexInfoHandler dexI
public List<MenuItem> createDexItems(JSONObject dexListJSON, ListProperty<Label> currentDexUi,
StringProperty currentDexName, DexService dexService) {
var jsonDexItems = jsonParserService.parseDexItems(dexListJSON);
dexInfoHandler.saveDexItems(jsonDexItems);
return createDexItems(jsonDexItems, currentDexUi, currentDexName, dexService);
}

@Override
public List<MenuItem> createDexItems(List<JsonDexItem> jsonDexItems, ListProperty<Label> currentDexUi,
StringProperty currentDexName, DexService dexService) {
dexInfoHandler.saveDexMenuList(jsonDexItems);

List<MenuItem> itemUiList = new ArrayList<>();
for (JsonDexItem item : jsonDexItems) {
String apiDexName = item.getApiDexName();
String uiDexName = DexNameMap.get(apiDexName);
String apiMonName = item.getApiDexName();
String uiDexName = Objects.requireNonNullElse(item.getUiName(), DexNameMap.get(apiMonName));

MenuItem dexItem = new MenuItem(uiDexName);
dexItem.setOnAction((event) -> dexService.loadDexList(
Expand Down
Loading

0 comments on commit 070763e

Please sign in to comment.