diff --git a/pkg/glazed/handlers/datatables/datatables.go b/pkg/glazed/handlers/datatables/datatables.go index 9d446c3..fc7388d 100644 --- a/pkg/glazed/handlers/datatables/datatables.go +++ b/pkg/glazed/handlers/datatables/datatables.go @@ -10,6 +10,7 @@ import ( "github.com/go-go-golems/glazed/pkg/formatters/json" table_formatter "github.com/go-go-golems/glazed/pkg/formatters/table" "github.com/go-go-golems/glazed/pkg/middlewares/row" + "github.com/go-go-golems/glazed/pkg/middlewares/table" "github.com/go-go-golems/glazed/pkg/types" "github.com/go-go-golems/parka/pkg/glazed" "github.com/go-go-golems/parka/pkg/glazed/handlers" @@ -61,6 +62,11 @@ type DataTables struct { // AdditionalData to be passed to the rendering engine AdditionalData map[string]interface{} + // StreamRows is used to control whether the rows should be streamed or not. + // If set to false, a TableMiddleware used (which collects all rows, but thus also all column names) + // into memory before passing them to the template. + // This is useful when the rows are "ragged" (i.e. not all rows have the same number of columns). + StreamRows bool } //go:embed templates/* @@ -162,6 +168,12 @@ func WithAdditionalData(data map[string]interface{}) QueryHandlerOption { } } +func WithStreamRows(streamRows bool) QueryHandlerOption { + return func(h *QueryHandler) { + h.dt.StreamRows = streamRows + } +} + func (qh *QueryHandler) Handle(c *gin.Context, w io.Writer) error { pc := glazed.NewCommandContext(qh.cmd) @@ -208,9 +220,14 @@ func (qh *QueryHandler) Handle(c *gin.Context, w io.Writer) error { return err } - gp.ReplaceTableMiddleware() - gp.AddRowMiddleware(row.NewColumnsChannelMiddleware(columnsC, true)) - gp.AddRowMiddleware(row.NewOutputChannelMiddleware(of, rowC)) + if dt_.StreamRows { + gp.ReplaceTableMiddleware() + gp.AddRowMiddleware(row.NewColumnsChannelMiddleware(columnsC, true)) + gp.AddRowMiddleware(row.NewOutputChannelMiddleware(of, rowC)) + } else { + gp.AddTableMiddleware(table.NewColumnsChannelMiddleware(columnsC)) + gp.AddTableMiddleware(table.NewOutputChannelMiddleware(of, rowC)) + } ctx := c.Request.Context() ctx2, cancel := context.WithCancel(ctx) diff --git a/pkg/handlers/command-dir/command-dir.go b/pkg/handlers/command-dir/command-dir.go index d65ecbe..0359b59 100644 --- a/pkg/handlers/command-dir/command-dir.go +++ b/pkg/handlers/command-dir/command-dir.go @@ -465,6 +465,7 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error { datatables.WithTemplateLookup(cd.TemplateLookup), datatables.WithTemplateName(cd.TemplateName), datatables.WithAdditionalData(cd.AdditionalData), + datatables.WithStreamRows(cd.Stream), } datatables.CreateDataTablesHandler(sqlCommand, path, commandPath, options...)(c)