sqlavro

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2022 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrExpectRecordSchema -
	ErrExpectRecordSchema = errors.New("ErrExpectRecordSchema")
	// ErrUnsupportedTypeForCriterion -
	ErrUnsupportedTypeForCriterion = errors.New("ErrUnsupportedTypeForCriterion")
	// ErrCriterionUnknownField -
	ErrCriterionUnknownField = errors.New("ErrCriterionUnknownField")
	// ErrCannotIgnoreOrder -
	ErrCannotIgnoreOrder = errors.New("ErrCannotIgnoreOrder")
	// ErrUnsupportedTypeForSQL -
	ErrUnsupportedTypeForSQL = errors.New("ErrUnsupportedTypeForSQL")
	// ErrQueryConfigMissingDB -
	ErrQueryConfigMissingDB = errors.New("ErrQueryConfigMissingDB")
	// ErrQueryConfigMissingDBName -
	ErrQueryConfigMissingDBName = errors.New("ErrQueryConfigMissingDBName")
	// ErrQueryConfigMissingSchema -
	ErrQueryConfigMissingSchema = errors.New("ErrQueryConfigMissingSchema")
	// ErrUnsupportedOutput - query doesn't supprot this output
	ErrUnsupportedOutput = errors.New("ErrUnsupportedOutput")
)

Functions

func EnsureCriterionTypes added in v1.1.5

func EnsureCriterionTypes(schema *avro.RecordSchema, criteria []Criterion) (err error)

EnsureCriterionTypes - search the given schema to find & set criteria types

func GetTables

func GetTables(db *sql.DB, dbName string) ([]string, error)

GetTables - returns table names of the given database

func SQLDatabase2AVRO

func SQLDatabase2AVRO(db *sql.DB, dbName string) ([]avro.RecordSchema, error)

SQLDatabase2AVRO - fetch all tables of the given SQL database and translate them to avro schemas

Example
package main

import (
	"database/sql"
	"encoding/json"
	"fmt"

	"github.com/khezen/avro/sqlavro"
)

func main() {
	db, err := sql.Open("mysql", "root@/blog")
	if err != nil {
		panic(err)
	}
	defer db.Close()
	_, err = db.Exec(
		`CREATE TABLE posts(
			ID INT NOT NULL,
			title VARCHAR(128) NOT NULL,
			body LONGBLOB NOT NULL,
			content_type VARCHAR(128) DEFAULT 'text/markdown; charset=UTF-8',
			post_date DATETIME NOT NULL,
			update_date DATETIME,
			reading_time_minutes DECIMAL(3,1),
			PRIMARY KEY(ID)
		)`,
	)
	if err != nil {
		panic(err)
	}
	schemas, err := sqlavro.SQLDatabase2AVRO(db, "blog")
	if err != nil {
		panic(err)
	}
	schemasBytes, err := json.Marshal(schemas)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(schemasBytes))
}
Output:

func SQLEscape

func SQLEscape(input string) string

SQLEscape -

func SQLTable2AVRO

func SQLTable2AVRO(db *sql.DB, dbName, tableName string) (*avro.RecordSchema, error)

SQLTable2AVRO - translate the given SQL table to AVRO schema

func UnderlyingType

func UnderlyingType(union avro.UnionSchema) (avro.Schema, error)

UnderlyingType -

Types

type Criterion

type Criterion struct {
	FieldName string `json:"field"`

	RawLimit *json.RawMessage `json:"limit,omitempty"`
	Order    avro.Order       `json:"order,omitempty"` // default: Ascending
	// contains filtered or unexported fields
}

Criterion -

func NewCriterionDate

func NewCriterionDate(fieldName string, limit *time.Time, order avro.Order) *Criterion

NewCriterionDate -

func NewCriterionDateTime

func NewCriterionDateTime(fieldName string, limit *time.Time, order avro.Order) *Criterion

NewCriterionDateTime -

func NewCriterionFloat64

func NewCriterionFloat64(fieldName string, limit *float64, order avro.Order) *Criterion

NewCriterionFloat64 -

func NewCriterionFromNative

func NewCriterionFromNative(field *avro.RecordFieldSchema, value interface{}, order avro.Order) (*Criterion, error)

NewCriterionFromNative -

func NewCriterionFromString

func NewCriterionFromString(field *avro.RecordFieldSchema, value string, order avro.Order) (*Criterion, error)

NewCriterionFromString -

func NewCriterionInt64

func NewCriterionInt64(fieldName string, limit *int64, order avro.Order) *Criterion

NewCriterionInt64 -

func NewCriterionString

func NewCriterionString(fieldName string, limit *string, order avro.Order) *Criterion

NewCriterionString -

func NewCriterionTime

func NewCriterionTime(fieldName string, limit *time.Time, order avro.Order) *Criterion

NewCriterionTime -

func Query

func Query(cfg QueryConfig) (resultBytes []byte, newCriteria []Criterion, err error)

Query -

Example
package main

import (
	"database/sql"
	"fmt"
	"io/ioutil"
	"time"

	"github.com/khezen/avro"
	"github.com/khezen/avro/sqlavro"
)

func main() {
	db, err := sql.Open("mysql", "root@/blog")
	if err != nil {
		panic(err)
	}
	defer db.Close()
	_, err = db.Exec(
		`CREATE TABLE posts(
			ID INT NOT NULL,
			title VARCHAR(128) NOT NULL,
			body LONGBLOB NOT NULL,
			content_type VARCHAR(128) DEFAULT 'text/markdown; charset=UTF-8',
			post_date DATETIME NOT NULL,
			update_date DATETIME,
			reading_time_minutes DECIMAL(3,1),
			PRIMARY KEY(ID)
		)`,
	)
	if err != nil {
		panic(err)
	}
	_, err = db.Exec(
		// statement
		`INSERT INTO posts(ID,title,body,content_type,post_date,update_date,reading_time_minutes)
		 VALUES (?,?,?,?,?,?,?)`,
		// values
		42,
		"lorem ispum",
		[]byte(`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`),
		"text/markdown; charset=UTF-8",
		"2009-04-10 00:00:00",
		"2009-04-10 00:00:00",
		"4.2",
	)
	if err != nil {
		panic(err)
	}
	schema, err := sqlavro.SQLTable2AVRO(db, "blog", "posts")
	if err != nil {
		panic(err)
	}
	limit := 1000
	order := avro.Ascending
	from, err := time.Parse("2006-02-01 15:04:05", "2009-04-10 00:00:00")
	if err != nil {
		panic(err)
	}
	avroBytes, updatedCriteria, err := sqlavro.Query(sqlavro.QueryConfig{
		DB:     db,
		DBName: "blog",
		Schema: schema,
		Limit:  limit,
		Criteria: []sqlavro.Criterion{
			*sqlavro.NewCriterionDateTime("post_date", &from, order),
		},
		Output: "avro",
	})
	if err != nil {
		panic(err)
	}
	err = ioutil.WriteFile("/tmp/blog_posts.avro", avroBytes, 0644)
	if err != nil {
		panic(err)
	}
	fmt.Println(updatedCriteria)
}
Output:

func (*Criterion) Limit

func (c *Criterion) Limit() (interface{}, error)

Limit -

func (*Criterion) OrderOperand

func (c *Criterion) OrderOperand() (string, error)

OrderOperand -

func (*Criterion) OrderSort

func (c *Criterion) OrderSort() (string, error)

OrderSort -

func (*Criterion) SetLimit added in v1.1.3

func (c *Criterion) SetLimit(limit interface{}) error

SetLimit - from native go

type QueryConfig

type QueryConfig struct {
	// DB - Required SQL connection pool used to access the database.
	DB *sql.DB
	// DBName - Required name of the database to select
	DBName string
	// Schema - Required avro Record Schema matching the table to query data from.
	Schema *avro.RecordSchema
	// Limit - Optional limit in the number of record to be retrieved.
	// 0(no limit) is used as default if not set
	Limit int
	// Criteria - Optional list of criterion to retreve data from.
	Criteria []Criterion
	// Compression -  Optional name of the compression codec used to compress blocks
	// "null", "deflate" and snappy are accepted.
	// If the value is empty, it is assumed to be "null"
	Compression string
	// Output - define the desired format for the output
	// AVRO and CSV are supported
	// if not set, then AVRO is the default choice
	Output string
	// Separator - if you use CSV output format then
	// you might want to set the separator.
	// Default value is ';'
	Separator rune
}

QueryConfig -

func (*QueryConfig) Verify

func (qc *QueryConfig) Verify() error

Verify and ensure the config is valid

type SQLType

type SQLType string

SQLType -

const (

	// Char -
	Char SQLType = "char"
	// NChar -
	NChar SQLType = "nchar"
	// VarChar -
	VarChar SQLType = "varchar"
	// NVarChar -
	NVarChar SQLType = "nvarchar"
	// Text -
	Text SQLType = "text"
	// TinyText -
	TinyText SQLType = "tinytext"
	// MediumText -
	MediumText SQLType = "mdeiumtext"
	// LongText -
	LongText SQLType = "longtext"
	// Blob -
	Blob SQLType = "blob"
	// MediumBlob -
	MediumBlob SQLType = "mediumblob"
	// LongBlob -
	LongBlob SQLType = "longblob"
	// Enum -
	Enum SQLType = "enum"
	// Set -
	Set SQLType = "set"

	// TinyInt -
	TinyInt SQLType = "tinyint"
	// SmallInt -
	SmallInt SQLType = "smallint"
	// MediumInt -
	MediumInt SQLType = "mediumint"
	// Int -
	Int SQLType = "int"
	// BigInt -
	BigInt SQLType = "bigint"
	// Float -
	Float SQLType = "float"
	// Double -
	Double SQLType = "double"
	// Decimal -
	Decimal SQLType = "decimal"

	// Date -
	Date SQLType = "date"
	// DateTime -
	DateTime SQLType = "datetime"
	// Timestamp -
	Timestamp SQLType = "timestamp"
	// Time -
	Time SQLType = "time"
	// Year -
	Year SQLType = "year"

	// Bit -
	Bit SQLType = "bit"

	// SQLDateTimeFormat -
	SQLDateTimeFormat = "2006-01-02 15:04:05"

	// SQLDateFormat -
	SQLDateFormat = "2006-01-02"

	// SQLTimeFormat -
	SQLTimeFormat = "15:04:05"
)

Jump to

Keyboard shortcuts

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