Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Database(host: String, port: String, database: String, userName: String,
- password: String, private val table: String) {
- private var dataSource = HikariDataSource()
- init {
- dataSource.maximumPoolSize = 3
- dataSource.connectionTimeout = 30000
- dataSource.jdbcUrl = "jdbc:mysql://$host:$port/$database"
- dataSource.username = userName
- dataSource.password = password
- dataSource.addDataSourceProperty("cachePrepStmts", true)
- dataSource.addDataSourceProperty("prepStmtCacheSize", 250)
- dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", 2048)
- dataSource.addDataSourceProperty("useServerPrepStmts", true)
- }
- private fun createTable() {
- val connection = dataSource.connection
- val stmt = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `$table` (`uuid` VARCHAR(36) NOT NULL, " +
- "`walletData` TEXT NOT NULL, PRIMARY KEY(uuid));")
- stmt.executeUpdate()
- connection.close()
- }
- fun loadUsers() {
- createTable()
- val connection = dataSource.connection
- val stmt = connection.prepareStatement("SELECT * FROM `$table`")
- val resultSet = stmt.executeQuery()
- while (resultSet.next()) {
- val uuid = UUID.fromString(resultSet.getString("uuid"))
- val user = EconomyUserUtils.getUserOrCreate(uuid)
- for (walletEntry in resultSet.getString("walletData").split(";")) {
- val entrySplit = walletEntry.split(",")
- val currency = CurrencyUtils.getCurrencyByName(entrySplit[0]) ?: continue
- user.wallet.openWalletIn(currency)
- user.wallet.getCurrencyValue(currency)!!.set(CurrencyValueImpl(entrySplit[1].toInt(), entrySplit[2].toInt()), currency.currencyDivision)
- }
- }
- resultSet.close()
- connection.close()
- }
- fun saveUser(user: EconomyUser) {
- createTable()
- val connection = dataSource.connection
- val uuid = user.uuid.toString()
- var walletData = ""
- for (walletEntry in user.wallet.currencies) {
- walletData += ";${walletEntry.first.majorName.name},${walletEntry.second.major},${walletEntry.second.minor}"
- }
- walletData = walletData.substring(1)
- val stmt = connection.prepareStatement("INSERT INTO `$table` VALUES('$uuid', '$walletData') ON DUPLICATE KEY UPDATE walletData=$walletData")
- stmt.executeUpdate()
- connection.close()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement