(orderByCommand *ast.OrderByCommand, table *Table, copyOfTable *Table, tableName string)
| 454 | } |
| 455 | |
| 456 | func (engine *DbEngine) getSortedTable(orderByCommand *ast.OrderByCommand, table *Table, copyOfTable *Table, tableName string) (*Table, error) { |
| 457 | sortPatterns := orderByCommand.SortPatterns |
| 458 | |
| 459 | columnNames := make([]string, 0) |
| 460 | for _, sortPattern := range sortPatterns { |
| 461 | columnNames = append(columnNames, sortPattern.ColumnName.Literal) |
| 462 | } |
| 463 | |
| 464 | missingColName := engine.getMissingColumnName(columnNames, table) |
| 465 | if missingColName != "" { |
| 466 | return nil, &ColumnDoesNotExistError{ |
| 467 | tableName: tableName, |
| 468 | columnName: missingColName, |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | rows := MapTableToRows(table).rows |
| 473 | |
| 474 | sort.Slice(rows, func(i, j int) bool { |
| 475 | howDeepWeSort := 0 |
| 476 | sortingType := sortPatterns[howDeepWeSort].Order.Type |
| 477 | columnToSort := sortPatterns[howDeepWeSort].ColumnName.Literal |
| 478 | |
| 479 | for rows[i][columnToSort].IsEqual(rows[j][columnToSort]) { |
| 480 | howDeepWeSort++ |
| 481 | sortingType = sortPatterns[howDeepWeSort].Order.Type |
| 482 | |
| 483 | if howDeepWeSort >= len(orderByCommand.SortPatterns) { |
| 484 | return true |
| 485 | } |
| 486 | columnToSort = sortPatterns[howDeepWeSort].ColumnName.Literal |
| 487 | } |
| 488 | |
| 489 | if sortingType == token.DESC { |
| 490 | return rows[i][columnToSort].isGreaterThan(rows[j][columnToSort]) |
| 491 | } |
| 492 | |
| 493 | return rows[i][columnToSort].isSmallerThan(rows[j][columnToSort]) |
| 494 | }) |
| 495 | |
| 496 | for _, row := range rows { |
| 497 | for _, newColumn := range copyOfTable.Columns { |
| 498 | value := row[newColumn.Name] |
| 499 | newColumn.Values = append(newColumn.Values, value) |
| 500 | } |
| 501 | } |
| 502 | return copyOfTable, nil |
| 503 | } |
| 504 | |
| 505 | func (engine *DbEngine) getMissingColumnName(columnNames []string, table *Table) string { |
| 506 | for _, columnName := range columnNames { |
no test coverage detected