MCPcopy Create free account
hub / github.com/LissaGreense/GO4SQL / getSortedTable

Method getSortedTable

engine/engine.go:456–503  ·  view source on GitHub ↗
(orderByCommand *ast.OrderByCommand, table *Table, copyOfTable *Table, tableName string)

Source from the content-addressed store, hash-verified

454}
455
456func (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
505func (engine *DbEngine) getMissingColumnName(columnNames []string, table *Table) string {
506 for _, columnName := range columnNames {

Calls 5

getMissingColumnNameMethod · 0.95
MapTableToRowsFunction · 0.85
IsEqualMethod · 0.65
isGreaterThanMethod · 0.65
isSmallerThanMethod · 0.65

Tested by

no test coverage detected