esqlo

package
v0.0.0-...-6f1d202 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ImplicitDb = "__mem__" // reserved scheme name for in-memory database. All tables are stored in this database for each page render.
)

Variables

This section is empty.

Functions

func LoadConfigs

func LoadConfigs(r io.Reader) (map[string]Database, error)

func RenderAll

func RenderAll(handler http.Handler) http.Handler

Types

type Database

type Database interface {
	OpenConnection(path *url.URL) error
	Query(query string) (*Result, error)
	Close() error
}

type DuckDB

type DuckDB struct {
	// contains filtered or unexported fields
}

func (*DuckDB) Close

func (d *DuckDB) Close() error

func (*DuckDB) OpenConnection

func (d *DuckDB) OpenConnection(path *url.URL) (err error)

func (*DuckDB) Query

func (d *DuckDB) Query(query string) (*Result, error)

type Err

type Err struct {
	Line, Col int
	Msg       error
}

func (*Err) Error

func (e *Err) Error() string

type Handler

type Handler struct {
	Databases map[string]Database
	// contains filtered or unexported fields
}

Handler is will wrap fileserver and render any esqlo templates returned. If a file is not html, it is served unmodified like a normal filesystem http server. If a file is html, it is parsed as a esqlo template and rendered with the sql statements automatically resolved using the configured database connections.

func (*Handler) ServeHTTP

func (d *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type LineCounter

type LineCounter struct {
	// contains filtered or unexported fields
}

LineCounter wraps an io.Reader and records line information to allow converting offsets into line numbers. Line feed (ASCII 10, '\n') is used as the line ending (files using CRLF line termination will of course count correctly as well).

func NewLineCounter

func NewLineCounter(r io.Reader) *LineCounter

NewLineCounter returns a new LineCounter reading from r.

func (*LineCounter) LineCol

func (l *LineCounter) LineCol(offset int) (line, col int)

LineCol returns, give a byte offset of the input, the line number and that line's byte offset in the input. Line numbers are 1-based, i.e. passing 0 returns line number 1. Offsets beyond the last recorded line will return the last recorded line.

func (*LineCounter) Read

func (l *LineCounter) Read(p []byte) (n int, err error)

Read calls through the underlying Reader and examines the returned data for line information before returning it.

type MemDB

type MemDB struct {
	Tables map[string]*MemTable
}

func (*MemDB) Close

func (db *MemDB) Close() error

func (*MemDB) OpenConnection

func (db *MemDB) OpenConnection(path *url.URL) error

func (*MemDB) Query

func (db *MemDB) Query(query string) (*Result, error)

type MemTable

type MemTable struct {
	Columns []string
	Rows    [][]any
}

type Renderer

type Renderer struct {
	Databases map[string]Database
	Errors    []*Err // any errors that occurred while processing the document (can be ignored gracefully)
	// contains filtered or unexported fields
}

func NewRenderer

func NewRenderer() *Renderer

func (*Renderer) LoadDatabase

func (r *Renderer) LoadDatabase(path *url.URL) (Database, error)

func (*Renderer) RenderHTML

func (r *Renderer) RenderHTML(src io.Reader, w io.Writer) error

/ RenderHTML renders an HTML document with embedded SQL statements. The SQL statements are resolved using the provided / database connections. The rendered HTML is written to the provided writer.

The SQL statements are embedded in the HTML using the following syntax:

	<!-- Define a table for this HTML document that can be used in later in the document to inject live data. -->
	<!-- The src must be a defined database with name persons, and the query must be a valid SQL query for that database type. -->
	<sql src="sqlite://persons.sqlite" name="famous_persons">SELECT name, age FROM persons WHERE is_famous={{query.is_famous}} LIMIT {{query.page_size}}"</sql>

	<p>Here is a list of famous people:</p>
 	<p>{{persons.name[0]}} is {{persons.age[0]}} years old</p>
 	<p>{{persons.name[1]}} is {{persons.age[1]}} years old</p>
 	<p>{{persons.name[2]}} is {{persons.age[2]}} years old</p>

RenderHTML does not modify the tree, besides removing the <sql> tags at the start of the html document.

Replacement values are injected into the HTML document afterwards using the Mustache template syntax.

type Result

type Result struct {
	Columns []string
	Rows    []any // each row with a slice of values for each row. len(rows) == number of results, len(rows[0]) == len(ColumnNames)
}

type SqlTag

type SqlTag struct {
	Offset, End int      // the offset in the source file where this tag starts
	Src         string   // the database connection to use
	Database    Database // the database connection to use
	TableName   string   // the name to store this table as

	Query  string  // the sql query to execute
	Result *Result // the result of the query
}

type Sqlite

type Sqlite struct {
	Path string `yaml:"path"`
}

func (*Sqlite) OpenConnection

func (s *Sqlite) OpenConnection(path *url.URL) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL