toBaseStatements converts omni's statement list into []base.Statement while enforcing byte-contiguity and position adjacency. Any bytes that omni did not attribute to a statement (gaps) are absorbed into the following statement so the output remains contiguous.
(sql string, omniStmts []omnimssql.Statement)
| 51 | // not attribute to a statement (gaps) are absorbed into the following |
| 52 | // statement so the output remains contiguous. |
| 53 | func toBaseStatements(sql string, omniStmts []omnimssql.Statement) []base.Statement { |
| 54 | if len(omniStmts) == 0 { |
| 55 | return nil |
| 56 | } |
| 57 | result := make([]base.Statement, 0, len(omniStmts)) |
| 58 | positionMapper := base.NewByteOffsetPositionMapper(sql) |
| 59 | prevEnd := &storepb.Position{Line: 1, Column: 1} |
| 60 | prevByte := 0 |
| 61 | for _, os := range omniStmts { |
| 62 | startByte := prevByte |
| 63 | endByte := os.ByteEnd |
| 64 | if endByte < startByte { |
| 65 | endByte = startByte |
| 66 | } |
| 67 | text := sql[startByte:endByte] |
| 68 | endPos := positionMapper.Position(endByte) |
| 69 | |
| 70 | _, isGo := os.AST.(*ast.GoStmt) |
| 71 | empty := isGo || os.Empty() || strings.TrimSpace(text) == "" |
| 72 | |
| 73 | result = append(result, base.Statement{ |
| 74 | Text: text, |
| 75 | Range: &storepb.Range{ |
| 76 | Start: int32(startByte), |
| 77 | End: int32(endByte), |
| 78 | }, |
| 79 | Start: prevEnd, |
| 80 | End: endPos, |
| 81 | Empty: empty, |
| 82 | }) |
| 83 | prevEnd = endPos |
| 84 | prevByte = endByte |
| 85 | } |
| 86 | return result |
| 87 | } |
no test coverage detected