1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 08:46:15 +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

@ -5,18 +5,18 @@ mainClassName= 'de.fabianonline.telegram_backup.CommandLineRunnerKt'
repositories { repositories {
mavenCentral() mavenCentral()
maven { maven {
url "https://jitpack.io" url "https://jitpack.io"
} }
} }
buildscript { buildscript {
ext.kotlin_version = '1.2.0' ext.kotlin_version = '1.2.0'
repositories { repositories {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
@ -27,7 +27,12 @@ apply plugin: 'kotlin'
compileKotlin { 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,7 +43,8 @@ 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'
} }
@ -65,11 +71,11 @@ jar {
manifest { manifest {
attributes "Main-Class": "$mainClassName" attributes "Main-Class": "$mainClassName"
} }
from { from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
} }
includes [ includes [
"*.mustache" "*.mustache"
] ]

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 {
@ -116,7 +141,7 @@ class Database private constructor(var client: TelegramClient) {
val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.user!!.getId()) val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.user!!.getId())
rs.next() rs.next()
return rs.getInt(1) return rs.getInt(1)
} catch (e: SQLException) { } catch (e: SQLException) {
throw RuntimeException(e) throw RuntimeException(e)
} }
@ -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)
@ -217,7 +243,7 @@ class Database private constructor(var client: TelegramClient) {
// Run updates // Run updates
val updates = DatabaseUpdates(conn!!, this) val updates = DatabaseUpdates(conn!!, this)
updates.doUpdates() updates.doUpdates()
System.out.println("Database is ready.") System.out.println("Database is ready.")
} }
@ -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 " +
@ -279,117 +305,118 @@ class Database private constructor(var client: TelegramClient) {
val columns = "(message_id, message_type, source_type, source_id, sender_id, fwd_from_id, text, time, has_media, media_type, media_file, media_size, data, api_layer) " + val columns = "(message_id, message_type, source_type, source_id, sender_id, fwd_from_id, text, time, has_media, media_type, media_file, media_size, data, api_layer) " +
"VALUES " + "VALUES " +
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
//1 2 3 4 5 6 7 8 9 10 11 12 13 14 //1 2 3 4 5 6 7 8 9 10 11 12 13 14
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)
for (abs in all) { logger.debug("Saving messages...")
if (abs is TLMessage) { conn!!.close()
val msg = abs println(db_path)
ps.setInt(1, msg.getId()) val db = org.jetbrains.exposed.sql.Database.connect(db_path, driver="org.sqlite.JDBC")
ps.setString(2, "message")
val peer = msg.getToId() (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"))
if (peer is TLPeerChat) { (exposedLogger as ch.qos.logback.classic.Logger).level = ch.qos.logback.classic.Level.TRACE
ps.setString(3, "group") transaction({
ps.setInt(4, peer.getChatId()) logger.addLogger(StdOutSqlLogger)
} else if (peer is TLPeerUser) { println(0)
var id = peer.getUserId() Messages.selectAll()//.limit(5,0).forEach({println("${it[Messages.text]}")})
if (id == this.user_manager.user!!.getId()) { println("0a")
id = msg.getFromId() })
} for (abs in all) {
ps.setString(3, "dialog") print(1)
ps.setInt(4, id) transaction {
} else if (peer is TLPeerChannel) { debug = true
ps.setString(3, "channel") print(2)
ps.setInt(4, peer.getChannelId()) logger.addLogger(StdOutSqlLogger)
} else { print(3)
throw RuntimeException("Unexpected Peer type: " + peer.javaClass) if (abs is TLMessage) {
} print(4)
val msg = abs
if (peer is TLPeerChannel) { println("4a")
// Message in a channel don't have a sender -> insert a null try {
ps.setNull(5, Types.INTEGER) println("4b")
} else { val a = Messages.insert {
ps.setInt(5, msg.getFromId()) println(5)
} it[message_id] = msg.getId()
/*it[message_type] = "message"
if (msg.getFwdFrom() != null && msg.getFwdFrom().getFromId() != null) {
ps.setInt(6, msg.getFwdFrom().getFromId()) val peer = msg.getToId()
} else { if (peer is TLPeerChat) {
ps.setNull(6, Types.INTEGER) it[source_type] = "group"
} it[source_id] = peer.getChatId()
} else if (peer is TLPeerUser) {
var text = msg.getMessage() var id = peer.getUserId()
if ((text == null || text.equals("")) && msg.getMedia() != null) { if (id == user_manager.user!!.getId()) {
val media = msg.getMedia() id = msg.getFromId()
if (media is TLMessageMediaDocument) { }
text = media.getCaption() it[source_type] = "dialog"
} else if (media is TLMessageMediaPhoto) { it[source_id] = id
text = media.getCaption() } else if (peer is TLPeerChannel) {
} it[source_type] = "channel"
} it[source_id] = peer.getChannelId()
ps.setString(7, text) } else {
ps.setString(8, "" + msg.getDate()) throw RuntimeException("Unexpected Peer type: " + peer.javaClass)
val f = FileManagerFactory.getFileManager(msg, user_manager, client) }
if (f == null) {
ps.setNull(9, Types.BOOLEAN) // Messages in a channel don't have a sender -> insert a null
ps.setNull(10, Types.VARCHAR) it[sender_id] = if (peer is TLPeerChannel) null else msg.getFromId()
ps.setNull(11, Types.VARCHAR) print(6)
ps.setNull(12, Types.INTEGER) it[fwd_from_id] = msg.getFwdFrom()?.getFromId()
} else {
ps.setBoolean(9, true) var msg_text = msg.getMessage()
ps.setString(10, f.name) if ((msg_text == null || msg_text.equals("")) && msg.getMedia() != null) {
ps.setString(11, f.targetFilename) val media = msg.getMedia()
ps.setInt(12, f.size) if (media is TLMessageMediaDocument) {
} msg_text = media.getCaption()
val stream = ByteArrayOutputStream() } else if (media is TLMessageMediaPhoto) {
msg.serializeBody(stream) msg_text = media.getCaption()
ps.setBytes(13, stream.toByteArray()) }
ps.setInt(14, api_layer) }
ps.addBatch() it[text] = msg_text
} else if (abs is TLMessageService) { it[time] = msg.getDate()
ps_insert_or_ignore.setInt(1, abs.getId())
ps_insert_or_ignore.setString(2, "service_message") val f = FileManagerFactory.getFileManager(msg, user_manager, client)
ps_insert_or_ignore.setNull(3, Types.INTEGER) if (f == null) {
ps_insert_or_ignore.setNull(4, Types.INTEGER) it[has_media] = null
ps_insert_or_ignore.setNull(5, Types.INTEGER) it[media_type] = null
ps_insert_or_ignore.setNull(6, Types.INTEGER) it[media_file] = null
ps_insert_or_ignore.setNull(7, Types.VARCHAR) it[media_size] = null
ps_insert_or_ignore.setNull(8, Types.INTEGER) } else {
ps_insert_or_ignore.setNull(9, Types.BOOLEAN) it[has_media] = true
ps_insert_or_ignore.setNull(10, Types.VARCHAR) it[media_type] = f.name
ps_insert_or_ignore.setNull(11, Types.VARCHAR) it[media_file] = f.targetFilename
ps_insert_or_ignore.setNull(12, Types.INTEGER) it[media_size] = f.size
ps_insert_or_ignore.setNull(13, Types.BLOB) }
ps_insert_or_ignore.setInt(14, api_layer) print(7)
ps_insert_or_ignore.addBatch() val stream = ByteArrayOutputStream()
} else if (abs is TLMessageEmpty) { msg.serializeBody(stream)
ps_insert_or_ignore.setInt(1, abs.getId()) it[data] = SerialBlob(stream.toByteArray())
ps_insert_or_ignore.setString(2, "empty_message") it[api_layer] = my_api_layer
ps_insert_or_ignore.setNull(3, Types.INTEGER) print(8)*/
ps_insert_or_ignore.setNull(4, Types.INTEGER) }
ps_insert_or_ignore.setNull(5, Types.INTEGER) } catch(e: Exception) {
ps_insert_or_ignore.setNull(6, Types.INTEGER) println("e")
ps_insert_or_ignore.setNull(7, Types.VARCHAR) }
ps_insert_or_ignore.setNull(8, Types.INTEGER) } else if (abs is TLMessageService) {
ps_insert_or_ignore.setNull(9, Types.BOOLEAN) Messages.insert {
ps_insert_or_ignore.setNull(10, Types.VARCHAR) it[message_id] = abs.getId()
ps_insert_or_ignore.setNull(11, Types.VARCHAR) //it[message_type] = "service_message"
ps_insert_or_ignore.setNull(12, Types.INTEGER) //it[api_layer] = my_api_layer
ps_insert_or_ignore.setNull(13, Types.BLOB) }
ps_insert_or_ignore.setInt(14, api_layer) } else if (abs is TLMessageEmpty) {
ps_insert_or_ignore.addBatch() Messages.insert {
} else { it[message_id] = abs.getId()
throw RuntimeException("Unexpected Message type: " + abs.javaClass) //it[message_type] = "empty_message"
} //it[api_layer] = my_api_layer
} }
conn!!.setAutoCommit(false) } else {
ps.executeBatch() throw RuntimeException("Unexpected Message type: " + abs.javaClass)
ps.clearBatch() }
ps_insert_or_ignore.executeBatch() print(9)
ps_insert_or_ignore.clearBatch() }
conn!!.commit() print("a")
conn!!.setAutoCommit(true) } // transaction
logger.debug("Messages saved.")
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
throw RuntimeException("Exception shown above happened.") throw RuntimeException("Exception shown above happened.")