test

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Example
package main

import (
	"context"
	"io"

	sqle "github.com/Rock-liyi/p2pdb-server"
	"github.com/Rock-liyi/p2pdb-store/memory"
	"github.com/Rock-liyi/p2pdb-store/sql"
	"github.com/Rock-liyi/p2pdb-store/sqlite"
	debug "github.com/favframework/debug"
)

func Select2() {
	debug.Dump("Example start====")

	// Create a test memory database and register it to the default engine.
	db := createTestMemory()
	e := sqle.NewDefault(sql.NewDatabaseProvider(db))

	ctx := sql.NewContext(context.Background()).WithCurrentDB("test")
	query := `select * from test.userinfo`
	//ctx.SetRawStatement(query)
	_, r, err := e.Query(ctx, query)

	debug.Dump("Example RawStatement")
	debug.Dump(ctx.RawStatement())
	// debug.Dump(r)

	checkIfError(err)

	// Iterate results and print them.
	for {
		row, err := r.Next(ctx)
		//debug.Dump(row)
		if err == io.EOF {
			break
		}
		checkIfError(err)
		debug.Dump(row)
	}
	debug.Dump("Example end====")
}

func main() {
	debug.Dump("Example start====")

	// Create a test memory database and register it to the default engine.
	db := createTestDatabase()
	e := sqle.NewDefault(sql.NewDatabaseProvider(db))

	ctx := sql.NewContext(context.Background()).WithCurrentDB("test")
	// query := `SELECT name, count(*) FROM mytable
	// WHERE name = 'John Doe'
	// GROUP BY name`

	query := `SELECT count(*) as a,count(*) as n,name,email FROM mytable`
	//ctx.SetRawStatement(query)
	_, r, err := e.Query(ctx, query)

	// debug.Dump("Example RawStatement")
	// debug.Dump(ctx.RawStatement())
	// debug.Dump(r)

	checkIfError(err)

	// Iterate results and print them.

	debug.Dump(ctx.Query())
	debug.Dump("Example  Next start====")
	//for {
	row, err := r.Next(ctx)
	//debug.Dump(row)
	if err == io.EOF {
		debug.Dump(err)
		//break
	}
	checkIfError(err)

	debug.Dump(row)
	debug.Dump("Example  Next end====")
	// name := row[0]
	// count := row[1]
	// fmt.Println(name, count)
	//}

	debug.Dump("Example end====")

}

func checkIfError(err error) {
	if err != nil {
		panic(err)
	}
}

func createTestDatabase() sql.Database {
	db := sqlite.NewDatabase("test")
	table := sqlite.NewTable("mytable", sql.NewPrimaryKeySchema(sql.Schema{
		{Name: "name", Type: sql.Text, Source: "mytable"},
		{Name: "email", Type: sql.Text, Source: "mytable"},
	}))
	db.AddTable("mytable", table)
	ctx := sql.NewEmptyContext()

	rows := []sql.Row{
		sql.NewRow("John Doe", "john@doe.com"),
		sql.NewRow("John Doe", "johnalt@doe.com"),
		sql.NewRow("Jane Doe", "jane@doe.com"),
		sql.NewRow("Evil Bob", "evilbob@gmail.com"),
	}

	for _, row := range rows {
		table.Insert(ctx, row)
	}

	return db
}

func createTestMemory() sql.Database {
	db := memory.NewDatabase("test")
	table := memory.NewTable("mytable", sql.NewPrimaryKeySchema(sql.Schema{
		{Name: "name", Type: sql.Text, Source: "mytable"},
		{Name: "email", Type: sql.Text, Source: "mytable"},
	}))
	db.AddTable("mytable", table)
	ctx := sql.NewEmptyContext()

	rows := []sql.Row{
		sql.NewRow("John Doe", "john@doe.com"),
		sql.NewRow("John Doe", "johnalt@doe.com"),
		sql.NewRow("Jane Doe", "jane@doe.com"),
		//	sql.NewRow("Evil Bob", "evilbob@gmail.com"),
	}

	for _, row := range rows {
		table.Insert(ctx, row)
	}

	return db
}
Output:

John Doe 2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCatalog

func NewCatalog(provider sql.DatabaseProvider) sql.Catalog

NewCatalog returns a new empty Catalog with the given provider

func TestDatabase_DropTable

func TestDatabase_DropTable(t *testing.T)

func TestDatabase_Name

func TestDatabase_Name(t *testing.T)

Types

type Catalog

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

func (*Catalog) AllDatabases

func (c *Catalog) AllDatabases() []sql.Database

AllDatabases returns all sliceDBProvider in the catalog.

func (*Catalog) CreateDatabase

func (c *Catalog) CreateDatabase(ctx *sql.Context, dbName string) error

CreateDatabase creates a new Database and adds it to the catalog.

func (*Catalog) Database

func (c *Catalog) Database(db string) (sql.Database, error)

Database returns the database with the given name.

func (*Catalog) Function

func (c *Catalog) Function(name string) (sql.Function, error)

func (*Catalog) HasDB

func (c *Catalog) HasDB(db string) bool

func (*Catalog) LockTable

func (c *Catalog) LockTable(ctx *sql.Context, table string)

func (*Catalog) RegisterFunction

func (c *Catalog) RegisterFunction(fns ...sql.Function)

func (*Catalog) RemoveDatabase

func (c *Catalog) RemoveDatabase(ctx *sql.Context, dbName string) error

RemoveDatabase removes a database from the catalog.

func (*Catalog) Table

func (c *Catalog) Table(ctx *sql.Context, dbName, tableName string) (sql.Table, sql.Database, error)

Table returns the table in the given database with the given name.

func (*Catalog) TableAsOf

func (c *Catalog) TableAsOf(ctx *sql.Context, dbName, tableName string, asOf interface{}) (sql.Table, sql.Database, error)

func (*Catalog) UnlockTables

func (c *Catalog) UnlockTables(ctx *sql.Context, id uint32) error

type MemTracer

type MemTracer struct {
	Spans []string
	sync.Mutex
}

MemTracer implements a simple tracer in memory for testing.

func (*MemTracer) Extract

func (t *MemTracer) Extract(format interface{}, carrier interface{}) (opentracing.SpanContext, error)

Extract implements opentracing.Tracer interface.

func (*MemTracer) Inject

func (t *MemTracer) Inject(sm opentracing.SpanContext, format interface{}, carrier interface{}) error

Inject implements opentracing.Tracer interface.

func (*MemTracer) StartSpan

func (t *MemTracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span

StartSpan implements opentracing.Tracer interface.

Jump to

Keyboard shortcuts

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