Class SqliteWorldRepository
java.lang.Object
za.co.wethinkcode.robots.server.persistence.SqliteWorldRepository
- All Implemented Interfaces:
WorldRepository
SQLite implementation of the WorldRepository.
Acts as a facade orchestrating transactions across three separate DAOs
(SqliteSnapshotDao, SqliteObjectDao, SqliteRobotDao). This ensures strict
separation of concerns and adheres to Single Responsibility OOP principles.
Opens a new database connection for each operation and ensures foreign
keys are explicitly enabled for ON DELETE CASCADE features to function.
-
Constructor Summary
ConstructorsConstructorDescriptionSqliteWorldRepository(String databaseFilePath) Constructs a new repository and ensures the v1 database schema is created. -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks if a world with the specified name exists in the database.listAll()Returns a list of all world names currently saved in the database.Restores a SavedWorld by looking up its most recent snapshot by name.voidsave(SavedWorld world) Saves a full snapshot of the world into the database as a single transaction.
-
Constructor Details
-
SqliteWorldRepository
Constructs a new repository and ensures the v1 database schema is created.- Parameters:
databaseFilePath- path to the SQLite database file (created if it doesn't exist)- Throws:
IllegalStateException- if the database schema fails to initialize
-
-
Method Details
-
save
Saves a full snapshot of the world into the database as a single transaction. Overwrites any existing snapshot that shares the same name.- Specified by:
savein interfaceWorldRepository- Parameters:
world- the world snapshot containing metadata, objects, and robots- Throws:
SQLException- if any part of the save transaction fails
-
restore
Restores a SavedWorld by looking up its most recent snapshot by name. Reconstructs the world along with its associated objects and robots.- Specified by:
restorein interfaceWorldRepository- Parameters:
name- the unique name of the world to restore- Returns:
- an Optional containing the SavedWorld if found, or empty if it does not exist
- Throws:
SQLException- if a database query fails
-
exists
Checks if a world with the specified name exists in the database.- Specified by:
existsin interfaceWorldRepository- Parameters:
name- the name of the world to look for- Returns:
- true if it exists, false otherwise
- Throws:
SQLException- if the database query fails
-
listAll
Returns a list of all world names currently saved in the database.- Specified by:
listAllin interfaceWorldRepository- Returns:
- a list of unique world names, sorted alphabetically
- Throws:
SQLException- if the database query fails
-