1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

Implemented Exposed in saveMessages(). It doesn't work. App quits when calling Messages.insert(...) or Messages.selectAll(). No Exception, no more ideas what's happening there. I give up.

This commit is contained in:
Fabian Schlenz 2017-12-12 17:50:29 +01:00
parent 7067f98943
commit 047b95c6ea
3 changed files with 153 additions and 119 deletions

View File

@ -28,6 +28,11 @@ compileKotlin {
kotlinOptions.apiVersion = "1.0" kotlinOptions.apiVersion = "1.0"
} }
repositories {
maven {
url "https://dl.bintray.com/kotlin/exposed"
}
}
dependencies { dependencies {
compile('com.github.badoualy:kotlogram:666a81ef9d6707f117a3fecc2d21c91d51c7d075') { compile('com.github.badoualy:kotlogram:666a81ef9d6707f117a3fecc2d21c91d51c7d075') {
@ -38,6 +43,7 @@ dependencies {
compile 'org.slf4j:slf4j-api:1.7.21' compile 'org.slf4j:slf4j-api:1.7.21'
compile 'ch.qos.logback:logback-classic:1.1.7' compile 'ch.qos.logback:logback-classic:1.1.7'
compile 'com.google.code.gson:gson:2.5' compile 'com.google.code.gson:gson:2.5'
compile 'org.jetbrains.exposed:exposed:0.9.1'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
} }

View File

@ -59,6 +59,7 @@ object CommandLineRunner {
val appender = ConsoleAppender<ILoggingEvent>() val appender = ConsoleAppender<ILoggingEvent>()
appender.setContext(rootContext) appender.setContext(rootContext)
appender.setEncoder(encoder) appender.setEncoder(encoder)
appender.setName("root")
appender.start() appender.start()
rootLogger.addAppender(appender) rootLogger.addAppender(appender)

View File

@ -22,6 +22,7 @@ import com.github.badoualy.telegram.api.TelegramClient
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.slf4j.Logger import org.slf4j.Logger
import javax.sql.rowset.serial.SerialBlob
import java.sql.Connection import java.sql.Connection
import java.sql.DriverManager import java.sql.DriverManager
import java.sql.Statement import java.sql.Statement
@ -31,6 +32,8 @@ import java.sql.ResultSetMetaData
import java.sql.PreparedStatement import java.sql.PreparedStatement
import java.sql.Types import java.sql.Types
import java.sql.Time import java.sql.Time
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
import java.io.File import java.io.File
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@ -47,10 +50,32 @@ import java.text.SimpleDateFormat
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory
object Messages : Table("messages") {
val id = integer("id").primaryKey().autoIncrement()
val message_id = integer("message_id")
val message_type = text("message_type")
val source_type = text("source_type")
val source_id = integer("source_id")
val sender_id = integer("sender_id").nullable()
val fwd_from_id = integer("fwd_from_id").nullable()
val text = text("text").nullable()
val time = integer("time").nullable()
val has_media = bool("has_media").nullable()
val media_type = text("media_type").nullable()
val media_file = text("media_file").nullable()
val media_size = integer("media_size").nullable()
val media_json = text("media_json").nullable()
val markup_json = text("markup_json").nullable()
val data = blob("data").nullable()
val api_layer = integer("api_layer").nullable()
//val unique = uniqueIndex(source_type, source_id, message_id)
}
class Database private constructor(var client: TelegramClient) { class Database private constructor(var client: TelegramClient) {
private var conn: Connection? = null private var conn: Connection? = null
private var stmt: Statement? = null private var stmt: Statement? = null
var user_manager: UserManager var user_manager: UserManager
val db_path: String
fun getTopMessageID(): Int { fun getTopMessageID(): Int {
try { try {
@ -206,6 +231,7 @@ class Database private constructor(var client: TelegramClient) {
} }
val path = "jdbc:sqlite:${user_manager.fileBase}${Config.FILE_NAME_DB}" val path = "jdbc:sqlite:${user_manager.fileBase}${Config.FILE_NAME_DB}"
db_path = path
try { try {
conn = DriverManager.getConnection(path) conn = DriverManager.getConnection(path)
@ -271,7 +297,7 @@ class Database private constructor(var client: TelegramClient) {
} }
@Synchronized @Synchronized
fun saveMessages(all: TLVector<TLAbsMessage>, api_layer: Int) { fun saveMessages(all: TLVector<TLAbsMessage>, my_api_layer: Int) {
try { try {
//"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " + //"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " +
//"VALUES " + //"VALUES " +
@ -283,113 +309,114 @@ class Database private constructor(var client: TelegramClient) {
val ps = conn!!.prepareStatement("INSERT OR REPLACE INTO messages " + columns) val ps = conn!!.prepareStatement("INSERT OR REPLACE INTO messages " + columns)
val ps_insert_or_ignore = conn!!.prepareStatement("INSERT OR IGNORE INTO messages " + columns) val ps_insert_or_ignore = conn!!.prepareStatement("INSERT OR IGNORE INTO messages " + columns)
logger.debug("Saving messages...")
conn!!.close()
println(db_path)
val db = org.jetbrains.exposed.sql.Database.connect(db_path, driver="org.sqlite.JDBC")
(exposedLogger as ch.qos.logback.classic.Logger).addAppender((logger as ch.qos.logback.classic.Logger).getLoggerContext().getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME).getAppender("root"))
(exposedLogger as ch.qos.logback.classic.Logger).level = ch.qos.logback.classic.Level.TRACE
transaction({
logger.addLogger(StdOutSqlLogger)
println(0)
Messages.selectAll()//.limit(5,0).forEach({println("${it[Messages.text]}")})
println("0a")
})
for (abs in all) { for (abs in all) {
print(1)
transaction {
debug = true
print(2)
logger.addLogger(StdOutSqlLogger)
print(3)
if (abs is TLMessage) { if (abs is TLMessage) {
print(4)
val msg = abs val msg = abs
ps.setInt(1, msg.getId()) println("4a")
ps.setString(2, "message") try {
println("4b")
val a = Messages.insert {
println(5)
it[message_id] = msg.getId()
/*it[message_type] = "message"
val peer = msg.getToId() val peer = msg.getToId()
if (peer is TLPeerChat) { if (peer is TLPeerChat) {
ps.setString(3, "group") it[source_type] = "group"
ps.setInt(4, peer.getChatId()) it[source_id] = peer.getChatId()
} else if (peer is TLPeerUser) { } else if (peer is TLPeerUser) {
var id = peer.getUserId() var id = peer.getUserId()
if (id == this.user_manager.user!!.getId()) { if (id == user_manager.user!!.getId()) {
id = msg.getFromId() id = msg.getFromId()
} }
ps.setString(3, "dialog") it[source_type] = "dialog"
ps.setInt(4, id) it[source_id] = id
} else if (peer is TLPeerChannel) { } else if (peer is TLPeerChannel) {
ps.setString(3, "channel") it[source_type] = "channel"
ps.setInt(4, peer.getChannelId()) it[source_id] = peer.getChannelId()
} else { } else {
throw RuntimeException("Unexpected Peer type: " + peer.javaClass) throw RuntimeException("Unexpected Peer type: " + peer.javaClass)
} }
if (peer is TLPeerChannel) { // Messages in a channel don't have a sender -> insert a null
// Message in a channel don't have a sender -> insert a null it[sender_id] = if (peer is TLPeerChannel) null else msg.getFromId()
ps.setNull(5, Types.INTEGER) print(6)
} else { it[fwd_from_id] = msg.getFwdFrom()?.getFromId()
ps.setInt(5, msg.getFromId())
}
if (msg.getFwdFrom() != null && msg.getFwdFrom().getFromId() != null) { var msg_text = msg.getMessage()
ps.setInt(6, msg.getFwdFrom().getFromId()) if ((msg_text == null || msg_text.equals("")) && msg.getMedia() != null) {
} else {
ps.setNull(6, Types.INTEGER)
}
var text = msg.getMessage()
if ((text == null || text.equals("")) && msg.getMedia() != null) {
val media = msg.getMedia() val media = msg.getMedia()
if (media is TLMessageMediaDocument) { if (media is TLMessageMediaDocument) {
text = media.getCaption() msg_text = media.getCaption()
} else if (media is TLMessageMediaPhoto) { } else if (media is TLMessageMediaPhoto) {
text = media.getCaption() msg_text = media.getCaption()
} }
} }
ps.setString(7, text) it[text] = msg_text
ps.setString(8, "" + msg.getDate()) it[time] = msg.getDate()
val f = FileManagerFactory.getFileManager(msg, user_manager, client) val f = FileManagerFactory.getFileManager(msg, user_manager, client)
if (f == null) { if (f == null) {
ps.setNull(9, Types.BOOLEAN) it[has_media] = null
ps.setNull(10, Types.VARCHAR) it[media_type] = null
ps.setNull(11, Types.VARCHAR) it[media_file] = null
ps.setNull(12, Types.INTEGER) it[media_size] = null
} else { } else {
ps.setBoolean(9, true) it[has_media] = true
ps.setString(10, f.name) it[media_type] = f.name
ps.setString(11, f.targetFilename) it[media_file] = f.targetFilename
ps.setInt(12, f.size) it[media_size] = f.size
} }
print(7)
val stream = ByteArrayOutputStream() val stream = ByteArrayOutputStream()
msg.serializeBody(stream) msg.serializeBody(stream)
ps.setBytes(13, stream.toByteArray()) it[data] = SerialBlob(stream.toByteArray())
ps.setInt(14, api_layer) it[api_layer] = my_api_layer
ps.addBatch() print(8)*/
}
} catch(e: Exception) {
println("e")
}
} else if (abs is TLMessageService) { } else if (abs is TLMessageService) {
ps_insert_or_ignore.setInt(1, abs.getId()) Messages.insert {
ps_insert_or_ignore.setString(2, "service_message") it[message_id] = abs.getId()
ps_insert_or_ignore.setNull(3, Types.INTEGER) //it[message_type] = "service_message"
ps_insert_or_ignore.setNull(4, Types.INTEGER) //it[api_layer] = my_api_layer
ps_insert_or_ignore.setNull(5, Types.INTEGER) }
ps_insert_or_ignore.setNull(6, Types.INTEGER)
ps_insert_or_ignore.setNull(7, Types.VARCHAR)
ps_insert_or_ignore.setNull(8, Types.INTEGER)
ps_insert_or_ignore.setNull(9, Types.BOOLEAN)
ps_insert_or_ignore.setNull(10, Types.VARCHAR)
ps_insert_or_ignore.setNull(11, Types.VARCHAR)
ps_insert_or_ignore.setNull(12, Types.INTEGER)
ps_insert_or_ignore.setNull(13, Types.BLOB)
ps_insert_or_ignore.setInt(14, api_layer)
ps_insert_or_ignore.addBatch()
} else if (abs is TLMessageEmpty) { } else if (abs is TLMessageEmpty) {
ps_insert_or_ignore.setInt(1, abs.getId()) Messages.insert {
ps_insert_or_ignore.setString(2, "empty_message") it[message_id] = abs.getId()
ps_insert_or_ignore.setNull(3, Types.INTEGER) //it[message_type] = "empty_message"
ps_insert_or_ignore.setNull(4, Types.INTEGER) //it[api_layer] = my_api_layer
ps_insert_or_ignore.setNull(5, Types.INTEGER) }
ps_insert_or_ignore.setNull(6, Types.INTEGER)
ps_insert_or_ignore.setNull(7, Types.VARCHAR)
ps_insert_or_ignore.setNull(8, Types.INTEGER)
ps_insert_or_ignore.setNull(9, Types.BOOLEAN)
ps_insert_or_ignore.setNull(10, Types.VARCHAR)
ps_insert_or_ignore.setNull(11, Types.VARCHAR)
ps_insert_or_ignore.setNull(12, Types.INTEGER)
ps_insert_or_ignore.setNull(13, Types.BLOB)
ps_insert_or_ignore.setInt(14, api_layer)
ps_insert_or_ignore.addBatch()
} else { } else {
throw RuntimeException("Unexpected Message type: " + abs.javaClass) throw RuntimeException("Unexpected Message type: " + abs.javaClass)
} }
print(9)
} }
conn!!.setAutoCommit(false) print("a")
ps.executeBatch() } // transaction
ps.clearBatch() logger.debug("Messages saved.")
ps_insert_or_ignore.executeBatch()
ps_insert_or_ignore.clearBatch()
conn!!.commit()
conn!!.setAutoCommit(true)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
throw RuntimeException("Exception shown above happened.") throw RuntimeException("Exception shown above happened.")