sqlcmd

package
v0.0.0-...-9c55465 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Conv2StructCmd = &gcli.Command{
	Name:    "struct",
	Aliases: []string{"to-struct", "tostruct", "go-struct"},
	Desc:    "convert create table SQL to Go struct",
	Config: func(c *gcli.Command) {

	},
	Func: func(c *gcli.Command, _ []string) error {
		return errors.New("TODO")
	},
}

Conv2StructCmd convert create table SQL to Go struct

View Source
var Create2Mkdown = &gcli.Command{
	Name:    "to-md",
	Aliases: []string{"tomd", "to-markdown"},
	Desc:    "convert create table SQL to markdown table",
	Config: func(c *gcli.Command) {

	},
	Func: func(c *gcli.Command, _ []string) error {
		return errors.New("TODO")
	},
}
View Source
var Insert2JSONCmd = &gcli.Command{
	Name:    "i2json",
	Aliases: []string{"i2map", "ijson"},
	Desc:    "convert INSERT ROW SQL to JSON object",
	Config: func(c *gcli.Command) {
		c.AddArg("sql", "the insert SQL. allow: @c")
	},
	Func: func(c *gcli.Command, _ []string) error {
		insertSql, err := apputil.ReadSource(c.Arg("sql").String())
		if err != nil {
			return err
		}

		fieldStr, valueStr := strutil.TrimCut(insertSql, " VALUES ")
		if len(valueStr) == 0 {
			return errors.New("not found keywords 'VALUES' in SQL")
		}

		fields := strutil.Split(strutil.Trim(fieldStr, " ()"), ",")
		if len(fields) > 0 {
			_, first := strutil.QuietCut(fields[0], "(")
			fields[0] = first
		}

		fields = arrutil.Map(fields, func(obj string) (val string, ok bool) {
			return strings.Trim(obj, " ` "), true
		})

		values := strutil.Split(strutil.Trim(valueStr, " ();"), ", ")
		anyVals := arrutil.Map(values, func(obj string) (val any, ok bool) {
			obj = strings.TrimSpace(obj)
			if strutil.IsNumeric(obj) {
				return strutil.SafeInt(obj), true
			}
			return strings.Trim(obj, "'\""), true
		})

		mp := arrutil.CombineToMap(fields, anyVals)
		bs, err := jsonutil.EncodePretty(mp)
		if err != nil {
			return err
		}

		fmt.Println(string(bs))
		return nil
	},
}

Insert2JSONCmd convert INSERT SQL to JSON object

View Source
var SQLToolCmd = &gcli.Command{
	Name: "sql",
	Desc: "SQL tools",
	Subs: []*gcli.Command{
		Create2Mkdown,
		Conv2StructCmd,
		Insert2JSONCmd,
		NewCreate2JSONCmd(),
	},
}

SQLToolCmd instance

Functions

func NewCreate2JSONCmd

func NewCreate2JSONCmd() *gcli.Command

NewCreate2JSONCmd convert create table SQL to JSON object

Types

This section is empty.

Jump to

Keyboard shortcuts

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