commit 14072025

This commit is contained in:
rdkartono
2025-07-14 08:07:11 +07:00
parent 36135e2fa1
commit b5325ad31c
3 changed files with 102 additions and 39 deletions

View File

@@ -1,7 +1,7 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<inspection_tool class="DuplicatedCode" enabled="false" level="WEAK WARNING" enabled_by_default="false">
<Languages>
<language minSize="47" name="Kotlin" />
</Languages>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="jetbrains.kotlinx.coroutines.core" type="repository">
<properties maven-id="org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlinx/kotlinx-coroutines-core/1.10.2/kotlinx-coroutines-core-1.10.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlinx/kotlinx-coroutines-core-jvm/1.10.2/kotlinx-coroutines-core-jvm-1.10.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/23.0.0/annotations-23.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/2.1.0/kotlin-stdlib-2.1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@@ -3,8 +3,7 @@ package database
import org.mariadb.jdbc.Connection
import org.tinylog.Logger
import java.sql.DriverManager
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.function.Consumer
/**
* A class to manage a connection to a MariaDB database.
@@ -28,13 +27,43 @@ class MariaDB (
var SoundbankList : ArrayList<Soundbank> = ArrayList()
var MessagebankList : ArrayList<Messagebank> = ArrayList()
companion object {
fun ValidDate(date: String): Boolean {
// Check if the date is in the format DD/MM/YYYY
val regex = Regex("""^\d{2}/\d{2}/\d{4}$""")
return regex.matches(date)
}
fun ValidTime(time: String): Boolean {
// Check if the time is in the format HH:MM:SS
val regex = Regex("""^\d{2}:\d{2}:\d{2}$""")
return regex.matches(time)
}
}
init {
try {
connection = DriverManager.getConnection("jdbc:mariadb://$address:$port/$dbName", username, password) as Connection
Logger.info("Connected to MariaDB" as Any)
connected = true
Reload_Soundbank()
val loadthread = Thread {
// Load soundbank and messagebank lists
Reload_Messagebank()
Logger.info { "Messagebank loaded" }
Reload_Soundbank()
Logger.info { "Soundbank loaded" }
}
loadthread.name = "LoadMariaDBThread"
loadthread.isDaemon = true
loadthread.start()
loadthread.join()
Logger.info { "Loading MariaDB completed" }
Logger.info { "Soundbank count: ${SoundbankList.size}" }
Logger.info { "Messagebank count: ${MessagebankList.size}" }
} catch (e : Exception) {
Logger.error("Failed to connect to MariaDB: ${e.message}" as Any)
}
@@ -55,10 +84,10 @@ class MariaDB (
}
/**
* Get Log from database
* @return ArrayList of Log objects
* Get All Log from database
* @param consumer A Consumer that will receive the list of logs
*/
fun GetLog() : ArrayList<Log> {
fun GetLog(consumer : Consumer<ArrayList<Log>>) {
val logList = ArrayList<Log>()
try {
val statement = connection?.createStatement()
@@ -76,16 +105,17 @@ class MariaDB (
} catch (e : Exception) {
Logger.error("Error fetching logs: ${e.message}" as Any)
}
return logList
consumer.accept(logList)
}
/**
* Get Log from database by date
* @param date The date to filter logs by (format: DD/MM/YYYY)
* @return ArrayList of Log objects for the specified date
* @param consumer A Consumer that will receive the list of logs for the specified date
*/
fun GetLog(date : String) : ArrayList<Log> {
fun GetLog(date : String, consumer: Consumer<ArrayList<Log>>) {
val logList = ArrayList<Log>()
if (ValidDate(date)){
try {
val statement = connection?.prepareStatement("SELECT * FROM log WHERE datenya = ?")
statement?.setString(1, date)
@@ -103,23 +133,42 @@ class MariaDB (
} catch (e : Exception) {
Logger.error("Error fetching logs for date $date: ${e.message}" as Any)
}
return logList
}
/**
* Get Log from database by date
* @param date The date to filter logs by (as LocalDateTime)
* @return ArrayList of Log objects for the specified date
*/
fun GetLog(date : LocalDateTime) : ArrayList<Log> {
val datenya = DateTimeFormatter.ofPattern("DD/MM/YYYY").format(date)
return GetLog(datenya)
consumer.accept(logList)
}
fun GetLog(date : String, filter : String, consumer: Consumer<ArrayList<Log>>) {
val logList = ArrayList<Log>()
if (ValidDate(date)){
try {
val statement = connection?.prepareStatement("SELECT * FROM log WHERE datenya = ? AND description LIKE ?")
statement?.setString(1, date)
statement?.setString(2, "%$filter%")
val resultSet = statement?.executeQuery()
while (resultSet?.next() == true) {
val log = Log(
resultSet.getLong("index").toULong(),
resultSet.getString("datenya"),
resultSet.getString("timenya"),
resultSet.getString("machine"),
resultSet.getString("description")
)
logList.add(log)
}
} catch (e : Exception) {
Logger.error("Error fetching logs for date $date with filter $filter: ${e.message}" as Any)
}
}
consumer.accept(logList)
}
/**
* Reloads the soundbank list from the database.
*/
private fun Reload_Soundbank() {
SoundbankList.clear()
try {
val statement = connection?.createStatement()
@@ -153,12 +202,13 @@ class MariaDB (
val messagebank = Messagebank(
resultSet.getLong("index").toUInt(),
resultSet.getString("Description"),
resultSet.getString("TAG"),
resultSet.getInt("Category").toUInt(),
resultSet.getString("Language"),
resultSet.getString("VoiceType"),
resultSet.getString("Path")
resultSet.getInt("ANN_ID").toUInt(),
resultSet.getString("Voice_Type"),
resultSet.getString("Message_Detail"),
resultSet.getString("Message_TAGS")
)
MessagebankList.add(messagebank)
}
} catch (e : Exception) {