table

package
v0.1.2-beta Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Table

type Table struct {
	schema.Schema
	relation.RelationSet
}
Example
package main

import (
	"fmt"

	"github.com/martindrlik/rex/schema"
	"github.com/martindrlik/rex/table"
	"github.com/martindrlik/rex/tuple"
)

func main() {
	foo := map[string]any{"foo": 1}
	bar := map[string]any{"bar": 2.0}
	foobar := tuple.Merge(foo, bar)

	q, _ := table.New(schema.FromTuple(foobar))
	q.Add(foo)
	q.Add(bar)
	q.Add(foobar)

	for t := range q.List() {
		fmt.Println(t)
	}

}
Output:

map[foo:1]
map[bar:2]
map[bar:2 foo:1]

func New

func New(s schema.Schema) (*Table, error)

New returns a new table with the given schema.

func (*Table) Add

func (t *Table) Add(u map[string]any) error

Add adds a tuple to the table. If the tuple is already in the table, it does nothing.

Example
package main

import (
	"fmt"

	"github.com/martindrlik/rex/schema"
	"github.com/martindrlik/rex/table"
	"github.com/martindrlik/rex/tuple"
)

func main() {
	foo := map[string]any{"foo": 1}
	bar := map[string]any{"bar": 2.0}
	q, _ := table.New(schema.FromTuple(foo))
	fmt.Println(q.Add(bar) == schema.ErrMismatch)

	r, _ := table.New(schema.FromTuple(tuple.Merge(foo, bar)))
	baz := map[string]any{"baz": "3"}
	fmt.Println(r.Add(baz) == schema.ErrMismatch)
	fmt.Println(r.Add(bar) == nil)

}
Output:

true
true
true

func (*Table) Delete

func (t *Table) Delete(u map[string]any)

Delete deletes tuples from the table.

func (*Table) Difference

func (t *Table) Difference(u *Table) (*Table, error)

func (*Table) Has

func (t *Table) Has(u map[string]any) bool

func (*Table) List

func (t *Table) List() iter.Seq[map[string]any]

List returns a sequence of all tuples in the table.

func (*Table) NaturalJoin

func (t *Table) NaturalJoin(u *Table) *Table
Example
package main

import (
	"fmt"

	"github.com/martindrlik/rex/schema"
	"github.com/martindrlik/rex/table"
)

func main() {
	foobar := map[string]any{"foo": 1, "bar": 2.0}
	barbaz := map[string]any{"bar": 2.0, "baz": "3"}

	q, _ := table.New(schema.FromTuple(foobar))
	q.Add(foobar)
	r, _ := table.New(schema.FromTuple(barbaz))
	r.Add(barbaz)

	s := q.NaturalJoin(r)

	for t := range s.List() {
		fmt.Println(t)
	}

}
Output:

map[bar:2 baz:3 foo:1]

func (*Table) Projection

func (t *Table) Projection(p ...string) (*Table, error)
Example
package main

import (
	"fmt"

	"github.com/martindrlik/rex/schema"
	"github.com/martindrlik/rex/table"
	"github.com/martindrlik/rex/tuple"
)

func main() {
	foo := map[string]any{"foo": 1}
	foo2 := map[string]any{"foo": 2}
	bar := map[string]any{"bar": 3.0}
	foobar := tuple.Merge(foo, bar)
	foo2bar := tuple.Merge(foo2, bar)

	q, _ := table.New(schema.FromTuple(foobar))
	_ = q.Add(foo)
	_ = q.Add(foo2)
	_ = q.Add(bar)
	_ = q.Add(foobar)
	_ = q.Add(foo2bar)

	s, _ := q.Projection("foo")

	for t := range s.List() {
		fmt.Println(t)
	}

}
Output:

map[foo:1]
map[foo:2]

func (*Table) Rename1

func (t *Table) Rename1(old, new string) (*Table, error)

Rename1 returns a new table with the attribute old renamed to new.

func (*Table) Union

func (t *Table) Union(u *Table) (*Table, error)

Union returns a new table that is the union of t and u.

Jump to

Keyboard shortcuts

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