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:
parent
7067f98943
commit
047b95c6ea
@ -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'
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
@ -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)
|
||||||
@ -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()
|
|
||||||
if (peer is TLPeerChat) {
|
|
||||||
ps.setString(3, "group")
|
|
||||||
ps.setInt(4, peer.getChatId())
|
|
||||||
} else if (peer is TLPeerUser) {
|
|
||||||
var id = peer.getUserId()
|
|
||||||
if (id == this.user_manager.user!!.getId()) {
|
|
||||||
id = msg.getFromId()
|
|
||||||
}
|
|
||||||
ps.setString(3, "dialog")
|
|
||||||
ps.setInt(4, id)
|
|
||||||
} else if (peer is TLPeerChannel) {
|
|
||||||
ps.setString(3, "channel")
|
|
||||||
ps.setInt(4, peer.getChannelId())
|
|
||||||
} else {
|
|
||||||
throw RuntimeException("Unexpected Peer type: " + peer.javaClass)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (peer is TLPeerChannel) {
|
(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"))
|
||||||
// Message in a channel don't have a sender -> insert a null
|
(exposedLogger as ch.qos.logback.classic.Logger).level = ch.qos.logback.classic.Level.TRACE
|
||||||
ps.setNull(5, Types.INTEGER)
|
transaction({
|
||||||
} else {
|
logger.addLogger(StdOutSqlLogger)
|
||||||
ps.setInt(5, msg.getFromId())
|
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
|
||||||
|
println("4a")
|
||||||
|
try {
|
||||||
|
println("4b")
|
||||||
|
val a = Messages.insert {
|
||||||
|
println(5)
|
||||||
|
it[message_id] = msg.getId()
|
||||||
|
/*it[message_type] = "message"
|
||||||
|
|
||||||
if (msg.getFwdFrom() != null && msg.getFwdFrom().getFromId() != null) {
|
val peer = msg.getToId()
|
||||||
ps.setInt(6, msg.getFwdFrom().getFromId())
|
if (peer is TLPeerChat) {
|
||||||
} else {
|
it[source_type] = "group"
|
||||||
ps.setNull(6, Types.INTEGER)
|
it[source_id] = peer.getChatId()
|
||||||
}
|
} else if (peer is TLPeerUser) {
|
||||||
|
var id = peer.getUserId()
|
||||||
|
if (id == user_manager.user!!.getId()) {
|
||||||
|
id = msg.getFromId()
|
||||||
|
}
|
||||||
|
it[source_type] = "dialog"
|
||||||
|
it[source_id] = id
|
||||||
|
} else if (peer is TLPeerChannel) {
|
||||||
|
it[source_type] = "channel"
|
||||||
|
it[source_id] = peer.getChannelId()
|
||||||
|
} else {
|
||||||
|
throw RuntimeException("Unexpected Peer type: " + peer.javaClass)
|
||||||
|
}
|
||||||
|
|
||||||
var text = msg.getMessage()
|
// Messages in a channel don't have a sender -> insert a null
|
||||||
if ((text == null || text.equals("")) && msg.getMedia() != null) {
|
it[sender_id] = if (peer is TLPeerChannel) null else msg.getFromId()
|
||||||
val media = msg.getMedia()
|
print(6)
|
||||||
if (media is TLMessageMediaDocument) {
|
it[fwd_from_id] = msg.getFwdFrom()?.getFromId()
|
||||||
text = media.getCaption()
|
|
||||||
} else if (media is TLMessageMediaPhoto) {
|
var msg_text = msg.getMessage()
|
||||||
text = media.getCaption()
|
if ((msg_text == null || msg_text.equals("")) && msg.getMedia() != null) {
|
||||||
}
|
val media = msg.getMedia()
|
||||||
}
|
if (media is TLMessageMediaDocument) {
|
||||||
ps.setString(7, text)
|
msg_text = media.getCaption()
|
||||||
ps.setString(8, "" + msg.getDate())
|
} else if (media is TLMessageMediaPhoto) {
|
||||||
val f = FileManagerFactory.getFileManager(msg, user_manager, client)
|
msg_text = media.getCaption()
|
||||||
if (f == null) {
|
}
|
||||||
ps.setNull(9, Types.BOOLEAN)
|
}
|
||||||
ps.setNull(10, Types.VARCHAR)
|
it[text] = msg_text
|
||||||
ps.setNull(11, Types.VARCHAR)
|
it[time] = msg.getDate()
|
||||||
ps.setNull(12, Types.INTEGER)
|
|
||||||
} else {
|
val f = FileManagerFactory.getFileManager(msg, user_manager, client)
|
||||||
ps.setBoolean(9, true)
|
if (f == null) {
|
||||||
ps.setString(10, f.name)
|
it[has_media] = null
|
||||||
ps.setString(11, f.targetFilename)
|
it[media_type] = null
|
||||||
ps.setInt(12, f.size)
|
it[media_file] = null
|
||||||
}
|
it[media_size] = null
|
||||||
val stream = ByteArrayOutputStream()
|
} else {
|
||||||
msg.serializeBody(stream)
|
it[has_media] = true
|
||||||
ps.setBytes(13, stream.toByteArray())
|
it[media_type] = f.name
|
||||||
ps.setInt(14, api_layer)
|
it[media_file] = f.targetFilename
|
||||||
ps.addBatch()
|
it[media_size] = f.size
|
||||||
} else if (abs is TLMessageService) {
|
}
|
||||||
ps_insert_or_ignore.setInt(1, abs.getId())
|
print(7)
|
||||||
ps_insert_or_ignore.setString(2, "service_message")
|
val stream = ByteArrayOutputStream()
|
||||||
ps_insert_or_ignore.setNull(3, Types.INTEGER)
|
msg.serializeBody(stream)
|
||||||
ps_insert_or_ignore.setNull(4, Types.INTEGER)
|
it[data] = SerialBlob(stream.toByteArray())
|
||||||
ps_insert_or_ignore.setNull(5, Types.INTEGER)
|
it[api_layer] = my_api_layer
|
||||||
ps_insert_or_ignore.setNull(6, Types.INTEGER)
|
print(8)*/
|
||||||
ps_insert_or_ignore.setNull(7, Types.VARCHAR)
|
}
|
||||||
ps_insert_or_ignore.setNull(8, Types.INTEGER)
|
} catch(e: Exception) {
|
||||||
ps_insert_or_ignore.setNull(9, Types.BOOLEAN)
|
println("e")
|
||||||
ps_insert_or_ignore.setNull(10, Types.VARCHAR)
|
}
|
||||||
ps_insert_or_ignore.setNull(11, Types.VARCHAR)
|
} else if (abs is TLMessageService) {
|
||||||
ps_insert_or_ignore.setNull(12, Types.INTEGER)
|
Messages.insert {
|
||||||
ps_insert_or_ignore.setNull(13, Types.BLOB)
|
it[message_id] = abs.getId()
|
||||||
ps_insert_or_ignore.setInt(14, api_layer)
|
//it[message_type] = "service_message"
|
||||||
ps_insert_or_ignore.addBatch()
|
//it[api_layer] = my_api_layer
|
||||||
} else if (abs is TLMessageEmpty) {
|
}
|
||||||
ps_insert_or_ignore.setInt(1, abs.getId())
|
} else if (abs is TLMessageEmpty) {
|
||||||
ps_insert_or_ignore.setString(2, "empty_message")
|
Messages.insert {
|
||||||
ps_insert_or_ignore.setNull(3, Types.INTEGER)
|
it[message_id] = abs.getId()
|
||||||
ps_insert_or_ignore.setNull(4, Types.INTEGER)
|
//it[message_type] = "empty_message"
|
||||||
ps_insert_or_ignore.setNull(5, Types.INTEGER)
|
//it[api_layer] = my_api_layer
|
||||||
ps_insert_or_ignore.setNull(6, Types.INTEGER)
|
}
|
||||||
ps_insert_or_ignore.setNull(7, Types.VARCHAR)
|
} else {
|
||||||
ps_insert_or_ignore.setNull(8, Types.INTEGER)
|
throw RuntimeException("Unexpected Message type: " + abs.javaClass)
|
||||||
ps_insert_or_ignore.setNull(9, Types.BOOLEAN)
|
}
|
||||||
ps_insert_or_ignore.setNull(10, Types.VARCHAR)
|
print(9)
|
||||||
ps_insert_or_ignore.setNull(11, Types.VARCHAR)
|
}
|
||||||
ps_insert_or_ignore.setNull(12, Types.INTEGER)
|
print("a")
|
||||||
ps_insert_or_ignore.setNull(13, Types.BLOB)
|
} // transaction
|
||||||
ps_insert_or_ignore.setInt(14, api_layer)
|
logger.debug("Messages saved.")
|
||||||
ps_insert_or_ignore.addBatch()
|
|
||||||
} else {
|
|
||||||
throw RuntimeException("Unexpected Message type: " + abs.javaClass)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
conn!!.setAutoCommit(false)
|
|
||||||
ps.executeBatch()
|
|
||||||
ps.clearBatch()
|
|
||||||
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.")
|
||||||
|
Loading…
Reference in New Issue
Block a user