sqload

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2023 License: Apache-2.0 Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sqload

sqload reads the SQL file embedded in the Go binary and converts the statements into a string slice.

It also checks that the statements it reads are valid.

Features

  • Parse statment in MySQL
  • Parse statment in PostgreSQL

Install

go get github.com/uh-zz/sqload

Getting started

Pattern 1: Reading from a single file
.
├── go.mod
├── go.sum
├── main.go
└── sql
    └── test.sql

sql/test.sql

INSERT INTO table001 (name,age) VALUES ('alice', 10);

You can also add multiple sqls in the same file.

INSERT INTO table001 (name,age) VALUES ('alice', 10);
INSERT INTO table001 (name,age) VALUES ('bob', 10);

main.go

package main

import (
	"bytes"
	"embed"
	"fmt"

	"github.com/uh-zz/sqload"
	"github.com/uh-zz/sqload/driver/mysql"
)

//go:embed sql/*
var content embed.FS

func main() {
	var (
		buf  bytes.Buffer // sql which read from file
		sqls []string // sql after parse
	)

	loader := sqload.New(mysql.Dialector{}) // for PostgreSQL: postgresql.Dialector{}

	if err := loader.Load(&content, &buf); err != nil {
		fmt.Printf("Load error: %s", err.Error())
	}

	if err := loader.Parse(buf.String(), &sqls); err != nil {
		fmt.Printf("Parse error: %s", err.Error())
	}

	fmt.Printf("%+v", sqls)
    // [INSERT INTO table001 (name,age) VALUES ('alice', 10);]
}
Pattern 2: Reading from a multiple files
.
├── go.mod
├── go.sum
├── main.go
└── sql
    ├── test.sql
    └── test_other.sql

sql/test.sql

INSERT INTO table001 (name,age) VALUES ('alice', 10);

sql/test_other.sql

INSERT INTO table001 (name,age) VALUES ('bob', 10);

main.go

package main

import (
	"bytes"
	"embed"
	"fmt"

	"github.com/uh-zz/sqload"
	"github.com/uh-zz/sqload/driver/mysql"
)

//go:embed sql/*
var content embed.FS

func main() {
	var (
		buf  bytes.Buffer // sql which read from file
		sqls []string // sql after parse
	)

	loader := sqload.New(mysql.Dialector{}) // for PostgreSQL: postgresql.Dialector{}

	if err := loader.Load(&content, &buf); err != nil {
		fmt.Printf("Load error: %s", err.Error())
	}

	if err := loader.Parse(buf.String(), &sqls); err != nil {
		fmt.Printf("Parse error: %s", err.Error())
	}

	fmt.Printf("%+v", sqls)
    // [INSERT INTO table001 (name,age) VALUES ('alice', 10); INSERT INTO table001 (name,age) VALUES ('bob', 20);]
}

Or you can choose to load a file.

main.go

package main

import (
	"bytes"
	"embed"
	"fmt"

	"github.com/uh-zz/sqload"
	"github.com/uh-zz/sqload/driver/mysql"
)

//go:embed sql/*
var content embed.FS

func main() {
	var (
		buf  bytes.Buffer // sql which read from file
		sqls []string // sql after parse
	)

	loader := sqload.New(mysql.Dialector{}) // for PostgreSQL: postgresql.Dialector{}

	if err := loader.LoadFrom(&content, &buf, "sql/test.sql"); err != nil {
		fmt.Printf("Load error: %s", err.Error())
	}

	if err := loader.Parse(buf.String(), &sqls); err != nil {
		fmt.Printf("Parse error: %s", err.Error())
	}

	fmt.Printf("%+v", sqls)
    // [INSERT INTO table001 (name,age) VALUES ('alice', 10);]
}

Contributing

Guidelines are being prepared, but Contributions are welcomed and greatly appreciated.

Todo

  • other dialect

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialector

type Dialector interface {
	Name() string
	Parse(string, *[]string) error
}

type Loader

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

func New

func New(dialector Dialector) Loader

func (Loader) Load

func (l Loader) Load(content *embed.FS, to *bytes.Buffer) error

func (Loader) LoadFrom

func (l Loader) LoadFrom(content *embed.FS, to *bytes.Buffer, fileNames ...string) error

func (Loader) Parse

func (l Loader) Parse(sqlfile string, to *[]string) error

Directories

Path Synopsis
driver

Jump to

Keyboard shortcuts

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