goql

package module
v0.0.0-...-31c2986 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 12 Imported by: 0

README

GoQL

A query language, over Go code, in Go!

Build Status Coverage Status GoDoc Go Report Card

This package is under heavy development, anything may change!

This is a golang sql driver, to interact with Go code. currently only select is possible, but the insert/update/delete is in todo list.

Usage

like any other sql driver in golang, just import the goql package in your code :

package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/3mard/goql"
	"github.com/3mard/goql/astdata"
)

func main() {
	// open the net/http package
	con, err := sql.Open("goql", "net/http")
	if err != nil {
		log.Fatal(err)
	}
	defer con.Close()

	rows, err := con.Query("SELECT name, receiver, def FROM funcs")
	if err != nil {
		log.Fatal(err)
	}

	for rows.Next() {
		var (
			name string
			rec  sql.NullString
			def  astdata.Definition
		)
		rows.Scan(&name, &rec, &def)
		if rec.Valid {
			name = rec.String + "." + name
		}
		fmt.Printf("\nfunc %s , definition : %s", name, def.String())
	}

}

Also there is an example command line is available for more advanced usage in cmd/goql by running go get -u github.com/3mard/goql/... the binary is available in your GOBIN directory. you can run query against any installed package in your GOPATH via this tool.

List of supported tables and fields are available in docs/table

there is one special type called definition. this type is printed as string, but one can use functions to handle special queries. list of supported functions are available at docs/functions

also its possible to add new tables/fields/functions using plugins. an example plugin is available at plugin/goqlimport

currently only supported query is select , with where,order and limit some example query :

select * from files where the docs is not null
select * from funcs where def = 'func()' and exported
select * from consts order by name desc limit 10, 10
select * from vars where is_struct(def) and name like 's%'
select * from types where is_map(def) and map_key(def) = 'string'
select * from imports where canonical = 'ctx'

Demo

asciicast

TODO

  • Write (more) documentation
  • UPDATE/INSERT/DELETE support (Yes, code generation with sql :) )

Documentation

Overview

Package goql is a go sql driver for query against go packages the tables and fields are registered dynamically at runtime, and registration can be done using Register* functions currently 3 types are accepted, Number/String/Bool type `Definition` is under development see the cmd/goql for an example.

Example
package main

import (
	"database/sql"
	"fmt"
	"log"
)

func main() {
	con, err := sql.Open("goql", "net/http")
	if err != nil {
		log.Fatal(err)
	}
	defer con.Close()

	rows, err := con.Query("SELECT name, receiver FROM funcs WHERE name='Do'") // client.Do
	if err != nil {
		log.Fatal(err)
	}
	var name, rec string
	for rows.Next() {
		if err := rows.Scan(&name, &rec); err != nil {
			log.Fatal(err)
		}

		fmt.Printf("%s.%s", rec, name)
	}
}
Output:

Client.Do

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterField

func RegisterField(t string, name string, valuer interface{})

RegisterField is the field registration for a table, table must registered before and the name must be unique in that table the value is one of the String/Bool/NumberValuer in any other case, it panics

func RegisterFunction

func RegisterFunction(name string, fn Function)

RegisterFunction is entry point for registering a function into system, the name must be unique

func RegisterTable

func RegisterTable(name string, data Table)

RegisterTable is the function to handle registration of a table, the name must be unique, and duplicate registration panics

Types

type Bool

type Bool struct {
	Bool bool
	Null bool
}

Bool is the boolean type

func (Bool) Get

func (b Bool) Get() interface{}

Get return the actual value (and nil)

type BoolValuer

type BoolValuer interface {
	Value(interface{}) Bool
}

BoolValuer is the Boolean valuer

type Definition

type Definition struct {
	Definition astdata.Definition
}

Definition is the type to handle type definition

func (Definition) Get

func (d Definition) Get() interface{}

Get return the actual definition

type DefinitionValuer

type DefinitionValuer interface {
	Value(interface{}) Definition
}

DefinitionValuer is used to handle definition column

type Function

type Function interface {
	// Execute is called on each row
	Execute(...Getter) (Getter, error)
}

Function is the functions in the system, it should check for arguments count and return error on wrong arguments count, but for type, it should try to cast TODO : check for function arguments on prepare

type Getter

type Getter interface {
	Get() interface{}
}

Getter is replacement to prevent using the interface{} it is used when the value is required

type Number

type Number struct {
	Number float64
	Null   bool
}

Number is the number, only float64 is supported

func (Number) Get

func (n Number) Get() interface{}

Get return the actual value (and nil)

type NumberValuer

type NumberValuer interface {
	Value(interface{}) Number
}

NumberValuer is the number valuer (float64 is supported only )

type String

type String struct {
	String string
	Null   bool
}

String is the string type, like function name, file name and ...

func (String) Get

func (s String) Get() interface{}

Get return the actual value (and nil)

type StringValuer

type StringValuer interface {
	Value(interface{}) String
}

StringValuer is provider for a value for a table

type Table

type Table interface {
	// the function argument is the object used as database. in our case it is the astdata.Package and the result
	// must be an array of items. items are depends on the table. for example on funcs table, the result
	// is a slice of astdata.Functions
	Provide(interface{}) []interface{}
}

Table is a interface for registration of a data

type ValueType

type ValueType int

ValueType is an enum contain supported value type in system

const (
	// ValueTypeString is the string type
	ValueTypeString ValueType = iota
	// ValueTypeNumber is the number type
	ValueTypeNumber
	// ValueTypeBool is the bool type
	ValueTypeBool
	// ValueTypeDefinition is a definition special type
	ValueTypeDefinition
)

Directories

Path Synopsis
cmd
Package parse is a simple sql lexer/parser with limited functionality it can handle queries like :
Package parse is a simple sql lexer/parser with limited functionality it can handle queries like :
plugin
goqlimport
Package goqlimport is a prof of concept, just to show how to add a new column to a table this is slow, just for demonstration
Package goqlimport is a prof of concept, just to show how to add a new column to a table this is slow, just for demonstration

Jump to

Keyboard shortcuts

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