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:
parent
7067f98943
commit
047b95c6ea
@ -28,6 +28,11 @@ compileKotlin {
|
||||
kotlinOptions.apiVersion = "1.0"
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
url "https://dl.bintray.com/kotlin/exposed"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile('com.github.badoualy:kotlogram:666a81ef9d6707f117a3fecc2d21c91d51c7d075') {
|
||||
@ -38,6 +43,7 @@ dependencies {
|
||||
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 'org.jetbrains.exposed:exposed:0.9.1'
|
||||
|
||||
testCompile 'junit:junit:4.12'
|
||||
}
|
||||
|
@ -59,6 +59,7 @@ object CommandLineRunner {
|
||||
val appender = ConsoleAppender<ILoggingEvent>()
|
||||
appender.setContext(rootContext)
|
||||
appender.setEncoder(encoder)
|
||||
appender.setName("root")
|
||||
appender.start()
|
||||
|
||||
rootLogger.addAppender(appender)
|
||||
|
@ -22,6 +22,7 @@ import com.github.badoualy.telegram.api.TelegramClient
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.slf4j.Logger
|
||||
|
||||
import javax.sql.rowset.serial.SerialBlob
|
||||
import java.sql.Connection
|
||||
import java.sql.DriverManager
|
||||
import java.sql.Statement
|
||||
@ -31,6 +32,8 @@ import java.sql.ResultSetMetaData
|
||||
import java.sql.PreparedStatement
|
||||
import java.sql.Types
|
||||
import java.sql.Time
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import java.io.File
|
||||
import java.io.ByteArrayInputStream
|
||||
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.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) {
|
||||
private var conn: Connection? = null
|
||||
private var stmt: Statement? = null
|
||||
var user_manager: UserManager
|
||||
val db_path: String
|
||||
|
||||
fun getTopMessageID(): Int {
|
||||
try {
|
||||
@ -206,6 +231,7 @@ class Database private constructor(var client: TelegramClient) {
|
||||
}
|
||||
|
||||
val path = "jdbc:sqlite:${user_manager.fileBase}${Config.FILE_NAME_DB}"
|
||||
db_path = path
|
||||
|
||||
try {
|
||||
conn = DriverManager.getConnection(path)
|
||||
@ -271,7 +297,7 @@ class Database private constructor(var client: TelegramClient) {
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun saveMessages(all: TLVector<TLAbsMessage>, api_layer: Int) {
|
||||
fun saveMessages(all: TLVector<TLAbsMessage>, my_api_layer: Int) {
|
||||
try {
|
||||
//"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " +
|
||||
//"VALUES " +
|
||||
@ -283,113 +309,114 @@ class Database private constructor(var client: TelegramClient) {
|
||||
val ps = conn!!.prepareStatement("INSERT OR REPLACE 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) {
|
||||
print(1)
|
||||
transaction {
|
||||
debug = true
|
||||
print(2)
|
||||
logger.addLogger(StdOutSqlLogger)
|
||||
print(3)
|
||||
if (abs is TLMessage) {
|
||||
print(4)
|
||||
val msg = abs
|
||||
ps.setInt(1, msg.getId())
|
||||
ps.setString(2, "message")
|
||||
println("4a")
|
||||
try {
|
||||
println("4b")
|
||||
val a = Messages.insert {
|
||||
println(5)
|
||||
it[message_id] = msg.getId()
|
||||
/*it[message_type] = "message"
|
||||
|
||||
val peer = msg.getToId()
|
||||
if (peer is TLPeerChat) {
|
||||
ps.setString(3, "group")
|
||||
ps.setInt(4, peer.getChatId())
|
||||
it[source_type] = "group"
|
||||
it[source_id] = peer.getChatId()
|
||||
} else if (peer is TLPeerUser) {
|
||||
var id = peer.getUserId()
|
||||
if (id == this.user_manager.user!!.getId()) {
|
||||
if (id == user_manager.user!!.getId()) {
|
||||
id = msg.getFromId()
|
||||
}
|
||||
ps.setString(3, "dialog")
|
||||
ps.setInt(4, id)
|
||||
it[source_type] = "dialog"
|
||||
it[source_id] = id
|
||||
} else if (peer is TLPeerChannel) {
|
||||
ps.setString(3, "channel")
|
||||
ps.setInt(4, peer.getChannelId())
|
||||
it[source_type] = "channel"
|
||||
it[source_id] = peer.getChannelId()
|
||||
} else {
|
||||
throw RuntimeException("Unexpected Peer type: " + peer.javaClass)
|
||||
}
|
||||
|
||||
if (peer is TLPeerChannel) {
|
||||
// Message in a channel don't have a sender -> insert a null
|
||||
ps.setNull(5, Types.INTEGER)
|
||||
} else {
|
||||
ps.setInt(5, msg.getFromId())
|
||||
}
|
||||
// Messages in a channel don't have a sender -> insert a null
|
||||
it[sender_id] = if (peer is TLPeerChannel) null else msg.getFromId()
|
||||
print(6)
|
||||
it[fwd_from_id] = msg.getFwdFrom()?.getFromId()
|
||||
|
||||
if (msg.getFwdFrom() != null && msg.getFwdFrom().getFromId() != null) {
|
||||
ps.setInt(6, msg.getFwdFrom().getFromId())
|
||||
} else {
|
||||
ps.setNull(6, Types.INTEGER)
|
||||
}
|
||||
|
||||
var text = msg.getMessage()
|
||||
if ((text == null || text.equals("")) && msg.getMedia() != null) {
|
||||
var msg_text = msg.getMessage()
|
||||
if ((msg_text == null || msg_text.equals("")) && msg.getMedia() != null) {
|
||||
val media = msg.getMedia()
|
||||
if (media is TLMessageMediaDocument) {
|
||||
text = media.getCaption()
|
||||
msg_text = media.getCaption()
|
||||
} else if (media is TLMessageMediaPhoto) {
|
||||
text = media.getCaption()
|
||||
msg_text = media.getCaption()
|
||||
}
|
||||
}
|
||||
ps.setString(7, text)
|
||||
ps.setString(8, "" + msg.getDate())
|
||||
it[text] = msg_text
|
||||
it[time] = msg.getDate()
|
||||
|
||||
val f = FileManagerFactory.getFileManager(msg, user_manager, client)
|
||||
if (f == null) {
|
||||
ps.setNull(9, Types.BOOLEAN)
|
||||
ps.setNull(10, Types.VARCHAR)
|
||||
ps.setNull(11, Types.VARCHAR)
|
||||
ps.setNull(12, Types.INTEGER)
|
||||
it[has_media] = null
|
||||
it[media_type] = null
|
||||
it[media_file] = null
|
||||
it[media_size] = null
|
||||
} else {
|
||||
ps.setBoolean(9, true)
|
||||
ps.setString(10, f.name)
|
||||
ps.setString(11, f.targetFilename)
|
||||
ps.setInt(12, f.size)
|
||||
it[has_media] = true
|
||||
it[media_type] = f.name
|
||||
it[media_file] = f.targetFilename
|
||||
it[media_size] = f.size
|
||||
}
|
||||
print(7)
|
||||
val stream = ByteArrayOutputStream()
|
||||
msg.serializeBody(stream)
|
||||
ps.setBytes(13, stream.toByteArray())
|
||||
ps.setInt(14, api_layer)
|
||||
ps.addBatch()
|
||||
it[data] = SerialBlob(stream.toByteArray())
|
||||
it[api_layer] = my_api_layer
|
||||
print(8)*/
|
||||
}
|
||||
} catch(e: Exception) {
|
||||
println("e")
|
||||
}
|
||||
} else if (abs is TLMessageService) {
|
||||
ps_insert_or_ignore.setInt(1, abs.getId())
|
||||
ps_insert_or_ignore.setString(2, "service_message")
|
||||
ps_insert_or_ignore.setNull(3, Types.INTEGER)
|
||||
ps_insert_or_ignore.setNull(4, Types.INTEGER)
|
||||
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()
|
||||
Messages.insert {
|
||||
it[message_id] = abs.getId()
|
||||
//it[message_type] = "service_message"
|
||||
//it[api_layer] = my_api_layer
|
||||
}
|
||||
} else if (abs is TLMessageEmpty) {
|
||||
ps_insert_or_ignore.setInt(1, abs.getId())
|
||||
ps_insert_or_ignore.setString(2, "empty_message")
|
||||
ps_insert_or_ignore.setNull(3, Types.INTEGER)
|
||||
ps_insert_or_ignore.setNull(4, Types.INTEGER)
|
||||
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()
|
||||
Messages.insert {
|
||||
it[message_id] = abs.getId()
|
||||
//it[message_type] = "empty_message"
|
||||
//it[api_layer] = my_api_layer
|
||||
}
|
||||
} else {
|
||||
throw RuntimeException("Unexpected Message type: " + abs.javaClass)
|
||||
}
|
||||
print(9)
|
||||
}
|
||||
conn!!.setAutoCommit(false)
|
||||
ps.executeBatch()
|
||||
ps.clearBatch()
|
||||
ps_insert_or_ignore.executeBatch()
|
||||
ps_insert_or_ignore.clearBatch()
|
||||
conn!!.commit()
|
||||
conn!!.setAutoCommit(true)
|
||||
print("a")
|
||||
} // transaction
|
||||
logger.debug("Messages saved.")
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
throw RuntimeException("Exception shown above happened.")
|
||||
|
Loading…
Reference in New Issue
Block a user