66 lines
1.5 KiB
Nim
66 lines
1.5 KiB
Nim
import sequtils
|
|
import db_connector/db_sqlite
|
|
import nimlevenshtein
|
|
import strutils
|
|
import tables
|
|
import algorithm
|
|
let db = open("ArlongPark.db", "", "", "")
|
|
|
|
proc getReplyToStart(db : DbConn, a : var seq[string]) =
|
|
var last = a[^1]
|
|
let parent = db.getRow(sql"""select postId, replyPostId, isReply from SubPost where PostId = ?""", last)
|
|
|
|
if parent[0] == "":
|
|
return
|
|
|
|
let isReply = parseBool parent[2]
|
|
|
|
if isReply:
|
|
a.add(parent[1])
|
|
getReplyToStart(db, a)
|
|
|
|
proc getReplyToEnd(db : DbConn, a : var seq[string]) =
|
|
var last = a[^1]
|
|
let parent = db.getRow(sql"""select postId, replyPostId, hasReply from SubPost where replyPostId = ?""", last)
|
|
|
|
if parent[0] == "":
|
|
return
|
|
|
|
let hasReply = parseBool parent[2]
|
|
a.add(parent[0])
|
|
|
|
if hasReply:
|
|
getReplyToEnd(db, a)
|
|
|
|
var result = newTable[string, seq[string]]()
|
|
for x in db.fastRows(sql"select postId, replyPostId, isReply, hasReply from SubPost where isReply = 'true' or hasReply = 'true'"):
|
|
let isReply = parseBool x[2]
|
|
let hasReply = parseBool x[3]
|
|
|
|
var originChain : seq[string]
|
|
var childChain : seq[string]
|
|
if isReply:
|
|
var chain = @[x[1]]
|
|
getReplyToStart(db, chain)
|
|
originChain = chain
|
|
originChain.reverse()
|
|
if hasReply:
|
|
var chain = @[x[0]]
|
|
getReplyToEnd(db, chain)
|
|
childChain = chain
|
|
|
|
let origin =
|
|
if isReply:
|
|
originChain[0]
|
|
else:
|
|
x[0]
|
|
let endPostId =
|
|
if hasReply:
|
|
childChain[^1]
|
|
else:
|
|
x[0]
|
|
echo (origin, endPostId)
|
|
|
|
|
|
|
|
echo "=="
|