add linenum to line, nextlinenum to window

This commit is contained in:
sawka 2022-09-20 17:01:25 -07:00
parent eab785409a
commit db142d97ec
4 changed files with 59 additions and 33 deletions

View File

@ -22,6 +22,7 @@ CREATE TABLE window (
curremoteownerid varchar(36) NOT NULL, curremoteownerid varchar(36) NOT NULL,
curremoteid varchar(36) NOT NULL, curremoteid varchar(36) NOT NULL,
curremotename varchar(50) NOT NULL, curremotename varchar(50) NOT NULL,
nextlinenum int NOT NULL,
winopts json NOT NULL, winopts json NOT NULL,
ownerid varchar(36) NOT NULL, ownerid varchar(36) NOT NULL,
sharemode varchar(12) NOT NULL, sharemode varchar(12) NOT NULL,
@ -63,10 +64,13 @@ CREATE TABLE remote_instance (
CREATE TABLE line ( CREATE TABLE line (
sessionid varchar(36) NOT NULL, sessionid varchar(36) NOT NULL,
windowid varchar(36) NOT NULL, windowid varchar(36) NOT NULL,
lineid varchar(36) NOT NULL,
userid varchar(36) NOT NULL, userid varchar(36) NOT NULL,
lineid varchar(36) NOT NULL,
ts bigint NOT NULL, ts bigint NOT NULL,
linenum int NOT NULL,
linenumtemp boolean NOT NULL,
linetype varchar(10) NOT NULL, linetype varchar(10) NOT NULL,
linelocal boolean NOT NULL,
text text NOT NULL, text text NOT NULL,
cmdid varchar(36) NOT NULL, cmdid varchar(36) NOT NULL,
ephemeral boolean NOT NULL, ephemeral boolean NOT NULL,

View File

@ -59,10 +59,13 @@ CREATE TABLE remote_instance (
CREATE TABLE line ( CREATE TABLE line (
sessionid varchar(36) NOT NULL, sessionid varchar(36) NOT NULL,
windowid varchar(36) NOT NULL, windowid varchar(36) NOT NULL,
lineid varchar(36) NOT NULL,
userid varchar(36) NOT NULL, userid varchar(36) NOT NULL,
lineid varchar(36) NOT NULL,
ts bigint NOT NULL, ts bigint NOT NULL,
linenum int NOT NULL,
linenumtemp boolean NOT NULL,
linetype varchar(10) NOT NULL, linetype varchar(10) NOT NULL,
linelocal boolean NOT NULL,
text text NOT NULL, text text NOT NULL,
cmdid varchar(36) NOT NULL, cmdid varchar(36) NOT NULL,
ephemeral boolean NOT NULL, ephemeral boolean NOT NULL,

View File

@ -515,13 +515,14 @@ func txCreateWindow(tx *TxWrap, sessionId string, curRemote RemotePtrType) strin
SessionId: sessionId, SessionId: sessionId,
WindowId: uuid.New().String(), WindowId: uuid.New().String(),
CurRemote: curRemote, CurRemote: curRemote,
NextLineNum: 1,
WinOpts: WindowOptsType{}, WinOpts: WindowOptsType{},
ShareMode: ShareModeLocal, ShareMode: ShareModeLocal,
ShareOpts: WindowShareOptsType{}, ShareOpts: WindowShareOptsType{},
} }
wmap := w.ToMap() wmap := w.ToMap()
query := `INSERT INTO window ( sessionid, windowid, curremoteownerid, curremoteid, curremotename, winopts, ownerid, sharemode, shareopts) query := `INSERT INTO window ( sessionid, windowid, curremoteownerid, curremoteid, curremotename, nextlinenum, winopts, ownerid, sharemode, shareopts)
VALUES (:sessionid,:windowid,:curremoteownerid,:curremoteid,:curremotename,:winopts,:ownerid,:sharemode,:shareopts)` VALUES (:sessionid,:windowid,:curremoteownerid,:curremoteid,:curremotename,:nextlinenum,:winopts,:ownerid,:sharemode,:shareopts)`
tx.NamedExecWrap(query, wmap) tx.NamedExecWrap(query, wmap)
return w.WindowId return w.WindowId
} }
@ -533,6 +534,9 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error {
if line.LineId == "" { if line.LineId == "" {
return fmt.Errorf("line must have lineid set") return fmt.Errorf("line must have lineid set")
} }
if line.LineNum != 0 {
return fmt.Errorf("line should not hage linenum set")
}
return WithTx(ctx, func(tx *TxWrap) error { return WithTx(ctx, func(tx *TxWrap) error {
var windowId string var windowId string
query := `SELECT windowid FROM window WHERE sessionid = ? AND windowid = ?` query := `SELECT windowid FROM window WHERE sessionid = ? AND windowid = ?`
@ -540,9 +544,14 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error {
if !hasWindow { if !hasWindow {
return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId) return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId)
} }
query = `INSERT INTO line ( sessionid, windowid, lineid, ts, userid, linetype, text, cmdid, ephemeral) query = `SELECT nextlinenum FROM window WHERE sessionid = ? AND windowid = ?`
VALUES (:sessionid,:windowid,:lineid,:ts,:userid,:linetype,:text,:cmdid,:ephemeral)` nextLineNum := tx.GetInt(query, line.SessionId, line.WindowId)
line.LineNum = int64(nextLineNum)
query = `INSERT INTO line ( sessionid, windowid, userid, lineid, ts, linenum, linenumtemp, linelocal, linetype, text, cmdid, ephemeral)
VALUES (:sessionid,:windowid,:userid,:lineid,:ts,:linenum,:linenumtemp,:linelocal,:linetype,:text,:cmdid,:ephemeral)`
tx.NamedExecWrap(query, line) tx.NamedExecWrap(query, line)
query = `UPDATE window SET nextlinenum = ? WHERE sessionid = ? AND windowid = ?`
tx.ExecWrap(query, nextLineNum+1, line.SessionId, line.WindowId)
if cmd != nil { if cmd != nil {
cmdMap := cmd.ToMap() cmdMap := cmd.ToMap()
query = ` query = `
@ -876,6 +885,8 @@ func ClearWindow(ctx context.Context, sessionId string, windowId string) (*Model
lineIds = tx.SelectStrings(query, sessionId, windowId) lineIds = tx.SelectStrings(query, sessionId, windowId)
query = `DELETE FROM line WHERE sessionid = ? AND windowid = ?` query = `DELETE FROM line WHERE sessionid = ? AND windowid = ?`
tx.ExecWrap(query, sessionId, windowId) tx.ExecWrap(query, sessionId, windowId)
query = `UPDATE window SET nextlinenum = 1 WHERE sessionid = ? AND windowid = ?`
tx.ExecWrap(query, sessionId, windowId)
return nil return nil
}) })
if txErr != nil { if txErr != nil {

View File

@ -202,6 +202,7 @@ type WindowType struct {
CurRemote RemotePtrType `json:"curremote"` CurRemote RemotePtrType `json:"curremote"`
WinOpts WindowOptsType `json:"winopts"` WinOpts WindowOptsType `json:"winopts"`
OwnerId string `json:"ownerid"` OwnerId string `json:"ownerid"`
NextLineNum int64 `json:"nextlinenum"`
ShareMode string `json:"sharemode"` ShareMode string `json:"sharemode"`
ShareOpts WindowShareOptsType `json:"shareopts"` ShareOpts WindowShareOptsType `json:"shareopts"`
Lines []*LineType `json:"lines"` Lines []*LineType `json:"lines"`
@ -218,6 +219,7 @@ func (w *WindowType) ToMap() map[string]interface{} {
rtn["curremoteownerid"] = w.CurRemote.OwnerId rtn["curremoteownerid"] = w.CurRemote.OwnerId
rtn["curremoteid"] = w.CurRemote.RemoteId rtn["curremoteid"] = w.CurRemote.RemoteId
rtn["curremotename"] = w.CurRemote.Name rtn["curremotename"] = w.CurRemote.Name
rtn["nextlinenum"] = w.NextLineNum
rtn["winopts"] = quickJson(w.WinOpts) rtn["winopts"] = quickJson(w.WinOpts)
rtn["ownerid"] = w.OwnerId rtn["ownerid"] = w.OwnerId
rtn["sharemode"] = w.ShareMode rtn["sharemode"] = w.ShareMode
@ -235,6 +237,7 @@ func WindowFromMap(m map[string]interface{}) *WindowType {
quickSetStr(&w.CurRemote.OwnerId, m, "curremoteownerid") quickSetStr(&w.CurRemote.OwnerId, m, "curremoteownerid")
quickSetStr(&w.CurRemote.RemoteId, m, "curremoteid") quickSetStr(&w.CurRemote.RemoteId, m, "curremoteid")
quickSetStr(&w.CurRemote.Name, m, "curremotename") quickSetStr(&w.CurRemote.Name, m, "curremotename")
quickSetInt64(&w.NextLineNum, m, "nextlinenum")
quickSetJson(&w.WinOpts, m, "winopts") quickSetJson(&w.WinOpts, m, "winopts")
quickSetStr(&w.OwnerId, m, "ownerid") quickSetStr(&w.OwnerId, m, "ownerid")
quickSetStr(&w.ShareMode, m, "sharemode") quickSetStr(&w.ShareMode, m, "sharemode")
@ -418,9 +421,12 @@ type RemoteInstance struct {
type LineType struct { type LineType struct {
SessionId string `json:"sessionid"` SessionId string `json:"sessionid"`
WindowId string `json:"windowid"` WindowId string `json:"windowid"`
UserId string `json:"userid"`
LineId string `json:"lineid"` LineId string `json:"lineid"`
Ts int64 `json:"ts"` Ts int64 `json:"ts"`
UserId string `json:"userid"` LineNum int64 `json:"linenum"`
LineNumTemp bool `json:"linenumtemp,omitempty"`
LineLocal bool `json:"linelocal"`
LineType string `json:"linetype"` LineType string `json:"linetype"`
Text string `json:"text,omitempty"` Text string `json:"text,omitempty"`
CmdId string `json:"cmdid,omitempty"` CmdId string `json:"cmdid,omitempty"`
@ -574,9 +580,10 @@ func makeNewLineCmd(sessionId string, windowId string, userId string, cmdId stri
rtn := &LineType{} rtn := &LineType{}
rtn.SessionId = sessionId rtn.SessionId = sessionId
rtn.WindowId = windowId rtn.WindowId = windowId
rtn.UserId = userId
rtn.LineId = uuid.New().String() rtn.LineId = uuid.New().String()
rtn.Ts = time.Now().UnixMilli() rtn.Ts = time.Now().UnixMilli()
rtn.UserId = userId rtn.LineLocal = true
rtn.LineType = LineTypeCmd rtn.LineType = LineTypeCmd
rtn.CmdId = cmdId rtn.CmdId = cmdId
return rtn return rtn
@ -586,9 +593,10 @@ func makeNewLineText(sessionId string, windowId string, userId string, text stri
rtn := &LineType{} rtn := &LineType{}
rtn.SessionId = sessionId rtn.SessionId = sessionId
rtn.WindowId = windowId rtn.WindowId = windowId
rtn.UserId = userId
rtn.LineId = uuid.New().String() rtn.LineId = uuid.New().String()
rtn.Ts = time.Now().UnixMilli() rtn.Ts = time.Now().UnixMilli()
rtn.UserId = userId rtn.LineLocal = true
rtn.LineType = LineTypeText rtn.LineType = LineTypeText
rtn.Text = text rtn.Text = text
return rtn return rtn