- commit
- 82563374e93545c3cd9762f541ebdddd2c582eb4
- parent
- c342b1ac0a63fdd60f8912b1962970256f48dce2
- parent
- 35de47e13dfaab8a66c9939d6cb6a37d2fa60f3e
- author
- Dami <dami.etoile@gmail.com>
| A | internal/ | 104 | ++++++++++++++++++++++++++++++ |
| M | internal/ | 4 | + |
@@ -0,0 +1,104 @@
1+package fileio2+3+import (4+ "context"5+ "errors"6+7+ "github.com/FelineStateMachine/012/internal/sheet"8+)9+10+// Sources: a Parquet file, or a SQLite table or query, read in place a11+// window of rows at a time rather than imported, for tables too big for12+// any grid. A Source streams its rows in its own order for what reads13+// them all (aggregates, pivot tables) and fetches the rows a screen14+// shows; a SourceView is its rows as a sort and a filter order them,15+// pushed down to SQL for SQLite and streamed for Parquet. Values come16+// out typed as an import types them.17+18+// SourceSpec names what a source reads.19+type SourceSpec struct {20+ // Path is the file.21+ Path string22+ // Format is "Parquet" or "SQLite", or "" to tell by the extension.23+ Format string24+ // Table is a SQLite table or view to read, or Query a query to run;25+ // both empty reads a database's only table.26+ Table, Query string27+ // TempDir is where views keep what they build (sorted row orders,28+ // SQLite's temporary tables); "" is the system's.29+ TempDir string30+}31+32+// SourceColumn is one of a source's columns.33+type SourceColumn struct {34+ Name string35+ // Format is what its values show in when the column's type says (a36+ // Parquet DATE column's FmtDate); a value's own may differ.37+ Format sheet.Format38+ // Numeric is set when its type holds numbers, dates and times39+ // included.40+ Numeric bool41+}42+43+// SourceSort is a column a view is sorted by.44+type SourceSort struct {45+ Col int46+ Desc bool47+}48+49+// SourceFilter is a condition a view's rows meet on a column.50+type SourceFilter struct {51+ Col int52+ Cond sheet.Condition53+}54+55+// SourceOrder is how a view sorts and filters a source's rows.56+type SourceOrder struct {57+ Sort []SourceSort58+ Filter []SourceFilter59+}60+61+// IsZero reports whether the order is the source's own, every row.62+func (o SourceOrder) IsZero() bool { return len(o.Sort) == 0 && len(o.Filter) == 0 }63+64+// Source is a table read in place. It is safe for use by several65+// goroutines at once.66+type Source interface {67+ // Columns are the source's columns, left to right.68+ Columns() []SourceColumn69+ // Rows counts its rows, the header not among them.70+ Rows() int6471+ // Scan streams the rows from row from on (counting from 0) in the72+ // source's order, calling fn with each row's number and the values73+ // of cols (every column when cols is nil, in that order; the slice74+ // is reused) until fn returns false, the rows end or ctx is done.75+ Scan(ctx context.Context, from int64, cols []int, fn func(row int64, vals []sheet.LiveCell) bool) error76+ // Fetch reads the rows numbered rows, in the order given, with the77+ // values of cols (every column when nil).78+ Fetch(ctx context.Context, rows []int64, cols []int) ([][]sheet.LiveCell, error)79+ // View orders the rows as o says. Building it may read the whole80+ // source once, which the caller does in the background.81+ View(ctx context.Context, o SourceOrder) (SourceView, error)82+ // Close lets the file go, and what its views built.83+ Close() error84+}85+86+// SourceView is a source's rows as a sort and a filter order them.87+type SourceView interface {88+ // Rows counts the rows the view shows.89+ Rows() int6490+ // Page reads n rows from position from of the view: each one's91+ // number in the source and its values.92+ Page(ctx context.Context, from int64, n int) ([]int64, [][]sheet.LiveCell, error)93+ // Close lets go of what the view built.94+ Close() error95+}96+97+// ErrNotSource is what OpenSource says of a file that is neither98+// Parquet nor SQLite.99+var ErrNotSource = errors.New("only Parquet files and SQLite tables or queries can be linked as sources")100+101+// OpenSource opens what spec names.102+func OpenSource(ctx context.Context, spec SourceSpec) (Source, error) {103+ return nil, ErrNotSource104+}@@ -316,6 +316,10 @@ func (s *Sheet) ShownText(a Addr) string {
316316 // condition, as the filter tests it.317317 func (c Condition) Matches(v Value, shown string) bool { return c.test()(v, shown) }318318 319+// Test compiles the condition, for what filters rows outside a sheet (a320+// linked source's view): see test.321+func (c Condition) Test() func(v Value, shown string) bool { return c.test() }322+319323 // test compiles the condition. Text conditions ignore case; comparisons320324 // need a number for a number (entered as $1,200 or 12% too) and text for321325 // text, and never match blanks.