Skip to content
/ sql2go Public

This repository is working for converting create statement of mysql to go struct.

Notifications You must be signed in to change notification settings

Yimismi/sql2go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sql2go


This repository is adapted from reverse command of go-xorm/cmd(https://github.com/go-xorm/cmd/blob/master/xorm/reverse.go). However go-xorm/cmd generates go code from database-source while this repository converts create-statement of mysql to go struct. You can experience it in http://stming.cn/tool/sql2go.html

Examples

	sql := `
CREATE TABLE IF NOT EXISTS t_person (
  f_age INT(11) NULL,
  f_id INT(11) PRIMARY KEY AUTO_INCREMENT NOT NULL,
  f_name VARCHAR(30) NOT NULL,
  f_sex VARCHAR(2) NULL
  ) ENGINE=InnoDB;
`
	args := sql2go.NewConvertArgs().SetGenJson(true).
		SetPackageName("test").
		SetColPrefix("f_").
		SetTablePrefix("t_")

	code, err := sql2go.FromSql(sql, args)
	if err != nil {
		t.Error(err)
		return
	}
	f, err := os.Create("db_struct3.go")
	if err != nil {
		t.Error(err)
		return
	}
	defer f.Close()
	f.Write(code)

The library can parse multiple Statements and will skip the statement which don't create tables

About

This repository is working for converting create statement of mysql to go struct.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages