mirror of
https://github.com/magefree/mage.git
synced 2026-01-09 12:22:10 -08:00
* Reworked DB comparison between client and server.
This commit is contained in:
parent
05a4a99ec6
commit
6ef8b4f976
12 changed files with 346 additions and 177 deletions
|
|
@ -38,7 +38,6 @@ import com.j256.ormlite.support.DatabaseConnection;
|
|||
import com.j256.ormlite.table.TableUtils;
|
||||
import java.io.File;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
|
@ -58,7 +57,10 @@ public enum CardRepository {
|
|||
|
||||
private static final String JDBC_URL = "jdbc:h2:file:./db/cards.h2;AUTO_SERVER=TRUE";
|
||||
private static final String VERSION_ENTITY_NAME = "card";
|
||||
// raise this if db structure was changed
|
||||
private static final long CARD_DB_VERSION = 36;
|
||||
// raise this if new cards were added to the server
|
||||
private static final long CARD_CONTENT_VERSION = 1;
|
||||
|
||||
private final Random random = new Random();
|
||||
private Dao<CardInfo, Object> cardDao;
|
||||
|
|
@ -277,6 +279,29 @@ public enum CardRepository {
|
|||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public long getContentVersionFromDB() {
|
||||
try {
|
||||
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
|
||||
return RepositoryUtil.getDatabaseVersion(connectionSource, VERSION_ENTITY_NAME + "Content");
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setContentVersion(long version) {
|
||||
try {
|
||||
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
|
||||
RepositoryUtil.updateVersion(connectionSource, VERSION_ENTITY_NAME + "Content", version);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public long getContentVersionConstant() {
|
||||
return CARD_CONTENT_VERSION;
|
||||
}
|
||||
|
||||
public void closeDB() {
|
||||
try {
|
||||
DatabaseConnection conn = cardDao.getConnectionSource().getReadWriteConnection();
|
||||
|
|
|
|||
|
|
@ -25,14 +25,21 @@
|
|||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
package mage.cards.repository;
|
||||
|
||||
import mage.cards.*;
|
||||
import mage.util.ClassScanner;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import mage.cards.Card;
|
||||
import mage.cards.CardImpl;
|
||||
import mage.cards.ExpansionSet;
|
||||
import mage.cards.Sets;
|
||||
import mage.cards.SplitCard;
|
||||
import mage.cards.repository.CardInfo;
|
||||
import mage.cards.repository.CardRepository;
|
||||
import mage.cards.repository.ExpansionInfo;
|
||||
import mage.cards.repository.ExpansionRepository;
|
||||
import mage.util.ClassScanner;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -57,6 +64,7 @@ public class CardScanner {
|
|||
packages.add(set.getPackageName());
|
||||
ExpansionRepository.instance.add(new ExpansionInfo(set));
|
||||
}
|
||||
ExpansionRepository.instance.setContentVersion(CardRepository.instance.getContentVersionConstant());
|
||||
|
||||
for (Class c : ClassScanner.findClasses(packages, CardImpl.class)) {
|
||||
if (!CardRepository.instance.cardExists(c.getCanonicalName())) {
|
||||
|
|
@ -75,6 +83,7 @@ public class CardScanner {
|
|||
logger.info("Cards need storing in DB: " + cardsToAdd.size());
|
||||
CardRepository.instance.addCards(cardsToAdd);
|
||||
}
|
||||
CardRepository.instance.setContentVersion(CardRepository.instance.getContentVersionConstant());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public enum ExpansionRepository {
|
|||
private static final String JDBC_URL = "jdbc:h2:file:./db/cards.h2;AUTO_SERVER=TRUE";
|
||||
private static final String VERSION_ENTITY_NAME = "expansion";
|
||||
private static final long EXPANSION_DB_VERSION = 3;
|
||||
private static final long EXPANSION_CONTENT_VERSION = 1;
|
||||
|
||||
private Dao<ExpansionInfo, Object> expansionDao;
|
||||
|
||||
|
|
@ -34,7 +35,7 @@ public enum ExpansionRepository {
|
|||
}
|
||||
try {
|
||||
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
|
||||
boolean obsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, EXPANSION_DB_VERSION);
|
||||
boolean obsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, 0);
|
||||
|
||||
if (obsolete) {
|
||||
TableUtils.dropTable(connectionSource, ExpansionInfo.class, true);
|
||||
|
|
@ -55,7 +56,7 @@ public enum ExpansionRepository {
|
|||
}
|
||||
|
||||
public List<String> getSetCodes() {
|
||||
List<String> setCodes = new ArrayList<String>();
|
||||
List<String> setCodes = new ArrayList<>();
|
||||
try {
|
||||
List<ExpansionInfo> expansions = expansionDao.queryForAll();
|
||||
for (ExpansionInfo expansion : expansions) {
|
||||
|
|
@ -80,7 +81,7 @@ public enum ExpansionRepository {
|
|||
}
|
||||
|
||||
public List<ExpansionInfo> getSetsWithBasicLandsByReleaseDate() {
|
||||
List<ExpansionInfo> sets = new LinkedList<ExpansionInfo>();
|
||||
List<ExpansionInfo> sets = new LinkedList<>();
|
||||
try {
|
||||
QueryBuilder<ExpansionInfo, Object> qb = expansionDao.queryBuilder();
|
||||
qb.orderBy("releaseDate", false);
|
||||
|
|
@ -122,6 +123,29 @@ public enum ExpansionRepository {
|
|||
return expansionDao.queryForAll();
|
||||
} catch (SQLException ex) {
|
||||
}
|
||||
return new ArrayList<ExpansionInfo>();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public long getContentVersionFromDB() {
|
||||
try {
|
||||
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
|
||||
return RepositoryUtil.getDatabaseVersion(connectionSource, VERSION_ENTITY_NAME + "Content");
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setContentVersion(long version) {
|
||||
try {
|
||||
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
|
||||
RepositoryUtil.updateVersion(connectionSource, VERSION_ENTITY_NAME + "Content", version);
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public long getContentVersionConstant() {
|
||||
return EXPANSION_CONTENT_VERSION;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@ package mage.cards.repository;
|
|||
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
import com.j256.ormlite.dao.DaoManager;
|
||||
import com.j256.ormlite.stmt.DeleteBuilder;
|
||||
import com.j256.ormlite.stmt.QueryBuilder;
|
||||
import com.j256.ormlite.stmt.SelectArg;
|
||||
import com.j256.ormlite.stmt.Where;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import com.j256.ormlite.table.TableUtils;
|
||||
import java.sql.SQLException;
|
||||
|
|
@ -31,4 +33,38 @@ public class RepositoryUtil {
|
|||
}
|
||||
return dbVersions.isEmpty();
|
||||
}
|
||||
|
||||
public static void updateVersion(ConnectionSource connectionSource, String entityName, long version) throws SQLException {
|
||||
TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class);
|
||||
Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
|
||||
|
||||
QueryBuilder<DatabaseVersion, Object> queryBuilder = dbVersionDao.queryBuilder();
|
||||
queryBuilder.where().eq("entity", new SelectArg(entityName));
|
||||
List<DatabaseVersion> dbVersions = dbVersionDao.query(queryBuilder.prepare());
|
||||
|
||||
if (!dbVersions.isEmpty()) {
|
||||
DeleteBuilder<DatabaseVersion, Object> deleteBuilder = dbVersionDao.deleteBuilder();
|
||||
deleteBuilder.where().eq("entity", new SelectArg(entityName));
|
||||
deleteBuilder.delete();
|
||||
}
|
||||
DatabaseVersion databaseVersion = new DatabaseVersion();
|
||||
databaseVersion.setEntity(entityName);
|
||||
databaseVersion.setVersion(version);
|
||||
dbVersionDao.create(databaseVersion);
|
||||
}
|
||||
|
||||
public static long getDatabaseVersion(ConnectionSource connectionSource, String entityName) throws SQLException {
|
||||
Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
|
||||
|
||||
QueryBuilder<DatabaseVersion, Object> queryBuilder = dbVersionDao.queryBuilder();
|
||||
queryBuilder.where().eq("entity", new SelectArg(entityName));
|
||||
List<DatabaseVersion> dbVersions = dbVersionDao.query(queryBuilder.prepare());
|
||||
if (dbVersions.isEmpty()) {
|
||||
return 0;
|
||||
} else {
|
||||
return dbVersions.get(0). getVersion();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue