-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui_data.go
More file actions
955 lines (847 loc) · 23.9 KB
/
tui_data.go
File metadata and controls
955 lines (847 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
package main
import (
"database/sql"
"fmt"
"slices"
"strings"
"time"
)
// e.table.SetData based on e.records and e.pointer
func (e *Editor) renderData() {
// number of rows to send to TableView
rowCount := len(e.buffer)
// if table does not fill the screen, we can add a row for the insert row
if len(e.insertRow) > 0 && e.table.rowsHeight > rowCount {
rowCount++
}
// reverse layout to make it easier to layout inserted rows
i := rowCount - 1
normalizedRows := make([]Row, rowCount)
if len(e.insertRow) > 0 {
normalizedRows[i] = BottomBorderRow
i--
normalizedRows[i] = Row{
state: RowStateInsert,
data: e.insertRow,
modified: nil,
}
i--
}
for ; i >= 0; i-- {
ptr := (e.pointer + i) % len(e.buffer)
// only if the table fills the screen
if len(e.insertRow) > 0 && e.table.rowsHeight == rowCount {
ptr = (ptr + 1) % len(e.buffer)
}
normalizedRows[i] = e.buffer[ptr] // Reference to Row, not a copy
}
e.table.SetDataReferences(normalizedRows)
}
// extractKeys returns a copy of the key values from a row
func (e *Editor) extractKeys(row []any) []any {
if row == nil || len(e.relation.Key) == 0 {
return nil
}
keys := make([]any, len(e.relation.Key))
for i, keyIdx := range e.relation.Key {
if keyIdx < len(row) {
keys[i] = row[keyIdx]
}
}
return keys
}
// keysEqual compares two key slices for equality
func keysEqual(k1, k2 []any) bool {
if len(k1) != len(k2) {
return false
}
for i := range k1 {
if k1[i] != k2[i] {
return false
}
}
return true
}
// getViewportBoundaryKeys returns the key values of the first and last visible rows
func (e *Editor) getViewportBoundaryKeys() (firstKeys, lastKeys []any) {
if len(e.buffer) == 0 {
return nil, nil
}
// Get first visible row
firstRow := e.buffer[e.pointer]
if firstRow.data != nil {
firstKeys = e.extractKeys(firstRow.data)
}
// Get last visible row (accounting for nil sentinel at end)
lastIdx := e.lastRowIdx()
lastRow := e.buffer[lastIdx]
// If last row is nil sentinel, try previous row
if lastRow.data == nil && lastIdx != e.pointer {
lastIdx = (lastIdx - 1 + len(e.buffer)) % len(e.buffer)
lastRow = e.buffer[lastIdx]
}
if lastRow.data != nil {
lastKeys = e.extractKeys(lastRow.data)
}
return firstKeys, lastKeys
}
// diffRows compares two rows and returns the indices of columns that differ
func diffRows(oldRow, newRow []any) []int {
if len(oldRow) != len(newRow) {
return nil
}
var modified []int
for i := range oldRow {
if oldRow[i] != newRow[i] {
modified = append(modified, i)
}
}
return modified
}
// rowsEqual checks if two slices of Rows are equal by comparing their data
func rowsEqual(rows1, rows2 []Row) bool {
if len(rows1) != len(rows2) {
return false
}
for i := range rows1 {
if len(rows1[i].data) != len(rows2[i].data) {
return false
}
for j := range rows1[i].data {
if rows1[i].data[j] != rows2[i].data[j] {
return false
}
}
}
return true
}
// findMatchingIndexes finds all pairs of matching rows between previousRows and currentRows
// based on primary key equality. Returns a slice of [2]int where [0] is the index in previousRows
// and [1] is the index in currentRows.
func (e *Editor) findMatchingIndexes(previousRows, currentRows []Row) [][2]int {
var matches [][2]int
// Build a map of current row keys to their indexes for faster lookup
currentKeyMap := make(map[string]int)
for i, row := range currentRows {
keys := e.extractKeys(row.data)
if keys != nil {
keyStr := fmt.Sprintf("%v", keys)
currentKeyMap[keyStr] = i
}
}
// Find matches by looking up each previous row's key in the current map
for i, prevRow := range previousRows {
prevKeys := e.extractKeys(prevRow.data)
if prevKeys != nil {
keyStr := fmt.Sprintf("%v", prevKeys)
if currIdx, exists := currentKeyMap[keyStr]; exists {
matches = append(matches, [2]int{i, currIdx})
}
}
}
return matches
}
// checkRowsExistInDB checks if rows with the given primary keys still exist in the database
// Returns a slice of booleans, one for each key, indicating existence
func (e *Editor) checkRowsExistInDB(keys [][]any) ([]bool, error) {
if len(keys) == 0 {
return nil, nil
}
// Build the IN clause query
keyColCount := len(e.relation.Key)
if keyColCount == 0 {
return nil, fmt.Errorf("no primary key defined for relation")
}
// Get key column names
keyColNames := make([]string, len(e.relation.Key))
for i, keyIdx := range e.relation.Key {
if keyIdx < len(e.relation.Columns) {
keyColNames[i] = e.relation.Columns[keyIdx].Name
}
}
// For single-column keys, we can use a simple IN clause
// For multi-column keys, we need to use OR conditions
var query string
var args []any
if keyColCount == 1 {
// Simple case: single column key
keyColName := keyColNames[0]
query = fmt.Sprintf("SELECT %s FROM %s WHERE %s IN (",
keyColName, e.relation.Name, keyColName)
placeholders := make([]string, len(keys))
for i, key := range keys {
placeholders[i] = "?"
args = append(args, key[0])
}
query += fmt.Sprintf("%s)", strings.Join(placeholders, ", "))
} else {
// Complex case: multi-column key
query = fmt.Sprintf("SELECT %s FROM %s WHERE ",
strings.Join(keyColNames, ", "), e.relation.Name)
conditions := make([]string, len(keys))
for i, key := range keys {
keyConditions := make([]string, keyColCount)
for j := 0; j < keyColCount; j++ {
keyConditions[j] = fmt.Sprintf("%s = ?", keyColNames[j])
args = append(args, key[j])
}
conditions[i] = fmt.Sprintf("(%s)", strings.Join(keyConditions, " AND "))
}
query += strings.Join(conditions, " OR ")
}
rows, err := e.db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
// Build a set of existing keys
existingKeys := make(map[string]bool)
scanTargets := make([]any, keyColCount)
scanPtrs := make([]any, keyColCount)
for i := range scanTargets {
scanPtrs[i] = &scanTargets[i]
}
for rows.Next() {
if err := rows.Scan(scanPtrs...); err != nil {
return nil, err
}
keyStr := fmt.Sprintf("%v", scanTargets)
existingKeys[keyStr] = true
}
// Check each input key against the set
result := make([]bool, len(keys))
for i, key := range keys {
keyStr := fmt.Sprintf("%v", key)
result[i] = existingKeys[keyStr]
}
return result, nil
}
// diffAndPopulateBuffer performs the diffing algorithm between previousRows and currentRows
// and populates the buffer with appropriate state markers (Normal/New/Deleted/Modified)
func (e *Editor) diffAndPopulateBuffer(currentRows []Row) {
// Use separate ptr variable instead of incrementing e.pointer
ptr := e.pointer
bufferSize := len(e.buffer)
// Find all matching indexes
matches := e.findMatchingIndexes(e.previousRows, currentRows)
// Track which indexes have been processed
prevIdx := 0
currIdx := 0
// Helper to add a row to buffer and advance ptr
addToBuffer := func(row Row) bool {
if ptr >= bufferSize {
return false // Buffer full
}
e.buffer[ptr] = row
ptr++
return true
}
// Process sequences between matches
for matchNum, match := range matches {
prevMatchIdx := match[0]
currMatchIdx := match[1]
// Process deleted rows (only in previousRows)
for i := prevIdx; i < prevMatchIdx; i++ {
deletedRow := e.previousRows[i]
deletedRow.state = RowStateDeleted
deletedRow.modified = nil
if !addToBuffer(deletedRow) {
return // Buffer full
}
}
// Process inserted rows (only in currentRows) before this match
for i := currIdx; i < currMatchIdx; i++ {
insertedRow := currentRows[i]
insertedRow.state = RowStateNew
insertedRow.modified = nil
if !addToBuffer(insertedRow) {
return // Buffer full
}
}
// Process the matched row (check for modifications)
matchedRow := currentRows[currMatchIdx]
matchedRow.state = RowStateNormal
matchedRow.modified = diffRows(e.previousRows[prevMatchIdx].data, currentRows[currMatchIdx].data)
if !addToBuffer(matchedRow) {
return // Buffer full
}
// Move indexes forward
prevIdx = prevMatchIdx + 1
currIdx = currMatchIdx + 1
// Special handling for last sequence
if matchNum == len(matches)-1 {
// Check if there are remaining rows in previousRows
if prevIdx < len(e.previousRows) {
// Build list of keys to check
var keysToCheck [][]any
for i := prevIdx; i < len(e.previousRows); i++ {
key := e.extractKeys(e.previousRows[i].data)
if key != nil {
keysToCheck = append(keysToCheck, key)
}
}
// Check if these rows still exist in DB
if len(keysToCheck) > 0 {
exists, err := e.checkRowsExistInDB(keysToCheck)
if err == nil {
// Add deleted rows for those that still exist in DB
keyIdx := 0
for i := prevIdx; i < len(e.previousRows); i++ {
if keyIdx < len(exists) && exists[keyIdx] {
deletedRow := e.previousRows[i]
deletedRow.state = RowStateDeleted
deletedRow.modified = nil
if !addToBuffer(deletedRow) {
return // Buffer full
}
}
keyIdx++
}
}
}
}
// Fill remaining buffer with inserted rows from currentRows
for i := currIdx; i < len(currentRows); i++ {
insertedRow := currentRows[i]
insertedRow.state = RowStateNew
insertedRow.modified = nil
if !addToBuffer(insertedRow) {
return // Buffer full
}
}
// Done processing
return
}
}
// If there were no matches, handle all rows
if len(matches) == 0 {
// All previousRows are deleted (if they exist in DB)
if len(e.previousRows) > 0 {
var keysToCheck [][]any
for _, row := range e.previousRows {
key := e.extractKeys(row.data)
if key != nil {
keysToCheck = append(keysToCheck, key)
}
}
if len(keysToCheck) > 0 {
exists, err := e.checkRowsExistInDB(keysToCheck)
if err == nil {
for i, row := range e.previousRows {
if i < len(exists) && exists[i] {
deletedRow := row
deletedRow.state = RowStateDeleted
deletedRow.modified = nil
if !addToBuffer(deletedRow) {
return // Buffer full
}
}
}
}
}
}
// All currentRows are new
for _, row := range currentRows {
insertedRow := row
insertedRow.state = RowStateNew
insertedRow.modified = nil
if !addToBuffer(insertedRow) {
return // Buffer full
}
}
}
}
// refresh reloads data from the current position and applies change tracking
func (e *Editor) refresh() error {
if e.relation == nil || e.relation.DB == nil {
return fmt.Errorf("no database connection available")
}
// Get current row ID for refresh anchor
if len(e.buffer) == 0 || e.buffer[e.pointer].data == nil {
return nil // Nothing to refresh
}
// Extract key values from the row using the key column indices
id := make([]any, len(e.relation.Key))
for i, keyIdx := range e.relation.Key {
id[i] = e.buffer[e.pointer].data[keyIdx]
}
// Close existing query before refresh
e.queryMu.Lock()
if e.query != nil {
e.query.Close()
e.query = nil
}
e.queryMu.Unlock()
// Stop refresh timer during refresh operation
e.stopRefreshTimer()
// Prepare query
headers := e.table.GetHeaders()
selectCols := make([]string, len(headers))
for i, col := range headers {
selectCols[i] = col.Name
}
colCount := len(headers)
if colCount == 0 {
return nil
}
// Query from current position (fromTop=true)
rows, err := e.relation.QueryRows(selectCols, nil, id, true, true)
if err != nil {
return err
}
// Scan all rows into local currentRows
e.pointer = 0
scanTargets := make([]any, colCount)
var currentRows []Row
for rows.Next() && len(currentRows) < e.table.rowsHeight {
row := make([]any, colCount)
for j := 0; j < colCount; j++ {
scanTargets[j] = &row[j]
}
if err := rows.Scan(scanTargets...); err != nil {
rows.Close()
return err
}
currentRows = append(currentRows, Row{
state: RowStateNormal,
data: row,
modified: nil,
})
}
rows.Close()
// Apply diff tracking if previous data exists and differs
if e.previousRows != nil && !rowsEqual(e.previousRows, currentRows) {
e.diffAndPopulateBuffer(currentRows)
} else {
// No diffing needed - just populate buffer normally
for i := 0; i < len(currentRows) && i < len(e.buffer); i++ {
e.buffer[i] = currentRows[i]
}
// Mark end with empty row if we didn't fill the buffer
if len(currentRows) < len(e.buffer) {
e.buffer[len(currentRows)] = BottomBorderRow
}
}
// Clone currentRows to previousRows for next refresh
e.previousRows = make([]Row, len(currentRows))
copy(e.previousRows, currentRows)
e.renderData()
e.table.Select(e.table.selectedRow, e.table.selectedCol)
e.startRefreshTimer()
return nil
}
// id can be nil, in which case load from the top or bottom
func (e *Editor) loadFromRowId(id []any, fromTop bool, focusColumn int) error {
if e.relation == nil || e.relation.DB == nil {
return fmt.Errorf("no database connection available")
}
// reset buffer to nil
e.buffer = nil
// Stop refresh timer when starting a new query
e.stopRefreshTimer()
headers := e.table.GetHeaders()
selectCols := make([]string, len(headers))
for i, col := range headers {
selectCols[i] = col.Name
}
colCount := len(headers)
if colCount == 0 {
return nil
}
var rows *sql.Rows
var err error
if fromTop {
// Load from top: use QueryRows with inclusive true, scrollDown true
rows, err = e.relation.QueryRows(selectCols, nil, id, true, true)
if err != nil {
return err
}
e.queryMu.Lock()
e.query = rows
e.scrollDown = true
e.queryMu.Unlock()
e.startRowsTimer()
// Scan rows into e.records starting from pointer
e.pointer = 0
scanTargets := make([]any, colCount)
// Scan all rows from database into local currentRows
var currentRows []Row
for rows.Next() && len(currentRows) < e.table.rowsHeight {
row := make([]any, colCount)
for j := 0; j < colCount; j++ {
scanTargets[j] = &row[j]
}
if err := rows.Scan(scanTargets...); err != nil {
return err
}
currentRows = append(currentRows, Row{
state: RowStateNormal,
data: row,
modified: nil,
})
}
// Populate buffer normally
for i := 0; i < len(currentRows); i++ {
e.buffer = append(e.buffer, currentRows[i])
}
// Mark end with empty row if we didn't fill the buffer
if len(currentRows) < e.table.rowsHeight {
e.buffer = append(e.buffer, BottomBorderRow)
}
// Set previousRows to currentRows
e.previousRows = currentRows
} else {
// Load from bottom: use QueryRows with inclusive true, scrollDown false
rows, err = e.relation.QueryRows(selectCols, nil, id, true, false)
if err != nil {
return err
}
e.queryMu.Lock()
e.query = rows
e.scrollDown = false
e.queryMu.Unlock()
e.startRowsTimer()
// Scan rows into e.records in reverse, starting from end of buffer
e.pointer = 0
scanTargets := make([]any, colCount)
// Scan all rows from database into local currentRows
var currentRows []Row
for rows.Next() {
row := make([]any, colCount)
for j := 0; j < colCount; j++ {
scanTargets[j] = &row[j]
}
if err := rows.Scan(scanTargets...); err != nil {
return err
}
currentRows = append(currentRows, Row{
state: RowStateNormal,
data: row,
modified: nil,
})
if len(currentRows) >= e.table.rowsHeight-1 {
break
}
}
// Reverse currentRows for fromBottom
slices.Reverse(currentRows)
// Populate buffer normally
for i := 0; i < len(currentRows); i++ {
e.buffer = append(e.buffer, currentRows[i])
}
// Mark end with empty row if we didn't fill the buffer
if len(currentRows) < e.table.rowsHeight {
e.buffer = append(e.buffer, BottomBorderRow)
}
// Set previousRows to currentRows
e.previousRows = currentRows
e.pointer = 0
}
e.renderData()
// Focus on the specified column
if fromTop {
e.table.Select(e.table.selectedRow, focusColumn)
} else {
e.table.Select(len(e.buffer)-1, focusColumn)
}
return nil
}
// nextRows fetches the next i rows from e.rows and resets the auto-close timer
// when there are no more rows, adds a nil sentinel to mark the end
// returns bool, err. bool if the edge of table is reached
func (e *Editor) nextRows(i int) (bool, error) {
// Only reuse query if it's in the correct direction (scrollDown)
// Check if we're already at the end (last record is nil)
if e.isAtBottom() {
return true, nil // No-op, already at end of data
}
// this is a problem
needNewQuery := e.query == nil || !e.scrollDown
if needNewQuery && e.query != nil {
// Close existing query if it's in the wrong direction
e.queryMu.Lock()
e.query.Close()
e.query = nil
e.queryMu.Unlock()
}
if needNewQuery {
// Stop refresh timer when starting a new query
e.stopRefreshTimer()
params := make([]any, len(e.relation.Key))
lastRecordIdx := (e.pointer - 1 + len(e.buffer)) % len(e.buffer)
if e.buffer[lastRecordIdx].data == nil {
return false, nil // Can't query from nil record
}
for i, keyIdx := range e.relation.Key {
if keyIdx < len(e.buffer[lastRecordIdx].data) {
params[i] = e.buffer[lastRecordIdx].data[keyIdx]
}
}
headers := e.table.GetHeaders()
selectCols := make([]string, len(headers))
for i, col := range headers {
selectCols[i] = col.Name
}
newQuery, err := e.relation.QueryRows(selectCols, nil, params, false, true)
if err != nil {
return false, err
}
e.query = newQuery
e.scrollDown = true
e.startRowsTimer()
}
// Signal timer reset
if e.rowsTimerReset != nil {
select {
case e.rowsTimerReset <- struct{}{}:
default:
}
}
colCount := len(e.table.GetHeaders())
if colCount == 0 {
return false, nil
}
// Get a local reference to the query to avoid holding the lock during scanning
e.queryMu.Lock()
query := e.query
e.queryMu.Unlock()
if query == nil {
return false, fmt.Errorf("query is nil")
}
scanTargets := make([]any, colCount)
rowsFetched := 0
for ; rowsFetched < i && query.Next(); rowsFetched++ {
row := make([]any, colCount)
for j := 0; j < colCount; j++ {
scanTargets[j] = &row[j]
}
if err := query.Scan(scanTargets...); err != nil {
return false, err
}
bufferIdx := (e.pointer + rowsFetched) % len(e.buffer)
e.buffer[bufferIdx] = Row{state: RowStateNormal, data: row}
}
// new pointer position
e.incrPtr(rowsFetched)
// If we fetched fewer rows than requested, we've reached the end
if rowsFetched < i {
e.incrPtr(1)
e.buffer[e.lastRowIdx()] = BottomBorderRow // Mark end of data
}
// Update previousRows to match current buffer view (for accurate diff on next refresh)
e.previousRows = make([]Row, 0, len(e.buffer))
for i := 0; i < len(e.buffer); i++ {
row := e.buffer[(e.pointer+i)%len(e.buffer)]
if row.data == nil {
break // Stop at nil sentinel
}
e.previousRows = append(e.previousRows, row)
}
e.renderData()
return rowsFetched < i, query.Err()
}
func (e *Editor) lastRowIdx() int {
return (e.pointer + len(e.buffer) - 1) % len(e.buffer)
}
// isAtBottom returns true if the table is currently at the bottom of the data
// (i.e., the last row in the buffer is nil, indicating we've reached the end)
func (e *Editor) isAtBottom() bool {
return e.buffer[e.lastRowIdx()].data == nil
}
func (e *Editor) incrPtr(n int) {
e.pointer = (e.pointer + n) % len(e.buffer)
}
// prevRows fetches the previous i rows (scrolling backwards in the circular buffer)
// returns whether rows were moved (false means you'ved the edge)
func (e *Editor) prevRows(i int) (bool, error) {
// Only reuse query if it's in the correct direction (!scrollDown)
e.queryMu.Lock()
needNewQuery := e.query == nil || e.scrollDown
if needNewQuery && e.query != nil {
// Close existing query if it's in the wrong direction
e.query.Close()
e.query = nil
}
e.queryMu.Unlock()
if needNewQuery {
// Stop refresh timer when starting a new query
e.stopRefreshTimer()
if len(e.buffer) == 0 || e.buffer[e.pointer].data == nil {
return false, nil // Can't query from nil or empty records
}
params := make([]any, len(e.relation.Key))
for i, keyIdx := range e.relation.Key {
if keyIdx < len(e.buffer[e.pointer].data) {
params[i] = e.buffer[e.pointer].data[keyIdx]
}
}
headers := e.table.GetHeaders()
selectCols := make([]string, len(headers))
for i, col := range headers {
selectCols[i] = col.Name
}
newQuery, err := e.relation.QueryRows(selectCols, nil, params, false, false)
if err != nil {
return false, err
}
e.queryMu.Lock()
e.query = newQuery
e.scrollDown = false
e.queryMu.Unlock()
e.startRowsTimer()
}
// Signal timer reset
if e.rowsTimerReset != nil {
select {
case e.rowsTimerReset <- struct{}{}:
default:
}
}
colCount := len(e.table.GetHeaders())
if colCount == 0 {
return false, nil
}
// Get a local reference to the query to avoid holding the lock during scanning
e.queryMu.Lock()
query := e.query
e.queryMu.Unlock()
if query == nil {
return false, fmt.Errorf("query is nil")
}
scanTargets := make([]any, colCount)
rowsFetched := 0
for ; rowsFetched < i && query.Next(); rowsFetched++ {
row := make([]any, colCount)
for j := 0; j < colCount; j++ {
scanTargets[j] = &row[j]
}
if err := query.Scan(scanTargets...); err != nil {
return false, err
}
e.pointer = e.lastRowIdx() // Move pointer backwards in the circular buffer
e.buffer[e.pointer] = Row{state: RowStateNormal, data: row}
}
// Update previousRows to match current buffer view (for accurate diff on next refresh)
e.previousRows = make([]Row, 0, len(e.buffer))
for i := 0; i < len(e.buffer); i++ {
row := e.buffer[(e.pointer+i)%len(e.buffer)]
if row.data == nil {
break // Stop at nil sentinel
}
e.previousRows = append(e.previousRows, row)
}
e.renderData()
return rowsFetched < i, query.Err()
}
// startRowsTimer starts or restarts the timer for auto-closing queries
func (e *Editor) startRowsTimer() {
// Stop existing timer if any
if e.rowsTimer != nil {
e.rowsTimer.Stop()
}
if e.rowsTimerReset != nil {
close(e.rowsTimerReset)
}
// Start new timer
e.rowsTimerReset = make(chan struct{})
e.rowsTimer = time.NewTimer(RowsTimerInterval)
// Timer goroutine to handle resets
go func() {
resetChan := e.rowsTimerReset
timer := e.rowsTimer
if timer == nil {
return
}
for {
select {
case _, ok := <-resetChan:
if !ok {
// Channel closed, exit goroutine
return
}
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
timer.Reset(RowsTimerInterval)
case <-timer.C:
e.stopRowsTimer()
return
}
}
}()
}
// stopRowsTimer stops the timer and closes the rows if active
func (e *Editor) stopRowsTimer() {
if e.rowsTimer != nil {
e.rowsTimer.Stop()
e.rowsTimer = nil
}
if e.rowsTimerReset != nil {
close(e.rowsTimerReset)
e.rowsTimerReset = nil
}
e.queryMu.Lock()
if e.query != nil {
if err := e.query.Close(); err != nil {
panic(err)
}
e.query = nil
}
e.queryMu.Unlock()
e.startRefreshTimer()
}
// startRefreshTimer starts a timer that refreshes data every 300ms
func (e *Editor) startRefreshTimer() {
if e.app == nil {
return
}
// Stop existing refresh timer if any
e.stopRefreshTimer()
// Start new refresh timer
e.refreshTimerStop = make(chan struct{})
e.refreshTimer = time.NewTimer(RefreshTimerInterval)
// Refresh timer goroutine
go func() {
stopChan := e.refreshTimerStop
timer := e.refreshTimer
app := e.app
if stopChan == nil || timer == nil {
return
}
for {
select {
case _, ok := <-stopChan:
if !ok {
// Channel closed, exit goroutine
return
}
case <-timer.C:
if app != nil && e.relation != nil && e.relation.DB != nil {
// Calculate data height before queueing the update
terminalHeight := getTerminalHeight()
dataHeight := terminalHeight - 3 // 3 lines for picker bar, status bar, command palette
app.QueueUpdateDraw(func() {
// Update table rowsHeight before loading rows from database
e.table.UpdateRowsHeightFromRect(dataHeight)
e.refresh()
})
}
timer.Reset(RefreshTimerInterval)
}
}
}()
}
// stopRefreshTimer stops the refresh timer if active
func (e *Editor) stopRefreshTimer() {
if e.refreshTimer != nil {
e.refreshTimer.Stop()
e.refreshTimer = nil
}
if e.refreshTimerStop != nil {
close(e.refreshTimerStop)
e.refreshTimerStop = nil
}
}