예제 #1
0
func (g *generator) runTable() {
	defer log.WhenDone().Info("Stats", "Package", g.tts.Package, "Step", "RunTable")
	type OneTable struct {
		Package          string
		Tick             string
		Name             string
		TableName        string
		Struct           string
		Slice            string
		Table            string
		GoColumns        codegen.Columns
		Columns          csdb.Columns
		MethodRecvPrefix string
		FindByPk         string
	}

	for _, table := range g.tables {

		columns, err := codegen.GetColumns(g.dbrConn.DB, table)
		codegen.LogFatal(err)
		codegen.LogFatal(columns.MapSQLToGoDBRType())

		name := g.getConsistentName(table)
		data := OneTable{
			Package:   g.tts.Package,
			Tick:      "`",
			Name:      name,
			TableName: g.getMagento2TableName(table), // original table name!
			Struct:    TypePrefix + name,             // getTableConstantName
			Slice:     TypePrefix + name + "Slice",   // getTableConstantName
			Table:     table,
			GoColumns: columns,
			Columns:   columns.CopyToCSDB(),
		}

		if data.Columns.PrimaryKeys().Len() > 0 {
			data.FindByPk = "FindBy" + utils.UnderscoreCamelize(data.Columns.PrimaryKeys().JoinFields("_"))
		}

		tplFuncs := template.FuncMap{
			"typePrefix": func(name string) string {
				// if the method already exists in package then add the prefix parent
				// to avoid duplicate function names.
				search := data.Slice + name
				if g.existingMethodSets.has(search) {
					return MethodRecvPrefix + name
				}
				return name
			},
			"findBy":  findBy,
			"dbrType": dbrType,
		}

		g.appendToFile(g.getGenericTemplate(table), data, tplFuncs)
	}
}
예제 #2
0
파일: main.go 프로젝트: bom-d-van/csfw
func generateStructures(tStruct *codegen.TableToStruct, db *sql.DB, dbrConn *dbr.Connection) {
	tplData := &dataContainer{
		Tables:  make([]map[string]interface{}, 0, 200),
		Package: tStruct.Package,
		Tick:    "`",
	}

	tables, err := codegen.GetTables(db, codegen.ReplaceTablePrefix(tStruct.SQLQuery))
	codegen.LogFatal(err)

	if len(tStruct.EntityTypeCodes) > 0 && tStruct.EntityTypeCodes[0] != "" {
		tplData.TypeCodeValueTables, err = codegen.GetEavValueTables(dbrConn, tStruct.EntityTypeCodes)
		codegen.LogFatal(err)

		for _, vTables := range tplData.TypeCodeValueTables {
			for t := range vTables {
				if false == isDuplicate(tables, t) {
					tables = append(tables, t)
				}
			}
		}
	}

	for _, table := range tables {

		columns, err := codegen.GetColumns(db, table)
		codegen.LogFatal(err)
		codegen.LogFatal(columns.MapSQLToGoDBRType())
		var name = table
		if mappedName, ok := codegen.TableMapMagento1To2[strings.Replace(table, codegen.TablePrefix, "", 1)]; ok {
			name = mappedName
		}
		tplData.Tables = append(tplData.Tables, map[string]interface{}{
			"name":    name,
			"table":   table,
			"columns": columns,
		})
	}

	formatted, err := codegen.GenerateCode(tStruct.Package, tplCode, tplData, nil)
	if err != nil {
		fmt.Printf("\n%s\n", formatted)
		codegen.LogFatal(err)
	}

	codegen.LogFatal(ioutil.WriteFile(tStruct.OutputFile, formatted, 0600))
}