-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: Add an ORM to save homes #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
57a9f8a
update: implement ORM support with ORMLite for home storage and manag…
AxenoDev 1abcf86
📝 Add docstrings to `feat/add-orm`
coderabbitai[bot] 017a44c
Merge pull request #6 from AxenoDev/coderabbitai/docstrings/57a9f8a
AxenoDev c990926
update: remove default database credentials from config.yml
AxenoDev 5718e39
update: enhance database management with transaction support and impr…
AxenoDev ab8e35f
update: simplify table creation in DatabaseManager by removing redund…
AxenoDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
13 changes: 0 additions & 13 deletions
13
src/main/java/me/axeno/hommr/listeners/PlayerListener.java
This file was deleted.
Oops, something went wrong.
114 changes: 114 additions & 0 deletions
114
src/main/java/me/axeno/hommr/managers/DatabaseManager.java
This file contains hidden or 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,114 @@ | ||
| package me.axeno.hommr.managers; | ||
|
|
||
| import com.j256.ormlite.dao.Dao; | ||
| import com.j256.ormlite.dao.DaoManager; | ||
| import com.j256.ormlite.jdbc.JdbcConnectionSource; | ||
| import com.j256.ormlite.misc.TransactionManager; | ||
| import com.j256.ormlite.support.ConnectionSource; | ||
| import com.j256.ormlite.table.TableUtils; | ||
| import lombok.Getter; | ||
| import me.axeno.hommr.Hommr; | ||
| import me.axeno.hommr.models.Home; | ||
|
|
||
| import java.io.File; | ||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
| import java.util.concurrent.Callable; | ||
|
|
||
| public class DatabaseManager { | ||
|
|
||
| private ConnectionSource connectionSource; | ||
| @Getter | ||
| private Dao<Home, Integer> homeDao; | ||
|
|
||
| /** | ||
| * Set up the database connection and DAO for Home entities based on configuration. | ||
| * | ||
| * Initializes the plugin data folder if necessary, creates a JDBC connection source | ||
| * (MySQL when `database.type` is `"mysql"`, otherwise SQLite using a `homes.db` file), | ||
| * and creates a Dao<Home, Integer> for accessing Home records. Ensures the Home table | ||
| * exists in the database; failures during folder creation, table creation, or overall | ||
| * initialization are logged. | ||
| */ | ||
| public void init() { | ||
| try { | ||
| File dataFolder = Hommr.getInstance().getDataFolder(); | ||
| if (!dataFolder.exists()) { | ||
| boolean created = dataFolder.mkdirs(); | ||
| if (!created) { | ||
| Hommr.getInstance().getLogger().warning("Could not create plugin data folder: " + dataFolder.getAbsolutePath()); | ||
| } | ||
| } | ||
|
|
||
| String type = Hommr.getInstance().getConfig().getString("database.type", "sqlite").toLowerCase(); | ||
| String databaseUrl; | ||
| String username = null; | ||
| String password = null; | ||
|
|
||
| if (type.equals("mysql")) { | ||
| databaseUrl = Hommr.getInstance().getConfig().getString("database.connection.url"); | ||
| username = Hommr.getInstance().getConfig().getString("database.connection.username"); | ||
| password = Hommr.getInstance().getConfig().getString("database.connection.password"); | ||
| if (databaseUrl == null || databaseUrl.isEmpty()) { | ||
| throw new IllegalArgumentException("MySQL database URL is required but not configured. Please check your config.yml."); | ||
| } | ||
| connectionSource = new JdbcConnectionSource(databaseUrl, username, password); | ||
| } else { | ||
| databaseUrl = "jdbc:sqlite:" + new File(dataFolder, "homes.db").getAbsolutePath(); | ||
| connectionSource = new JdbcConnectionSource(databaseUrl); | ||
| } | ||
|
|
||
| homeDao = DaoManager.createDao(connectionSource, Home.class); | ||
|
|
||
| TableUtils.createTableIfNotExists(connectionSource, Home.class); | ||
|
|
||
| } catch (SQLException e) { | ||
| Hommr.getInstance().getLogger().log(java.util.logging.Level.SEVERE, "Failed to initialize database", e); | ||
| throw new RuntimeException("Database initialization failed", e); | ||
| } | ||
| } | ||
|
AxenoDev marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Closes the underlying database connection source and releases related resources. | ||
| * | ||
| * If an error occurs while closing, the exception is caught and a warning is logged. | ||
| */ | ||
| public void close() { | ||
| if (connectionSource != null) { | ||
| try { | ||
| connectionSource.close(); | ||
| connectionSource = null; | ||
| } catch (Exception e) { | ||
| Hommr.getInstance().getLogger().log(java.util.logging.Level.WARNING, "Error closing database connection", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve all Home records from the database. | ||
| * | ||
| * @return a list of all Home objects | ||
| * @throws SQLException if a database access error occurs while querying for homes | ||
| */ | ||
| public List<Home> getAllHomes() throws SQLException { | ||
| return homeDao.queryForAll(); | ||
| } | ||
|
|
||
| /** | ||
| * Replaces all stored Home records with the provided list. | ||
| * | ||
| * Clears the Home table and inserts the given homes in a single batch operation. | ||
| * | ||
| * @param homes the list of Home objects to persist (may be empty) | ||
| * @throws SQLException if an error occurs while clearing the table or saving records | ||
| */ | ||
| public void saveAllHomes(List<Home> homes) throws SQLException { | ||
| TransactionManager.callInTransaction(connectionSource, () -> { | ||
| TableUtils.clearTable(connectionSource, Home.class); | ||
| for (Home home : homes) { | ||
| homeDao.create(home); | ||
| } | ||
| return null; | ||
| }); | ||
| } | ||
|
AxenoDev marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.