Skip to content

Commit

Permalink
Merge pull request #154 from jorgerojas26/fix/delete-row
Browse files Browse the repository at this point in the history
Partial fix to delete row functionality
  • Loading branch information
jorgerojas26 authored Jan 22, 2025
2 parents 6feb35c + f63c5f8 commit 65b46fa
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions components/results_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ func (table *ResultsTable) AppendNewRow(cells []models.CellValue, index int, UUI
for i, cell := range cells {
tableCell := tview.NewTableCell(cell.Value.(string))
tableCell.SetExpansion(1)
// Appended rows have a reference to the row UUID so we can identify them later
// Also, rows that have columns marked to be UPDATED will have a reference to the type of the new value (NULL, EMPTY, DEFAULT)
// So, the cell reference will be used to determine if the row/column is an inserted row or if it's an UPDATED row
// there might be a better way to do this, but it works for now
tableCell.SetReference(UUID)
tableCell.SetTextColor(app.Styles.PrimaryTextColor)
tableCell.SetBackgroundColor(tcell.ColorDarkGreen)
Expand Down Expand Up @@ -342,6 +346,7 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event
case commands.RecordsMenu:
table.Menu.SetSelectedOption(1)
table.UpdateRows(table.GetRecords())
table.colorChangedCells()
table.AddInsertedRows()
case commands.ColumnsMenu:
table.Menu.SetSelectedOption(2)
Expand Down Expand Up @@ -415,17 +420,8 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event
}
} else if command == commands.Delete {
if table.Menu.GetSelectedOption() == 1 {
isAnInsertedRow := false
indexOfInsertedRow := -1

for i, insertedRow := range *table.state.listOfDBChanges {
cellReference := table.GetCell(selectedRowIndex, 0).GetReference()

if cellReference != nil && insertedRow.PrimaryKeyInfo[0].Value == cellReference {
isAnInsertedRow = true
indexOfInsertedRow = i
}
}
isAnInsertedRow, indexOfInsertedRow := table.isAnInsertedRow(selectedRowIndex)

if isAnInsertedRow {
*table.state.listOfDBChanges = append((*table.state.listOfDBChanges)[:indexOfInsertedRow], (*table.state.listOfDBChanges)[indexOfInsertedRow+1:]...)
Expand All @@ -438,7 +434,7 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event
}
}
} else {
table.AppendNewChange(models.DMLDeleteType, selectedRowIndex, -1, models.CellValue{})
table.AppendNewChange(models.DMLDeleteType, selectedRowIndex, -1, models.CellValue{TableColumnIndex: -1, TableRowIndex: selectedRowIndex})
}

}
Expand Down Expand Up @@ -749,6 +745,7 @@ func (table *ResultsTable) GetPrimaryKeyColumnNames() []string {
func (table *ResultsTable) SetRecords(rows [][]string) {
table.state.records = rows
table.UpdateRows(rows)
table.colorChangedCells()
}

func (table *ResultsTable) SetColumns(columns [][]string) {
Expand Down Expand Up @@ -1048,11 +1045,11 @@ func (table *ResultsTable) AppendNewChange(changeType models.DMLType, rowIndex i
dmlChangeAlreadyExists := false

// If the column has a reference, it means it's an inserted rowIndex
// These is maybe a better way to detect it is an inserted row
// There is maybe a better way to detect it is an inserted row
tableCell := table.GetCell(rowIndex, colIndex)
tableCellReference := tableCell.GetReference()

isAnInsertedRow := tableCellReference != nil && tableCellReference.(string) != "NULL&" && tableCellReference.(string) != "EMPTY&" && tableCellReference.(string) != "DEFAULT&"
isAnInsertedRow, _ := table.isAnInsertedRow(rowIndex)

if isAnInsertedRow {
table.MutateInsertedRowCell(tableCellReference.(string), value)
Expand Down Expand Up @@ -1375,3 +1372,37 @@ func (table *ResultsTable) UpdateSidebar() {

}
}

func (table *ResultsTable) isAnInsertedRow(rowIndex int) (isAnInsertedRow bool, DBChangeIndex int) {
for i, dmlChange := range *table.state.listOfDBChanges {
for _, value := range dmlChange.Values {
if value.TableRowIndex != rowIndex {
continue
}
cellReference := table.GetCell(rowIndex, 0).GetReference()
if cellReference == nil {
break
}
switch cellReference.(string) {
case "NULL&", "EMPTY&", "DEFAULT&":
default:
return true, i
}
break
}
}
return false, -1
}

func (table *ResultsTable) colorChangedCells() {
for _, dmlChange := range *table.state.listOfDBChanges {
switch dmlChange.Type {
case models.DMLDeleteType:
table.SetRowColor(dmlChange.Values[0].TableRowIndex, colorTableDelete)
case models.DMLUpdateType:
for _, value := range dmlChange.Values {
table.SetCellColor(value.TableRowIndex, value.TableColumnIndex, colorTableChange)
}
}
}
}

0 comments on commit 65b46fa

Please sign in to comment.