Exemplo n.º 1
0
// CreateTables creates the tables from SQL file at path.
// The file should be named '[dbms]_table.sql'
func (cx *DBContext) CreateTables(path string) error {
	if cx.DB == nil {
		return ConnClosedErr
	}

	return cx.ExectxFile(
		dbutil.GetSQLFilename(path, "table", cx.Conn.GetDBMS()),
		"Create tables",
	)
}
Exemplo n.º 2
0
// Write writes the SQL code to a file got from the one given.
func (s *SQLInfo) Write(dir string) error {
	tmpl := template.Must(template.New("createtb").Parse(tmplCreatetb))
	srcDir, srcFile := filepath.Split(s.SrcFilePath)

	data := tmplData{
		Info: s,
		Header: fmt.Sprintf("# Code generated by getsql from %q\n# DO NOT EDIT",
			filepath.Join(filepath.Base(srcDir), srcFile)),
	}

	createFile := func(path, tmplName string) error {
		file, err := os.Create(path)
		if err != nil {
			return err
		}

		err = tmpl.ExecuteTemplate(file, tmplName, data)
		file.Close()
		if err != nil {
			return err
		}

		if datautil.VERBOSE {
			log.Printf("Write file: '%s'", packer.RelativeDir(path))
		}
		return nil
	}

	// Directory for SQL files
	err := os.MkdirAll(dir, 0775)
	if err != nil && !os.IsExist(err) {
		return err
	}
	// Create the SQL file
	return createFile(dbutil.GetSQLFilename(dir, "table", s.DBMS), "createtb")
}