Skip to content
This repository has been archived by the owner on Apr 27, 2020. It is now read-only.

Commit

Permalink
Added a DatabaseUpdate to convert the data to json.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianonline committed Apr 12, 2018
1 parent df1c905 commit 2402356
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 38,8 @@ dependencies {
compile 'com.github.spullara.mustache.java:compiler:0.9.5'
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'ch.qos.logback:logback-classic:1.1.7'
compile 'com.google.code.gson:gson:2.5'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.github.salomonbrys.kotson:kotson:2.5.0'
compile 'com.github.kittinunf.fuel:fuel:1.12.0'

testCompile 'junit:junit:4.12'
Expand Down
40 changes: 40 additions & 0 deletions src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 15,8 @@ import org.slf4j.LoggerFactory
import org.slf4j.Logger
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager
import com.github.salomonbrys.kotson.*
import com.google.gson.*

class DatabaseUpdates(protected var conn: Connection, protected var db: Database) {

Expand All @@ -33,6 35,7 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database
register(DB_Update_8(conn, db))
register(DB_Update_9(conn, db))
register(DB_Update_10(conn, db))
register(DB_Update_11(conn, db))
}

fun doUpdates() {
Expand Down Expand Up @@ -451,3 454,40 @@ internal class DB_Update_10(conn: Connection, db: Database) : DatabaseUpdate(con
execute("CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT)")
}
}

internal class DB_Update_11(conn: Connection, db: Database) : DatabaseUpdate(conn, db) {
override val version = 11
val logger = LoggerFactory.getLogger(DB_Update_11::class.java)

override fun _doUpdate() {
execute("ALTER TABLE messages ADD COLUMN json TEXT NULL")
val limit = 5000
var offset = 0
var i = 0
val ps = conn.prepareStatement("UPDATE messages SET json=? WHERE id=?")
do {
i = 0
logger.debug("Querying with limit $limit and offset $offset")
val rs = db.executeQuery("SELECT id, data FROM messages WHERE json IS NULL AND api_layer=53 LIMIT $limit")
while (rs.next()) {
i
val id = rs.getInt(1)
val msg = Database.bytesToTLMessage(rs.getBytes(2))
if (msg == null) continue
val json = msg.toJson()
ps.setString(1, json)
ps.setInt(2, id)
ps.addBatch()
}
rs.close()
conn.setAutoCommit(false)
ps.executeBatch()
ps.clearBatch()
conn.commit()
conn.setAutoCommit(true)

print(".")
} while (i >= limit)
ps.close()
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/de/fabianonline/telegram_backup/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 17,13 @@
package de.fabianonline.telegram_backup

import com.github.badoualy.telegram.tl.exception.RpcErrorException
import com.github.badoualy.telegram.tl.api.TLMessage
import java.io.File
import java.util.Vector
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import com.google.gson.*
import com.github.salomonbrys.kotson.*
import java.net.URL
import org.apache.commons.io.IOUtils
import de.fabianonline.telegram_backup.Version
Expand Down Expand Up @@ -223,3 225,25 @@ fun Any.toJson(): String = Gson().toJson(this)
fun Any.toPrettyJson(): String = GsonBuilder().setPrettyPrinting().create().toJson(this)

class MaxTriesExceededException(): RuntimeException("Max tries exceeded") {}

fun TLMessage.toJson(): String {
val json = Gson().toJsonTree(this)
cleanUpMessageJson(json)
return json.toString()
}

fun cleanUpMessageJson(json : JsonElement) {
if (json.isJsonArray) {
json.array.forEach {cleanUpMessageJson(it)}
return
} else if (!json.isJsonObject) {
return
}
if (json.obj.has("bytes")) {
json.obj -= "bytes"
return
}
json.obj.forEach {_: String, elm: JsonElement ->
if (elm.isJsonObject || elm.isJsonArray) cleanUpMessageJson(elm)
}
}

0 comments on commit 2402356

Please sign in to comment.