Skip to content

arnehormann/dbexplorer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dbexplorer

Scan your database and create code.

See gen.go for an example with administrative queries dumped to the command line.

Exact workings are undocumented so far, but generated code may be used somewhat like this:

// appendMystruct fetches Mystruct values from the database and appends them to dest.
func appendMystruct(dest []*Mystruct, db *sql.DB) []*Mystruct {
	bound := &Mystruct{}

	// Bind() creates an []interface{} for all fields in the struct
	dest := bound.Bind()

	// Query() fetches the sql query used to get struct instances
	// second arg is only used for variable identifiers like schemas, tables, users and hosts	
	query, _ := bound.Query()

	rows, err := db.Query(query)
	if err != nil {
		panic(err) // TODO fix
	}
	defer rows.Close() // TODO check error...

	for rows.Next() {
		err = rows.Scan(dest...)
		if err != nil {
			panic(err) // TODO fix
		}

		// copy() creates a shallow copy of the bound struct.
		// Doing it this way means the interface slice has to be created only once.
		dest = append(dest, bound.copy())
	}
	if err = rows.Err(); err != nil {
		panic(err) // TODO fix
	}
	return dest
}

About

Scan your database and generate access code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages