MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / formatWindowSpec

Method formatWindowSpec

cmd/gosqlx/cmd/sql_formatter.go:889–941  ·  view source on GitHub ↗

formatWindowSpec formats window specifications for window functions

(spec *ast.WindowSpec)

Source from the content-addressed store, hash-verified

887
888// formatWindowSpec formats window specifications for window functions
889func (f *SQLFormatter) formatWindowSpec(spec *ast.WindowSpec) error {
890 if len(spec.PartitionBy) > 0 {
891 f.writeKeyword("PARTITION BY")
892 f.builder.WriteString(" ")
893 f.formatExpressionList(spec.PartitionBy, ", ")
894 }
895
896 if len(spec.OrderBy) > 0 {
897 if len(spec.PartitionBy) > 0 {
898 f.builder.WriteString(" ")
899 }
900 f.writeKeyword("ORDER BY")
901 f.builder.WriteString(" ")
902 for i, orderBy := range spec.OrderBy {
903 if i > 0 {
904 f.builder.WriteString(", ")
905 }
906 if orderBy.Expression != nil {
907 if err := f.formatExpression(orderBy.Expression); err != nil {
908 fmt.Fprintf(os.Stderr, "Warning: failed to format window ORDER BY expression: %v\n", err)
909 }
910 }
911 if !orderBy.Ascending {
912 f.builder.WriteString(" DESC")
913 }
914 if orderBy.NullsFirst != nil {
915 if *orderBy.NullsFirst {
916 f.builder.WriteString(" NULLS FIRST")
917 } else {
918 f.builder.WriteString(" NULLS LAST")
919 }
920 }
921 }
922 }
923
924 if spec.FrameClause != nil {
925 if len(spec.PartitionBy) > 0 || len(spec.OrderBy) > 0 {
926 f.builder.WriteString(" ")
927 }
928 f.writeKeyword(spec.FrameClause.Type)
929 f.builder.WriteString(" ")
930 f.writeKeyword("BETWEEN")
931 f.builder.WriteString(" ")
932 f.builder.WriteString(spec.FrameClause.Start.Type)
933 if spec.FrameClause.End != nil {
934 f.builder.WriteString(" ")
935 f.writeKeyword("AND")
936 f.builder.WriteString(" " + spec.FrameClause.End.Type)
937 }
938 }
939
940 return nil
941}
942
943// Helper methods for formatting
944func (f *SQLFormatter) formatExpressionList(exprs []ast.Expression, separator string) {

Callers 1

formatExpressionMethod · 0.95

Calls 3

writeKeywordMethod · 0.95
formatExpressionListMethod · 0.95
formatExpressionMethod · 0.95

Tested by

no test coverage detected