Class SQLDialect
- java.lang.Object
-
- xyz.srnyx.annoyingapi.storage.dialects.Dialect
-
- xyz.srnyx.annoyingapi.storage.dialects.sql.SQLDialect
-
- Direct Known Subclasses:
H2Dialect,MariaDBDialect,MySQLDialect,PostgreSQLDialect,SQLiteDialect
public abstract class SQLDialect extends Dialect
SQL dialect for a specific type of database
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class xyz.srnyx.annoyingapi.storage.dialects.Dialect
Dialect.MigrationData
-
-
Field Summary
Fields Modifier and Type Field Description @NotNull java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.concurrent.ConcurrentHashMap<java.lang.String,Value>>>cacheKey: table name Value: Key: target Value: Key: data key Value: data value@NotNull java.sql.ConnectionconnectionTheconnectionto the database-
Fields inherited from class xyz.srnyx.annoyingapi.storage.dialects.Dialect
dataManager
-
-
Constructor Summary
Constructors Constructor Description SQLDialect(@NotNull DataManager dataManager)Construct a newSQLDialectwith the givenDataManager
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description @Nullable java.sql.PreparedStatementcreateKey(@NotNull java.lang.String table, @NotNull java.lang.String key)Create a key in the given table (converts the key to lowercase)protected abstract @Nullable java.sql.PreparedStatementcreateKeyImpl(@NotNull java.lang.String table, @NotNull java.lang.String key)Create a key in the given table@NotNull java.sql.PreparedStatementcreateTable(@NotNull java.lang.String table)Create a table in the databaseprotected abstract @NotNull java.sql.PreparedStatementcreateTableImpl(@NotNull java.lang.String table)Create a table in the databasevoidcreateTablesKeys(@NotNull java.util.Map<java.lang.String,java.util.Set<java.lang.String>> tablesKeys)Create the given tables and keys in the database@NotNull java.sql.PreparedStatementgetAllValuesFromDatabase(@NotNull java.lang.String table)Get all values from the databaseprotected abstract @NotNull java.sql.PreparedStatementgetAllValuesFromDatabaseImpl(@NotNull java.lang.String table)Get all values from the database@Nullable ValuegetFromCacheImpl(@NotNull java.lang.String table, @NotNull java.lang.String target, @NotNull java.lang.String key)Get a value from the cache@NotNull java.util.Optional<Dialect.MigrationData>getMigrationDataFromDatabaseImpl(@NotNull DataManager newManager)Get migration data from the database@NotNull java.sql.PreparedStatementgetTables()Get all tables from the databaseprotected abstract @NotNull java.sql.PreparedStatementgetTablesImpl()Get all tables from the databasevoidmarkRemovedInCacheImpl(@NotNull java.lang.String table, @NotNull java.lang.String target, @NotNull java.lang.String key)Mark a value as removed in the cachevoidsaveCacheImpl()Save all cache data to the databasevoidsaveCacheImpl(@NotNull java.lang.String table, @NotNull java.lang.String target)Save a specific target in a table to the cachevoidsetToCacheImpl(@NotNull java.lang.String table, @NotNull java.lang.String target, @NotNull java.lang.String key, @NotNull Value value)Set a value to the cacheprotected @NotNull java.sql.PreparedStatementsetValuesParameters(@NotNull java.lang.String target, @NotNull java.util.List<Value> values, @NotNull java.lang.StringBuilder insertBuilder, @NotNull java.lang.StringBuilder valuesBuilder, @Nullable java.lang.StringBuilder updateBuilder)Used forDialect.setToDatabaseImpl(String, String, String, String)to set the parameters of thePreparedStatement-
Methods inherited from class xyz.srnyx.annoyingapi.storage.dialects.Dialect
getFromCache, getFromDatabase, getFromDatabaseImpl, getMigrationDataFromDatabase, markRemovedInCache, removeFromDatabaseImpl, removeValueFromDatabase, saveCache, saveCache, setToCache, setToDatabase, setToDatabase, setToDatabase, setToDatabaseImpl, setToDatabaseImpl
-
-
-
-
Field Detail
-
connection
@NotNull public final @NotNull java.sql.Connection connection
Theconnectionto the database
-
cache
@NotNull public final @NotNull java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.concurrent.ConcurrentHashMap<java.lang.String,Value>>> cache
- Key: table name
- Value:
- Key: target
- Value:
- Key: data key
- Value: data value
-
-
Constructor Detail
-
SQLDialect
public SQLDialect(@NotNull @NotNull DataManager dataManager) throws ConnectionExceptionConstruct a newSQLDialectwith the givenDataManager- Parameters:
dataManager-Dialect.dataManager- Throws:
ConnectionException- if a database connection error occurs
-
-
Method Detail
-
getFromCacheImpl
@Nullable public @Nullable Value getFromCacheImpl(@NotNull @NotNull java.lang.String table, @NotNull @NotNull java.lang.String target, @NotNull @NotNull java.lang.String key)
Description copied from class:DialectGet a value from the cache- Specified by:
getFromCacheImplin classDialect- Parameters:
table- the tabletarget- the targetkey- the key- Returns:
- the value inside a
Value, null if it isn't cached
-
setToCacheImpl
public void setToCacheImpl(@NotNull @NotNull java.lang.String table, @NotNull @NotNull java.lang.String target, @NotNull @NotNull java.lang.String key, @NotNull @NotNull Value value)Description copied from class:DialectSet a value to the cache- Specified by:
setToCacheImplin classDialect- Parameters:
table- the tabletarget- the targetkey- the keyvalue- the value inside aValue
-
markRemovedInCacheImpl
public void markRemovedInCacheImpl(@NotNull @NotNull java.lang.String table, @NotNull @NotNull java.lang.String target, @NotNull @NotNull java.lang.String key)Description copied from class:DialectMark a value as removed in the cache- Specified by:
markRemovedInCacheImplin classDialect- Parameters:
table- the tabletarget- the targetkey- the key
-
saveCacheImpl
public void saveCacheImpl()
Description copied from class:DialectSave all cache data to the database- Specified by:
saveCacheImplin classDialect
-
saveCacheImpl
public void saveCacheImpl(@NotNull @NotNull java.lang.String table, @NotNull @NotNull java.lang.String target)Description copied from class:DialectSave a specific target in a table to the cache- Specified by:
saveCacheImplin classDialect- Parameters:
table- the table to save intarget- the target to save
-
getMigrationDataFromDatabaseImpl
@NotNull public @NotNull java.util.Optional<Dialect.MigrationData> getMigrationDataFromDatabaseImpl(@NotNull @NotNull DataManager newManager)
Description copied from class:DialectGet migration data from the database- Specified by:
getMigrationDataFromDatabaseImplin classDialect- Parameters:
newManager- the newDataManagerto migrate to- Returns:
- the migration data, empty if something went wrong
-
createTablesKeys
public void createTablesKeys(@NotNull @NotNull java.util.Map<java.lang.String,java.util.Set<java.lang.String>> tablesKeys)Create the given tables and keys in the database- Parameters:
tablesKeys- the tables and keys to create
-
setValuesParameters
@NotNull protected @NotNull java.sql.PreparedStatement setValuesParameters(@NotNull @NotNull java.lang.String target, @NotNull @NotNull java.util.List<Value> values, @NotNull @NotNull java.lang.StringBuilder insertBuilder, @NotNull @NotNull java.lang.StringBuilder valuesBuilder, @Nullable @Nullable java.lang.StringBuilder updateBuilder) throws java.sql.SQLExceptionUsed forDialect.setToDatabaseImpl(String, String, String, String)to set the parameters of thePreparedStatement- Parameters:
target- the target to set the value tovalues- the values to set asValuesinsertBuilder- the insert query buildervaluesBuilder- the values query builderupdateBuilder- the update query builder- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database access error occurs
-
getTables
@NotNull public final @NotNull java.sql.PreparedStatement getTables() throws java.sql.SQLExceptionGet all tables from the database- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database error occurs
-
createTable
@NotNull public final @NotNull java.sql.PreparedStatement createTable(@NotNull @NotNull java.lang.String table) throws java.sql.SQLExceptionCreate a table in the database- Parameters:
table- the table to create- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database error occurs
-
createKey
@Nullable public final @Nullable java.sql.PreparedStatement createKey(@NotNull @NotNull java.lang.String table, @NotNull @NotNull java.lang.String key) throws java.sql.SQLExceptionCreate a key in the given table (converts the key to lowercase)- Parameters:
table- the table to create the key inkey- the key to create- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database error occurs
-
getAllValuesFromDatabase
@NotNull public final @NotNull java.sql.PreparedStatement getAllValuesFromDatabase(@NotNull @NotNull java.lang.String table) throws java.sql.SQLExceptionGet all values from the database- Parameters:
table- the table to get the values from- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database error occurs
-
getTablesImpl
@NotNull protected abstract @NotNull java.sql.PreparedStatement getTablesImpl() throws java.sql.SQLExceptionGet all tables from the database- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database error occurs
-
createTableImpl
@NotNull protected abstract @NotNull java.sql.PreparedStatement createTableImpl(@NotNull @NotNull java.lang.String table) throws java.sql.SQLExceptionCreate a table in the database- Parameters:
table- the table to create- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database error occurs
-
createKeyImpl
@Nullable protected abstract @Nullable java.sql.PreparedStatement createKeyImpl(@NotNull @NotNull java.lang.String table, @NotNull @NotNull java.lang.String key) throws java.sql.SQLExceptionCreate a key in the given table- Parameters:
table- the table to create the key inkey- the key to create- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database error occurs
-
getAllValuesFromDatabaseImpl
@NotNull protected abstract @NotNull java.sql.PreparedStatement getAllValuesFromDatabaseImpl(@NotNull @NotNull java.lang.String table) throws java.sql.SQLExceptionGet all values from the database- Parameters:
table- the table to get the values from- Returns:
- the
PreparedStatementwith the set parameters - Throws:
java.sql.SQLException- if a database error occurs
-
-