Categorygithub.com/codeindex2937/ddlcode
modulepackage
0.0.0-20240901153315-4bae21b3540e
Repository: https://github.com/codeindex2937/ddlcode.git
Documentation: pkg.go.dev

# README

ddlcode

Generate code from Oracle DDL

Support

CREATE TABLE ADD CONSTRAINT ... FOREIGN KEY ... REFERENCES ...

Reference

sql2code go-sqlparser

Example Code


func main() {

	sql := `CREATE TABLE TBL (
		ID1 INTEGER NOT NULL,
		ID2 INTEGER NOT NULL,
		CREATED TIMESTAMP WITH TIME ZONE DEFAULT 0,
		TEXT VARCHAR(1),
		CONSTRAINT PK PRIMARY KEY (ID1, ID2)
	);
	CREATE TABLE TBL2 (
		ID3 INTEGER NOT NULL,
		ID4 INTEGER NOT NULL,
		TEXT BLOB UNIQUE,
		CONSTRAINT PK PRIMARY KEY (ID3, ID4)
	);
	ALTER TABLE TBL2 ADD CONSTRAINT fk_name FOREIGN KEY (ID1,ID2) REFERENCES TBL(ID3,ID4);`

	tables := ddlcode.Parse(sql)

	generateDrawio(tables)
	generateGorm(tables)
	generateJavaCode(tables)
}

func generateDrawio(tables []*ddlcode.Table) {
	config := ddlcode.GetDefaultDrawioConfig()
	config.ExportPath = "codegen.drawio"
	config.Tables = tables
	config.Width = 1100
	config.Height = 850
	file, err := ddlcode.GenerateDrawio(config)
	if err != nil {
		log.Fatal(err)
	}
	file.Flush()
}

func generateGorm(tables []*ddlcode.Table) {
	config := ddlcode.GetDefaultGormConfig()
	config.Package = "model"

	for _, config.Table = range tables {
		files, err := ddlcode.GenerateGorm(config)
		if err != nil {
			log.Fatal(err)
		}

		for _, f := range files {
			dirPath := filepath.Dir(f.Path)
			if _, err := os.Stat(dirPath); os.IsNotExist(err) {
				if err := os.Mkdir(dirPath, 0750); err != nil {
					log.Fatal(err)
				}
			}
			if err := f.Flush(); err != nil {
				log.Fatal(err)
			}
		}
	}
}

func generateJavaCode(tables []*ddlcode.Table) {
	config := ddlcode.GetDefaultJavaConfig()
	config.Package = "com.codegen"
	config.Schema = "schema"

	for _, config.Table = range tables {
		files, err := ddlcode.GenerateJava(config)
		if err != nil {
			log.Fatal(err)
		}

		for _, f := range files {
			dirPath := filepath.Dir(f.Path)
			if _, err := os.Stat(dirPath); os.IsNotExist(err) {
				if err := os.Mkdir(dirPath, 0750); err != nil {
					log.Fatal(err)
				}
			}
			if err := f.Flush(); err != nil {
				log.Fatal(err)
			}
		}
	}
}

# Packages

No description provided by the author
No description provided by the author
ref: github.com/stevenle/topsort ref: github.com/oko/toposort.

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author